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