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 ------------------------------------------------------------------------------- */
42 returns a pseudorandom number between 0 and 1
46 return (vec_t) rand() / RAND_MAX;
50 char *Q_strncpyz( char *dst, const char *src, size_t len ) {
55 strncpy( dst, src, len );
56 dst[ len - 1 ] = '\0';
61 char *Q_strcat( char *dst, size_t dlen, const char *src ) {
62 size_t n = strlen( dst );
65 abort(); /* buffer overflow */
68 return Q_strncpyz( dst + n, src, dlen - n );
72 char *Q_strncat( char *dst, size_t dlen, const char *src, size_t slen ) {
73 size_t n = strlen( dst );
76 abort(); /* buffer overflow */
79 return Q_strncpyz( dst + n, src, MIN( slen, dlen - n ) );
88 static void ExitQ3Map( void ){
90 if ( mapDrawSurfs != NULL ) {
98 shifts a map: works correctly only with axial faces, placed in positive half of axis
99 for testing physics with huge coordinates
102 int ShiftBSPMain( int argc, char **argv ){
110 float *old_xyzst = NULL;
116 Sys_Printf( "Usage: q3map [-v] -shift [-tex] [-spawn_ref <value>] <value> <mapname>\n" );
121 for ( i = 1; i < argc - 2; ++i )
123 if ( !strcmp( argv[i], "-tex" ) ) {
126 else if ( !strcmp( argv[i], "-spawn_ref" ) ) {
127 spawn_ref = atof( argv[i + 1] );
136 // if(argc-2 >= i) // always true
137 scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
138 if ( argc - 3 >= i ) {
139 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
141 if ( argc - 4 >= i ) {
142 scale[0] = atof( argv[ argc - 4 ] );
145 uniform = ( ( scale[0] == scale[1] ) && ( scale[1] == scale[2] ) );
148 /* do some path mangling */
149 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
150 StripExtension( source );
151 DefaultExtension( source, ".bsp" );
154 Sys_Printf( "Loading %s\n", source );
155 LoadBSPFile( source );
159 Sys_Printf( "--- ShiftBSP ---\n" );
160 Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
162 /* shift entity keys */
163 for ( i = 0; i < numBSPEntities && i < numEntities; i++ )
166 GetVectorForKey( &entities[ i ], "origin", vec );
167 if ( ( vec[ 0 ] || vec[ 1 ] || vec[ 2 ] ) ) {
168 if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
174 if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
177 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
178 SetKeyValue( &entities[ i ], "origin", str );
184 for ( i = 0; i < numBSPModels; i++ )
186 bspModels[ i ].mins[0] += scale[0];
187 bspModels[ i ].mins[1] += scale[1];
188 bspModels[ i ].mins[2] += scale[2];
189 bspModels[ i ].maxs[0] += scale[0];
190 bspModels[ i ].maxs[1] += scale[1];
191 bspModels[ i ].maxs[2] += scale[2];
195 for ( i = 0; i < numBSPNodes; i++ )
197 bspNodes[ i ].mins[0] += scale[0];
198 bspNodes[ i ].mins[1] += scale[1];
199 bspNodes[ i ].mins[2] += scale[2];
200 bspNodes[ i ].maxs[0] += scale[0];
201 bspNodes[ i ].maxs[1] += scale[1];
202 bspNodes[ i ].maxs[2] += scale[2];
206 for ( i = 0; i < numBSPLeafs; i++ )
208 bspLeafs[ i ].mins[0] += scale[0];
209 bspLeafs[ i ].mins[1] += scale[1];
210 bspLeafs[ i ].mins[2] += scale[2];
211 bspLeafs[ i ].maxs[0] += scale[0];
212 bspLeafs[ i ].maxs[1] += scale[1];
213 bspLeafs[ i ].maxs[2] += scale[2];
216 /* shift drawverts */
217 for ( i = 0; i < numBSPDrawVerts; i++ )
219 bspDrawVerts[i].xyz[0] += scale[0];
220 bspDrawVerts[i].xyz[1] += scale[1];
221 bspDrawVerts[i].xyz[2] += scale[2];
228 for ( i = 0; i < numBSPPlanes; i++ )
230 //find point on plane
231 for ( j=0; j<3; j++ ){
232 if ( fabs( bspPlanes[ i ].normal[j] ) > 0.5 ){
233 point[j] = bspPlanes[ i ].dist / bspPlanes[ i ].normal[j];
234 point[(j+1)%3] = point[(j+2)%3] = 0;
239 for ( j=0; j<3; j++ ){
240 point[j] += scale[j];
242 //calc new plane dist
243 bspPlanes[ i ].dist = DotProduct( point, bspPlanes[ i ].normal );
248 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
249 if ( ( vec[ 0 ] + vec[ 1 ] + vec[ 2 ] ) == 0.0f ) {
250 VectorCopy( gridSize, vec );
255 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
256 SetKeyValue( &entities[ 0 ], "gridsize", str );
258 /* inject command line parameters */
259 InjectCommandLine( argv, 0, argc - 1 );
263 StripExtension( source );
264 DefaultExtension( source, "_sh.bsp" );
265 Sys_Printf( "Writing %s\n", source );
266 WriteBSPFile( source );
268 /* return to sender */
279 int main( int argc, char **argv ){
282 extern qboolean werror;
285 /* we want consistent 'randomness' */
289 start = I_FloatTime();
291 /* this was changed to emit version number over the network */
292 printf( Q3MAP_VERSION "\n" );
297 /* read general options first */
298 for ( i = 1; i < argc; i++ )
301 if ( !strcmp( argv[ i ], "-h" ) || !strcmp( argv[ i ], "--help" )
302 || !strcmp( argv[ i ], "-help" ) ) {
308 if ( !strcmp( argv[ i ], "-connect" ) ) {
311 Broadcast_Setup( argv[ i ] );
316 else if ( !strcmp( argv[ i ], "-v" ) ) {
324 else if ( !strcmp( argv[ i ], "-force" ) ) {
329 /* make all warnings into errors */
330 else if ( !strcmp( argv[ i ], "-werror" ) ) {
335 /* patch subdivisions */
336 else if ( !strcmp( argv[ i ], "-subdivisions" ) ) {
339 patchSubdivisions = atoi( argv[ i ] );
341 if ( patchSubdivisions <= 0 ) {
342 patchSubdivisions = 1;
347 else if ( !strcmp( argv[ i ], "-threads" ) ) {
350 numthreads = atoi( argv[ i ] );
353 else if( !strcmp( argv[ i ], "-nocmdline" ) )
355 Sys_Printf( "noCmdLine\n" );
362 /* init model library */
364 PicoSetMallocFunc( safe_malloc );
365 PicoSetFreeFunc( free );
366 PicoSetPrintFunc( PicoPrintFunc );
367 PicoSetLoadFileFunc( PicoLoadFileFunc );
368 PicoSetFreeFileFunc( free );
370 /* set number of threads */
373 /* generate sinusoid jitter table */
374 for ( i = 0; i < MAX_JITTERS; i++ )
376 jitters[ i ] = sin( i * 139.54152147 );
377 //% Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
380 /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
381 and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
383 Sys_Printf( "Q3Map - v1.0r (c) 1999 Id Software Inc.\n" );
384 Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
385 Sys_Printf( "NetRadiant - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
386 Sys_Printf( "%s\n", Q3MAP_MOTD );
388 /* ydnar: new path initialization */
389 InitPaths( &argc, argv );
391 /* set game options */
392 if ( !patchSubdivisions ) {
393 patchSubdivisions = game->patchSubdivisions;
396 /* check if we have enough options left to attempt something */
398 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
402 if ( !strcmp( argv[ 1 ], "-fixaas" ) ) {
403 r = FixAASMain( argc - 1, argv + 1 );
407 else if ( !strcmp( argv[ 1 ], "-analyze" ) ) {
408 r = AnalyzeBSPMain( argc - 1, argv + 1 );
412 else if ( !strcmp( argv[ 1 ], "-info" ) ) {
413 r = BSPInfoMain( argc - 2, argv + 2 );
417 else if ( !strcmp( argv[ 1 ], "-vis" ) ) {
418 r = VisMain( argc - 1, argv + 1 );
422 else if ( !strcmp( argv[ 1 ], "-light" ) ) {
423 r = LightMain( argc - 1, argv + 1 );
427 else if ( !strcmp( argv[ 1 ], "-vlight" ) ) {
428 Sys_FPrintf( SYS_WRN, "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
429 argv[ 1 ] = "-fast"; /* eek a hack */
430 r = LightMain( argc, argv );
433 /* QBall: export entities */
434 else if ( !strcmp( argv[ 1 ], "-exportents" ) ) {
435 r = ExportEntitiesMain( argc - 1, argv + 1 );
438 /* ydnar: lightmap export */
439 else if ( !strcmp( argv[ 1 ], "-export" ) ) {
440 r = ExportLightmapsMain( argc - 1, argv + 1 );
443 /* ydnar: lightmap import */
444 else if ( !strcmp( argv[ 1 ], "-import" ) ) {
445 r = ImportLightmapsMain( argc - 1, argv + 1 );
448 /* ydnar: bsp scaling */
449 else if ( !strcmp( argv[ 1 ], "-scale" ) ) {
450 r = ScaleBSPMain( argc - 1, argv + 1 );
454 else if ( !strcmp( argv[ 1 ], "-shift" ) ) {
455 r = ShiftBSPMain( argc - 1, argv + 1 );
458 /* ydnar: bsp conversion */
459 else if ( !strcmp( argv[ 1 ], "-convert" ) ) {
460 r = ConvertBSPMain( argc - 1, argv + 1 );
464 else if ( !strcmp( argv[ 1 ], "-minimap" ) ) {
465 r = MiniMapBSPMain( argc - 1, argv + 1 );
468 /* ydnar: otherwise create a bsp */
470 r = BSPMain( argc, argv );
475 Sys_Printf( "%9.0f seconds elapsed\n", end - start );
477 /* shut down connection */
478 Broadcast_Shutdown();
480 /* return any error code */