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