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 ------------------------------------------------------------------------------- */
40 #define NUM_PREFIXINFO 11 //very important
49 static prefixInfo_t prefixInfo[] = {
50 { "metal", Q_SURF_METAL},
51 { "wood", Q_SURF_WOOD},
52 { "cloth", Q_SURF_CLOTH},
53 { "dirt", Q_SURF_DIRT},
54 { "glass", Q_SURF_GLASS},
55 { "plant", Q_SURF_PLANT},
56 { "sand", Q_SURF_SAND},
57 { "snow", Q_SURF_SNOW},
58 { "stone", Q_SURF_STONE},
59 { "water", Q_SURF_WATER},
60 { "grass", Q_SURF_GRASS},
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;
99 emits a bsp shader entry
102 int EmitShader( const char *shader, int *contentFlags, int *surfaceFlags ){
107 /* handle special cases */
108 if ( shader == NULL ) {
112 /* try to find an existing shader */
113 for ( i = 0; i < numBSPShaders; i++ )
115 /* ydnar: handle custom surface/content flags */
117 if ( surfaceFlags != NULL && bspShaders[ i ].surfaceFlags != *surfaceFlags ) {
120 if ( contentFlags != NULL && bspShaders[ i ].contentFlags != *contentFlags ) {
126 if ( !Q_stricmp( shader, bspShaders[ i ].shader ) ) {
132 si = ShaderInfoForShader( shader );
134 /* emit a new shader */
135 AUTOEXPAND_BY_REALLOC_BSP( Shaders, 1024 );
138 strcpy( bspShaders[ i ].shader, shader );
139 bspShaders[ i ].surfaceFlags = si->surfaceFlags;
141 bspShaders[ i ].surfaceFlags |= GetSurfaceParm(si->shader);
143 bspShaders[ i ].contentFlags = si->contentFlags;
145 /* handle custom content/surface flags */
147 if ( surfaceFlags != NULL ) {
148 bspShaders[ i ].surfaceFlags = *surfaceFlags;
150 if ( contentFlags != NULL ) {
151 bspShaders[ i ].contentFlags = *contentFlags;
155 /* recursively emit any damage shaders */
156 if ( si->damageShader != NULL && si->damageShader[ 0 ] != '\0' ) {
157 Sys_FPrintf( SYS_VRB, "Shader %s has damage shader %s\n", si->shader, si->damageShader );
158 EmitShader( si->damageShader, NULL, NULL );
169 there is no oportunity to discard planes, because all of the original
170 brushes will be saved in the map
173 void EmitPlanes( void ){
179 /* walk plane list */
181 for ( i = 0; i < nummapplanes; i++, mp++ )
183 AUTOEXPAND_BY_REALLOC_BSP( Planes, 1024 );
184 bp = &bspPlanes[ numBSPPlanes ];
185 VectorCopy( mp->normal, bp->normal );
190 /* emit some statistics */
191 Sys_FPrintf( SYS_VRB, "%9d BSP planes\n", numBSPPlanes );
198 emits a leafnode to the bsp file
201 void EmitLeaf( node_t *node ){
208 if ( numBSPLeafs >= MAX_MAP_LEAFS ) {
209 Error( "MAX_MAP_LEAFS" );
212 leaf_p = &bspLeafs[numBSPLeafs];
215 leaf_p->cluster = node->cluster;
216 leaf_p->area = node->area;
218 /* emit bounding box */
219 VectorCopy( node->mins, leaf_p->mins );
220 VectorCopy( node->maxs, leaf_p->maxs );
222 /* emit leaf brushes */
223 leaf_p->firstBSPLeafBrush = numBSPLeafBrushes;
224 for ( b = node->brushlist; b; b = b->next )
226 /* something is corrupting brushes */
227 if ( (size_t) b < 256 ) {
228 Sys_FPrintf( SYS_WRN, "WARNING: Node brush list corrupted (0x%08X)\n", b );
231 //% if( b->guard != 0xDEADBEEF )
232 //% 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 );
234 AUTOEXPAND_BY_REALLOC_BSP( LeafBrushes, 1024 );
235 bspLeafBrushes[ numBSPLeafBrushes ] = b->original->outputNum;
239 leaf_p->numBSPLeafBrushes = numBSPLeafBrushes - leaf_p->firstBSPLeafBrush;
241 /* emit leaf surfaces */
242 if ( node->opaque ) {
246 /* add the drawSurfRef_t drawsurfs */
247 leaf_p->firstBSPLeafSurface = numBSPLeafSurfaces;
248 for ( dsr = node->drawSurfReferences; dsr; dsr = dsr->nextRef )
250 AUTOEXPAND_BY_REALLOC_BSP( LeafSurfaces, 1024 );
251 bspLeafSurfaces[ numBSPLeafSurfaces ] = dsr->outputNum;
252 numBSPLeafSurfaces++;
255 leaf_p->numBSPLeafSurfaces = numBSPLeafSurfaces - leaf_p->firstBSPLeafSurface;
261 recursively emit the bsp nodes
264 int EmitDrawNode_r( node_t *node ){
269 /* check for leafnode */
270 if ( node->planenum == PLANENUM_LEAF ) {
276 AUTOEXPAND_BY_REALLOC_BSP( Nodes, 1024 );
281 VectorCopy( node->mins, n->mins );
282 VectorCopy( node->maxs, n->maxs );
284 if ( node->planenum & 1 ) {
285 Error( "WriteDrawNodes_r: odd planenum" );
287 n->planeNum = node->planenum;
290 // recursively output the other nodes
292 for ( i = 0 ; i < 2 ; i++ )
294 if ( node->children[i]->planenum == PLANENUM_LEAF ) {
295 n->children[i] = -( numBSPLeafs + 1 );
296 EmitLeaf( node->children[i] );
300 n->children[i] = numBSPNodes;
301 EmitDrawNode_r( node->children[i] );
302 // n may have become invalid here, so...
317 void SetModelNumbers( void ){
323 for ( i = 1 ; i < numEntities ; i++ ) {
324 if ( entities[i].brushes || entities[i].patches ) {
325 sprintf( value, "*%i", models );
327 SetKeyValue( &entities[i], "model", value );
338 sets style keys for entity lights
341 void SetLightStyles( void ){
342 int i, j, style, numStyles;
347 char lightTargets[ MAX_SWITCHED_LIGHTS ][ 64 ];
348 int lightStyles[ MAX_SWITCHED_LIGHTS ];
350 /* -keeplights option: force lights to be kept and ignore what the map file says */
352 SetKeyValue( &entities[0], "_keepLights", "1" );
355 /* ydnar: determine if we keep lights in the bsp */
356 if ( KeyExists( &entities[ 0 ], "_keepLights" ) == qtrue ) {
357 t = ValueForKey( &entities[ 0 ], "_keepLights" );
358 keepLights = ( t[ 0 ] == '1' ) ? qtrue : qfalse;
361 /* any light that is controlled (has a targetname) must have a unique style number generated for it */
363 for ( i = 1; i < numEntities; i++ )
367 t = ValueForKey( e, "classname" );
368 if ( Q_strncasecmp( t, "light", 5 ) ) {
371 t = ValueForKey( e, "targetname" );
372 if ( t[ 0 ] == '\0' ) {
373 /* ydnar: strip the light from the BSP file */
374 if ( keepLights == qfalse ) {
392 /* get existing style */
393 style = IntForKey( e, "style" );
394 if ( style < LS_NORMAL || style > LS_NONE ) {
395 Error( "Invalid lightstyle (%d) on entity %d", style, i );
398 /* find this targetname */
399 for ( j = 0; j < numStyles; j++ )
400 if ( lightStyles[ j ] == style && !strcmp( lightTargets[ j ], t ) ) {
404 /* add a new style */
405 if ( j >= numStyles ) {
406 if ( numStyles == MAX_SWITCHED_LIGHTS ) {
407 Error( "MAX_SWITCHED_LIGHTS (%d) exceeded, reduce the number of lights with targetnames", MAX_SWITCHED_LIGHTS );
409 strcpy( lightTargets[ j ], t );
410 lightStyles[ j ] = style;
414 /* set explicit style */
415 sprintf( value, "%d", 32 + j );
416 SetKeyValue( e, "style", value );
419 if ( style != LS_NORMAL ) {
420 sprintf( value, "%d", style );
421 SetKeyValue( e, "switch_style", value );
425 /* emit some statistics */
426 Sys_FPrintf( SYS_VRB, "%9d light entities stripped\n", numStrippedLights );
433 starts a new bsp file
436 void BeginBSPFile( void ){
437 /* these values may actually be initialized if the file existed when loaded, so clear them explicitly */
440 numBSPBrushSides = 0;
441 numBSPLeafSurfaces = 0;
442 numBSPLeafBrushes = 0;
444 /* leave leaf 0 as an error, because leafs are referenced as negative number nodes */
448 /* ydnar: gs mods: set the first 6 drawindexes to 0 1 2 2 1 3 for triangles and quads */
449 numBSPDrawIndexes = 6;
450 AUTOEXPAND_BY_REALLOC_BSP( DrawIndexes, 1024 );
451 bspDrawIndexes[ 0 ] = 0;
452 bspDrawIndexes[ 1 ] = 1;
453 bspDrawIndexes[ 2 ] = 2;
454 bspDrawIndexes[ 3 ] = 0;
455 bspDrawIndexes[ 4 ] = 2;
456 bspDrawIndexes[ 5 ] = 3;
462 //added by spoon to get back the changed surfaceflags
463 void RestoreSurfaceFlags(char *filename){
466 int surfaceFlags[MAX_MAP_DRAW_SURFS];
469 //first parse the tex-file
470 texfile = fopen(filename, "r");
472 fscanf( texfile, "TEXFILE\n%i\n", &numTexInfos);
473 //Sys_Printf("%i\n", numTexInfos);
475 for(i=0; i<numTexInfos; i++){
477 fscanf( texfile, "%i %f %f %f\n", &surfaceFlags[i],
478 &color[0], &color[1], &color[2]);
479 bspShaders[i].surfaceFlags = surfaceFlags[i];
480 //Sys_Printf("%i\n", surfaceFlags[i]);
483 Sys_Printf("couldn't find %s not tex-file is now writed without surfaceFlags!\n", filename);
486 void WriteTexFile( char* name){
491 sprintf (filename, "%s.tex", name);
494 RestoreSurfaceFlags(filename);
497 Sys_Printf("Writing %s ...\n", filename);
498 texfile = fopen (filename, "w");
500 fprintf( texfile, "TEXFILE\n");
502 fprintf( texfile, "%i\n", numBSPShaders);
503 for ( i = 0 ; i < numBSPShaders ; i++ ) {
504 shaderInfo_t *se = ShaderInfoForShader(bspShaders[i].shader);
506 fprintf( texfile, "\n%i %f %f %f", bspShaders[i].surfaceFlags,
507 se->color[0], se->color[1], se->color[2]);
510 bspShaders[i].surfaceFlags = i;
520 finishes a new bsp and writes to disk
523 void EndBSPFile( qboolean do_write, const char *BSPFilePath, const char *surfaceFilePath ){
525 Sys_FPrintf( SYS_VRB, "--- EndBSPFile ---\n" );
529 numBSPEntities = numEntities;
533 /* write the surface extra file */
534 WriteSurfaceExtraFile( surfaceFilePath );
537 //only create tex file if it is the first compile
538 WriteTexFile (source);
542 Sys_Printf( "Writing %s\n", BSPFilePath );
543 WriteBSPFile( BSPFilePath );
551 writes the brush list to the bsp
554 void EmitBrushes( brush_t *brushes, int *firstBrush, int *numBrushes ){
561 /* set initial brush */
562 if ( firstBrush != NULL ) {
563 *firstBrush = numBSPBrushes;
565 if ( numBrushes != NULL ) {
569 /* walk list of brushes */
570 for ( b = brushes; b != NULL; b = b->next )
573 AUTOEXPAND_BY_REALLOC_BSP( Brushes, 1024 );
576 b->outputNum = numBSPBrushes;
577 db = &bspBrushes[ numBSPBrushes ];
579 if ( numBrushes != NULL ) {
583 db->shaderNum = EmitShader( b->contentShader->shader, &b->contentShader->contentFlags, &b->contentShader->surfaceFlags );
584 db->firstSide = numBSPBrushSides;
588 for ( j = 0; j < b->numsides; j++ )
590 /* set output number to bogus initially */
591 b->sides[ j ].outputNum = -1;
594 AUTOEXPAND_BY_REALLOC_BSP( BrushSides, 1024 );
597 b->sides[ j ].outputNum = numBSPBrushSides;
598 cp = &bspBrushSides[ numBSPBrushSides ];
601 cp->planeNum = b->sides[ j ].planenum;
604 if ( b->sides[ j ].shaderInfo ) {
605 cp->shaderNum = EmitShader( b->sides[ j ].shaderInfo->shader, &b->sides[ j ].shaderInfo->contentFlags, &b->sides[ j ].shaderInfo->surfaceFlags );
608 cp->shaderNum = EmitShader( NULL, NULL, NULL );
618 turns map fogs into bsp fogs
621 void EmitFogs( void ){
626 numBSPFogs = numMapFogs;
629 for ( i = 0; i < numMapFogs; i++ )
632 strcpy( bspFogs[ i ].shader, mapFogs[ i ].si->shader );
634 /* global fog doesn't have an associated brush */
635 if ( mapFogs[ i ].brush == NULL ) {
636 bspFogs[ i ].brushNum = -1;
637 bspFogs[ i ].visibleSide = -1;
642 bspFogs[ i ].brushNum = mapFogs[ i ].brush->outputNum;
644 /* try to use forced visible side */
645 if ( mapFogs[ i ].visibleSide >= 0 ) {
646 bspFogs[ i ].visibleSide = mapFogs[ i ].visibleSide;
650 /* find visible side */
651 for ( j = 0; j < 6; j++ )
653 if ( mapFogs[ i ].brush->sides[ j ].visibleHull != NULL ) {
654 Sys_Printf( "Fog %d has visible side %d\n", i, j );
655 bspFogs[ i ].visibleSide = j;
667 sets up a new brush model
670 void BeginModel( void ){
675 vec3_t lgMins, lgMaxs; /* ydnar: lightgrid mins/maxs */
681 AUTOEXPAND_BY_REALLOC_BSP( Models, 256 );
683 /* get model and entity */
684 mod = &bspModels[ numBSPModels ];
685 e = &entities[ mapEntityNum ];
687 /* ydnar: lightgrid mins/maxs */
688 ClearBounds( lgMins, lgMaxs );
690 /* bound the brushes */
691 ClearBounds( mins, maxs );
692 for ( b = e->brushes; b; b = b->next )
694 /* ignore non-real brushes (origin, etc) */
695 if ( b->numsides == 0 ) {
698 AddPointToBounds( b->mins, mins, maxs );
699 AddPointToBounds( b->maxs, mins, maxs );
701 /* ydnar: lightgrid bounds */
702 if ( b->compileFlags & C_LIGHTGRID ) {
703 AddPointToBounds( b->mins, lgMins, lgMaxs );
704 AddPointToBounds( b->maxs, lgMins, lgMaxs );
709 for ( p = e->patches; p; p = p->next )
711 for ( i = 0; i < ( p->mesh.width * p->mesh.height ); i++ )
712 AddPointToBounds( p->mesh.verts[i].xyz, mins, maxs );
715 /* ydnar: lightgrid mins/maxs */
716 if ( lgMins[ 0 ] < 99999 ) {
717 /* use lightgrid bounds */
718 VectorCopy( lgMins, mod->mins );
719 VectorCopy( lgMaxs, mod->maxs );
723 /* use brush/patch bounds */
724 VectorCopy( mins, mod->mins );
725 VectorCopy( maxs, mod->maxs );
729 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 ] );
730 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 ] );
733 mod->firstBSPSurface = numBSPDrawSurfaces;
734 mod->firstBSPBrush = numBSPBrushes;
742 finish a model's processing
745 void EndModel( entity_t *e, node_t *headnode ){
750 Sys_FPrintf( SYS_VRB, "--- EndModel ---\n" );
753 mod = &bspModels[ numBSPModels ];
754 EmitDrawNode_r( headnode );
756 /* set surfaces and brushes */
757 mod->numBSPSurfaces = numBSPDrawSurfaces - mod->firstBSPSurface;
758 mod->firstBSPBrush = e->firstBrush;
759 mod->numBSPBrushes = e->numBrushes;
761 /* increment model count */