1 /* -------------------------------------------------------------------------------;
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
6 This file is part of GtkRadiant.
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.
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.
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
22 -------------------------------------------------------------------------------
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."
27 ------------------------------------------------------------------------------- */
43 returns a pseudorandom number between 0 and 1
47 return (vec_t) rand() / RAND_MAX;
51 char *Q_strncpyz( char *dst, const char *src, size_t len ) {
56 strncpy( dst, src, len );
57 dst[ len - 1 ] = '\0';
62 char *Q_strcat( char *dst, size_t dlen, const char *src ) {
63 size_t n = strlen( dst );
66 abort(); /* buffer overflow */
69 return Q_strncpyz( dst + n, src, dlen - n );
73 char *Q_strncat( char *dst, size_t dlen, const char *src, size_t slen ) {
74 size_t n = strlen( dst );
77 abort(); /* buffer overflow */
80 return Q_strncpyz( dst + n, src, MIN( slen, dlen - n ) );
89 static void ExitQ3Map( void ){
91 if ( mapDrawSurfs != NULL ) {
100 typedef struct minimap_s
106 float *sample_offsets;
107 float sharpen_boxmult;
108 float sharpen_centermult;
109 float boost, brightness, contrast;
111 float *sharpendata1f;
115 static minimap_t minimap;
117 qboolean BrushIntersectionWithLine( bspBrush_t *brush, vec3_t start, vec3_t dir, float *t_in, float *t_out ){
119 qboolean in = qfalse, out = qfalse;
120 bspBrushSide_t *sides = &bspBrushSides[brush->firstSide];
122 for ( i = 0; i < brush->numSides; ++i )
124 bspPlane_t *p = &bspPlanes[sides[i].planeNum];
125 float sn = DotProduct( start, p->normal );
126 float dn = DotProduct( dir, p->normal );
128 if ( sn > p->dist ) {
129 return qfalse; // outside!
134 float t = ( p->dist - sn ) / dn;
136 if ( !in || t > *t_in ) {
139 // as t_in can only increase, and t_out can only decrease, early out
140 if ( out && *t_in >= *t_out ) {
147 if ( !out || t < *t_out ) {
150 // as t_in can only increase, and t_out can only decrease, early out
151 if ( in && *t_in >= *t_out ) {
161 static float MiniMapSample( float x, float y ){
179 for ( i = 0; i < minimap.model->numBSPBrushes; ++i )
181 bi = minimap.model->firstBSPBrush + i;
182 if ( opaqueBrushes[bi >> 3] & ( 1 << ( bi & 7 ) ) ) {
185 // sort out mins/maxs of the brush
186 s = &bspBrushSides[b->firstSide];
187 if ( x < -bspPlanes[s[0].planeNum].dist ) {
190 if ( x > +bspPlanes[s[1].planeNum].dist ) {
193 if ( y < -bspPlanes[s[2].planeNum].dist ) {
196 if ( y > +bspPlanes[s[3].planeNum].dist ) {
200 if ( BrushIntersectionWithLine( b, org, dir, &t0, &t1 ) ) {
210 void RandomVector2f( float v[2] ){
213 v[0] = 2 * Random() - 1;
214 v[1] = 2 * Random() - 1;
216 while ( v[0] * v[0] + v[1] * v[1] > 1 );
219 static void MiniMapRandomlySupersampled( int y ){
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;
228 for ( x = 0; x < minimap.width; ++x )
230 float xmin = minimap.mins[0] + minimap.size[0] * ( x / (float) minimap.width );
233 for ( i = 0; i < minimap.samples; ++i )
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 */
242 val /= minimap.samples * minimap.size[2];
247 static void MiniMapSupersampled( int y ){
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;
254 for ( x = 0; x < minimap.width; ++x )
256 float xmin = minimap.mins[0] + minimap.size[0] * ( x / (float) minimap.width );
259 for ( i = 0; i < minimap.samples; ++i )
261 float thisval = MiniMapSample(
262 xmin + minimap.sample_offsets[2 * i + 0] * dx,
263 ymin + minimap.sample_offsets[2 * i + 1] * dy
267 val /= minimap.samples * minimap.size[2];
272 static void MiniMapNoSupersampling( int y ){
274 float *p = &minimap.data1f[y * minimap.width];
275 float ymin = minimap.mins[1] + minimap.size[1] * ( ( y + 0.5 ) / (float) minimap.height );
277 for ( x = 0; x < minimap.width; ++x )
279 float xmin = minimap.mins[0] + minimap.size[0] * ( ( x + 0.5 ) / (float) minimap.width );
280 *p++ = MiniMapSample( xmin, ymin ) / minimap.size[2];
284 static void MiniMapSharpen( int y ){
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];
291 for ( x = 0; x < minimap.width; ++x )
293 qboolean left = ( x > 0 );
294 qboolean right = ( x < minimap.width - 1 );
295 float val = p[0] * minimap.sharpen_centermult;
298 val += p[-1 - minimap.width] * minimap.sharpen_boxmult;
300 if ( left && down ) {
301 val += p[-1 + minimap.width] * minimap.sharpen_boxmult;
304 val += p[+1 - minimap.width] * minimap.sharpen_boxmult;
306 if ( right && down ) {
307 val += p[+1 + minimap.width] * minimap.sharpen_boxmult;
311 val += p[-1] * minimap.sharpen_boxmult;
314 val += p[+1] * minimap.sharpen_boxmult;
317 val += p[-minimap.width] * minimap.sharpen_boxmult;
320 val += p[+minimap.width] * minimap.sharpen_boxmult;
328 static void MiniMapContrastBoost( int y ){
330 float *q = &minimap.data1f[y * minimap.width];
331 for ( x = 0; x < minimap.width; ++x )
333 *q = *q * minimap.boost / ( ( minimap.boost - 1 ) * *q + 1 );
338 static void MiniMapBrightnessContrast( int y ){
340 float *q = &minimap.data1f[y * minimap.width];
341 for ( x = 0; x < minimap.width; ++x )
343 *q = *q * minimap.contrast + minimap.brightness;
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 );
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] );
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;
364 mins[1] -= ( extend[0] - extend[1] ) * 0.5;
365 maxs[1] += ( extend[0] - extend[1] ) * 0.5;
369 /* border: amount of black area around the image */
370 /* input: border, 1-2*border, border but we need border/(1-2*border) */
372 VectorSubtract( maxs, mins, extend );
373 VectorScale( extend, border / ( 1 - 2 * border ), extend );
375 VectorSubtract( mins, extend, mins );
376 VectorAdd( maxs, extend, maxs );
378 VectorCopy( mins, minimap.mins );
379 VectorSubtract( maxs, mins, minimap.size );
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] );
386 MiniMapSetupBrushes()
387 determines solid non-sky brushes in the world
390 void MiniMapSetupBrushes( void ){
391 SetupBrushesFlags( C_SOLID | C_SKY, C_SOLID, 0, 0 );
392 // at least one must be solid
394 // not all may be nodraw
397 qboolean MiniMapEvaluateSampleOffsets( int *bestj, int *bestk, float *bestval ){
401 *bestj = *bestk = -1;
402 *bestval = 3; /* max possible val is 2 */
404 for ( j = 0; j < minimap.samples; ++j )
405 for ( k = j + 1; k < minimap.samples; ++k )
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];
421 val = dx * dx + dy * dy;
422 if ( val < *bestval ) {
432 void MiniMapMakeSampleOffsets(){
434 float val, valj, valk, sx, sy, rx, ry;
436 Sys_Printf( "Generating good sample offsets (this may take a while)...\n" );
438 /* start with entirely random samples */
439 for ( i = 0; i < minimap.samples; ++i )
441 minimap.sample_offsets[2 * i + 0] = Random();
442 minimap.sample_offsets[2 * i + 1] = Random();
445 for ( i = 0; i < 1000; ++i )
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 ) ) {
455 minimap.sample_offsets[2 * j + 0] = sx;
456 minimap.sample_offsets[2 * j + 1] = sy;
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 ) ) {
465 minimap.sample_offsets[2 * k + 0] = sx;
466 minimap.sample_offsets[2 * k + 1] = sy;
470 /* valj is the greatest */
471 minimap.sample_offsets[2 * j + 0] = rx;
472 minimap.sample_offsets[2 * j + 1] = ry;
477 /* valj is the greater and it is useless - forget it */
483 /* valk is the greatest */
484 minimap.sample_offsets[2 * k + 0] = rx;
485 minimap.sample_offsets[2 * k + 1] = ry;
490 /* valk is the greater and it is useless - forget it */
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] == '\\' ) )
504 while ( relative[0] == '.' && relative[1] == '.' && ( relative[2] == '/' || relative[2] == '\\' ) )
507 while ( endpos != absolute )
510 if ( *endpos == '/' || *endpos == '\\' ) {
514 while ( endpos != absolute && ( endpos[-1] == '/' || endpos[-1] == '\\' ) )
517 memcpy( out, absolute, endpos - absolute );
518 out[endpos - absolute] = '/';
519 strcpy( out + ( endpos - absolute + 1 ), relative );
522 int MiniMapBSPMain( int argc, char **argv ){
523 char minimapFilename[1024];
526 char relativeMinimapFilename[1024];
528 float minimapSharpen;
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" );
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 );
551 LoadBSPFile( source );
553 minimap.model = &bspModels[0];
554 VectorCopy( minimap.model->mins, mins );
555 VectorCopy( minimap.model->maxs, maxs );
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;
566 minimap.sample_offsets = NULL;
568 minimap.brightness = 0.0;
569 minimap.contrast = 1.0;
571 /* process arguments */
572 for ( i = 1; i < ( argc - 1 ); i++ )
574 if ( !strcmp( argv[ i ], "-size" ) ) {
575 minimap.width = minimap.height = atoi( argv[i + 1] );
577 Sys_Printf( "Image size set to %i\n", minimap.width );
579 else if ( !strcmp( argv[ i ], "-sharpen" ) ) {
580 minimapSharpen = atof( argv[i + 1] );
582 Sys_Printf( "Sharpening coefficient set to %f\n", minimapSharpen );
584 else if ( !strcmp( argv[ i ], "-samples" ) ) {
585 minimap.samples = atoi( argv[i + 1] );
587 Sys_Printf( "Samples set to %i\n", minimap.samples );
588 if ( minimap.sample_offsets ) {
589 free( minimap.sample_offsets );
591 minimap.sample_offsets = malloc( 2 * sizeof( *minimap.sample_offsets ) * minimap.samples );
592 MiniMapMakeSampleOffsets();
594 else if ( !strcmp( argv[ i ], "-random" ) ) {
595 minimap.samples = atoi( argv[i + 1] );
597 Sys_Printf( "Random samples set to %i\n", minimap.samples );
598 if ( minimap.sample_offsets ) {
599 free( minimap.sample_offsets );
601 minimap.sample_offsets = NULL;
603 else if ( !strcmp( argv[ i ], "-border" ) ) {
604 border = atof( argv[i + 1] );
606 Sys_Printf( "Border set to %f\n", border );
608 else if ( !strcmp( argv[ i ], "-keepaspect" ) ) {
610 Sys_Printf( "Keeping aspect ratio by letterboxing\n", border );
612 else if ( !strcmp( argv[ i ], "-nokeepaspect" ) ) {
614 Sys_Printf( "Not keeping aspect ratio\n", border );
616 else if ( !strcmp( argv[ i ], "-o" ) ) {
617 strcpy( minimapFilename, argv[i + 1] );
619 Sys_Printf( "Output file name set to %s\n", minimapFilename );
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] );
629 Sys_Printf( "Map mins/maxs overridden\n" );
631 else if ( !strcmp( argv[ i ], "-gray" ) ) {
632 mode = MINIMAP_MODE_GRAY;
633 Sys_Printf( "Writing as white-on-black image\n" );
635 else if ( !strcmp( argv[ i ], "-black" ) ) {
636 mode = MINIMAP_MODE_BLACK;
637 Sys_Printf( "Writing as black alpha image\n" );
639 else if ( !strcmp( argv[ i ], "-white" ) ) {
640 mode = MINIMAP_MODE_WHITE;
641 Sys_Printf( "Writing as white alpha image\n" );
643 else if ( !strcmp( argv[ i ], "-boost" ) && i < ( argc - 2 ) ) {
644 minimap.boost = atof( argv[i + 1] );
646 Sys_Printf( "Contrast boost set to %f\n", minimap.boost );
648 else if ( !strcmp( argv[ i ], "-brightness" ) && i < ( argc - 2 ) ) {
649 minimap.brightness = atof( argv[i + 1] );
651 Sys_Printf( "Brightness set to %f\n", minimap.brightness );
653 else if ( !strcmp( argv[ i ], "-contrast" ) && i < ( argc - 2 ) ) {
654 minimap.contrast = atof( argv[i + 1] );
656 Sys_Printf( "Contrast set to %f\n", minimap.contrast );
658 else if ( !strcmp( argv[ i ], "-autolevel" ) ) {
660 Sys_Printf( "Auto level enabled\n", border );
662 else if ( !strcmp( argv[ i ], "-noautolevel" ) ) {
664 Sys_Printf( "Auto level disabled\n", border );
668 MiniMapMakeMinsMaxs( mins, maxs, border, keepaspect );
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 );
677 ExtractFilePath( minimapFilename, path );
680 if ( minimapSharpen >= 0 ) {
681 minimap.sharpen_centermult = 8 * minimapSharpen + 1;
682 minimap.sharpen_boxmult = -minimapSharpen;
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 ) );
691 MiniMapSetupBrushes();
693 if ( minimap.samples <= 1 ) {
694 Sys_Printf( "\n--- MiniMapNoSupersampling (%d) ---\n", minimap.height );
695 RunThreadsOnIndividual( minimap.height, qtrue, MiniMapNoSupersampling );
699 if ( minimap.sample_offsets ) {
700 Sys_Printf( "\n--- MiniMapSupersampled (%d) ---\n", minimap.height );
701 RunThreadsOnIndividual( minimap.height, qtrue, MiniMapSupersampled );
705 Sys_Printf( "\n--- MiniMapRandomlySupersampled (%d) ---\n", minimap.height );
706 RunThreadsOnIndividual( minimap.height, qtrue, MiniMapRandomlySupersampled );
710 if ( minimap.boost != 1.0 ) {
711 Sys_Printf( "\n--- MiniMapContrastBoost (%d) ---\n", minimap.height );
712 RunThreadsOnIndividual( minimap.height, qtrue, MiniMapContrastBoost );
716 Sys_Printf( "\n--- MiniMapAutoLevel (%d) ---\n", minimap.height );
717 float mi = 1, ma = 0;
722 for ( y = 0; y < minimap.height; ++y )
723 for ( x = 0; x < minimap.width; ++x )
735 o = mi / ( ma - mi );
738 // brightness + contrast * v
740 // brightness + contrast * (v * s - o)
742 // (brightness - contrast * o) + (contrast * s) * v
743 minimap.brightness = minimap.brightness - minimap.contrast * o;
744 minimap.contrast *= s;
746 Sys_Printf( "Auto level: Brightness changed to %f\n", minimap.brightness );
747 Sys_Printf( "Auto level: Contrast changed to %f\n", minimap.contrast );
750 Sys_Printf( "Auto level: failed because all pixels are the same value\n" );
754 if ( minimap.brightness != 0 || minimap.contrast != 1 ) {
755 Sys_Printf( "\n--- MiniMapBrightnessContrast (%d) ---\n", minimap.height );
756 RunThreadsOnIndividual( minimap.height, qtrue, MiniMapBrightnessContrast );
759 if ( minimap.sharpendata1f ) {
760 Sys_Printf( "\n--- MiniMapSharpen (%d) ---\n", minimap.height );
761 RunThreadsOnIndividual( minimap.height, qtrue, MiniMapSharpen );
762 q = minimap.sharpendata1f;
769 Sys_Printf( "\nConverting..." );
773 case MINIMAP_MODE_GRAY:
775 for ( y = 0; y < minimap.height; ++y )
776 for ( x = 0; x < minimap.width; ++x )
783 if ( v > 255.0 / 256.0 ) {
789 Sys_Printf( " writing to %s...", minimapFilename );
790 WriteTGAGray( minimapFilename, data4b, minimap.width, minimap.height );
792 case MINIMAP_MODE_BLACK:
794 for ( y = 0; y < minimap.height; ++y )
795 for ( x = 0; x < minimap.width; ++x )
802 if ( v > 255.0 / 256.0 ) {
811 Sys_Printf( " writing to %s...", minimapFilename );
812 WriteTGA( minimapFilename, data4b, minimap.width, minimap.height );
814 case MINIMAP_MODE_WHITE:
816 for ( y = 0; y < minimap.height; ++y )
817 for ( x = 0; x < minimap.width; ++x )
824 if ( v > 255.0 / 256.0 ) {
833 Sys_Printf( " writing to %s...", minimapFilename );
834 WriteTGA( minimapFilename, data4b, minimap.width, minimap.height );
838 Sys_Printf( " done.\n" );
840 /* return to sender */
850 calculates an md4 checksum for a block of data
853 static int MD4BlockChecksum( void *buffer, int length ){
854 return Com_BlockChecksum( buffer, length );
859 resets an aas checksum to match the given BSP
862 int FixAAS( int argc, char **argv ){
863 int length, checksum;
866 char aas[ 1024 ], **ext;
878 Sys_Printf( "Usage: q3map -fixaas [-v] <mapname>\n" );
882 /* do some path mangling */
883 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
884 StripExtension( source );
885 DefaultExtension( source, ".bsp" );
888 Sys_Printf( "--- FixAAS ---\n" );
891 Sys_Printf( "Loading %s\n", source );
892 length = LoadFile( source, &buffer );
894 /* create bsp checksum */
895 Sys_Printf( "Creating checksum...\n" );
896 checksum = LittleLong( MD4BlockChecksum( buffer, length ) );
898 /* write checksum to aas */
903 strcpy( aas, source );
904 StripExtension( aas );
906 Sys_Printf( "Trying %s\n", aas );
910 file = fopen( aas, "r+b" );
914 if ( fwrite( &checksum, 4, 1, file ) != 1 ) {
915 Error( "Error writing checksum to %s", aas );
920 /* return to sender */
928 analyzes a Quake engine BSP file
931 typedef struct abspHeader_s
936 bspLump_t lumps[ 1 ]; /* unknown size */
940 typedef struct abspLumpTest_s
947 int AnalyzeBSP( int argc, char **argv ){
948 abspHeader_t *header;
949 int size, i, version, offset, length, lumpInt, count;
953 char lumpString[ 1024 ], source[ 1024 ];
954 qboolean lumpSwap = qfalse;
955 abspLumpTest_t *lumpTest;
956 static abspLumpTest_t lumpTests[] =
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)" },
977 Sys_Printf( "Usage: q3map -analyze [-lumpswap] [-v] <mapname>\n" );
981 /* process arguments */
982 for ( i = 1; i < ( argc - 1 ); i++ )
984 /* -format map|ase|... */
985 if ( !strcmp( argv[ i ], "-lumpswap" ) ) {
986 Sys_Printf( "Swapped lump structs enabled\n" );
991 /* clean up map name */
992 strcpy( source, ExpandArg( argv[ i ] ) );
993 Sys_Printf( "Loading %s\n", source );
996 size = LoadFile( source, (void**) &header );
997 if ( size == 0 || header == NULL ) {
998 Sys_Printf( "Unable to load %s.\n", source );
1002 /* analyze ident/version */
1003 memcpy( ident, header->ident, 4 );
1005 version = LittleLong( header->version );
1007 Sys_Printf( "Identity: %s\n", ident );
1008 Sys_Printf( "Version: %d\n", version );
1009 Sys_Printf( "---------------------------------------\n" );
1011 /* analyze each lump */
1012 for ( i = 0; i < 100; i++ )
1014 /* call of duty swapped lump pairs */
1016 offset = LittleLong( header->lumps[ i ].length );
1017 length = LittleLong( header->lumps[ i ].offset );
1020 /* standard lump pairs */
1023 offset = LittleLong( header->lumps[ i ].offset );
1024 length = LittleLong( header->lumps[ i ].length );
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';
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 );
1039 /* only operate on valid lumps */
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 );
1047 /* guess lump type */
1048 if ( lumpString[ 0 ] == '{' && lumpString[ 2 ] == '"' ) {
1049 Sys_Printf( "Type guess: IBSP LUMP_ENTITIES\n" );
1051 else if ( strstr( lumpString, "textures/" ) ) {
1052 Sys_Printf( "Type guess: IBSP LUMP_SHADERS\n" );
1056 /* guess based on size/count */
1057 for ( lumpTest = lumpTests; lumpTest->radix > 0; lumpTest++ )
1059 if ( ( length % lumpTest->radix ) != 0 ) {
1062 count = length / lumpTest->radix;
1063 if ( count < lumpTest->minCount ) {
1066 Sys_Printf( "Type guess: %s (%d x %d)\n", lumpTest->name, count, lumpTest->radix );
1071 Sys_Printf( "---------------------------------------\n" );
1074 if ( offset + length >= size ) {
1080 Sys_Printf( "Lump count: %d\n", i + 1 );
1081 Sys_Printf( "File size: %d bytes\n", size );
1083 /* return to caller */
1091 emits statistics about the bsp file
1094 int BSPInfo( int count, char **fileNames ){
1096 char source[ 1024 ], ext[ 64 ];
1103 Sys_Printf( "No files to dump info for.\n" );
1107 /* enable info mode */
1110 /* walk file list */
1111 for ( i = 0; i < count; i++ )
1113 Sys_Printf( "---------------------------------\n" );
1115 /* mangle filename and get size */
1116 strcpy( source, fileNames[ i ] );
1117 ExtractFileExtension( source, ext );
1118 if ( !Q_stricmp( ext, "map" ) ) {
1119 StripExtension( source );
1121 DefaultExtension( source, ".bsp" );
1122 f = fopen( source, "rb" );
1124 size = Q_filelength( f );
1131 /* load the bsp file and print lump sizes */
1132 Sys_Printf( "%s\n", source );
1133 LoadBSPFile( source );
1134 PrintBSPFileSizes();
1138 Sys_Printf( " total %9d\n", size );
1139 Sys_Printf( " %9d KB\n", size / 1024 );
1140 Sys_Printf( " %9d MB\n", size / ( 1024 * 1024 ) );
1142 Sys_Printf( "---------------------------------\n" );
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;
1157 VectorSubtract( bxyz, axyz, dab );
1158 VectorSubtract( cxyz, axyz, dac );
1159 CrossProduct( dab, dac, norm );
1163 // s(v + norm) = s(v) when n ortho xyz
1165 // s(v) = DotProduct(v, scoeffs) + scoeffs[3]
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;
1190 md = m4_det( solvematrix );
1191 if ( md * md < 1e-10 ) {
1192 Sys_Printf( "Cannot invert some matrix, some texcoords aren't extrapolated!" );
1196 m4x4_invert( solvematrix );
1198 scoeffs[0] = ast[0];
1199 scoeffs[1] = bst[0];
1200 scoeffs[2] = cst[0];
1202 m4x4_transform_vec4( solvematrix, scoeffs );
1203 tcoeffs[0] = ast[1];
1204 tcoeffs[1] = bst[1];
1205 tcoeffs[2] = cst[1];
1207 m4x4_transform_vec4( solvematrix, tcoeffs );
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];
1219 amaze and confuse your enemies with wierd scaled maps!
1222 int ScaleBSPMain( int argc, char **argv ){
1230 float *old_xyzst = NULL;
1231 float spawn_ref = 0;
1236 Sys_Printf( "Usage: q3map [-v] -scale [-tex] [-spawn_ref <value>] <value> <mapname>\n" );
1241 for ( i = 1; i < argc - 2; ++i )
1243 if ( !strcmp( argv[i], "-tex" ) ) {
1246 else if ( !strcmp( argv[i], "-spawn_ref" ) ) {
1247 spawn_ref = atof( argv[i + 1] );
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 ] );
1261 if ( argc - 4 >= i ) {
1262 scale[0] = atof( argv[ argc - 4 ] );
1265 uniform = ( ( scale[0] == scale[1] ) && ( scale[1] == scale[2] ) );
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" );
1273 /* do some path mangling */
1274 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
1275 StripExtension( source );
1276 DefaultExtension( source, ".bsp" );
1279 Sys_Printf( "Loading %s\n", source );
1280 LoadBSPFile( source );
1284 Sys_Printf( "--- ScaleBSP ---\n" );
1285 Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
1287 /* scale entity keys */
1288 for ( i = 0; i < numBSPEntities && i < numEntities; i++ )
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;
1299 if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
1300 vec[2] -= spawn_ref;
1302 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1303 SetKeyValue( &entities[ i ], "origin", str );
1306 a = FloatForKey( &entities[ i ], "angle" );
1307 if ( a == -1 || a == -2 ) { // z scale
1310 else if ( fabs( sin( DEG2RAD( a ) ) ) < 0.707 ) {
1317 /* scale door lip */
1318 f = FloatForKey( &entities[ i ], "lip" );
1321 sprintf( str, "%f", f );
1322 SetKeyValue( &entities[ i ], "lip", str );
1325 /* scale plat height */
1326 f = FloatForKey( &entities[ i ], "height" );
1329 sprintf( str, "%f", f );
1330 SetKeyValue( &entities[ i ], "height", str );
1333 // TODO maybe allow a definition file for entities to specify which values are scaled how?
1337 for ( i = 0; i < numBSPModels; i++ )
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];
1348 for ( i = 0; i < numBSPNodes; i++ )
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];
1359 for ( i = 0; i < numBSPLeafs; i++ )
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];
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++ )
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];
1382 /* scale drawverts */
1383 for ( i = 0; i < numBSPDrawVerts; i++ )
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 );
1395 for ( i = 0; i < numBSPDrawSurfaces; i++ )
1397 switch ( bspDrawSurfaces[i].surfaceType )
1401 if ( bspDrawSurfaces[i].numIndexes % 3 ) {
1402 Error( "Not a triangulation!" );
1404 for ( j = bspDrawSurfaces[i].firstIndex; j < bspDrawSurfaces[i].firstIndex + bspDrawSurfaces[i].numIndexes; j += 3 )
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];
1413 ExtrapolateTexcoords(
1428 for ( i = 0; i < numBSPPlanes; i++ )
1430 bspPlanes[ i ].dist *= scale[0];
1435 for ( i = 0; i < numBSPPlanes; i++ )
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;
1446 /* scale gridsize */
1447 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
1448 if ( ( vec[ 0 ] + vec[ 1 ] + vec[ 2 ] ) == 0.0f ) {
1449 VectorCopy( gridSize, vec );
1454 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1455 SetKeyValue( &entities[ 0 ], "gridsize", str );
1457 /* inject command line parameters */
1458 InjectCommandLine( argv, 0, argc - 1 );
1462 StripExtension( source );
1463 DefaultExtension( source, "_s.bsp" );
1464 Sys_Printf( "Writing %s\n", source );
1465 WriteBSPFile( source );
1467 /* return to sender */
1474 shifts a map: works correctly only with axial faces, placed in positive half of axis
1475 for testing physics with huge coordinates
1478 int ShiftBSPMain( int argc, char **argv ){
1486 float *old_xyzst = NULL;
1487 float spawn_ref = 0;
1492 Sys_Printf( "Usage: q3map [-v] -shift [-tex] [-spawn_ref <value>] <value> <mapname>\n" );
1497 for ( i = 1; i < argc - 2; ++i )
1499 if ( !strcmp( argv[i], "-tex" ) ) {
1502 else if ( !strcmp( argv[i], "-spawn_ref" ) ) {
1503 spawn_ref = atof( argv[i + 1] );
1512 // if(argc-2 >= i) // always true
1513 scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
1514 if ( argc - 3 >= i ) {
1515 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
1517 if ( argc - 4 >= i ) {
1518 scale[0] = atof( argv[ argc - 4 ] );
1521 uniform = ( ( scale[0] == scale[1] ) && ( scale[1] == scale[2] ) );
1524 /* do some path mangling */
1525 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
1526 StripExtension( source );
1527 DefaultExtension( source, ".bsp" );
1530 Sys_Printf( "Loading %s\n", source );
1531 LoadBSPFile( source );
1535 Sys_Printf( "--- ShiftBSP ---\n" );
1536 Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
1538 /* shift entity keys */
1539 for ( i = 0; i < numBSPEntities && i < numEntities; i++ )
1542 GetVectorForKey( &entities[ i ], "origin", vec );
1543 if ( ( vec[ 0 ] || vec[ 1 ] || vec[ 2 ] ) ) {
1544 if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
1545 vec[2] += spawn_ref;
1550 if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
1551 vec[2] -= spawn_ref;
1553 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1554 SetKeyValue( &entities[ i ], "origin", str );
1560 for ( i = 0; i < numBSPModels; i++ )
1562 bspModels[ i ].mins[0] += scale[0];
1563 bspModels[ i ].mins[1] += scale[1];
1564 bspModels[ i ].mins[2] += scale[2];
1565 bspModels[ i ].maxs[0] += scale[0];
1566 bspModels[ i ].maxs[1] += scale[1];
1567 bspModels[ i ].maxs[2] += scale[2];
1571 for ( i = 0; i < numBSPNodes; i++ )
1573 bspNodes[ i ].mins[0] += scale[0];
1574 bspNodes[ i ].mins[1] += scale[1];
1575 bspNodes[ i ].mins[2] += scale[2];
1576 bspNodes[ i ].maxs[0] += scale[0];
1577 bspNodes[ i ].maxs[1] += scale[1];
1578 bspNodes[ i ].maxs[2] += scale[2];
1582 for ( i = 0; i < numBSPLeafs; i++ )
1584 bspLeafs[ i ].mins[0] += scale[0];
1585 bspLeafs[ i ].mins[1] += scale[1];
1586 bspLeafs[ i ].mins[2] += scale[2];
1587 bspLeafs[ i ].maxs[0] += scale[0];
1588 bspLeafs[ i ].maxs[1] += scale[1];
1589 bspLeafs[ i ].maxs[2] += scale[2];
1592 /* shift drawverts */
1593 for ( i = 0; i < numBSPDrawVerts; i++ )
1595 bspDrawVerts[i].xyz[0] += scale[0];
1596 bspDrawVerts[i].xyz[1] += scale[1];
1597 bspDrawVerts[i].xyz[2] += scale[2];
1604 for ( i = 0; i < numBSPPlanes; i++ )
1606 //find point on plane
1607 for ( j=0; j<3; j++ ){
1608 if ( fabs( bspPlanes[ i ].normal[j] ) > 0.5 ){
1609 point[j] = bspPlanes[ i ].dist / bspPlanes[ i ].normal[j];
1610 point[(j+1)%3] = point[(j+2)%3] = 0;
1615 for ( j=0; j<3; j++ ){
1616 point[j] += scale[j];
1618 //calc new plane dist
1619 bspPlanes[ i ].dist = DotProduct( point, bspPlanes[ i ].normal );
1622 /* scale gridsize */
1624 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
1625 if ( ( vec[ 0 ] + vec[ 1 ] + vec[ 2 ] ) == 0.0f ) {
1626 VectorCopy( gridSize, vec );
1631 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1632 SetKeyValue( &entities[ 0 ], "gridsize", str );
1634 /* inject command line parameters */
1635 InjectCommandLine( argv, 0, argc - 1 );
1639 StripExtension( source );
1640 DefaultExtension( source, "_sh.bsp" );
1641 Sys_Printf( "Writing %s\n", source );
1642 WriteBSPFile( source );
1644 /* return to sender */
1652 a stripped down ProcessModels
1654 void PseudoCompileBSP( qboolean need_tree ){
1656 char modelValue[10];
1665 SetDrawSurfacesBuffer();
1666 mapDrawSurfs = safe_malloc( sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
1667 memset( mapDrawSurfs, 0, sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
1668 numMapDrawSurfs = 0;
1672 for ( mapEntityNum = 0; mapEntityNum < numEntities; mapEntityNum++ )
1675 entity = &entities[ mapEntityNum ];
1676 if ( entity->brushes == NULL && entity->patches == NULL ) {
1680 if ( mapEntityNum != 0 ) {
1681 sprintf( modelValue, "*%d", models++ );
1682 SetKeyValue( entity, "model", modelValue );
1685 /* process the model */
1686 Sys_FPrintf( SYS_VRB, "############### model %i ###############\n", numBSPModels );
1689 entity->firstDrawSurf = numMapDrawSurfs;
1691 ClearMetaTriangles();
1692 PatchMapDrawSurfs( entity );
1694 if ( mapEntityNum == 0 && need_tree ) {
1695 faces = MakeStructuralBSPFaceList( entities[0].brushes );
1696 tree = FaceBSP( faces );
1697 node = tree->headnode;
1702 node->planenum = PLANENUM_LEAF;
1704 tree->headnode = node;
1707 /* a minimized ClipSidesIntoTree */
1708 for ( brush = entity->brushes; brush; brush = brush->next )
1710 /* walk the brush sides */
1711 for ( i = 0; i < brush->numsides; i++ )
1714 side = &brush->sides[ i ];
1715 if ( side->winding == NULL ) {
1719 if ( side->shaderInfo == NULL ) {
1722 /* save this winding as a visible surface */
1723 DrawSurfaceForSide( entity, brush, side, side->winding );
1728 ClassifyEntitySurfaces( entity );
1729 MakeEntityDecals( entity );
1730 MakeEntityMetaTriangles( entity );
1731 SmoothMetaTriangles();
1732 MergeMetaTriangles();
1734 FilterDrawsurfsIntoTree( entity, tree );
1736 FilterStructuralBrushesIntoTree( entity, tree );
1737 FilterDetailBrushesIntoTree( entity, tree );
1739 EmitBrushes( entity->brushes, &entity->firstBrush, &entity->numBrushes );
1740 EndModel( entity, node );
1742 EndBSPFile( qfalse );
1747 main argument processing function for bsp conversion
1750 int ConvertBSPMain( int argc, char **argv ){
1752 int ( *convertFunc )( char * );
1753 game_t *convertGame;
1755 qboolean map_allowed, force_bsp, force_map;
1759 convertFunc = ConvertBSPToASE;
1761 map_allowed = qfalse;
1767 Sys_Printf( "Usage: q3map -convert [-format <ase|obj|map_bp|map>] [-shadersasbitmap|-lightmapsastexcoord|-deluxemapsastexcoord] [-readbsp|-readmap [-meta|-patchmeta]] [-v] <mapname>\n" );
1771 /* process arguments */
1772 for ( i = 1; i < ( argc - 1 ); i++ )
1774 /* -format map|ase|... */
1775 if ( !strcmp( argv[ i ], "-format" ) ) {
1777 if ( !Q_stricmp( argv[ i ], "ase" ) ) {
1778 convertFunc = ConvertBSPToASE;
1779 map_allowed = qfalse;
1781 else if ( !Q_stricmp( argv[ i ], "obj" ) ) {
1782 convertFunc = ConvertBSPToOBJ;
1783 map_allowed = qfalse;
1785 else if ( !Q_stricmp( argv[ i ], "map_bp" ) ) {
1786 convertFunc = ConvertBSPToMap_BP;
1787 map_allowed = qtrue;
1789 else if ( !Q_stricmp( argv[ i ], "map" ) ) {
1790 convertFunc = ConvertBSPToMap;
1791 map_allowed = qtrue;
1795 convertGame = GetGame( argv[ i ] );
1796 map_allowed = qfalse;
1797 if ( convertGame == NULL ) {
1798 Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
1802 else if ( !strcmp( argv[ i ], "-ne" ) ) {
1803 normalEpsilon = atof( argv[ i + 1 ] );
1805 Sys_Printf( "Normal epsilon set to %f\n", normalEpsilon );
1807 else if ( !strcmp( argv[ i ], "-de" ) ) {
1808 distanceEpsilon = atof( argv[ i + 1 ] );
1810 Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
1812 else if ( !strcmp( argv[ i ], "-shaderasbitmap" ) || !strcmp( argv[ i ], "-shadersasbitmap" ) ) {
1813 shadersAsBitmap = qtrue;
1815 else if ( !strcmp( argv[ i ], "-lightmapastexcoord" ) || !strcmp( argv[ i ], "-lightmapsastexcoord" ) ) {
1816 lightmapsAsTexcoord = qtrue;
1818 else if ( !strcmp( argv[ i ], "-deluxemapastexcoord" ) || !strcmp( argv[ i ], "-deluxemapsastexcoord" ) ) {
1819 lightmapsAsTexcoord = qtrue;
1822 else if ( !strcmp( argv[ i ], "-readbsp" ) ) {
1825 else if ( !strcmp( argv[ i ], "-readmap" ) ) {
1828 else if ( !strcmp( argv[ i ], "-meta" ) ) {
1831 else if ( !strcmp( argv[ i ], "-patchmeta" ) ) {
1839 /* clean up map name */
1840 strcpy( source, ExpandArg( argv[i] ) );
1841 ExtractFileExtension( source, ext );
1843 if ( !map_allowed && !force_map ) {
1847 if ( force_map || ( !force_bsp && !Q_stricmp( ext, "map" ) && map_allowed ) ) {
1848 if ( !map_allowed ) {
1849 Sys_Printf( "WARNING: the requested conversion should not be done from .map files. Compile a .bsp first.\n" );
1851 StripExtension( source );
1852 DefaultExtension( source, ".map" );
1853 Sys_Printf( "Loading %s\n", source );
1854 LoadMapFile( source, qfalse, convertGame == NULL );
1855 PseudoCompileBSP( convertGame != NULL );
1859 StripExtension( source );
1860 DefaultExtension( source, ".bsp" );
1861 Sys_Printf( "Loading %s\n", source );
1862 LoadBSPFile( source );
1866 /* bsp format convert? */
1867 if ( convertGame != NULL ) {
1868 /* set global game */
1872 StripExtension( source );
1873 DefaultExtension( source, "_c.bsp" );
1874 Sys_Printf( "Writing %s\n", source );
1875 WriteBSPFile( source );
1877 /* return to sender */
1881 /* normal convert */
1882 return convertFunc( source );
1892 int main( int argc, char **argv ){
1897 /* we want consistent 'randomness' */
1901 start = I_FloatTime();
1903 /* this was changed to emit version number over the network */
1904 printf( Q3MAP_VERSION "\n" );
1907 atexit( ExitQ3Map );
1909 /* read general options first */
1910 for ( i = 1; i < argc; i++ )
1913 if ( !strcmp( argv[ i ], "-connect" ) ) {
1916 Broadcast_Setup( argv[ i ] );
1921 else if ( !strcmp( argv[ i ], "-v" ) ) {
1929 else if ( !strcmp( argv[ i ], "-force" ) ) {
1934 /* patch subdivisions */
1935 else if ( !strcmp( argv[ i ], "-subdivisions" ) ) {
1938 patchSubdivisions = atoi( argv[ i ] );
1940 if ( patchSubdivisions <= 0 ) {
1941 patchSubdivisions = 1;
1946 else if ( !strcmp( argv[ i ], "-threads" ) ) {
1949 numthreads = atoi( argv[ i ] );
1952 else if( !strcmp( argv[ i ], "-nocmdline" ) )
1954 Sys_Printf( "noCmdLine\n" );
1961 /* init model library */
1963 PicoSetMallocFunc( safe_malloc );
1964 PicoSetFreeFunc( free );
1965 PicoSetPrintFunc( PicoPrintFunc );
1966 PicoSetLoadFileFunc( PicoLoadFileFunc );
1967 PicoSetFreeFileFunc( free );
1969 /* set number of threads */
1972 /* generate sinusoid jitter table */
1973 for ( i = 0; i < MAX_JITTERS; i++ )
1975 jitters[ i ] = sin( i * 139.54152147 );
1976 //% Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
1979 /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
1980 and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
1982 Sys_Printf( "Q3Map - v1.0r (c) 1999 Id Software Inc.\n" );
1983 Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
1984 Sys_Printf( "NetRadiant - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
1985 Sys_Printf( "%s\n", Q3MAP_MOTD );
1987 /* ydnar: new path initialization */
1988 InitPaths( &argc, argv );
1990 /* set game options */
1991 if ( !patchSubdivisions ) {
1992 patchSubdivisions = game->patchSubdivisions;
1995 /* check if we have enough options left to attempt something */
1997 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
2001 if ( !strcmp( argv[ 1 ], "-fixaas" ) ) {
2002 r = FixAAS( argc - 1, argv + 1 );
2006 else if ( !strcmp( argv[ 1 ], "-analyze" ) ) {
2007 r = AnalyzeBSP( argc - 1, argv + 1 );
2011 else if ( !strcmp( argv[ 1 ], "-info" ) ) {
2012 r = BSPInfo( argc - 2, argv + 2 );
2016 else if ( !strcmp( argv[ 1 ], "-vis" ) ) {
2017 r = VisMain( argc - 1, argv + 1 );
2021 else if ( !strcmp( argv[ 1 ], "-light" ) ) {
2022 r = LightMain( argc - 1, argv + 1 );
2026 else if ( !strcmp( argv[ 1 ], "-vlight" ) ) {
2027 Sys_Printf( "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
2028 argv[ 1 ] = "-fast"; /* eek a hack */
2029 r = LightMain( argc, argv );
2032 /* ydnar: lightmap export */
2033 else if ( !strcmp( argv[ 1 ], "-export" ) ) {
2034 r = ExportLightmapsMain( argc - 1, argv + 1 );
2037 /* ydnar: lightmap import */
2038 else if ( !strcmp( argv[ 1 ], "-import" ) ) {
2039 r = ImportLightmapsMain( argc - 1, argv + 1 );
2042 /* ydnar: bsp scaling */
2043 else if ( !strcmp( argv[ 1 ], "-scale" ) ) {
2044 r = ScaleBSPMain( argc - 1, argv + 1 );
2048 else if ( !strcmp( argv[ 1 ], "-shift" ) ) {
2049 r = ShiftBSPMain( argc - 1, argv + 1 );
2052 /* ydnar: bsp conversion */
2053 else if ( !strcmp( argv[ 1 ], "-convert" ) ) {
2054 r = ConvertBSPMain( argc - 1, argv + 1 );
2058 else if ( !strcmp( argv[ 1 ], "-minimap" ) ) {
2059 r = MiniMapBSPMain( argc - 1, argv + 1 );
2062 /* ydnar: otherwise create a bsp */
2064 r = BSPMain( argc, argv );
2068 end = I_FloatTime();
2069 Sys_Printf( "%9.0f seconds elapsed\n", end - start );
2071 /* shut down connection */
2072 Broadcast_Shutdown();
2074 /* return any error code */