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 ------------------------------------------------------------------------------- */
47 static prefixInfo_t prefixInfo[] = {
48 { "metal", TEX_SURF_METAL},
49 { "wood", TEX_SURF_WOOD},
50 { "cloth", TEX_SURF_CLOTH},
51 { "dirt", TEX_SURF_DIRT},
52 { "glass", TEX_SURF_GLASS},
53 { "plant", TEX_SURF_PLANT},
54 { "sand", TEX_SURF_SAND},
55 { "snow", TEX_SURF_SNOW},
56 { "stone", TEX_SURF_STONE},
57 { "water", TEX_SURF_WATER},
58 { "grass", TEX_SURF_GRASS},
61 #define NUM_PREFIXINFO 11 /* very important */
63 //Added by Spoon to recognize surfaceparms by shadernames
64 int GetSurfaceParm(const char *tex){
65 char surf[MAX_QPATH], tex2[MAX_QPATH];
71 for(i = 0; i < 64 && tex2[i] != '\0'; i++){
72 if(tex2[i] == '\\' || tex2[i] == '/')
84 /* Sys_Printf("%s\n", surf); */
86 for(i=0; i < NUM_PREFIXINFO; i++){
87 if(!Q_stricmp(surf, prefixInfo[i].name)){
88 return prefixInfo[i].surfaceFlags;
98 emits a bsp shader entry
101 int EmitShader( const char *shader, int *contentFlags, int *surfaceFlags ){
106 /* handle special cases */
107 if ( shader == NULL ) {
111 /* try to find an existing shader */
112 for ( i = 0; i < numBSPShaders; i++ )
114 /* if not Smokin'Guns like tex file */
115 if ( !game->texFile )
117 /* ydnar: handle custom surface/content flags */
118 if ( surfaceFlags != NULL && bspShaders[ i ].surfaceFlags != *surfaceFlags ) {
121 if ( contentFlags != NULL && bspShaders[ i ].contentFlags != *contentFlags ) {
127 if ( !Q_stricmp( shader, bspShaders[ i ].shader ) ) {
133 si = ShaderInfoForShader( shader );
135 /* emit a new shader */
136 AUTOEXPAND_BY_REALLOC0_BSP( Shaders, 1024 );
139 strcpy( bspShaders[ i ].shader, shader );
140 bspShaders[ i ].surfaceFlags = si->surfaceFlags;
144 /* Smokin'Guns like tex file */
145 bspShaders[ i ].surfaceFlags |= GetSurfaceParm(si->shader);
148 bspShaders[ i ].contentFlags = si->contentFlags;
150 /* if not Smokin'Guns like tex file */
151 if ( !game->texFile )
153 /* handle custom content/surface flags */
154 if ( surfaceFlags != NULL ) {
155 bspShaders[ i ].surfaceFlags = *surfaceFlags;
157 if ( contentFlags != NULL ) {
158 bspShaders[ i ].contentFlags = *contentFlags;
162 /* recursively emit any damage shaders */
163 if ( si->damageShader != NULL && si->damageShader[ 0 ] != '\0' ) {
164 Sys_FPrintf( SYS_VRB, "Shader %s has damage shader %s\n", si->shader, si->damageShader );
165 EmitShader( si->damageShader, NULL, NULL );
176 there is no oportunity to discard planes, because all of the original
177 brushes will be saved in the map
180 void EmitPlanes( void ){
186 /* walk plane list */
188 for ( i = 0; i < nummapplanes; i++, mp++ )
190 AUTOEXPAND_BY_REALLOC_BSP( Planes, 1024 );
191 bp = &bspPlanes[ numBSPPlanes ];
192 VectorCopy( mp->normal, bp->normal );
197 /* emit some statistics */
198 Sys_FPrintf( SYS_VRB, "%9d BSP planes\n", numBSPPlanes );
205 emits a leafnode to the bsp file
208 void EmitLeaf( node_t *node ){
215 if ( numBSPLeafs >= MAX_MAP_LEAFS ) {
216 Error( "MAX_MAP_LEAFS" );
219 leaf_p = &bspLeafs[numBSPLeafs];
222 leaf_p->cluster = node->cluster;
223 leaf_p->area = node->area;
225 /* emit bounding box */
226 VectorCopy( node->mins, leaf_p->mins );
227 VectorCopy( node->maxs, leaf_p->maxs );
229 /* emit leaf brushes */
230 leaf_p->firstBSPLeafBrush = numBSPLeafBrushes;
231 for ( b = node->brushlist; b; b = b->next )
233 /* something is corrupting brushes */
234 if ( (size_t) b < 256 ) {
235 Sys_FPrintf( SYS_WRN, "WARNING: Node brush list corrupted (0x%08X)\n", b );
238 //% if( b->guard != 0xDEADBEEF )
239 //% Sys_Printf( "Brush %6d: 0x%08X Guard: 0x%08X Next: 0x%08X Original: 0x%08X Sides: %d\n", b->brushNum, b, b, b->next, b->original, b->numsides );
241 AUTOEXPAND_BY_REALLOC_BSP( LeafBrushes, 1024 );
242 bspLeafBrushes[ numBSPLeafBrushes ] = b->original->outputNum;
246 leaf_p->numBSPLeafBrushes = numBSPLeafBrushes - leaf_p->firstBSPLeafBrush;
248 /* emit leaf surfaces */
249 if ( node->opaque ) {
253 /* add the drawSurfRef_t drawsurfs */
254 leaf_p->firstBSPLeafSurface = numBSPLeafSurfaces;
255 for ( dsr = node->drawSurfReferences; dsr; dsr = dsr->nextRef )
257 AUTOEXPAND_BY_REALLOC_BSP( LeafSurfaces, 1024 );
258 bspLeafSurfaces[ numBSPLeafSurfaces ] = dsr->outputNum;
259 numBSPLeafSurfaces++;
262 leaf_p->numBSPLeafSurfaces = numBSPLeafSurfaces - leaf_p->firstBSPLeafSurface;
268 recursively emit the bsp nodes
271 int EmitDrawNode_r( node_t *node ){
276 /* check for leafnode */
277 if ( node->planenum == PLANENUM_LEAF ) {
283 AUTOEXPAND_BY_REALLOC_BSP( Nodes, 1024 );
288 VectorCopy( node->mins, n->mins );
289 VectorCopy( node->maxs, n->maxs );
291 if ( node->planenum & 1 ) {
292 Error( "WriteDrawNodes_r: odd planenum" );
294 n->planeNum = node->planenum;
297 // recursively output the other nodes
299 for ( i = 0 ; i < 2 ; i++ )
301 if ( node->children[i]->planenum == PLANENUM_LEAF ) {
302 n->children[i] = -( numBSPLeafs + 1 );
303 EmitLeaf( node->children[i] );
307 n->children[i] = numBSPNodes;
308 EmitDrawNode_r( node->children[i] );
309 // n may have become invalid here, so...
324 void SetModelNumbers( void ){
330 for ( i = 1 ; i < numEntities ; i++ ) {
331 if ( entities[i].brushes || entities[i].patches ) {
332 sprintf( value, "*%i", models );
334 SetKeyValue( &entities[i], "model", value );
345 sets style keys for entity lights
348 void SetLightStyles( void ){
349 int i, j, style, numStyles;
354 char lightTargets[ MAX_SWITCHED_LIGHTS ][ 64 ];
355 int lightStyles[ MAX_SWITCHED_LIGHTS ];
357 /* -keeplights option: force lights to be kept and ignore what the map file says */
359 SetKeyValue( &entities[0], "_keepLights", "1" );
362 /* ydnar: determine if we keep lights in the bsp */
363 if ( KeyExists( &entities[ 0 ], "_keepLights" ) == qtrue ) {
364 t = ValueForKey( &entities[ 0 ], "_keepLights" );
365 keepLights = ( t[ 0 ] == '1' ) ? qtrue : qfalse;
368 /* any light that is controlled (has a targetname) must have a unique style number generated for it */
370 for ( i = 1; i < numEntities; i++ )
374 t = ValueForKey( e, "classname" );
375 if ( Q_strncasecmp( t, "light", 5 ) ) {
378 t = ValueForKey( e, "targetname" );
379 if ( t[ 0 ] == '\0' ) {
380 /* ydnar: strip the light from the BSP file */
381 if ( keepLights == qfalse ) {
399 /* get existing style */
400 style = IntForKey( e, "style" );
401 if ( style < LS_NORMAL || style > LS_NONE ) {
402 Error( "Invalid lightstyle (%d) on entity %d", style, i );
405 /* find this targetname */
406 for ( j = 0; j < numStyles; j++ )
407 if ( lightStyles[ j ] == style && !strcmp( lightTargets[ j ], t ) ) {
411 /* add a new style */
412 if ( j >= numStyles ) {
413 if ( numStyles == MAX_SWITCHED_LIGHTS ) {
414 Error( "MAX_SWITCHED_LIGHTS (%d) exceeded, reduce the number of lights with targetnames", MAX_SWITCHED_LIGHTS );
416 strcpy( lightTargets[ j ], t );
417 lightStyles[ j ] = style;
421 /* set explicit style */
422 sprintf( value, "%d", 32 + j );
423 SetKeyValue( e, "style", value );
426 if ( style != LS_NORMAL ) {
427 sprintf( value, "%d", style );
428 SetKeyValue( e, "switch_style", value );
432 /* emit some statistics */
433 Sys_FPrintf( SYS_VRB, "%9d light entities stripped\n", numStrippedLights );
440 starts a new bsp file
443 void BeginBSPFile( void ){
444 /* these values may actually be initialized if the file existed when loaded, so clear them explicitly */
447 numBSPBrushSides = 0;
448 numBSPLeafSurfaces = 0;
449 numBSPLeafBrushes = 0;
451 /* leave leaf 0 as an error, because leafs are referenced as negative number nodes */
455 /* ydnar: gs mods: set the first 6 drawindexes to 0 1 2 2 1 3 for triangles and quads */
456 numBSPDrawIndexes = 6;
457 AUTOEXPAND_BY_REALLOC_BSP( DrawIndexes, 1024 );
458 bspDrawIndexes[ 0 ] = 0;
459 bspDrawIndexes[ 1 ] = 1;
460 bspDrawIndexes[ 2 ] = 2;
461 bspDrawIndexes[ 3 ] = 0;
462 bspDrawIndexes[ 4 ] = 2;
463 bspDrawIndexes[ 5 ] = 3;
469 RestoreSurfaceFlags()
470 read Smokin'Guns like tex file
471 added by spoon to get back the changed surfaceflags
474 void RestoreSurfaceFlags( char *filename ) {
477 int surfaceFlags[ MAX_MAP_DRAW_SURFS ];
480 /* first parse the tex file */
481 texfile = fopen( filename, "r" );
484 fscanf( texfile, "TEXFILE\n%i\n", &numTexInfos );
486 /* Sys_Printf( "%i\n", numTexInfos ); */
488 for ( i = 0; i < numTexInfos; i++ ) {
491 fscanf( texfile, "%i %f %f %f\n", &surfaceFlags[ i ],
492 &color[ 0 ], &color[ 1 ], &color[ 2 ]);
494 bspShaders[ i ].surfaceFlags = surfaceFlags[ i ];
496 /* Sys_Printf( "%i\n", surfaceFlags[ i ] ); */
499 Sys_Printf("couldn't find %s not tex-file is now writed without surfaceFlags!\n", filename);
507 write Smokin'Guns like tex file
511 void WriteTexFile( char* filename ) {
515 if ( !compile_map ) {
516 RestoreSurfaceFlags( filename );
519 Sys_Printf( "Writing %s ...\n", filename );
521 texfile = fopen ( filename, "w" );
523 fprintf( texfile, "TEXFILE\n" );
525 fprintf( texfile, "%i\n", numBSPShaders );
527 for ( i = 0 ; i < numBSPShaders ; i++ ) {
528 shaderInfo_t *se = ShaderInfoForShader( bspShaders[ i ].shader );
530 fprintf( texfile, "\n%i %f %f %f", bspShaders[ i ].surfaceFlags,
531 se->color[ 0 ], se->color[ 1 ], se->color[ 2 ] );
533 bspShaders[ i ].surfaceFlags = i;
543 finishes a new bsp and writes to disk
546 void EndBSPFile( qboolean do_write, const char *BSPFilePath, const char *surfaceFilePath ){
548 Sys_FPrintf( SYS_VRB, "--- EndBSPFile ---\n" );
552 numBSPEntities = numEntities;
556 /* write the surface extra file */
557 WriteSurfaceExtraFile( surfaceFilePath );
561 char basename[ 1024 ];
562 char filename[ 1024 ];
563 strncpy( basename, BSPFilePath, sizeof(basename) );
564 StripExtension( basename );
565 sprintf( filename, "%s.tex", basename );
567 /* only create tex file if it is the first compile */
568 WriteTexFile( filename );
572 Sys_Printf( "Writing %s\n", BSPFilePath );
573 WriteBSPFile( BSPFilePath );
581 writes the brush list to the bsp
584 void EmitBrushes( brush_t *brushes, int *firstBrush, int *numBrushes ){
591 /* set initial brush */
592 if ( firstBrush != NULL ) {
593 *firstBrush = numBSPBrushes;
595 if ( numBrushes != NULL ) {
599 /* walk list of brushes */
600 for ( b = brushes; b != NULL; b = b->next )
603 AUTOEXPAND_BY_REALLOC_BSP( Brushes, 1024 );
606 b->outputNum = numBSPBrushes;
607 db = &bspBrushes[ numBSPBrushes ];
609 if ( numBrushes != NULL ) {
613 db->shaderNum = EmitShader( b->contentShader->shader, &b->contentShader->contentFlags, &b->contentShader->surfaceFlags );
614 db->firstSide = numBSPBrushSides;
618 for ( j = 0; j < b->numsides; j++ )
620 /* set output number to bogus initially */
621 b->sides[ j ].outputNum = -1;
624 AUTOEXPAND_BY_REALLOC_BSP( BrushSides, 1024 );
627 b->sides[ j ].outputNum = numBSPBrushSides;
628 cp = &bspBrushSides[ numBSPBrushSides ];
631 cp->planeNum = b->sides[ j ].planenum;
634 if ( b->sides[ j ].shaderInfo ) {
635 cp->shaderNum = EmitShader( b->sides[ j ].shaderInfo->shader, &b->sides[ j ].shaderInfo->contentFlags, &b->sides[ j ].shaderInfo->surfaceFlags );
638 cp->shaderNum = EmitShader( NULL, NULL, NULL );
648 turns map fogs into bsp fogs
651 void EmitFogs( void ){
656 numBSPFogs = numMapFogs;
659 for ( i = 0; i < numMapFogs; i++ )
662 strcpy( bspFogs[ i ].shader, mapFogs[ i ].si->shader );
664 /* global fog doesn't have an associated brush */
665 if ( mapFogs[ i ].brush == NULL ) {
666 bspFogs[ i ].brushNum = -1;
667 bspFogs[ i ].visibleSide = -1;
672 bspFogs[ i ].brushNum = mapFogs[ i ].brush->outputNum;
674 /* try to use forced visible side */
675 if ( mapFogs[ i ].visibleSide >= 0 ) {
676 bspFogs[ i ].visibleSide = mapFogs[ i ].visibleSide;
680 /* find visible side */
681 for ( j = 0; j < 6; j++ )
683 if ( mapFogs[ i ].brush->sides[ j ].visibleHull != NULL ) {
684 Sys_Printf( "Fog %d has visible side %d\n", i, j );
685 bspFogs[ i ].visibleSide = j;
697 sets up a new brush model
700 void BeginModel( void ){
705 vec3_t lgMins, lgMaxs; /* ydnar: lightgrid mins/maxs */
711 AUTOEXPAND_BY_REALLOC_BSP( Models, 256 );
713 /* get model and entity */
714 mod = &bspModels[ numBSPModels ];
715 e = &entities[ mapEntityNum ];
717 /* ydnar: lightgrid mins/maxs */
718 ClearBounds( lgMins, lgMaxs );
720 /* bound the brushes */
721 ClearBounds( mins, maxs );
722 for ( b = e->brushes; b; b = b->next )
724 /* ignore non-real brushes (origin, etc) */
725 if ( b->numsides == 0 ) {
728 AddPointToBounds( b->mins, mins, maxs );
729 AddPointToBounds( b->maxs, mins, maxs );
731 /* ydnar: lightgrid bounds */
732 if ( b->compileFlags & C_LIGHTGRID ) {
733 AddPointToBounds( b->mins, lgMins, lgMaxs );
734 AddPointToBounds( b->maxs, lgMins, lgMaxs );
739 for ( p = e->patches; p; p = p->next )
741 for ( i = 0; i < ( p->mesh.width * p->mesh.height ); i++ )
742 AddPointToBounds( p->mesh.verts[i].xyz, mins, maxs );
745 /* ydnar: lightgrid mins/maxs */
746 if ( lgMins[ 0 ] < 99999 ) {
747 /* use lightgrid bounds */
748 VectorCopy( lgMins, mod->mins );
749 VectorCopy( lgMaxs, mod->maxs );
753 /* use brush/patch bounds */
754 VectorCopy( mins, mod->mins );
755 VectorCopy( maxs, mod->maxs );
759 Sys_FPrintf( SYS_VRB, "BSP bounds: { %f %f %f } { %f %f %f }\n", mins[ 0 ], mins[ 1 ], mins[ 2 ], maxs[ 0 ], maxs[ 1 ], maxs[ 2 ] );
760 Sys_FPrintf( SYS_VRB, "Lightgrid bounds: { %f %f %f } { %f %f %f }\n", lgMins[ 0 ], lgMins[ 1 ], lgMins[ 2 ], lgMaxs[ 0 ], lgMaxs[ 1 ], lgMaxs[ 2 ] );
763 mod->firstBSPSurface = numBSPDrawSurfaces;
764 mod->firstBSPBrush = numBSPBrushes;
772 finish a model's processing
775 void EndModel( entity_t *e, node_t *headnode ){
780 Sys_FPrintf( SYS_VRB, "--- EndModel ---\n" );
783 mod = &bspModels[ numBSPModels ];
784 EmitDrawNode_r( headnode );
786 /* set surfaces and brushes */
787 mod->numBSPSurfaces = numBSPDrawSurfaces - mod->firstBSPSurface;
788 mod->firstBSPBrush = e->firstBrush;
789 mod->numBSPBrushes = e->numBrushes;
791 /* increment model count */