]> git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/main.c
Q3map2:
[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         return (vec_t) rand() / RAND_MAX;
48 }
49
50
51 char *Q_strncpyz( char *dst, const char *src, size_t len ) {
52         if ( len == 0 ) {
53                 abort();
54         }
55
56         strncpy( dst, src, len );
57         dst[ len - 1 ] = '\0';
58         return dst;
59 }
60
61
62 char *Q_strcat( char *dst, size_t dlen, const char *src ) {
63         size_t n = strlen( dst );
64
65         if ( n > dlen ) {
66                 abort(); /* buffer overflow */
67         }
68
69         return Q_strncpyz( dst + n, src, dlen - n );
70 }
71
72
73 char *Q_strncat( char *dst, size_t dlen, const char *src, size_t slen ) {
74         size_t n = strlen( dst );
75
76         if ( n > dlen ) {
77                 abort(); /* buffer overflow */
78         }
79
80         return Q_strncpyz( dst + n, src, MIN( slen, dlen - n ) );
81 }
82
83
84 /*
85    ExitQ3Map()
86    cleanup routine
87  */
88
89 static void ExitQ3Map( void ){
90         BSPFilesCleanup();
91         if ( mapDrawSurfs != NULL ) {
92                 free( mapDrawSurfs );
93         }
94 }
95
96
97
98 /* minimap stuff */
99
100 typedef struct minimap_s
101 {
102         bspModel_t *model;
103         int width;
104         int height;
105         int samples;
106         float *sample_offsets;
107         float sharpen_boxmult;
108         float sharpen_centermult;
109         float boost, brightness, contrast;
110         float *data1f;
111         float *sharpendata1f;
112         vec3_t mins, size;
113 }
114 minimap_t;
115 static minimap_t minimap;
116
117 qboolean BrushIntersectionWithLine( bspBrush_t *brush, vec3_t start, vec3_t dir, float *t_in, float *t_out ){
118         int i;
119         qboolean in = qfalse, out = qfalse;
120         bspBrushSide_t *sides = &bspBrushSides[brush->firstSide];
121
122         for ( i = 0; i < brush->numSides; ++i )
123         {
124                 bspPlane_t *p = &bspPlanes[sides[i].planeNum];
125                 float sn = DotProduct( start, p->normal );
126                 float dn = DotProduct( dir, p->normal );
127                 if ( dn == 0 ) {
128                         if ( sn > p->dist ) {
129                                 return qfalse; // outside!
130                         }
131                 }
132                 else
133                 {
134                         float t = ( p->dist - sn ) / dn;
135                         if ( dn < 0 ) {
136                                 if ( !in || t > *t_in ) {
137                                         *t_in = t;
138                                         in = qtrue;
139                                         // as t_in can only increase, and t_out can only decrease, early out
140                                         if ( out && *t_in >= *t_out ) {
141                                                 return qfalse;
142                                         }
143                                 }
144                         }
145                         else
146                         {
147                                 if ( !out || t < *t_out ) {
148                                         *t_out = t;
149                                         out = qtrue;
150                                         // as t_in can only increase, and t_out can only decrease, early out
151                                         if ( in && *t_in >= *t_out ) {
152                                                 return qfalse;
153                                         }
154                                 }
155                         }
156                 }
157         }
158         return in && out;
159 }
160
161 static float MiniMapSample( float x, float y ){
162         vec3_t org, dir;
163         int i, bi;
164         float t0, t1;
165         float samp;
166         bspBrush_t *b;
167         bspBrushSide_t *s;
168         int cnt;
169
170         org[0] = x;
171         org[1] = y;
172         org[2] = 0;
173         dir[0] = 0;
174         dir[1] = 0;
175         dir[2] = 1;
176
177         cnt = 0;
178         samp = 0;
179         for ( i = 0; i < minimap.model->numBSPBrushes; ++i )
180         {
181                 bi = minimap.model->firstBSPBrush + i;
182                 if ( opaqueBrushes[bi >> 3] & ( 1 << ( bi & 7 ) ) ) {
183                         b = &bspBrushes[bi];
184
185                         // sort out mins/maxs of the brush
186                         s = &bspBrushSides[b->firstSide];
187                         if ( x < -bspPlanes[s[0].planeNum].dist ) {
188                                 continue;
189                         }
190                         if ( x > +bspPlanes[s[1].planeNum].dist ) {
191                                 continue;
192                         }
193                         if ( y < -bspPlanes[s[2].planeNum].dist ) {
194                                 continue;
195                         }
196                         if ( y > +bspPlanes[s[3].planeNum].dist ) {
197                                 continue;
198                         }
199
200                         if ( BrushIntersectionWithLine( b, org, dir, &t0, &t1 ) ) {
201                                 samp += t1 - t0;
202                                 ++cnt;
203                         }
204                 }
205         }
206
207         return samp;
208 }
209
210 void RandomVector2f( float v[2] ){
211         do
212         {
213                 v[0] = 2 * Random() - 1;
214                 v[1] = 2 * Random() - 1;
215         }
216         while ( v[0] * v[0] + v[1] * v[1] > 1 );
217 }
218
219 static void MiniMapRandomlySupersampled( int y ){
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         float uv[2];
226         float thisval;
227
228         for ( x = 0; x < minimap.width; ++x )
229         {
230                 float xmin = minimap.mins[0] + minimap.size[0] * ( x / (float) minimap.width );
231                 float val = 0;
232
233                 for ( i = 0; i < minimap.samples; ++i )
234                 {
235                         RandomVector2f( uv );
236                         thisval = MiniMapSample(
237                                 xmin + ( uv[0] + 0.5 ) * dx, /* exaggerated random pattern for better results */
238                                 ymin + ( uv[1] + 0.5 ) * dy  /* exaggerated random pattern for better results */
239                                 );
240                         val += thisval;
241                 }
242                 val /= minimap.samples * minimap.size[2];
243                 *p++ = val;
244         }
245 }
246
247 static void MiniMapSupersampled( int y ){
248         int x, i;
249         float *p = &minimap.data1f[y * minimap.width];
250         float ymin = minimap.mins[1] + minimap.size[1] * ( y / (float) minimap.height );
251         float dx   =                   minimap.size[0]      / (float) minimap.width;
252         float dy   =                   minimap.size[1]      / (float) minimap.height;
253
254         for ( x = 0; x < minimap.width; ++x )
255         {
256                 float xmin = minimap.mins[0] + minimap.size[0] * ( x / (float) minimap.width );
257                 float val = 0;
258
259                 for ( i = 0; i < minimap.samples; ++i )
260                 {
261                         float thisval = MiniMapSample(
262                                 xmin + minimap.sample_offsets[2 * i + 0] * dx,
263                                 ymin + minimap.sample_offsets[2 * i + 1] * dy
264                                 );
265                         val += thisval;
266                 }
267                 val /= minimap.samples * minimap.size[2];
268                 *p++ = val;
269         }
270 }
271
272 static void MiniMapNoSupersampling( int y ){
273         int x;
274         float *p = &minimap.data1f[y * minimap.width];
275         float ymin = minimap.mins[1] + minimap.size[1] * ( ( y + 0.5 ) / (float) minimap.height );
276
277         for ( x = 0; x < minimap.width; ++x )
278         {
279                 float xmin = minimap.mins[0] + minimap.size[0] * ( ( x + 0.5 ) / (float) minimap.width );
280                 *p++ = MiniMapSample( xmin, ymin ) / minimap.size[2];
281         }
282 }
283
284 static void MiniMapSharpen( int y ){
285         int x;
286         qboolean up = ( y > 0 );
287         qboolean down = ( y < minimap.height - 1 );
288         float *p = &minimap.data1f[y * minimap.width];
289         float *q = &minimap.sharpendata1f[y * minimap.width];
290
291         for ( x = 0; x < minimap.width; ++x )
292         {
293                 qboolean left = ( x > 0 );
294                 qboolean right = ( x < minimap.width - 1 );
295                 float val = p[0] * minimap.sharpen_centermult;
296
297                 if ( left && up ) {
298                         val += p[-1 - minimap.width] * minimap.sharpen_boxmult;
299                 }
300                 if ( left && down ) {
301                         val += p[-1 + minimap.width] * minimap.sharpen_boxmult;
302                 }
303                 if ( right && up ) {
304                         val += p[+1 - minimap.width] * minimap.sharpen_boxmult;
305                 }
306                 if ( right && down ) {
307                         val += p[+1 + minimap.width] * minimap.sharpen_boxmult;
308                 }
309
310                 if ( left ) {
311                         val += p[-1] * minimap.sharpen_boxmult;
312                 }
313                 if ( right ) {
314                         val += p[+1] * minimap.sharpen_boxmult;
315                 }
316                 if ( up ) {
317                         val += p[-minimap.width] * minimap.sharpen_boxmult;
318                 }
319                 if ( down ) {
320                         val += p[+minimap.width] * minimap.sharpen_boxmult;
321                 }
322
323                 ++p;
324                 *q++ = val;
325         }
326 }
327
328 static void MiniMapContrastBoost( int y ){
329         int x;
330         float *q = &minimap.data1f[y * minimap.width];
331         for ( x = 0; x < minimap.width; ++x )
332         {
333                 *q = *q * minimap.boost / ( ( minimap.boost - 1 ) * *q + 1 );
334                 ++q;
335         }
336 }
337
338 static void MiniMapBrightnessContrast( int y ){
339         int x;
340         float *q = &minimap.data1f[y * minimap.width];
341         for ( x = 0; x < minimap.width; ++x )
342         {
343                 *q = *q * minimap.contrast + minimap.brightness;
344                 ++q;
345         }
346 }
347
348 void MiniMapMakeMinsMaxs( vec3_t mins_in, vec3_t maxs_in, float border, qboolean keepaspect ){
349         vec3_t mins, maxs, extend;
350         VectorCopy( mins_in, mins );
351         VectorCopy( maxs_in, maxs );
352
353         // line compatible to nexuiz mapinfo
354         Sys_Printf( "size %f %f %f %f %f %f\n", mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2] );
355
356         if ( keepaspect ) {
357                 VectorSubtract( maxs, mins, extend );
358                 if ( extend[1] > extend[0] ) {
359                         mins[0] -= ( extend[1] - extend[0] ) * 0.5;
360                         maxs[0] += ( extend[1] - extend[0] ) * 0.5;
361                 }
362                 else
363                 {
364                         mins[1] -= ( extend[0] - extend[1] ) * 0.5;
365                         maxs[1] += ( extend[0] - extend[1] ) * 0.5;
366                 }
367         }
368
369         /* border: amount of black area around the image */
370         /* input: border, 1-2*border, border but we need border/(1-2*border) */
371
372         VectorSubtract( maxs, mins, extend );
373         VectorScale( extend, border / ( 1 - 2 * border ), extend );
374
375         VectorSubtract( mins, extend, mins );
376         VectorAdd( maxs, extend, maxs );
377
378         VectorCopy( mins, minimap.mins );
379         VectorSubtract( maxs, mins, minimap.size );
380
381         // line compatible to nexuiz mapinfo
382         Sys_Printf( "size_texcoords %f %f %f %f %f %f\n", mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2] );
383 }
384
385 /*
386    MiniMapSetupBrushes()
387    determines solid non-sky brushes in the world
388  */
389
390 void MiniMapSetupBrushes( void ){
391         SetupBrushesFlags( C_SOLID | C_SKY, C_SOLID, 0, 0 );
392         // at least one must be solid
393         // none may be sky
394         // not all may be nodraw
395 }
396
397 qboolean MiniMapEvaluateSampleOffsets( int *bestj, int *bestk, float *bestval ){
398         float val, dx, dy;
399         int j, k;
400
401         *bestj = *bestk = -1;
402         *bestval = 3; /* max possible val is 2 */
403
404         for ( j = 0; j < minimap.samples; ++j )
405                 for ( k = j + 1; k < minimap.samples; ++k )
406                 {
407                         dx = minimap.sample_offsets[2 * j + 0] - minimap.sample_offsets[2 * k + 0];
408                         dy = minimap.sample_offsets[2 * j + 1] - minimap.sample_offsets[2 * k + 1];
409                         if ( dx > +0.5 ) {
410                                 dx -= 1;
411                         }
412                         if ( dx < -0.5 ) {
413                                 dx += 1;
414                         }
415                         if ( dy > +0.5 ) {
416                                 dy -= 1;
417                         }
418                         if ( dy < -0.5 ) {
419                                 dy += 1;
420                         }
421                         val = dx * dx + dy * dy;
422                         if ( val < *bestval ) {
423                                 *bestj = j;
424                                 *bestk = k;
425                                 *bestval = val;
426                         }
427                 }
428
429         return *bestval < 3;
430 }
431
432 void MiniMapMakeSampleOffsets(){
433         int i, j, k, jj, kk;
434         float val, valj, valk, sx, sy, rx, ry;
435
436         Sys_Printf( "Generating good sample offsets (this may take a while)...\n" );
437
438         /* start with entirely random samples */
439         for ( i = 0; i < minimap.samples; ++i )
440         {
441                 minimap.sample_offsets[2 * i + 0] = Random();
442                 minimap.sample_offsets[2 * i + 1] = Random();
443         }
444
445         for ( i = 0; i < 1000; ++i )
446         {
447                 if ( MiniMapEvaluateSampleOffsets( &j, &k, &val ) ) {
448                         sx = minimap.sample_offsets[2 * j + 0];
449                         sy = minimap.sample_offsets[2 * j + 1];
450                         minimap.sample_offsets[2 * j + 0] = rx = Random();
451                         minimap.sample_offsets[2 * j + 1] = ry = Random();
452                         if ( !MiniMapEvaluateSampleOffsets( &jj, &kk, &valj ) ) {
453                                 valj = -1;
454                         }
455                         minimap.sample_offsets[2 * j + 0] = sx;
456                         minimap.sample_offsets[2 * j + 1] = sy;
457
458                         sx = minimap.sample_offsets[2 * k + 0];
459                         sy = minimap.sample_offsets[2 * k + 1];
460                         minimap.sample_offsets[2 * k + 0] = rx;
461                         minimap.sample_offsets[2 * k + 1] = ry;
462                         if ( !MiniMapEvaluateSampleOffsets( &jj, &kk, &valk ) ) {
463                                 valk = -1;
464                         }
465                         minimap.sample_offsets[2 * k + 0] = sx;
466                         minimap.sample_offsets[2 * k + 1] = sy;
467
468                         if ( valj > valk ) {
469                                 if ( valj > val ) {
470                                         /* valj is the greatest */
471                                         minimap.sample_offsets[2 * j + 0] = rx;
472                                         minimap.sample_offsets[2 * j + 1] = ry;
473                                         i = -1;
474                                 }
475                                 else
476                                 {
477                                         /* valj is the greater and it is useless - forget it */
478                                 }
479                         }
480                         else
481                         {
482                                 if ( valk > val ) {
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
500 void MergeRelativePath( char *out, const char *absolute, const char *relative ){
501         const char *endpos = absolute + strlen( absolute );
502         while ( endpos != absolute && ( endpos[-1] == '/' || endpos[-1] == '\\' ) )
503                 --endpos;
504         while ( relative[0] == '.' && relative[1] == '.' && ( relative[2] == '/' || relative[2] == '\\' ) )
505         {
506                 relative += 3;
507                 while ( endpos != absolute )
508                 {
509                         --endpos;
510                         if ( *endpos == '/' || *endpos == '\\' ) {
511                                 break;
512                         }
513                 }
514                 while ( endpos != absolute && ( endpos[-1] == '/' || endpos[-1] == '\\' ) )
515                         --endpos;
516         }
517         memcpy( out, absolute, endpos - absolute );
518         out[endpos - absolute] = '/';
519         strcpy( out + ( endpos - absolute + 1 ), relative );
520 }
521
522 int MiniMapBSPMain( int argc, char **argv ){
523         char minimapFilename[1024];
524         char basename[1024];
525         char path[1024];
526         char relativeMinimapFilename[1024];
527         qboolean autolevel;
528         float minimapSharpen;
529         float border;
530         byte *data4b, *p;
531         float *q;
532         int x, y;
533         int i;
534         miniMapMode_t mode;
535         vec3_t mins, maxs;
536         qboolean keepaspect;
537
538         /* arg checking */
539         if ( argc < 2 ) {
540                 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" );
541                 return 0;
542         }
543
544         /* load the BSP first */
545         strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
546         StripExtension( source );
547         DefaultExtension( source, ".bsp" );
548         Sys_Printf( "Loading %s\n", source );
549         //BeginMapShaderFile( source ); //do not delete q3map2_*.shader on minimap generation
550         LoadShaderInfo();
551         LoadBSPFile( source );
552
553         minimap.model = &bspModels[0];
554         VectorCopy( minimap.model->mins, mins );
555         VectorCopy( minimap.model->maxs, maxs );
556
557         *minimapFilename = 0;
558         minimapSharpen = game->miniMapSharpen;
559         minimap.width = minimap.height = game->miniMapSize;
560         border = game->miniMapBorder;
561         keepaspect = game->miniMapKeepAspect;
562         mode = game->miniMapMode;
563
564         autolevel = qfalse;
565         minimap.samples = 1;
566         minimap.sample_offsets = NULL;
567         minimap.boost = 1.0;
568         minimap.brightness = 0.0;
569         minimap.contrast = 1.0;
570
571         /* process arguments */
572         for ( i = 1; i < ( argc - 1 ); i++ )
573         {
574                 if ( !strcmp( argv[ i ],  "-size" ) ) {
575                         minimap.width = minimap.height = atoi( argv[i + 1] );
576                         i++;
577                         Sys_Printf( "Image size set to %i\n", minimap.width );
578                 }
579                 else if ( !strcmp( argv[ i ],  "-sharpen" ) ) {
580                         minimapSharpen = atof( argv[i + 1] );
581                         i++;
582                         Sys_Printf( "Sharpening coefficient set to %f\n", minimapSharpen );
583                 }
584                 else if ( !strcmp( argv[ i ],  "-samples" ) ) {
585                         minimap.samples = atoi( argv[i + 1] );
586                         i++;
587                         Sys_Printf( "Samples set to %i\n", minimap.samples );
588                         if ( minimap.sample_offsets ) {
589                                 free( minimap.sample_offsets );
590                         }
591                         minimap.sample_offsets = malloc( 2 * sizeof( *minimap.sample_offsets ) * minimap.samples );
592                         MiniMapMakeSampleOffsets();
593                 }
594                 else if ( !strcmp( argv[ i ],  "-random" ) ) {
595                         minimap.samples = atoi( argv[i + 1] );
596                         i++;
597                         Sys_Printf( "Random samples set to %i\n", minimap.samples );
598                         if ( minimap.sample_offsets ) {
599                                 free( minimap.sample_offsets );
600                         }
601                         minimap.sample_offsets = NULL;
602                 }
603                 else if ( !strcmp( argv[ i ],  "-border" ) ) {
604                         border = atof( argv[i + 1] );
605                         i++;
606                         Sys_Printf( "Border set to %f\n", border );
607                 }
608                 else if ( !strcmp( argv[ i ],  "-keepaspect" ) ) {
609                         keepaspect = qtrue;
610                         Sys_Printf( "Keeping aspect ratio by letterboxing\n", border );
611                 }
612                 else if ( !strcmp( argv[ i ],  "-nokeepaspect" ) ) {
613                         keepaspect = qfalse;
614                         Sys_Printf( "Not keeping aspect ratio\n", border );
615                 }
616                 else if ( !strcmp( argv[ i ],  "-o" ) ) {
617                         strcpy( minimapFilename, argv[i + 1] );
618                         i++;
619                         Sys_Printf( "Output file name set to %s\n", minimapFilename );
620                 }
621                 else if ( !strcmp( argv[ i ],  "-minmax" ) && i < ( argc - 7 ) ) {
622                         mins[0] = atof( argv[i + 1] );
623                         mins[1] = atof( argv[i + 2] );
624                         mins[2] = atof( argv[i + 3] );
625                         maxs[0] = atof( argv[i + 4] );
626                         maxs[1] = atof( argv[i + 5] );
627                         maxs[2] = atof( argv[i + 6] );
628                         i += 6;
629                         Sys_Printf( "Map mins/maxs overridden\n" );
630                 }
631                 else if ( !strcmp( argv[ i ],  "-gray" ) ) {
632                         mode = MINIMAP_MODE_GRAY;
633                         Sys_Printf( "Writing as white-on-black image\n" );
634                 }
635                 else if ( !strcmp( argv[ i ],  "-black" ) ) {
636                         mode = MINIMAP_MODE_BLACK;
637                         Sys_Printf( "Writing as black alpha image\n" );
638                 }
639                 else if ( !strcmp( argv[ i ],  "-white" ) ) {
640                         mode = MINIMAP_MODE_WHITE;
641                         Sys_Printf( "Writing as white alpha image\n" );
642                 }
643                 else if ( !strcmp( argv[ i ],  "-boost" ) && i < ( argc - 2 ) ) {
644                         minimap.boost = atof( argv[i + 1] );
645                         i++;
646                         Sys_Printf( "Contrast boost set to %f\n", minimap.boost );
647                 }
648                 else if ( !strcmp( argv[ i ],  "-brightness" ) && i < ( argc - 2 ) ) {
649                         minimap.brightness = atof( argv[i + 1] );
650                         i++;
651                         Sys_Printf( "Brightness set to %f\n", minimap.brightness );
652                 }
653                 else if ( !strcmp( argv[ i ],  "-contrast" ) && i < ( argc - 2 ) ) {
654                         minimap.contrast = atof( argv[i + 1] );
655                         i++;
656                         Sys_Printf( "Contrast set to %f\n", minimap.contrast );
657                 }
658                 else if ( !strcmp( argv[ i ],  "-autolevel" ) ) {
659                         autolevel = qtrue;
660                         Sys_Printf( "Auto level enabled\n", border );
661                 }
662                 else if ( !strcmp( argv[ i ],  "-noautolevel" ) ) {
663                         autolevel = qfalse;
664                         Sys_Printf( "Auto level disabled\n", border );
665                 }
666         }
667
668         MiniMapMakeMinsMaxs( mins, maxs, border, keepaspect );
669
670         if ( !*minimapFilename ) {
671                 ExtractFileBase( source, basename );
672                 ExtractFilePath( source, path );
673                 sprintf( relativeMinimapFilename, game->miniMapNameFormat, basename );
674                 MergeRelativePath( minimapFilename, path, relativeMinimapFilename );
675                 Sys_Printf( "Output file name automatically set to %s\n", minimapFilename );
676         }
677         ExtractFilePath( minimapFilename, path );
678         Q_mkdir( path );
679
680         if ( minimapSharpen >= 0 ) {
681                 minimap.sharpen_centermult = 8 * minimapSharpen + 1;
682                 minimap.sharpen_boxmult    =    -minimapSharpen;
683         }
684
685         minimap.data1f = safe_malloc( minimap.width * minimap.height * sizeof( *minimap.data1f ) );
686         data4b = safe_malloc( minimap.width * minimap.height * 4 );
687         if ( minimapSharpen >= 0 ) {
688                 minimap.sharpendata1f = safe_malloc( minimap.width * minimap.height * sizeof( *minimap.data1f ) );
689         }
690
691         MiniMapSetupBrushes();
692
693         if ( minimap.samples <= 1 ) {
694                 Sys_Printf( "\n--- MiniMapNoSupersampling (%d) ---\n", minimap.height );
695                 RunThreadsOnIndividual( minimap.height, qtrue, MiniMapNoSupersampling );
696         }
697         else
698         {
699                 if ( minimap.sample_offsets ) {
700                         Sys_Printf( "\n--- MiniMapSupersampled (%d) ---\n", minimap.height );
701                         RunThreadsOnIndividual( minimap.height, qtrue, MiniMapSupersampled );
702                 }
703                 else
704                 {
705                         Sys_Printf( "\n--- MiniMapRandomlySupersampled (%d) ---\n", minimap.height );
706                         RunThreadsOnIndividual( minimap.height, qtrue, MiniMapRandomlySupersampled );
707                 }
708         }
709
710         if ( minimap.boost != 1.0 ) {
711                 Sys_Printf( "\n--- MiniMapContrastBoost (%d) ---\n", minimap.height );
712                 RunThreadsOnIndividual( minimap.height, qtrue, MiniMapContrastBoost );
713         }
714
715         if ( autolevel ) {
716                 Sys_Printf( "\n--- MiniMapAutoLevel (%d) ---\n", minimap.height );
717                 float mi = 1, ma = 0;
718                 float s, o;
719
720                 // TODO threads!
721                 q = minimap.data1f;
722                 for ( y = 0; y < minimap.height; ++y )
723                         for ( x = 0; x < minimap.width; ++x )
724                         {
725                                 float v = *q++;
726                                 if ( v < mi ) {
727                                         mi = v;
728                                 }
729                                 if ( v > ma ) {
730                                         ma = v;
731                                 }
732                         }
733                 if ( ma > mi ) {
734                         s = 1 / ( ma - mi );
735                         o = mi / ( ma - mi );
736
737                         // equations:
738                         //   brightness + contrast * v
739                         // after autolevel:
740                         //   brightness + contrast * (v * s - o)
741                         // =
742                         //   (brightness - contrast * o) + (contrast * s) * v
743                         minimap.brightness = minimap.brightness - minimap.contrast * o;
744                         minimap.contrast *= s;
745
746                         Sys_Printf( "Auto level: Brightness changed to %f\n", minimap.brightness );
747                         Sys_Printf( "Auto level: Contrast changed to %f\n", minimap.contrast );
748                 }
749                 else{
750                         Sys_Printf( "Auto level: failed because all pixels are the same value\n" );
751                 }
752         }
753
754         if ( minimap.brightness != 0 || minimap.contrast != 1 ) {
755                 Sys_Printf( "\n--- MiniMapBrightnessContrast (%d) ---\n", minimap.height );
756                 RunThreadsOnIndividual( minimap.height, qtrue, MiniMapBrightnessContrast );
757         }
758
759         if ( minimap.sharpendata1f ) {
760                 Sys_Printf( "\n--- MiniMapSharpen (%d) ---\n", minimap.height );
761                 RunThreadsOnIndividual( minimap.height, qtrue, MiniMapSharpen );
762                 q = minimap.sharpendata1f;
763         }
764         else
765         {
766                 q = minimap.data1f;
767         }
768
769         Sys_Printf( "\nConverting..." );
770
771         switch ( mode )
772         {
773         case MINIMAP_MODE_GRAY:
774                 p = data4b;
775                 for ( y = 0; y < minimap.height; ++y )
776                         for ( x = 0; x < minimap.width; ++x )
777                         {
778                                 byte b;
779                                 float v = *q++;
780                                 if ( v < 0 ) {
781                                         v = 0;
782                                 }
783                                 if ( v > 255.0 / 256.0 ) {
784                                         v = 255.0 / 256.0;
785                                 }
786                                 b = v * 256;
787                                 *p++ = b;
788                         }
789                 Sys_Printf( " writing to %s...", minimapFilename );
790                 WriteTGAGray( minimapFilename, data4b, minimap.width, minimap.height );
791                 break;
792         case MINIMAP_MODE_BLACK:
793                 p = data4b;
794                 for ( y = 0; y < minimap.height; ++y )
795                         for ( x = 0; x < minimap.width; ++x )
796                         {
797                                 byte b;
798                                 float v = *q++;
799                                 if ( v < 0 ) {
800                                         v = 0;
801                                 }
802                                 if ( v > 255.0 / 256.0 ) {
803                                         v = 255.0 / 256.0;
804                                 }
805                                 b = v * 256;
806                                 *p++ = 0;
807                                 *p++ = 0;
808                                 *p++ = 0;
809                                 *p++ = b;
810                         }
811                 Sys_Printf( " writing to %s...", minimapFilename );
812                 WriteTGA( minimapFilename, data4b, minimap.width, minimap.height );
813                 break;
814         case MINIMAP_MODE_WHITE:
815                 p = data4b;
816                 for ( y = 0; y < minimap.height; ++y )
817                         for ( x = 0; x < minimap.width; ++x )
818                         {
819                                 byte b;
820                                 float v = *q++;
821                                 if ( v < 0 ) {
822                                         v = 0;
823                                 }
824                                 if ( v > 255.0 / 256.0 ) {
825                                         v = 255.0 / 256.0;
826                                 }
827                                 b = v * 256;
828                                 *p++ = 255;
829                                 *p++ = 255;
830                                 *p++ = 255;
831                                 *p++ = b;
832                         }
833                 Sys_Printf( " writing to %s...", minimapFilename );
834                 WriteTGA( minimapFilename, data4b, minimap.width, minimap.height );
835                 break;
836         }
837
838         Sys_Printf( " done.\n" );
839
840         /* return to sender */
841         return 0;
842 }
843
844
845
846
847
848 /*
849    MD4BlockChecksum()
850    calculates an md4 checksum for a block of data
851  */
852
853 static int MD4BlockChecksum( void *buffer, int length ){
854         return Com_BlockChecksum( buffer, length );
855 }
856
857 /*
858    FixAAS()
859    resets an aas checksum to match the given BSP
860  */
861
862 int FixAAS( int argc, char **argv ){
863         int length, checksum;
864         void        *buffer;
865         FILE        *file;
866         char aas[ 1024 ], **ext;
867         char        *exts[] =
868         {
869                 ".aas",
870                 "_b0.aas",
871                 "_b1.aas",
872                 NULL
873         };
874
875
876         /* arg checking */
877         if ( argc < 2 ) {
878                 Sys_Printf( "Usage: q3map -fixaas [-v] <mapname>\n" );
879                 return 0;
880         }
881
882         /* do some path mangling */
883         strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
884         StripExtension( source );
885         DefaultExtension( source, ".bsp" );
886
887         /* note it */
888         Sys_Printf( "--- FixAAS ---\n" );
889
890         /* load the bsp */
891         Sys_Printf( "Loading %s\n", source );
892         length = LoadFile( source, &buffer );
893
894         /* create bsp checksum */
895         Sys_Printf( "Creating checksum...\n" );
896         checksum = LittleLong( MD4BlockChecksum( buffer, length ) );
897
898         /* write checksum to aas */
899         ext = exts;
900         while ( *ext )
901         {
902                 /* mangle name */
903                 strcpy( aas, source );
904                 StripExtension( aas );
905                 strcat( aas, *ext );
906                 Sys_Printf( "Trying %s\n", aas );
907                 ext++;
908
909                 /* fix it */
910                 file = fopen( aas, "r+b" );
911                 if ( !file ) {
912                         continue;
913                 }
914                 if ( fwrite( &checksum, 4, 1, file ) != 1 ) {
915                         Error( "Error writing checksum to %s", aas );
916                 }
917                 fclose( file );
918         }
919
920         /* return to sender */
921         return 0;
922 }
923
924
925
926 /*
927    AnalyzeBSP() - ydnar
928    analyzes a Quake engine BSP file
929  */
930
931 typedef struct abspHeader_s
932 {
933         char ident[ 4 ];
934         int version;
935
936         bspLump_t lumps[ 1 ];       /* unknown size */
937 }
938 abspHeader_t;
939
940 typedef struct abspLumpTest_s
941 {
942         int radix, minCount;
943         char            *name;
944 }
945 abspLumpTest_t;
946
947 int AnalyzeBSP( int argc, char **argv ){
948         abspHeader_t            *header;
949         int size, i, version, offset, length, lumpInt, count;
950         char ident[ 5 ];
951         void                    *lump;
952         float lumpFloat;
953         char lumpString[ 1024 ], source[ 1024 ];
954         qboolean lumpSwap = qfalse;
955         abspLumpTest_t          *lumpTest;
956         static abspLumpTest_t lumpTests[] =
957         {
958                 { sizeof( bspPlane_t ),         6,      "IBSP LUMP_PLANES" },
959                 { sizeof( bspBrush_t ),         1,      "IBSP LUMP_BRUSHES" },
960                 { 8,                            6,      "IBSP LUMP_BRUSHSIDES" },
961                 { sizeof( bspBrushSide_t ),     6,      "RBSP LUMP_BRUSHSIDES" },
962                 { sizeof( bspModel_t ),         1,      "IBSP LUMP_MODELS" },
963                 { sizeof( bspNode_t ),          2,      "IBSP LUMP_NODES" },
964                 { sizeof( bspLeaf_t ),          1,      "IBSP LUMP_LEAFS" },
965                 { 104,                          3,      "IBSP LUMP_DRAWSURFS" },
966                 { 44,                           3,      "IBSP LUMP_DRAWVERTS" },
967                 { 4,                            6,      "IBSP LUMP_DRAWINDEXES" },
968                 { 128 * 128 * 3,                1,      "IBSP LUMP_LIGHTMAPS" },
969                 { 256 * 256 * 3,                1,      "IBSP LUMP_LIGHTMAPS (256 x 256)" },
970                 { 512 * 512 * 3,                1,      "IBSP LUMP_LIGHTMAPS (512 x 512)" },
971                 { 0, 0, NULL }
972         };
973
974
975         /* arg checking */
976         if ( argc < 1 ) {
977                 Sys_Printf( "Usage: q3map -analyze [-lumpswap] [-v] <mapname>\n" );
978                 return 0;
979         }
980
981         /* process arguments */
982         for ( i = 1; i < ( argc - 1 ); i++ )
983         {
984                 /* -format map|ase|... */
985                 if ( !strcmp( argv[ i ],  "-lumpswap" ) ) {
986                         Sys_Printf( "Swapped lump structs enabled\n" );
987                         lumpSwap = qtrue;
988                 }
989         }
990
991         /* clean up map name */
992         strcpy( source, ExpandArg( argv[ i ] ) );
993         Sys_Printf( "Loading %s\n", source );
994
995         /* load the file */
996         size = LoadFile( source, (void**) &header );
997         if ( size == 0 || header == NULL ) {
998                 Sys_Printf( "Unable to load %s.\n", source );
999                 return -1;
1000         }
1001
1002         /* analyze ident/version */
1003         memcpy( ident, header->ident, 4 );
1004         ident[ 4 ] = '\0';
1005         version = LittleLong( header->version );
1006
1007         Sys_Printf( "Identity:      %s\n", ident );
1008         Sys_Printf( "Version:       %d\n", version );
1009         Sys_Printf( "---------------------------------------\n" );
1010
1011         /* analyze each lump */
1012         for ( i = 0; i < 100; i++ )
1013         {
1014                 /* call of duty swapped lump pairs */
1015                 if ( lumpSwap ) {
1016                         offset = LittleLong( header->lumps[ i ].length );
1017                         length = LittleLong( header->lumps[ i ].offset );
1018                 }
1019
1020                 /* standard lump pairs */
1021                 else
1022                 {
1023                         offset = LittleLong( header->lumps[ i ].offset );
1024                         length = LittleLong( header->lumps[ i ].length );
1025                 }
1026
1027                 /* extract data */
1028                 lump = (byte*) header + offset;
1029                 lumpInt = LittleLong( (int) *( (int*) lump ) );
1030                 lumpFloat = LittleFloat( (float) *( (float*) lump ) );
1031                 memcpy( lumpString, (char*) lump, ( (size_t)length < sizeof( lumpString ) ? (size_t)length : sizeof( lumpString ) - 1 ) );
1032                 lumpString[ sizeof( lumpString ) - 1 ] = '\0';
1033
1034                 /* print basic lump info */
1035                 Sys_Printf( "Lump:          %d\n", i );
1036                 Sys_Printf( "Offset:        %d bytes\n", offset );
1037                 Sys_Printf( "Length:        %d bytes\n", length );
1038
1039                 /* only operate on valid lumps */
1040                 if ( length > 0 ) {
1041                         /* print data in 4 formats */
1042                         Sys_Printf( "As hex:        %08X\n", lumpInt );
1043                         Sys_Printf( "As int:        %d\n", lumpInt );
1044                         Sys_Printf( "As float:      %f\n", lumpFloat );
1045                         Sys_Printf( "As string:     %s\n", lumpString );
1046
1047                         /* guess lump type */
1048                         if ( lumpString[ 0 ] == '{' && lumpString[ 2 ] == '"' ) {
1049                                 Sys_Printf( "Type guess:    IBSP LUMP_ENTITIES\n" );
1050                         }
1051                         else if ( strstr( lumpString, "textures/" ) ) {
1052                                 Sys_Printf( "Type guess:    IBSP LUMP_SHADERS\n" );
1053                         }
1054                         else
1055                         {
1056                                 /* guess based on size/count */
1057                                 for ( lumpTest = lumpTests; lumpTest->radix > 0; lumpTest++ )
1058                                 {
1059                                         if ( ( length % lumpTest->radix ) != 0 ) {
1060                                                 continue;
1061                                         }
1062                                         count = length / lumpTest->radix;
1063                                         if ( count < lumpTest->minCount ) {
1064                                                 continue;
1065                                         }
1066                                         Sys_Printf( "Type guess:    %s (%d x %d)\n", lumpTest->name, count, lumpTest->radix );
1067                                 }
1068                         }
1069                 }
1070
1071                 Sys_Printf( "---------------------------------------\n" );
1072
1073                 /* end of file */
1074                 if ( offset + length >= size ) {
1075                         break;
1076                 }
1077         }
1078
1079         /* last stats */
1080         Sys_Printf( "Lump count:    %d\n", i + 1 );
1081         Sys_Printf( "File size:     %d bytes\n", size );
1082
1083         /* return to caller */
1084         return 0;
1085 }
1086
1087
1088
1089 /*
1090    BSPInfo()
1091    emits statistics about the bsp file
1092  */
1093
1094 int BSPInfo( int count, char **fileNames ){
1095         int i;
1096         char source[ 1024 ], ext[ 64 ];
1097         int size;
1098         FILE        *f;
1099
1100
1101         /* dummy check */
1102         if ( count < 1 ) {
1103                 Sys_Printf( "No files to dump info for.\n" );
1104                 return -1;
1105         }
1106
1107         /* enable info mode */
1108         infoMode = qtrue;
1109
1110         /* walk file list */
1111         for ( i = 0; i < count; i++ )
1112         {
1113                 Sys_Printf( "---------------------------------\n" );
1114
1115                 /* mangle filename and get size */
1116                 strcpy( source, fileNames[ i ] );
1117                 ExtractFileExtension( source, ext );
1118                 if ( !Q_stricmp( ext, "map" ) ) {
1119                         StripExtension( source );
1120                 }
1121                 DefaultExtension( source, ".bsp" );
1122                 f = fopen( source, "rb" );
1123                 if ( f ) {
1124                         size = Q_filelength( f );
1125                         fclose( f );
1126                 }
1127                 else{
1128                         size = 0;
1129                 }
1130
1131                 /* load the bsp file and print lump sizes */
1132                 Sys_Printf( "%s\n", source );
1133                 LoadBSPFile( source );
1134                 PrintBSPFileSizes();
1135
1136                 /* print sizes */
1137                 Sys_Printf( "\n" );
1138                 Sys_Printf( "          total         %9d\n", size );
1139                 Sys_Printf( "                        %9d KB\n", size / 1024 );
1140                 Sys_Printf( "                        %9d MB\n", size / ( 1024 * 1024 ) );
1141
1142                 Sys_Printf( "---------------------------------\n" );
1143         }
1144
1145         /* return count */
1146         return i;
1147 }
1148
1149
1150 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 ){
1151         vec4_t scoeffs, tcoeffs;
1152         float md;
1153         m4x4_t solvematrix;
1154
1155         vec3_t norm;
1156         vec3_t dab, dac;
1157         VectorSubtract( bxyz, axyz, dab );
1158         VectorSubtract( cxyz, axyz, dac );
1159         CrossProduct( dab, dac, norm );
1160
1161         // assume:
1162         //   s = f(x, y, z)
1163         //   s(v + norm) = s(v) when n ortho xyz
1164
1165         // s(v) = DotProduct(v, scoeffs) + scoeffs[3]
1166
1167         // solve:
1168         //   scoeffs * (axyz, 1) == ast[0]
1169         //   scoeffs * (bxyz, 1) == bst[0]
1170         //   scoeffs * (cxyz, 1) == cst[0]
1171         //   scoeffs * (norm, 0) == 0
1172         // scoeffs * [axyz, 1 | bxyz, 1 | cxyz, 1 | norm, 0] = [ast[0], bst[0], cst[0], 0]
1173         solvematrix[0] = axyz[0];
1174         solvematrix[4] = axyz[1];
1175         solvematrix[8] = axyz[2];
1176         solvematrix[12] = 1;
1177         solvematrix[1] = bxyz[0];
1178         solvematrix[5] = bxyz[1];
1179         solvematrix[9] = bxyz[2];
1180         solvematrix[13] = 1;
1181         solvematrix[2] = cxyz[0];
1182         solvematrix[6] = cxyz[1];
1183         solvematrix[10] = cxyz[2];
1184         solvematrix[14] = 1;
1185         solvematrix[3] = norm[0];
1186         solvematrix[7] = norm[1];
1187         solvematrix[11] = norm[2];
1188         solvematrix[15] = 0;
1189
1190         md = m4_det( solvematrix );
1191         if ( md * md < 1e-10 ) {
1192                 Sys_Printf( "Cannot invert some matrix, some texcoords aren't extrapolated!" );
1193                 return;
1194         }
1195
1196         m4x4_invert( solvematrix );
1197
1198         scoeffs[0] = ast[0];
1199         scoeffs[1] = bst[0];
1200         scoeffs[2] = cst[0];
1201         scoeffs[3] = 0;
1202         m4x4_transform_vec4( solvematrix, scoeffs );
1203         tcoeffs[0] = ast[1];
1204         tcoeffs[1] = bst[1];
1205         tcoeffs[2] = cst[1];
1206         tcoeffs[3] = 0;
1207         m4x4_transform_vec4( solvematrix, tcoeffs );
1208
1209         ast_out[0] = scoeffs[0] * axyz_new[0] + scoeffs[1] * axyz_new[1] + scoeffs[2] * axyz_new[2] + scoeffs[3];
1210         ast_out[1] = tcoeffs[0] * axyz_new[0] + tcoeffs[1] * axyz_new[1] + tcoeffs[2] * axyz_new[2] + tcoeffs[3];
1211         bst_out[0] = scoeffs[0] * bxyz_new[0] + scoeffs[1] * bxyz_new[1] + scoeffs[2] * bxyz_new[2] + scoeffs[3];
1212         bst_out[1] = tcoeffs[0] * bxyz_new[0] + tcoeffs[1] * bxyz_new[1] + tcoeffs[2] * bxyz_new[2] + tcoeffs[3];
1213         cst_out[0] = scoeffs[0] * cxyz_new[0] + scoeffs[1] * cxyz_new[1] + scoeffs[2] * cxyz_new[2] + scoeffs[3];
1214         cst_out[1] = tcoeffs[0] * cxyz_new[0] + tcoeffs[1] * cxyz_new[1] + tcoeffs[2] * cxyz_new[2] + tcoeffs[3];
1215 }
1216
1217 /*
1218    ScaleBSPMain()
1219    amaze and confuse your enemies with wierd scaled maps!
1220  */
1221
1222 int ScaleBSPMain( int argc, char **argv ){
1223         int i, j;
1224         float f, a;
1225         vec3_t scale;
1226         vec3_t vec;
1227         char str[ 1024 ];
1228         int uniform, axis;
1229         qboolean texscale;
1230         float *old_xyzst = NULL;
1231         float spawn_ref = 0;
1232
1233
1234         /* arg checking */
1235         if ( argc < 3 ) {
1236                 Sys_Printf( "Usage: q3map [-v] -scale [-tex] [-spawn_ref <value>] <value> <mapname>\n" );
1237                 return 0;
1238         }
1239
1240         texscale = qfalse;
1241         for ( i = 1; i < argc - 2; ++i )
1242         {
1243                 if ( !strcmp( argv[i], "-tex" ) ) {
1244                         texscale = qtrue;
1245                 }
1246                 else if ( !strcmp( argv[i], "-spawn_ref" ) ) {
1247                         spawn_ref = atof( argv[i + 1] );
1248                         ++i;
1249                 }
1250                 else{
1251                         break;
1252                 }
1253         }
1254
1255         /* get scale */
1256         // if(argc-2 >= i) // always true
1257         scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
1258         if ( argc - 3 >= i ) {
1259                 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
1260         }
1261         if ( argc - 4 >= i ) {
1262                 scale[0] = atof( argv[ argc - 4 ] );
1263         }
1264
1265         uniform = ( ( scale[0] == scale[1] ) && ( scale[1] == scale[2] ) );
1266
1267         if ( scale[0] == 0.0f || scale[1] == 0.0f || scale[2] == 0.0f ) {
1268                 Sys_Printf( "Usage: q3map [-v] -scale [-tex] [-spawn_ref <value>] <value> <mapname>\n" );
1269                 Sys_Printf( "Non-zero scale value required.\n" );
1270                 return 0;
1271         }
1272
1273         /* do some path mangling */
1274         strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
1275         StripExtension( source );
1276         DefaultExtension( source, ".bsp" );
1277
1278         /* load the bsp */
1279         Sys_Printf( "Loading %s\n", source );
1280         LoadBSPFile( source );
1281         ParseEntities();
1282
1283         /* note it */
1284         Sys_Printf( "--- ScaleBSP ---\n" );
1285         Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
1286
1287         /* scale entity keys */
1288         for ( i = 0; i < numBSPEntities && i < numEntities; i++ )
1289         {
1290                 /* scale origin */
1291                 GetVectorForKey( &entities[ i ], "origin", vec );
1292                 if ( ( vec[ 0 ] || vec[ 1 ] || vec[ 2 ] ) ) {
1293                         if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
1294                                 vec[2] += spawn_ref;
1295                         }
1296                         vec[0] *= scale[0];
1297                         vec[1] *= scale[1];
1298                         vec[2] *= scale[2];
1299                         if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
1300                                 vec[2] -= spawn_ref;
1301                         }
1302                         sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1303                         SetKeyValue( &entities[ i ], "origin", str );
1304                 }
1305
1306                 a = FloatForKey( &entities[ i ], "angle" );
1307                 if ( a == -1 || a == -2 ) { // z scale
1308                         axis = 2;
1309                 }
1310                 else if ( fabs( sin( DEG2RAD( a ) ) ) < 0.707 ) {
1311                         axis = 0;
1312                 }
1313                 else{
1314                         axis = 1;
1315                 }
1316
1317                 /* scale door lip */
1318                 f = FloatForKey( &entities[ i ], "lip" );
1319                 if ( f ) {
1320                         f *= scale[axis];
1321                         sprintf( str, "%f", f );
1322                         SetKeyValue( &entities[ i ], "lip", str );
1323                 }
1324
1325                 /* scale plat height */
1326                 f = FloatForKey( &entities[ i ], "height" );
1327                 if ( f ) {
1328                         f *= scale[2];
1329                         sprintf( str, "%f", f );
1330                         SetKeyValue( &entities[ i ], "height", str );
1331                 }
1332
1333                 // TODO maybe allow a definition file for entities to specify which values are scaled how?
1334         }
1335
1336         /* scale models */
1337         for ( i = 0; i < numBSPModels; i++ )
1338         {
1339                 bspModels[ i ].mins[0] *= scale[0];
1340                 bspModels[ i ].mins[1] *= scale[1];
1341                 bspModels[ i ].mins[2] *= scale[2];
1342                 bspModels[ i ].maxs[0] *= scale[0];
1343                 bspModels[ i ].maxs[1] *= scale[1];
1344                 bspModels[ i ].maxs[2] *= scale[2];
1345         }
1346
1347         /* scale nodes */
1348         for ( i = 0; i < numBSPNodes; i++ )
1349         {
1350                 bspNodes[ i ].mins[0] *= scale[0];
1351                 bspNodes[ i ].mins[1] *= scale[1];
1352                 bspNodes[ i ].mins[2] *= scale[2];
1353                 bspNodes[ i ].maxs[0] *= scale[0];
1354                 bspNodes[ i ].maxs[1] *= scale[1];
1355                 bspNodes[ i ].maxs[2] *= scale[2];
1356         }
1357
1358         /* scale leafs */
1359         for ( i = 0; i < numBSPLeafs; i++ )
1360         {
1361                 bspLeafs[ i ].mins[0] *= scale[0];
1362                 bspLeafs[ i ].mins[1] *= scale[1];
1363                 bspLeafs[ i ].mins[2] *= scale[2];
1364                 bspLeafs[ i ].maxs[0] *= scale[0];
1365                 bspLeafs[ i ].maxs[1] *= scale[1];
1366                 bspLeafs[ i ].maxs[2] *= scale[2];
1367         }
1368
1369         if ( texscale ) {
1370                 Sys_Printf( "Using texture unlocking (and probably breaking texture alignment a lot)\n" );
1371                 old_xyzst = safe_malloc( sizeof( *old_xyzst ) * numBSPDrawVerts * 5 );
1372                 for ( i = 0; i < numBSPDrawVerts; i++ )
1373                 {
1374                         old_xyzst[5 * i + 0] = bspDrawVerts[i].xyz[0];
1375                         old_xyzst[5 * i + 1] = bspDrawVerts[i].xyz[1];
1376                         old_xyzst[5 * i + 2] = bspDrawVerts[i].xyz[2];
1377                         old_xyzst[5 * i + 3] = bspDrawVerts[i].st[0];
1378                         old_xyzst[5 * i + 4] = bspDrawVerts[i].st[1];
1379                 }
1380         }
1381
1382         /* scale drawverts */
1383         for ( i = 0; i < numBSPDrawVerts; i++ )
1384         {
1385                 bspDrawVerts[i].xyz[0] *= scale[0];
1386                 bspDrawVerts[i].xyz[1] *= scale[1];
1387                 bspDrawVerts[i].xyz[2] *= scale[2];
1388                 bspDrawVerts[i].normal[0] /= scale[0];
1389                 bspDrawVerts[i].normal[1] /= scale[1];
1390                 bspDrawVerts[i].normal[2] /= scale[2];
1391                 VectorNormalize( bspDrawVerts[i].normal, bspDrawVerts[i].normal );
1392         }
1393
1394         if ( texscale ) {
1395                 for ( i = 0; i < numBSPDrawSurfaces; i++ )
1396                 {
1397                         switch ( bspDrawSurfaces[i].surfaceType )
1398                         {
1399                         case SURFACE_FACE:
1400                         case SURFACE_META:
1401                                 if ( bspDrawSurfaces[i].numIndexes % 3 ) {
1402                                         Error( "Not a triangulation!" );
1403                                 }
1404                                 for ( j = bspDrawSurfaces[i].firstIndex; j < bspDrawSurfaces[i].firstIndex + bspDrawSurfaces[i].numIndexes; j += 3 )
1405                                 {
1406                                         int ia = bspDrawIndexes[j] + bspDrawSurfaces[i].firstVert, ib = bspDrawIndexes[j + 1] + bspDrawSurfaces[i].firstVert, ic = bspDrawIndexes[j + 2] + bspDrawSurfaces[i].firstVert;
1407                                         bspDrawVert_t *a = &bspDrawVerts[ia], *b = &bspDrawVerts[ib], *c = &bspDrawVerts[ic];
1408                                         float *oa = &old_xyzst[ia * 5], *ob = &old_xyzst[ib * 5], *oc = &old_xyzst[ic * 5];
1409                                         // extrapolate:
1410                                         //   a->xyz -> oa
1411                                         //   b->xyz -> ob
1412                                         //   c->xyz -> oc
1413                                         ExtrapolateTexcoords(
1414                                                 &oa[0], &oa[3],
1415                                                 &ob[0], &ob[3],
1416                                                 &oc[0], &oc[3],
1417                                                 a->xyz, a->st,
1418                                                 b->xyz, b->st,
1419                                                 c->xyz, c->st );
1420                                 }
1421                                 break;
1422                         }
1423                 }
1424         }
1425
1426         /* scale planes */
1427         if ( uniform ) {
1428                 for ( i = 0; i < numBSPPlanes; i++ )
1429                 {
1430                         bspPlanes[ i ].dist *= scale[0];
1431                 }
1432         }
1433         else
1434         {
1435                 for ( i = 0; i < numBSPPlanes; i++ )
1436                 {
1437                         bspPlanes[ i ].normal[0] /= scale[0];
1438                         bspPlanes[ i ].normal[1] /= scale[1];
1439                         bspPlanes[ i ].normal[2] /= scale[2];
1440                         f = 1 / VectorLength( bspPlanes[i].normal );
1441                         VectorScale( bspPlanes[i].normal, f, bspPlanes[i].normal );
1442                         bspPlanes[ i ].dist *= f;
1443                 }
1444         }
1445
1446         /* scale gridsize */
1447         GetVectorForKey( &entities[ 0 ], "gridsize", vec );
1448         if ( ( vec[ 0 ] + vec[ 1 ] + vec[ 2 ] ) == 0.0f ) {
1449                 VectorCopy( gridSize, vec );
1450         }
1451         vec[0] *= scale[0];
1452         vec[1] *= scale[1];
1453         vec[2] *= scale[2];
1454         sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1455         SetKeyValue( &entities[ 0 ], "gridsize", str );
1456
1457         /* inject command line parameters */
1458         InjectCommandLine( argv, 0, argc - 1 );
1459
1460         /* write the bsp */
1461         UnparseEntities();
1462         StripExtension( source );
1463         DefaultExtension( source, "_s.bsp" );
1464         Sys_Printf( "Writing %s\n", source );
1465         WriteBSPFile( source );
1466
1467         /* return to sender */
1468         return 0;
1469 }
1470
1471
1472 /*
1473    ShiftBSPMain()
1474    shifts a map: for testing physics with huge coordinates
1475  */
1476
1477 int ShiftBSPMain( int argc, char **argv ){
1478         int i, j;
1479         vec3_t scale;
1480         vec3_t vec;
1481         char str[ 1024 ];
1482         float spawn_ref = 0;
1483
1484
1485         /* arg checking */
1486         if ( argc < 3 ) {
1487                 Sys_Printf( "Usage: q3map [-v] -shift [-tex] [-spawn_ref <value>] <value> <mapname>\n" );
1488                 return 0;
1489         }
1490
1491         for ( i = 1; i < argc - 2; ++i )
1492         {
1493                 if ( !strcmp( argv[i], "-tex" ) ) {
1494                 }
1495                 else if ( !strcmp( argv[i], "-spawn_ref" ) ) {
1496                         spawn_ref = atof( argv[i + 1] );
1497                         ++i;
1498                 }
1499                 else{
1500                         break;
1501                 }
1502         }
1503
1504         /* get shift */
1505         // if(argc-2 >= i) // always true
1506         scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
1507         if ( argc - 3 >= i ) {
1508                 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
1509         }
1510         if ( argc - 4 >= i ) {
1511                 scale[0] = atof( argv[ argc - 4 ] );
1512         }
1513
1514
1515         /* do some path mangling */
1516         strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
1517         StripExtension( source );
1518         DefaultExtension( source, ".bsp" );
1519
1520         /* load the bsp */
1521         Sys_Printf( "Loading %s\n", source );
1522         LoadBSPFile( source );
1523         ParseEntities();
1524
1525         /* note it */
1526         Sys_Printf( "--- ShiftBSP ---\n" );
1527         Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
1528
1529         /* shift entity keys */
1530         for ( i = 0; i < numBSPEntities && i < numEntities; i++ )
1531         {
1532                 /* shift origin */
1533                 GetVectorForKey( &entities[ i ], "origin", vec );
1534                 if ( ( vec[ 0 ] || vec[ 1 ] || vec[ 2 ] ) ) {
1535                         if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
1536                                 vec[2] += spawn_ref;
1537                         }
1538                         vec[0] += scale[0];
1539                         vec[1] += scale[1];
1540                         vec[2] += scale[2];
1541                         if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
1542                                 vec[2] -= spawn_ref;
1543                         }
1544                         sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1545                         SetKeyValue( &entities[ i ], "origin", str );
1546                 }
1547
1548         }
1549
1550         /* shift models */
1551         for ( i = 0; i < numBSPModels; i++ )
1552         {
1553                 bspModels[ i ].mins[0] += scale[0];
1554                 bspModels[ i ].mins[1] += scale[1];
1555                 bspModels[ i ].mins[2] += scale[2];
1556                 bspModels[ i ].maxs[0] += scale[0];
1557                 bspModels[ i ].maxs[1] += scale[1];
1558                 bspModels[ i ].maxs[2] += scale[2];
1559         }
1560
1561         /* shift nodes */
1562         for ( i = 0; i < numBSPNodes; i++ )
1563         {
1564                 bspNodes[ i ].mins[0] += scale[0];
1565                 bspNodes[ i ].mins[1] += scale[1];
1566                 bspNodes[ i ].mins[2] += scale[2];
1567                 bspNodes[ i ].maxs[0] += scale[0];
1568                 bspNodes[ i ].maxs[1] += scale[1];
1569                 bspNodes[ i ].maxs[2] += scale[2];
1570         }
1571
1572         /* shift leafs */
1573         for ( i = 0; i < numBSPLeafs; i++ )
1574         {
1575                 bspLeafs[ i ].mins[0] += scale[0];
1576                 bspLeafs[ i ].mins[1] += scale[1];
1577                 bspLeafs[ i ].mins[2] += scale[2];
1578                 bspLeafs[ i ].maxs[0] += scale[0];
1579                 bspLeafs[ i ].maxs[1] += scale[1];
1580                 bspLeafs[ i ].maxs[2] += scale[2];
1581         }
1582
1583         /* shift drawverts */
1584         for ( i = 0; i < numBSPDrawVerts; i++ )
1585         {
1586                 bspDrawVerts[i].xyz[0] += scale[0];
1587                 bspDrawVerts[i].xyz[1] += scale[1];
1588                 bspDrawVerts[i].xyz[2] += scale[2];
1589         }
1590
1591         /* shift planes */
1592
1593         vec3_t point;
1594
1595         for ( i = 0; i < numBSPPlanes; i++ )
1596         {
1597                 //find point on plane
1598                 for ( j=0; j<3; j++ ){
1599                         if ( fabs( bspPlanes[ i ].normal[j] ) > 0.5 ){
1600                                 point[j] = bspPlanes[ i ].dist / bspPlanes[ i ].normal[j];
1601                                 point[(j+1)%3] = point[(j+2)%3] = 0;
1602                                 break;
1603                         }
1604                 }
1605                 //shift point
1606                 for ( j=0; j<3; j++ ){
1607                         point[j] += scale[j];
1608                 }
1609                 //calc new plane dist
1610                 bspPlanes[ i ].dist = DotProduct( point, bspPlanes[ i ].normal );
1611         }
1612
1613         /* scale gridsize */
1614         /*
1615         GetVectorForKey( &entities[ 0 ], "gridsize", vec );
1616         if ( ( vec[ 0 ] + vec[ 1 ] + vec[ 2 ] ) == 0.0f ) {
1617                 VectorCopy( gridSize, vec );
1618         }
1619         vec[0] *= scale[0];
1620         vec[1] *= scale[1];
1621         vec[2] *= scale[2];
1622         sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1623         SetKeyValue( &entities[ 0 ], "gridsize", str );
1624 */
1625         /* inject command line parameters */
1626         InjectCommandLine( argv, 0, argc - 1 );
1627
1628         /* write the bsp */
1629         UnparseEntities();
1630         StripExtension( source );
1631         DefaultExtension( source, "_sh.bsp" );
1632         Sys_Printf( "Writing %s\n", source );
1633         WriteBSPFile( source );
1634
1635         /* return to sender */
1636         return 0;
1637 }
1638
1639
1640 void FixDOSName( char *src ){
1641         if ( src == NULL ) {
1642                 return;
1643         }
1644
1645         while ( *src )
1646         {
1647                 if ( *src == '\\' ) {
1648                         *src = '/';
1649                 }
1650                 src++;
1651         }
1652 }
1653
1654 /*
1655         Check if newcoming texture is unique and not excluded
1656 */
1657 void tex2list( char* texlist, int *texnum, char* EXtex, int *EXtexnum ){
1658         int i;
1659         if ( token[0] == '\0') return;
1660         StripExtension( token );
1661         FixDOSName( token );
1662         for ( i = 0; i < *texnum; i++ ){
1663                 if ( !Q_stricmp( texlist + i*65, token ) ) return;
1664         }
1665         for ( i = 0; i < *EXtexnum; i++ ){
1666                 if ( !Q_stricmp( EXtex + i*65, token ) ) return;
1667         }
1668         strcpy ( texlist + (*texnum)*65, token );
1669         (*texnum)++;
1670         return;
1671 }
1672
1673 /* 4 repack */
1674 void tex2list2( char* texlist, int *texnum, char* EXtex, int *EXtexnum, char* rEXtex, int *rEXtexnum ){
1675         int i;
1676         if ( token[0] == '\0') return;
1677         //StripExtension( token );
1678         char* dot = strrchr( token, '.' );
1679         if ( dot != NULL){
1680                 if ( Q_stricmp( dot, ".tga" ) && Q_stricmp( dot, ".jpg" ) && Q_stricmp( dot, ".png" ) ){
1681                         Sys_Printf( "WARNING4: %s : weird or missing extension in shader\n", token );
1682                 }
1683                 else{
1684                         *dot = '\0';
1685                 }
1686         }
1687         FixDOSName( token );
1688         strcpy ( texlist + (*texnum)*65, token );
1689         strcat( token, ".tga" );
1690
1691         /* exclude */
1692         for ( i = 0; i < *texnum; i++ ){
1693                 if ( !Q_stricmp( texlist + i*65, texlist + (*texnum)*65 ) ) return;
1694         }
1695         for ( i = 0; i < *EXtexnum; i++ ){
1696                 if ( !Q_stricmp( EXtex + i*65, texlist + (*texnum)*65 ) ) return;
1697         }
1698         for ( i = 0; i < *rEXtexnum; i++ ){
1699                 if ( !Q_stricmp( rEXtex + i*65, texlist + (*texnum)*65 ) ) return;
1700         }
1701         (*texnum)++;
1702         return;
1703 }
1704
1705
1706 /*
1707         Check if newcoming res is unique
1708 */
1709 void res2list( char* data, int *num ){
1710         int i;
1711         if ( strlen( data + (*num)*65 ) > 64 ){
1712                 Sys_Printf( "WARNING6: %s : path too long.\n", data + (*num)*65 );
1713         }
1714         while ( *( data + (*num)*65 ) == '\\' || *( data + (*num)*65 ) == '/' ){
1715                 char* cut = data + (*num)*65 + 1;
1716                 strcpy( data + (*num)*65, cut );
1717         }
1718         if ( *( data + (*num)*65 ) == '\0') return;
1719         for ( i = 0; i < *num; i++ ){
1720                 if ( !Q_stricmp( data + i*65, data + (*num)*65 ) ) return;
1721         }
1722         (*num)++;
1723         return;
1724 }
1725
1726 void parseEXblock ( char* data, int *num, const char *exName ){
1727         if ( !GetToken( qtrue ) || strcmp( token, "{" ) ) {
1728                 Error( "ReadExclusionsFile: %s, line %d: { not found", exName, scriptline );
1729         }
1730         while ( 1 )
1731         {
1732                 if ( !GetToken( qtrue ) ) {
1733                         break;
1734                 }
1735                 if ( !strcmp( token, "}" ) ) {
1736                         break;
1737                 }
1738                 if ( token[0] == '{' ) {
1739                         Error( "ReadExclusionsFile: %s, line %d: brace, opening twice in a row.", exName, scriptline );
1740                 }
1741
1742                 /* add to list */
1743                 strcpy( data + (*num)*65, token );
1744                 (*num)++;
1745         }
1746         return;
1747 }
1748
1749 char q3map2path[1024];
1750 /*
1751    pk3BSPMain()
1752    map autopackager, works for Q3 type of shaders and ents
1753  */
1754
1755 int pk3BSPMain( int argc, char **argv ){
1756         int i, j, len;
1757         qboolean dbg = qfalse, png = qfalse, packFAIL = qfalse;
1758
1759         /* process arguments */
1760         for ( i = 1; i < ( argc - 1 ); i++ ){
1761                 if ( !strcmp( argv[ i ],  "-dbg" ) ) {
1762                         dbg = qtrue;
1763                 }
1764                 else if ( !strcmp( argv[ i ],  "-png" ) ) {
1765                         png = qtrue;
1766                 }
1767         }
1768
1769         /* do some path mangling */
1770         strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
1771         StripExtension( source );
1772         DefaultExtension( source, ".bsp" );
1773
1774         /* load the bsp */
1775         Sys_Printf( "Loading %s\n", source );
1776         LoadBSPFile( source );
1777         ParseEntities();
1778
1779
1780         char packname[ 1024 ], packFailName[ 1024 ], base[ 1024 ], nameOFmap[ 1024 ], temp[ 1024 ];
1781
1782         /* copy map name */
1783         strcpy( base, source );
1784         StripExtension( base );
1785
1786         /* extract map name */
1787         len = strlen( base ) - 1;
1788         while ( len > 0 && base[ len ] != '/' && base[ len ] != '\\' )
1789                 len--;
1790         strcpy( nameOFmap, &base[ len + 1 ] );
1791
1792
1793         qboolean drawsurfSHs[1024] = { qfalse };
1794
1795         for ( i = 0; i < numBSPDrawSurfaces; i++ ){
1796                 /* can't exclude nodraw patches here (they want shaders :0!) */
1797                 //if ( !( bspDrawSurfaces[i].surfaceType == 2 && bspDrawSurfaces[i].numIndexes == 0 ) ) drawsurfSHs[bspDrawSurfaces[i].shaderNum] = qtrue;
1798                 drawsurfSHs[ bspDrawSurfaces[i].shaderNum ] = qtrue;
1799                 //Sys_Printf( "%s\n", bspShaders[bspDrawSurfaces[i].shaderNum].shader );
1800         }
1801
1802         int pk3ShadersN = 0;
1803         char* pk3Shaders = (char *)calloc( 1024*65, sizeof( char ) );
1804         int pk3SoundsN = 0;
1805         char* pk3Sounds = (char *)calloc( 1024*65, sizeof( char ) );
1806         int pk3ShaderfilesN = 0;
1807         char* pk3Shaderfiles = (char *)calloc( 1024*65, sizeof( char ) );
1808         int pk3TexturesN = 0;
1809         char* pk3Textures = (char *)calloc( 1024*65, sizeof( char ) );
1810         int pk3VideosN = 0;
1811         char* pk3Videos = (char *)calloc( 1024*65, sizeof( char ) );
1812
1813
1814
1815         for ( i = 0; i < numBSPShaders; i++ ){
1816                 if ( drawsurfSHs[i] ){
1817                         strcpy( pk3Shaders + pk3ShadersN*65, bspShaders[i].shader );
1818                         res2list( pk3Shaders, &pk3ShadersN );
1819                         //pk3ShadersN++;
1820                         //Sys_Printf( "%s\n", bspShaders[i].shader );
1821                 }
1822         }
1823
1824         /* Ent keys */
1825         epair_t *ep;
1826         for ( ep = entities[0].epairs; ep != NULL; ep = ep->next )
1827         {
1828                 if ( !Q_strncasecmp( ep->key, "vertexremapshader", 17 ) ) {
1829                         sscanf( ep->value, "%*[^;] %*[;] %s", pk3Shaders + pk3ShadersN*65 );
1830                         res2list( pk3Shaders, &pk3ShadersN );
1831                 }
1832         }
1833         strcpy( pk3Sounds + pk3SoundsN*65, ValueForKey( &entities[0], "music" ) );
1834         if ( *( pk3Sounds + pk3SoundsN*65 ) != '\0' ){
1835                 FixDOSName( pk3Sounds + pk3SoundsN*65 );
1836                 DefaultExtension( pk3Sounds + pk3SoundsN*65, ".wav" );
1837                 res2list( pk3Sounds, &pk3SoundsN );
1838         }
1839
1840         for ( i = 0; i < numBSPEntities && i < numEntities; i++ )
1841         {
1842                 strcpy( pk3Sounds + pk3SoundsN*65, ValueForKey( &entities[i], "noise" ) );
1843                 if ( *( pk3Sounds + pk3SoundsN*65 ) != '\0' && *( pk3Sounds + pk3SoundsN*65 ) != '*' ){
1844                         FixDOSName( pk3Sounds + pk3SoundsN*65 );
1845                         DefaultExtension( pk3Sounds + pk3SoundsN*65, ".wav" );
1846                         res2list( pk3Sounds, &pk3SoundsN );
1847                 }
1848
1849                 if ( !Q_stricmp( ValueForKey( &entities[i], "classname" ), "func_plat" ) ){
1850                         strcpy( pk3Sounds + pk3SoundsN*65, "sound/movers/plats/pt1_strt.wav");
1851                         res2list( pk3Sounds, &pk3SoundsN );
1852                         strcpy( pk3Sounds + pk3SoundsN*65, "sound/movers/plats/pt1_end.wav");
1853                         res2list( pk3Sounds, &pk3SoundsN );
1854                 }
1855                 if ( !Q_stricmp( ValueForKey( &entities[i], "classname" ), "target_push" ) ){
1856                         if ( !(IntForKey( &entities[i], "spawnflags") & 1) ){
1857                                 strcpy( pk3Sounds + pk3SoundsN*65, "sound/misc/windfly.wav");
1858                                 res2list( pk3Sounds, &pk3SoundsN );
1859                         }
1860                 }
1861                 strcpy( pk3Shaders + pk3ShadersN*65, ValueForKey( &entities[i], "targetShaderNewName" ) );
1862                 res2list( pk3Shaders, &pk3ShadersN );
1863         }
1864
1865         //levelshot
1866         sprintf( pk3Shaders + pk3ShadersN*65, "levelshots/%s", nameOFmap );
1867         res2list( pk3Shaders, &pk3ShadersN );
1868
1869
1870         if( dbg ){
1871                 Sys_Printf( "\n\tDrawsurface+ent calls....%i\n", pk3ShadersN );
1872                 for ( i = 0; i < pk3ShadersN; i++ ){
1873                         Sys_Printf( "%s\n", pk3Shaders + i*65 );
1874                 }
1875                 Sys_Printf( "\n\tSounds....%i\n", pk3SoundsN );
1876                 for ( i = 0; i < pk3SoundsN; i++ ){
1877                         Sys_Printf( "%s\n", pk3Sounds + i*65 );
1878                 }
1879         }
1880
1881         vfsListShaderFiles( pk3Shaderfiles, &pk3ShaderfilesN );
1882
1883         if( dbg ){
1884                 Sys_Printf( "\n\tSchroider fileses.....%i\n", pk3ShaderfilesN );
1885                 for ( i = 0; i < pk3ShaderfilesN; i++ ){
1886                         Sys_Printf( "%s\n", pk3Shaderfiles + i*65 );
1887                 }
1888         }
1889
1890
1891         /* load exclusions file */
1892         int ExTexturesN = 0;
1893         char* ExTextures = (char *)calloc( 4096*65, sizeof( char ) );
1894         int ExShadersN = 0;
1895         char* ExShaders = (char *)calloc( 4096*65, sizeof( char ) );
1896         int ExSoundsN = 0;
1897         char* ExSounds = (char *)calloc( 4096*65, sizeof( char ) );
1898         int ExShaderfilesN = 0;
1899         char* ExShaderfiles = (char *)calloc( 4096*65, sizeof( char ) );
1900         int ExVideosN = 0;
1901         char* ExVideos = (char *)calloc( 4096*65, sizeof( char ) );
1902         int ExPureTexturesN = 0;
1903         char* ExPureTextures = (char *)calloc( 4096*65, sizeof( char ) );
1904
1905         char* ExReasonShader[4096] = { NULL };
1906         char* ExReasonShaderFile[4096] = { NULL };
1907
1908         char exName[ 1024 ];
1909         byte *buffer;
1910         int size;
1911
1912         strcpy( exName, q3map2path );
1913         char *cut = strrchr( exName, '\\' );
1914         char *cut2 = strrchr( exName, '/' );
1915         if ( cut == NULL && cut2 == NULL ){
1916                 Sys_Printf( "WARNING: Unable to load exclusions file.\n" );
1917                 goto skipEXfile;
1918         }
1919         if ( cut2 > cut ) cut = cut2;
1920         cut[1] = '\0';
1921         strcat( exName, game->arg );
1922         strcat( exName, ".exclude" );
1923
1924         Sys_Printf( "Loading %s\n", exName );
1925         size = TryLoadFile( exName, (void**) &buffer );
1926         if ( size <= 0 ) {
1927                 Sys_Printf( "WARNING: Unable to find exclusions file %s.\n", exName );
1928                 goto skipEXfile;
1929         }
1930
1931         /* parse the file */
1932         ParseFromMemory( (char *) buffer, size );
1933
1934         /* tokenize it */
1935         while ( 1 )
1936         {
1937                 /* test for end of file */
1938                 if ( !GetToken( qtrue ) ) {
1939                         break;
1940                 }
1941
1942                 /* blocks */
1943                 if ( !Q_stricmp( token, "textures" ) ){
1944                         parseEXblock ( ExTextures, &ExTexturesN, exName );
1945                 }
1946                 else if ( !Q_stricmp( token, "shaders" ) ){
1947                         parseEXblock ( ExShaders, &ExShadersN, exName );
1948                 }
1949                 else if ( !Q_stricmp( token, "shaderfiles" ) ){
1950                         parseEXblock ( ExShaderfiles, &ExShaderfilesN, exName );
1951                 }
1952                 else if ( !Q_stricmp( token, "sounds" ) ){
1953                         parseEXblock ( ExSounds, &ExSoundsN, exName );
1954                 }
1955                 else if ( !Q_stricmp( token, "videos" ) ){
1956                         parseEXblock ( ExVideos, &ExVideosN, exName );
1957                 }
1958                 else{
1959                         Error( "ReadExclusionsFile: %s, line %d: unknown block name!\nValid ones are: textures, shaders, shaderfiles, sounds, videos.", exName, scriptline );
1960                 }
1961         }
1962
1963         /* free the buffer */
1964         free( buffer );
1965
1966         for ( i = 0; i < ExTexturesN; i++ ){
1967                 for ( j = 0; j < ExShadersN; j++ ){
1968                         if ( !Q_stricmp( ExTextures + i*65, ExShaders + j*65 ) ){
1969                                 break;
1970                         }
1971                 }
1972                 if ( j == ExShadersN ){
1973                         strcpy ( ExPureTextures + ExPureTexturesN*65, ExTextures + i*65 );
1974                         ExPureTexturesN++;
1975                 }
1976         }
1977
1978 skipEXfile:
1979
1980         if( dbg ){
1981                 Sys_Printf( "\n\tExTextures....%i\n", ExTexturesN );
1982                 for ( i = 0; i < ExTexturesN; i++ ) Sys_Printf( "%s\n", ExTextures + i*65 );
1983                 Sys_Printf( "\n\tExPureTextures....%i\n", ExPureTexturesN );
1984                 for ( i = 0; i < ExPureTexturesN; i++ ) Sys_Printf( "%s\n", ExPureTextures + i*65 );
1985                 Sys_Printf( "\n\tExShaders....%i\n", ExShadersN );
1986                 for ( i = 0; i < ExShadersN; i++ ) Sys_Printf( "%s\n", ExShaders + i*65 );
1987                 Sys_Printf( "\n\tExShaderfiles....%i\n", ExShaderfilesN );
1988                 for ( i = 0; i < ExShaderfilesN; i++ ) Sys_Printf( "%s\n", ExShaderfiles + i*65 );
1989                 Sys_Printf( "\n\tExSounds....%i\n", ExSoundsN );
1990                 for ( i = 0; i < ExSoundsN; i++ ) Sys_Printf( "%s\n", ExSounds + i*65 );
1991                 Sys_Printf( "\n\tExVideos....%i\n", ExVideosN );
1992                 for ( i = 0; i < ExVideosN; i++ ) Sys_Printf( "%s\n", ExVideos + i*65 );
1993         }
1994
1995         /* can exclude pure textures right now, shouldn't create shaders for them anyway */
1996         for ( i = 0; i < pk3ShadersN ; i++ ){
1997                 for ( j = 0; j < ExPureTexturesN ; j++ ){
1998                         if ( !Q_stricmp( pk3Shaders + i*65, ExPureTextures + j*65 ) ){
1999                                 *( pk3Shaders + i*65 ) = '\0';
2000                                 break;
2001                         }
2002                 }
2003         }
2004
2005         //Parse Shader Files
2006          /* hack */
2007         endofscript = qtrue;
2008
2009         for ( i = 0; i < pk3ShaderfilesN; i++ ){
2010                 qboolean wantShader = qfalse, wantShaderFile = qfalse, ShaderFileExcluded = qfalse;
2011                 int shader;
2012                 char* reasonShader = NULL;
2013                 char* reasonShaderFile = NULL;
2014
2015                 /* load the shader */
2016                 sprintf( temp, "%s/%s", game->shaderPath, pk3Shaderfiles + i*65 );
2017                 SilentLoadScriptFile( temp, 0 );
2018                 if( dbg ) Sys_Printf( "\n\tentering %s\n", pk3Shaderfiles + i*65 );
2019
2020                 /* do wanna le shader file? */
2021                 for ( j = 0; j < ExShaderfilesN; j++ ){
2022                         if ( !Q_stricmp( ExShaderfiles + j*65, pk3Shaderfiles + i*65 ) ){
2023                                 ShaderFileExcluded = qtrue;
2024                                 reasonShaderFile = ExShaderfiles + j*65;
2025                                 break;
2026                         }
2027                 }
2028                 /* tokenize it */
2029                 /* check if shader file has to be excluded */
2030                 while ( !ShaderFileExcluded )
2031                 {
2032                         /* test for end of file */
2033                         if ( !GetToken( qtrue ) ) {
2034                                 break;
2035                         }
2036
2037                         /* does it contain restricted shaders/textures? */
2038                         for ( j = 0; j < ExShadersN; j++ ){
2039                                 if ( !Q_stricmp( ExShaders + j*65, token ) ){
2040                                         ShaderFileExcluded = qtrue;
2041                                         reasonShader = ExShaders + j*65;
2042                                         break;
2043                                 }
2044                         }
2045                         if ( ShaderFileExcluded )
2046                                 break;
2047                         for ( j = 0; j < ExPureTexturesN; j++ ){
2048                                 if ( !Q_stricmp( ExPureTextures + j*65, token ) ){
2049                                         ShaderFileExcluded = qtrue;
2050                                         reasonShader = ExPureTextures + j*65;
2051                                         break;
2052                                 }
2053                         }
2054                         if ( ShaderFileExcluded )
2055                                 break;
2056
2057                         /* handle { } section */
2058                         if ( !GetToken( qtrue ) ) {
2059                                 break;
2060                         }
2061                         if ( strcmp( token, "{" ) ) {
2062                                         Error( "ParseShaderFile: %s, line %d: { not found!\nFound instead: %s\nFile location be: %s",
2063                                                 temp, scriptline, token, g_strLoadedFileLocation );
2064                         }
2065
2066                         while ( 1 )
2067                         {
2068                                 /* get the next token */
2069                                 if ( !GetToken( qtrue ) ) {
2070                                         break;
2071                                 }
2072                                 if ( !strcmp( token, "}" ) ) {
2073                                         break;
2074                                 }
2075                                 /* parse stage directives */
2076                                 if ( !strcmp( token, "{" ) ) {
2077                                         while ( 1 )
2078                                         {
2079                                                 if ( !GetToken( qtrue ) ) {
2080                                                         break;
2081                                                 }
2082                                                 if ( !strcmp( token, "}" ) ) {
2083                                                         break;
2084                                                 }
2085                                         }
2086                                 }
2087                         }
2088                 }
2089
2090                 /* tokenize it again */
2091                 SilentLoadScriptFile( temp, 0 );
2092                 while ( 1 )
2093                 {
2094                         /* test for end of file */
2095                         if ( !GetToken( qtrue ) ) {
2096                                 break;
2097                         }
2098                         //dump shader names
2099                         if( dbg ) Sys_Printf( "%s\n", token );
2100
2101                         /* do wanna le shader? */
2102                         wantShader = qfalse;
2103                         for ( j = 0; j < pk3ShadersN; j++ ){
2104                                 if ( !Q_stricmp( pk3Shaders + j*65, token) ){
2105                                         shader = j;
2106                                         wantShader = qtrue;
2107                                         break;
2108                                 }
2109                         }
2110
2111                         /* handle { } section */
2112                         if ( !GetToken( qtrue ) ) {
2113                                 break;
2114                         }
2115                         if ( strcmp( token, "{" ) ) {
2116                                         Error( "ParseShaderFile: %s, line %d: { not found!\nFound instead: %s\nFile location be: %s",
2117                                                 temp, scriptline, token, g_strLoadedFileLocation );
2118                         }
2119
2120                         qboolean hasmap = qfalse;
2121                         while ( 1 )
2122                         {
2123                                 /* get the next token */
2124                                 if ( !GetToken( qtrue ) ) {
2125                                         break;
2126                                 }
2127                                 if ( !strcmp( token, "}" ) ) {
2128                                         break;
2129                                 }
2130
2131
2132                                 /* -----------------------------------------------------------------
2133                                 shader stages (passes)
2134                                 ----------------------------------------------------------------- */
2135
2136                                 /* parse stage directives */
2137                                 if ( !strcmp( token, "{" ) ) {
2138                                         while ( 1 )
2139                                         {
2140                                                 if ( !GetToken( qtrue ) ) {
2141                                                         break;
2142                                                 }
2143                                                 if ( !strcmp( token, "}" ) ) {
2144                                                         break;
2145                                                 }
2146                                                 if ( !strcmp( token, "{" ) ) {
2147                                                         Sys_Printf( "WARNING9: %s : line %d : opening brace inside shader stage\n", temp, scriptline );
2148                                                 }
2149                                                 if ( !Q_stricmp( token, "mapComp" ) || !Q_stricmp( token, "mapNoComp" ) || !Q_stricmp( token, "animmapcomp" ) || !Q_stricmp( token, "animmapnocomp" ) ){
2150                                                         Sys_Printf( "WARNING7: %s : line %d : unsupported '%s' map directive\n", temp, scriptline, token );
2151                                                 }
2152                                                 /* skip the shader */
2153                                                 if ( !wantShader ) continue;
2154
2155                                                 /* digest any images */
2156                                                 if ( !Q_stricmp( token, "map" ) ||
2157                                                         !Q_stricmp( token, "clampMap" ) ) {
2158                                                         hasmap = qtrue;
2159                                                         /* get an image */
2160                                                         GetToken( qfalse );
2161                                                         if ( token[ 0 ] != '*' && token[ 0 ] != '$' ) {
2162                                                                 tex2list( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN );
2163                                                         }
2164                                                 }
2165                                                 else if ( !Q_stricmp( token, "animMap" ) ||
2166                                                         !Q_stricmp( token, "clampAnimMap" ) ) {
2167                                                         hasmap = qtrue;
2168                                                         GetToken( qfalse );// skip num
2169                                                         while ( TokenAvailable() ){
2170                                                                 GetToken( qfalse );
2171                                                                 tex2list( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN );
2172                                                         }
2173                                                 }
2174                                                 else if ( !Q_stricmp( token, "videoMap" ) ){
2175                                                         hasmap = qtrue;
2176                                                         GetToken( qfalse );
2177                                                         FixDOSName( token );
2178                                                         if ( strchr( token, '/' ) == NULL && strchr( token, '\\' ) == NULL ){
2179                                                                 sprintf( temp, "video/%s", token );
2180                                                                 strcpy( token, temp );
2181                                                         }
2182                                                         FixDOSName( token );
2183                                                         for ( j = 0; j < pk3VideosN; j++ ){
2184                                                                 if ( !Q_stricmp( pk3Videos + j*65, token ) ){
2185                                                                         goto away;
2186                                                                 }
2187                                                         }
2188                                                         for ( j = 0; j < ExVideosN; j++ ){
2189                                                                 if ( !Q_stricmp( ExVideos + j*65, token ) ){
2190                                                                         goto away;
2191                                                                 }
2192                                                         }
2193                                                         strcpy ( pk3Videos + pk3VideosN*65, token );
2194                                                         pk3VideosN++;
2195                                                         away:
2196                                                         j = 0;
2197                                                 }
2198                                         }
2199                                 }
2200                                 else if ( !Q_strncasecmp( token, "implicit", 8 ) ){
2201                                         Sys_Printf( "WARNING5: %s : line %d : unsupported %s shader\n", temp, scriptline, token );
2202                                 }
2203                                 /* skip the shader */
2204                                 else if ( !wantShader ) continue;
2205
2206                                 /* -----------------------------------------------------------------
2207                                 surfaceparm * directives
2208                                 ----------------------------------------------------------------- */
2209
2210                                 /* match surfaceparm */
2211                                 else if ( !Q_stricmp( token, "surfaceparm" ) ) {
2212                                         GetToken( qfalse );
2213                                         if ( !Q_stricmp( token, "nodraw" ) ) {
2214                                                 wantShader = qfalse;
2215                                                 *( pk3Shaders + shader*65 ) = '\0';
2216                                         }
2217                                 }
2218
2219                                 /* skyparms <outer image> <cloud height> <inner image> */
2220                                 else if ( !Q_stricmp( token, "skyParms" ) ) {
2221                                         hasmap = qtrue;
2222                                         /* get image base */
2223                                         GetToken( qfalse );
2224
2225                                         /* ignore bogus paths */
2226                                         if ( Q_stricmp( token, "-" ) && Q_stricmp( token, "full" ) ) {
2227                                                 strcpy ( temp, token );
2228                                                 sprintf( token, "%s_up", temp );
2229                                                 tex2list( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN );
2230                                                 sprintf( token, "%s_dn", temp );
2231                                                 tex2list( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN );
2232                                                 sprintf( token, "%s_lf", temp );
2233                                                 tex2list( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN );
2234                                                 sprintf( token, "%s_rt", temp );
2235                                                 tex2list( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN );
2236                                                 sprintf( token, "%s_bk", temp );
2237                                                 tex2list( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN );
2238                                                 sprintf( token, "%s_ft", temp );
2239                                                 tex2list( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN );
2240                                         }
2241                                         /* skip rest of line */
2242                                         GetToken( qfalse );
2243                                         GetToken( qfalse );
2244                                 }
2245                                 else if ( !Q_stricmp( token, "fogparms" ) ){
2246                                         hasmap = qtrue;
2247                                 }
2248                         }
2249
2250                         //exclude shader
2251                         if ( wantShader ){
2252                                 for ( j = 0; j < ExShadersN; j++ ){
2253                                         if ( !Q_stricmp( ExShaders + j*65, pk3Shaders + shader*65 ) ){
2254                                                 wantShader = qfalse;
2255                                                 *( pk3Shaders + shader*65 ) = '\0';
2256                                                 break;
2257                                         }
2258                                 }
2259                                 if ( !hasmap ){
2260                                         wantShader = qfalse;
2261                                 }
2262                                 if ( wantShader ){
2263                                         if ( ShaderFileExcluded ){
2264                                                 if ( reasonShaderFile != NULL ){
2265                                                         ExReasonShaderFile[ shader ] = reasonShaderFile;
2266                                                 }
2267                                                 else{
2268                                                         ExReasonShaderFile[ shader ] = ( char* ) calloc( 65, sizeof( char ) );
2269                                                         strcpy( ExReasonShaderFile[ shader ], pk3Shaderfiles + i*65 );
2270                                                 }
2271                                                 ExReasonShader[ shader ] = reasonShader;
2272                                         }
2273                                         else{
2274                                                 wantShaderFile = qtrue;
2275                                                 *( pk3Shaders + shader*65 ) = '\0';
2276                                         }
2277                                 }
2278                         }
2279                 }
2280                 if ( !wantShaderFile ){
2281                         *( pk3Shaderfiles + i*65 ) = '\0';
2282                 }
2283         }
2284
2285
2286
2287 /* exclude stuff */
2288 //wanted shaders from excluded .shaders
2289         Sys_Printf( "\n" );
2290         for ( i = 0; i < pk3ShadersN; i++ ){
2291                 if ( *( pk3Shaders + i*65 ) != '\0' && ( ExReasonShader[i] != NULL || ExReasonShaderFile[i] != NULL ) ){
2292                         Sys_Printf( "  !FAIL! %s\n", pk3Shaders + i*65 );
2293                         packFAIL = qtrue;
2294                         if ( ExReasonShader[i] != NULL ){
2295                                 Sys_Printf( "     reason: is located in %s,\n     containing restricted shader %s\n", ExReasonShaderFile[i], ExReasonShader[i] );
2296                         }
2297                         else{
2298                                 Sys_Printf( "     reason: is located in restricted %s\n", ExReasonShaderFile[i] );
2299                         }
2300                         *( pk3Shaders + i*65 ) = '\0';
2301                 }
2302         }
2303 //pure textures (shader ones are done)
2304         for ( i = 0; i < pk3ShadersN; i++ ){
2305                 if ( *( pk3Shaders + i*65 ) != '\0' ){
2306                         FixDOSName( pk3Shaders + i*65 );
2307                         for ( j = 0; j < pk3TexturesN; j++ ){
2308                                 if ( !Q_stricmp( pk3Shaders + i*65, pk3Textures + j*65 ) ){
2309                                         *( pk3Shaders + i*65 ) = '\0';
2310                                         break;
2311                                 }
2312                         }
2313                         if ( *( pk3Shaders + i*65 ) == '\0' ) continue;
2314                         for ( j = 0; j < ExTexturesN; j++ ){
2315                                 if ( !Q_stricmp( pk3Shaders + i*65, ExTextures + j*65 ) ){
2316                                         *( pk3Shaders + i*65 ) = '\0';
2317                                         break;
2318                                 }
2319                         }
2320                 }
2321         }
2322
2323 //snds
2324         for ( i = 0; i < pk3SoundsN; i++ ){
2325                 for ( j = 0; j < ExSoundsN; j++ ){
2326                         if ( !Q_stricmp( pk3Sounds + i*65, ExSounds + j*65 ) ){
2327                                 *( pk3Sounds + i*65 ) = '\0';
2328                                 break;
2329                         }
2330                 }
2331         }
2332
2333         /* make a pack */
2334         sprintf( packname, "%s/%s_autopacked.pk3", EnginePath, nameOFmap );
2335         remove( packname );
2336         sprintf( packFailName, "%s/%s_FAILEDpack.pk3", EnginePath, nameOFmap );
2337         remove( packFailName );
2338
2339         Sys_Printf( "\n--- ZipZip ---\n" );
2340
2341         Sys_Printf( "\n\tShader referenced textures....\n" );
2342
2343         for ( i = 0; i < pk3TexturesN; i++ ){
2344                 if ( png ){
2345                         sprintf( temp, "%s.png", pk3Textures + i*65 );
2346                         if ( vfsPackFile( temp, packname, 10 ) ){
2347                                 Sys_Printf( "++%s\n", temp );
2348                                 continue;
2349                         }
2350                 }
2351                 sprintf( temp, "%s.tga", pk3Textures + i*65 );
2352                 if ( vfsPackFile( temp, packname, 10 ) ){
2353                         Sys_Printf( "++%s\n", temp );
2354                         continue;
2355                 }
2356                 sprintf( temp, "%s.jpg", pk3Textures + i*65 );
2357                 if ( vfsPackFile( temp, packname, 10 ) ){
2358                         Sys_Printf( "++%s\n", temp );
2359                         continue;
2360                 }
2361                 Sys_Printf( "  !FAIL! %s\n", pk3Textures + i*65 );
2362                 packFAIL = qtrue;
2363         }
2364
2365         Sys_Printf( "\n\tPure textures....\n" );
2366
2367         for ( i = 0; i < pk3ShadersN; i++ ){
2368                 if ( *( pk3Shaders + i*65 ) != '\0' ){
2369                         if ( png ){
2370                                 sprintf( temp, "%s.png", pk3Shaders + i*65 );
2371                                 if ( vfsPackFile( temp, packname, 10 ) ){
2372                                         Sys_Printf( "++%s\n", temp );
2373                                         continue;
2374                                 }
2375                         }
2376                         sprintf( temp, "%s.tga", pk3Shaders + i*65 );
2377                         if ( vfsPackFile( temp, packname, 10 ) ){
2378                                 Sys_Printf( "++%s\n", temp );
2379                                 continue;
2380                         }
2381                         sprintf( temp, "%s.jpg", pk3Shaders + i*65 );
2382                         if ( vfsPackFile( temp, packname, 10 ) ){
2383                                 Sys_Printf( "++%s\n", temp );
2384                                 continue;
2385                         }
2386
2387                         if ( i == pk3ShadersN - 1 ){ //levelshot typically
2388                                 Sys_Printf( "  ~fail  %s\n", pk3Shaders + i*65 );
2389                         }
2390                         else{
2391                                 Sys_Printf( "  !FAIL! %s\n", pk3Shaders + i*65 );
2392                                 packFAIL = qtrue;
2393                         }
2394                 }
2395         }
2396
2397         Sys_Printf( "\n\tShaizers....\n" );
2398
2399         for ( i = 0; i < pk3ShaderfilesN; i++ ){
2400                 if ( *( pk3Shaderfiles + i*65 ) != '\0' ){
2401                         sprintf( temp, "%s/%s", game->shaderPath, pk3Shaderfiles + i*65 );
2402                         if ( vfsPackFile( temp, packname, 10 ) ){
2403                                 Sys_Printf( "++%s\n", temp );
2404                                 continue;
2405                         }
2406                         Sys_Printf( "  !FAIL! %s\n", pk3Shaders + i*65 );
2407                         packFAIL = qtrue;
2408                 }
2409         }
2410
2411         Sys_Printf( "\n\tSounds....\n" );
2412
2413         for ( i = 0; i < pk3SoundsN; i++ ){
2414                 if ( *( pk3Sounds + i*65 ) != '\0' ){
2415                         if ( vfsPackFile( pk3Sounds + i*65, packname, 10 ) ){
2416                                 Sys_Printf( "++%s\n", pk3Sounds + i*65 );
2417                                 continue;
2418                         }
2419                         Sys_Printf( "  !FAIL! %s\n", pk3Sounds + i*65 );
2420                         packFAIL = qtrue;
2421                 }
2422         }
2423
2424         Sys_Printf( "\n\tVideos....\n" );
2425
2426         for ( i = 0; i < pk3VideosN; i++ ){
2427                 if ( vfsPackFile( pk3Videos + i*65, packname, 10 ) ){
2428                         Sys_Printf( "++%s\n", pk3Videos + i*65 );
2429                         continue;
2430                 }
2431                 Sys_Printf( "  !FAIL! %s\n", pk3Videos + i*65 );
2432                 packFAIL = qtrue;
2433         }
2434
2435         Sys_Printf( "\n\t.bsp and stuff\n" );
2436
2437         sprintf( temp, "maps/%s.bsp", nameOFmap );
2438         //if ( vfsPackFile( temp, packname, 10 ) ){
2439         if ( vfsPackFile_Absolute_Path( source, temp, packname, 10 ) ){
2440                         Sys_Printf( "++%s\n", temp );
2441                 }
2442         else{
2443                 Sys_Printf( "  !FAIL! %s\n", temp );
2444                 packFAIL = qtrue;
2445         }
2446
2447         sprintf( temp, "maps/%s.aas", nameOFmap );
2448         if ( vfsPackFile( temp, packname, 10 ) ){
2449                         Sys_Printf( "++%s\n", temp );
2450                 }
2451         else{
2452                 Sys_Printf( "  ~fail  %s\n", temp );
2453         }
2454
2455         sprintf( temp, "scripts/%s.arena", nameOFmap );
2456         if ( vfsPackFile( temp, packname, 10 ) ){
2457                         Sys_Printf( "++%s\n", temp );
2458                 }
2459         else{
2460                 Sys_Printf( "  ~fail  %s\n", temp );
2461         }
2462
2463         sprintf( temp, "scripts/%s.defi", nameOFmap );
2464         if ( vfsPackFile( temp, packname, 10 ) ){
2465                         Sys_Printf( "++%s\n", temp );
2466                 }
2467         else{
2468                 Sys_Printf( "  ~fail  %s\n", temp );
2469         }
2470
2471         if ( !packFAIL ){
2472         Sys_Printf( "\nSaved to %s\n", packname );
2473         }
2474         else{
2475                 rename( packname, packFailName );
2476                 Sys_Printf( "\nSaved to %s\n", packFailName );
2477         }
2478         /* return to sender */
2479         return 0;
2480 }
2481
2482
2483 /*
2484    repackBSPMain()
2485    repack multiple maps, strip out only required shaders
2486    works for Q3 type of shaders and ents
2487  */
2488
2489 int repackBSPMain( int argc, char **argv ){
2490         int i, j, len, compLevel = 0;
2491         qboolean dbg = qfalse, png = qfalse;
2492
2493         /* process arguments */
2494         for ( i = 1; i < ( argc - 1 ); i++ ){
2495                 if ( !strcmp( argv[ i ],  "-dbg" ) ) {
2496                         dbg = qtrue;
2497                 }
2498                 else if ( !strcmp( argv[ i ],  "-png" ) ) {
2499                         png = qtrue;
2500                 }
2501                 else if ( !strcmp( argv[ i ],  "-complevel" ) ) {
2502                         compLevel = atoi( argv[ i + 1 ] );
2503                         i++;
2504                         if ( compLevel < -1 ) compLevel = -1;
2505                         if ( compLevel > 10 ) compLevel = 10;
2506                         Sys_Printf( "Compression level set to %i\n", compLevel );
2507                 }
2508         }
2509
2510 /* load exclusions file */
2511         int ExTexturesN = 0;
2512         char* ExTextures = (char *)calloc( 4096*65, sizeof( char ) );
2513         int ExShadersN = 0;
2514         char* ExShaders = (char *)calloc( 4096*65, sizeof( char ) );
2515         int ExSoundsN = 0;
2516         char* ExSounds = (char *)calloc( 4096*65, sizeof( char ) );
2517         int ExShaderfilesN = 0;
2518         char* ExShaderfiles = (char *)calloc( 4096*65, sizeof( char ) );
2519         int ExVideosN = 0;
2520         char* ExVideos = (char *)calloc( 4096*65, sizeof( char ) );
2521         int ExPureTexturesN = 0;
2522         char* ExPureTextures = (char *)calloc( 4096*65, sizeof( char ) );
2523
2524
2525         char exName[ 1024 ];
2526         byte *buffer;
2527         int size;
2528
2529         strcpy( exName, q3map2path );
2530         char *cut = strrchr( exName, '\\' );
2531         char *cut2 = strrchr( exName, '/' );
2532         if ( cut == NULL && cut2 == NULL ){
2533                 Sys_Printf( "WARNING: Unable to load exclusions file.\n" );
2534                 goto skipEXfile;
2535         }
2536         if ( cut2 > cut ) cut = cut2;
2537         cut[1] = '\0';
2538         strcat( exName, game->arg );
2539         strcat( exName, ".exclude" );
2540
2541         Sys_Printf( "Loading %s\n", exName );
2542         size = TryLoadFile( exName, (void**) &buffer );
2543         if ( size <= 0 ) {
2544                 Sys_Printf( "WARNING: Unable to find exclusions file %s.\n", exName );
2545                 goto skipEXfile;
2546         }
2547
2548         /* parse the file */
2549         ParseFromMemory( (char *) buffer, size );
2550
2551         /* tokenize it */
2552         while ( 1 )
2553         {
2554                 /* test for end of file */
2555                 if ( !GetToken( qtrue ) ) {
2556                         break;
2557                 }
2558
2559                 /* blocks */
2560                 if ( !Q_stricmp( token, "textures" ) ){
2561                         parseEXblock ( ExTextures, &ExTexturesN, exName );
2562                 }
2563                 else if ( !Q_stricmp( token, "shaders" ) ){
2564                         parseEXblock ( ExShaders, &ExShadersN, exName );
2565                 }
2566                 else if ( !Q_stricmp( token, "shaderfiles" ) ){
2567                         parseEXblock ( ExShaderfiles, &ExShaderfilesN, exName );
2568                 }
2569                 else if ( !Q_stricmp( token, "sounds" ) ){
2570                         parseEXblock ( ExSounds, &ExSoundsN, exName );
2571                 }
2572                 else if ( !Q_stricmp( token, "videos" ) ){
2573                         parseEXblock ( ExVideos, &ExVideosN, exName );
2574                 }
2575                 else{
2576                         Error( "ReadExclusionsFile: %s, line %d: unknown block name!\nValid ones are: textures, shaders, shaderfiles, sounds, videos.", exName, scriptline );
2577                 }
2578         }
2579
2580         /* free the buffer */
2581         free( buffer );
2582
2583         for ( i = 0; i < ExTexturesN; i++ ){
2584                 for ( j = 0; j < ExShadersN; j++ ){
2585                         if ( !Q_stricmp( ExTextures + i*65, ExShaders + j*65 ) ){
2586                                 break;
2587                         }
2588                 }
2589                 if ( j == ExShadersN ){
2590                         strcpy ( ExPureTextures + ExPureTexturesN*65, ExTextures + i*65 );
2591                         ExPureTexturesN++;
2592                 }
2593         }
2594
2595 skipEXfile:
2596
2597         if( dbg ){
2598                 Sys_Printf( "\n\tExTextures....%i\n", ExTexturesN );
2599                 for ( i = 0; i < ExTexturesN; i++ ) Sys_Printf( "%s\n", ExTextures + i*65 );
2600                 Sys_Printf( "\n\tExPureTextures....%i\n", ExPureTexturesN );
2601                 for ( i = 0; i < ExPureTexturesN; i++ ) Sys_Printf( "%s\n", ExPureTextures + i*65 );
2602                 Sys_Printf( "\n\tExShaders....%i\n", ExShadersN );
2603                 for ( i = 0; i < ExShadersN; i++ ) Sys_Printf( "%s\n", ExShaders + i*65 );
2604                 Sys_Printf( "\n\tExShaderfiles....%i\n", ExShaderfilesN );
2605                 for ( i = 0; i < ExShaderfilesN; i++ ) Sys_Printf( "%s\n", ExShaderfiles + i*65 );
2606                 Sys_Printf( "\n\tExSounds....%i\n", ExSoundsN );
2607                 for ( i = 0; i < ExSoundsN; i++ ) Sys_Printf( "%s\n", ExSounds + i*65 );
2608                 Sys_Printf( "\n\tExVideos....%i\n", ExVideosN );
2609                 for ( i = 0; i < ExVideosN; i++ ) Sys_Printf( "%s\n", ExVideos + i*65 );
2610         }
2611
2612
2613
2614
2615 /* load repack.exclude */
2616         int rExTexturesN = 0;
2617         char* rExTextures = (char *)calloc( 65536*65, sizeof( char ) );
2618         int rExShadersN = 0;
2619         char* rExShaders = (char *)calloc( 32768*65, sizeof( char ) );
2620         int rExSoundsN = 0;
2621         char* rExSounds = (char *)calloc( 8192*65, sizeof( char ) );
2622         int rExShaderfilesN = 0;
2623         char* rExShaderfiles = (char *)calloc( 4096*65, sizeof( char ) );
2624         int rExVideosN = 0;
2625         char* rExVideos = (char *)calloc( 4096*65, sizeof( char ) );
2626
2627         strcpy( exName, q3map2path );
2628         cut = strrchr( exName, '\\' );
2629         cut2 = strrchr( exName, '/' );
2630         if ( cut == NULL && cut2 == NULL ){
2631                 Sys_Printf( "WARNING: Unable to load repack exclusions file.\n" );
2632                 goto skipEXrefile;
2633         }
2634         if ( cut2 > cut ) cut = cut2;
2635         cut[1] = '\0';
2636         strcat( exName, "repack.exclude" );
2637
2638         Sys_Printf( "Loading %s\n", exName );
2639         size = TryLoadFile( exName, (void**) &buffer );
2640         if ( size <= 0 ) {
2641                 Sys_Printf( "WARNING: Unable to find repack exclusions file %s.\n", exName );
2642                 goto skipEXrefile;
2643         }
2644
2645         /* parse the file */
2646         ParseFromMemory( (char *) buffer, size );
2647
2648         /* tokenize it */
2649         while ( 1 )
2650         {
2651                 /* test for end of file */
2652                 if ( !GetToken( qtrue ) ) {
2653                         break;
2654                 }
2655
2656                 /* blocks */
2657                 if ( !Q_stricmp( token, "textures" ) ){
2658                         parseEXblock ( rExTextures, &rExTexturesN, exName );
2659                 }
2660                 else if ( !Q_stricmp( token, "shaders" ) ){
2661                         parseEXblock ( rExShaders, &rExShadersN, exName );
2662                 }
2663                 else if ( !Q_stricmp( token, "shaderfiles" ) ){
2664                         parseEXblock ( rExShaderfiles, &rExShaderfilesN, exName );
2665                 }
2666                 else if ( !Q_stricmp( token, "sounds" ) ){
2667                         parseEXblock ( rExSounds, &rExSoundsN, exName );
2668                 }
2669                 else if ( !Q_stricmp( token, "videos" ) ){
2670                         parseEXblock ( rExVideos, &rExVideosN, exName );
2671                 }
2672                 else{
2673                         Error( "ReadExclusionsFile: %s, line %d: unknown block name!\nValid ones are: textures, shaders, shaderfiles, sounds, videos.", exName, scriptline );
2674                 }
2675         }
2676
2677         /* free the buffer */
2678         free( buffer );
2679
2680 skipEXrefile:
2681
2682         if( dbg ){
2683                 Sys_Printf( "\n\trExTextures....%i\n", rExTexturesN );
2684                 for ( i = 0; i < rExTexturesN; i++ ) Sys_Printf( "%s\n", rExTextures + i*65 );
2685                 Sys_Printf( "\n\trExShaders....%i\n", rExShadersN );
2686                 for ( i = 0; i < rExShadersN; i++ ) Sys_Printf( "%s\n", rExShaders + i*65 );
2687                 Sys_Printf( "\n\trExShaderfiles....%i\n", rExShaderfilesN );
2688                 for ( i = 0; i < rExShaderfilesN; i++ ) Sys_Printf( "%s\n", rExShaderfiles + i*65 );
2689                 Sys_Printf( "\n\trExSounds....%i\n", rExSoundsN );
2690                 for ( i = 0; i < rExSoundsN; i++ ) Sys_Printf( "%s\n", rExSounds + i*65 );
2691                 Sys_Printf( "\n\trExVideos....%i\n", rExVideosN );
2692                 for ( i = 0; i < rExVideosN; i++ ) Sys_Printf( "%s\n", rExVideos + i*65 );
2693         }
2694
2695
2696
2697
2698         int bspListN = 0;
2699         char* bspList = (char *)calloc( 8192*1024, sizeof( char ) );
2700
2701         /* do some path mangling */
2702         strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
2703         if ( !Q_stricmp( strrchr( source, '.' ), ".bsp" ) ){
2704                 strcpy( bspList, source );
2705                 bspListN++;
2706         }
2707         else{
2708                 /* load bsps paths list */
2709                 Sys_Printf( "Loading %s\n", source );
2710                 size = TryLoadFile( source, (void**) &buffer );
2711                 if ( size <= 0 ) {
2712                         Sys_Printf( "WARNING: Unable to open bsps paths list file %s.\n", source );
2713                 }
2714
2715                 /* parse the file */
2716                 ParseFromMemory( (char *) buffer, size );
2717
2718                 /* tokenize it */
2719                 while ( 1 )
2720                 {
2721                         /* test for end of file */
2722                         if ( !GetToken( qtrue ) ) {
2723                                 break;
2724                         }
2725                         strcpy( bspList + bspListN * 1024 , token );
2726                         bspListN++;
2727                 }
2728
2729                 /* free the buffer */
2730                 free( buffer );
2731         }
2732
2733         char packname[ 1024 ], nameOFrepack[ 1024 ], nameOFmap[ 1024 ], temp[ 1024 ];
2734
2735         /* copy input file name */
2736         strcpy( temp, source );
2737         StripExtension( temp );
2738
2739         /* extract input file name */
2740         len = strlen( temp ) - 1;
2741         while ( len > 0 && temp[ len ] != '/' && temp[ len ] != '\\' )
2742                 len--;
2743         strcpy( nameOFrepack, &temp[ len + 1 ] );
2744
2745
2746 /* load bsps */
2747         int pk3ShadersN = 0;
2748         char* pk3Shaders = (char *)calloc( 65536*65, sizeof( char ) );
2749         int pk3SoundsN = 0;
2750         char* pk3Sounds = (char *)calloc( 4096*65, sizeof( char ) );
2751         int pk3ShaderfilesN = 0;
2752         char* pk3Shaderfiles = (char *)calloc( 4096*65, sizeof( char ) );
2753         int pk3TexturesN = 0;
2754         char* pk3Textures = (char *)calloc( 65536*65, sizeof( char ) );
2755         int pk3VideosN = 0;
2756         char* pk3Videos = (char *)calloc( 1024*65, sizeof( char ) );
2757
2758         for( j = 0; j < bspListN; j++ ){
2759
2760                 int pk3SoundsNold = pk3SoundsN;
2761                 int pk3ShadersNold = pk3ShadersN;
2762
2763                 strcpy( source, bspList + j*1024 );
2764                 StripExtension( source );
2765                 DefaultExtension( source, ".bsp" );
2766
2767                 /* load the bsp */
2768                 Sys_Printf( "\nLoading %s\n", source );
2769                 PartialLoadBSPFile( source );
2770                 ParseEntities();
2771
2772                 /* copy map name */
2773                 strcpy( temp, source );
2774                 StripExtension( temp );
2775
2776                 /* extract map name */
2777                 len = strlen( temp ) - 1;
2778                 while ( len > 0 && temp[ len ] != '/' && temp[ len ] != '\\' )
2779                         len--;
2780                 strcpy( nameOFmap, &temp[ len + 1 ] );
2781
2782
2783                 qboolean drawsurfSHs[1024] = { qfalse };
2784
2785                 for ( i = 0; i < numBSPDrawSurfaces; i++ ){
2786                         drawsurfSHs[ bspDrawSurfaces[i].shaderNum ] = qtrue;
2787                 }
2788
2789                 for ( i = 0; i < numBSPShaders; i++ ){
2790                         if ( drawsurfSHs[i] ){
2791                                 strcpy( pk3Shaders + pk3ShadersN*65, bspShaders[i].shader );
2792                                 res2list( pk3Shaders, &pk3ShadersN );
2793                         }
2794                 }
2795
2796                 /* Ent keys */
2797                 epair_t *ep;
2798                 for ( ep = entities[0].epairs; ep != NULL; ep = ep->next )
2799                 {
2800                         if ( !Q_strncasecmp( ep->key, "vertexremapshader", 17 ) ) {
2801                                 sscanf( ep->value, "%*[^;] %*[;] %s", pk3Shaders + pk3ShadersN*65 );
2802                                 res2list( pk3Shaders, &pk3ShadersN );
2803                         }
2804                 }
2805                 strcpy( pk3Sounds + pk3SoundsN*65, ValueForKey( &entities[0], "music" ) );
2806                 if ( *( pk3Sounds + pk3SoundsN*65 ) != '\0' ){
2807                         FixDOSName( pk3Sounds + pk3SoundsN*65 );
2808                         DefaultExtension( pk3Sounds + pk3SoundsN*65, ".wav" );
2809                         res2list( pk3Sounds, &pk3SoundsN );
2810                 }
2811
2812                 for ( i = 0; i < numBSPEntities && i < numEntities; i++ )
2813                 {
2814                         strcpy( pk3Sounds + pk3SoundsN*65, ValueForKey( &entities[i], "noise" ) );
2815                         if ( *( pk3Sounds + pk3SoundsN*65 ) != '\0' && *( pk3Sounds + pk3SoundsN*65 ) != '*' ){
2816                                 FixDOSName( pk3Sounds + pk3SoundsN*65 );
2817                                 DefaultExtension( pk3Sounds + pk3SoundsN*65, ".wav" );
2818                                 res2list( pk3Sounds, &pk3SoundsN );
2819                         }
2820
2821                         if ( !Q_stricmp( ValueForKey( &entities[i], "classname" ), "func_plat" ) ){
2822                                 strcpy( pk3Sounds + pk3SoundsN*65, "sound/movers/plats/pt1_strt.wav");
2823                                 res2list( pk3Sounds, &pk3SoundsN );
2824                                 strcpy( pk3Sounds + pk3SoundsN*65, "sound/movers/plats/pt1_end.wav");
2825                                 res2list( pk3Sounds, &pk3SoundsN );
2826                         }
2827                         if ( !Q_stricmp( ValueForKey( &entities[i], "classname" ), "target_push" ) ){
2828                                 if ( !(IntForKey( &entities[i], "spawnflags") & 1) ){
2829                                         strcpy( pk3Sounds + pk3SoundsN*65, "sound/misc/windfly.wav");
2830                                         res2list( pk3Sounds, &pk3SoundsN );
2831                                 }
2832                         }
2833                         strcpy( pk3Shaders + pk3ShadersN*65, ValueForKey( &entities[i], "targetShaderNewName" ) );
2834                         res2list( pk3Shaders, &pk3ShadersN );
2835                 }
2836
2837                 //levelshot
2838                 sprintf( pk3Shaders + pk3ShadersN*65, "levelshots/%s", nameOFmap );
2839                 res2list( pk3Shaders, &pk3ShadersN );
2840
2841
2842
2843                 Sys_Printf( "\n\t+Drawsurface+ent calls....%i\n", pk3ShadersN - pk3ShadersNold );
2844                 for ( i = pk3ShadersNold; i < pk3ShadersN; i++ ){
2845                         Sys_Printf( "%s\n", pk3Shaders + i*65 );
2846                 }
2847                 Sys_Printf( "\n\t+Sounds....%i\n", pk3SoundsN - pk3SoundsNold );
2848                 for ( i = pk3SoundsNold; i < pk3SoundsN; i++ ){
2849                         Sys_Printf( "%s\n", pk3Sounds + i*65 );
2850                 }
2851                 /* free bsp data */
2852 /*
2853                 if ( bspDrawVerts != 0 ) {
2854                         free( bspDrawVerts );
2855                         bspDrawVerts = NULL;
2856                         //numBSPDrawVerts = 0;
2857                         Sys_Printf( "freed BSPDrawVerts\n" );
2858                 }
2859 */              if ( bspDrawSurfaces != 0 ) {
2860                         free( bspDrawSurfaces );
2861                         bspDrawSurfaces = NULL;
2862                         //numBSPDrawSurfaces = 0;
2863                         //Sys_Printf( "freed bspDrawSurfaces\n" );
2864                 }
2865 /*              if ( bspLightBytes != 0 ) {
2866                         free( bspLightBytes );
2867                         bspLightBytes = NULL;
2868                         //numBSPLightBytes = 0;
2869                         Sys_Printf( "freed BSPLightBytes\n" );
2870                 }
2871                 if ( bspGridPoints != 0 ) {
2872                         free( bspGridPoints );
2873                         bspGridPoints = NULL;
2874                         //numBSPGridPoints = 0;
2875                         Sys_Printf( "freed BSPGridPoints\n" );
2876                 }
2877                 if ( bspPlanes != 0 ) {
2878                         free( bspPlanes );
2879                         bspPlanes = NULL;
2880                         Sys_Printf( "freed bspPlanes\n" );
2881                         //numBSPPlanes = 0;
2882                         //allocatedBSPPlanes = 0;
2883                 }
2884                 if ( bspBrushes != 0 ) {
2885                         free( bspBrushes );
2886                         bspBrushes = NULL;
2887                         Sys_Printf( "freed bspBrushes\n" );
2888                         //numBSPBrushes = 0;
2889                         //allocatedBSPBrushes = 0;
2890                 }
2891 */              if ( entities != 0 ) {
2892                         epair_t *ep2free;
2893                         for ( i = 0; i < numBSPEntities && i < numEntities; i++ ){
2894                                 ep = entities[i].epairs;
2895                                 while( ep != NULL){
2896                                         ep2free = ep;
2897                                         ep = ep->next;
2898                                         free( ep2free );
2899                                 }
2900                         }
2901                         free( entities );
2902                         entities = NULL;
2903                         //Sys_Printf( "freed entities\n" );
2904                         numEntities = 0;
2905                         numBSPEntities = 0;
2906                         allocatedEntities = 0;
2907                 }
2908 /*              if ( bspModels != 0 ) {
2909                         free( bspModels );
2910                         bspModels = NULL;
2911                         Sys_Printf( "freed bspModels\n" );
2912                         //numBSPModels = 0;
2913                         //allocatedBSPModels = 0;
2914                 }
2915 */              if ( bspShaders != 0 ) {
2916                         free( bspShaders );
2917                         bspShaders = NULL;
2918                         //Sys_Printf( "freed bspShaders\n" );
2919                         //numBSPShaders = 0;
2920                         //allocatedBSPShaders = 0;
2921                 }
2922                 if ( bspEntData != 0 ) {
2923                         free( bspEntData );
2924                         bspEntData = NULL;
2925                         //Sys_Printf( "freed bspEntData\n" );
2926                         //bspEntDataSize = 0;
2927                         //allocatedBSPEntData = 0;
2928                 }
2929 /*              if ( bspNodes != 0 ) {
2930                         free( bspNodes );
2931                         bspNodes = NULL;
2932                         Sys_Printf( "freed bspNodes\n" );
2933                         //numBSPNodes = 0;
2934                         //allocatedBSPNodes = 0;
2935                 }
2936                 if ( bspDrawIndexes != 0 ) {
2937                         free( bspDrawIndexes );
2938                         bspDrawIndexes = NULL;
2939                         Sys_Printf( "freed bspDrawIndexes\n" );
2940                         //numBSPDrawIndexes = 0;
2941                         //allocatedBSPDrawIndexes = 0;
2942                 }
2943                 if ( bspLeafSurfaces != 0 ) {
2944                         free( bspLeafSurfaces );
2945                         bspLeafSurfaces = NULL;
2946                         Sys_Printf( "freed bspLeafSurfaces\n" );
2947                         //numBSPLeafSurfaces = 0;
2948                         //allocatedBSPLeafSurfaces = 0;
2949                 }
2950                 if ( bspLeafBrushes != 0 ) {
2951                         free( bspLeafBrushes );
2952                         bspLeafBrushes = NULL;
2953                         Sys_Printf( "freed bspLeafBrushes\n" );
2954                         //numBSPLeafBrushes = 0;
2955                         //allocatedBSPLeafBrushes = 0;
2956                 }
2957                 if ( bspBrushSides != 0 ) {
2958                         free( bspBrushSides );
2959                         bspBrushSides = NULL;
2960                         Sys_Printf( "freed bspBrushSides\n" );
2961                         numBSPBrushSides = 0;
2962                         allocatedBSPBrushSides = 0;
2963                 }
2964                 if ( numBSPFogs != 0 ) {
2965                         Sys_Printf( "freed numBSPFogs\n" );
2966                         numBSPFogs = 0;
2967                 }
2968                 if ( numBSPAds != 0 ) {
2969                         Sys_Printf( "freed numBSPAds\n" );
2970                         numBSPAds = 0;
2971                 }
2972                 if ( numBSPLeafs != 0 ) {
2973                         Sys_Printf( "freed numBSPLeafs\n" );
2974                         numBSPLeafs = 0;
2975                 }
2976                 if ( numBSPVisBytes != 0 ) {
2977                         Sys_Printf( "freed numBSPVisBytes\n" );
2978                         numBSPVisBytes = 0;
2979                 }
2980 */      }
2981
2982
2983
2984         vfsListShaderFiles( pk3Shaderfiles, &pk3ShaderfilesN );
2985
2986         if( dbg ){
2987                 Sys_Printf( "\n\tSchroider fileses.....%i\n", pk3ShaderfilesN );
2988                 for ( i = 0; i < pk3ShaderfilesN; i++ ){
2989                         Sys_Printf( "%s\n", pk3Shaderfiles + i*65 );
2990                 }
2991         }
2992
2993
2994
2995         /* can exclude pure *base* textures right now, shouldn't create shaders for them anyway */
2996         for ( i = 0; i < pk3ShadersN ; i++ ){
2997                 for ( j = 0; j < ExPureTexturesN ; j++ ){
2998                         if ( !Q_stricmp( pk3Shaders + i*65, ExPureTextures + j*65 ) ){
2999                                 *( pk3Shaders + i*65 ) = '\0';
3000                                 break;
3001                         }
3002                 }
3003         }
3004         /* can exclude repack.exclude shaders, assuming they got all their images */
3005         for ( i = 0; i < pk3ShadersN ; i++ ){
3006                 for ( j = 0; j < rExShadersN ; j++ ){
3007                         if ( !Q_stricmp( pk3Shaders + i*65, rExShaders + j*65 ) ){
3008                                 *( pk3Shaders + i*65 ) = '\0';
3009                                 break;
3010                         }
3011                 }
3012         }
3013
3014         //Parse Shader Files
3015         Sys_Printf( "\t\nParsing shaders....\n\n" );
3016         char shaderText[ 8192 ];
3017         char* allShaders = (char *)calloc( 16777216, sizeof( char ) );
3018          /* hack */
3019         endofscript = qtrue;
3020
3021         for ( i = 0; i < pk3ShaderfilesN; i++ ){
3022                 qboolean wantShader = qfalse;
3023                 int shader;
3024
3025                 /* load the shader */
3026                 sprintf( temp, "%s/%s", game->shaderPath, pk3Shaderfiles + i*65 );
3027                 if ( dbg ) Sys_Printf( "\n\tentering %s\n", pk3Shaderfiles + i*65 );
3028                 SilentLoadScriptFile( temp, 0 );
3029
3030                 /* tokenize it */
3031                 while ( 1 )
3032                 {
3033                         int line = scriptline;
3034                         /* test for end of file */
3035                         if ( !GetToken( qtrue ) ) {
3036                                 break;
3037                         }
3038                         //dump shader names
3039                         if( dbg ) Sys_Printf( "%s\n", token );
3040
3041                         strcpy( shaderText, token );
3042
3043                         if ( strchr( token, '\\') != NULL  ){
3044                                 Sys_Printf( "WARNING1: %s : %s : shader name with backslash\n", pk3Shaderfiles + i*65, token );
3045                         }
3046
3047                         /* do wanna le shader? */
3048                         wantShader = qfalse;
3049                         for ( j = 0; j < pk3ShadersN; j++ ){
3050                                 if ( !Q_stricmp( pk3Shaders + j*65, token) ){
3051                                         shader = j;
3052                                         wantShader = qtrue;
3053                                         break;
3054                                 }
3055                         }
3056                         if ( wantShader ){
3057                                 for ( j = 0; j < rExTexturesN ; j++ ){
3058                                         if ( !Q_stricmp( pk3Shaders + shader*65, rExTextures + j*65 ) ){
3059                                                 Sys_Printf( "WARNING3: %s : about to include shader for excluded texture\n", pk3Shaders + shader*65 );
3060                                                 break;
3061                                         }
3062                                 }
3063                         }
3064
3065                         /* handle { } section */
3066                         if ( !GetToken( qtrue ) ) {
3067                                 break;
3068                         }
3069                         if ( strcmp( token, "{" ) ) {
3070                                         Error( "ParseShaderFile: %s, line %d: { not found!\nFound instead: %s\nFile location be: %s",
3071                                                 temp, scriptline, token, g_strLoadedFileLocation );
3072                         }
3073                         strcat( shaderText, "\n{" );
3074                         qboolean hasmap = qfalse;
3075
3076                         while ( 1 )
3077                         {
3078                                 line = scriptline;
3079                                 /* get the next token */
3080                                 if ( !GetToken( qtrue ) ) {
3081                                         break;
3082                                 }
3083                                 if ( !strcmp( token, "}" ) ) {
3084                                         strcat( shaderText, "\n}\n\n" );
3085                                         break;
3086                                 }
3087                                 /* parse stage directives */
3088                                 if ( !strcmp( token, "{" ) ) {
3089                                         qboolean tokenready = qfalse;
3090                                         strcat( shaderText, "\n\t{" );
3091                                         while ( 1 )
3092                                         {
3093                                                 /* detour of TokenAvailable() '~' */
3094                                                 if ( tokenready ) tokenready = qfalse;
3095                                                 else line = scriptline;
3096                                                 if ( !GetToken( qtrue ) ) {
3097                                                         break;
3098                                                 }
3099                                                 if ( !strcmp( token, "}" ) ) {
3100                                                         strcat( shaderText, "\n\t}" );
3101                                                         break;
3102                                                 }
3103                                                 if ( !strcmp( token, "{" ) ) {
3104                                                         strcat( shaderText, "\n\t{" );
3105                                                         Sys_Printf( "WARNING9: %s : line %d : opening brace inside shader stage\n", temp, scriptline );
3106                                                 }
3107                                                 /* skip the shader */
3108                                                 if ( !wantShader ) continue;
3109
3110                                                 /* digest any images */
3111                                                 if ( !Q_stricmp( token, "map" ) ||
3112                                                         !Q_stricmp( token, "clampMap" ) ) {
3113                                                         strcat( shaderText, "\n\t\t" );
3114                                                         strcat( shaderText, token );
3115                                                         hasmap = qtrue;
3116
3117                                                         /* get an image */
3118                                                         GetToken( qfalse );
3119                                                         if ( token[ 0 ] != '*' && token[ 0 ] != '$' ) {
3120                                                                 tex2list2( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN, rExTextures, &rExTexturesN );
3121                                                         }
3122                                                         strcat( shaderText, " " );
3123                                                         strcat( shaderText, token );
3124                                                 }
3125                                                 else if ( !Q_stricmp( token, "animMap" ) ||
3126                                                         !Q_stricmp( token, "clampAnimMap" ) ) {
3127                                                         strcat( shaderText, "\n\t\t" );
3128                                                         strcat( shaderText, token );
3129                                                         hasmap = qtrue;
3130
3131                                                         GetToken( qfalse );// skip num
3132                                                         strcat( shaderText, " " );
3133                                                         strcat( shaderText, token );
3134                                                         while ( TokenAvailable() ){
3135                                                                 GetToken( qfalse );
3136                                                                 tex2list2( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN, rExTextures, &rExTexturesN );
3137                                                                 strcat( shaderText, " " );
3138                                                                 strcat( shaderText, token );
3139                                                         }
3140                                                         tokenready = qtrue;
3141                                                 }
3142                                                 else if ( !Q_stricmp( token, "videoMap" ) ){
3143                                                         strcat( shaderText, "\n\t\t" );
3144                                                         strcat( shaderText, token );
3145                                                         hasmap = qtrue;
3146                                                         GetToken( qfalse );
3147                                                         strcat( shaderText, " " );
3148                                                         strcat( shaderText, token );
3149                                                         FixDOSName( token );
3150                                                         if ( strchr( token, '/' ) == NULL && strchr( token, '\\' ) == NULL ){
3151                                                                 sprintf( temp, "video/%s", token );
3152                                                                 strcpy( token, temp );
3153                                                         }
3154                                                         FixDOSName( token );
3155                                                         for ( j = 0; j < pk3VideosN; j++ ){
3156                                                                 if ( !Q_stricmp( pk3Videos + j*65, token ) ){
3157                                                                         goto away;
3158                                                                 }
3159                                                         }
3160                                                         for ( j = 0; j < ExVideosN; j++ ){
3161                                                                 if ( !Q_stricmp( ExVideos + j*65, token ) ){
3162                                                                         goto away;
3163                                                                 }
3164                                                         }
3165                                                         for ( j = 0; j < rExVideosN; j++ ){
3166                                                                 if ( !Q_stricmp( rExVideos + j*65, token ) ){
3167                                                                         goto away;
3168                                                                 }
3169                                                         }
3170                                                         strcpy ( pk3Videos + pk3VideosN*65, token );
3171                                                         pk3VideosN++;
3172                                                         away:
3173                                                         j = 0;
3174                                                 }
3175                                                 else if ( !Q_stricmp( token, "mapComp" ) || !Q_stricmp( token, "mapNoComp" ) || !Q_stricmp( token, "animmapcomp" ) || !Q_stricmp( token, "animmapnocomp" ) ){
3176                                                         Sys_Printf( "WARNING7: %s : %s shader\n", pk3Shaders + shader*65, token );
3177                                                         hasmap = qtrue;
3178                                                         if ( line == scriptline ){
3179                                                                 strcat( shaderText, " " );
3180                                                                 strcat( shaderText, token );
3181                                                         }
3182                                                         else{
3183                                                                 strcat( shaderText, "\n\t\t" );
3184                                                                 strcat( shaderText, token );
3185                                                         }
3186                                                 }
3187                                                 else if ( line == scriptline ){
3188                                                         strcat( shaderText, " " );
3189                                                         strcat( shaderText, token );
3190                                                 }
3191                                                 else{
3192                                                         strcat( shaderText, "\n\t\t" );
3193                                                         strcat( shaderText, token );
3194                                                 }
3195                                         }
3196                                 }
3197                                 /* skip the shader */
3198                                 else if ( !wantShader ) continue;
3199
3200                                 /* -----------------------------------------------------------------
3201                                 surfaceparm * directives
3202                                 ----------------------------------------------------------------- */
3203
3204                                 /* match surfaceparm */
3205                                 else if ( !Q_stricmp( token, "surfaceparm" ) ) {
3206                                         strcat( shaderText, "\n\tsurfaceparm " );
3207                                         GetToken( qfalse );
3208                                         strcat( shaderText, token );
3209                                         if ( !Q_stricmp( token, "nodraw" ) ) {
3210                                                 wantShader = qfalse;
3211                                                 *( pk3Shaders + shader*65 ) = '\0';
3212                                         }
3213                                 }
3214
3215                                 /* skyparms <outer image> <cloud height> <inner image> */
3216                                 else if ( !Q_stricmp( token, "skyParms" ) ) {
3217                                         strcat( shaderText, "\n\tskyParms " );
3218                                         hasmap = qtrue;
3219                                         /* get image base */
3220                                         GetToken( qfalse );
3221                                         strcat( shaderText, token );
3222
3223                                         /* ignore bogus paths */
3224                                         if ( Q_stricmp( token, "-" ) && Q_stricmp( token, "full" ) ) {
3225                                                 strcpy ( temp, token );
3226                                                 sprintf( token, "%s_up", temp );
3227                                                 tex2list2( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN, rExTextures, &rExTexturesN );
3228                                                 sprintf( token, "%s_dn", temp );
3229                                                 tex2list2( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN, rExTextures, &rExTexturesN );
3230                                                 sprintf( token, "%s_lf", temp );
3231                                                 tex2list2( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN, rExTextures, &rExTexturesN );
3232                                                 sprintf( token, "%s_rt", temp );
3233                                                 tex2list2( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN, rExTextures, &rExTexturesN );
3234                                                 sprintf( token, "%s_bk", temp );
3235                                                 tex2list2( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN, rExTextures, &rExTexturesN );
3236                                                 sprintf( token, "%s_ft", temp );
3237                                                 tex2list2( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN, rExTextures, &rExTexturesN );
3238                                         }
3239                                         /* skip rest of line */
3240                                         GetToken( qfalse );
3241                                         strcat( shaderText, " " );
3242                                         strcat( shaderText, token );
3243                                         GetToken( qfalse );
3244                                         strcat( shaderText, " " );
3245                                         strcat( shaderText, token );
3246                                 }
3247                                 else if ( !Q_strncasecmp( token, "implicit", 8 ) ){
3248                                         Sys_Printf( "WARNING5: %s : %s shader\n", pk3Shaders + shader*65, token );
3249                                         hasmap = qtrue;
3250                                         if ( line == scriptline ){
3251                                                 strcat( shaderText, " " );
3252                                                 strcat( shaderText, token );
3253                                         }
3254                                         else{
3255                                                 strcat( shaderText, "\n\t" );
3256                                                 strcat( shaderText, token );
3257                                         }
3258                                 }
3259                                 else if ( !Q_stricmp( token, "fogparms" ) ){
3260                                         hasmap = qtrue;
3261                                         if ( line == scriptline ){
3262                                                 strcat( shaderText, " " );
3263                                                 strcat( shaderText, token );
3264                                         }
3265                                         else{
3266                                                 strcat( shaderText, "\n\t" );
3267                                                 strcat( shaderText, token );
3268                                         }
3269                                 }
3270                                 else if ( line == scriptline ){
3271                                         strcat( shaderText, " " );
3272                                         strcat( shaderText, token );
3273                                 }
3274                                 else{
3275                                         strcat( shaderText, "\n\t" );
3276                                         strcat( shaderText, token );
3277                                 }
3278                         }
3279
3280                         //exclude shader
3281                         if ( wantShader ){
3282                                 for ( j = 0; j < ExShadersN; j++ ){
3283                                         if ( !Q_stricmp( ExShaders + j*65, pk3Shaders + shader*65 ) ){
3284                                                 wantShader = qfalse;
3285                                                 *( pk3Shaders + shader*65 ) = '\0';
3286                                                 break;
3287                                         }
3288                                 }
3289                                 if ( wantShader && !hasmap ){
3290                                         Sys_Printf( "WARNING8: %s : shader has no known maps\n", pk3Shaders + shader*65 );
3291                                         wantShader = qfalse;
3292                                         *( pk3Shaders + shader*65 ) = '\0';
3293                                 }
3294                                 if ( wantShader ){
3295                                         strcat( allShaders, shaderText );
3296                                         *( pk3Shaders + shader*65 ) = '\0';
3297                                 }
3298                         }
3299                 }
3300         }
3301 /* TODO: RTCW's mapComp, mapNoComp, animmapcomp, animmapnocomp; nocompress?; ET's implicitmap, implicitblend, implicitmask */
3302
3303
3304 /* exclude stuff */
3305
3306 //pure textures (shader ones are done)
3307         for ( i = 0; i < pk3ShadersN; i++ ){
3308                 if ( *( pk3Shaders + i*65 ) != '\0' ){
3309                         if ( strchr( pk3Shaders + i*65, '\\') != NULL  ){
3310                                 Sys_Printf( "WARNING2: %s : bsp shader path with backslash\n", pk3Shaders + i*65 );
3311                                 FixDOSName( pk3Shaders + i*65 );
3312                                 //what if theres properly slashed one in the list?
3313                                 for ( j = 0; j < pk3ShadersN; j++ ){
3314                                         if ( !Q_stricmp( pk3Shaders + i*65, pk3Shaders + j*65 ) && (i != j) ){
3315                                                 *( pk3Shaders + i*65 ) = '\0';
3316                                                 break;
3317                                         }
3318                                 }
3319                         }
3320                         if ( *( pk3Shaders + i*65 ) == '\0' ) continue;
3321                         for ( j = 0; j < pk3TexturesN; j++ ){
3322                                 if ( !Q_stricmp( pk3Shaders + i*65, pk3Textures + j*65 ) ){
3323                                         *( pk3Shaders + i*65 ) = '\0';
3324                                         break;
3325                                 }
3326                         }
3327                         if ( *( pk3Shaders + i*65 ) == '\0' ) continue;
3328                         for ( j = 0; j < ExTexturesN; j++ ){
3329                                 if ( !Q_stricmp( pk3Shaders + i*65, ExTextures + j*65 ) ){
3330                                         *( pk3Shaders + i*65 ) = '\0';
3331                                         break;
3332                                 }
3333                         }
3334                         if ( *( pk3Shaders + i*65 ) == '\0' ) continue;
3335                         for ( j = 0; j < rExTexturesN; j++ ){
3336                                 if ( !Q_stricmp( pk3Shaders + i*65, rExTextures + j*65 ) ){
3337                                         *( pk3Shaders + i*65 ) = '\0';
3338                                         break;
3339                                 }
3340                         }
3341                 }
3342         }
3343
3344 //snds
3345         for ( i = 0; i < pk3SoundsN; i++ ){
3346                 for ( j = 0; j < ExSoundsN; j++ ){
3347                         if ( !Q_stricmp( pk3Sounds + i*65, ExSounds + j*65 ) ){
3348                                 *( pk3Sounds + i*65 ) = '\0';
3349                                 break;
3350                         }
3351                 }
3352                 if ( *( pk3Sounds + i*65 ) == '\0' ) continue;
3353                 for ( j = 0; j < rExSoundsN; j++ ){
3354                         if ( !Q_stricmp( pk3Sounds + i*65, rExSounds + j*65 ) ){
3355                                 *( pk3Sounds + i*65 ) = '\0';
3356                                 break;
3357                         }
3358                 }
3359         }
3360
3361         /* write shader */
3362         sprintf( temp, "%s/%s_strippedBYrepacker.shader", EnginePath, nameOFrepack );
3363         FILE *f;
3364         f = fopen( temp, "wb" );
3365         fwrite( allShaders, sizeof( char ), strlen( allShaders ), f );
3366         fclose( f );
3367         Sys_Printf( "Shaders saved to %s\n", temp );
3368
3369         /* make a pack */
3370         sprintf( packname, "%s/%s_repacked.pk3", EnginePath, nameOFrepack );
3371         remove( packname );
3372
3373         Sys_Printf( "\n--- ZipZip ---\n" );
3374
3375         Sys_Printf( "\n\tShader referenced textures....\n" );
3376
3377         for ( i = 0; i < pk3TexturesN; i++ ){
3378                 if ( png ){
3379                         sprintf( temp, "%s.png", pk3Textures + i*65 );
3380                         if ( vfsPackFile( temp, packname, compLevel ) ){
3381                                 Sys_Printf( "++%s\n", temp );
3382                                 continue;
3383                         }
3384                 }
3385                 sprintf( temp, "%s.tga", pk3Textures + i*65 );
3386                 if ( vfsPackFile( temp, packname, compLevel ) ){
3387                         Sys_Printf( "++%s\n", temp );
3388                         continue;
3389                 }
3390                 sprintf( temp, "%s.jpg", pk3Textures + i*65 );
3391                 if ( vfsPackFile( temp, packname, compLevel ) ){
3392                         Sys_Printf( "++%s\n", temp );
3393                         continue;
3394                 }
3395                 Sys_Printf( "  !FAIL! %s\n", pk3Textures + i*65 );
3396         }
3397
3398         Sys_Printf( "\n\tPure textures....\n" );
3399
3400         for ( i = 0; i < pk3ShadersN; i++ ){
3401                 if ( *( pk3Shaders + i*65 ) != '\0' ){
3402                         if ( png ){
3403                                 sprintf( temp, "%s.png", pk3Shaders + i*65 );
3404                                 if ( vfsPackFile( temp, packname, compLevel ) ){
3405                                         Sys_Printf( "++%s\n", temp );
3406                                         continue;
3407                                 }
3408                         }
3409                         sprintf( temp, "%s.tga", pk3Shaders + i*65 );
3410                         if ( vfsPackFile( temp, packname, compLevel ) ){
3411                                 Sys_Printf( "++%s\n", temp );
3412                                 continue;
3413                         }
3414                         sprintf( temp, "%s.jpg", pk3Shaders + i*65 );
3415                         if ( vfsPackFile( temp, packname, compLevel ) ){
3416                                 Sys_Printf( "++%s\n", temp );
3417                                 continue;
3418                         }
3419                         Sys_Printf( "  !FAIL! %s\n", pk3Shaders + i*65 );
3420                 }
3421         }
3422
3423         Sys_Printf( "\n\tSounds....\n" );
3424
3425         for ( i = 0; i < pk3SoundsN; i++ ){
3426                 if ( *( pk3Sounds + i*65 ) != '\0' ){
3427                         if ( vfsPackFile( pk3Sounds + i*65, packname, compLevel ) ){
3428                                 Sys_Printf( "++%s\n", pk3Sounds + i*65 );
3429                                 continue;
3430                         }
3431                         Sys_Printf( "  !FAIL! %s\n", pk3Sounds + i*65 );
3432                 }
3433         }
3434
3435         Sys_Printf( "\n\tVideos....\n" );
3436
3437         for ( i = 0; i < pk3VideosN; i++ ){
3438                 if ( vfsPackFile( pk3Videos + i*65, packname, compLevel ) ){
3439                         Sys_Printf( "++%s\n", pk3Videos + i*65 );
3440                         continue;
3441                 }
3442                 Sys_Printf( "  !FAIL! %s\n", pk3Videos + i*65 );
3443         }
3444
3445         Sys_Printf( "\nSaved to %s\n", packname );
3446
3447         /* return to sender */
3448         return 0;
3449 }
3450
3451
3452
3453 /*
3454    PseudoCompileBSP()
3455    a stripped down ProcessModels
3456  */
3457 void PseudoCompileBSP( qboolean need_tree ){
3458         int models;
3459         char modelValue[10];
3460         entity_t *entity;
3461         face_t *faces;
3462         tree_t *tree;
3463         node_t *node;
3464         brush_t *brush;
3465         side_t *side;
3466         int i;
3467
3468         SetDrawSurfacesBuffer();
3469         mapDrawSurfs = safe_malloc( sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
3470         memset( mapDrawSurfs, 0, sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
3471         numMapDrawSurfs = 0;
3472
3473         BeginBSPFile();
3474         models = 1;
3475         for ( mapEntityNum = 0; mapEntityNum < numEntities; mapEntityNum++ )
3476         {
3477                 /* get entity */
3478                 entity = &entities[ mapEntityNum ];
3479                 if ( entity->brushes == NULL && entity->patches == NULL ) {
3480                         continue;
3481                 }
3482
3483                 if ( mapEntityNum != 0 ) {
3484                         sprintf( modelValue, "*%d", models++ );
3485                         SetKeyValue( entity, "model", modelValue );
3486                 }
3487
3488                 /* process the model */
3489                 Sys_FPrintf( SYS_VRB, "############### model %i ###############\n", numBSPModels );
3490                 BeginModel();
3491
3492                 entity->firstDrawSurf = numMapDrawSurfs;
3493
3494                 ClearMetaTriangles();
3495                 PatchMapDrawSurfs( entity );
3496
3497                 if ( mapEntityNum == 0 && need_tree ) {
3498                         faces = MakeStructuralBSPFaceList( entities[0].brushes );
3499                         tree = FaceBSP( faces );
3500                         node = tree->headnode;
3501                 }
3502                 else
3503                 {
3504                         node = AllocNode();
3505                         node->planenum = PLANENUM_LEAF;
3506                         tree = AllocTree();
3507                         tree->headnode = node;
3508                 }
3509
3510                 /* a minimized ClipSidesIntoTree */
3511                 for ( brush = entity->brushes; brush; brush = brush->next )
3512                 {
3513                         /* walk the brush sides */
3514                         for ( i = 0; i < brush->numsides; i++ )
3515                         {
3516                                 /* get side */
3517                                 side = &brush->sides[ i ];
3518                                 if ( side->winding == NULL ) {
3519                                         continue;
3520                                 }
3521                                 /* shader? */
3522                                 if ( side->shaderInfo == NULL ) {
3523                                         continue;
3524                                 }
3525                                 /* save this winding as a visible surface */
3526                                 DrawSurfaceForSide( entity, brush, side, side->winding );
3527                         }
3528                 }
3529
3530                 if ( meta ) {
3531                         ClassifyEntitySurfaces( entity );
3532                         MakeEntityDecals( entity );
3533                         MakeEntityMetaTriangles( entity );
3534                         SmoothMetaTriangles();
3535                         MergeMetaTriangles();
3536                 }
3537                 FilterDrawsurfsIntoTree( entity, tree );
3538
3539                 FilterStructuralBrushesIntoTree( entity, tree );
3540                 FilterDetailBrushesIntoTree( entity, tree );
3541
3542                 EmitBrushes( entity->brushes, &entity->firstBrush, &entity->numBrushes );
3543                 EndModel( entity, node );
3544         }
3545         EndBSPFile( qfalse );
3546 }
3547
3548 /*
3549    ConvertBSPMain()
3550    main argument processing function for bsp conversion
3551  */
3552
3553 int ConvertBSPMain( int argc, char **argv ){
3554         int i;
3555         int ( *convertFunc )( char * );
3556         game_t  *convertGame;
3557         char ext[1024];
3558         qboolean map_allowed, force_bsp, force_map;
3559
3560
3561         /* set default */
3562         convertFunc = ConvertBSPToASE;
3563         convertGame = NULL;
3564         map_allowed = qfalse;
3565         force_bsp = qfalse;
3566         force_map = qfalse;
3567
3568         /* arg checking */
3569         if ( argc < 1 ) {
3570                 Sys_Printf( "Usage: q3map -convert [-format <ase|obj|map_bp|map>] [-shadersasbitmap|-lightmapsastexcoord|-deluxemapsastexcoord] [-readbsp|-readmap [-meta|-patchmeta]] [-v] <mapname>\n" );
3571                 return 0;
3572         }
3573
3574         /* process arguments */
3575         for ( i = 1; i < ( argc - 1 ); i++ )
3576         {
3577                 /* -format map|ase|... */
3578                 if ( !strcmp( argv[ i ],  "-format" ) ) {
3579                         i++;
3580                         if ( !Q_stricmp( argv[ i ], "ase" ) ) {
3581                                 convertFunc = ConvertBSPToASE;
3582                                 map_allowed = qfalse;
3583                         }
3584                         else if ( !Q_stricmp( argv[ i ], "obj" ) ) {
3585                                 convertFunc = ConvertBSPToOBJ;
3586                                 map_allowed = qfalse;
3587                         }
3588                         else if ( !Q_stricmp( argv[ i ], "map_bp" ) ) {
3589                                 convertFunc = ConvertBSPToMap_BP;
3590                                 map_allowed = qtrue;
3591                         }
3592                         else if ( !Q_stricmp( argv[ i ], "map" ) ) {
3593                                 convertFunc = ConvertBSPToMap;
3594                                 map_allowed = qtrue;
3595                         }
3596                         else
3597                         {
3598                                 convertGame = GetGame( argv[ i ] );
3599                                 map_allowed = qfalse;
3600                                 if ( convertGame == NULL ) {
3601                                         Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
3602                                 }
3603                         }
3604                 }
3605                 else if ( !strcmp( argv[ i ],  "-ne" ) ) {
3606                         normalEpsilon = atof( argv[ i + 1 ] );
3607                         i++;
3608                         Sys_Printf( "Normal epsilon set to %f\n", normalEpsilon );
3609                 }
3610                 else if ( !strcmp( argv[ i ],  "-de" ) ) {
3611                         distanceEpsilon = atof( argv[ i + 1 ] );
3612                         i++;
3613                         Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
3614                 }
3615                 else if ( !strcmp( argv[ i ],  "-shaderasbitmap" ) || !strcmp( argv[ i ],  "-shadersasbitmap" ) ) {
3616                         shadersAsBitmap = qtrue;
3617                 }
3618                 else if ( !strcmp( argv[ i ],  "-lightmapastexcoord" ) || !strcmp( argv[ i ],  "-lightmapsastexcoord" ) ) {
3619                         lightmapsAsTexcoord = qtrue;
3620                 }
3621                 else if ( !strcmp( argv[ i ],  "-deluxemapastexcoord" ) || !strcmp( argv[ i ],  "-deluxemapsastexcoord" ) ) {
3622                         lightmapsAsTexcoord = qtrue;
3623                         deluxemap = qtrue;
3624                 }
3625                 else if ( !strcmp( argv[ i ],  "-readbsp" ) ) {
3626                         force_bsp = qtrue;
3627                 }
3628                 else if ( !strcmp( argv[ i ],  "-readmap" ) ) {
3629                         force_map = qtrue;
3630                 }
3631                 else if ( !strcmp( argv[ i ],  "-meta" ) ) {
3632                         meta = qtrue;
3633                 }
3634                 else if ( !strcmp( argv[ i ],  "-patchmeta" ) ) {
3635                         meta = qtrue;
3636                         patchMeta = qtrue;
3637                 }
3638                 else if ( !strcmp( argv[ i ],  "-fast" ) ) {
3639                         fast = qtrue;
3640                 }
3641         }
3642
3643         LoadShaderInfo();
3644
3645         /* clean up map name */
3646         strcpy( source, ExpandArg( argv[i] ) );
3647         ExtractFileExtension( source, ext );
3648
3649         if ( !map_allowed && !force_map ) {
3650                 force_bsp = qtrue;
3651         }
3652
3653         if ( force_map || ( !force_bsp && !Q_stricmp( ext, "map" ) && map_allowed ) ) {
3654                 if ( !map_allowed ) {
3655                         Sys_Printf( "WARNING: the requested conversion should not be done from .map files. Compile a .bsp first.\n" );
3656                 }
3657                 StripExtension( source );
3658                 DefaultExtension( source, ".map" );
3659                 Sys_Printf( "Loading %s\n", source );
3660                 LoadMapFile( source, qfalse, convertGame == NULL );
3661                 PseudoCompileBSP( convertGame != NULL );
3662         }
3663         else
3664         {
3665                 StripExtension( source );
3666                 DefaultExtension( source, ".bsp" );
3667                 Sys_Printf( "Loading %s\n", source );
3668                 LoadBSPFile( source );
3669                 ParseEntities();
3670         }
3671
3672         /* bsp format convert? */
3673         if ( convertGame != NULL ) {
3674                 /* set global game */
3675                 game = convertGame;
3676
3677                 /* write bsp */
3678                 StripExtension( source );
3679                 DefaultExtension( source, "_c.bsp" );
3680                 Sys_Printf( "Writing %s\n", source );
3681                 WriteBSPFile( source );
3682
3683                 /* return to sender */
3684                 return 0;
3685         }
3686
3687         /* normal convert */
3688         return convertFunc( source );
3689 }
3690
3691
3692
3693 /*
3694    main()
3695    q3map mojo...
3696  */
3697
3698 int main( int argc, char **argv ){
3699         int i, r;
3700         double start, end;
3701
3702 #ifdef WIN32
3703         _setmaxstdio(2048);
3704 #endif
3705
3706         /* we want consistent 'randomness' */
3707         srand( 0 );
3708
3709         /* start timer */
3710         start = I_FloatTime();
3711
3712         /* this was changed to emit version number over the network */
3713         printf( Q3MAP_VERSION "\n" );
3714
3715         /* set exit call */
3716         atexit( ExitQ3Map );
3717
3718         /* read general options first */
3719         for ( i = 1; i < argc; i++ )
3720         {
3721                 /* -connect */
3722                 if ( !strcmp( argv[ i ], "-connect" ) ) {
3723                         argv[ i ] = NULL;
3724                         i++;
3725                         Broadcast_Setup( argv[ i ] );
3726                         argv[ i ] = NULL;
3727                 }
3728
3729                 /* verbose */
3730                 else if ( !strcmp( argv[ i ], "-v" ) ) {
3731                         if ( !verbose ) {
3732                                 verbose = qtrue;
3733                                 argv[ i ] = NULL;
3734                         }
3735                 }
3736
3737                 /* force */
3738                 else if ( !strcmp( argv[ i ], "-force" ) ) {
3739                         force = qtrue;
3740                         argv[ i ] = NULL;
3741                 }
3742
3743                 /* patch subdivisions */
3744                 else if ( !strcmp( argv[ i ], "-subdivisions" ) ) {
3745                         argv[ i ] = NULL;
3746                         i++;
3747                         patchSubdivisions = atoi( argv[ i ] );
3748                         argv[ i ] = NULL;
3749                         if ( patchSubdivisions <= 0 ) {
3750                                 patchSubdivisions = 1;
3751                         }
3752                 }
3753
3754                 /* threads */
3755                 else if ( !strcmp( argv[ i ], "-threads" ) ) {
3756                         argv[ i ] = NULL;
3757                         i++;
3758                         numthreads = atoi( argv[ i ] );
3759                         argv[ i ] = NULL;
3760                 }
3761
3762                 else if( !strcmp( argv[ i ], "-nocmdline" ) )
3763                 {
3764                         Sys_Printf( "noCmdLine\n" );
3765                         nocmdline = qtrue;
3766                         argv[ i ] = NULL;
3767                 }
3768
3769         }
3770
3771         /* init model library */
3772         PicoInit();
3773         PicoSetMallocFunc( safe_malloc );
3774         PicoSetFreeFunc( free );
3775         PicoSetPrintFunc( PicoPrintFunc );
3776         PicoSetLoadFileFunc( PicoLoadFileFunc );
3777         PicoSetFreeFileFunc( free );
3778
3779         /* set number of threads */
3780         ThreadSetDefault();
3781
3782         /* generate sinusoid jitter table */
3783         for ( i = 0; i < MAX_JITTERS; i++ )
3784         {
3785                 jitters[ i ] = sin( i * 139.54152147 );
3786                 //%     Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
3787         }
3788
3789         /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
3790            and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
3791
3792         Sys_Printf( "Q3Map         - v1.0r (c) 1999 Id Software Inc.\n" );
3793         Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
3794         Sys_Printf( "NetRadiant    - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
3795         Sys_Printf( "%s\n", Q3MAP_MOTD );
3796         Sys_Printf( "%s\n", argv[0] );
3797
3798         strcpy( q3map2path, argv[0] );//fuer autoPack func
3799
3800         /* ydnar: new path initialization */
3801         InitPaths( &argc, argv );
3802
3803         /* set game options */
3804         if ( !patchSubdivisions ) {
3805                 patchSubdivisions = game->patchSubdivisions;
3806         }
3807
3808         /* check if we have enough options left to attempt something */
3809         if ( argc < 2 ) {
3810                 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
3811         }
3812
3813         /* fixaas */
3814         if ( !strcmp( argv[ 1 ], "-fixaas" ) ) {
3815                 r = FixAAS( argc - 1, argv + 1 );
3816         }
3817
3818         /* analyze */
3819         else if ( !strcmp( argv[ 1 ], "-analyze" ) ) {
3820                 r = AnalyzeBSP( argc - 1, argv + 1 );
3821         }
3822
3823         /* info */
3824         else if ( !strcmp( argv[ 1 ], "-info" ) ) {
3825                 r = BSPInfo( argc - 2, argv + 2 );
3826         }
3827
3828         /* vis */
3829         else if ( !strcmp( argv[ 1 ], "-vis" ) ) {
3830                 r = VisMain( argc - 1, argv + 1 );
3831         }
3832
3833         /* light */
3834         else if ( !strcmp( argv[ 1 ], "-light" ) ) {
3835                 r = LightMain( argc - 1, argv + 1 );
3836         }
3837
3838         /* vlight */
3839         else if ( !strcmp( argv[ 1 ], "-vlight" ) ) {
3840                 Sys_Printf( "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
3841                 argv[ 1 ] = "-fast";    /* eek a hack */
3842                 r = LightMain( argc, argv );
3843         }
3844
3845         /* ydnar: lightmap export */
3846         else if ( !strcmp( argv[ 1 ], "-export" ) ) {
3847                 r = ExportLightmapsMain( argc - 1, argv + 1 );
3848         }
3849
3850         /* ydnar: lightmap import */
3851         else if ( !strcmp( argv[ 1 ], "-import" ) ) {
3852                 r = ImportLightmapsMain( argc - 1, argv + 1 );
3853         }
3854
3855         /* ydnar: bsp scaling */
3856         else if ( !strcmp( argv[ 1 ], "-scale" ) ) {
3857                 r = ScaleBSPMain( argc - 1, argv + 1 );
3858         }
3859
3860         /* bsp shifting */
3861         else if ( !strcmp( argv[ 1 ], "-shift" ) ) {
3862                 r = ShiftBSPMain( argc - 1, argv + 1 );
3863         }
3864
3865         /* autopacking */
3866         else if ( !strcmp( argv[ 1 ], "-pk3" ) ) {
3867                 r = pk3BSPMain( argc - 1, argv + 1 );
3868         }
3869
3870         /* repacker */
3871         else if ( !strcmp( argv[ 1 ], "-repack" ) ) {
3872                 r = repackBSPMain( argc - 1, argv + 1 );
3873         }
3874
3875         /* ydnar: bsp conversion */
3876         else if ( !strcmp( argv[ 1 ], "-convert" ) ) {
3877                 r = ConvertBSPMain( argc - 1, argv + 1 );
3878         }
3879
3880         /* div0: minimap */
3881         else if ( !strcmp( argv[ 1 ], "-minimap" ) ) {
3882                 r = MiniMapBSPMain( argc - 1, argv + 1 );
3883         }
3884
3885         /* ydnar: otherwise create a bsp */
3886         else{
3887                 r = BSPMain( argc, argv );
3888         }
3889
3890         /* emit time */
3891         end = I_FloatTime();
3892         Sys_Printf( "%9.0f seconds elapsed\n", end - start );
3893
3894         /* shut down connection */
3895         Broadcast_Shutdown();
3896
3897         /* return any error code */
3898         return r;
3899 }