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