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