]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/writebsp.c
591ccdc7d0c6911e1e218b7a53c56ccfd686e221
[xonotic/netradiant.git] / tools / quake3 / q3map2 / writebsp.c
1 /* -------------------------------------------------------------------------------
2
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6 This file is part of GtkRadiant.
7
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.
12
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.
17
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
21
22 ----------------------------------------------------------------------------------
23
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."
26
27 ------------------------------------------------------------------------------- */
28
29
30
31 /* marker */
32 #define WRITEBSP_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /*
42 EmitShader()
43 emits a bsp shader entry
44 */
45
46 int     EmitShader( const char *shader, int *contentFlags, int *surfaceFlags )
47 {
48         int                             i;
49         shaderInfo_t    *si;
50         
51         
52         /* handle special cases */
53         if( shader == NULL )
54                 shader = "noshader";
55         
56         /* try to find an existing shader */
57         for( i = 0; i < numBSPShaders; i++ )
58         {
59                 /* ydnar: handle custom surface/content flags */
60                 if( surfaceFlags != NULL && bspShaders[ i ].surfaceFlags != *surfaceFlags )
61                         continue;
62                 if( contentFlags != NULL && bspShaders[ i ].contentFlags != *contentFlags )
63                         continue;
64                 
65                 /* compare name */
66                 if( !Q_stricmp( shader, bspShaders[ i ].shader ) )
67                         return i;
68         }
69         
70         /* get shaderinfo */
71         si = ShaderInfoForShader( shader );
72         
73         /* emit a new shader */
74         if( i == MAX_MAP_SHADERS )
75                 Error( "MAX_MAP_SHADERS" );
76         numBSPShaders++;
77         strcpy( bspShaders[ i ].shader, shader );
78         bspShaders[ i ].surfaceFlags = si->surfaceFlags;
79         bspShaders[ i ].contentFlags = si->contentFlags;
80         
81         /* handle custom content/surface flags */
82         if( surfaceFlags != NULL )
83                 bspShaders[ i ].surfaceFlags = *surfaceFlags;
84         if( contentFlags != NULL )
85                 bspShaders[ i ].contentFlags = *contentFlags;
86         
87         /* recursively emit any damage shaders */
88         if( si->damageShader != NULL && si->damageShader[ 0 ] != '\0' )
89         {
90                 Sys_FPrintf( SYS_VRB, "Shader %s has damage shader %s\n", si->shader, si->damageShader );
91                 EmitShader( si->damageShader, NULL, NULL );
92         }
93         
94         /* return it */
95         return i;
96 }
97
98
99
100 /*
101 EmitPlanes()
102 there is no oportunity to discard planes, because all of the original
103 brushes will be saved in the map
104 */
105
106 void EmitPlanes( void )
107 {
108         int                     i;
109         bspPlane_t      *bp;
110         plane_t         *mp;
111         
112         
113         /* walk plane list */
114         mp = mapplanes;
115         for( i = 0; i < nummapplanes; i++, mp++ )
116         {
117                 bp = &bspPlanes[ numBSPPlanes ];
118                 VectorCopy( mp->normal, bp->normal );
119                 bp->dist = mp->dist;
120                 numBSPPlanes++;
121         }
122         
123         /* emit some statistics */
124         Sys_FPrintf( SYS_VRB, "%9d BSP planes\n", numBSPPlanes );
125 }
126
127
128
129 /*
130 EmitLeaf()
131 emits a leafnode to the bsp file
132 */
133
134 void EmitLeaf( node_t *node )
135 {
136         bspLeaf_t               *leaf_p;
137         brush_t                 *b;
138         drawSurfRef_t   *dsr;
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( (size_t) 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                         /* check count */
463                         if( numBSPBrushSides == MAX_MAP_BRUSHSIDES )
464                                 Error( "MAX_MAP_BRUSHSIDES ");
465                         
466                         /* emit side */
467                         b->sides[ j ].outputNum = numBSPBrushSides;
468                         cp = &bspBrushSides[ numBSPBrushSides ];
469                         db->numSides++;
470                         numBSPBrushSides++;
471                         cp->planeNum = b->sides[ j ].planenum;
472                         
473                         /* emit shader */
474                         if( b->sides[ j ].shaderInfo )
475                                 cp->shaderNum = EmitShader( b->sides[ j ].shaderInfo->shader, &b->sides[ j ].shaderInfo->contentFlags, &b->sides[ j ].shaderInfo->surfaceFlags );
476                         else
477                                 cp->shaderNum = EmitShader( NULL, NULL, NULL );
478                 }
479         }
480 }
481
482
483
484 /*
485 EmitFogs() - ydnar
486 turns map fogs into bsp fogs
487 */
488
489 void EmitFogs( void )
490 {
491         int                     i, j;
492         
493         
494         /* setup */
495         numBSPFogs = numMapFogs;
496         
497         /* walk list */
498         for( i = 0; i < numMapFogs; i++ )
499         {
500                 /* set shader */
501                 strcpy( bspFogs[ i ].shader, mapFogs[ i ].si->shader );
502                 
503                 /* global fog doesn't have an associated brush */
504                 if( mapFogs[ i ].brush == NULL )
505                 {
506                         bspFogs[ i ].brushNum = -1;
507                         bspFogs[ i ].visibleSide = -1;
508                 }
509                 else
510                 {
511                         /* set brush */
512                         bspFogs[ i ].brushNum = mapFogs[ i ].brush->outputNum;
513                         
514                         /* try to use forced visible side */
515                         if( mapFogs[ i ].visibleSide >= 0 )
516                         {
517                                 bspFogs[ i ].visibleSide = mapFogs[ i ].visibleSide;
518                                 continue;
519                         }
520                         
521                         /* find visible side */
522                         for( j = 0; j < 6; j++ )
523                         {
524                                 if( mapFogs[ i ].brush->sides[ j ].visibleHull != NULL )
525                                 {
526                                         Sys_Printf( "Fog %d has visible side %d\n", i, j );
527                                         bspFogs[ i ].visibleSide = j;
528                                         break;
529                                 }
530                         }
531                 }
532         }
533 }
534
535
536
537 /*
538 BeginModel()
539 sets up a new brush model
540 */
541
542 void BeginModel( void )
543 {
544         bspModel_t      *mod;
545         brush_t         *b;
546         entity_t        *e;
547         vec3_t          mins, maxs;
548         vec3_t          lgMins, lgMaxs;         /* ydnar: lightgrid mins/maxs */
549         parseMesh_t     *p;
550         int                     i;
551         
552         
553         /* test limits */
554         if( numBSPModels == MAX_MAP_MODELS )
555                 Error( "MAX_MAP_MODELS" );
556         
557         /* get model and entity */
558         mod = &bspModels[ numBSPModels ];
559         e = &entities[ mapEntityNum ];
560         
561         /* ydnar: lightgrid mins/maxs */
562         ClearBounds( lgMins, lgMaxs );
563         
564         /* bound the brushes */
565         ClearBounds( mins, maxs );
566         for ( b = e->brushes; b; b = b->next )
567         {
568                 /* ignore non-real brushes (origin, etc) */
569                 if( b->numsides == 0 )
570                         continue;
571                 AddPointToBounds( b->mins, mins, maxs );
572                 AddPointToBounds( b->maxs, mins, maxs );
573                 
574                 /* ydnar: lightgrid bounds */
575                 if( b->compileFlags & C_LIGHTGRID )
576                 {
577                         AddPointToBounds( b->mins, lgMins, lgMaxs );
578                         AddPointToBounds( b->maxs, lgMins, lgMaxs );
579                 }
580         }
581         
582         /* bound patches */
583         for( p = e->patches; p; p = p->next )
584         {
585                 for( i = 0; i < (p->mesh.width * p->mesh.height); i++ )
586                         AddPointToBounds( p->mesh.verts[i].xyz, mins, maxs );
587         }
588         
589         /* ydnar: lightgrid mins/maxs */
590         if( lgMins[ 0 ] < 99999 )
591         {
592                 /* use lightgrid bounds */
593                 VectorCopy( lgMins, mod->mins );
594                 VectorCopy( lgMaxs, mod->maxs );
595         }
596         else
597         {
598                 /* use brush/patch bounds */
599                 VectorCopy( mins, mod->mins );
600                 VectorCopy( maxs, mod->maxs );
601         }
602         
603         /* note size */
604         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 ] );
605         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 ] );
606         
607         /* set firsts */
608         mod->firstBSPSurface = numBSPDrawSurfaces;
609         mod->firstBSPBrush = numBSPBrushes;
610 }
611
612
613
614
615 /*
616 EndModel()
617 finish a model's processing
618 */
619
620 void EndModel( entity_t *e, node_t *headnode )
621 {
622         bspModel_t      *mod;
623         
624         
625         /* note it */
626         Sys_FPrintf( SYS_VRB, "--- EndModel ---\n" );
627         
628         /* emit the bsp */
629         mod = &bspModels[ numBSPModels ];
630         EmitDrawNode_r( headnode );
631         
632         /* set surfaces and brushes */
633         mod->numBSPSurfaces = numBSPDrawSurfaces - mod->firstBSPSurface;
634         mod->firstBSPBrush = e->firstBrush;
635         mod->numBSPBrushes = e->numBrushes;
636         
637         /* increment model count */
638         numBSPModels++;
639 }
640
641
642