]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/writebsp.c
7fb51d83630e85baa920a8836f4f1107abfb2796
[xonotic/netradiant.git] / tools / quake3 / q3map2 / writebsp.c
1 /*
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 ----------------------------------------------------------------------------------
22
23 This code has been altered significantly from its original form, to support
24 several games based on the Quake III Arena engine, in the form of "Q3Map2."
25
26 ------------------------------------------------------------------------------- */
27
28
29
30 /* marker */
31 #define WRITEBSP_C
32
33
34
35 /* dependencies */
36 #include "q3map2.h"
37
38
39
40 /*
41 EmitShader()
42 emits a bsp shader entry
43 */
44
45 int     EmitShader( const char *shader, int *contentFlags, int *surfaceFlags )
46 {
47         int                             i;
48         shaderInfo_t    *si;
49         
50         
51         /* handle special cases */
52         if( shader == NULL )
53                 shader = "noshader";
54         
55         /* try to find an existing shader */
56         for( i = 0; i < numBSPShaders; i++ )
57         {
58                 /* ydnar: handle custom surface/content flags */
59                 if( surfaceFlags != NULL && bspShaders[ i ].surfaceFlags != *surfaceFlags )
60                         continue;
61                 if( contentFlags != NULL && bspShaders[ i ].contentFlags != *contentFlags )
62                         continue;
63                 
64                 /* compare name */
65                 if( !Q_stricmp( shader, bspShaders[ i ].shader ) )
66                         return i;
67         }
68         
69         /* get shaderinfo */
70         si = ShaderInfoForShader( shader );
71         
72         /* emit a new shader */
73         if( i == MAX_MAP_SHADERS )
74                 Error( "MAX_MAP_SHADERS" );
75         numBSPShaders++;
76         strcpy( bspShaders[ i ].shader, shader );
77         bspShaders[ i ].surfaceFlags = si->surfaceFlags;
78         bspShaders[ i ].contentFlags = si->contentFlags;
79         
80         /* handle custom content/surface flags */
81         if( surfaceFlags != NULL )
82                 bspShaders[ i ].surfaceFlags = *surfaceFlags;
83         if( contentFlags != NULL )
84                 bspShaders[ i ].contentFlags = *contentFlags;
85         
86         /* recursively emit any damage shaders */
87         if( si->damageShader[ 0 ] != '\0' )
88         {
89                 Sys_FPrintf( SYS_VRB, "Shader %s has damage shader %s\n", si->shader, si->damageShader );
90                 EmitShader( si->damageShader, NULL, NULL );
91         }
92         
93         /* return it */
94         return i;
95 }
96
97
98
99 /*
100 EmitPlanes()
101 there is no oportunity to discard planes, because all of the original
102 brushes will be saved in the map
103 */
104
105 void EmitPlanes( void )
106 {
107         int                     i;
108         bspPlane_t      *bp;
109         plane_t         *mp;
110         
111         
112         /* walk plane list */
113         mp = mapplanes;
114         for( i = 0; i < nummapplanes; i++, mp++ )
115         {
116                 bp = &bspPlanes[ numBSPPlanes ];
117                 VectorCopy( mp->normal, bp->normal );
118                 bp->dist = mp->dist;
119                 numBSPPlanes++;
120         }
121         
122         /* emit some statistics */
123         Sys_FPrintf( SYS_VRB, "%9d BSP planes\n", numBSPPlanes );
124 }
125
126
127
128 /*
129 EmitLeaf()
130 emits a leafnode to the bsp file
131 */
132
133 void EmitLeaf( node_t *node )
134 {
135         bspLeaf_t               *leaf_p;
136         brush_t                 *b;
137         drawSurfRef_t   *dsr;
138         int                             i = 0;
139
140         
141         /* check limits */
142         if( numBSPLeafs >= MAX_MAP_LEAFS )
143                 Error( "MAX_MAP_LEAFS" );
144
145         leaf_p = &bspLeafs[numBSPLeafs];
146         numBSPLeafs++;
147
148         leaf_p->cluster = node->cluster;
149         leaf_p->area = node->area;
150
151         /* emit bounding box */
152         VectorCopy( node->mins, leaf_p->mins );
153         VectorCopy( node->maxs, leaf_p->maxs );
154         
155         /* emit leaf brushes */
156         leaf_p->firstBSPLeafBrush = numBSPLeafBrushes;
157         for( b = node->brushlist; b; b = b->next )
158         {
159                 /* something is corrupting brushes */
160                 if( (int) b < 256 )
161                 {
162                         Sys_Printf( "WARNING: Node brush list corrupted (0x%08X)\n", b );
163                         break;
164                 }
165                 //%     if( b->guard != 0xDEADBEEF )
166                 //%             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 );
167                 
168                 if( numBSPLeafBrushes >= MAX_MAP_LEAFBRUSHES )
169                         Error( "MAX_MAP_LEAFBRUSHES" );
170                 bspLeafBrushes[ numBSPLeafBrushes ] = b->original->outputNum;
171                 numBSPLeafBrushes++;
172         }
173         
174         leaf_p->numBSPLeafBrushes = numBSPLeafBrushes - leaf_p->firstBSPLeafBrush;
175         
176         /* emit leaf surfaces */
177         if( node->opaque )
178                 return;
179         
180         /* add the drawSurfRef_t drawsurfs */
181         leaf_p->firstBSPLeafSurface = numBSPLeafSurfaces;
182         for ( dsr = node->drawSurfReferences; dsr; dsr = dsr->nextRef )
183         {
184                 if( numBSPLeafSurfaces >= MAX_MAP_LEAFFACES )
185                         Error( "MAX_MAP_LEAFFACES" );
186                 bspLeafSurfaces[ numBSPLeafSurfaces ] = dsr->outputNum;
187                 numBSPLeafSurfaces++;                   
188         }
189         
190         leaf_p->numBSPLeafSurfaces = numBSPLeafSurfaces - leaf_p->firstBSPLeafSurface;
191 }
192
193
194 /*
195 EmitDrawNode_r()
196 recursively emit the bsp nodes
197 */
198
199 int EmitDrawNode_r( node_t *node )
200 {
201         bspNode_t       *n;
202         int                     i;
203         
204         
205         /* check for leafnode */
206         if( node->planenum == PLANENUM_LEAF )
207         {
208                 EmitLeaf( node );
209                 return -numBSPLeafs;
210         }
211         
212         /* emit a node */
213         if( numBSPNodes == MAX_MAP_NODES )
214                 Error( "MAX_MAP_NODES" );
215         n = &bspNodes[ numBSPNodes ];
216         numBSPNodes++;
217         
218         VectorCopy (node->mins, n->mins);
219         VectorCopy (node->maxs, n->maxs);
220
221         if (node->planenum & 1)
222                 Error ("WriteDrawNodes_r: odd planenum");
223         n->planeNum = node->planenum;
224
225         //
226         // recursively output the other nodes
227         //      
228         for (i=0 ; i<2 ; i++)
229         {
230                 if (node->children[i]->planenum == PLANENUM_LEAF)
231                 {
232                         n->children[i] = -(numBSPLeafs + 1);
233                         EmitLeaf (node->children[i]);
234                 }
235                 else
236                 {
237                         n->children[i] = numBSPNodes;   
238                         EmitDrawNode_r (node->children[i]);
239                 }
240         }
241
242         return n - bspNodes;
243 }
244
245
246
247 /*
248 ============
249 SetModelNumbers
250 ============
251 */
252 void SetModelNumbers (void)
253 {
254         int             i;
255         int             models;
256         char    value[10];
257
258         models = 1;
259         for ( i=1 ; i<numEntities ; i++ ) {
260                 if ( entities[i].brushes || entities[i].patches ) {
261                         sprintf ( value, "*%i", models );
262                         models++;
263                         SetKeyValue (&entities[i], "model", value);
264                 }
265         }
266
267 }
268
269
270
271
272 /*
273 SetLightStyles()
274 sets style keys for entity lights
275 */
276
277 void SetLightStyles( void )
278 {
279         int                     i, j, style, numStyles;
280         qboolean        keepLights;
281         const char      *t;
282         entity_t        *e;
283         epair_t         *ep, *next;
284         char            value[ 10 ];
285         char            lightTargets[ MAX_SWITCHED_LIGHTS ][ 64 ];
286         int                     lightStyles[ MAX_SWITCHED_LIGHTS ];
287         
288         
289         /* ydnar: determine if we keep lights in the bsp */
290         t = ValueForKey( &entities[ 0 ], "_keepLights" );
291         keepLights = (t[ 0 ] == '1') ? qtrue : qfalse;
292         
293         /* any light that is controlled (has a targetname) must have a unique style number generated for it */
294         numStyles = 0;
295         for( i = 1; i < numEntities; i++ )
296         {
297                 e = &entities[ i ];
298
299                 t = ValueForKey( e, "classname" );
300                 if( Q_strncasecmp( t, "light", 5 ) )
301                         continue;
302                 t = ValueForKey( e, "targetname" );
303                 if( t[ 0 ] == '\0' )
304                 {
305                         /* ydnar: strip the light from the BSP file */
306                         if( keepLights == qfalse )
307                         {
308                                 ep = e->epairs;
309                                 while( ep != NULL )
310                                 {
311                                         next = ep->next;
312                                         free( ep->key );
313                                         free( ep->value );
314                                         free( ep );
315                                         ep = next;
316                                 }
317                                 e->epairs = NULL;
318                                 numStrippedLights++;
319                         }
320                         
321                         /* next light */
322                         continue;
323                 }
324                 
325                 /* get existing style */
326                 style = IntForKey( e, "style" );
327                 if( style < LS_NORMAL || style > LS_NONE )
328                         Error( "Invalid lightstyle (%d) on entity %d", style, i );
329                 
330                 /* find this targetname */
331                 for( j = 0; j < numStyles; j++ )
332                         if( lightStyles[ j ] == style && !strcmp( lightTargets[ j ], t ) )
333                                 break;
334                 
335                 /* add a new style */
336                 if( j >= numStyles )
337                 {
338                         if( numStyles == MAX_SWITCHED_LIGHTS )
339                                 Error( "MAX_SWITCHED_LIGHTS (%d) exceeded, reduce the number of lights with targetnames", MAX_SWITCHED_LIGHTS );
340                         strcpy( lightTargets[ j ], t );
341                         lightStyles[ j ] = style;
342                         numStyles++;
343                 }
344                 
345                 /* set explicit style */
346                 sprintf( value, "%d", 32 + j );
347                 SetKeyValue( e, "style", value );
348                 
349                 /* set old style */
350                 if( style != LS_NORMAL )
351                 {
352                         sprintf( value, "%d", style );
353                         SetKeyValue( e, "switch_style", value );
354                 }
355         }
356         
357         /* emit some statistics */
358         Sys_FPrintf( SYS_VRB, "%9d light entities stripped\n", numStrippedLights );
359 }
360
361
362
363 /*
364 BeginBSPFile()
365 starts a new bsp file
366 */
367
368 void BeginBSPFile( void )
369 {
370         /* these values may actually be initialized if the file existed when loaded, so clear them explicitly */
371         numBSPModels = 0;
372         numBSPNodes = 0;
373         numBSPBrushSides = 0;
374         numBSPLeafSurfaces = 0;
375         numBSPLeafBrushes = 0;
376         
377         /* leave leaf 0 as an error, because leafs are referenced as negative number nodes */
378         numBSPLeafs = 1;
379         
380         
381         /* ydnar: gs mods: set the first 6 drawindexes to 0 1 2 2 1 3 for triangles and quads */
382         numBSPDrawIndexes = 6;
383         bspDrawIndexes[ 0 ] = 0;
384         bspDrawIndexes[ 1 ] = 1;
385         bspDrawIndexes[ 2 ] = 2;
386         bspDrawIndexes[ 3 ] = 0;
387         bspDrawIndexes[ 4 ] = 2;
388         bspDrawIndexes[ 5 ] = 3;
389 }
390
391
392
393 /*
394 EndBSPFile()
395 finishes a new bsp and writes to disk
396 */
397
398 void EndBSPFile( void )
399 {
400         char    path[ 1024 ];
401         
402
403         EmitPlanes();
404         
405         numBSPEntities = numEntities;
406         UnparseEntities();
407         
408         /* write the surface extra file */
409         WriteSurfaceExtraFile( source );
410         
411         /* write the bsp */
412         sprintf( path, "%s.bsp", source );
413         Sys_Printf( "Writing %s\n", path );
414         WriteBSPFile( path );
415 }
416
417
418
419 /*
420 EmitBrushes()
421 writes the brush list to the bsp
422 */
423
424 void EmitBrushes( brush_t *brushes, int *firstBrush, int *numBrushes )
425 {
426         int                             j;
427         brush_t                 *b;
428         bspBrush_t              *db;
429         bspBrushSide_t  *cp;
430         
431         
432         /* set initial brush */
433         if( firstBrush != NULL )
434                 *firstBrush = numBSPBrushes;
435         if( numBrushes != NULL )
436                 *numBrushes = 0;
437         
438         /* walk list of brushes */
439         for( b = brushes; b != NULL; b = b->next )
440         {
441                 /* check limits */
442                 if( numBSPBrushes == MAX_MAP_BRUSHES )
443                         Error( "MAX_MAP_BRUSHES (%d)", numBSPBrushes );
444                 
445                 /* get bsp brush */
446                 b->outputNum = numBSPBrushes;
447                 db = &bspBrushes[ numBSPBrushes ];
448                 numBSPBrushes++;
449                 if( numBrushes != NULL )
450                         (*numBrushes)++;
451                 
452                 db->shaderNum = EmitShader( b->contentShader->shader, &b->contentShader->contentFlags, &b->contentShader->surfaceFlags );
453                 db->firstSide = numBSPBrushSides;
454                 
455                 /* walk sides */
456                 db->numSides = 0;
457                 for( j = 0; j < b->numsides; j++ )
458                 {
459                         /* set output number to bogus initially */
460                         b->sides[ j ].outputNum = -1;
461                         
462                         /* don't emit generated backSide sides */
463                         if ( b->sides[ j ].backSide )
464                                 continue;
465                         
466                         /* check count */
467                         if( numBSPBrushSides == MAX_MAP_BRUSHSIDES )
468                                 Error( "MAX_MAP_BRUSHSIDES ");
469                         
470                         /* emit side */
471                         b->sides[ j ].outputNum = numBSPBrushSides;
472                         cp = &bspBrushSides[ numBSPBrushSides ];
473                         db->numSides++;
474                         numBSPBrushSides++;
475                         cp->planeNum = b->sides[ j ].planenum;
476                         
477                         /* emit shader */
478                         if( b->sides[ j ].shaderInfo )
479                                 cp->shaderNum = EmitShader( b->sides[ j ].shaderInfo->shader, &b->sides[ j ].shaderInfo->contentFlags, &b->sides[ j ].shaderInfo->surfaceFlags );
480                         else
481                                 cp->shaderNum = EmitShader( NULL, NULL, NULL );
482                 }
483         }
484 }
485
486
487
488 /*
489 EmitFogs() - ydnar
490 turns map fogs into bsp fogs
491 */
492
493 void EmitFogs( void )
494 {
495         int                     i, j;
496         
497         
498         /* setup */
499         numBSPFogs = numMapFogs;
500         
501         /* walk list */
502         for( i = 0; i < numMapFogs; i++ )
503         {
504                 /* set shader */
505                 strcpy( bspFogs[ i ].shader, mapFogs[ i ].si->shader );
506                 
507                 /* global fog doesn't have an associated brush */
508                 if( mapFogs[ i ].brush == NULL )
509                 {
510                         bspFogs[ i ].brushNum = -1;
511                         bspFogs[ i ].visibleSide = -1;
512                 }
513                 else
514                 {
515                         /* set brush */
516                         bspFogs[ i ].brushNum = mapFogs[ i ].brush->outputNum;
517                         
518                         /* try to use forced visible side */
519                         if( mapFogs[ i ].visibleSide >= 0 )
520                         {
521                                 bspFogs[ i ].visibleSide = mapFogs[ i ].visibleSide;
522                                 continue;
523                         }
524                         
525                         /* find visible side */
526                         for( j = 0; j < 6; j++ )
527                         {
528                                 if( mapFogs[ i ].brush->sides[ j ].visibleHull != NULL )
529                                 {
530                                         Sys_Printf( "Fog %d has visible side %d\n", i, j );
531                                         bspFogs[ i ].visibleSide = j;
532                                         break;
533                                 }
534                         }
535                 }
536         }
537 }
538
539
540
541 /*
542 BeginModel()
543 sets up a new brush model
544 */
545
546 void BeginModel( void )
547 {
548         bspModel_t      *mod;
549         brush_t         *b;
550         entity_t        *e;
551         vec3_t          mins, maxs;
552         vec3_t          lgMins, lgMaxs;         /* ydnar: lightgrid mins/maxs */
553         parseMesh_t     *p;
554         int                     i;
555         
556         
557         /* test limits */
558         if( numBSPModels == MAX_MAP_MODELS )
559                 Error( "MAX_MAP_MODELS" );
560         
561         /* get model and entity */
562         mod = &bspModels[ numBSPModels ];
563         e = &entities[ mapEntityNum ];
564         
565         /* ydnar: lightgrid mins/maxs */
566         ClearBounds( lgMins, lgMaxs );
567         
568         /* bound the brushes */
569         ClearBounds( mins, maxs );
570         for ( b = e->brushes; b; b = b->next )
571         {
572                 /* ignore non-real brushes (origin, etc) */
573                 if( b->numsides == 0 )
574                         continue;
575                 AddPointToBounds( b->mins, mins, maxs );
576                 AddPointToBounds( b->maxs, mins, maxs );
577                 
578                 /* ydnar: lightgrid bounds */
579                 if( b->compileFlags & C_LIGHTGRID )
580                 {
581                         AddPointToBounds( b->mins, lgMins, lgMaxs );
582                         AddPointToBounds( b->maxs, lgMins, lgMaxs );
583                 }
584         }
585         
586         /* bound patches */
587         for( p = e->patches; p; p = p->next )
588         {
589                 for( i = 0; i < (p->mesh.width * p->mesh.height); i++ )
590                         AddPointToBounds( p->mesh.verts[i].xyz, mins, maxs );
591         }
592         
593         /* ydnar: lightgrid mins/maxs */
594         if( lgMins[ 0 ] < 99999 )
595         {
596                 /* use lightgrid bounds */
597                 VectorCopy( lgMins, mod->mins );
598                 VectorCopy( lgMaxs, mod->maxs );
599         }
600         else
601         {
602                 /* use brush/patch bounds */
603                 VectorCopy( mins, mod->mins );
604                 VectorCopy( maxs, mod->maxs );
605         }
606         
607         /* note size */
608         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 ] );
609         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 ] );
610         
611         /* set firsts */
612         mod->firstBSPSurface = numBSPDrawSurfaces;
613         mod->firstBSPBrush = numBSPBrushes;
614 }
615
616
617
618
619 /*
620 EndModel()
621 finish a model's processing
622 */
623
624 void EndModel( entity_t *e, node_t *headnode )
625 {
626         bspModel_t      *mod;
627         
628         
629         /* note it */
630         Sys_FPrintf( SYS_VRB, "--- EndModel ---\n" );
631         
632         /* emit the bsp */
633         mod = &bspModels[ numBSPModels ];
634         EmitDrawNode_r( headnode );
635         
636         /* set surfaces and brushes */
637         mod->numBSPSurfaces = numBSPDrawSurfaces - mod->firstBSPSurface;
638         mod->firstBSPBrush = e->firstBrush;
639         mod->numBSPBrushes = e->numBrushes;
640         
641         /* increment model count */
642         numBSPModels++;
643 }
644
645
646