]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bobtoolz/misc.cpp
portability fixes
[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 "ientity.h"
41 #include "iscenegraph.h"
42 #include "qerplugin.h"
43
44 #include <vector>
45 #include <list>
46 #include <map>
47 #include <algorithm>
48
49 #include "scenelib.h"
50
51 /*==========================
52                 Global Vars
53 ==========================*/
54
55 //HANDLE bsp_process;
56 char    g_CurrentTexture[256] = "";
57
58 //=============================================================
59 //=============================================================
60
61 void ReadCurrentTexture()
62 {
63         const char* textureName = GlobalRadiant().TextureBrowser_getSelectedShader();
64         strcpy(g_CurrentTexture, textureName);
65 }
66
67 const char*  GetCurrentTexture()
68 {
69         ReadCurrentTexture();
70         return g_CurrentTexture;
71 }
72
73 void MoveBlock(int dir, vec3_t min, vec3_t max, float dist)
74 {
75         switch(dir)
76         {
77                 case MOVE_EAST:
78                 {
79                         min[0]+=dist;
80                         max[0]+=dist;
81                         break;
82                 }
83                 case MOVE_WEST:
84                 {
85                         min[0]-=dist;
86                         max[0]-=dist;
87                         break;
88                 }
89                 case MOVE_NORTH:
90                 {
91                         min[1]+=dist;
92                         max[1]+=dist;
93                         break;
94                 }
95                 case MOVE_SOUTH:
96                 {
97                         min[1]-=dist;
98                         max[1]-=dist;
99                         break;
100                 }
101         }
102 }
103
104 void SetInitialStairPos(int dir, vec3_t min, vec3_t max, float width)
105 {
106         switch(dir)
107         {
108                 case MOVE_EAST:
109                 {
110                         max[0] = min[0] + width;
111                         break;
112                 }
113                 case MOVE_WEST:
114                 {
115                         min[0] = max[0] - width;
116                         break;
117                 }
118                 case MOVE_NORTH:
119                 {
120                         max[1] = min[1] + width;
121                         break;
122                 }
123                 case MOVE_SOUTH:
124                 {
125                         min[1] = max[1] - width;
126                         break;
127                 }
128         }
129 }
130
131 char* TranslateString (char *buf)
132 {
133         static  char    buf2[32768];
134         int             i, l;
135         char    *out;
136
137         l = strlen(buf);
138         out = buf2;
139         for (i=0 ; i<l ; i++)
140         {
141                 if (buf[i] == '\n')
142                 {
143                         *out++ = '\r';
144                         *out++ = '\n';
145                 }
146                 else
147                         *out++ = buf[i];
148         }
149         *out++ = 0;
150
151         return buf2;
152 }
153
154
155 char* UnixToDosPath(char* path)
156 {
157 #ifndef WIN32
158         return path;
159 #else
160         for(char* p = path; *p; p++)
161         {
162                 if(*p == '/')
163                         *p = '\\';
164         }
165         return path;
166 #endif
167 }
168
169 const char* ExtractFilename(const char* path)
170 {
171         char* p = strrchr(path, '/');
172         if(!p)
173         {
174                 p = strrchr(path, '\\');
175
176                 if(!p)
177                         return path;
178         }
179         return ++p;
180 }
181
182 extern char* PLUGIN_NAME;
183 /*char* GetGameFilename(char* buffer, const char* filename)
184 {
185         strcpy(buffer, g_FuncTable.m_pfnGetGamePath());
186         char* p = strrchr(buffer, '/');
187         *++p = '\0';
188         strcat(buffer, filename);
189         buffer = UnixToDosPath(buffer);
190         return buffer;
191 }*/
192
193 #if defined (POSIX)
194 // the bCreateConsole parameter is ignored on linux ..
195 bool Q_Exec( const char *pCmd, bool bCreateConsole )
196 {
197         switch (fork())
198     {
199         case -1:
200       return false;
201 //      Error ("CreateProcess failed");
202       break;
203     case 0:
204 #ifdef _DEBUG
205                   printf("Running system...\n");
206                         printf("Command: %s\n", pCmd);
207 #endif
208                         // NOTE: we could use that to detect when a step finishes. But then it
209                         // would not work for remote compiling stuff.
210 //      execlp (pCmd, pCmd, NULL);
211                         system( pCmd );
212       printf ("system() returned");
213       _exit (0);
214       break;
215     }
216     return true;
217 }
218 #endif
219
220 #include <windows.h>
221
222 #ifdef WIN32
223
224 #include <windows.h>
225
226 bool Q_Exec( const char *pCmd, bool bCreateConsole )
227 {
228         // G_DeWan: Don't know if this is needed for linux version
229
230         PROCESS_INFORMATION pi;
231         STARTUPINFO si = {0};            // Initialize all members to zero
232         si.cb = sizeof(STARTUPINFO);     // Set byte count
233   DWORD dwCreationFlags;
234
235   if (bCreateConsole)
236     dwCreationFlags = CREATE_NEW_CONSOLE | NORMAL_PRIORITY_CLASS;
237   else
238     dwCreationFlags = DETACHED_PROCESS | NORMAL_PRIORITY_CLASS;
239
240         for(; *pCmd == ' '; pCmd++);
241
242         if(!CreateProcess(NULL, (char *)pCmd, NULL, NULL, FALSE, dwCreationFlags, NULL, NULL, &si, &pi))
243                 return false;
244
245   return true;
246 }
247 #endif
248
249 void StartBSP()
250 {
251         char exename[256];
252         GetFilename(exename, "q3map");
253         UnixToDosPath(exename); // do we want this done in linux version?
254
255         char mapname[256];  
256   const char *pn = GlobalRadiant().getMapsPath();
257   
258         strcpy( mapname, pn );
259         strcat( mapname, "/ac_prt.map" );
260         UnixToDosPath(mapname);
261
262         char command[1024];
263         sprintf(command, "%s -nowater -fulldetail %s", exename, mapname);
264
265         Q_Exec( command, TRUE );
266 }
267
268 class EntityWriteMiniPrt
269 {
270         mutable DEntity world;
271   FILE* pFile;
272   std::list<Str>* exclusionList;
273 public:
274   EntityWriteMiniPrt(FILE* pFile, std::list<Str>* exclusionList)
275     : pFile(pFile), exclusionList(exclusionList)
276   {
277   }
278   void operator()(scene::Instance& instance) const
279   {
280                 const char* classname = Node_getEntity(instance.path().top())->getKeyValue("classname");
281
282                 if(!strcmp(classname, "worldspawn"))
283                 {
284                         world.LoadFromEntity(instance.path().top(), FALSE);
285                         world.RemoveNonCheckBrushes(exclusionList, TRUE);
286                         world.SaveToFile(pFile);
287                 }
288                 else if(strstr(classname, "info_"))
289                 {
290                         world.ClearBrushes();
291                         world.ClearEPairs();
292                         world.LoadEPairList(Node_getEntity(instance.path().top()));
293                         world.SaveToFile(pFile);
294                 }
295   }
296 };
297  
298 void BuildMiniPrt(std::list<Str>* exclusionList)
299 {
300         // yes, we could just use -fulldetail option, but, as SPOG said
301         // it'd be faster without all the hint, donotenter etc textures and
302         // doors, etc
303
304         
305         
306         char buffer[128];
307   const char *pn = GlobalRadiant().getMapsPath();
308
309         strcpy( buffer, pn );
310         strcat( buffer, "/ac_prt.map" );
311         FILE* pFile = fopen(buffer, "w");
312
313         // ahem, thx rr2
314         if(!pFile)
315                 return;
316
317   Scene_forEachEntity(EntityWriteMiniPrt(pFile, exclusionList));
318
319         fclose(pFile);
320
321         StartBSP();
322 }
323
324 class EntityFindByTargetName
325 {
326   const char* targetname;
327 public:
328         mutable const scene::Path* result;
329   EntityFindByTargetName(const char* targetname)
330     : targetname(targetname), result(0)
331   {
332   }
333   void operator()(scene::Instance& instance) const
334   {
335     if(result == 0)
336     {
337                   const char* value = Node_getEntity(instance.path().top())->getKeyValue("targetname");
338
339                   if(!strcmp(value, targetname))
340                   {
341                           result = &instance.path();
342                   }
343     }
344   }
345 };
346  
347 const scene::Path* FindEntityFromTargetname(const char* targetname)
348 {
349   return Scene_forEachEntity(EntityFindByTargetName(targetname)).result;
350 }
351
352 void FillDefaultTexture(_QERFaceData* faceData, vec3_t va, vec3_t vb, vec3_t vc, const char* texture)
353 {
354         faceData->m_texdef.rotate = 0;
355         faceData->m_texdef.scale[0] = 0.5;
356         faceData->m_texdef.scale[1] = 0.5;
357         faceData->m_texdef.shift[0] = 0;
358         faceData->m_texdef.shift[1] = 0;
359         faceData->contents = 0;
360         faceData->flags = 0;
361         faceData->value = 0;
362         if(*texture)
363                 faceData->m_shader = texture;
364         else
365                 faceData->m_shader = "textures/common/caulk";
366         VectorCopy(va, faceData->m_p0);
367         VectorCopy(vb, faceData->m_p1);
368         VectorCopy(vc, faceData->m_p2);
369 }
370
371 float Determinant3x3(float a1, float a2, float a3,
372                                          float b1, float b2, float b3,
373                                          float c1, float c2, float c3)
374 {
375         return a1*(b2*c3-b3*c2) - a2*(b1*c3-b3*c1) + a3*(b1*c2-b2*c1);
376 }
377
378 bool GetEntityCentre(const char* entity, vec3_t centre)
379 {
380   const scene::Path* ent = FindEntityFromTargetname(entity);
381         if(!ent)
382                 return FALSE;
383
384   scene::Instance& instance = *GlobalSceneGraph().find(*ent);
385   VectorCopy(instance.worldAABB().origin, centre);
386
387         return TRUE;
388 }
389
390 vec_t Min(vec_t a, vec_t b)
391 {
392         if(a < b)
393                 return a;
394         return b;
395 }
396
397 void MakeNormal( const vec_t* va, const vec_t* vb, const vec_t* vc, vec_t* out ) {
398         vec3_t v1, v2;
399         VectorSubtract(va, vb, v1);
400         VectorSubtract(vc, vb, v2);
401         CrossProduct(v1, v2, out);
402 }
403
404 char* GetFilename(char* buffer, const char* filename) {
405         strcpy(buffer, GlobalRadiant().getAppPath());
406         strcat(buffer, "plugins/");
407         strcat(buffer, filename);
408         return buffer;
409 }
410