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 ------------------------------------------------------------------------------- */
43 returns a pseudorandom number between 0 and 1
48 return (vec_t) rand() / RAND_MAX;
58 static void ExitQ3Map( void )
61 if( mapDrawSurfs != NULL )
69 typedef struct minimap_s
75 float *sample_offsets;
76 float sharpen_boxmult;
77 float sharpen_centermult;
83 static minimap_t minimap;
85 qboolean BrushIntersectionWithLine(bspBrush_t *brush, vec3_t start, vec3_t dir, float *t_in, float *t_out)
88 qboolean in = qfalse, out = qfalse;
89 bspBrushSide_t *sides = &bspBrushSides[brush->firstSide];
91 for(i = 0; i < brush->numSides; ++i)
93 bspPlane_t *p = &bspPlanes[sides[i].planeNum];
94 float sn = DotProduct(start, p->normal);
95 float dn = DotProduct(dir, p->normal);
99 return qfalse; // outside!
103 float t = (p->dist - sn) / dn;
110 // as t_in can only increase, and t_out can only decrease, early out
111 if(out && *t_in >= *t_out)
117 if(!out || t < *t_out)
121 // as t_in can only increase, and t_out can only decrease, early out
122 if(in && *t_in >= *t_out)
131 static float MiniMapSample(float x, float y)
150 for(i = 0; i < minimap.model->numBSPBrushes; ++i)
152 bi = minimap.model->firstBSPBrush + i;
153 if(opaqueBrushes[bi >> 3] & (1 << (bi & 7)))
157 // sort out mins/maxs of the brush
158 s = &bspBrushSides[b->firstSide];
159 if(x < -bspPlanes[s[0].planeNum].dist)
161 if(x > +bspPlanes[s[1].planeNum].dist)
163 if(y < -bspPlanes[s[2].planeNum].dist)
165 if(y > +bspPlanes[s[3].planeNum].dist)
168 if(BrushIntersectionWithLine(b, org, dir, &t0, &t1))
179 void RandomVector2f(float v[2])
183 v[0] = 2 * Random() - 1;
184 v[1] = 2 * Random() - 1;
186 while(v[0] * v[0] + v[1] * v[1] > 1);
189 static void MiniMapRandomlySupersampled(int y)
192 float *p = &minimap.data1f[y * minimap.width];
193 float ymin = minimap.mins[1] + minimap.size[1] * (y / (float) minimap.height);
194 float dx = minimap.size[0] / (float) minimap.width;
195 float dy = minimap.size[1] / (float) minimap.height;
199 for(x = 0; x < minimap.width; ++x)
201 float xmin = minimap.mins[0] + minimap.size[0] * (x / (float) minimap.width);
204 for(i = 0; i < minimap.samples; ++i)
207 thisval = MiniMapSample(
208 xmin + (uv[0] + 0.5) * dx, /* exaggerated random pattern for better results */
209 ymin + (uv[1] + 0.5) * dy /* exaggerated random pattern for better results */
213 val /= minimap.samples * minimap.size[2];
218 static void MiniMapSupersampled(int y)
221 float *p = &minimap.data1f[y * minimap.width];
222 float ymin = minimap.mins[1] + minimap.size[1] * (y / (float) minimap.height);
223 float dx = minimap.size[0] / (float) minimap.width;
224 float dy = minimap.size[1] / (float) minimap.height;
226 for(x = 0; x < minimap.width; ++x)
228 float xmin = minimap.mins[0] + minimap.size[0] * (x / (float) minimap.width);
231 for(i = 0; i < minimap.samples; ++i)
233 float thisval = MiniMapSample(
234 xmin + minimap.sample_offsets[2*i+0] * dx,
235 ymin + minimap.sample_offsets[2*i+1] * dy
239 val /= minimap.samples * minimap.size[2];
244 static void MiniMapNoSupersampling(int y)
247 float *p = &minimap.data1f[y * minimap.width];
248 float ymin = minimap.mins[1] + minimap.size[1] * ((y + 0.5) / (float) minimap.height);
250 for(x = 0; x < minimap.width; ++x)
252 float xmin = minimap.mins[0] + minimap.size[0] * ((x + 0.5) / (float) minimap.width);
253 *p++ = MiniMapSample(xmin, ymin) / minimap.size[2];
257 static void MiniMapSharpen(int y)
260 qboolean up = (y > 0);
261 qboolean down = (y < minimap.height - 1);
262 float *p = &minimap.data1f[y * minimap.width];
263 float *q = &minimap.sharpendata1f[y * minimap.width];
265 for(x = 0; x < minimap.width; ++x)
267 qboolean left = (x > 0);
268 qboolean right = (x < minimap.width - 1);
269 float val = p[0] * minimap.sharpen_centermult;
272 val += p[-1 -minimap.width] * minimap.sharpen_boxmult;
274 val += p[-1 +minimap.width] * minimap.sharpen_boxmult;
276 val += p[+1 -minimap.width] * minimap.sharpen_boxmult;
278 val += p[+1 +minimap.width] * minimap.sharpen_boxmult;
281 val += p[-1] * minimap.sharpen_boxmult;
283 val += p[+1] * minimap.sharpen_boxmult;
285 val += p[-minimap.width] * minimap.sharpen_boxmult;
287 val += p[+minimap.width] * minimap.sharpen_boxmult;
294 void MiniMapMakeMinsMaxs(vec3_t mins_in, vec3_t maxs_in, float border, qboolean keepaspect)
296 vec3_t mins, maxs, extend;
297 VectorCopy(mins_in, mins);
298 VectorCopy(maxs_in, maxs);
300 // line compatible to nexuiz mapinfo
301 Sys_Printf("size %f %f %f %f %f %f\n", mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]);
305 VectorSubtract(maxs, mins, extend);
306 if(extend[1] > extend[0])
308 mins[0] -= (extend[1] - extend[0]) * 0.5;
309 maxs[0] += (extend[1] - extend[0]) * 0.5;
313 mins[1] -= (extend[0] - extend[1]) * 0.5;
314 maxs[1] += (extend[0] - extend[1]) * 0.5;
318 /* border: amount of black area around the image */
319 /* input: border, 1-2*border, border but we need border/(1-2*border) */
321 VectorSubtract(maxs, mins, extend);
322 VectorScale(extend, border / (1 - 2 * border), extend);
324 VectorSubtract(mins, extend, mins);
325 VectorAdd(maxs, extend, maxs);
327 VectorCopy(mins, minimap.mins);
328 VectorSubtract(maxs, mins, minimap.size);
330 // line compatible to nexuiz mapinfo
331 Sys_Printf("size_texcoords %f %f %f %f %f %f\n", mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]);
335 MiniMapSetupBrushes()
336 determines solid non-sky brushes in the world
339 void MiniMapSetupBrushes( void )
341 int i, b, compileFlags;
348 Sys_FPrintf( SYS_VRB, "--- MiniMapSetupBrushes ---\n" );
351 if( opaqueBrushes == NULL )
352 opaqueBrushes = safe_malloc( numBSPBrushes / 8 + 1 );
355 memset( opaqueBrushes, 0, numBSPBrushes / 8 + 1 );
356 numOpaqueBrushes = 0;
358 /* walk the list of worldspawn brushes */
359 for( i = 0; i < minimap.model->numBSPBrushes; i++ )
362 b = minimap.model->firstBSPBrush + i;
363 brush = &bspBrushes[ b ];
366 /* check all sides */
368 for( j = 0; j < brush->numSides; j++ )
370 /* do bsp shader calculations */
371 side = &bspBrushSides[ brush->firstSide + j ];
372 shader = &bspShaders[ side->shaderNum ];
374 /* get shader info */
375 si = ShaderInfoForShader( shader->shader );
379 /* or together compile flags */
380 compileFlags |= si->compileFlags;
383 shader = &bspShaders[ brush->shaderNum ];
384 si = ShaderInfoForShader( shader->shader );
388 compileFlags = si->compileFlags;
391 /* determine if this brush is solid */
392 if( (compileFlags & (C_SOLID | C_SKY)) == C_SOLID )
394 opaqueBrushes[ b >> 3 ] |= (1 << (b & 7));
400 /* emit some statistics */
401 Sys_FPrintf( SYS_VRB, "%9d solid brushes\n", numOpaqueBrushes );
404 qboolean MiniMapEvaluateSampleOffsets(int *bestj, int *bestk, float *bestval)
409 *bestj = *bestk = -1;
410 *bestval = 3; /* max possible val is 2 */
412 for(j = 0; j < minimap.samples; ++j)
413 for(k = j + 1; k < minimap.samples; ++k)
415 dx = minimap.sample_offsets[2*j+0] - minimap.sample_offsets[2*k+0];
416 dy = minimap.sample_offsets[2*j+1] - minimap.sample_offsets[2*k+1];
417 if(dx > +0.5) dx -= 1;
418 if(dx < -0.5) dx += 1;
419 if(dy > +0.5) dy -= 1;
420 if(dy < -0.5) dy += 1;
421 val = dx * dx + dy * dy;
433 void MiniMapMakeSampleOffsets()
436 float val, valj, valk, sx, sy, rx, ry;
438 Sys_Printf( "Generating good sample offsets (this may take a while)...\n" );
440 /* start with entirely random samples */
441 for(i = 0; i < minimap.samples; ++i)
443 minimap.sample_offsets[2*i+0] = Random();
444 minimap.sample_offsets[2*i+1] = Random();
447 for(i = 0; i < 1000; ++i)
449 if(MiniMapEvaluateSampleOffsets(&j, &k, &val))
451 sx = minimap.sample_offsets[2*j+0];
452 sy = minimap.sample_offsets[2*j+1];
453 minimap.sample_offsets[2*j+0] = rx = Random();
454 minimap.sample_offsets[2*j+1] = ry = Random();
455 if(!MiniMapEvaluateSampleOffsets(&jj, &kk, &valj))
457 minimap.sample_offsets[2*j+0] = sx;
458 minimap.sample_offsets[2*j+1] = sy;
460 sx = minimap.sample_offsets[2*k+0];
461 sy = minimap.sample_offsets[2*k+1];
462 minimap.sample_offsets[2*k+0] = rx;
463 minimap.sample_offsets[2*k+1] = ry;
464 if(!MiniMapEvaluateSampleOffsets(&jj, &kk, &valk))
466 minimap.sample_offsets[2*k+0] = sx;
467 minimap.sample_offsets[2*k+1] = sy;
473 /* valj is the greatest */
474 minimap.sample_offsets[2*j+0] = rx;
475 minimap.sample_offsets[2*j+1] = ry;
480 /* valj is the greater and it is useless - forget it */
487 /* valk is the greatest */
488 minimap.sample_offsets[2*k+0] = rx;
489 minimap.sample_offsets[2*k+1] = ry;
494 /* valk is the greater and it is useless - forget it */
503 void MergeRelativePath(char *out, const char *absolute, const char *relative)
505 const char *endpos = absolute + strlen(absolute);
506 while(endpos != absolute && (endpos[-1] == '/' || endpos[-1] == '\\'))
508 while(relative[0] == '.' && relative[1] == '.' && (relative[2] == '/' || relative[2] == '\\'))
511 while(endpos != absolute)
514 if(*endpos == '/' || *endpos == '\\')
517 while(endpos != absolute && (endpos[-1] == '/' || endpos[-1] == '\\'))
520 memcpy(out, absolute, endpos - absolute);
521 out[endpos - absolute] = '/';
522 strcpy(out + (endpos - absolute + 1), relative);
525 int MiniMapBSPMain( int argc, char **argv )
527 char minimapFilename[1024];
530 char relativeMinimapFilename[1024];
531 float minimapSharpen;
544 Sys_Printf( "Usage: q3map [-v] -minimap [-size n] [-sharpen f] [-samples n | -random n] [-o filename.tga] [-minmax Xmin Ymin Zmin Xmax Ymax Zmax] <mapname>\n" );
548 /* load the BSP first */
549 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
550 StripExtension( source );
551 DefaultExtension( source, ".bsp" );
552 Sys_Printf( "Loading %s\n", source );
553 BeginMapShaderFile( source );
555 LoadBSPFile( source );
557 minimap.model = &bspModels[0];
558 VectorCopy(minimap.model->mins, mins);
559 VectorCopy(minimap.model->maxs, maxs);
561 *minimapFilename = 0;
562 minimapSharpen = game->miniMapSharpen;
563 minimap.width = minimap.height = game->miniMapSize;
564 border = game->miniMapBorder;
565 keepaspect = game->miniMapKeepAspect;
566 mode = game->miniMapMode;
569 minimap.sample_offsets = NULL;
571 /* process arguments */
572 for( i = 1; i < (argc - 1); i++ )
574 if( !strcmp( argv[ i ], "-size" ) )
576 minimap.width = minimap.height = atoi(argv[i + 1]);
578 Sys_Printf( "Image size set to %i\n", minimap.width );
580 else if( !strcmp( argv[ i ], "-sharpen" ) )
582 minimapSharpen = atof(argv[i + 1]);
584 Sys_Printf( "Sharpening coefficient set to %f\n", minimapSharpen );
586 else if( !strcmp( argv[ i ], "-samples" ) )
588 minimap.samples = atoi(argv[i + 1]);
590 Sys_Printf( "Samples set to %i\n", minimap.samples );
591 if(minimap.sample_offsets)
592 free(minimap.sample_offsets);
593 minimap.sample_offsets = malloc(2 * sizeof(*minimap.sample_offsets) * minimap.samples);
594 MiniMapMakeSampleOffsets();
596 else if( !strcmp( argv[ i ], "-random" ) )
598 minimap.samples = atoi(argv[i + 1]);
600 Sys_Printf( "Random samples set to %i\n", minimap.samples );
601 if(minimap.sample_offsets)
602 free(minimap.sample_offsets);
603 minimap.sample_offsets = NULL;
605 else if( !strcmp( argv[ i ], "-border" ) )
607 border = atof(argv[i + 1]);
609 Sys_Printf( "Border set to %f\n", border );
611 else if( !strcmp( argv[ i ], "-keepaspect" ) )
614 Sys_Printf( "Keeping aspect ratio by letterboxing\n", border );
616 else if( !strcmp( argv[ i ], "-nokeepaspect" ) )
619 Sys_Printf( "Not keeping aspect ratio\n", border );
621 else if( !strcmp( argv[ i ], "-o" ) )
623 strcpy(minimapFilename, argv[i + 1]);
625 Sys_Printf( "Output file name set to %s\n", minimapFilename );
627 else if( !strcmp( argv[ i ], "-minmax" ) && i < (argc - 7) )
629 mins[0] = atof(argv[i + 1]);
630 mins[1] = atof(argv[i + 2]);
631 mins[2] = atof(argv[i + 3]);
632 maxs[0] = atof(argv[i + 4]);
633 maxs[1] = atof(argv[i + 5]);
634 maxs[2] = atof(argv[i + 6]);
636 Sys_Printf( "Map mins/maxs overridden\n" );
638 else if( !strcmp( argv[ i ], "-gray" ) )
640 mode = MINIMAP_MODE_GRAY;
641 Sys_Printf( "Writing as white-on-black image\n" );
643 else if( !strcmp( argv[ i ], "-black" ) )
645 mode = MINIMAP_MODE_BLACK;
646 Sys_Printf( "Writing as black alpha image\n" );
648 else if( !strcmp( argv[ i ], "-white" ) )
650 mode = MINIMAP_MODE_WHITE;
651 Sys_Printf( "Writing as white alpha image\n" );
655 MiniMapMakeMinsMaxs(mins, maxs, border, keepaspect);
657 if(!*minimapFilename)
659 ExtractFileBase(source, basename);
660 ExtractFilePath(source, path);
661 sprintf(relativeMinimapFilename, game->miniMapNameFormat, basename);
662 MergeRelativePath(minimapFilename, path, relativeMinimapFilename);
663 Sys_Printf("Output file name automatically set to %s\n", minimapFilename);
665 ExtractFilePath(minimapFilename, path);
668 if(minimapSharpen >= 0)
670 minimap.sharpen_centermult = 8 * minimapSharpen + 1;
671 minimap.sharpen_boxmult = -minimapSharpen;
674 minimap.data1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
675 data4b = safe_malloc(minimap.width * minimap.height * 4);
676 if(minimapSharpen >= 0)
677 minimap.sharpendata1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
679 MiniMapSetupBrushes();
681 if(minimap.samples <= 1)
683 Sys_Printf( "\n--- MiniMapNoSupersampling (%d) ---\n", minimap.height );
684 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapNoSupersampling);
688 if(minimap.sample_offsets)
690 Sys_Printf( "\n--- MiniMapSupersampled (%d) ---\n", minimap.height );
691 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSupersampled);
695 Sys_Printf( "\n--- MiniMapRandomlySupersampled (%d) ---\n", minimap.height );
696 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapRandomlySupersampled);
700 if(minimap.sharpendata1f)
702 Sys_Printf( "\n--- MiniMapSharpen (%d) ---\n", minimap.height );
703 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSharpen);
704 q = minimap.sharpendata1f;
711 Sys_Printf( "\nConverting...");
715 case MINIMAP_MODE_GRAY:
717 for(y = 0; y < minimap.height; ++y)
718 for(x = 0; x < minimap.width; ++x)
723 if(v > 255.0/256.0) v = 255.0/256.0;
727 Sys_Printf( " writing to %s...", minimapFilename );
728 WriteTGAGray(minimapFilename, data4b, minimap.width, minimap.height);
730 case MINIMAP_MODE_BLACK:
732 for(y = 0; y < minimap.height; ++y)
733 for(x = 0; x < minimap.width; ++x)
738 if(v > 255.0/256.0) v = 255.0/256.0;
745 Sys_Printf( " writing to %s...", minimapFilename );
746 WriteTGA(minimapFilename, data4b, minimap.width, minimap.height);
748 case MINIMAP_MODE_WHITE:
750 for(y = 0; y < minimap.height; ++y)
751 for(x = 0; x < minimap.width; ++x)
756 if(v > 255.0/256.0) v = 255.0/256.0;
763 Sys_Printf( " writing to %s...", minimapFilename );
764 WriteTGA(minimapFilename, data4b, minimap.width, minimap.height);
768 Sys_Printf( " done.\n" );
770 /* return to sender */
780 calculates an md4 checksum for a block of data
783 static int MD4BlockChecksum( void *buffer, int length )
785 return Com_BlockChecksum(buffer, length);
790 resets an aas checksum to match the given BSP
793 int FixAAS( int argc, char **argv )
795 int length, checksum;
798 char aas[ 1024 ], **ext;
811 Sys_Printf( "Usage: q3map -fixaas [-v] <mapname>\n" );
815 /* do some path mangling */
816 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
817 StripExtension( source );
818 DefaultExtension( source, ".bsp" );
821 Sys_Printf( "--- FixAAS ---\n" );
824 Sys_Printf( "Loading %s\n", source );
825 length = LoadFile( source, &buffer );
827 /* create bsp checksum */
828 Sys_Printf( "Creating checksum...\n" );
829 checksum = LittleLong( MD4BlockChecksum( buffer, length ) );
831 /* write checksum to aas */
836 strcpy( aas, source );
837 StripExtension( aas );
839 Sys_Printf( "Trying %s\n", aas );
843 file = fopen( aas, "r+b" );
846 if( fwrite( &checksum, 4, 1, file ) != 1 )
847 Error( "Error writing checksum to %s", aas );
851 /* return to sender */
859 analyzes a Quake engine BSP file
862 typedef struct abspHeader_s
867 bspLump_t lumps[ 1 ]; /* unknown size */
871 typedef struct abspLumpTest_s
878 int AnalyzeBSP( int argc, char **argv )
880 abspHeader_t *header;
881 int size, i, version, offset, length, lumpInt, count;
885 char lumpString[ 1024 ], source[ 1024 ];
886 qboolean lumpSwap = qfalse;
887 abspLumpTest_t *lumpTest;
888 static abspLumpTest_t lumpTests[] =
890 { sizeof( bspPlane_t ), 6, "IBSP LUMP_PLANES" },
891 { sizeof( bspBrush_t ), 1, "IBSP LUMP_BRUSHES" },
892 { 8, 6, "IBSP LUMP_BRUSHSIDES" },
893 { sizeof( bspBrushSide_t ), 6, "RBSP LUMP_BRUSHSIDES" },
894 { sizeof( bspModel_t ), 1, "IBSP LUMP_MODELS" },
895 { sizeof( bspNode_t ), 2, "IBSP LUMP_NODES" },
896 { sizeof( bspLeaf_t ), 1, "IBSP LUMP_LEAFS" },
897 { 104, 3, "IBSP LUMP_DRAWSURFS" },
898 { 44, 3, "IBSP LUMP_DRAWVERTS" },
899 { 4, 6, "IBSP LUMP_DRAWINDEXES" },
900 { 128 * 128 * 3, 1, "IBSP LUMP_LIGHTMAPS" },
901 { 256 * 256 * 3, 1, "IBSP LUMP_LIGHTMAPS (256 x 256)" },
902 { 512 * 512 * 3, 1, "IBSP LUMP_LIGHTMAPS (512 x 512)" },
910 Sys_Printf( "Usage: q3map -analyze [-lumpswap] [-v] <mapname>\n" );
914 /* process arguments */
915 for( i = 1; i < (argc - 1); i++ )
917 /* -format map|ase|... */
918 if( !strcmp( argv[ i ], "-lumpswap" ) )
920 Sys_Printf( "Swapped lump structs enabled\n" );
925 /* clean up map name */
926 strcpy( source, ExpandArg( argv[ i ] ) );
927 Sys_Printf( "Loading %s\n", source );
930 size = LoadFile( source, (void**) &header );
931 if( size == 0 || header == NULL )
933 Sys_Printf( "Unable to load %s.\n", source );
937 /* analyze ident/version */
938 memcpy( ident, header->ident, 4 );
940 version = LittleLong( header->version );
942 Sys_Printf( "Identity: %s\n", ident );
943 Sys_Printf( "Version: %d\n", version );
944 Sys_Printf( "---------------------------------------\n" );
946 /* analyze each lump */
947 for( i = 0; i < 100; i++ )
949 /* call of duty swapped lump pairs */
952 offset = LittleLong( header->lumps[ i ].length );
953 length = LittleLong( header->lumps[ i ].offset );
956 /* standard lump pairs */
959 offset = LittleLong( header->lumps[ i ].offset );
960 length = LittleLong( header->lumps[ i ].length );
964 lump = (byte*) header + offset;
965 lumpInt = LittleLong( (int) *((int*) lump) );
966 lumpFloat = LittleFloat( (float) *((float*) lump) );
967 memcpy( lumpString, (char*) lump, (length < 1024 ? length : 1024) );
968 lumpString[ 1024 ] = '\0';
970 /* print basic lump info */
971 Sys_Printf( "Lump: %d\n", i );
972 Sys_Printf( "Offset: %d bytes\n", offset );
973 Sys_Printf( "Length: %d bytes\n", length );
975 /* only operate on valid lumps */
978 /* print data in 4 formats */
979 Sys_Printf( "As hex: %08X\n", lumpInt );
980 Sys_Printf( "As int: %d\n", lumpInt );
981 Sys_Printf( "As float: %f\n", lumpFloat );
982 Sys_Printf( "As string: %s\n", lumpString );
984 /* guess lump type */
985 if( lumpString[ 0 ] == '{' && lumpString[ 2 ] == '"' )
986 Sys_Printf( "Type guess: IBSP LUMP_ENTITIES\n" );
987 else if( strstr( lumpString, "textures/" ) )
988 Sys_Printf( "Type guess: IBSP LUMP_SHADERS\n" );
991 /* guess based on size/count */
992 for( lumpTest = lumpTests; lumpTest->radix > 0; lumpTest++ )
994 if( (length % lumpTest->radix) != 0 )
996 count = length / lumpTest->radix;
997 if( count < lumpTest->minCount )
999 Sys_Printf( "Type guess: %s (%d x %d)\n", lumpTest->name, count, lumpTest->radix );
1004 Sys_Printf( "---------------------------------------\n" );
1007 if( offset + length >= size )
1012 Sys_Printf( "Lump count: %d\n", i + 1 );
1013 Sys_Printf( "File size: %d bytes\n", size );
1015 /* return to caller */
1023 emits statistics about the bsp file
1026 int BSPInfo( int count, char **fileNames )
1029 char source[ 1024 ], ext[ 64 ];
1037 Sys_Printf( "No files to dump info for.\n");
1041 /* enable info mode */
1044 /* walk file list */
1045 for( i = 0; i < count; i++ )
1047 Sys_Printf( "---------------------------------\n" );
1049 /* mangle filename and get size */
1050 strcpy( source, fileNames[ i ] );
1051 ExtractFileExtension( source, ext );
1052 if( !Q_stricmp( ext, "map" ) )
1053 StripExtension( source );
1054 DefaultExtension( source, ".bsp" );
1055 f = fopen( source, "rb" );
1058 size = Q_filelength (f);
1064 /* load the bsp file and print lump sizes */
1065 Sys_Printf( "%s\n", source );
1066 LoadBSPFile( source );
1067 PrintBSPFileSizes();
1071 Sys_Printf( " total %9d\n", size );
1072 Sys_Printf( " %9d KB\n", size / 1024 );
1073 Sys_Printf( " %9d MB\n", size / (1024 * 1024) );
1075 Sys_Printf( "---------------------------------\n" );
1083 static void ExtrapolateTexcoords(const float *axyz, const float *ast, const float *bxyz, const float *bst, const float *cxyz, const float *cst, const float *axyz_new, float *ast_out, const float *bxyz_new, float *bst_out, const float *cxyz_new, float *cst_out)
1085 vec4_t scoeffs, tcoeffs;
1091 VectorSubtract(bxyz, axyz, dab);
1092 VectorSubtract(cxyz, axyz, dac);
1093 CrossProduct(dab, dac, norm);
1097 // s(v + norm) = s(v) when n ortho xyz
1099 // s(v) = DotProduct(v, scoeffs) + scoeffs[3]
1102 // scoeffs * (axyz, 1) == ast[0]
1103 // scoeffs * (bxyz, 1) == bst[0]
1104 // scoeffs * (cxyz, 1) == cst[0]
1105 // scoeffs * (norm, 0) == 0
1106 // scoeffs * [axyz, 1 | bxyz, 1 | cxyz, 1 | norm, 0] = [ast[0], bst[0], cst[0], 0]
1107 solvematrix[0] = axyz[0];
1108 solvematrix[4] = axyz[1];
1109 solvematrix[8] = axyz[2];
1110 solvematrix[12] = 1;
1111 solvematrix[1] = bxyz[0];
1112 solvematrix[5] = bxyz[1];
1113 solvematrix[9] = bxyz[2];
1114 solvematrix[13] = 1;
1115 solvematrix[2] = cxyz[0];
1116 solvematrix[6] = cxyz[1];
1117 solvematrix[10] = cxyz[2];
1118 solvematrix[14] = 1;
1119 solvematrix[3] = norm[0];
1120 solvematrix[7] = norm[1];
1121 solvematrix[11] = norm[2];
1122 solvematrix[15] = 0;
1124 md = m4_det(solvematrix);
1127 Sys_Printf("Cannot invert some matrix, some texcoords aren't extrapolated!");
1131 m4x4_invert(solvematrix);
1133 scoeffs[0] = ast[0];
1134 scoeffs[1] = bst[0];
1135 scoeffs[2] = cst[0];
1137 m4x4_transform_vec4(solvematrix, scoeffs);
1138 tcoeffs[0] = ast[1];
1139 tcoeffs[1] = bst[1];
1140 tcoeffs[2] = cst[1];
1142 m4x4_transform_vec4(solvematrix, tcoeffs);
1144 ast_out[0] = scoeffs[0] * axyz_new[0] + scoeffs[1] * axyz_new[1] + scoeffs[2] * axyz_new[2] + scoeffs[3];
1145 ast_out[1] = tcoeffs[0] * axyz_new[0] + tcoeffs[1] * axyz_new[1] + tcoeffs[2] * axyz_new[2] + tcoeffs[3];
1146 bst_out[0] = scoeffs[0] * bxyz_new[0] + scoeffs[1] * bxyz_new[1] + scoeffs[2] * bxyz_new[2] + scoeffs[3];
1147 bst_out[1] = tcoeffs[0] * bxyz_new[0] + tcoeffs[1] * bxyz_new[1] + tcoeffs[2] * bxyz_new[2] + tcoeffs[3];
1148 cst_out[0] = scoeffs[0] * cxyz_new[0] + scoeffs[1] * cxyz_new[1] + scoeffs[2] * cxyz_new[2] + scoeffs[3];
1149 cst_out[1] = tcoeffs[0] * cxyz_new[0] + tcoeffs[1] * cxyz_new[1] + tcoeffs[2] * cxyz_new[2] + tcoeffs[3];
1154 amaze and confuse your enemies with wierd scaled maps!
1157 int ScaleBSPMain( int argc, char **argv )
1166 float *old_xyzst = NULL;
1172 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
1177 scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
1179 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
1181 scale[0] = atof( argv[ argc - 4 ] );
1183 texscale = !strcmp(argv[1], "-tex");
1185 uniform = ((scale[0] == scale[1]) && (scale[1] == scale[2]));
1187 if( scale[0] == 0.0f || scale[1] == 0.0f || scale[2] == 0.0f )
1189 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
1190 Sys_Printf( "Non-zero scale value required.\n" );
1194 /* do some path mangling */
1195 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
1196 StripExtension( source );
1197 DefaultExtension( source, ".bsp" );
1200 Sys_Printf( "Loading %s\n", source );
1201 LoadBSPFile( source );
1205 Sys_Printf( "--- ScaleBSP ---\n" );
1206 Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
1208 /* scale entity keys */
1209 for( i = 0; i < numBSPEntities && i < numEntities; i++ )
1212 GetVectorForKey( &entities[ i ], "origin", vec );
1213 if( (vec[ 0 ] || vec[ 1 ] || vec[ 2 ]) )
1218 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1219 SetKeyValue( &entities[ i ], "origin", str );
1222 a = FloatForKey( &entities[ i ], "angle" );
1223 if(a == -1 || a == -2) // z scale
1225 else if(fabs(sin(DEG2RAD(a))) < 0.707)
1230 /* scale door lip */
1231 f = FloatForKey( &entities[ i ], "lip" );
1235 sprintf( str, "%f", f );
1236 SetKeyValue( &entities[ i ], "lip", str );
1239 /* scale plat height */
1240 f = FloatForKey( &entities[ i ], "height" );
1244 sprintf( str, "%f", f );
1245 SetKeyValue( &entities[ i ], "height", str );
1248 // TODO maybe allow a definition file for entities to specify which values are scaled how?
1252 for( i = 0; i < numBSPModels; i++ )
1254 bspModels[ i ].mins[0] *= scale[0];
1255 bspModels[ i ].mins[1] *= scale[1];
1256 bspModels[ i ].mins[2] *= scale[2];
1257 bspModels[ i ].maxs[0] *= scale[0];
1258 bspModels[ i ].maxs[1] *= scale[1];
1259 bspModels[ i ].maxs[2] *= scale[2];
1263 for( i = 0; i < numBSPNodes; i++ )
1265 bspNodes[ i ].mins[0] *= scale[0];
1266 bspNodes[ i ].mins[1] *= scale[1];
1267 bspNodes[ i ].mins[2] *= scale[2];
1268 bspNodes[ i ].maxs[0] *= scale[0];
1269 bspNodes[ i ].maxs[1] *= scale[1];
1270 bspNodes[ i ].maxs[2] *= scale[2];
1274 for( i = 0; i < numBSPLeafs; i++ )
1276 bspLeafs[ i ].mins[0] *= scale[0];
1277 bspLeafs[ i ].mins[1] *= scale[1];
1278 bspLeafs[ i ].mins[2] *= scale[2];
1279 bspLeafs[ i ].maxs[0] *= scale[0];
1280 bspLeafs[ i ].maxs[1] *= scale[1];
1281 bspLeafs[ i ].maxs[2] *= scale[2];
1286 Sys_Printf("Using texture unlocking (and probably breaking texture alignment a lot)\n");
1287 old_xyzst = safe_malloc(sizeof(*old_xyzst) * numBSPDrawVerts * 5);
1288 for(i = 0; i < numBSPDrawVerts; i++)
1290 old_xyzst[5*i+0] = bspDrawVerts[i].xyz[0];
1291 old_xyzst[5*i+1] = bspDrawVerts[i].xyz[1];
1292 old_xyzst[5*i+2] = bspDrawVerts[i].xyz[2];
1293 old_xyzst[5*i+3] = bspDrawVerts[i].st[0];
1294 old_xyzst[5*i+4] = bspDrawVerts[i].st[1];
1298 /* scale drawverts */
1299 for( i = 0; i < numBSPDrawVerts; i++ )
1301 bspDrawVerts[i].xyz[0] *= scale[0];
1302 bspDrawVerts[i].xyz[1] *= scale[1];
1303 bspDrawVerts[i].xyz[2] *= scale[2];
1304 bspDrawVerts[i].normal[0] /= scale[0];
1305 bspDrawVerts[i].normal[1] /= scale[1];
1306 bspDrawVerts[i].normal[2] /= scale[2];
1307 VectorNormalize(bspDrawVerts[i].normal, bspDrawVerts[i].normal);
1312 for(i = 0; i < numBSPDrawSurfaces; i++)
1314 switch(bspDrawSurfaces[i].surfaceType)
1318 if(bspDrawSurfaces[i].numIndexes % 3)
1319 Error("Not a triangulation!");
1320 for(j = bspDrawSurfaces[i].firstIndex; j < bspDrawSurfaces[i].firstIndex + bspDrawSurfaces[i].numIndexes; j += 3)
1322 int ia = bspDrawIndexes[j] + bspDrawSurfaces[i].firstVert, ib = bspDrawIndexes[j+1] + bspDrawSurfaces[i].firstVert, ic = bspDrawIndexes[j+2] + bspDrawSurfaces[i].firstVert;
1323 bspDrawVert_t *a = &bspDrawVerts[ia], *b = &bspDrawVerts[ib], *c = &bspDrawVerts[ic];
1324 float *oa = &old_xyzst[ia*5], *ob = &old_xyzst[ib*5], *oc = &old_xyzst[ic*5];
1329 ExtrapolateTexcoords(
1345 for( i = 0; i < numBSPPlanes; i++ )
1347 bspPlanes[ i ].dist *= scale[0];
1352 for( i = 0; i < numBSPPlanes; i++ )
1354 bspPlanes[ i ].normal[0] /= scale[0];
1355 bspPlanes[ i ].normal[1] /= scale[1];
1356 bspPlanes[ i ].normal[2] /= scale[2];
1357 f = 1/VectorLength(bspPlanes[i].normal);
1358 VectorScale(bspPlanes[i].normal, f, bspPlanes[i].normal);
1359 bspPlanes[ i ].dist *= f;
1363 /* scale gridsize */
1364 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
1365 if( (vec[ 0 ] + vec[ 1 ] + vec[ 2 ]) == 0.0f )
1366 VectorCopy( gridSize, vec );
1370 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1371 SetKeyValue( &entities[ 0 ], "gridsize", str );
1373 /* inject command line parameters */
1374 InjectCommandLine(argv, 0, argc - 1);
1378 StripExtension( source );
1379 DefaultExtension( source, "_s.bsp" );
1380 Sys_Printf( "Writing %s\n", source );
1381 WriteBSPFile( source );
1383 /* return to sender */
1391 main argument processing function for bsp conversion
1394 int ConvertBSPMain( int argc, char **argv )
1397 int (*convertFunc)( char * );
1398 game_t *convertGame;
1402 convertFunc = ConvertBSPToASE;
1408 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
1412 /* process arguments */
1413 for( i = 1; i < (argc - 1); i++ )
1415 /* -format map|ase|... */
1416 if( !strcmp( argv[ i ], "-format" ) )
1419 if( !Q_stricmp( argv[ i ], "ase" ) )
1420 convertFunc = ConvertBSPToASE;
1421 else if( !Q_stricmp( argv[ i ], "map" ) )
1422 convertFunc = ConvertBSPToMap;
1425 convertGame = GetGame( argv[ i ] );
1426 if( convertGame == NULL )
1427 Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
1430 else if( !strcmp( argv[ i ], "-ne" ) )
1432 normalEpsilon = atof( argv[ i + 1 ] );
1434 Sys_Printf( "Normal epsilon set to %f\n", normalEpsilon );
1436 else if( !strcmp( argv[ i ], "-de" ) )
1438 distanceEpsilon = atof( argv[ i + 1 ] );
1440 Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
1442 else if( !strcmp( argv[ i ], "-shadersasbitmap" ) )
1443 shadersAsBitmap = qtrue;
1446 /* clean up map name */
1447 strcpy( source, ExpandArg( argv[ i ] ) );
1448 StripExtension( source );
1449 DefaultExtension( source, ".bsp" );
1453 Sys_Printf( "Loading %s\n", source );
1455 /* ydnar: load surface file */
1456 //% LoadSurfaceExtraFile( source );
1458 LoadBSPFile( source );
1460 /* parse bsp entities */
1463 /* bsp format convert? */
1464 if( convertGame != NULL )
1466 /* set global game */
1470 StripExtension( source );
1471 DefaultExtension( source, "_c.bsp" );
1472 Sys_Printf( "Writing %s\n", source );
1473 WriteBSPFile( source );
1475 /* return to sender */
1479 /* normal convert */
1480 return convertFunc( source );
1490 int main( int argc, char **argv )
1496 /* we want consistent 'randomness' */
1500 start = I_FloatTime();
1502 /* this was changed to emit version number over the network */
1503 printf( Q3MAP_VERSION "\n" );
1506 atexit( ExitQ3Map );
1508 /* read general options first */
1509 for( i = 1; i < argc; i++ )
1512 if( !strcmp( argv[ i ], "-connect" ) )
1516 Broadcast_Setup( argv[ i ] );
1521 else if( !strcmp( argv[ i ], "-v" ) )
1531 else if( !strcmp( argv[ i ], "-force" ) )
1537 /* patch subdivisions */
1538 else if( !strcmp( argv[ i ], "-subdivisions" ) )
1542 patchSubdivisions = atoi( argv[ i ] );
1544 if( patchSubdivisions <= 0 )
1545 patchSubdivisions = 1;
1549 else if( !strcmp( argv[ i ], "-threads" ) )
1553 numthreads = atoi( argv[ i ] );
1558 /* init model library */
1560 PicoSetMallocFunc( safe_malloc );
1561 PicoSetFreeFunc( free );
1562 PicoSetPrintFunc( PicoPrintFunc );
1563 PicoSetLoadFileFunc( PicoLoadFileFunc );
1564 PicoSetFreeFileFunc( free );
1566 /* set number of threads */
1569 /* generate sinusoid jitter table */
1570 for( i = 0; i < MAX_JITTERS; i++ )
1572 jitters[ i ] = sin( i * 139.54152147 );
1573 //% Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
1576 /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
1577 and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
1579 Sys_Printf( "Q3Map - v1.0r (c) 1999 Id Software Inc.\n" );
1580 Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
1581 Sys_Printf( "NetRadiant - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
1582 Sys_Printf( "%s\n", Q3MAP_MOTD );
1584 /* ydnar: new path initialization */
1585 InitPaths( &argc, argv );
1587 /* set game options */
1588 if (!patchSubdivisions)
1589 patchSubdivisions = game->patchSubdivisions;
1591 /* check if we have enough options left to attempt something */
1593 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
1596 if( !strcmp( argv[ 1 ], "-fixaas" ) )
1597 r = FixAAS( argc - 1, argv + 1 );
1600 else if( !strcmp( argv[ 1 ], "-analyze" ) )
1601 r = AnalyzeBSP( argc - 1, argv + 1 );
1604 else if( !strcmp( argv[ 1 ], "-info" ) )
1605 r = BSPInfo( argc - 2, argv + 2 );
1608 else if( !strcmp( argv[ 1 ], "-vis" ) )
1609 r = VisMain( argc - 1, argv + 1 );
1612 else if( !strcmp( argv[ 1 ], "-light" ) )
1613 r = LightMain( argc - 1, argv + 1 );
1616 else if( !strcmp( argv[ 1 ], "-vlight" ) )
1618 Sys_Printf( "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
1619 argv[ 1 ] = "-fast"; /* eek a hack */
1620 r = LightMain( argc, argv );
1623 /* ydnar: lightmap export */
1624 else if( !strcmp( argv[ 1 ], "-export" ) )
1625 r = ExportLightmapsMain( argc - 1, argv + 1 );
1627 /* ydnar: lightmap import */
1628 else if( !strcmp( argv[ 1 ], "-import" ) )
1629 r = ImportLightmapsMain( argc - 1, argv + 1 );
1631 /* ydnar: bsp scaling */
1632 else if( !strcmp( argv[ 1 ], "-scale" ) )
1633 r = ScaleBSPMain( argc - 1, argv + 1 );
1635 /* ydnar: bsp conversion */
1636 else if( !strcmp( argv[ 1 ], "-convert" ) )
1637 r = ConvertBSPMain( argc - 1, argv + 1 );
1640 else if( !strcmp( argv[ 1 ], "-minimap" ) )
1641 r = MiniMapBSPMain(argc - 1, argv + 1);
1643 /* ydnar: otherwise create a bsp */
1645 r = BSPMain( argc, argv );
1648 end = I_FloatTime();
1649 Sys_Printf( "%9.0f seconds elapsed\n", end - start );
1651 /* shut down connection */
1652 Broadcast_Shutdown();
1654 /* return any error code */