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