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