]> icculus.org git repositories - divverent/netradiant.git/blob - contrib/bobtoolz/misc.cpp
fix two other bugs in the same function
[divverent/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
135   std::size_t l = strlen(buf);
136         char* out = buf2;
137         for (int 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         const 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 (POSIX)
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 #ifdef WIN32
219
220 #include <windows.h>
221
222 bool Q_Exec( const char *pCmd, bool bCreateConsole )
223 {
224         // G_DeWan: Don't know if this is needed for linux version
225
226         PROCESS_INFORMATION pi;
227         STARTUPINFO si = {0};            // Initialize all members to zero
228         si.cb = sizeof(STARTUPINFO);     // Set byte count
229   DWORD dwCreationFlags;
230
231   if (bCreateConsole)
232     dwCreationFlags = CREATE_NEW_CONSOLE | NORMAL_PRIORITY_CLASS;
233   else
234     dwCreationFlags = DETACHED_PROCESS | NORMAL_PRIORITY_CLASS;
235
236         for(; *pCmd == ' '; pCmd++);
237
238         if(!CreateProcess(NULL, (char *)pCmd, NULL, NULL, false, dwCreationFlags, NULL, NULL, &si, &pi))
239                 return false;
240
241   return true;
242 }
243 #endif
244
245 void StartBSP()
246 {
247         char exename[256];
248         GetFilename(exename, "q3map");
249         UnixToDosPath(exename); // do we want this done in linux version?
250
251         char mapname[256];  
252   const char *pn = GlobalRadiant().getMapsPath();
253   
254         strcpy( mapname, pn );
255         strcat( mapname, "/ac_prt.map" );
256         UnixToDosPath(mapname);
257
258         char command[1024];
259         sprintf(command, "%s -nowater -fulldetail %s", exename, mapname);
260
261         Q_Exec( command, true );
262 }
263
264 class EntityWriteMiniPrt
265 {
266         mutable DEntity world;
267   FILE* pFile;
268   std::list<Str>* exclusionList;
269 public:
270   EntityWriteMiniPrt(FILE* pFile, std::list<Str>* exclusionList)
271     : pFile(pFile), exclusionList(exclusionList)
272   {
273   }
274   void operator()(scene::Instance& instance) const
275   {
276                 const char* classname = Node_getEntity(instance.path().top())->getKeyValue("classname");
277
278                 if(!strcmp(classname, "worldspawn"))
279                 {
280                         world.LoadFromEntity(instance.path().top(), false);
281                         world.RemoveNonCheckBrushes(exclusionList, true);
282                         world.SaveToFile(pFile);
283                 }
284                 else if(strstr(classname, "info_"))
285                 {
286                         world.ClearBrushes();
287                         world.ClearEPairs();
288                         world.LoadEPairList(Node_getEntity(instance.path().top()));
289                         world.SaveToFile(pFile);
290                 }
291   }
292 };
293  
294 void BuildMiniPrt(std::list<Str>* exclusionList)
295 {
296         // yes, we could just use -fulldetail option, but, as SPOG said
297         // it'd be faster without all the hint, donotenter etc textures and
298         // doors, etc
299
300         
301         
302         char buffer[128];
303   const char *pn = GlobalRadiant().getMapsPath();
304
305         strcpy( buffer, pn );
306         strcat( buffer, "/ac_prt.map" );
307         FILE* pFile = fopen(buffer, "w");
308
309         // ahem, thx rr2
310         if(!pFile)
311                 return;
312
313   Scene_forEachEntity(EntityWriteMiniPrt(pFile, exclusionList));
314
315         fclose(pFile);
316
317         StartBSP();
318 }
319
320 class EntityFindByTargetName
321 {
322   const char* targetname;
323 public:
324         mutable const scene::Path* result;
325   EntityFindByTargetName(const char* targetname)
326     : targetname(targetname), result(0)
327   {
328   }
329   void operator()(scene::Instance& instance) const
330   {
331     if(result == 0)
332     {
333                   const char* value = Node_getEntity(instance.path().top())->getKeyValue("targetname");
334
335                   if(!strcmp(value, targetname))
336                   {
337                           result = &instance.path();
338                   }
339     }
340   }
341 };
342  
343 const scene::Path* FindEntityFromTargetname(const char* targetname)
344 {
345   return Scene_forEachEntity(EntityFindByTargetName(targetname)).result;
346 }
347
348 void FillDefaultTexture(_QERFaceData* faceData, vec3_t va, vec3_t vb, vec3_t vc, const char* texture)
349 {
350         faceData->m_texdef.rotate = 0;
351         faceData->m_texdef.scale[0] = 0.5;
352         faceData->m_texdef.scale[1] = 0.5;
353         faceData->m_texdef.shift[0] = 0;
354         faceData->m_texdef.shift[1] = 0;
355         faceData->contents = 0;
356         faceData->flags = 0;
357         faceData->value = 0;
358         if(*texture)
359                 faceData->m_shader = texture;
360         else
361                 faceData->m_shader = "textures/common/caulk";
362         VectorCopy(va, faceData->m_p0);
363         VectorCopy(vb, faceData->m_p1);
364         VectorCopy(vc, faceData->m_p2);
365 }
366
367 float Determinant3x3(float a1, float a2, float a3,
368                                          float b1, float b2, float b3,
369                                          float c1, float c2, float c3)
370 {
371         return a1*(b2*c3-b3*c2) - a2*(b1*c3-b3*c1) + a3*(b1*c2-b2*c1);
372 }
373
374 bool GetEntityCentre(const char* entity, vec3_t centre)
375 {
376   const scene::Path* ent = FindEntityFromTargetname(entity);
377         if(!ent)
378                 return false;
379
380   scene::Instance& instance = *GlobalSceneGraph().find(*ent);
381   VectorCopy(instance.worldAABB().origin, centre);
382
383         return true;
384 }
385
386 vec_t Min(vec_t a, vec_t b)
387 {
388         if(a < b)
389                 return a;
390         return b;
391 }
392
393 void MakeNormal( const vec_t* va, const vec_t* vb, const vec_t* vc, vec_t* out ) {
394         vec3_t v1, v2;
395         VectorSubtract(va, vb, v1);
396         VectorSubtract(vc, vb, v2);
397         CrossProduct(v1, v2, out);
398 }
399
400 char* GetFilename(char* buffer, const char* filename) {
401         strcpy(buffer, GlobalRadiant().getAppPath());
402         strcat(buffer, "plugins/");
403         strcat(buffer, filename);
404         return buffer;
405 }
406