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