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;
55 static void ExitQ3Map( void ){
57 if ( mapDrawSurfs != NULL ) {
62 static int MD4BlockChecksum( void * buffer, int length ) {
63 unsigned char digest[16];
66 md4_get_digest( buffer, length, digest );
67 /* I suppose it has to be done that way for legacy reasons? */
68 checksum = digest[0] & ( digest[1] << 8 ) & ( digest[2] << 16 ) & ( digest[3] << 24 );
69 checksum ^= digest[4] & ( digest[5] << 8 ) & ( digest[6] << 16 ) & ( digest[7] << 24 );
70 checksum ^= digest[8] & ( digest[9] << 8 ) & ( digest[10] << 16 ) & ( digest[11] << 24 );
71 checksum ^= digest[12] & ( digest[13] << 8 ) & ( digest[14] << 16 ) & ( digest[15] << 24 );
77 resets an aas checksum to match the given BSP
80 int FixAAS( int argc, char **argv ){
84 char aas[ 1024 ], **ext;
96 Sys_Printf( "Usage: q3map -fixaas [-v] <mapname>\n" );
100 /* do some path mangling */
101 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
102 StripExtension( source );
103 DefaultExtension( source, ".bsp" );
106 Sys_Printf( "--- FixAAS ---\n" );
109 Sys_Printf( "Loading %s\n", source );
110 length = LoadFile( source, &buffer );
112 /* create bsp checksum */
113 Sys_Printf( "Creating checksum...\n" );
114 checksum = LittleLong( MD4BlockChecksum( buffer, length ) );
116 /* write checksum to aas */
121 strcpy( aas, source );
122 StripExtension( aas );
124 Sys_Printf( "Trying %s\n", aas );
128 file = fopen( aas, "r+b" );
132 if ( fwrite( &checksum, 4, 1, file ) != 1 ) {
133 Error( "Error writing checksum to %s", aas );
138 /* return to sender */
146 analyzes a Quake engine BSP file
149 typedef struct abspHeader_s
154 bspLump_t lumps[ 1 ]; /* unknown size */
158 typedef struct abspLumpTest_s
165 int AnalyzeBSP( int argc, char **argv ){
166 abspHeader_t *header;
167 int size, i, version, offset, length, lumpInt, count;
171 char lumpString[ 1024 ], source[ 1024 ];
172 qboolean lumpSwap = qfalse;
173 abspLumpTest_t *lumpTest;
174 static abspLumpTest_t lumpTests[] =
176 { sizeof( bspPlane_t ), 6, "IBSP LUMP_PLANES" },
177 { sizeof( bspBrush_t ), 1, "IBSP LUMP_BRUSHES" },
178 { 8, 6, "IBSP LUMP_BRUSHSIDES" },
179 { sizeof( bspBrushSide_t ), 6, "RBSP LUMP_BRUSHSIDES" },
180 { sizeof( bspModel_t ), 1, "IBSP LUMP_MODELS" },
181 { sizeof( bspNode_t ), 2, "IBSP LUMP_NODES" },
182 { sizeof( bspLeaf_t ), 1, "IBSP LUMP_LEAFS" },
183 { 104, 3, "IBSP LUMP_DRAWSURFS" },
184 { 44, 3, "IBSP LUMP_DRAWVERTS" },
185 { 4, 6, "IBSP LUMP_DRAWINDEXES" },
186 { 128 * 128 * 3, 1, "IBSP LUMP_LIGHTMAPS" },
187 { 256 * 256 * 3, 1, "IBSP LUMP_LIGHTMAPS (256 x 256)" },
188 { 512 * 512 * 3, 1, "IBSP LUMP_LIGHTMAPS (512 x 512)" },
195 Sys_Printf( "Usage: q3map -analyze [-lumpswap] [-v] <mapname>\n" );
199 /* process arguments */
200 for ( i = 1; i < ( argc - 1 ); i++ )
202 /* -format map|ase|... */
203 if ( !strcmp( argv[ i ], "-lumpswap" ) ) {
204 Sys_Printf( "Swapped lump structs enabled\n" );
209 /* clean up map name */
210 strcpy( source, ExpandArg( argv[ i ] ) );
211 Sys_Printf( "Loading %s\n", source );
214 size = LoadFile( source, (void**) &header );
215 if ( size == 0 || header == NULL ) {
216 Sys_Printf( "Unable to load %s.\n", source );
220 /* analyze ident/version */
221 memcpy( ident, header->ident, 4 );
223 version = LittleLong( header->version );
225 Sys_Printf( "Identity: %s\n", ident );
226 Sys_Printf( "Version: %d\n", version );
227 Sys_Printf( "---------------------------------------\n" );
229 /* analyze each lump */
230 for ( i = 0; i < 100; i++ )
232 /* call of duty swapped lump pairs */
234 offset = LittleLong( header->lumps[ i ].length );
235 length = LittleLong( header->lumps[ i ].offset );
238 /* standard lump pairs */
241 offset = LittleLong( header->lumps[ i ].offset );
242 length = LittleLong( header->lumps[ i ].length );
246 lump = (byte*) header + offset;
247 lumpInt = LittleLong( (int) *( (int*) lump ) );
248 lumpFloat = LittleFloat( (float) *( (float*) lump ) );
249 memcpy( lumpString, (char*) lump, ( length < 1024 ? length : 1024 ) );
250 lumpString[ 1024 ] = '\0';
252 /* print basic lump info */
253 Sys_Printf( "Lump: %d\n", i );
254 Sys_Printf( "Offset: %d bytes\n", offset );
255 Sys_Printf( "Length: %d bytes\n", length );
257 /* only operate on valid lumps */
259 /* print data in 4 formats */
260 Sys_Printf( "As hex: %08X\n", lumpInt );
261 Sys_Printf( "As int: %d\n", lumpInt );
262 Sys_Printf( "As float: %f\n", lumpFloat );
263 Sys_Printf( "As string: %s\n", lumpString );
265 /* guess lump type */
266 if ( lumpString[ 0 ] == '{' && lumpString[ 2 ] == '"' ) {
267 Sys_Printf( "Type guess: IBSP LUMP_ENTITIES\n" );
269 else if ( strstr( lumpString, "textures/" ) ) {
270 Sys_Printf( "Type guess: IBSP LUMP_SHADERS\n" );
274 /* guess based on size/count */
275 for ( lumpTest = lumpTests; lumpTest->radix > 0; lumpTest++ )
277 if ( ( length % lumpTest->radix ) != 0 ) {
280 count = length / lumpTest->radix;
281 if ( count < lumpTest->minCount ) {
284 Sys_Printf( "Type guess: %s (%d x %d)\n", lumpTest->name, count, lumpTest->radix );
289 Sys_Printf( "---------------------------------------\n" );
292 if ( offset + length >= size ) {
298 Sys_Printf( "Lump count: %d\n", i + 1 );
299 Sys_Printf( "File size: %d bytes\n", size );
301 /* return to caller */
309 emits statistics about the bsp file
312 int BSPInfo( int count, char **fileNames ){
314 char source[ 1024 ], ext[ 64 ];
321 Sys_Printf( "No files to dump info for.\n" );
325 /* enable info mode */
329 for ( i = 0; i < count; i++ )
331 Sys_Printf( "---------------------------------\n" );
333 /* mangle filename and get size */
334 strcpy( source, fileNames[ i ] );
335 ExtractFileExtension( source, ext );
336 if ( !Q_stricmp( ext, "map" ) ) {
337 StripExtension( source );
339 DefaultExtension( source, ".bsp" );
340 f = fopen( source, "rb" );
342 size = Q_filelength( f );
349 /* load the bsp file and print lump sizes */
350 Sys_Printf( "%s\n", source );
351 LoadBSPFile( source );
356 Sys_Printf( " total %9d\n", size );
357 Sys_Printf( " %9d KB\n", size / 1024 );
358 Sys_Printf( " %9d MB\n", size / ( 1024 * 1024 ) );
360 Sys_Printf( "---------------------------------\n" );
371 amaze and confuse your enemies with wierd scaled maps!
374 int ScaleBSPMain( int argc, char **argv ){
383 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
388 scale = atof( argv[ argc - 2 ] );
389 if ( scale == 0.0f ) {
390 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
391 Sys_Printf( "Non-zero scale value required.\n" );
395 /* do some path mangling */
396 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
397 StripExtension( source );
398 DefaultExtension( source, ".bsp" );
401 Sys_Printf( "Loading %s\n", source );
402 LoadBSPFile( source );
406 Sys_Printf( "--- ScaleBSP ---\n" );
407 Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
409 /* scale entity keys */
410 for ( i = 0; i < numBSPEntities && i < numEntities; i++ )
413 GetVectorForKey( &entities[ i ], "origin", vec );
414 if ( ( vec[ 0 ] + vec[ 1 ] + vec[ 2 ] ) ) {
415 VectorScale( vec, scale, vec );
416 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
417 SetKeyValue( &entities[ i ], "origin", str );
421 f = FloatForKey( &entities[ i ], "lip" );
424 sprintf( str, "%f", f );
425 SetKeyValue( &entities[ i ], "lip", str );
430 for ( i = 0; i < numBSPModels; i++ )
432 VectorScale( bspModels[ i ].mins, scale, bspModels[ i ].mins );
433 VectorScale( bspModels[ i ].maxs, scale, bspModels[ i ].maxs );
437 for ( i = 0; i < numBSPNodes; i++ )
439 VectorScale( bspNodes[ i ].mins, scale, bspNodes[ i ].mins );
440 VectorScale( bspNodes[ i ].maxs, scale, bspNodes[ i ].maxs );
444 for ( i = 0; i < numBSPLeafs; i++ )
446 VectorScale( bspLeafs[ i ].mins, scale, bspLeafs[ i ].mins );
447 VectorScale( bspLeafs[ i ].maxs, scale, bspLeafs[ i ].maxs );
450 /* scale drawverts */
451 for ( i = 0; i < numBSPDrawVerts; i++ )
452 VectorScale( bspDrawVerts[ i ].xyz, scale, bspDrawVerts[ i ].xyz );
455 for ( i = 0; i < numBSPPlanes; i++ )
456 bspPlanes[ i ].dist *= scale;
459 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
460 if ( ( vec[ 0 ] + vec[ 1 ] + vec[ 2 ] ) == 0.0f ) {
461 VectorCopy( gridSize, vec );
463 VectorScale( vec, scale, vec );
464 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
465 SetKeyValue( &entities[ 0 ], "gridsize", str );
469 StripExtension( source );
470 DefaultExtension( source, "_s.bsp" );
471 Sys_Printf( "Writing %s\n", source );
472 WriteBSPFile( source );
474 /* return to sender */
482 main argument processing function for bsp conversion
485 int ConvertBSPMain( int argc, char **argv ){
487 int ( *convertFunc )( char * );
492 convertFunc = ConvertBSPToASE;
497 Sys_Printf( "Usage: q3map -convert [-format <ase|map>] [-v] <mapname>\n" );
501 /* process arguments */
502 for ( i = 1; i < ( argc - 1 ); i++ )
504 /* -format map|ase|... */
505 if ( !strcmp( argv[ i ], "-format" ) ) {
507 if ( !Q_stricmp( argv[ i ], "ase" ) ) {
508 convertFunc = ConvertBSPToASE;
510 else if ( !Q_stricmp( argv[ i ], "map" ) ) {
511 convertFunc = ConvertBSPToMap;
515 convertGame = GetGame( argv[ i ] );
516 if ( convertGame == NULL ) {
517 Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
523 /* clean up map name */
524 strcpy( source, ExpandArg( argv[ i ] ) );
525 StripExtension( source );
526 DefaultExtension( source, ".bsp" );
530 Sys_Printf( "Loading %s\n", source );
532 /* ydnar: load surface file */
533 //% LoadSurfaceExtraFile( source );
535 LoadBSPFile( source );
537 /* parse bsp entities */
540 /* bsp format convert? */
541 if ( convertGame != NULL ) {
542 /* set global game */
546 StripExtension( source );
547 DefaultExtension( source, "_c.bsp" );
548 Sys_Printf( "Writing %s\n", source );
549 WriteBSPFile( source );
551 /* return to sender */
556 return convertFunc( source );
566 int main( int argc, char **argv ){
571 /* we want consistent 'randomness' */
575 start = I_FloatTime();
577 /* this was changed to emit version number over the network */
578 printf( Q3MAP_VERSION "\n" );
583 /* read general options first */
584 for ( i = 1; i < argc; i++ )
587 if ( !strcmp( argv[ i ], "-connect" ) ) {
590 Broadcast_Setup( argv[ i ] );
595 else if ( !strcmp( argv[ i ], "-v" ) ) {
601 else if ( !strcmp( argv[ i ], "-force" ) ) {
606 /* patch subdivisions */
607 else if ( !strcmp( argv[ i ], "-subdivisions" ) ) {
610 patchSubdivisions = atoi( argv[ i ] );
612 if ( patchSubdivisions <= 0 ) {
613 patchSubdivisions = 1;
618 else if ( !strcmp( argv[ i ], "-threads" ) ) {
621 numthreads = atoi( argv[ i ] );
626 /* init model library */
628 PicoSetMallocFunc( safe_malloc );
629 PicoSetFreeFunc( free );
630 PicoSetPrintFunc( PicoPrintFunc );
631 PicoSetLoadFileFunc( PicoLoadFileFunc );
632 PicoSetFreeFileFunc( free );
634 /* set number of threads */
637 /* generate sinusoid jitter table */
638 for ( i = 0; i < MAX_JITTERS; i++ )
640 jitters[ i ] = sin( i * 139.54152147 );
641 //% Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
644 /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
645 and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
647 Sys_Printf( "Q3Map - v1.0r (c) 1999 Id Software Inc.\n" );
648 Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
649 Sys_Printf( "GtkRadiant - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
650 Sys_Printf( "%s\n", Q3MAP_MOTD );
652 /* ydnar: new path initialization */
653 InitPaths( &argc, argv );
655 /* check if we have enough options left to attempt something */
657 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
661 if ( !strcmp( argv[ 1 ], "-fixaas" ) ) {
662 r = FixAAS( argc - 1, argv + 1 );
666 else if ( !strcmp( argv[ 1 ], "-analyze" ) ) {
667 r = AnalyzeBSP( argc - 1, argv + 1 );
671 else if ( !strcmp( argv[ 1 ], "-info" ) ) {
672 r = BSPInfo( argc - 2, argv + 2 );
676 else if ( !strcmp( argv[ 1 ], "-vis" ) ) {
677 r = VisMain( argc - 1, argv + 1 );
681 else if ( !strcmp( argv[ 1 ], "-light" ) ) {
682 r = LightMain( argc - 1, argv + 1 );
686 else if ( !strcmp( argv[ 1 ], "-vlight" ) ) {
687 Sys_Printf( "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
688 argv[ 1 ] = "-fast"; /* eek a hack */
689 r = LightMain( argc, argv );
692 /* ydnar: lightmap export */
693 else if ( !strcmp( argv[ 1 ], "-export" ) ) {
694 r = ExportLightmapsMain( argc - 1, argv + 1 );
697 /* ydnar: lightmap import */
698 else if ( !strcmp( argv[ 1 ], "-import" ) ) {
699 r = ImportLightmapsMain( argc - 1, argv + 1 );
702 /* ydnar: bsp scaling */
703 else if ( !strcmp( argv[ 1 ], "-scale" ) ) {
704 r = ScaleBSPMain( argc - 1, argv + 1 );
707 /* ydnar: bsp conversion */
708 else if ( !strcmp( argv[ 1 ], "-convert" ) ) {
709 r = ConvertBSPMain( argc - 1, argv + 1 );
712 /* ydnar: otherwise create a bsp */
714 r = BSPMain( argc, argv );
719 Sys_Printf( "%9.0f seconds elapsed\n", end - start );
721 /* shut down connection */
722 Broadcast_Shutdown();
724 /* return any error code */