]> icculus.org git repositories - divverent/netradiant.git/blob - tools/quake3/q3map2/main.c
make -minmax work
[divverent/netradiant.git] / tools / quake3 / q3map2 / main.c
1 /* -------------------------------------------------------------------------------
2
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6 This file is part of GtkRadiant.
7
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 GtkRadiant is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21
22 -------------------------------------------------------------------------------
23
24 This code has been altered significantly from its original form, to support
25 several games based on the Quake III Arena engine, in the form of "Q3Map2."
26
27 ------------------------------------------------------------------------------- */
28
29
30
31 /* marker */
32 #define MAIN_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /*
42 Random()
43 returns a pseudorandom number between 0 and 1
44 */
45
46 vec_t Random( void )
47 {
48         return (vec_t) rand() / RAND_MAX;
49 }
50
51
52
53 /*
54 ExitQ3Map()
55 cleanup routine
56 */
57
58 static void ExitQ3Map( void )
59 {
60         BSPFilesCleanup();
61         if( mapDrawSurfs != NULL )
62                 free( mapDrawSurfs );
63 }
64
65
66
67 /* minimap stuff */
68
69 /* borrowed from light.c */
70 void WriteTGA24( char *filename, byte *data, int width, int height, qboolean flip );
71 typedef struct minimap_s
72 {
73         bspModel_t *model;
74         int width;
75         int height;
76         int samples;
77         float *sample_offsets;
78         float sharpen_boxmult;
79         float sharpen_centermult;
80         float *data1f;
81         float *sharpendata1f;
82         vec3_t mins, size;
83 }
84 minimap_t;
85 static minimap_t minimap;
86
87 qboolean BrushIntersectionWithLine(bspBrush_t *brush, vec3_t start, vec3_t dir, float *t_in, float *t_out)
88 {
89         int i;
90         qboolean in = qfalse, out = qfalse;
91         bspBrushSide_t *sides = &bspBrushSides[brush->firstSide];
92
93         for(i = 0; i < brush->numSides; ++i)
94         {
95                 bspPlane_t *p = &bspPlanes[sides[i].planeNum];
96                 float sn = DotProduct(start, p->normal);
97                 float dn = DotProduct(dir, p->normal);
98                 if(dn == 0)
99                 {
100                         if(sn > p->dist)
101                                 return qfalse; // outside!
102                 }
103                 else
104                 {
105                         float t = (p->dist - sn) / dn;
106                         if(dn < 0)
107                         {
108                                 if(!in || t > *t_in)
109                                 {
110                                         *t_in = t;
111                                         in = qtrue;
112                                         // as t_in can only increase, and t_out can only decrease, early out
113                                         if(out && *t_in >= *t_out)
114                                                 return qfalse;
115                                 }
116                         }
117                         else
118                         {
119                                 if(!out || t < *t_out)
120                                 {
121                                         *t_out = t;
122                                         out = qtrue;
123                                         // as t_in can only increase, and t_out can only decrease, early out
124                                         if(in && *t_in >= *t_out)
125                                                 return qfalse;
126                                 }
127                         }
128                 }
129         }
130         return in && out;
131 }
132
133 static float MiniMapSample(float x, float y)
134 {
135         vec3_t org, dir;
136         int i, bi;
137         float t0, t1;
138         float samp;
139         bspBrush_t *b;
140         int cnt;
141
142         org[0] = x;
143         org[1] = y;
144         org[2] = 0;
145         dir[0] = 0;
146         dir[1] = 0;
147         dir[2] = 1;
148
149         cnt = 0;
150         samp = 0;
151         for(i = 0; i < minimap.model->numBSPBrushes; ++i)
152         {
153                 bi = minimap.model->firstBSPBrush + i;
154                 if(opaqueBrushes[bi >> 3] & (1 << (bi & 7)))
155                 {
156                         b = &bspBrushes[bi];
157
158                         // sort out mins/maxs of the brush
159                         bspBrushSide_t *s = &bspBrushSides[b->firstSide];
160                         if(x < -bspPlanes[s[0].planeNum].dist)
161                                 continue;
162                         if(x > +bspPlanes[s[1].planeNum].dist)
163                                 continue;
164                         if(y < -bspPlanes[s[2].planeNum].dist)
165                                 continue;
166                         if(y > +bspPlanes[s[3].planeNum].dist)
167                                 continue;
168
169                         if(BrushIntersectionWithLine(b, org, dir, &t0, &t1))
170                         {
171                                 samp += t1 - t0;
172                                 ++cnt;
173                         }
174                 }
175         }
176
177         return samp;
178 }
179
180 static void MiniMapRandomlySupersampled(int y)
181 {
182         int x, i;
183         float *p = &minimap.data1f[y * minimap.width];
184         float ymin = minimap.mins[1] + minimap.size[1] * (y / (float) minimap.height);
185         float dx   =                   minimap.size[0]      / (float) minimap.width;
186         float dy   =                   minimap.size[1]      / (float) minimap.height;
187
188         for(x = 0; x < minimap.width; ++x)
189         {
190                 float xmin = minimap.mins[0] + minimap.size[0] * (x / (float) minimap.width);
191                 float val = 0;
192
193                 for(i = 0; i < minimap.samples; ++i)
194                 {
195                         float thisval = MiniMapSample(
196                                 xmin + (2 * Random() - 0.5) * dx, /* exaggerated random pattern for better results */
197                                 ymin + (2 * Random() - 0.5) * dy  /* exaggerated random pattern for better results */
198                         );
199                         val += thisval;
200                 }
201                 val /= minimap.samples * minimap.size[2];
202                 *p++ = val;
203         }
204 }
205
206 static void MiniMapSupersampled(int y)
207 {
208         int x, i;
209         float *p = &minimap.data1f[y * minimap.width];
210         float ymin = minimap.mins[1] + minimap.size[1] * (y / (float) minimap.height);
211         float dx   =                   minimap.size[0]      / (float) minimap.width;
212         float dy   =                   minimap.size[1]      / (float) minimap.height;
213
214         for(x = 0; x < minimap.width; ++x)
215         {
216                 float xmin = minimap.mins[0] + minimap.size[0] * (x / (float) minimap.width);
217                 float val = 0;
218
219                 for(i = 0; i < minimap.samples; ++i)
220                 {
221                         float thisval = MiniMapSample(
222                                 xmin + minimap.sample_offsets[2*i+0] * dx,
223                                 ymin + minimap.sample_offsets[2*i+1] * dy
224                         );
225                         val += thisval;
226                 }
227                 val /= minimap.samples * minimap.size[2];
228                 *p++ = val;
229         }
230 }
231
232 static void MiniMapNoSupersampling(int y)
233 {
234         int x;
235         float *p = &minimap.data1f[y * minimap.width];
236         float ymin = minimap.mins[1] + minimap.size[1] * (y / (float) minimap.height);
237
238         for(x = 0; x < minimap.width; ++x)
239         {
240                 float xmin = minimap.mins[0] + minimap.size[0] * (x / (float) minimap.width);
241                 *p++ = MiniMapSample(xmin, ymin) / minimap.size[2];
242         }
243 }
244
245 static void MiniMapSharpen(int y)
246 {
247         int x;
248         qboolean up = (y > 0);
249         qboolean down = (y < minimap.height - 1);
250         float *p = &minimap.data1f[y * minimap.width];
251         float *q = &minimap.sharpendata1f[y * minimap.width];
252
253         for(x = 0; x < minimap.width; ++x)
254         {
255                 qboolean left = (x > 0);
256                 qboolean right = (x < minimap.width - 1);
257                 float val = p[0] * minimap.sharpen_centermult;
258
259                 if(left && up)
260                         val += p[-1 -minimap.width] * minimap.sharpen_boxmult;
261                 if(left && down)
262                         val += p[-1 +minimap.width] * minimap.sharpen_boxmult;
263                 if(right && up)
264                         val += p[+1 -minimap.width] * minimap.sharpen_boxmult;
265                 if(right && down)
266                         val += p[+1 +minimap.width] * minimap.sharpen_boxmult;
267                         
268                 if(left)
269                         val += p[-1] * minimap.sharpen_boxmult;
270                 if(right)
271                         val += p[+1] * minimap.sharpen_boxmult;
272                 if(up)
273                         val += p[-minimap.width] * minimap.sharpen_boxmult;
274                 if(down)
275                         val += p[+minimap.width] * minimap.sharpen_boxmult;
276
277                 ++p;
278                 *q++ = val;
279         }
280 }
281
282 void MiniMapMakeMinsMaxs(vec3_t mins_in, vec3_t maxs_in)
283 {
284         vec3_t mins, maxs, extend;
285         VectorCopy(mins_in, mins);
286         VectorCopy(maxs_in, maxs);
287
288         // line compatible to nexuiz mapinfo
289         Sys_Printf("size %f %f %f %f %f %f\n", mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]);
290
291         VectorSubtract(maxs, mins, extend);
292
293         if(extend[1] > extend[0])
294         {
295                 mins[0] -= (extend[1] - extend[0]) * 0.5;
296                 maxs[0] += (extend[1] - extend[0]) * 0.5;
297         }
298         else
299         {
300                 mins[1] -= (extend[0] - extend[1]) * 0.5;
301                 maxs[1] += (extend[0] - extend[1]) * 0.5;
302         }
303
304         VectorSubtract(maxs, mins, extend);
305         VectorScale(extend, 1.0 / 64.0, extend);
306
307         VectorSubtract(mins, extend, mins);
308         VectorAdd(maxs, extend, maxs);
309
310         VectorCopy(mins, minimap.mins);
311         VectorSubtract(maxs, mins, minimap.size);
312
313         // line compatible to nexuiz mapinfo
314         Sys_Printf("size_texcoords %f %f %f %f %f %f\n", mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]);
315 }
316
317 /*
318 MiniMapSetupBrushes()
319 determines solid non-sky brushes in the world
320 */
321
322 void MiniMapSetupBrushes( void )
323 {
324         int                             i, j, b, compileFlags;
325         bspBrush_t              *brush;
326         bspBrushSide_t  *side;
327         bspShader_t             *shader;
328         shaderInfo_t    *si;
329         
330         
331         /* note it */
332         Sys_FPrintf( SYS_VRB, "--- MiniMapSetupBrushes ---\n" );
333         
334         /* allocate */
335         if( opaqueBrushes == NULL )
336                 opaqueBrushes = safe_malloc( numBSPBrushes / 8 + 1 );
337         
338         /* clear */
339         memset( opaqueBrushes, 0, numBSPBrushes / 8 + 1 );
340         numOpaqueBrushes = 0;
341         
342         /* walk the list of worldspawn brushes */
343         for( i = 0; i < minimap.model->numBSPBrushes; i++ )
344         {
345                 /* get brush */
346                 b = minimap.model->firstBSPBrush + i;
347                 brush = &bspBrushes[ b ];
348                 
349 #if 0
350                 /* check all sides */
351                 compileFlags = 0;
352                 for( j = 0; j < brush->numSides; j++ )
353                 {
354                         /* do bsp shader calculations */
355                         side = &bspBrushSides[ brush->firstSide + j ];
356                         shader = &bspShaders[ side->shaderNum ];
357                         
358                         /* get shader info */
359                         si = ShaderInfoForShader( shader->shader );
360                         if( si == NULL )
361                                 continue;
362                         
363                         /* or together compile flags */
364                         compileFlags |= si->compileFlags;
365                 }
366 #else
367                 shader = &bspShaders[ brush->shaderNum ];
368                 si = ShaderInfoForShader( shader->shader );
369                 if( si == NULL )
370                         compileFlags = 0;
371                 else
372                         compileFlags = si->compileFlags;
373 #endif
374                 
375                 /* determine if this brush is solid */
376                 if( (compileFlags & (C_SOLID | C_SKY)) == C_SOLID )
377                 {
378                         opaqueBrushes[ b >> 3 ] |= (1 << (b & 7));
379                         numOpaqueBrushes++;
380                         maxOpaqueBrush = i;
381                 }
382         }
383         
384         /* emit some statistics */
385         Sys_FPrintf( SYS_VRB, "%9d solid brushes\n", numOpaqueBrushes );
386 }
387
388 int MiniMapBSPMain( int argc, char **argv )
389 {
390         char minimapFilename[1024];
391         char basename[1024];
392         char path[1024];
393         char parentpath[1024];
394         float minimapSharpen;
395         byte *data3b, *p;
396         float *q;
397         int x, y;
398         int i;
399         vec3_t mins, maxs;
400
401         /* arg checking */
402         if( argc < 2 )
403         {
404                 Sys_Printf( "Usage: q3map [-v] -minimap [-size n] [-sharpen f] [-samples n | -random n] [-o filename.tga] [-minmax Xmin Ymin Zmin Xmax Ymax Zmax] <mapname>\n" );
405                 return 0;
406         }
407
408         /* load the BSP first */
409         strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
410         StripExtension( source );
411         DefaultExtension( source, ".bsp" );
412         Sys_Printf( "Loading %s\n", source );
413         BeginMapShaderFile( source );
414         LoadShaderInfo();
415         LoadBSPFile( source );
416
417         minimap.model = &bspModels[0];
418         VectorCopy(minimap.model->mins, mins);
419         VectorCopy(minimap.model->maxs, maxs);
420         *minimapFilename = 0;
421         minimapSharpen = 1;
422         minimap.width = minimap.height = 512;
423         minimap.samples = 1;
424         minimap.sample_offsets = NULL;
425
426         /* process arguments */
427         for( i = 1; i < (argc - 1); i++ )
428         {
429                 if( !strcmp( argv[ i ],  "-size" ) )
430                 {
431                         minimap.width = minimap.height = atoi(argv[i + 1]);
432                         i++;
433                         Sys_Printf( "Image size set to %i\n", minimap.width );
434                 }
435                 else if( !strcmp( argv[ i ],  "-sharpen" ) )
436                 {
437                         minimapSharpen = atof(argv[i + 1]);
438                         i++;
439                         Sys_Printf( "Sharpening coefficient set to %f\n", minimapSharpen );
440                 }
441                 else if( !strcmp( argv[ i ],  "-samples" ) )
442                 {
443                         minimap.samples = atoi(argv[i + 1]);
444                         i++;
445                         Sys_Printf( "Samples set to %i\n", minimap.samples );
446                         if(minimap.sample_offsets)
447                                 free(minimap.sample_offsets);
448                         minimap.sample_offsets = malloc(2 * sizeof(*minimap.sample_offsets) * minimap.samples);
449                         /* TODO use a better pattern */
450                         for(x = 0; x < 2 * minimap.samples; ++x)
451                                 minimap.sample_offsets[x] = Random();
452                 }
453                 else if( !strcmp( argv[ i ],  "-random" ) )
454                 {
455                         minimap.samples = atoi(argv[i + 1]);
456                         i++;
457                         Sys_Printf( "Random samples set to %i\n", minimap.samples );
458                         if(minimap.sample_offsets)
459                                 free(minimap.sample_offsets);
460                         minimap.sample_offsets = NULL;
461                 }
462                 else if( !strcmp( argv[ i ],  "-o" ) )
463                 {
464                         strcpy(minimapFilename, argv[i + 1]);
465                         i++;
466                         Sys_Printf( "Output file name set to %s\n", minimapFilename );
467                 }
468                 else if( !strcmp( argv[ i ],  "-minmax" ) && i < (argc - 7) )
469                 {
470                         mins[0] = atof(argv[i + 1]);
471                         mins[1] = atof(argv[i + 2]);
472                         mins[2] = atof(argv[i + 3]);
473                         maxs[0] = atof(argv[i + 4]);
474                         maxs[1] = atof(argv[i + 5]);
475                         maxs[2] = atof(argv[i + 6]);
476                         i += 6;
477                         Sys_Printf( "Map mins/maxs overridden\n" );
478                 }
479         }
480
481         MiniMapMakeMinsMaxs(mins, maxs);
482
483         if(!*minimapFilename)
484         {
485                 ExtractFileBase(source, basename);
486                 ExtractFilePath(source, path);
487                 if(*path)
488                         path[strlen(path)-1] = 0;
489                 ExtractFilePath(path, parentpath);
490                 sprintf(minimapFilename, "%sgfx", parentpath);
491                 Q_mkdir(minimapFilename);
492                 sprintf(minimapFilename, "%sgfx/%s_mini.tga", parentpath, basename);
493                 Sys_Printf("Output file name automatically set to %s\n", minimapFilename);
494         }
495
496         if(minimapSharpen >= 0)
497         {
498                 minimap.sharpen_centermult = 8 * minimapSharpen + 1;
499                 minimap.sharpen_boxmult    =    -minimapSharpen;
500         }
501
502         minimap.data1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
503         data3b = safe_malloc(minimap.width * minimap.height * 3);
504         if(minimapSharpen >= 0)
505                 minimap.sharpendata1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
506
507         MiniMapSetupBrushes();
508
509         if(minimap.samples <= 1)
510         {
511                 Sys_Printf( "\n--- MiniMapNoSupersampling (%d) ---\n", minimap.height );
512                 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapNoSupersampling);
513         }
514         else
515         {
516                 if(minimap.sample_offsets)
517                 {
518                         Sys_Printf( "\n--- MiniMapSupersampled (%d) ---\n", minimap.height );
519                         RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSupersampled);
520                 }
521                 else
522                 {
523                         Sys_Printf( "\n--- MiniMapRandomlySupersampled (%d) ---\n", minimap.height );
524                         RunThreadsOnIndividual(minimap.height, qtrue, MiniMapRandomlySupersampled);
525                 }
526         }
527
528         if(minimap.sharpendata1f)
529         {
530                 Sys_Printf( "\n--- MiniMapSharpen (%d) ---\n", minimap.height );
531                 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSharpen);
532                 q = minimap.sharpendata1f;
533         }
534         else
535         {
536                 q = minimap.data1f;
537         }
538
539         Sys_Printf( "\nConverting...");
540         p = data3b;
541         for(y = 0; y < minimap.height; ++y)
542                 for(x = 0; x < minimap.width; ++x)
543                 {
544                         byte b;
545                         float v = *q++;
546                         if(v < 0) v = 0;
547                         if(v > 255.0/256.0) v = 255.0/256.0;
548                         b = v * 256;
549                         *p++ = b;
550                         *p++ = b;
551                         *p++ = b;
552                 }
553
554         Sys_Printf( " writing to %s...", minimapFilename );
555         WriteTGA24(minimapFilename, data3b, minimap.width, minimap.height, qfalse);
556
557         Sys_Printf( " done.\n" );
558
559         /* return to sender */
560         return 0;
561 }
562
563
564
565
566
567 /*
568 MD4BlockChecksum()
569 calculates an md4 checksum for a block of data
570 */
571
572 static int MD4BlockChecksum( void *buffer, int length )
573 {
574         return Com_BlockChecksum(buffer, length);
575 }
576
577 /*
578 FixAAS()
579 resets an aas checksum to match the given BSP
580 */
581
582 int FixAAS( int argc, char **argv )
583 {
584         int                     length, checksum;
585         void            *buffer;
586         FILE            *file;
587         char            aas[ 1024 ], **ext;
588         char            *exts[] =
589                                 {
590                                         ".aas",
591                                         "_b0.aas",
592                                         "_b1.aas",
593                                         NULL
594                                 };
595         
596         
597         /* arg checking */
598         if( argc < 2 )
599         {
600                 Sys_Printf( "Usage: q3map -fixaas [-v] <mapname>\n" );
601                 return 0;
602         }
603         
604         /* do some path mangling */
605         strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
606         StripExtension( source );
607         DefaultExtension( source, ".bsp" );
608         
609         /* note it */
610         Sys_Printf( "--- FixAAS ---\n" );
611         
612         /* load the bsp */
613         Sys_Printf( "Loading %s\n", source );
614         length = LoadFile( source, &buffer );
615         
616         /* create bsp checksum */
617         Sys_Printf( "Creating checksum...\n" );
618         checksum = LittleLong( MD4BlockChecksum( buffer, length ) );
619         
620         /* write checksum to aas */
621         ext = exts;
622         while( *ext )
623         {
624                 /* mangle name */
625                 strcpy( aas, source );
626                 StripExtension( aas );
627                 strcat( aas, *ext );
628                 Sys_Printf( "Trying %s\n", aas );
629                 ext++;
630                 
631                 /* fix it */
632                 file = fopen( aas, "r+b" );
633                 if( !file )
634                         continue;
635                 if( fwrite( &checksum, 4, 1, file ) != 1 )
636                         Error( "Error writing checksum to %s", aas );
637                 fclose( file );
638         }
639         
640         /* return to sender */
641         return 0;
642 }
643
644
645
646 /*
647 AnalyzeBSP() - ydnar
648 analyzes a Quake engine BSP file
649 */
650
651 typedef struct abspHeader_s
652 {
653         char                    ident[ 4 ];
654         int                             version;
655         
656         bspLump_t               lumps[ 1 ];     /* unknown size */
657 }
658 abspHeader_t;
659
660 typedef struct abspLumpTest_s
661 {
662         int                             radix, minCount;
663         char                    *name;
664 }
665 abspLumpTest_t;
666
667 int AnalyzeBSP( int argc, char **argv )
668 {
669         abspHeader_t                    *header;
670         int                                             size, i, version, offset, length, lumpInt, count;
671         char                                    ident[ 5 ];
672         void                                    *lump;
673         float                                   lumpFloat;
674         char                                    lumpString[ 1024 ], source[ 1024 ];
675         qboolean                                lumpSwap = qfalse;
676         abspLumpTest_t                  *lumpTest;
677         static abspLumpTest_t   lumpTests[] =
678                                                         {
679                                                                 { sizeof( bspPlane_t ),                 6,              "IBSP LUMP_PLANES" },
680                                                                 { sizeof( bspBrush_t ),                 1,              "IBSP LUMP_BRUSHES" },
681                                                                 { 8,                                                    6,              "IBSP LUMP_BRUSHSIDES" },
682                                                                 { sizeof( bspBrushSide_t ),             6,              "RBSP LUMP_BRUSHSIDES" },
683                                                                 { sizeof( bspModel_t ),                 1,              "IBSP LUMP_MODELS" },
684                                                                 { sizeof( bspNode_t ),                  2,              "IBSP LUMP_NODES" },
685                                                                 { sizeof( bspLeaf_t ),                  1,              "IBSP LUMP_LEAFS" },
686                                                                 { 104,                                                  3,              "IBSP LUMP_DRAWSURFS" },
687                                                                 { 44,                                                   3,              "IBSP LUMP_DRAWVERTS" },
688                                                                 { 4,                                                    6,              "IBSP LUMP_DRAWINDEXES" },
689                                                                 { 128 * 128 * 3,                                1,              "IBSP LUMP_LIGHTMAPS" },
690                                                                 { 256 * 256 * 3,                                1,              "IBSP LUMP_LIGHTMAPS (256 x 256)" },
691                                                                 { 512 * 512 * 3,                                1,              "IBSP LUMP_LIGHTMAPS (512 x 512)" },
692                                                                 { 0, 0, NULL }
693                                                         };
694         
695         
696         /* arg checking */
697         if( argc < 1 )
698         {
699                 Sys_Printf( "Usage: q3map -analyze [-lumpswap] [-v] <mapname>\n" );
700                 return 0;
701         }
702         
703         /* process arguments */
704         for( i = 1; i < (argc - 1); i++ )
705         {
706                 /* -format map|ase|... */
707                 if( !strcmp( argv[ i ],  "-lumpswap" ) )
708                 {
709                         Sys_Printf( "Swapped lump structs enabled\n" );
710                         lumpSwap = qtrue;
711                 }
712         }
713         
714         /* clean up map name */
715         strcpy( source, ExpandArg( argv[ i ] ) );
716         Sys_Printf( "Loading %s\n", source );
717         
718         /* load the file */
719         size = LoadFile( source, (void**) &header );
720         if( size == 0 || header == NULL )
721         {
722                 Sys_Printf( "Unable to load %s.\n", source );
723                 return -1;
724         }
725         
726         /* analyze ident/version */
727         memcpy( ident, header->ident, 4 );
728         ident[ 4 ] = '\0';
729         version = LittleLong( header->version );
730         
731         Sys_Printf( "Identity:      %s\n", ident );
732         Sys_Printf( "Version:       %d\n", version );
733         Sys_Printf( "---------------------------------------\n" );
734         
735         /* analyze each lump */
736         for( i = 0; i < 100; i++ )
737         {
738                 /* call of duty swapped lump pairs */
739                 if( lumpSwap )
740                 {
741                         offset = LittleLong( header->lumps[ i ].length );
742                         length = LittleLong( header->lumps[ i ].offset );
743                 }
744                 
745                 /* standard lump pairs */
746                 else
747                 {
748                         offset = LittleLong( header->lumps[ i ].offset );
749                         length = LittleLong( header->lumps[ i ].length );
750                 }
751                 
752                 /* extract data */
753                 lump = (byte*) header + offset;
754                 lumpInt = LittleLong( (int) *((int*) lump) );
755                 lumpFloat = LittleFloat( (float) *((float*) lump) );
756                 memcpy( lumpString, (char*) lump, (length < 1024 ? length : 1024) );
757                 lumpString[ 1024 ] = '\0';
758                 
759                 /* print basic lump info */
760                 Sys_Printf( "Lump:          %d\n", i );
761                 Sys_Printf( "Offset:        %d bytes\n", offset );
762                 Sys_Printf( "Length:        %d bytes\n", length );
763                 
764                 /* only operate on valid lumps */
765                 if( length > 0 )
766                 {
767                         /* print data in 4 formats */
768                         Sys_Printf( "As hex:        %08X\n", lumpInt );
769                         Sys_Printf( "As int:        %d\n", lumpInt );
770                         Sys_Printf( "As float:      %f\n", lumpFloat );
771                         Sys_Printf( "As string:     %s\n", lumpString );
772                         
773                         /* guess lump type */
774                         if( lumpString[ 0 ] == '{' && lumpString[ 2 ] == '"' )
775                                 Sys_Printf( "Type guess:    IBSP LUMP_ENTITIES\n" );
776                         else if( strstr( lumpString, "textures/" ) )
777                                 Sys_Printf( "Type guess:    IBSP LUMP_SHADERS\n" );
778                         else
779                         {
780                                 /* guess based on size/count */
781                                 for( lumpTest = lumpTests; lumpTest->radix > 0; lumpTest++ )
782                                 {
783                                         if( (length % lumpTest->radix) != 0 )
784                                                 continue;
785                                         count = length / lumpTest->radix;
786                                         if( count < lumpTest->minCount )
787                                                 continue;
788                                         Sys_Printf( "Type guess:    %s (%d x %d)\n", lumpTest->name, count, lumpTest->radix );
789                                 }
790                         }
791                 }
792                 
793                 Sys_Printf( "---------------------------------------\n" );
794                 
795                 /* end of file */
796                 if( offset + length >= size )
797                         break;
798         }
799         
800         /* last stats */
801         Sys_Printf( "Lump count:    %d\n", i + 1 );
802         Sys_Printf( "File size:     %d bytes\n", size );
803         
804         /* return to caller */
805         return 0;
806 }
807
808
809
810 /*
811 BSPInfo()
812 emits statistics about the bsp file
813 */
814
815 int BSPInfo( int count, char **fileNames )
816 {
817         int                     i;
818         char            source[ 1024 ], ext[ 64 ];
819         int                     size;
820         FILE            *f;
821         
822         
823         /* dummy check */
824         if( count < 1 )
825         {
826                 Sys_Printf( "No files to dump info for.\n");
827                 return -1;
828         }
829         
830         /* enable info mode */
831         infoMode = qtrue;
832         
833         /* walk file list */
834         for( i = 0; i < count; i++ )
835         {
836                 Sys_Printf( "---------------------------------\n" );
837                 
838                 /* mangle filename and get size */
839                 strcpy( source, fileNames[ i ] );
840                 ExtractFileExtension( source, ext );
841                 if( !Q_stricmp( ext, "map" ) )
842                         StripExtension( source );
843                 DefaultExtension( source, ".bsp" );
844                 f = fopen( source, "rb" );
845                 if( f )
846                 {
847                         size = Q_filelength (f);
848                         fclose( f );
849                 }
850                 else
851                         size = 0;
852                 
853                 /* load the bsp file and print lump sizes */
854                 Sys_Printf( "%s\n", source );
855                 LoadBSPFile( source );          
856                 PrintBSPFileSizes();
857                 
858                 /* print sizes */
859                 Sys_Printf( "\n" );
860                 Sys_Printf( "          total         %9d\n", size );
861                 Sys_Printf( "                        %9d KB\n", size / 1024 );
862                 Sys_Printf( "                        %9d MB\n", size / (1024 * 1024) );
863                 
864                 Sys_Printf( "---------------------------------\n" );
865         }
866         
867         /* return count */
868         return i;
869 }
870
871
872 static void ExtrapolateTexcoords(const float *axyz, const float *ast, const float *bxyz, const float *bst, const float *cxyz, const float *cst, const float *axyz_new, float *ast_out, const float *bxyz_new, float *bst_out, const float *cxyz_new, float *cst_out)
873 {
874         vec4_t scoeffs, tcoeffs;
875         float md;
876         m4x4_t solvematrix;
877
878         vec3_t norm;
879         vec3_t dab, dac;
880         VectorSubtract(bxyz, axyz, dab);
881         VectorSubtract(cxyz, axyz, dac);
882         CrossProduct(dab, dac, norm);
883         
884         // assume:
885         //   s = f(x, y, z)
886         //   s(v + norm) = s(v) when n ortho xyz
887         
888         // s(v) = DotProduct(v, scoeffs) + scoeffs[3]
889
890         // solve:
891         //   scoeffs * (axyz, 1) == ast[0]
892         //   scoeffs * (bxyz, 1) == bst[0]
893         //   scoeffs * (cxyz, 1) == cst[0]
894         //   scoeffs * (norm, 0) == 0
895         // scoeffs * [axyz, 1 | bxyz, 1 | cxyz, 1 | norm, 0] = [ast[0], bst[0], cst[0], 0]
896         solvematrix[0] = axyz[0];
897         solvematrix[4] = axyz[1];
898         solvematrix[8] = axyz[2];
899         solvematrix[12] = 1;
900         solvematrix[1] = bxyz[0];
901         solvematrix[5] = bxyz[1];
902         solvematrix[9] = bxyz[2];
903         solvematrix[13] = 1;
904         solvematrix[2] = cxyz[0];
905         solvematrix[6] = cxyz[1];
906         solvematrix[10] = cxyz[2];
907         solvematrix[14] = 1;
908         solvematrix[3] = norm[0];
909         solvematrix[7] = norm[1];
910         solvematrix[11] = norm[2];
911         solvematrix[15] = 0;
912
913         md = m4_det(solvematrix);
914         if(md*md < 1e-10)
915         {
916                 Sys_Printf("Cannot invert some matrix, some texcoords aren't extrapolated!");
917                 return;
918         }
919
920         m4x4_invert(solvematrix);
921
922         scoeffs[0] = ast[0];
923         scoeffs[1] = bst[0];
924         scoeffs[2] = cst[0];
925         scoeffs[3] = 0;
926         m4x4_transform_vec4(solvematrix, scoeffs);
927         tcoeffs[0] = ast[1];
928         tcoeffs[1] = bst[1];
929         tcoeffs[2] = cst[1];
930         tcoeffs[3] = 0;
931         m4x4_transform_vec4(solvematrix, tcoeffs);
932
933         ast_out[0] = scoeffs[0] * axyz_new[0] + scoeffs[1] * axyz_new[1] + scoeffs[2] * axyz_new[2] + scoeffs[3];
934         ast_out[1] = tcoeffs[0] * axyz_new[0] + tcoeffs[1] * axyz_new[1] + tcoeffs[2] * axyz_new[2] + tcoeffs[3];
935         bst_out[0] = scoeffs[0] * bxyz_new[0] + scoeffs[1] * bxyz_new[1] + scoeffs[2] * bxyz_new[2] + scoeffs[3];
936         bst_out[1] = tcoeffs[0] * bxyz_new[0] + tcoeffs[1] * bxyz_new[1] + tcoeffs[2] * bxyz_new[2] + tcoeffs[3];
937         cst_out[0] = scoeffs[0] * cxyz_new[0] + scoeffs[1] * cxyz_new[1] + scoeffs[2] * cxyz_new[2] + scoeffs[3];
938         cst_out[1] = tcoeffs[0] * cxyz_new[0] + tcoeffs[1] * cxyz_new[1] + tcoeffs[2] * cxyz_new[2] + tcoeffs[3];
939 }
940
941 /*
942 ScaleBSPMain()
943 amaze and confuse your enemies with wierd scaled maps!
944 */
945
946 int ScaleBSPMain( int argc, char **argv )
947 {
948         int                     i, j;
949         float           f, a;
950         vec3_t scale;
951         vec3_t          vec;
952         char            str[ 1024 ];
953         int uniform, axis;
954         qboolean texscale;
955         float *old_xyzst = NULL;
956         
957         
958         /* arg checking */
959         if( argc < 3 )
960         {
961                 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
962                 return 0;
963         }
964         
965         /* get scale */
966         scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
967         if(argc >= 4)
968                 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
969         if(argc >= 5)
970                 scale[0] = atof( argv[ argc - 4 ] );
971
972         texscale = !strcmp(argv[1], "-tex");
973         
974         uniform = ((scale[0] == scale[1]) && (scale[1] == scale[2]));
975
976         if( scale[0] == 0.0f || scale[1] == 0.0f || scale[2] == 0.0f )
977         {
978                 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
979                 Sys_Printf( "Non-zero scale value required.\n" );
980                 return 0;
981         }
982         
983         /* do some path mangling */
984         strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
985         StripExtension( source );
986         DefaultExtension( source, ".bsp" );
987         
988         /* load the bsp */
989         Sys_Printf( "Loading %s\n", source );
990         LoadBSPFile( source );
991         ParseEntities();
992         
993         /* note it */
994         Sys_Printf( "--- ScaleBSP ---\n" );
995         Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
996         
997         /* scale entity keys */
998         for( i = 0; i < numBSPEntities && i < numEntities; i++ )
999         {
1000                 /* scale origin */
1001                 GetVectorForKey( &entities[ i ], "origin", vec );
1002                 if( (vec[ 0 ] || vec[ 1 ] || vec[ 2 ]) )
1003                 {
1004                         vec[0] *= scale[0];
1005                         vec[1] *= scale[1];
1006                         vec[2] *= scale[2];
1007                         sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1008                         SetKeyValue( &entities[ i ], "origin", str );
1009                 }
1010
1011                 a = FloatForKey( &entities[ i ], "angle" );
1012                 if(a == -1 || a == -2) // z scale
1013                         axis = 2;
1014                 else if(fabs(sin(DEG2RAD(a))) < 0.707)
1015                         axis = 0;
1016                 else
1017                         axis = 1;
1018                 
1019                 /* scale door lip */
1020                 f = FloatForKey( &entities[ i ], "lip" );
1021                 if( f )
1022                 {
1023                         f *= scale[axis];
1024                         sprintf( str, "%f", f );
1025                         SetKeyValue( &entities[ i ], "lip", str );
1026                 }
1027                 
1028                 /* scale plat height */
1029                 f = FloatForKey( &entities[ i ], "height" );
1030                 if( f )
1031                 {
1032                         f *= scale[2];
1033                         sprintf( str, "%f", f );
1034                         SetKeyValue( &entities[ i ], "height", str );
1035                 }
1036
1037                 // TODO maybe allow a definition file for entities to specify which values are scaled how?
1038         }
1039         
1040         /* scale models */
1041         for( i = 0; i < numBSPModels; i++ )
1042         {
1043                 bspModels[ i ].mins[0] *= scale[0];
1044                 bspModels[ i ].mins[1] *= scale[1];
1045                 bspModels[ i ].mins[2] *= scale[2];
1046                 bspModels[ i ].maxs[0] *= scale[0];
1047                 bspModels[ i ].maxs[1] *= scale[1];
1048                 bspModels[ i ].maxs[2] *= scale[2];
1049         }
1050         
1051         /* scale nodes */
1052         for( i = 0; i < numBSPNodes; i++ )
1053         {
1054                 bspNodes[ i ].mins[0] *= scale[0];
1055                 bspNodes[ i ].mins[1] *= scale[1];
1056                 bspNodes[ i ].mins[2] *= scale[2];
1057                 bspNodes[ i ].maxs[0] *= scale[0];
1058                 bspNodes[ i ].maxs[1] *= scale[1];
1059                 bspNodes[ i ].maxs[2] *= scale[2];
1060         }
1061         
1062         /* scale leafs */
1063         for( i = 0; i < numBSPLeafs; i++ )
1064         {
1065                 bspLeafs[ i ].mins[0] *= scale[0];
1066                 bspLeafs[ i ].mins[1] *= scale[1];
1067                 bspLeafs[ i ].mins[2] *= scale[2];
1068                 bspLeafs[ i ].maxs[0] *= scale[0];
1069                 bspLeafs[ i ].maxs[1] *= scale[1];
1070                 bspLeafs[ i ].maxs[2] *= scale[2];
1071         }
1072         
1073         if(texscale)
1074         {
1075                 Sys_Printf("Using texture unlocking (and probably breaking texture alignment a lot)\n");
1076                 old_xyzst = safe_malloc(sizeof(*old_xyzst) * numBSPDrawVerts * 5);
1077                 for(i = 0; i < numBSPDrawVerts; i++)
1078                 {
1079                         old_xyzst[5*i+0] = bspDrawVerts[i].xyz[0];
1080                         old_xyzst[5*i+1] = bspDrawVerts[i].xyz[1];
1081                         old_xyzst[5*i+2] = bspDrawVerts[i].xyz[2];
1082                         old_xyzst[5*i+3] = bspDrawVerts[i].st[0];
1083                         old_xyzst[5*i+4] = bspDrawVerts[i].st[1];
1084                 }
1085         }
1086
1087         /* scale drawverts */
1088         for( i = 0; i < numBSPDrawVerts; i++ )
1089         {
1090                 bspDrawVerts[i].xyz[0] *= scale[0];
1091                 bspDrawVerts[i].xyz[1] *= scale[1];
1092                 bspDrawVerts[i].xyz[2] *= scale[2];
1093                 bspDrawVerts[i].normal[0] /= scale[0];
1094                 bspDrawVerts[i].normal[1] /= scale[1];
1095                 bspDrawVerts[i].normal[2] /= scale[2];
1096                 VectorNormalize(bspDrawVerts[i].normal, bspDrawVerts[i].normal);
1097         }
1098
1099         if(texscale)
1100         {
1101                 for(i = 0; i < numBSPDrawSurfaces; i++)
1102                 {
1103                         switch(bspDrawSurfaces[i].surfaceType)
1104                         {
1105                                 case SURFACE_FACE:
1106                                 case SURFACE_META:
1107                                         if(bspDrawSurfaces[i].numIndexes % 3)
1108                                                 Error("Not a triangulation!");
1109                                         for(j = bspDrawSurfaces[i].firstIndex; j < bspDrawSurfaces[i].firstIndex + bspDrawSurfaces[i].numIndexes; j += 3)
1110                                         {
1111                                                 int ia = bspDrawIndexes[j] + bspDrawSurfaces[i].firstVert, ib = bspDrawIndexes[j+1] + bspDrawSurfaces[i].firstVert, ic = bspDrawIndexes[j+2] + bspDrawSurfaces[i].firstVert;
1112                                                 bspDrawVert_t *a = &bspDrawVerts[ia], *b = &bspDrawVerts[ib], *c = &bspDrawVerts[ic];
1113                                                 float *oa = &old_xyzst[ia*5], *ob = &old_xyzst[ib*5], *oc = &old_xyzst[ic*5];
1114                                                 // extrapolate:
1115                                                 //   a->xyz -> oa
1116                                                 //   b->xyz -> ob
1117                                                 //   c->xyz -> oc
1118                                                 ExtrapolateTexcoords(
1119                                                         &oa[0], &oa[3],
1120                                                         &ob[0], &ob[3],
1121                                                         &oc[0], &oc[3],
1122                                                         a->xyz, a->st,
1123                                                         b->xyz, b->st,
1124                                                         c->xyz, c->st);
1125                                         }
1126                                         break;
1127                         }
1128                 }
1129         }
1130         
1131         /* scale planes */
1132         if(uniform)
1133         {
1134                 for( i = 0; i < numBSPPlanes; i++ )
1135                 {
1136                         bspPlanes[ i ].dist *= scale[0];
1137                 }
1138         }
1139         else
1140         {
1141                 for( i = 0; i < numBSPPlanes; i++ )
1142                 {
1143                         bspPlanes[ i ].normal[0] /= scale[0];
1144                         bspPlanes[ i ].normal[1] /= scale[1];
1145                         bspPlanes[ i ].normal[2] /= scale[2];
1146                         f = 1/VectorLength(bspPlanes[i].normal);
1147                         VectorScale(bspPlanes[i].normal, f, bspPlanes[i].normal);
1148                         bspPlanes[ i ].dist *= f;
1149                 }
1150         }
1151         
1152         /* scale gridsize */
1153         GetVectorForKey( &entities[ 0 ], "gridsize", vec );
1154         if( (vec[ 0 ] + vec[ 1 ] + vec[ 2 ]) == 0.0f )
1155                 VectorCopy( gridSize, vec );
1156         vec[0] *= scale[0];
1157         vec[1] *= scale[1];
1158         vec[2] *= scale[2];
1159         sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1160         SetKeyValue( &entities[ 0 ], "gridsize", str );
1161
1162         /* inject command line parameters */
1163         InjectCommandLine(argv, 0, argc - 1);
1164         
1165         /* write the bsp */
1166         UnparseEntities();
1167         StripExtension( source );
1168         DefaultExtension( source, "_s.bsp" );
1169         Sys_Printf( "Writing %s\n", source );
1170         WriteBSPFile( source );
1171         
1172         /* return to sender */
1173         return 0;
1174 }
1175
1176
1177
1178 /*
1179 ConvertBSPMain()
1180 main argument processing function for bsp conversion
1181 */
1182
1183 int ConvertBSPMain( int argc, char **argv )
1184 {
1185         int             i;
1186         int             (*convertFunc)( char * );
1187         game_t  *convertGame;
1188         
1189         
1190         /* set default */
1191         convertFunc = ConvertBSPToASE;
1192         convertGame = NULL;
1193         
1194         /* arg checking */
1195         if( argc < 1 )
1196         {
1197                 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
1198                 return 0;
1199         }
1200         
1201         /* process arguments */
1202         for( i = 1; i < (argc - 1); i++ )
1203         {
1204                 /* -format map|ase|... */
1205                 if( !strcmp( argv[ i ],  "-format" ) )
1206                 {
1207                         i++;
1208                         if( !Q_stricmp( argv[ i ], "ase" ) )
1209                                 convertFunc = ConvertBSPToASE;
1210                         else if( !Q_stricmp( argv[ i ], "map" ) )
1211                                 convertFunc = ConvertBSPToMap;
1212                         else
1213                         {
1214                                 convertGame = GetGame( argv[ i ] );
1215                                 if( convertGame == NULL )
1216                                         Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
1217                         }
1218                 }
1219                 else if( !strcmp( argv[ i ],  "-ne" ) )
1220                 {
1221                         normalEpsilon = atof( argv[ i + 1 ] );
1222                         i++;
1223                         Sys_Printf( "Normal epsilon set to %f\n", normalEpsilon );
1224                 }
1225                 else if( !strcmp( argv[ i ],  "-de" ) )
1226                 {
1227                         distanceEpsilon = atof( argv[ i + 1 ] );
1228                         i++;
1229                         Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
1230                 }
1231                 else if( !strcmp( argv[ i ],  "-shadersasbitmap" ) )
1232                         shadersAsBitmap = qtrue;
1233         }
1234         
1235         /* clean up map name */
1236         strcpy( source, ExpandArg( argv[ i ] ) );
1237         StripExtension( source );
1238         DefaultExtension( source, ".bsp" );
1239         
1240         LoadShaderInfo();
1241         
1242         Sys_Printf( "Loading %s\n", source );
1243         
1244         /* ydnar: load surface file */
1245         //%     LoadSurfaceExtraFile( source );
1246         
1247         LoadBSPFile( source );
1248         
1249         /* parse bsp entities */
1250         ParseEntities();
1251         
1252         /* bsp format convert? */
1253         if( convertGame != NULL )
1254         {
1255                 /* set global game */
1256                 game = convertGame;
1257                 
1258                 /* write bsp */
1259                 StripExtension( source );
1260                 DefaultExtension( source, "_c.bsp" );
1261                 Sys_Printf( "Writing %s\n", source );
1262                 WriteBSPFile( source );
1263                 
1264                 /* return to sender */
1265                 return 0;
1266         }
1267         
1268         /* normal convert */
1269         return convertFunc( source );
1270 }
1271
1272
1273
1274 /*
1275 main()
1276 q3map mojo...
1277 */
1278
1279 int main( int argc, char **argv )
1280 {
1281         int             i, r;
1282         double  start, end;
1283         
1284         
1285         /* we want consistent 'randomness' */
1286         srand( 0 );
1287         
1288         /* start timer */
1289         start = I_FloatTime();
1290
1291         /* this was changed to emit version number over the network */
1292         printf( Q3MAP_VERSION "\n" );
1293         
1294         /* set exit call */
1295         atexit( ExitQ3Map );
1296
1297         /* read general options first */
1298         for( i = 1; i < argc; i++ )
1299         {
1300                 /* -connect */
1301                 if( !strcmp( argv[ i ], "-connect" ) )
1302                 {
1303                         argv[ i ] = NULL;
1304                         i++;
1305                         Broadcast_Setup( argv[ i ] );
1306                         argv[ i ] = NULL;
1307                 }
1308                 
1309                 /* verbose */
1310                 else if( !strcmp( argv[ i ], "-v" ) )
1311                 {
1312                         if(!verbose)
1313                         {
1314                                 verbose = qtrue;
1315                                 argv[ i ] = NULL;
1316                         }
1317                 }
1318                 
1319                 /* force */
1320                 else if( !strcmp( argv[ i ], "-force" ) )
1321                 {
1322                         force = qtrue;
1323                         argv[ i ] = NULL;
1324                 }
1325                 
1326                 /* patch subdivisions */
1327                 else if( !strcmp( argv[ i ], "-subdivisions" ) )
1328                 {
1329                         argv[ i ] = NULL;
1330                         i++;
1331                         patchSubdivisions = atoi( argv[ i ] );
1332                         argv[ i ] = NULL;
1333                         if( patchSubdivisions <= 0 )
1334                                 patchSubdivisions = 1;
1335                 }
1336                 
1337                 /* threads */
1338                 else if( !strcmp( argv[ i ], "-threads" ) )
1339                 {
1340                         argv[ i ] = NULL;
1341                         i++;
1342                         numthreads = atoi( argv[ i ] );
1343                         argv[ i ] = NULL;
1344                 }
1345         }
1346         
1347         /* init model library */
1348         PicoInit();
1349         PicoSetMallocFunc( safe_malloc );
1350         PicoSetFreeFunc( free );
1351         PicoSetPrintFunc( PicoPrintFunc );
1352         PicoSetLoadFileFunc( PicoLoadFileFunc );
1353         PicoSetFreeFileFunc( free );
1354         
1355         /* set number of threads */
1356         ThreadSetDefault();
1357         
1358         /* generate sinusoid jitter table */
1359         for( i = 0; i < MAX_JITTERS; i++ )
1360         {
1361                 jitters[ i ] = sin( i * 139.54152147 );
1362                 //%     Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
1363         }
1364         
1365         /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
1366            and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
1367         
1368         Sys_Printf( "Q3Map         - v1.0r (c) 1999 Id Software Inc.\n" );
1369         Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
1370         Sys_Printf( "NetRadiant    - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
1371         Sys_Printf( "%s\n", Q3MAP_MOTD );
1372         
1373         /* ydnar: new path initialization */
1374         InitPaths( &argc, argv );
1375
1376         /* set game options */
1377         if (!patchSubdivisions)
1378                 patchSubdivisions = game->patchSubdivisions;
1379         
1380         /* check if we have enough options left to attempt something */
1381         if( argc < 2 )
1382                 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
1383         
1384         /* fixaas */
1385         if( !strcmp( argv[ 1 ], "-fixaas" ) )
1386                 r = FixAAS( argc - 1, argv + 1 );
1387         
1388         /* analyze */
1389         else if( !strcmp( argv[ 1 ], "-analyze" ) )
1390                 r = AnalyzeBSP( argc - 1, argv + 1 );
1391         
1392         /* info */
1393         else if( !strcmp( argv[ 1 ], "-info" ) )
1394                 r = BSPInfo( argc - 2, argv + 2 );
1395         
1396         /* vis */
1397         else if( !strcmp( argv[ 1 ], "-vis" ) )
1398                 r = VisMain( argc - 1, argv + 1 );
1399         
1400         /* light */
1401         else if( !strcmp( argv[ 1 ], "-light" ) )
1402                 r = LightMain( argc - 1, argv + 1 );
1403         
1404         /* vlight */
1405         else if( !strcmp( argv[ 1 ], "-vlight" ) )
1406         {
1407                 Sys_Printf( "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
1408                 argv[ 1 ] = "-fast";    /* eek a hack */
1409                 r = LightMain( argc, argv );
1410         }
1411         
1412         /* ydnar: lightmap export */
1413         else if( !strcmp( argv[ 1 ], "-export" ) )
1414                 r = ExportLightmapsMain( argc - 1, argv + 1 );
1415         
1416         /* ydnar: lightmap import */
1417         else if( !strcmp( argv[ 1 ], "-import" ) )
1418                 r = ImportLightmapsMain( argc - 1, argv + 1 );
1419         
1420         /* ydnar: bsp scaling */
1421         else if( !strcmp( argv[ 1 ], "-scale" ) )
1422                 r = ScaleBSPMain( argc - 1, argv + 1 );
1423         
1424         /* ydnar: bsp conversion */
1425         else if( !strcmp( argv[ 1 ], "-convert" ) )
1426                 r = ConvertBSPMain( argc - 1, argv + 1 );
1427         
1428         /* div0: minimap */
1429         else if( !strcmp( argv[ 1 ], "-minimap" ) )
1430                 r = MiniMapBSPMain(argc - 1, argv + 1);
1431
1432         /* ydnar: otherwise create a bsp */
1433         else
1434                 r = BSPMain( argc, argv );
1435         
1436         /* emit time */
1437         end = I_FloatTime();
1438         Sys_Printf( "%9.0f seconds elapsed\n", end - start );
1439         
1440         /* shut down connection */
1441         Broadcast_Shutdown();
1442         
1443         /* return any error code */
1444         return r;
1445 }