e56c4d89129056299a27bba921889c80733e1c00
[xonotic/netradiant.git] / contrib / bobtoolz / misc.cpp
1 /*
2 BobToolz plugin for GtkRadiant
3 Copyright (C) 2001 Gordon Biggans
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 #include "misc.h"
21
22 #include <list>
23 #include "str.h"
24
25 #include "DPoint.h"
26 #include "DPlane.h"
27 #include "DBrush.h"
28 #include "DEPair.h"
29 #include "DPatch.h"
30 #include "DEntity.h"
31
32 #include "funchandlers.h"
33
34 #ifdef __linux__
35 #include <sys/types.h>
36 #include <unistd.h>
37 #endif
38
39 #include "iundo.h"
40 #include "qerplugin.h"
41
42 #include <vector>
43 #include <list>
44 #include <map>
45 #include <algorithm>
46
47 #include "scenelib.h"
48
49 /*==========================
50                 Global Vars
51 ==========================*/
52
53 //HANDLE bsp_process;
54 char    g_CurrentTexture[256] = "";
55
56 //=============================================================
57 //=============================================================
58
59 void ReadCurrentTexture()
60 {
61         const char* textureName = GlobalRadiant().TextureBrowser_getSelectedShader();
62         strcpy(g_CurrentTexture, textureName);
63 }
64
65 const char*  GetCurrentTexture()
66 {
67         ReadCurrentTexture();
68         return g_CurrentTexture;
69 }
70
71 void MoveBlock(int dir, vec3_t min, vec3_t max, float dist)
72 {
73         switch(dir)
74         {
75                 case MOVE_EAST:
76                 {
77                         min[0]+=dist;
78                         max[0]+=dist;
79                         break;
80                 }
81                 case MOVE_WEST:
82                 {
83                         min[0]-=dist;
84                         max[0]-=dist;
85                         break;
86                 }
87                 case MOVE_NORTH:
88                 {
89                         min[1]+=dist;
90                         max[1]+=dist;
91                         break;
92                 }
93                 case MOVE_SOUTH:
94                 {
95                         min[1]-=dist;
96                         max[1]-=dist;
97                         break;
98                 }
99         }
100 }
101
102 void SetInitialStairPos(int dir, vec3_t min, vec3_t max, float width)
103 {
104         switch(dir)
105         {
106                 case MOVE_EAST:
107                 {
108                         max[0] = min[0] + width;
109                         break;
110                 }
111                 case MOVE_WEST:
112                 {
113                         min[0] = max[0] - width;
114                         break;
115                 }
116                 case MOVE_NORTH:
117                 {
118                         max[1] = min[1] + width;
119                         break;
120                 }
121                 case MOVE_SOUTH:
122                 {
123                         min[1] = max[1] - width;
124                         break;
125                 }
126         }
127 }
128
129 char* TranslateString (char *buf)
130 {
131         static  char    buf2[32768];
132         int             i, l;
133         char    *out;
134
135         l = strlen(buf);
136         out = buf2;
137         for (i=0 ; i<l ; i++)
138         {
139                 if (buf[i] == '\n')
140                 {
141                         *out++ = '\r';
142                         *out++ = '\n';
143                 }
144                 else
145                         *out++ = buf[i];
146         }
147         *out++ = 0;
148
149         return buf2;
150 }
151
152
153 char* UnixToDosPath(char* path)
154 {
155 #ifndef WIN32
156         return path;
157 #else
158         for(char* p = path; *p; p++)
159         {
160                 if(*p == '/')
161                         *p = '\\';
162         }
163         return path;
164 #endif
165 }
166
167 const char* ExtractFilename(const char* path)
168 {
169         char* p = strrchr(path, '/');
170         if(!p)
171         {
172                 p = strrchr(path, '\\');
173
174                 if(!p)
175                         return path;
176         }
177         return ++p;
178 }
179
180 extern char* PLUGIN_NAME;
181 /*char* GetGameFilename(char* buffer, const char* filename)
182 {
183         strcpy(buffer, g_FuncTable.m_pfnGetGamePath());
184         char* p = strrchr(buffer, '/');
185         *++p = '\0';
186         strcat(buffer, filename);
187         buffer = UnixToDosPath(buffer);
188         return buffer;
189 }*/
190
191 #if defined (__linux__) || defined (__APPLE__)
192 // the bCreateConsole parameter is ignored on linux ..
193 bool Q_Exec( const char *pCmd, bool bCreateConsole )
194 {
195         switch (fork())
196     {
197         case -1:
198       return false;
199 //      Error ("CreateProcess failed");
200       break;
201     case 0:
202 #ifdef _DEBUG
203                   printf("Running system...\n");
204                         printf("Command: %s\n", pCmd);
205 #endif
206                         // NOTE: we could use that to detect when a step finishes. But then it
207                         // would not work for remote compiling stuff.
208 //      execlp (pCmd, pCmd, NULL);
209                         system( pCmd );
210       printf ("system() returned");
211       _exit (0);
212       break;
213     }
214     return true;
215 }
216 #endif
217
218 #include <windows.h>
219
220 #ifdef WIN32
221
222 #include <windows.h>
223
224 bool Q_Exec( const char *pCmd, bool bCreateConsole )
225 {
226         // G_DeWan: Don't know if this is needed for linux version
227
228         PROCESS_INFORMATION pi;
229         STARTUPINFO si = {0};            // Initialize all members to zero
230         si.cb = sizeof(STARTUPINFO);     // Set byte count
231   DWORD dwCreationFlags;
232
233   if (bCreateConsole)
234     dwCreationFlags = CREATE_NEW_CONSOLE | NORMAL_PRIORITY_CLASS;
235   else
236     dwCreationFlags = DETACHED_PROCESS | NORMAL_PRIORITY_CLASS;
237
238         for(; *pCmd == ' '; pCmd++);
239
240         if(!CreateProcess(NULL, (char *)pCmd, NULL, NULL, FALSE, dwCreationFlags, NULL, NULL, &si, &pi))
241                 return false;
242
243   return true;
244 }
245 #endif
246
247 void StartBSP()
248 {
249         char exename[256];
250         GetFilename(exename, "q3map");
251         UnixToDosPath(exename); // do we want this done in linux version?
252
253         char mapname[256];  
254   const char *pn = g_FuncTable.m_pfnReadProjectKey("mapspath");
255   
256         strcpy( mapname, pn );
257         strcat( mapname, "/ac_prt.map" );
258         UnixToDosPath(mapname);
259
260         char command[1024];
261         sprintf(command, "%s -nowater -fulldetail %s", exename, mapname);
262
263         Q_Exec( command, TRUE );
264 }
265
266 void BuildMiniPrt(std::list<Str>* exclusionList)
267 {
268         // yes, we could just use -fulldetail option, but, as SPOG said
269         // it'd be faster without all the hint, donotenter etc textures and
270         // doors, etc
271
272         DEntity world;
273         
274         char buffer[128];
275   const char *pn = g_FuncTable.m_pfnReadProjectKey("mapspath");
276
277         strcpy( buffer, pn );
278         strcat( buffer, "/ac_prt.map" );
279         FILE* pFile = fopen(buffer, "w");
280
281         // ahem, thx rr2
282         if(!pFile)
283                 return;
284
285 #if 0
286   int count = g_FuncTable.m_pfnGetEntityCount();
287         for(int i = 0; i < count; i++)
288         {
289                 entity_t* ent = (entity_t*)g_FuncTable.m_pfnGetEntityHandle(i);
290
291                 epair_t* epl = *g_EntityTable.m_pfnGetEntityKeyValList(ent);
292
293                 epair_t* ep = epl;
294                 while(ep)
295                 {
296                         if(!strcmp(ep->key, "classname"))
297                         {
298                                 if(!strcmp(ep->value, "worldspawn"))
299                                 {
300                                         world.LoadFromEntity(i, FALSE);
301                                         world.RemoveNonCheckBrushes(exclusionList, TRUE);
302                                         world.SaveToFile(pFile);
303                                 }
304                                 else if(strstr(ep->value, "info_"))
305                                 {
306                                         world.ClearBrushes();
307                                         world.ClearEPairs();
308                                         world.LoadEPairList(epl);
309                                         world.SaveToFile(pFile);
310                                 }
311                                 break;
312                         }
313
314                         ep = ep->next;
315                 }
316         }
317 #endif
318
319         fclose(pFile);
320
321         StartBSP();
322 }
323
324 template<typename Functor>
325 class EntityWalker
326 {
327   const Functor& functor;
328 public:
329   EntityWalker(const Functor& functor) : functor(functor)
330   {
331   }
332   bool pre(const Path& path, Instance& instance) const
333   {
334     if(Node_isEntity(path.top()))
335     {
336       functor(path.top());
337     }
338   }
339 };
340
341 template<typename Functor>
342 const Functor& Scene_forEachEntity(const Functor& functor)
343 {
344   GlobalSceneGraph().traverse(EntityWalker<Functor>(functor));
345 }
346
347 void 
348
349 scene::Path* FindEntityFromTargetname(const char* targetname, int* entNum)
350 {
351 #if 0
352         DEntity world;
353
354         int count = g_FuncTable.m_pfnGetEntityCount();
355         for(int i = 0; i < count; i++)
356         {
357                 world.ClearEPairs();
358
359                 entity_s* ent = (entity_s*)g_FuncTable.m_pfnGetEntityHandle(i);
360
361                 world.LoadEPairList(*g_EntityTable.m_pfnGetEntityKeyValList(ent));
362
363                 DEPair* tn = world.FindEPairByKey("targetname");
364                 if(tn)
365                 {
366                         if(string_equal_nocase(tn->value, targetname)) {
367                                 if(entNum) {
368                                         *entNum = i;
369                                 }
370                                 return ent;
371                         }
372                 }
373         }
374
375 #endif
376         return NULL;
377 }
378
379 void FillDefaultTexture(_QERFaceData* faceData, vec3_t va, vec3_t vb, vec3_t vc, const char* texture)
380 {
381         faceData->m_texdef.rotate = 0;
382         faceData->m_texdef.scale[0] = 0.5;
383         faceData->m_texdef.scale[1] = 0.5;
384         faceData->m_texdef.shift[0] = 0;
385         faceData->m_texdef.shift[1] = 0;
386         faceData->m_texdef.contents = 0;
387         faceData->m_texdef.flags = 0;
388         faceData->m_texdef.value = 0;
389         if(*texture)
390                 faceData->m_shader = texture;
391         else
392                 faceData->m_shader = "textures/common/caulk";
393         VectorCopy(va, faceData->m_p0);
394         VectorCopy(vb, faceData->m_p1);
395         VectorCopy(vc, faceData->m_p2);
396 }
397
398 float Determinant3x3(float a1, float a2, float a3,
399                                          float b1, float b2, float b3,
400                                          float c1, float c2, float c3)
401 {
402         return a1*(b2*c3-b3*c2) - a2*(b1*c3-b3*c1) + a3*(b1*c2-b2*c1);
403 }
404
405 bool GetEntityCentre(const char* entity, vec3_t centre)
406 {
407   const scene::Path* ent = FindEntityFromTargetname(entity, NULL);
408         if(!ent)
409                 return FALSE;
410
411   scene::Instance& instance = *GlobalSceneGraph().find(*ent);
412   VectorCopy(instance.aabb_world().origin, centre);
413
414         return TRUE;
415 }
416
417 vec_t Min(vec_t a, vec_t b)
418 {
419         if(a < b)
420                 return a;
421         return b;
422 }
423
424 void MakeNormal( const vec_t* va, const vec_t* vb, const vec_t* vc, vec_t* out ) {
425         vec3_t v1, v2;
426         VectorSubtract(va, vb, v1);
427         VectorSubtract(vc, vb, v2);
428         CrossProduct(v1, v2, out);
429 }
430
431 char* GetFilename(char* buffer, const char* filename) {
432         strcpy(buffer, g_pSynapseServer->GetModuleFilename(&g_SynapseClient));
433         StripFilename( buffer );
434         strcat(buffer, "/");
435         strcat(buffer, filename);
436         //buffer = UnixToDosPath(buffer);
437         return buffer;
438 }
439