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