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
48 return (vec_t) rand() / RAND_MAX;
58 static void ExitQ3Map( void )
61 if( mapDrawSurfs != NULL )
69 calculates an md4 checksum for a block of data
72 static int MD4BlockChecksum( void *buffer, int length )
75 int digest[ 4 ], checksum;
79 mh = mhash_init( MHASH_MD4 );
81 Error( "Unable to initialize MD4 hash context" );
82 mhash( mh, buffer, length );
83 mhash_deinit( mh, digest );
85 /* xor the bits and return */
86 checksum = digest[ 0 ] ^ digest[ 1 ] ^ digest[ 2 ] ^ digest[ 3 ];
94 resets an aas checksum to match the given BSP
97 int FixAAS( int argc, char **argv )
102 char aas[ 1024 ], **ext;
115 Sys_Printf( "Usage: q3map -fixaas [-v] <mapname>\n" );
119 /* do some path mangling */
120 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
121 StripExtension( source );
122 DefaultExtension( source, ".bsp" );
125 Sys_Printf( "--- FixAAS ---\n" );
128 Sys_Printf( "Loading %s\n", source );
129 length = LoadFile( source, &buffer );
131 /* create bsp checksum */
132 Sys_Printf( "Creating checksum...\n" );
133 checksum = LittleLong( MD4BlockChecksum( buffer, length ) );
135 /* write checksum to aas */
140 strcpy( aas, source );
141 StripExtension( aas );
143 Sys_Printf( "Trying %s\n", aas );
147 file = fopen( aas, "r+b" );
150 if( fwrite( &checksum, 4, 1, file ) != 1 )
151 Error( "Error writing checksum to %s", aas );
155 /* return to sender */
163 analyzes a Quake engine BSP file
166 typedef struct abspHeader_s
171 bspLump_t lumps[ 1 ]; /* unknown size */
175 typedef struct abspLumpTest_s
182 int AnalyzeBSP( int argc, char **argv )
184 abspHeader_t *header;
185 int size, i, version, offset, length, lumpInt, count;
189 char lumpString[ 1024 ], source[ 1024 ];
190 qboolean lumpSwap = qfalse;
191 abspLumpTest_t *lumpTest;
192 static abspLumpTest_t lumpTests[] =
194 { sizeof( bspPlane_t ), 6, "IBSP LUMP_PLANES" },
195 { sizeof( bspBrush_t ), 1, "IBSP LUMP_BRUSHES" },
196 { 8, 6, "IBSP LUMP_BRUSHSIDES" },
197 { sizeof( bspBrushSide_t ), 6, "RBSP LUMP_BRUSHSIDES" },
198 { sizeof( bspModel_t ), 1, "IBSP LUMP_MODELS" },
199 { sizeof( bspNode_t ), 2, "IBSP LUMP_NODES" },
200 { sizeof( bspLeaf_t ), 1, "IBSP LUMP_LEAFS" },
201 { 104, 3, "IBSP LUMP_DRAWSURFS" },
202 { 44, 3, "IBSP LUMP_DRAWVERTS" },
203 { 4, 6, "IBSP LUMP_DRAWINDEXES" },
204 { 128 * 128 * 3, 1, "IBSP LUMP_LIGHTMAPS" },
205 { 256 * 256 * 3, 1, "IBSP LUMP_LIGHTMAPS (256 x 256)" },
206 { 512 * 512 * 3, 1, "IBSP LUMP_LIGHTMAPS (512 x 512)" },
214 Sys_Printf( "Usage: q3map -analyze [-lumpswap] [-v] <mapname>\n" );
218 /* process arguments */
219 for( i = 1; i < (argc - 1); i++ )
221 /* -format map|ase|... */
222 if( !strcmp( argv[ i ], "-lumpswap" ) )
224 Sys_Printf( "Swapped lump structs enabled\n" );
229 /* clean up map name */
230 strcpy( source, ExpandArg( argv[ i ] ) );
231 Sys_Printf( "Loading %s\n", source );
234 size = LoadFile( source, (void**) &header );
235 if( size == 0 || header == NULL )
237 Sys_Printf( "Unable to load %s.\n", source );
241 /* analyze ident/version */
242 memcpy( ident, header->ident, 4 );
244 version = LittleLong( header->version );
246 Sys_Printf( "Identity: %s\n", ident );
247 Sys_Printf( "Version: %d\n", version );
248 Sys_Printf( "---------------------------------------\n" );
250 /* analyze each lump */
251 for( i = 0; i < 100; i++ )
253 /* call of duty swapped lump pairs */
256 offset = LittleLong( header->lumps[ i ].length );
257 length = LittleLong( header->lumps[ i ].offset );
260 /* standard lump pairs */
263 offset = LittleLong( header->lumps[ i ].offset );
264 length = LittleLong( header->lumps[ i ].length );
268 lump = (byte*) header + offset;
269 lumpInt = LittleLong( (int) *((int*) lump) );
270 lumpFloat = LittleFloat( (float) *((float*) lump) );
271 memcpy( lumpString, (char*) lump, (length < 1024 ? length : 1024) );
272 lumpString[ 1024 ] = '\0';
274 /* print basic lump info */
275 Sys_Printf( "Lump: %d\n", i );
276 Sys_Printf( "Offset: %d bytes\n", offset );
277 Sys_Printf( "Length: %d bytes\n", length );
279 /* only operate on valid lumps */
282 /* print data in 4 formats */
283 Sys_Printf( "As hex: %08X\n", lumpInt );
284 Sys_Printf( "As int: %d\n", lumpInt );
285 Sys_Printf( "As float: %f\n", lumpFloat );
286 Sys_Printf( "As string: %s\n", lumpString );
288 /* guess lump type */
289 if( lumpString[ 0 ] == '{' && lumpString[ 2 ] == '"' )
290 Sys_Printf( "Type guess: IBSP LUMP_ENTITIES\n" );
291 else if( strstr( lumpString, "textures/" ) )
292 Sys_Printf( "Type guess: IBSP LUMP_SHADERS\n" );
295 /* guess based on size/count */
296 for( lumpTest = lumpTests; lumpTest->radix > 0; lumpTest++ )
298 if( (length % lumpTest->radix) != 0 )
300 count = length / lumpTest->radix;
301 if( count < lumpTest->minCount )
303 Sys_Printf( "Type guess: %s (%d x %d)\n", lumpTest->name, count, lumpTest->radix );
308 Sys_Printf( "---------------------------------------\n" );
311 if( offset + length >= size )
316 Sys_Printf( "Lump count: %d\n", i + 1 );
317 Sys_Printf( "File size: %d bytes\n", size );
319 /* return to caller */
327 emits statistics about the bsp file
330 int BSPInfo( int count, char **fileNames )
333 char source[ 1024 ], ext[ 64 ];
341 Sys_Printf( "No files to dump info for.\n");
345 /* enable info mode */
349 for( i = 0; i < count; i++ )
351 Sys_Printf( "---------------------------------\n" );
353 /* mangle filename and get size */
354 strcpy( source, fileNames[ i ] );
355 ExtractFileExtension( source, ext );
356 if( !Q_stricmp( ext, "map" ) )
357 StripExtension( source );
358 DefaultExtension( source, ".bsp" );
359 f = fopen( source, "rb" );
362 size = Q_filelength (f);
368 /* load the bsp file and print lump sizes */
369 Sys_Printf( "%s\n", source );
370 LoadBSPFile( source );
375 Sys_Printf( " total %9d\n", size );
376 Sys_Printf( " %9d KB\n", size / 1024 );
377 Sys_Printf( " %9d MB\n", size / (1024 * 1024) );
379 Sys_Printf( "---------------------------------\n" );
390 amaze and confuse your enemies with wierd scaled maps!
393 int ScaleBSPMain( int argc, char **argv )
404 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
409 scale = atof( argv[ argc - 2 ] );
412 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
413 Sys_Printf( "Non-zero scale value required.\n" );
417 /* do some path mangling */
418 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
419 StripExtension( source );
420 DefaultExtension( source, ".bsp" );
423 Sys_Printf( "Loading %s\n", source );
424 LoadBSPFile( source );
428 Sys_Printf( "--- ScaleBSP ---\n" );
429 Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
431 /* scale entity keys */
432 for( i = 0; i < numBSPEntities && i < numEntities; i++ )
435 GetVectorForKey( &entities[ i ], "origin", vec );
436 if( (vec[ 0 ] + vec[ 1 ] + vec[ 2 ]) )
438 VectorScale( vec, scale, vec );
439 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
440 SetKeyValue( &entities[ i ], "origin", str );
444 f = FloatForKey( &entities[ i ], "lip" );
448 sprintf( str, "%f", f );
449 SetKeyValue( &entities[ i ], "lip", str );
454 for( i = 0; i < numBSPModels; i++ )
456 VectorScale( bspModels[ i ].mins, scale, bspModels[ i ].mins );
457 VectorScale( bspModels[ i ].maxs, scale, bspModels[ i ].maxs );
461 for( i = 0; i < numBSPNodes; i++ )
463 VectorScale( bspNodes[ i ].mins, scale, bspNodes[ i ].mins );
464 VectorScale( bspNodes[ i ].maxs, scale, bspNodes[ i ].maxs );
468 for( i = 0; i < numBSPLeafs; i++ )
470 VectorScale( bspLeafs[ i ].mins, scale, bspLeafs[ i ].mins );
471 VectorScale( bspLeafs[ i ].maxs, scale, bspLeafs[ i ].maxs );
474 /* scale drawverts */
475 for( i = 0; i < numBSPDrawVerts; i++ )
476 VectorScale( bspDrawVerts[ i ].xyz, scale, bspDrawVerts[ i ].xyz );
479 for( i = 0; i < numBSPPlanes; i++ )
480 bspPlanes[ i ].dist *= scale;
483 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
484 if( (vec[ 0 ] + vec[ 1 ] + vec[ 2 ]) == 0.0f )
485 VectorCopy( gridSize, vec );
486 VectorScale( vec, scale, vec );
487 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
488 SetKeyValue( &entities[ 0 ], "gridsize", str );
492 StripExtension( source );
493 DefaultExtension( source, "_s.bsp" );
494 Sys_Printf( "Writing %s\n", source );
495 WriteBSPFile( source );
497 /* return to sender */
505 main argument processing function for bsp conversion
508 int ConvertBSPMain( int argc, char **argv )
511 int (*convertFunc)( char * );
516 convertFunc = ConvertBSPToASE;
522 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
526 /* process arguments */
527 for( i = 1; i < (argc - 1); i++ )
529 /* -format map|ase|... */
530 if( !strcmp( argv[ i ], "-format" ) )
533 if( !Q_stricmp( argv[ i ], "ase" ) )
534 convertFunc = ConvertBSPToASE;
535 else if( !Q_stricmp( argv[ i ], "map" ) )
536 convertFunc = ConvertBSPToMap;
539 convertGame = GetGame( argv[ i ] );
540 if( convertGame == NULL )
541 Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
544 else if( !strcmp( argv[ i ], "-ne" ) )
546 normalEpsilon = atof( argv[ i + 1 ] );
548 Sys_Printf( "Normal epsilon set to %f\n", normalEpsilon );
550 else if( !strcmp( argv[ i ], "-de" ) )
552 distanceEpsilon = atof( argv[ i + 1 ] );
554 Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
558 /* clean up map name */
559 strcpy( source, ExpandArg( argv[ i ] ) );
560 StripExtension( source );
561 DefaultExtension( source, ".bsp" );
565 Sys_Printf( "Loading %s\n", source );
567 /* ydnar: load surface file */
568 //% LoadSurfaceExtraFile( source );
570 LoadBSPFile( source );
572 /* parse bsp entities */
575 /* bsp format convert? */
576 if( convertGame != NULL )
578 /* set global game */
582 StripExtension( source );
583 DefaultExtension( source, "_c.bsp" );
584 Sys_Printf( "Writing %s\n", source );
585 WriteBSPFile( source );
587 /* return to sender */
592 return convertFunc( source );
602 int main( int argc, char **argv )
608 /* we want consistent 'randomness' */
612 start = I_FloatTime();
614 /* this was changed to emit version number over the network */
615 printf( Q3MAP_VERSION "\n" );
620 /* read general options first */
621 for( i = 1; i < argc; i++ )
624 if( !strcmp( argv[ i ], "-connect" ) )
628 Broadcast_Setup( argv[ i ] );
633 else if( !strcmp( argv[ i ], "-v" ) )
640 else if( !strcmp( argv[ i ], "-force" ) )
646 /* patch subdivisions */
647 else if( !strcmp( argv[ i ], "-subdivisions" ) )
651 patchSubdivisions = atoi( argv[ i ] );
653 if( patchSubdivisions <= 0 )
654 patchSubdivisions = 1;
658 else if( !strcmp( argv[ i ], "-threads" ) )
662 numthreads = atoi( argv[ i ] );
667 /* init model library */
669 PicoSetMallocFunc( safe_malloc );
670 PicoSetFreeFunc( free );
671 PicoSetPrintFunc( PicoPrintFunc );
672 PicoSetLoadFileFunc( PicoLoadFileFunc );
673 PicoSetFreeFileFunc( free );
675 /* set number of threads */
678 /* generate sinusoid jitter table */
679 for( i = 0; i < MAX_JITTERS; i++ )
681 jitters[ i ] = sin( i * 139.54152147 );
682 //% Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
685 /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
686 and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
688 Sys_Printf( "Q3Map - v1.0r (c) 1999 Id Software Inc.\n" );
689 Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
690 Sys_Printf( "GtkRadiant - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
691 Sys_Printf( "%s\n", Q3MAP_MOTD );
693 /* ydnar: new path initialization */
694 InitPaths( &argc, argv );
696 /* check if we have enough options left to attempt something */
698 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
701 if( !strcmp( argv[ 1 ], "-fixaas" ) )
702 r = FixAAS( argc - 1, argv + 1 );
705 else if( !strcmp( argv[ 1 ], "-analyze" ) )
706 r = AnalyzeBSP( argc - 1, argv + 1 );
709 else if( !strcmp( argv[ 1 ], "-info" ) )
710 r = BSPInfo( argc - 2, argv + 2 );
713 else if( !strcmp( argv[ 1 ], "-vis" ) )
714 r = VisMain( argc - 1, argv + 1 );
717 else if( !strcmp( argv[ 1 ], "-light" ) )
718 r = LightMain( argc - 1, argv + 1 );
721 else if( !strcmp( argv[ 1 ], "-vlight" ) )
723 Sys_Printf( "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
724 argv[ 1 ] = "-fast"; /* eek a hack */
725 r = LightMain( argc, argv );
728 /* ydnar: lightmap export */
729 else if( !strcmp( argv[ 1 ], "-export" ) )
730 r = ExportLightmapsMain( argc - 1, argv + 1 );
732 /* ydnar: lightmap import */
733 else if( !strcmp( argv[ 1 ], "-import" ) )
734 r = ImportLightmapsMain( argc - 1, argv + 1 );
736 /* ydnar: bsp scaling */
737 else if( !strcmp( argv[ 1 ], "-scale" ) )
738 r = ScaleBSPMain( argc - 1, argv + 1 );
740 /* ydnar: bsp conversion */
741 else if( !strcmp( argv[ 1 ], "-convert" ) )
742 r = ConvertBSPMain( argc - 1, argv + 1 );
744 /* ydnar: otherwise create a bsp */
746 r = BSPMain( argc, argv );
750 Sys_Printf( "%9.0f seconds elapsed\n", end - start );
752 /* shut down connection */
753 Broadcast_Shutdown();
755 /* return any error code */