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 /* scale 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];
217 Sys_Printf( "Using texture unlocking (and probably breaking texture alignment a lot)\n" );
218 old_xyzst = safe_malloc( sizeof( *old_xyzst ) * numBSPDrawVerts * 5 );
219 for ( i = 0; i < numBSPDrawVerts; i++ )
221 old_xyzst[5 * i + 0] = bspDrawVerts[i].xyz[0];
222 old_xyzst[5 * i + 1] = bspDrawVerts[i].xyz[1];
223 old_xyzst[5 * i + 2] = bspDrawVerts[i].xyz[2];
224 old_xyzst[5 * i + 3] = bspDrawVerts[i].st[0];
225 old_xyzst[5 * i + 4] = bspDrawVerts[i].st[1];
229 /* scale drawverts */
230 for ( i = 0; i < numBSPDrawVerts; i++ )
232 bspDrawVerts[i].xyz[0] += scale[0];
233 bspDrawVerts[i].xyz[1] += scale[1];
234 bspDrawVerts[i].xyz[2] += scale[2];
235 // bspDrawVerts[i].normal[0] /= scale[0];
236 // bspDrawVerts[i].normal[1] /= scale[1];
237 // bspDrawVerts[i].normal[2] /= scale[2];
238 // VectorNormalize( bspDrawVerts[i].normal, bspDrawVerts[i].normal );
242 for ( i = 0; i < numBSPDrawSurfaces; i++ )
244 switch ( bspDrawSurfaces[i].surfaceType )
248 if ( bspDrawSurfaces[i].numIndexes % 3 ) {
249 Error( "Not a triangulation!" );
251 for ( j = bspDrawSurfaces[i].firstIndex; j < bspDrawSurfaces[i].firstIndex + bspDrawSurfaces[i].numIndexes; j += 3 )
253 int ia = bspDrawIndexes[j] + bspDrawSurfaces[i].firstVert, ib = bspDrawIndexes[j + 1] + bspDrawSurfaces[i].firstVert, ic = bspDrawIndexes[j + 2] + bspDrawSurfaces[i].firstVert;
254 bspDrawVert_t *a = &bspDrawVerts[ia], *b = &bspDrawVerts[ib], *c = &bspDrawVerts[ic];
255 float *oa = &old_xyzst[ia * 5], *ob = &old_xyzst[ib * 5], *oc = &old_xyzst[ic * 5];
260 ExtrapolateTexcoords(
275 for ( i = 0; i < numBSPPlanes; i++ )
277 if ( bspPlanes[ i ].dist > 0 ){
278 if ( bspPlanes[ i ].normal[0] ){
279 bspPlanes[ i ].dist += scale[0];
282 else if ( bspPlanes[ i ].normal[1] ){
283 bspPlanes[ i ].dist += scale[1];
286 else if ( bspPlanes[ i ].normal[2] ){
287 bspPlanes[ i ].dist += scale[2];
292 if ( bspPlanes[ i ].normal[0] ){
293 bspPlanes[ i ].dist -= scale[0];
296 else if ( bspPlanes[ i ].normal[1] ){
297 bspPlanes[ i ].dist -= scale[1];
300 else if ( bspPlanes[ i ].normal[2] ){
301 bspPlanes[ i ].dist -= scale[2];
309 for ( i = 0; i < numBSPPlanes; i++ )
311 bspPlanes[ i ].dist += scale[0];
316 for ( i = 0; i < numBSPPlanes; i++ )
318 // bspPlanes[ i ].normal[0] /= scale[0];
319 // bspPlanes[ i ].normal[1] /= scale[1];
320 // bspPlanes[ i ].normal[2] /= scale[2];
321 f = 1 / VectorLength( bspPlanes[i].normal );
322 VectorScale( bspPlanes[i].normal, f, bspPlanes[i].normal );
323 bspPlanes[ i ].dist *= f;
329 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
330 if ( ( vec[ 0 ] + vec[ 1 ] + vec[ 2 ] ) == 0.0f ) {
331 VectorCopy( gridSize, vec );
336 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
337 SetKeyValue( &entities[ 0 ], "gridsize", str );
339 /* inject command line parameters */
340 InjectCommandLine( argv, 0, argc - 1 );
344 StripExtension( source );
345 DefaultExtension( source, "_sh.bsp" );
346 Sys_Printf( "Writing %s\n", source );
347 WriteBSPFile( source );
349 /* return to sender */
360 int main( int argc, char **argv ){
363 extern qboolean werror;
366 /* we want consistent 'randomness' */
370 start = I_FloatTime();
372 /* this was changed to emit version number over the network */
373 printf( Q3MAP_VERSION "\n" );
378 /* read general options first */
379 for ( i = 1; i < argc; i++ )
382 if ( !strcmp( argv[ i ], "-h" ) || !strcmp( argv[ i ], "--help" )
383 || !strcmp( argv[ i ], "-help" ) ) {
389 if ( !strcmp( argv[ i ], "-connect" ) ) {
392 Broadcast_Setup( argv[ i ] );
397 else if ( !strcmp( argv[ i ], "-v" ) ) {
405 else if ( !strcmp( argv[ i ], "-force" ) ) {
410 /* make all warnings into errors */
411 else if ( !strcmp( argv[ i ], "-werror" ) ) {
416 /* patch subdivisions */
417 else if ( !strcmp( argv[ i ], "-subdivisions" ) ) {
420 patchSubdivisions = atoi( argv[ i ] );
422 if ( patchSubdivisions <= 0 ) {
423 patchSubdivisions = 1;
428 else if ( !strcmp( argv[ i ], "-threads" ) ) {
431 numthreads = atoi( argv[ i ] );
434 else if( !strcmp( argv[ i ], "-nocmdline" ) )
436 Sys_Printf( "noCmdLine\n" );
443 /* init model library */
445 PicoSetMallocFunc( safe_malloc );
446 PicoSetFreeFunc( free );
447 PicoSetPrintFunc( PicoPrintFunc );
448 PicoSetLoadFileFunc( PicoLoadFileFunc );
449 PicoSetFreeFileFunc( free );
451 /* set number of threads */
454 /* generate sinusoid jitter table */
455 for ( i = 0; i < MAX_JITTERS; i++ )
457 jitters[ i ] = sin( i * 139.54152147 );
458 //% Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
461 /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
462 and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
464 Sys_Printf( "Q3Map - v1.0r (c) 1999 Id Software Inc.\n" );
465 Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
466 Sys_Printf( "NetRadiant - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
467 Sys_Printf( "%s\n", Q3MAP_MOTD );
469 /* ydnar: new path initialization */
470 InitPaths( &argc, argv );
472 /* set game options */
473 if ( !patchSubdivisions ) {
474 patchSubdivisions = game->patchSubdivisions;
477 /* check if we have enough options left to attempt something */
479 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
483 if ( !strcmp( argv[ 1 ], "-fixaas" ) ) {
484 r = FixAASMain( argc - 1, argv + 1 );
488 else if ( !strcmp( argv[ 1 ], "-analyze" ) ) {
489 r = AnalyzeBSPMain( argc - 1, argv + 1 );
493 else if ( !strcmp( argv[ 1 ], "-info" ) ) {
494 r = BSPInfoMain( argc - 2, argv + 2 );
498 else if ( !strcmp( argv[ 1 ], "-vis" ) ) {
499 r = VisMain( argc - 1, argv + 1 );
503 else if ( !strcmp( argv[ 1 ], "-light" ) ) {
504 r = LightMain( argc - 1, argv + 1 );
508 else if ( !strcmp( argv[ 1 ], "-vlight" ) ) {
509 Sys_FPrintf( SYS_WRN, "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
510 argv[ 1 ] = "-fast"; /* eek a hack */
511 r = LightMain( argc, argv );
514 /* QBall: export entities */
515 else if ( !strcmp( argv[ 1 ], "-exportents" ) ) {
516 r = ExportEntitiesMain( argc - 1, argv + 1 );
519 /* ydnar: lightmap export */
520 else if ( !strcmp( argv[ 1 ], "-export" ) ) {
521 r = ExportLightmapsMain( argc - 1, argv + 1 );
524 /* ydnar: lightmap import */
525 else if ( !strcmp( argv[ 1 ], "-import" ) ) {
526 r = ImportLightmapsMain( argc - 1, argv + 1 );
529 /* ydnar: bsp scaling */
530 else if ( !strcmp( argv[ 1 ], "-scale" ) ) {
531 r = ScaleBSPMain( argc - 1, argv + 1 );
535 else if ( !strcmp( argv[ 1 ], "-shift" ) ) {
536 r = ShiftBSPMain( argc - 1, argv + 1 );
539 /* ydnar: bsp conversion */
540 else if ( !strcmp( argv[ 1 ], "-convert" ) ) {
541 r = ConvertBSPMain( argc - 1, argv + 1 );
545 else if ( !strcmp( argv[ 1 ], "-minimap" ) ) {
546 r = MiniMapBSPMain( argc - 1, argv + 1 );
549 /* ydnar: otherwise create a bsp */
551 r = BSPMain( argc, argv );
556 Sys_Printf( "%9.0f seconds elapsed\n", end - start );
558 /* shut down connection */
559 Broadcast_Shutdown();
561 /* return any error code */