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 )
74 return Com_BlockChecksum(buffer, length);
81 resets an aas checksum to match the given BSP
84 int FixAAS( int argc, char **argv )
89 char aas[ 1024 ], **ext;
102 Sys_Printf( "Usage: q3map -fixaas [-v] <mapname>\n" );
106 /* do some path mangling */
107 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
108 StripExtension( source );
109 DefaultExtension( source, ".bsp" );
112 Sys_Printf( "--- FixAAS ---\n" );
115 Sys_Printf( "Loading %s\n", source );
116 length = LoadFile( source, &buffer );
118 /* create bsp checksum */
119 Sys_Printf( "Creating checksum...\n" );
120 checksum = LittleLong( MD4BlockChecksum( buffer, length ) );
122 /* write checksum to aas */
127 strcpy( aas, source );
128 StripExtension( aas );
130 Sys_Printf( "Trying %s\n", aas );
134 file = fopen( aas, "r+b" );
137 if( fwrite( &checksum, 4, 1, file ) != 1 )
138 Error( "Error writing checksum to %s", aas );
142 /* return to sender */
150 analyzes a Quake engine BSP file
153 typedef struct abspHeader_s
158 bspLump_t lumps[ 1 ]; /* unknown size */
162 typedef struct abspLumpTest_s
169 int AnalyzeBSP( int argc, char **argv )
171 abspHeader_t *header;
172 int size, i, version, offset, length, lumpInt, count;
176 char lumpString[ 1024 ], source[ 1024 ];
177 qboolean lumpSwap = qfalse;
178 abspLumpTest_t *lumpTest;
179 static abspLumpTest_t lumpTests[] =
181 { sizeof( bspPlane_t ), 6, "IBSP LUMP_PLANES" },
182 { sizeof( bspBrush_t ), 1, "IBSP LUMP_BRUSHES" },
183 { 8, 6, "IBSP LUMP_BRUSHSIDES" },
184 { sizeof( bspBrushSide_t ), 6, "RBSP LUMP_BRUSHSIDES" },
185 { sizeof( bspModel_t ), 1, "IBSP LUMP_MODELS" },
186 { sizeof( bspNode_t ), 2, "IBSP LUMP_NODES" },
187 { sizeof( bspLeaf_t ), 1, "IBSP LUMP_LEAFS" },
188 { 104, 3, "IBSP LUMP_DRAWSURFS" },
189 { 44, 3, "IBSP LUMP_DRAWVERTS" },
190 { 4, 6, "IBSP LUMP_DRAWINDEXES" },
191 { 128 * 128 * 3, 1, "IBSP LUMP_LIGHTMAPS" },
192 { 256 * 256 * 3, 1, "IBSP LUMP_LIGHTMAPS (256 x 256)" },
193 { 512 * 512 * 3, 1, "IBSP LUMP_LIGHTMAPS (512 x 512)" },
201 Sys_Printf( "Usage: q3map -analyze [-lumpswap] [-v] <mapname>\n" );
205 /* process arguments */
206 for( i = 1; i < (argc - 1); i++ )
208 /* -format map|ase|... */
209 if( !strcmp( argv[ i ], "-lumpswap" ) )
211 Sys_Printf( "Swapped lump structs enabled\n" );
216 /* clean up map name */
217 strcpy( source, ExpandArg( argv[ i ] ) );
218 Sys_Printf( "Loading %s\n", source );
221 size = LoadFile( source, (void**) &header );
222 if( size == 0 || header == NULL )
224 Sys_Printf( "Unable to load %s.\n", source );
228 /* analyze ident/version */
229 memcpy( ident, header->ident, 4 );
231 version = LittleLong( header->version );
233 Sys_Printf( "Identity: %s\n", ident );
234 Sys_Printf( "Version: %d\n", version );
235 Sys_Printf( "---------------------------------------\n" );
237 /* analyze each lump */
238 for( i = 0; i < 100; i++ )
240 /* call of duty swapped lump pairs */
243 offset = LittleLong( header->lumps[ i ].length );
244 length = LittleLong( header->lumps[ i ].offset );
247 /* standard lump pairs */
250 offset = LittleLong( header->lumps[ i ].offset );
251 length = LittleLong( header->lumps[ i ].length );
255 lump = (byte*) header + offset;
256 lumpInt = LittleLong( (int) *((int*) lump) );
257 lumpFloat = LittleFloat( (float) *((float*) lump) );
258 memcpy( lumpString, (char*) lump, (length < 1024 ? length : 1024) );
259 lumpString[ 1024 ] = '\0';
261 /* print basic lump info */
262 Sys_Printf( "Lump: %d\n", i );
263 Sys_Printf( "Offset: %d bytes\n", offset );
264 Sys_Printf( "Length: %d bytes\n", length );
266 /* only operate on valid lumps */
269 /* print data in 4 formats */
270 Sys_Printf( "As hex: %08X\n", lumpInt );
271 Sys_Printf( "As int: %d\n", lumpInt );
272 Sys_Printf( "As float: %f\n", lumpFloat );
273 Sys_Printf( "As string: %s\n", lumpString );
275 /* guess lump type */
276 if( lumpString[ 0 ] == '{' && lumpString[ 2 ] == '"' )
277 Sys_Printf( "Type guess: IBSP LUMP_ENTITIES\n" );
278 else if( strstr( lumpString, "textures/" ) )
279 Sys_Printf( "Type guess: IBSP LUMP_SHADERS\n" );
282 /* guess based on size/count */
283 for( lumpTest = lumpTests; lumpTest->radix > 0; lumpTest++ )
285 if( (length % lumpTest->radix) != 0 )
287 count = length / lumpTest->radix;
288 if( count < lumpTest->minCount )
290 Sys_Printf( "Type guess: %s (%d x %d)\n", lumpTest->name, count, lumpTest->radix );
295 Sys_Printf( "---------------------------------------\n" );
298 if( offset + length >= size )
303 Sys_Printf( "Lump count: %d\n", i + 1 );
304 Sys_Printf( "File size: %d bytes\n", size );
306 /* return to caller */
314 emits statistics about the bsp file
317 int BSPInfo( int count, char **fileNames )
320 char source[ 1024 ], ext[ 64 ];
328 Sys_Printf( "No files to dump info for.\n");
332 /* enable info mode */
336 for( i = 0; i < count; i++ )
338 Sys_Printf( "---------------------------------\n" );
340 /* mangle filename and get size */
341 strcpy( source, fileNames[ i ] );
342 ExtractFileExtension( source, ext );
343 if( !Q_stricmp( ext, "map" ) )
344 StripExtension( source );
345 DefaultExtension( source, ".bsp" );
346 f = fopen( source, "rb" );
349 size = Q_filelength (f);
355 /* load the bsp file and print lump sizes */
356 Sys_Printf( "%s\n", source );
357 LoadBSPFile( source );
362 Sys_Printf( " total %9d\n", size );
363 Sys_Printf( " %9d KB\n", size / 1024 );
364 Sys_Printf( " %9d MB\n", size / (1024 * 1024) );
366 Sys_Printf( "---------------------------------\n" );
377 amaze and confuse your enemies with wierd scaled maps!
380 int ScaleBSPMain( int argc, char **argv )
393 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
398 scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
400 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
402 scale[0] = atof( argv[ argc - 4 ] );
404 uniform = ((scale[0] == scale[1]) && (scale[1] == scale[2]));
406 if( scale[0] == 0.0f || scale[1] == 0.0f || scale[2] == 0.0f )
408 Sys_Printf( "Usage: q3map -scale <value> <mapname>\n" );
409 Sys_Printf( "Non-zero scale value required.\n" );
413 /* do some path mangling */
414 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
415 StripExtension( source );
416 DefaultExtension( source, ".bsp" );
419 Sys_Printf( "Loading %s\n", source );
420 LoadBSPFile( source );
424 Sys_Printf( "--- ScaleBSP ---\n" );
425 Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
427 /* scale entity keys */
428 for( i = 0; i < numBSPEntities && i < numEntities; i++ )
431 GetVectorForKey( &entities[ i ], "origin", vec );
432 if( (vec[ 0 ] || vec[ 1 ] || vec[ 2 ]) )
437 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
438 SetKeyValue( &entities[ i ], "origin", str );
442 f = FloatForKey( &entities[ i ], "lip" );
443 a = FloatForKey( &entities[ i ], "angle" );
446 if(a == -1 || a == -2) // z scale
448 else if(fabs(sin(DEG2RAD(a))) < 0.707)
452 sprintf( str, "%f", f );
453 SetKeyValue( &entities[ i ], "lip", str );
458 for( i = 0; i < numBSPModels; i++ )
460 bspModels[ i ].mins[0] *= scale[0];
461 bspModels[ i ].mins[1] *= scale[1];
462 bspModels[ i ].mins[2] *= scale[2];
463 bspModels[ i ].maxs[0] *= scale[0];
464 bspModels[ i ].maxs[1] *= scale[1];
465 bspModels[ i ].maxs[2] *= scale[2];
469 for( i = 0; i < numBSPNodes; i++ )
471 bspNodes[ i ].mins[0] *= scale[0];
472 bspNodes[ i ].mins[1] *= scale[1];
473 bspNodes[ i ].mins[2] *= scale[2];
474 bspNodes[ i ].maxs[0] *= scale[0];
475 bspNodes[ i ].maxs[1] *= scale[1];
476 bspNodes[ i ].maxs[2] *= scale[2];
480 for( i = 0; i < numBSPLeafs; i++ )
482 bspLeafs[ i ].mins[0] *= scale[0];
483 bspLeafs[ i ].mins[1] *= scale[1];
484 bspLeafs[ i ].mins[2] *= scale[2];
485 bspLeafs[ i ].maxs[0] *= scale[0];
486 bspLeafs[ i ].maxs[1] *= scale[1];
487 bspLeafs[ i ].maxs[2] *= scale[2];
490 /* scale drawverts */
491 for( i = 0; i < numBSPDrawVerts; i++ )
493 bspDrawVerts[i].xyz[0] *= scale[0];
494 bspDrawVerts[i].xyz[1] *= scale[1];
495 bspDrawVerts[i].xyz[2] *= scale[2];
496 bspDrawVerts[i].normal[0] /= scale[0];
497 bspDrawVerts[i].normal[1] /= scale[1];
498 bspDrawVerts[i].normal[2] /= scale[2];
499 VectorNormalize(bspDrawVerts[i].normal, bspDrawVerts[i].normal);
505 for( i = 0; i < numBSPPlanes; i++ )
507 bspPlanes[ i ].dist *= scale[0];
512 for( i = 0; i < numBSPPlanes; i++ )
514 bspPlanes[ i ].normal[0] /= scale[0];
515 bspPlanes[ i ].normal[1] /= scale[1];
516 bspPlanes[ i ].normal[2] /= scale[2];
517 f = 1/VectorLength(bspPlanes[i].normal);
518 VectorScale(bspPlanes[i].normal, f, bspPlanes[i].normal);
519 bspPlanes[ i ].dist *= f;
524 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
525 if( (vec[ 0 ] + vec[ 1 ] + vec[ 2 ]) == 0.0f )
526 VectorCopy( gridSize, vec );
530 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
531 SetKeyValue( &entities[ 0 ], "gridsize", str );
533 /* inject command line parameters */
534 InjectCommandLine(argv, 0, argc - 1);
538 StripExtension( source );
539 DefaultExtension( source, "_s.bsp" );
540 Sys_Printf( "Writing %s\n", source );
541 WriteBSPFile( source );
543 /* return to sender */
551 main argument processing function for bsp conversion
554 int ConvertBSPMain( int argc, char **argv )
557 int (*convertFunc)( char * );
562 convertFunc = ConvertBSPToASE;
568 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
572 /* process arguments */
573 for( i = 1; i < (argc - 1); i++ )
575 /* -format map|ase|... */
576 if( !strcmp( argv[ i ], "-format" ) )
579 if( !Q_stricmp( argv[ i ], "ase" ) )
580 convertFunc = ConvertBSPToASE;
581 else if( !Q_stricmp( argv[ i ], "map" ) )
582 convertFunc = ConvertBSPToMap;
585 convertGame = GetGame( argv[ i ] );
586 if( convertGame == NULL )
587 Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
590 else if( !strcmp( argv[ i ], "-ne" ) )
592 normalEpsilon = atof( argv[ i + 1 ] );
594 Sys_Printf( "Normal epsilon set to %f\n", normalEpsilon );
596 else if( !strcmp( argv[ i ], "-de" ) )
598 distanceEpsilon = atof( argv[ i + 1 ] );
600 Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
602 else if( !strcmp( argv[ i ], "-shadersasbitmap" ) )
603 shadersAsBitmap = qtrue;
606 /* clean up map name */
607 strcpy( source, ExpandArg( argv[ i ] ) );
608 StripExtension( source );
609 DefaultExtension( source, ".bsp" );
613 Sys_Printf( "Loading %s\n", source );
615 /* ydnar: load surface file */
616 //% LoadSurfaceExtraFile( source );
618 LoadBSPFile( source );
620 /* parse bsp entities */
623 /* bsp format convert? */
624 if( convertGame != NULL )
626 /* set global game */
630 StripExtension( source );
631 DefaultExtension( source, "_c.bsp" );
632 Sys_Printf( "Writing %s\n", source );
633 WriteBSPFile( source );
635 /* return to sender */
640 return convertFunc( source );
650 int main( int argc, char **argv )
656 /* we want consistent 'randomness' */
660 start = I_FloatTime();
662 /* this was changed to emit version number over the network */
663 printf( Q3MAP_VERSION "\n" );
668 /* read general options first */
669 for( i = 1; i < argc; i++ )
672 if( !strcmp( argv[ i ], "-connect" ) )
676 Broadcast_Setup( argv[ i ] );
681 else if( !strcmp( argv[ i ], "-v" ) )
688 else if( !strcmp( argv[ i ], "-force" ) )
694 /* patch subdivisions */
695 else if( !strcmp( argv[ i ], "-subdivisions" ) )
699 patchSubdivisions = atoi( argv[ i ] );
701 if( patchSubdivisions <= 0 )
702 patchSubdivisions = 1;
706 else if( !strcmp( argv[ i ], "-threads" ) )
710 numthreads = atoi( argv[ i ] );
715 /* init model library */
717 PicoSetMallocFunc( safe_malloc );
718 PicoSetFreeFunc( free );
719 PicoSetPrintFunc( PicoPrintFunc );
720 PicoSetLoadFileFunc( PicoLoadFileFunc );
721 PicoSetFreeFileFunc( free );
723 /* set number of threads */
726 /* generate sinusoid jitter table */
727 for( i = 0; i < MAX_JITTERS; i++ )
729 jitters[ i ] = sin( i * 139.54152147 );
730 //% Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
733 /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
734 and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
736 Sys_Printf( "Q3Map - v1.0r (c) 1999 Id Software Inc.\n" );
737 Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
738 Sys_Printf( "NetRadiant - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
739 Sys_Printf( "%s\n", Q3MAP_MOTD );
741 /* ydnar: new path initialization */
742 InitPaths( &argc, argv );
744 /* check if we have enough options left to attempt something */
746 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
749 if( !strcmp( argv[ 1 ], "-fixaas" ) )
750 r = FixAAS( argc - 1, argv + 1 );
753 else if( !strcmp( argv[ 1 ], "-analyze" ) )
754 r = AnalyzeBSP( argc - 1, argv + 1 );
757 else if( !strcmp( argv[ 1 ], "-info" ) )
758 r = BSPInfo( argc - 2, argv + 2 );
761 else if( !strcmp( argv[ 1 ], "-vis" ) )
762 r = VisMain( argc - 1, argv + 1 );
765 else if( !strcmp( argv[ 1 ], "-light" ) )
766 r = LightMain( argc - 1, argv + 1 );
769 else if( !strcmp( argv[ 1 ], "-vlight" ) )
771 Sys_Printf( "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
772 argv[ 1 ] = "-fast"; /* eek a hack */
773 r = LightMain( argc, argv );
776 /* ydnar: lightmap export */
777 else if( !strcmp( argv[ 1 ], "-export" ) )
778 r = ExportLightmapsMain( argc - 1, argv + 1 );
780 /* ydnar: lightmap import */
781 else if( !strcmp( argv[ 1 ], "-import" ) )
782 r = ImportLightmapsMain( argc - 1, argv + 1 );
784 /* ydnar: bsp scaling */
785 else if( !strcmp( argv[ 1 ], "-scale" ) )
786 r = ScaleBSPMain( argc - 1, argv + 1 );
788 /* ydnar: bsp conversion */
789 else if( !strcmp( argv[ 1 ], "-convert" ) )
790 r = ConvertBSPMain( argc - 1, argv + 1 );
792 /* ydnar: otherwise create a bsp */
794 r = BSPMain( argc, argv );
798 Sys_Printf( "%9.0f seconds elapsed\n", end - start );
800 /* shut down connection */
801 Broadcast_Shutdown();
803 /* return any error code */