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