]> icculus.org git repositories - divverent/netradiant.git/blob - tools/quake3/q3map2/main.c
ignore nonsolid brushes
[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()
283 {
284         vec3_t mins, maxs, extend;
285         VectorCopy(minimap.model->mins, mins);
286         VectorCopy(minimap.model->maxs, 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                 /* check all sides */
350                 compileFlags = 0;
351                 for( j = 0; j < brush->numSides; j++ )
352                 {
353                         /* do bsp shader calculations */
354                         side = &bspBrushSides[ brush->firstSide + j ];
355                         shader = &bspShaders[ side->shaderNum ];
356                         
357                         /* get shader info */
358                         si = ShaderInfoForShader( shader->shader );
359                         if( si == NULL )
360                                 continue;
361                         
362                         /* or together compile flags */
363                         compileFlags |= si->compileFlags;
364                 }
365                 
366                 /* determine if this brush is solid */
367                 if( (compileFlags & (C_SOLID | C_SKY)) == C_SOLID )
368                 {
369                         opaqueBrushes[ b >> 3 ] |= (1 << (b & 7));
370                         numOpaqueBrushes++;
371                         maxOpaqueBrush = i;
372                 }
373         }
374         
375         /* emit some statistics */
376         Sys_FPrintf( SYS_VRB, "%9d solid brushes\n", numOpaqueBrushes );
377 }
378
379 int MiniMapBSPMain( int argc, char **argv )
380 {
381         char minimapFilename[1024];
382         char basename[1024];
383         char path[1024];
384         char parentpath[1024];
385         float minimapSharpen;
386         byte *data3b, *p;
387         float *q;
388         int x, y;
389         int i;
390
391         /* arg checking */
392         if( argc < 2 )
393         {
394                 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" );
395                 return 0;
396         }
397
398         /* load the BSP first */
399         strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
400         StripExtension( source );
401         DefaultExtension( source, ".bsp" );
402         Sys_Printf( "Loading %s\n", source );
403         BeginMapShaderFile( source );
404         LoadShaderInfo();
405         LoadBSPFile( source );
406
407         minimap.model = &bspModels[0];
408         MiniMapMakeMinsMaxs();
409
410         *minimapFilename = 0;
411         minimapSharpen = 1;
412         minimap.width = minimap.height = 512;
413         minimap.samples = 1;
414         minimap.sample_offsets = NULL;
415
416         /* process arguments */
417         for( i = 1; i < (argc - 1); i++ )
418         {
419                 if( !strcmp( argv[ i ],  "-size" ) )
420                 {
421                         minimap.width = minimap.height = atoi(argv[i + 1]);
422                         i++;
423                         Sys_Printf( "Image size set to %i\n", minimap.width );
424                 }
425                 else if( !strcmp( argv[ i ],  "-sharpen" ) )
426                 {
427                         minimapSharpen = atof(argv[i + 1]);
428                         i++;
429                         Sys_Printf( "Sharpening coefficient set to %f\n", minimapSharpen );
430                 }
431                 else if( !strcmp( argv[ i ],  "-samples" ) )
432                 {
433                         minimap.samples = atoi(argv[i + 1]);
434                         i++;
435                         Sys_Printf( "Samples set to %i\n", minimap.samples );
436                         if(minimap.sample_offsets)
437                                 free(minimap.sample_offsets);
438                         minimap.sample_offsets = malloc(2 * sizeof(*minimap.sample_offsets) * minimap.samples);
439                         /* TODO use a better pattern */
440                         for(x = 0; x < 2 * minimap.samples; ++x)
441                                 minimap.sample_offsets[x] = Random();
442                 }
443                 else if( !strcmp( argv[ i ],  "-random" ) )
444                 {
445                         minimap.samples = atoi(argv[i + 1]);
446                         i++;
447                         Sys_Printf( "Random samples set to %i\n", minimap.samples );
448                         if(minimap.sample_offsets)
449                                 free(minimap.sample_offsets);
450                         minimap.sample_offsets = NULL;
451                 }
452                 else if( !strcmp( argv[ i ],  "-o" ) )
453                 {
454                         strcpy(minimapFilename, argv[i + 1]);
455                         i++;
456                         Sys_Printf( "Output file name set to %s\n", minimapFilename );
457                 }
458                 else if( !strcmp( argv[ i ],  "-minmax" ) && i < (argc - 7) )
459                 {
460                         minimap.mins[0] = atof(argv[i + 1]);
461                         minimap.mins[1] = atof(argv[i + 2]);
462                         minimap.mins[2] = atof(argv[i + 3]);
463                         minimap.size[0] = atof(argv[i + 4]) - minimap.mins[0];
464                         minimap.size[1] = atof(argv[i + 5]) - minimap.mins[1];
465                         minimap.size[2] = atof(argv[i + 6]) - minimap.mins[2];
466                         i += 6;
467                         Sys_Printf( "Map mins/maxs overridden\n" );
468                 }
469         }
470
471         if(!*minimapFilename)
472         {
473                 ExtractFileBase(source, basename);
474                 ExtractFilePath(source, path);
475                 if(*path)
476                         path[strlen(path)-1] = 0;
477                 ExtractFilePath(path, parentpath);
478                 sprintf(minimapFilename, "%sgfx", parentpath);
479                 Q_mkdir(minimapFilename);
480                 sprintf(minimapFilename, "%sgfx/%s_mini.tga", parentpath, basename);
481                 Sys_Printf("Output file name automatically set to %s\n", minimapFilename);
482         }
483
484         if(minimapSharpen >= 0)
485         {
486                 minimap.sharpen_centermult = 8 * minimapSharpen + 1;
487                 minimap.sharpen_boxmult    =    -minimapSharpen;
488         }
489
490         minimap.data1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
491         data3b = safe_malloc(minimap.width * minimap.height * 3);
492         if(minimapSharpen >= 0)
493                 minimap.sharpendata1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
494
495         MiniMapSetupBrushes();
496
497         if(minimap.samples <= 1)
498         {
499                 Sys_Printf( "\n--- MiniMapNoSupersampling (%d) ---\n", minimap.height );
500                 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapNoSupersampling);
501         }
502         else
503         {
504                 if(minimap.sample_offsets)
505                 {
506                         Sys_Printf( "\n--- MiniMapSupersampled (%d) ---\n", minimap.height );
507                         RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSupersampled);
508                 }
509                 else
510                 {
511                         Sys_Printf( "\n--- MiniMapRandomlySupersampled (%d) ---\n", minimap.height );
512                         RunThreadsOnIndividual(minimap.height, qtrue, MiniMapRandomlySupersampled);
513                 }
514         }
515
516         if(minimap.sharpendata1f)
517         {
518                 Sys_Printf( "\n--- MiniMapSharpen (%d) ---\n", minimap.height );
519                 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSharpen);
520                 q = minimap.sharpendata1f;
521         }
522         else
523         {
524                 q = minimap.data1f;
525         }
526
527         Sys_Printf( "\nConverting...");
528         p = data3b;
529         for(y = 0; y < minimap.height; ++y)
530                 for(x = 0; x < minimap.width; ++x)
531                 {
532                         byte b;
533                         float v = *q++;
534                         if(v < 0) v = 0;
535                         if(v > 255.0/256.0) v = 255.0/256.0;
536                         b = v * 256;
537                         *p++ = b;
538                         *p++ = b;
539                         *p++ = b;
540                 }
541
542         Sys_Printf( " writing to %s...", minimapFilename );
543         WriteTGA24(minimapFilename, data3b, minimap.width, minimap.height, qfalse);
544
545         Sys_Printf( " done.\n" );
546
547         /* return to sender */
548         return 0;
549 }
550
551
552
553
554
555 /*
556 MD4BlockChecksum()
557 calculates an md4 checksum for a block of data
558 */
559
560 static int MD4BlockChecksum( void *buffer, int length )
561 {
562         return Com_BlockChecksum(buffer, length);
563 }
564
565 /*
566 FixAAS()
567 resets an aas checksum to match the given BSP
568 */
569
570 int FixAAS( int argc, char **argv )
571 {
572         int                     length, checksum;
573         void            *buffer;
574         FILE            *file;
575         char            aas[ 1024 ], **ext;
576         char            *exts[] =
577                                 {
578                                         ".aas",
579                                         "_b0.aas",
580                                         "_b1.aas",
581                                         NULL
582                                 };
583         
584         
585         /* arg checking */
586         if( argc < 2 )
587         {
588                 Sys_Printf( "Usage: q3map -fixaas [-v] <mapname>\n" );
589                 return 0;
590         }
591         
592         /* do some path mangling */
593         strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
594         StripExtension( source );
595         DefaultExtension( source, ".bsp" );
596         
597         /* note it */
598         Sys_Printf( "--- FixAAS ---\n" );
599         
600         /* load the bsp */
601         Sys_Printf( "Loading %s\n", source );
602         length = LoadFile( source, &buffer );
603         
604         /* create bsp checksum */
605         Sys_Printf( "Creating checksum...\n" );
606         checksum = LittleLong( MD4BlockChecksum( buffer, length ) );
607         
608         /* write checksum to aas */
609         ext = exts;
610         while( *ext )
611         {
612                 /* mangle name */
613                 strcpy( aas, source );
614                 StripExtension( aas );
615                 strcat( aas, *ext );
616                 Sys_Printf( "Trying %s\n", aas );
617                 ext++;
618                 
619                 /* fix it */
620                 file = fopen( aas, "r+b" );
621                 if( !file )
622                         continue;
623                 if( fwrite( &checksum, 4, 1, file ) != 1 )
624                         Error( "Error writing checksum to %s", aas );
625                 fclose( file );
626         }
627         
628         /* return to sender */
629         return 0;
630 }
631
632
633
634 /*
635 AnalyzeBSP() - ydnar
636 analyzes a Quake engine BSP file
637 */
638
639 typedef struct abspHeader_s
640 {
641         char                    ident[ 4 ];
642         int                             version;
643         
644         bspLump_t               lumps[ 1 ];     /* unknown size */
645 }
646 abspHeader_t;
647
648 typedef struct abspLumpTest_s
649 {
650         int                             radix, minCount;
651         char                    *name;
652 }
653 abspLumpTest_t;
654
655 int AnalyzeBSP( int argc, char **argv )
656 {
657         abspHeader_t                    *header;
658         int                                             size, i, version, offset, length, lumpInt, count;
659         char                                    ident[ 5 ];
660         void                                    *lump;
661         float                                   lumpFloat;
662         char                                    lumpString[ 1024 ], source[ 1024 ];
663         qboolean                                lumpSwap = qfalse;
664         abspLumpTest_t                  *lumpTest;
665         static abspLumpTest_t   lumpTests[] =
666                                                         {
667                                                                 { sizeof( bspPlane_t ),                 6,              "IBSP LUMP_PLANES" },
668                                                                 { sizeof( bspBrush_t ),                 1,              "IBSP LUMP_BRUSHES" },
669                                                                 { 8,                                                    6,              "IBSP LUMP_BRUSHSIDES" },
670                                                                 { sizeof( bspBrushSide_t ),             6,              "RBSP LUMP_BRUSHSIDES" },
671                                                                 { sizeof( bspModel_t ),                 1,              "IBSP LUMP_MODELS" },
672                                                                 { sizeof( bspNode_t ),                  2,              "IBSP LUMP_NODES" },
673                                                                 { sizeof( bspLeaf_t ),                  1,              "IBSP LUMP_LEAFS" },
674                                                                 { 104,                                                  3,              "IBSP LUMP_DRAWSURFS" },
675                                                                 { 44,                                                   3,              "IBSP LUMP_DRAWVERTS" },
676                                                                 { 4,                                                    6,              "IBSP LUMP_DRAWINDEXES" },
677                                                                 { 128 * 128 * 3,                                1,              "IBSP LUMP_LIGHTMAPS" },
678                                                                 { 256 * 256 * 3,                                1,              "IBSP LUMP_LIGHTMAPS (256 x 256)" },
679                                                                 { 512 * 512 * 3,                                1,              "IBSP LUMP_LIGHTMAPS (512 x 512)" },
680                                                                 { 0, 0, NULL }
681                                                         };
682         
683         
684         /* arg checking */
685         if( argc < 1 )
686         {
687                 Sys_Printf( "Usage: q3map -analyze [-lumpswap] [-v] <mapname>\n" );
688                 return 0;
689         }
690         
691         /* process arguments */
692         for( i = 1; i < (argc - 1); i++ )
693         {
694                 /* -format map|ase|... */
695                 if( !strcmp( argv[ i ],  "-lumpswap" ) )
696                 {
697                         Sys_Printf( "Swapped lump structs enabled\n" );
698                         lumpSwap = qtrue;
699                 }
700         }
701         
702         /* clean up map name */
703         strcpy( source, ExpandArg( argv[ i ] ) );
704         Sys_Printf( "Loading %s\n", source );
705         
706         /* load the file */
707         size = LoadFile( source, (void**) &header );
708         if( size == 0 || header == NULL )
709         {
710                 Sys_Printf( "Unable to load %s.\n", source );
711                 return -1;
712         }
713         
714         /* analyze ident/version */
715         memcpy( ident, header->ident, 4 );
716         ident[ 4 ] = '\0';
717         version = LittleLong( header->version );
718         
719         Sys_Printf( "Identity:      %s\n", ident );
720         Sys_Printf( "Version:       %d\n", version );
721         Sys_Printf( "---------------------------------------\n" );
722         
723         /* analyze each lump */
724         for( i = 0; i < 100; i++ )
725         {
726                 /* call of duty swapped lump pairs */
727                 if( lumpSwap )
728                 {
729                         offset = LittleLong( header->lumps[ i ].length );
730                         length = LittleLong( header->lumps[ i ].offset );
731                 }
732                 
733                 /* standard lump pairs */
734                 else
735                 {
736                         offset = LittleLong( header->lumps[ i ].offset );
737                         length = LittleLong( header->lumps[ i ].length );
738                 }
739                 
740                 /* extract data */
741                 lump = (byte*) header + offset;
742                 lumpInt = LittleLong( (int) *((int*) lump) );
743                 lumpFloat = LittleFloat( (float) *((float*) lump) );
744                 memcpy( lumpString, (char*) lump, (length < 1024 ? length : 1024) );
745                 lumpString[ 1024 ] = '\0';
746                 
747                 /* print basic lump info */
748                 Sys_Printf( "Lump:          %d\n", i );
749                 Sys_Printf( "Offset:        %d bytes\n", offset );
750                 Sys_Printf( "Length:        %d bytes\n", length );
751                 
752                 /* only operate on valid lumps */
753                 if( length > 0 )
754                 {
755                         /* print data in 4 formats */
756                         Sys_Printf( "As hex:        %08X\n", lumpInt );
757                         Sys_Printf( "As int:        %d\n", lumpInt );
758                         Sys_Printf( "As float:      %f\n", lumpFloat );
759                         Sys_Printf( "As string:     %s\n", lumpString );
760                         
761                         /* guess lump type */
762                         if( lumpString[ 0 ] == '{' && lumpString[ 2 ] == '"' )
763                                 Sys_Printf( "Type guess:    IBSP LUMP_ENTITIES\n" );
764                         else if( strstr( lumpString, "textures/" ) )
765                                 Sys_Printf( "Type guess:    IBSP LUMP_SHADERS\n" );
766                         else
767                         {
768                                 /* guess based on size/count */
769                                 for( lumpTest = lumpTests; lumpTest->radix > 0; lumpTest++ )
770                                 {
771                                         if( (length % lumpTest->radix) != 0 )
772                                                 continue;
773                                         count = length / lumpTest->radix;
774                                         if( count < lumpTest->minCount )
775                                                 continue;
776                                         Sys_Printf( "Type guess:    %s (%d x %d)\n", lumpTest->name, count, lumpTest->radix );
777                                 }
778                         }
779                 }
780                 
781                 Sys_Printf( "---------------------------------------\n" );
782                 
783                 /* end of file */
784                 if( offset + length >= size )
785                         break;
786         }
787         
788         /* last stats */
789         Sys_Printf( "Lump count:    %d\n", i + 1 );
790         Sys_Printf( "File size:     %d bytes\n", size );
791         
792         /* return to caller */
793         return 0;
794 }
795
796
797
798 /*
799 BSPInfo()
800 emits statistics about the bsp file
801 */
802
803 int BSPInfo( int count, char **fileNames )
804 {
805         int                     i;
806         char            source[ 1024 ], ext[ 64 ];
807         int                     size;
808         FILE            *f;
809         
810         
811         /* dummy check */
812         if( count < 1 )
813         {
814                 Sys_Printf( "No files to dump info for.\n");
815                 return -1;
816         }
817         
818         /* enable info mode */
819         infoMode = qtrue;
820         
821         /* walk file list */
822         for( i = 0; i < count; i++ )
823         {
824                 Sys_Printf( "---------------------------------\n" );
825                 
826                 /* mangle filename and get size */
827                 strcpy( source, fileNames[ i ] );
828                 ExtractFileExtension( source, ext );
829                 if( !Q_stricmp( ext, "map" ) )
830                         StripExtension( source );
831                 DefaultExtension( source, ".bsp" );
832                 f = fopen( source, "rb" );
833                 if( f )
834                 {
835                         size = Q_filelength (f);
836                         fclose( f );
837                 }
838                 else
839                         size = 0;
840                 
841                 /* load the bsp file and print lump sizes */
842                 Sys_Printf( "%s\n", source );
843                 LoadBSPFile( source );          
844                 PrintBSPFileSizes();
845                 
846                 /* print sizes */
847                 Sys_Printf( "\n" );
848                 Sys_Printf( "          total         %9d\n", size );
849                 Sys_Printf( "                        %9d KB\n", size / 1024 );
850                 Sys_Printf( "                        %9d MB\n", size / (1024 * 1024) );
851                 
852                 Sys_Printf( "---------------------------------\n" );
853         }
854         
855         /* return count */
856         return i;
857 }
858
859
860 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)
861 {
862         vec4_t scoeffs, tcoeffs;
863         float md;
864         m4x4_t solvematrix;
865
866         vec3_t norm;
867         vec3_t dab, dac;
868         VectorSubtract(bxyz, axyz, dab);
869         VectorSubtract(cxyz, axyz, dac);
870         CrossProduct(dab, dac, norm);
871         
872         // assume:
873         //   s = f(x, y, z)
874         //   s(v + norm) = s(v) when n ortho xyz
875         
876         // s(v) = DotProduct(v, scoeffs) + scoeffs[3]
877
878         // solve:
879         //   scoeffs * (axyz, 1) == ast[0]
880         //   scoeffs * (bxyz, 1) == bst[0]
881         //   scoeffs * (cxyz, 1) == cst[0]
882         //   scoeffs * (norm, 0) == 0
883         // scoeffs * [axyz, 1 | bxyz, 1 | cxyz, 1 | norm, 0] = [ast[0], bst[0], cst[0], 0]
884         solvematrix[0] = axyz[0];
885         solvematrix[4] = axyz[1];
886         solvematrix[8] = axyz[2];
887         solvematrix[12] = 1;
888         solvematrix[1] = bxyz[0];
889         solvematrix[5] = bxyz[1];
890         solvematrix[9] = bxyz[2];
891         solvematrix[13] = 1;
892         solvematrix[2] = cxyz[0];
893         solvematrix[6] = cxyz[1];
894         solvematrix[10] = cxyz[2];
895         solvematrix[14] = 1;
896         solvematrix[3] = norm[0];
897         solvematrix[7] = norm[1];
898         solvematrix[11] = norm[2];
899         solvematrix[15] = 0;
900
901         md = m4_det(solvematrix);
902         if(md*md < 1e-10)
903         {
904                 Sys_Printf("Cannot invert some matrix, some texcoords aren't extrapolated!");
905                 return;
906         }
907
908         m4x4_invert(solvematrix);
909
910         scoeffs[0] = ast[0];
911         scoeffs[1] = bst[0];
912         scoeffs[2] = cst[0];
913         scoeffs[3] = 0;
914         m4x4_transform_vec4(solvematrix, scoeffs);
915         tcoeffs[0] = ast[1];
916         tcoeffs[1] = bst[1];
917         tcoeffs[2] = cst[1];
918         tcoeffs[3] = 0;
919         m4x4_transform_vec4(solvematrix, tcoeffs);
920
921         ast_out[0] = scoeffs[0] * axyz_new[0] + scoeffs[1] * axyz_new[1] + scoeffs[2] * axyz_new[2] + scoeffs[3];
922         ast_out[1] = tcoeffs[0] * axyz_new[0] + tcoeffs[1] * axyz_new[1] + tcoeffs[2] * axyz_new[2] + tcoeffs[3];
923         bst_out[0] = scoeffs[0] * bxyz_new[0] + scoeffs[1] * bxyz_new[1] + scoeffs[2] * bxyz_new[2] + scoeffs[3];
924         bst_out[1] = tcoeffs[0] * bxyz_new[0] + tcoeffs[1] * bxyz_new[1] + tcoeffs[2] * bxyz_new[2] + tcoeffs[3];
925         cst_out[0] = scoeffs[0] * cxyz_new[0] + scoeffs[1] * cxyz_new[1] + scoeffs[2] * cxyz_new[2] + scoeffs[3];
926         cst_out[1] = tcoeffs[0] * cxyz_new[0] + tcoeffs[1] * cxyz_new[1] + tcoeffs[2] * cxyz_new[2] + tcoeffs[3];
927 }
928
929 /*
930 ScaleBSPMain()
931 amaze and confuse your enemies with wierd scaled maps!
932 */
933
934 int ScaleBSPMain( int argc, char **argv )
935 {
936         int                     i, j;
937         float           f, a;
938         vec3_t scale;
939         vec3_t          vec;
940         char            str[ 1024 ];
941         int uniform, axis;
942         qboolean texscale;
943         float *old_xyzst = NULL;
944         
945         
946         /* arg checking */
947         if( argc < 3 )
948         {
949                 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
950                 return 0;
951         }
952         
953         /* get scale */
954         scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
955         if(argc >= 4)
956                 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
957         if(argc >= 5)
958                 scale[0] = atof( argv[ argc - 4 ] );
959
960         texscale = !strcmp(argv[1], "-tex");
961         
962         uniform = ((scale[0] == scale[1]) && (scale[1] == scale[2]));
963
964         if( scale[0] == 0.0f || scale[1] == 0.0f || scale[2] == 0.0f )
965         {
966                 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
967                 Sys_Printf( "Non-zero scale value required.\n" );
968                 return 0;
969         }
970         
971         /* do some path mangling */
972         strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
973         StripExtension( source );
974         DefaultExtension( source, ".bsp" );
975         
976         /* load the bsp */
977         Sys_Printf( "Loading %s\n", source );
978         LoadBSPFile( source );
979         ParseEntities();
980         
981         /* note it */
982         Sys_Printf( "--- ScaleBSP ---\n" );
983         Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
984         
985         /* scale entity keys */
986         for( i = 0; i < numBSPEntities && i < numEntities; i++ )
987         {
988                 /* scale origin */
989                 GetVectorForKey( &entities[ i ], "origin", vec );
990                 if( (vec[ 0 ] || vec[ 1 ] || vec[ 2 ]) )
991                 {
992                         vec[0] *= scale[0];
993                         vec[1] *= scale[1];
994                         vec[2] *= scale[2];
995                         sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
996                         SetKeyValue( &entities[ i ], "origin", str );
997                 }
998
999                 a = FloatForKey( &entities[ i ], "angle" );
1000                 if(a == -1 || a == -2) // z scale
1001                         axis = 2;
1002                 else if(fabs(sin(DEG2RAD(a))) < 0.707)
1003                         axis = 0;
1004                 else
1005                         axis = 1;
1006                 
1007                 /* scale door lip */
1008                 f = FloatForKey( &entities[ i ], "lip" );
1009                 if( f )
1010                 {
1011                         f *= scale[axis];
1012                         sprintf( str, "%f", f );
1013                         SetKeyValue( &entities[ i ], "lip", str );
1014                 }
1015                 
1016                 /* scale plat height */
1017                 f = FloatForKey( &entities[ i ], "height" );
1018                 if( f )
1019                 {
1020                         f *= scale[2];
1021                         sprintf( str, "%f", f );
1022                         SetKeyValue( &entities[ i ], "height", str );
1023                 }
1024
1025                 // TODO maybe allow a definition file for entities to specify which values are scaled how?
1026         }
1027         
1028         /* scale models */
1029         for( i = 0; i < numBSPModels; i++ )
1030         {
1031                 bspModels[ i ].mins[0] *= scale[0];
1032                 bspModels[ i ].mins[1] *= scale[1];
1033                 bspModels[ i ].mins[2] *= scale[2];
1034                 bspModels[ i ].maxs[0] *= scale[0];
1035                 bspModels[ i ].maxs[1] *= scale[1];
1036                 bspModels[ i ].maxs[2] *= scale[2];
1037         }
1038         
1039         /* scale nodes */
1040         for( i = 0; i < numBSPNodes; i++ )
1041         {
1042                 bspNodes[ i ].mins[0] *= scale[0];
1043                 bspNodes[ i ].mins[1] *= scale[1];
1044                 bspNodes[ i ].mins[2] *= scale[2];
1045                 bspNodes[ i ].maxs[0] *= scale[0];
1046                 bspNodes[ i ].maxs[1] *= scale[1];
1047                 bspNodes[ i ].maxs[2] *= scale[2];
1048         }
1049         
1050         /* scale leafs */
1051         for( i = 0; i < numBSPLeafs; i++ )
1052         {
1053                 bspLeafs[ i ].mins[0] *= scale[0];
1054                 bspLeafs[ i ].mins[1] *= scale[1];
1055                 bspLeafs[ i ].mins[2] *= scale[2];
1056                 bspLeafs[ i ].maxs[0] *= scale[0];
1057                 bspLeafs[ i ].maxs[1] *= scale[1];
1058                 bspLeafs[ i ].maxs[2] *= scale[2];
1059         }
1060         
1061         if(texscale)
1062         {
1063                 Sys_Printf("Using texture unlocking (and probably breaking texture alignment a lot)\n");
1064                 old_xyzst = safe_malloc(sizeof(*old_xyzst) * numBSPDrawVerts * 5);
1065                 for(i = 0; i < numBSPDrawVerts; i++)
1066                 {
1067                         old_xyzst[5*i+0] = bspDrawVerts[i].xyz[0];
1068                         old_xyzst[5*i+1] = bspDrawVerts[i].xyz[1];
1069                         old_xyzst[5*i+2] = bspDrawVerts[i].xyz[2];
1070                         old_xyzst[5*i+3] = bspDrawVerts[i].st[0];
1071                         old_xyzst[5*i+4] = bspDrawVerts[i].st[1];
1072                 }
1073         }
1074
1075         /* scale drawverts */
1076         for( i = 0; i < numBSPDrawVerts; i++ )
1077         {
1078                 bspDrawVerts[i].xyz[0] *= scale[0];
1079                 bspDrawVerts[i].xyz[1] *= scale[1];
1080                 bspDrawVerts[i].xyz[2] *= scale[2];
1081                 bspDrawVerts[i].normal[0] /= scale[0];
1082                 bspDrawVerts[i].normal[1] /= scale[1];
1083                 bspDrawVerts[i].normal[2] /= scale[2];
1084                 VectorNormalize(bspDrawVerts[i].normal, bspDrawVerts[i].normal);
1085         }
1086
1087         if(texscale)
1088         {
1089                 for(i = 0; i < numBSPDrawSurfaces; i++)
1090                 {
1091                         switch(bspDrawSurfaces[i].surfaceType)
1092                         {
1093                                 case SURFACE_FACE:
1094                                 case SURFACE_META:
1095                                         if(bspDrawSurfaces[i].numIndexes % 3)
1096                                                 Error("Not a triangulation!");
1097                                         for(j = bspDrawSurfaces[i].firstIndex; j < bspDrawSurfaces[i].firstIndex + bspDrawSurfaces[i].numIndexes; j += 3)
1098                                         {
1099                                                 int ia = bspDrawIndexes[j] + bspDrawSurfaces[i].firstVert, ib = bspDrawIndexes[j+1] + bspDrawSurfaces[i].firstVert, ic = bspDrawIndexes[j+2] + bspDrawSurfaces[i].firstVert;
1100                                                 bspDrawVert_t *a = &bspDrawVerts[ia], *b = &bspDrawVerts[ib], *c = &bspDrawVerts[ic];
1101                                                 float *oa = &old_xyzst[ia*5], *ob = &old_xyzst[ib*5], *oc = &old_xyzst[ic*5];
1102                                                 // extrapolate:
1103                                                 //   a->xyz -> oa
1104                                                 //   b->xyz -> ob
1105                                                 //   c->xyz -> oc
1106                                                 ExtrapolateTexcoords(
1107                                                         &oa[0], &oa[3],
1108                                                         &ob[0], &ob[3],
1109                                                         &oc[0], &oc[3],
1110                                                         a->xyz, a->st,
1111                                                         b->xyz, b->st,
1112                                                         c->xyz, c->st);
1113                                         }
1114                                         break;
1115                         }
1116                 }
1117         }
1118         
1119         /* scale planes */
1120         if(uniform)
1121         {
1122                 for( i = 0; i < numBSPPlanes; i++ )
1123                 {
1124                         bspPlanes[ i ].dist *= scale[0];
1125                 }
1126         }
1127         else
1128         {
1129                 for( i = 0; i < numBSPPlanes; i++ )
1130                 {
1131                         bspPlanes[ i ].normal[0] /= scale[0];
1132                         bspPlanes[ i ].normal[1] /= scale[1];
1133                         bspPlanes[ i ].normal[2] /= scale[2];
1134                         f = 1/VectorLength(bspPlanes[i].normal);
1135                         VectorScale(bspPlanes[i].normal, f, bspPlanes[i].normal);
1136                         bspPlanes[ i ].dist *= f;
1137                 }
1138         }
1139         
1140         /* scale gridsize */
1141         GetVectorForKey( &entities[ 0 ], "gridsize", vec );
1142         if( (vec[ 0 ] + vec[ 1 ] + vec[ 2 ]) == 0.0f )
1143                 VectorCopy( gridSize, vec );
1144         vec[0] *= scale[0];
1145         vec[1] *= scale[1];
1146         vec[2] *= scale[2];
1147         sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1148         SetKeyValue( &entities[ 0 ], "gridsize", str );
1149
1150         /* inject command line parameters */
1151         InjectCommandLine(argv, 0, argc - 1);
1152         
1153         /* write the bsp */
1154         UnparseEntities();
1155         StripExtension( source );
1156         DefaultExtension( source, "_s.bsp" );
1157         Sys_Printf( "Writing %s\n", source );
1158         WriteBSPFile( source );
1159         
1160         /* return to sender */
1161         return 0;
1162 }
1163
1164
1165
1166 /*
1167 ConvertBSPMain()
1168 main argument processing function for bsp conversion
1169 */
1170
1171 int ConvertBSPMain( int argc, char **argv )
1172 {
1173         int             i;
1174         int             (*convertFunc)( char * );
1175         game_t  *convertGame;
1176         
1177         
1178         /* set default */
1179         convertFunc = ConvertBSPToASE;
1180         convertGame = NULL;
1181         
1182         /* arg checking */
1183         if( argc < 1 )
1184         {
1185                 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
1186                 return 0;
1187         }
1188         
1189         /* process arguments */
1190         for( i = 1; i < (argc - 1); i++ )
1191         {
1192                 /* -format map|ase|... */
1193                 if( !strcmp( argv[ i ],  "-format" ) )
1194                 {
1195                         i++;
1196                         if( !Q_stricmp( argv[ i ], "ase" ) )
1197                                 convertFunc = ConvertBSPToASE;
1198                         else if( !Q_stricmp( argv[ i ], "map" ) )
1199                                 convertFunc = ConvertBSPToMap;
1200                         else
1201                         {
1202                                 convertGame = GetGame( argv[ i ] );
1203                                 if( convertGame == NULL )
1204                                         Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
1205                         }
1206                 }
1207                 else if( !strcmp( argv[ i ],  "-ne" ) )
1208                 {
1209                         normalEpsilon = atof( argv[ i + 1 ] );
1210                         i++;
1211                         Sys_Printf( "Normal epsilon set to %f\n", normalEpsilon );
1212                 }
1213                 else if( !strcmp( argv[ i ],  "-de" ) )
1214                 {
1215                         distanceEpsilon = atof( argv[ i + 1 ] );
1216                         i++;
1217                         Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
1218                 }
1219                 else if( !strcmp( argv[ i ],  "-shadersasbitmap" ) )
1220                         shadersAsBitmap = qtrue;
1221         }
1222         
1223         /* clean up map name */
1224         strcpy( source, ExpandArg( argv[ i ] ) );
1225         StripExtension( source );
1226         DefaultExtension( source, ".bsp" );
1227         
1228         LoadShaderInfo();
1229         
1230         Sys_Printf( "Loading %s\n", source );
1231         
1232         /* ydnar: load surface file */
1233         //%     LoadSurfaceExtraFile( source );
1234         
1235         LoadBSPFile( source );
1236         
1237         /* parse bsp entities */
1238         ParseEntities();
1239         
1240         /* bsp format convert? */
1241         if( convertGame != NULL )
1242         {
1243                 /* set global game */
1244                 game = convertGame;
1245                 
1246                 /* write bsp */
1247                 StripExtension( source );
1248                 DefaultExtension( source, "_c.bsp" );
1249                 Sys_Printf( "Writing %s\n", source );
1250                 WriteBSPFile( source );
1251                 
1252                 /* return to sender */
1253                 return 0;
1254         }
1255         
1256         /* normal convert */
1257         return convertFunc( source );
1258 }
1259
1260
1261
1262 /*
1263 main()
1264 q3map mojo...
1265 */
1266
1267 int main( int argc, char **argv )
1268 {
1269         int             i, r;
1270         double  start, end;
1271         
1272         
1273         /* we want consistent 'randomness' */
1274         srand( 0 );
1275         
1276         /* start timer */
1277         start = I_FloatTime();
1278
1279         /* this was changed to emit version number over the network */
1280         printf( Q3MAP_VERSION "\n" );
1281         
1282         /* set exit call */
1283         atexit( ExitQ3Map );
1284
1285         /* read general options first */
1286         for( i = 1; i < argc; i++ )
1287         {
1288                 /* -connect */
1289                 if( !strcmp( argv[ i ], "-connect" ) )
1290                 {
1291                         argv[ i ] = NULL;
1292                         i++;
1293                         Broadcast_Setup( argv[ i ] );
1294                         argv[ i ] = NULL;
1295                 }
1296                 
1297                 /* verbose */
1298                 else if( !strcmp( argv[ i ], "-v" ) )
1299                 {
1300                         if(!verbose)
1301                         {
1302                                 verbose = qtrue;
1303                                 argv[ i ] = NULL;
1304                         }
1305                 }
1306                 
1307                 /* force */
1308                 else if( !strcmp( argv[ i ], "-force" ) )
1309                 {
1310                         force = qtrue;
1311                         argv[ i ] = NULL;
1312                 }
1313                 
1314                 /* patch subdivisions */
1315                 else if( !strcmp( argv[ i ], "-subdivisions" ) )
1316                 {
1317                         argv[ i ] = NULL;
1318                         i++;
1319                         patchSubdivisions = atoi( argv[ i ] );
1320                         argv[ i ] = NULL;
1321                         if( patchSubdivisions <= 0 )
1322                                 patchSubdivisions = 1;
1323                 }
1324                 
1325                 /* threads */
1326                 else if( !strcmp( argv[ i ], "-threads" ) )
1327                 {
1328                         argv[ i ] = NULL;
1329                         i++;
1330                         numthreads = atoi( argv[ i ] );
1331                         argv[ i ] = NULL;
1332                 }
1333         }
1334         
1335         /* init model library */
1336         PicoInit();
1337         PicoSetMallocFunc( safe_malloc );
1338         PicoSetFreeFunc( free );
1339         PicoSetPrintFunc( PicoPrintFunc );
1340         PicoSetLoadFileFunc( PicoLoadFileFunc );
1341         PicoSetFreeFileFunc( free );
1342         
1343         /* set number of threads */
1344         ThreadSetDefault();
1345         
1346         /* generate sinusoid jitter table */
1347         for( i = 0; i < MAX_JITTERS; i++ )
1348         {
1349                 jitters[ i ] = sin( i * 139.54152147 );
1350                 //%     Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
1351         }
1352         
1353         /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
1354            and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
1355         
1356         Sys_Printf( "Q3Map         - v1.0r (c) 1999 Id Software Inc.\n" );
1357         Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
1358         Sys_Printf( "NetRadiant    - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
1359         Sys_Printf( "%s\n", Q3MAP_MOTD );
1360         
1361         /* ydnar: new path initialization */
1362         InitPaths( &argc, argv );
1363
1364         /* set game options */
1365         if (!patchSubdivisions)
1366                 patchSubdivisions = game->patchSubdivisions;
1367         
1368         /* check if we have enough options left to attempt something */
1369         if( argc < 2 )
1370                 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
1371         
1372         /* fixaas */
1373         if( !strcmp( argv[ 1 ], "-fixaas" ) )
1374                 r = FixAAS( argc - 1, argv + 1 );
1375         
1376         /* analyze */
1377         else if( !strcmp( argv[ 1 ], "-analyze" ) )
1378                 r = AnalyzeBSP( argc - 1, argv + 1 );
1379         
1380         /* info */
1381         else if( !strcmp( argv[ 1 ], "-info" ) )
1382                 r = BSPInfo( argc - 2, argv + 2 );
1383         
1384         /* vis */
1385         else if( !strcmp( argv[ 1 ], "-vis" ) )
1386                 r = VisMain( argc - 1, argv + 1 );
1387         
1388         /* light */
1389         else if( !strcmp( argv[ 1 ], "-light" ) )
1390                 r = LightMain( argc - 1, argv + 1 );
1391         
1392         /* vlight */
1393         else if( !strcmp( argv[ 1 ], "-vlight" ) )
1394         {
1395                 Sys_Printf( "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
1396                 argv[ 1 ] = "-fast";    /* eek a hack */
1397                 r = LightMain( argc, argv );
1398         }
1399         
1400         /* ydnar: lightmap export */
1401         else if( !strcmp( argv[ 1 ], "-export" ) )
1402                 r = ExportLightmapsMain( argc - 1, argv + 1 );
1403         
1404         /* ydnar: lightmap import */
1405         else if( !strcmp( argv[ 1 ], "-import" ) )
1406                 r = ImportLightmapsMain( argc - 1, argv + 1 );
1407         
1408         /* ydnar: bsp scaling */
1409         else if( !strcmp( argv[ 1 ], "-scale" ) )
1410                 r = ScaleBSPMain( argc - 1, argv + 1 );
1411         
1412         /* ydnar: bsp conversion */
1413         else if( !strcmp( argv[ 1 ], "-convert" ) )
1414                 r = ConvertBSPMain( argc - 1, argv + 1 );
1415         
1416         /* div0: minimap */
1417         else if( !strcmp( argv[ 1 ], "-minimap" ) )
1418                 r = MiniMapBSPMain(argc - 1, argv + 1);
1419
1420         /* ydnar: otherwise create a bsp */
1421         else
1422                 r = BSPMain( argc, argv );
1423         
1424         /* emit time */
1425         end = I_FloatTime();
1426         Sys_Printf( "%9.0f seconds elapsed\n", end - start );
1427         
1428         /* shut down connection */
1429         Broadcast_Shutdown();
1430         
1431         /* return any error code */
1432         return r;
1433 }