2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 -------------------------------------------------------------------------------
23 This code has been altered significantly from its original form, to support
24 several games based on the Quake III Arena engine, in the form of "Q3Map2."
26 ------------------------------------------------------------------------------- */
42 returns a pseudorandom number between 0 and 1
47 return (vec_t) rand() / RAND_MAX;
57 static void ExitQ3Map( void )
60 if( mapDrawSurfs != NULL )
68 emits statistics about the bsp file
71 int BSPInfo( int count, char **fileNames )
74 char source[ 1024 ], ext[ 64 ];
82 Sys_Printf( "No files to dump info for.\n");
86 /* enable info mode */
90 for( i = 0; i < count; i++ )
92 Sys_Printf( "---------------------------------\n" );
94 /* mangle filename and get size */
95 strcpy( source, fileNames[ i ] );
96 ExtractFileExtension( source, ext );
97 if( !Q_stricmp( ext, "map" ) )
98 StripExtension( source );
99 DefaultExtension( source, ".bsp" );
100 f = fopen( source, "rb" );
103 size = Q_filelength (f);
109 /* load the bsp file and print lump sizes */
110 Sys_Printf( "%s\n", source );
111 LoadBSPFile( source );
116 Sys_Printf( " total %9d\n", size );
117 Sys_Printf( " %9d KB\n", size / 1024 );
118 Sys_Printf( " %9d MB\n", size / (1024 * 1024) );
120 Sys_Printf( "---------------------------------\n" );
131 amaze and confuse your enemies with wierd scaled maps!
134 int ScaleBSPMain( int argc, char **argv )
145 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
150 scale = atof( argv[ argc - 2 ] );
153 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
154 Sys_Printf( "Non-zero scale value required.\n" );
158 /* do some path mangling */
159 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
160 StripExtension( source );
161 DefaultExtension( source, ".bsp" );
164 Sys_Printf( "Loading %s\n", source );
165 LoadBSPFile( source );
169 Sys_Printf( "--- ScaleBSP ---\n" );
170 Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
172 /* scale entity keys */
173 for( i = 0; i < numBSPEntities && i < numEntities; i++ )
176 GetVectorForKey( &entities[ i ], "origin", vec );
177 if( (vec[ 0 ] + vec[ 1 ] + vec[ 2 ]) )
179 VectorScale( vec, scale, vec );
180 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
181 SetKeyValue( &entities[ i ], "origin", str );
185 f = FloatForKey( &entities[ i ], "lip" );
189 sprintf( str, "%f", f );
190 SetKeyValue( &entities[ i ], "lip", str );
195 for( i = 0; i < numBSPModels; i++ )
197 VectorScale( bspModels[ i ].mins, scale, bspModels[ i ].mins );
198 VectorScale( bspModels[ i ].maxs, scale, bspModels[ i ].maxs );
202 for( i = 0; i < numBSPNodes; i++ )
204 VectorScale( bspNodes[ i ].mins, scale, bspNodes[ i ].mins );
205 VectorScale( bspNodes[ i ].maxs, scale, bspNodes[ i ].maxs );
209 for( i = 0; i < numBSPLeafs; i++ )
211 VectorScale( bspLeafs[ i ].mins, scale, bspLeafs[ i ].mins );
212 VectorScale( bspLeafs[ i ].maxs, scale, bspLeafs[ i ].maxs );
215 /* scale drawverts */
216 for( i = 0; i < numBSPDrawVerts; i++ )
217 VectorScale( bspDrawVerts[ i ].xyz, scale, bspDrawVerts[ i ].xyz );
220 for( i = 0; i < numBSPPlanes; i++ )
221 bspPlanes[ i ].dist *= scale;
224 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
225 if( (vec[ 0 ] + vec[ 1 ] + vec[ 2 ]) == 0.0f )
226 VectorCopy( gridSize, vec );
227 VectorScale( vec, scale, vec );
228 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
229 SetKeyValue( &entities[ 0 ], "gridsize", str );
233 StripExtension( source );
234 DefaultExtension( source, "_s.bsp" );
235 Sys_Printf( "Writing %s\n", source );
236 WriteBSPFile( source );
238 /* return to sender */
246 main argument processing function for bsp conversion
249 int ConvertBSPMain( int argc, char **argv )
252 int (*convertFunc)( char * );
256 convertFunc = ConvertBSPToASE;
261 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
265 /* process arguments */
266 for( i = 1; i < (argc - 1); i++ )
268 /* -format map|ase|... */
269 if( !strcmp( argv[ i ], "-format" ) )
272 if( !Q_stricmp( argv[ i ], "ase" ) )
273 convertFunc = ConvertBSPToASE;
274 else if( !Q_stricmp( argv[ i ], "map" ) )
275 convertFunc = ConvertBSPToMap;
277 Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
281 /* clean up map name */
282 strcpy( source, ExpandArg( argv[ i ] ) );
283 StripExtension( source );
284 DefaultExtension( source, ".bsp" );
288 Sys_Printf( "Loading %s\n", source );
290 /* ydnar: load surface file */
291 //% LoadSurfaceExtraFile( source );
293 LoadBSPFile( source );
295 /* parse bsp entities */
299 return convertFunc( source );
309 int main( int argc, char **argv )
315 /* we want consistent 'randomness' */
319 start = I_FloatTime();
321 /* this was changed to emit version number over the network */
322 printf( Q3MAP_VERSION "\n" );
327 /* read general options first */
328 for( i = 1; i < argc; i++ )
331 if( !strcmp( argv[ i ], "-connect" ) )
335 Broadcast_Setup( argv[ i ] );
340 else if( !strcmp( argv[ i ], "-v" ) )
347 else if( !strcmp( argv[ i ], "-force" ) )
353 /* patch subdivisions */
354 else if( !strcmp( argv[ i ], "-subdivisions" ) )
358 patchSubdivisions = atoi( argv[ i ] );
360 if( patchSubdivisions <= 0 )
361 patchSubdivisions = 1;
365 else if( !strcmp( argv[ i ], "-threads" ) )
369 numthreads = atoi( argv[ i ] );
374 /* init model library */
376 PicoSetMallocFunc( safe_malloc );
377 PicoSetFreeFunc( free );
378 PicoSetPrintFunc( PicoPrintFunc );
379 PicoSetLoadFileFunc( PicoLoadFileFunc );
380 PicoSetFreeFileFunc( free );
382 /* set number of threads */
385 /* generate sinusoid jitter table */
386 for( i = 0; i < MAX_JITTERS; i++ )
388 jitters[ i ] = sin( i * 139.54152147 );
389 //% Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
392 /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
393 and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
395 Sys_Printf( "Q3Map - v1.0r (c) 1999 Id Software Inc.\n" );
396 Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
397 Sys_Printf( "GtkRadiant - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
398 Sys_Printf( "%s\n", Q3MAP_MOTD );
400 /* ydnar: new path initialization */
401 InitPaths( &argc, argv );
403 /* check if we have enough options left to attempt something */
405 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
408 if( !strcmp( argv[ 1 ], "-info" ) )
409 r = BSPInfo( argc - 2, argv + 2 );
412 else if( !strcmp( argv[ 1 ], "-vis" ) )
413 r = VisMain( argc - 1, argv + 1 );
416 else if( !strcmp( argv[ 1 ], "-light" ) )
417 r = LightMain( argc - 1, argv + 1 );
420 else if( !strcmp( argv[ 1 ], "-vlight" ) )
422 Sys_Printf( "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
423 argv[ 1 ] = "-fast"; /* eek a hack */
424 r = LightMain( argc, argv );
427 /* ydnar: lightmap export */
428 else if( !strcmp( argv[ 1 ], "-export" ) )
429 r = ExportLightmapsMain( argc - 1, argv + 1 );
431 /* ydnar: lightmap import */
432 else if( !strcmp( argv[ 1 ], "-import" ) )
433 r = ImportLightmapsMain( argc - 1, argv + 1 );
435 /* ydnar: bsp scaling */
436 else if( !strcmp( argv[ 1 ], "-scale" ) )
437 r = ScaleBSPMain( argc - 1, argv + 1 );
439 /* ydnar: bsp conversion */
440 else if( !strcmp( argv[ 1 ], "-convert" ) )
441 r = ConvertBSPMain( argc - 1, argv + 1 );
443 /* ydnar: otherwise create a bsp */
445 r = BSPMain( argc, argv );
449 Sys_Printf( "%9.0f seconds elapsed\n", end - start );
451 /* shut down connection */
452 Broadcast_Shutdown();
454 /* return any error code */