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 ------------------------------------------------------------------------------- */
41 returns a pseudorandom number between 0 and 1
45 return (vec_t) rand() / RAND_MAX;
49 char *Q_strncpyz( char *dst, const char *src, size_t len ) {
54 strncpy( dst, src, len );
55 dst[ len - 1 ] = '\0';
60 char *Q_strcat( char *dst, size_t dlen, const char *src ) {
61 size_t n = strlen( dst );
64 abort(); /* buffer overflow */
67 return Q_strncpyz( dst + n, src, dlen - n );
71 char *Q_strncat( char *dst, size_t dlen, const char *src, size_t slen ) {
72 size_t n = strlen( dst );
75 abort(); /* buffer overflow */
78 return Q_strncpyz( dst + n, src, MIN( slen, dlen - n ) );
87 static void ExitQ3Map( void ){
89 if ( mapDrawSurfs != NULL ) {
94 static int MD4BlockChecksum( void * buffer, int length ) {
95 unsigned char digest[16];
98 md4_get_digest( buffer, length, digest );
99 /* I suppose it has to be done that way for legacy reasons? */
100 checksum = digest[0] & ( digest[1] << 8 ) & ( digest[2] << 16 ) & ( digest[3] << 24 );
101 checksum ^= digest[4] & ( digest[5] << 8 ) & ( digest[6] << 16 ) & ( digest[7] << 24 );
102 checksum ^= digest[8] & ( digest[9] << 8 ) & ( digest[10] << 16 ) & ( digest[11] << 24 );
103 checksum ^= digest[12] & ( digest[13] << 8 ) & ( digest[14] << 16 ) & ( digest[15] << 24 );
109 resets an aas checksum to match the given BSP
112 int FixAAS( int argc, char **argv ){
113 int length, checksum;
116 char aas[ 1024 ], **ext;
128 Sys_Printf( "Usage: q3map -fixaas [-v] <mapname>\n" );
132 /* do some path mangling */
133 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
134 StripExtension( source );
135 DefaultExtension( source, ".bsp" );
138 Sys_Printf( "--- FixAAS ---\n" );
141 Sys_Printf( "Loading %s\n", source );
142 length = LoadFile( source, &buffer );
144 /* create bsp checksum */
145 Sys_Printf( "Creating checksum...\n" );
146 checksum = LittleLong( MD4BlockChecksum( buffer, length ) );
148 /* write checksum to aas */
153 strcpy( aas, source );
154 StripExtension( aas );
156 Sys_Printf( "Trying %s\n", aas );
160 file = fopen( aas, "r+b" );
164 if ( fwrite( &checksum, 4, 1, file ) != 1 ) {
165 Error( "Error writing checksum to %s", aas );
170 /* return to sender */
178 analyzes a Quake engine BSP file
181 typedef struct abspHeader_s
186 bspLump_t lumps[ 1 ]; /* unknown size */
190 typedef struct abspLumpTest_s
197 int AnalyzeBSP( int argc, char **argv ){
198 abspHeader_t *header;
199 int size, i, version, offset, length, lumpInt, count;
203 char lumpString[ 1024 ], source[ 1024 ];
204 qboolean lumpSwap = qfalse;
205 abspLumpTest_t *lumpTest;
206 static abspLumpTest_t lumpTests[] =
208 { sizeof( bspPlane_t ), 6, "IBSP LUMP_PLANES" },
209 { sizeof( bspBrush_t ), 1, "IBSP LUMP_BRUSHES" },
210 { 8, 6, "IBSP LUMP_BRUSHSIDES" },
211 { sizeof( bspBrushSide_t ), 6, "RBSP LUMP_BRUSHSIDES" },
212 { sizeof( bspModel_t ), 1, "IBSP LUMP_MODELS" },
213 { sizeof( bspNode_t ), 2, "IBSP LUMP_NODES" },
214 { sizeof( bspLeaf_t ), 1, "IBSP LUMP_LEAFS" },
215 { 104, 3, "IBSP LUMP_DRAWSURFS" },
216 { 44, 3, "IBSP LUMP_DRAWVERTS" },
217 { 4, 6, "IBSP LUMP_DRAWINDEXES" },
218 { 128 * 128 * 3, 1, "IBSP LUMP_LIGHTMAPS" },
219 { 256 * 256 * 3, 1, "IBSP LUMP_LIGHTMAPS (256 x 256)" },
220 { 512 * 512 * 3, 1, "IBSP LUMP_LIGHTMAPS (512 x 512)" },
227 Sys_Printf( "Usage: q3map -analyze [-lumpswap] [-v] <mapname>\n" );
231 /* process arguments */
232 for ( i = 1; i < ( argc - 1 ); i++ )
234 /* -format map|ase|... */
235 if ( !strcmp( argv[ i ], "-lumpswap" ) ) {
236 Sys_Printf( "Swapped lump structs enabled\n" );
241 /* clean up map name */
242 strcpy( source, ExpandArg( argv[ i ] ) );
243 Sys_Printf( "Loading %s\n", source );
246 size = LoadFile( source, (void**) &header );
247 if ( size == 0 || header == NULL ) {
248 Sys_Printf( "Unable to load %s.\n", source );
252 /* analyze ident/version */
253 memcpy( ident, header->ident, 4 );
255 version = LittleLong( header->version );
257 Sys_Printf( "Identity: %s\n", ident );
258 Sys_Printf( "Version: %d\n", version );
259 Sys_Printf( "---------------------------------------\n" );
261 /* analyze each lump */
262 for ( i = 0; i < 100; i++ )
264 /* call of duty swapped lump pairs */
266 offset = LittleLong( header->lumps[ i ].length );
267 length = LittleLong( header->lumps[ i ].offset );
270 /* standard lump pairs */
273 offset = LittleLong( header->lumps[ i ].offset );
274 length = LittleLong( header->lumps[ i ].length );
278 lump = (byte*) header + offset;
279 lumpInt = LittleLong( (int) *( (int*) lump ) );
280 lumpFloat = LittleFloat( (float) *( (float*) lump ) );
281 memcpy( lumpString, (char*) lump, ( length < 1024 ? length : 1024 ) );
282 lumpString[ 1024 ] = '\0';
284 /* print basic lump info */
285 Sys_Printf( "Lump: %d\n", i );
286 Sys_Printf( "Offset: %d bytes\n", offset );
287 Sys_Printf( "Length: %d bytes\n", length );
289 /* only operate on valid lumps */
291 /* print data in 4 formats */
292 Sys_Printf( "As hex: %08X\n", lumpInt );
293 Sys_Printf( "As int: %d\n", lumpInt );
294 Sys_Printf( "As float: %f\n", lumpFloat );
295 Sys_Printf( "As string: %s\n", lumpString );
297 /* guess lump type */
298 if ( lumpString[ 0 ] == '{' && lumpString[ 2 ] == '"' ) {
299 Sys_Printf( "Type guess: IBSP LUMP_ENTITIES\n" );
301 else if ( strstr( lumpString, "textures/" ) ) {
302 Sys_Printf( "Type guess: IBSP LUMP_SHADERS\n" );
306 /* guess based on size/count */
307 for ( lumpTest = lumpTests; lumpTest->radix > 0; lumpTest++ )
309 if ( ( length % lumpTest->radix ) != 0 ) {
312 count = length / lumpTest->radix;
313 if ( count < lumpTest->minCount ) {
316 Sys_Printf( "Type guess: %s (%d x %d)\n", lumpTest->name, count, lumpTest->radix );
321 Sys_Printf( "---------------------------------------\n" );
324 if ( offset + length >= size ) {
330 Sys_Printf( "Lump count: %d\n", i + 1 );
331 Sys_Printf( "File size: %d bytes\n", size );
333 /* return to caller */
341 emits statistics about the bsp file
344 int BSPInfo( int count, char **fileNames ){
346 char source[ 1024 ], ext[ 64 ];
353 Sys_Printf( "No files to dump info for.\n" );
357 /* enable info mode */
361 for ( i = 0; i < count; i++ )
363 Sys_Printf( "---------------------------------\n" );
365 /* mangle filename and get size */
366 strcpy( source, fileNames[ i ] );
367 ExtractFileExtension( source, ext );
368 if ( !Q_stricmp( ext, "map" ) ) {
369 StripExtension( source );
371 DefaultExtension( source, ".bsp" );
372 f = fopen( source, "rb" );
374 size = Q_filelength( f );
381 /* load the bsp file and print lump sizes */
382 Sys_Printf( "%s\n", source );
383 LoadBSPFile( source );
388 Sys_Printf( " total %9d\n", size );
389 Sys_Printf( " %9d KB\n", size / 1024 );
390 Sys_Printf( " %9d MB\n", size / ( 1024 * 1024 ) );
392 Sys_Printf( "---------------------------------\n" );
403 amaze and confuse your enemies with wierd scaled maps!
406 int ScaleBSPMain( int argc, char **argv ){
415 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
420 scale = atof( argv[ argc - 2 ] );
421 if ( scale == 0.0f ) {
422 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
423 Sys_Printf( "Non-zero scale value required.\n" );
427 /* do some path mangling */
428 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
429 StripExtension( source );
430 DefaultExtension( source, ".bsp" );
433 Sys_Printf( "Loading %s\n", source );
434 LoadBSPFile( source );
438 Sys_Printf( "--- ScaleBSP ---\n" );
439 Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
441 /* scale entity keys */
442 for ( i = 0; i < numBSPEntities && i < numEntities; i++ )
445 GetVectorForKey( &entities[ i ], "origin", vec );
446 if ( ( vec[ 0 ] + vec[ 1 ] + vec[ 2 ] ) ) {
447 VectorScale( vec, scale, vec );
448 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
449 SetKeyValue( &entities[ i ], "origin", str );
453 f = FloatForKey( &entities[ i ], "lip" );
456 sprintf( str, "%f", f );
457 SetKeyValue( &entities[ i ], "lip", str );
462 for ( i = 0; i < numBSPModels; i++ )
464 VectorScale( bspModels[ i ].mins, scale, bspModels[ i ].mins );
465 VectorScale( bspModels[ i ].maxs, scale, bspModels[ i ].maxs );
469 for ( i = 0; i < numBSPNodes; i++ )
471 VectorScale( bspNodes[ i ].mins, scale, bspNodes[ i ].mins );
472 VectorScale( bspNodes[ i ].maxs, scale, bspNodes[ i ].maxs );
476 for ( i = 0; i < numBSPLeafs; i++ )
478 VectorScale( bspLeafs[ i ].mins, scale, bspLeafs[ i ].mins );
479 VectorScale( bspLeafs[ i ].maxs, scale, bspLeafs[ i ].maxs );
482 /* scale drawverts */
483 for ( i = 0; i < numBSPDrawVerts; i++ )
484 VectorScale( bspDrawVerts[ i ].xyz, scale, bspDrawVerts[ i ].xyz );
487 for ( i = 0; i < numBSPPlanes; i++ )
488 bspPlanes[ i ].dist *= scale;
491 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
492 if ( ( vec[ 0 ] + vec[ 1 ] + vec[ 2 ] ) == 0.0f ) {
493 VectorCopy( gridSize, vec );
495 VectorScale( vec, scale, vec );
496 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
497 SetKeyValue( &entities[ 0 ], "gridsize", str );
501 StripExtension( source );
502 DefaultExtension( source, "_s.bsp" );
503 Sys_Printf( "Writing %s\n", source );
504 WriteBSPFile( source );
506 /* return to sender */
514 main argument processing function for bsp conversion
517 int ConvertBSPMain( int argc, char **argv ){
519 int ( *convertFunc )( char * );
524 convertFunc = ConvertBSPToASE;
529 Sys_Printf( "Usage: q3map -convert [-format <ase|map>] [-v] <mapname>\n" );
533 /* process arguments */
534 for ( i = 1; i < ( argc - 1 ); i++ )
536 /* -format map|ase|... */
537 if ( !strcmp( argv[ i ], "-format" ) ) {
539 if ( !Q_stricmp( argv[ i ], "ase" ) ) {
540 convertFunc = ConvertBSPToASE;
542 else if ( !Q_stricmp( argv[ i ], "map" ) ) {
543 convertFunc = ConvertBSPToMap;
547 convertGame = GetGame( argv[ i ] );
548 if ( convertGame == NULL ) {
549 Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
555 /* clean up map name */
556 strcpy( source, ExpandArg( argv[ i ] ) );
557 StripExtension( source );
558 DefaultExtension( source, ".bsp" );
562 Sys_Printf( "Loading %s\n", source );
564 /* ydnar: load surface file */
565 //% LoadSurfaceExtraFile( source );
567 LoadBSPFile( source );
569 /* parse bsp entities */
572 /* bsp format convert? */
573 if ( convertGame != NULL ) {
574 /* set global game */
578 StripExtension( source );
579 DefaultExtension( source, "_c.bsp" );
580 Sys_Printf( "Writing %s\n", source );
581 WriteBSPFile( source );
583 /* return to sender */
588 return convertFunc( source );
598 int main( int argc, char **argv ){
603 /* we want consistent 'randomness' */
607 start = I_FloatTime();
609 /* this was changed to emit version number over the network */
610 printf( Q3MAP_VERSION "\n" );
615 /* read general options first */
616 for ( i = 1; i < argc; i++ )
619 if ( !strcmp( argv[ i ], "-connect" ) ) {
622 Broadcast_Setup( argv[ i ] );
627 else if ( !strcmp( argv[ i ], "-v" ) ) {
633 else if ( !strcmp( argv[ i ], "-force" ) ) {
638 /* patch subdivisions */
639 else if ( !strcmp( argv[ i ], "-subdivisions" ) ) {
642 patchSubdivisions = atoi( argv[ i ] );
644 if ( patchSubdivisions <= 0 ) {
645 patchSubdivisions = 1;
650 else if ( !strcmp( argv[ i ], "-threads" ) ) {
653 numthreads = atoi( argv[ i ] );
658 /* init model library */
660 PicoSetMallocFunc( safe_malloc );
661 PicoSetFreeFunc( free );
662 PicoSetPrintFunc( PicoPrintFunc );
663 PicoSetLoadFileFunc( PicoLoadFileFunc );
664 PicoSetFreeFileFunc( free );
666 /* set number of threads */
669 /* generate sinusoid jitter table */
670 for ( i = 0; i < MAX_JITTERS; i++ )
672 jitters[ i ] = sin( i * 139.54152147 );
673 //% Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
676 /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
677 and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
679 Sys_Printf( "Q3Map - v1.0r (c) 1999 Id Software Inc.\n" );
680 Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
681 Sys_Printf( "GtkRadiant - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
682 Sys_Printf( "%s\n", Q3MAP_MOTD );
684 /* ydnar: new path initialization */
685 InitPaths( &argc, argv );
687 /* check if we have enough options left to attempt something */
689 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
693 if ( !strcmp( argv[ 1 ], "-fixaas" ) ) {
694 r = FixAAS( argc - 1, argv + 1 );
698 else if ( !strcmp( argv[ 1 ], "-analyze" ) ) {
699 r = AnalyzeBSP( argc - 1, argv + 1 );
703 else if ( !strcmp( argv[ 1 ], "-info" ) ) {
704 r = BSPInfo( argc - 2, argv + 2 );
708 else if ( !strcmp( argv[ 1 ], "-vis" ) ) {
709 r = VisMain( argc - 1, argv + 1 );
713 else if ( !strcmp( argv[ 1 ], "-light" ) ) {
714 r = LightMain( argc - 1, argv + 1 );
718 else if ( !strcmp( argv[ 1 ], "-vlight" ) ) {
719 Sys_Printf( "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
720 argv[ 1 ] = "-fast"; /* eek a hack */
721 r = LightMain( argc, argv );
724 /* ydnar: lightmap export */
725 else if ( !strcmp( argv[ 1 ], "-export" ) ) {
726 r = ExportLightmapsMain( argc - 1, argv + 1 );
729 /* ydnar: lightmap import */
730 else if ( !strcmp( argv[ 1 ], "-import" ) ) {
731 r = ImportLightmapsMain( argc - 1, argv + 1 );
734 /* ydnar: bsp scaling */
735 else if ( !strcmp( argv[ 1 ], "-scale" ) ) {
736 r = ScaleBSPMain( argc - 1, argv + 1 );
739 /* ydnar: bsp conversion */
740 else if ( !strcmp( argv[ 1 ], "-convert" ) ) {
741 r = ConvertBSPMain( argc - 1, argv + 1 );
744 /* ydnar: otherwise create a bsp */
746 r = BSPMain( argc, argv );
751 Sys_Printf( "%9.0f seconds elapsed\n", end - start );
753 /* shut down connection */
754 Broadcast_Shutdown();
756 /* return any error code */