]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/writebsp.c
Merge branch 'nomagicpath' into 'master'
[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         /* get shaderinfo */
74         si = ShaderInfoForShader( shader );
75
76         /* emit a new shader */
77         AUTOEXPAND_BY_REALLOC_BSP( Shaders, 1024 );
78
79         numBSPShaders++;
80         strcpy( bspShaders[ i ].shader, shader );
81         bspShaders[ i ].surfaceFlags = si->surfaceFlags;
82         bspShaders[ i ].contentFlags = si->contentFlags;
83
84         /* handle custom content/surface flags */
85         if ( surfaceFlags != NULL ) {
86                 bspShaders[ i ].surfaceFlags = *surfaceFlags;
87         }
88         if ( contentFlags != NULL ) {
89                 bspShaders[ i ].contentFlags = *contentFlags;
90         }
91
92         /* recursively emit any damage shaders */
93         if ( si->damageShader != NULL && si->damageShader[ 0 ] != '\0' ) {
94                 Sys_FPrintf( SYS_VRB, "Shader %s has damage shader %s\n", si->shader, si->damageShader );
95                 EmitShader( si->damageShader, NULL, NULL );
96         }
97
98         /* return it */
99         return i;
100 }
101
102
103
104 /*
105    EmitPlanes()
106    there is no oportunity to discard planes, because all of the original
107    brushes will be saved in the map
108  */
109
110 void EmitPlanes( void ){
111         int i;
112         bspPlane_t  *bp;
113         plane_t     *mp;
114
115
116         /* walk plane list */
117         mp = mapplanes;
118         for ( i = 0; i < nummapplanes; i++, mp++ )
119         {
120                 AUTOEXPAND_BY_REALLOC_BSP( Planes, 1024 );
121                 bp = &bspPlanes[ numBSPPlanes ];
122                 VectorCopy( mp->normal, bp->normal );
123                 bp->dist = mp->dist;
124                 numBSPPlanes++;
125         }
126
127         /* emit some statistics */
128         Sys_FPrintf( SYS_VRB, "%9d BSP planes\n", numBSPPlanes );
129 }
130
131
132
133 /*
134    EmitLeaf()
135    emits a leafnode to the bsp file
136  */
137
138 void EmitLeaf( node_t *node ){
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
149         leaf_p = &bspLeafs[numBSPLeafs];
150         numBSPLeafs++;
151
152         leaf_p->cluster = node->cluster;
153         leaf_p->area = node->area;
154
155         /* emit bounding box */
156         VectorCopy( node->mins, leaf_p->mins );
157         VectorCopy( node->maxs, leaf_p->maxs );
158
159         /* emit leaf brushes */
160         leaf_p->firstBSPLeafBrush = numBSPLeafBrushes;
161         for ( b = node->brushlist; b; b = b->next )
162         {
163                 /* something is corrupting brushes */
164                 if ( (size_t) b < 256 ) {
165                         Sys_FPrintf( SYS_WRN, "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
183         /* add the drawSurfRef_t drawsurfs */
184         leaf_p->firstBSPLeafSurface = numBSPLeafSurfaces;
185         for ( dsr = node->drawSurfReferences; dsr; dsr = dsr->nextRef )
186         {
187                 AUTOEXPAND_BY_REALLOC_BSP( LeafSurfaces, 1024 );
188                 bspLeafSurfaces[ numBSPLeafSurfaces ] = dsr->outputNum;
189                 numBSPLeafSurfaces++;
190         }
191
192         leaf_p->numBSPLeafSurfaces = numBSPLeafSurfaces - leaf_p->firstBSPLeafSurface;
193 }
194
195
196 /*
197    EmitDrawNode_r()
198    recursively emit the bsp nodes
199  */
200
201 int EmitDrawNode_r( node_t *node ){
202         bspNode_t   *n;
203         int i, n0;
204
205
206         /* check for leafnode */
207         if ( node->planenum == PLANENUM_LEAF ) {
208                 EmitLeaf( node );
209                 return -numBSPLeafs;
210         }
211
212         /* emit a node */
213         AUTOEXPAND_BY_REALLOC_BSP( Nodes, 1024 );
214         n0 = numBSPNodes;
215         n = &bspNodes[ n0 ];
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         }
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                         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                         // n may have become invalid here, so...
240                         n = &bspNodes[ n0 ];
241                 }
242         }
243
244         return n - bspNodes;
245 }
246
247
248
249 /*
250    ============
251    SetModelNumbers
252    ============
253  */
254 void SetModelNumbers( void ){
255         int i;
256         int models;
257         char value[10];
258
259         models = 1;
260         for ( i = 1 ; i < numEntities ; i++ ) {
261                 if ( entities[i].brushes || entities[i].patches ) {
262                         sprintf( value, "*%i", models );
263                         models++;
264                         SetKeyValue( &entities[i], "model", value );
265                 }
266         }
267
268 }
269
270
271
272
273 /*
274    SetLightStyles()
275    sets style keys for entity lights
276  */
277
278 void SetLightStyles( void ){
279         int i, j, style, numStyles;
280         const char  *t;
281         entity_t    *e;
282         epair_t     *ep, *next;
283         char value[ 10 ];
284         char lightTargets[ MAX_SWITCHED_LIGHTS ][ 64 ];
285         int lightStyles[ MAX_SWITCHED_LIGHTS ];
286
287         /* -keeplights option: force lights to be kept and ignore what the map file says */
288         if ( keepLights ) {
289                 SetKeyValue( &entities[0], "_keepLights", "1" );
290         }
291
292         /* ydnar: determine if we keep lights in the bsp */
293         if ( KeyExists( &entities[ 0 ], "_keepLights" ) == qtrue ) {
294                 t = ValueForKey( &entities[ 0 ], "_keepLights" );
295                 keepLights = ( t[ 0 ] == '1' ) ? qtrue : qfalse;
296         }
297
298         /* any light that is controlled (has a targetname) must have a unique style number generated for it */
299         numStyles = 0;
300         for ( i = 1; i < numEntities; i++ )
301         {
302                 e = &entities[ i ];
303
304                 t = ValueForKey( e, "classname" );
305                 if ( Q_strncasecmp( t, "light", 5 ) ) {
306                         continue;
307                 }
308                 t = ValueForKey( e, "targetname" );
309                 if ( t[ 0 ] == '\0' ) {
310                         /* ydnar: strip the light from the BSP file */
311                         if ( keepLights == qfalse ) {
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
335                 /* find this targetname */
336                 for ( j = 0; j < numStyles; j++ )
337                         if ( lightStyles[ j ] == style && !strcmp( lightTargets[ j ], t ) ) {
338                                 break;
339                         }
340
341                 /* add a new style */
342                 if ( j >= numStyles ) {
343                         if ( numStyles == MAX_SWITCHED_LIGHTS ) {
344                                 Error( "MAX_SWITCHED_LIGHTS (%d) exceeded, reduce the number of lights with targetnames", MAX_SWITCHED_LIGHTS );
345                         }
346                         strcpy( lightTargets[ j ], t );
347                         lightStyles[ j ] = style;
348                         numStyles++;
349                 }
350
351                 /* set explicit style */
352                 sprintf( value, "%d", 32 + j );
353                 SetKeyValue( e, "style", value );
354
355                 /* set old style */
356                 if ( style != LS_NORMAL ) {
357                         sprintf( value, "%d", style );
358                         SetKeyValue( e, "switch_style", value );
359                 }
360         }
361
362         /* emit some statistics */
363         Sys_FPrintf( SYS_VRB, "%9d light entities stripped\n", numStrippedLights );
364 }
365
366
367
368 /*
369    BeginBSPFile()
370    starts a new bsp file
371  */
372
373 void BeginBSPFile( void ){
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         AUTOEXPAND_BY_REALLOC_BSP( DrawIndexes, 1024 );
388         bspDrawIndexes[ 0 ] = 0;
389         bspDrawIndexes[ 1 ] = 1;
390         bspDrawIndexes[ 2 ] = 2;
391         bspDrawIndexes[ 3 ] = 0;
392         bspDrawIndexes[ 4 ] = 2;
393         bspDrawIndexes[ 5 ] = 3;
394 }
395
396
397
398 /*
399    EndBSPFile()
400    finishes a new bsp and writes to disk
401  */
402
403 void EndBSPFile( qboolean do_write, const char *BSPFilePath, const char *surfaceFilePath ){
404
405         Sys_FPrintf( SYS_VRB, "--- EndBSPFile ---\n" );
406
407         EmitPlanes();
408
409         numBSPEntities = numEntities;
410         UnparseEntities();
411
412         if ( do_write ) {
413                 /* write the surface extra file */
414                 WriteSurfaceExtraFile( surfaceFilePath );
415
416                 /* write the bsp */
417                 Sys_Printf( "Writing %s\n", BSPFilePath );
418                 WriteBSPFile( BSPFilePath );
419         }
420 }
421
422
423
424 /*
425    EmitBrushes()
426    writes the brush list to the bsp
427  */
428
429 void EmitBrushes( brush_t *brushes, int *firstBrush, int *numBrushes ){
430         int j;
431         brush_t         *b;
432         bspBrush_t      *db;
433         bspBrushSide_t  *cp;
434
435
436         /* set initial brush */
437         if ( firstBrush != NULL ) {
438                 *firstBrush = numBSPBrushes;
439         }
440         if ( numBrushes != NULL ) {
441                 *numBrushes = 0;
442         }
443
444         /* walk list of brushes */
445         for ( b = brushes; b != NULL; b = b->next )
446         {
447                 /* check limits */
448                 AUTOEXPAND_BY_REALLOC_BSP( Brushes, 1024 );
449
450                 /* get bsp brush */
451                 b->outputNum = numBSPBrushes;
452                 db = &bspBrushes[ numBSPBrushes ];
453                 numBSPBrushes++;
454                 if ( numBrushes != NULL ) {
455                         ( *numBrushes )++;
456                 }
457
458                 db->shaderNum = EmitShader( b->contentShader->shader, &b->contentShader->contentFlags, &b->contentShader->surfaceFlags );
459                 db->firstSide = numBSPBrushSides;
460
461                 /* walk sides */
462                 db->numSides = 0;
463                 for ( j = 0; j < b->numsides; j++ )
464                 {
465                         /* set output number to bogus initially */
466                         b->sides[ j ].outputNum = -1;
467
468                         /* check count */
469                         AUTOEXPAND_BY_REALLOC_BSP( BrushSides, 1024 );
470
471                         /* emit side */
472                         b->sides[ j ].outputNum = numBSPBrushSides;
473                         cp = &bspBrushSides[ numBSPBrushSides ];
474                         db->numSides++;
475                         numBSPBrushSides++;
476                         cp->planeNum = b->sides[ j ].planenum;
477
478                         /* emit shader */
479                         if ( b->sides[ j ].shaderInfo ) {
480                                 cp->shaderNum = EmitShader( b->sides[ j ].shaderInfo->shader, &b->sides[ j ].shaderInfo->contentFlags, &b->sides[ j ].shaderInfo->surfaceFlags );
481                         }
482                         else{
483                                 cp->shaderNum = EmitShader( NULL, NULL, NULL );
484                         }
485                 }
486         }
487 }
488
489
490
491 /*
492    EmitFogs() - ydnar
493    turns map fogs into bsp fogs
494  */
495
496 void EmitFogs( void ){
497         int i, j;
498
499
500         /* setup */
501         numBSPFogs = numMapFogs;
502
503         /* walk list */
504         for ( i = 0; i < numMapFogs; i++ )
505         {
506                 /* set shader */
507                 strcpy( bspFogs[ i ].shader, mapFogs[ i ].si->shader );
508
509                 /* global fog doesn't have an associated brush */
510                 if ( mapFogs[ i ].brush == NULL ) {
511                         bspFogs[ i ].brushNum = -1;
512                         bspFogs[ i ].visibleSide = -1;
513                 }
514                 else
515                 {
516                         /* set brush */
517                         bspFogs[ i ].brushNum = mapFogs[ i ].brush->outputNum;
518
519                         /* try to use forced visible side */
520                         if ( mapFogs[ i ].visibleSide >= 0 ) {
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                                         Sys_Printf( "Fog %d has visible side %d\n", i, j );
530                                         bspFogs[ i ].visibleSide = j;
531                                         break;
532                                 }
533                         }
534                 }
535         }
536 }
537
538
539
540 /*
541    BeginModel()
542    sets up a new brush model
543  */
544
545 void BeginModel( void ){
546         bspModel_t  *mod;
547         brush_t     *b;
548         entity_t    *e;
549         vec3_t mins, maxs;
550         vec3_t lgMins, lgMaxs;          /* ydnar: lightgrid mins/maxs */
551         parseMesh_t *p;
552         int i;
553
554
555         /* test limits */
556         AUTOEXPAND_BY_REALLOC_BSP( Models, 256 );
557
558         /* get model and entity */
559         mod = &bspModels[ numBSPModels ];
560         e = &entities[ mapEntityNum ];
561
562         /* ydnar: lightgrid mins/maxs */
563         ClearBounds( lgMins, lgMaxs );
564
565         /* bound the brushes */
566         ClearBounds( mins, maxs );
567         for ( b = e->brushes; b; b = b->next )
568         {
569                 /* ignore non-real brushes (origin, etc) */
570                 if ( b->numsides == 0 ) {
571                         continue;
572                 }
573                 AddPointToBounds( b->mins, mins, maxs );
574                 AddPointToBounds( b->maxs, mins, maxs );
575
576                 /* ydnar: lightgrid bounds */
577                 if ( b->compileFlags & C_LIGHTGRID ) {
578                         AddPointToBounds( b->mins, lgMins, lgMaxs );
579                         AddPointToBounds( b->maxs, lgMins, lgMaxs );
580                 }
581         }
582
583         /* bound patches */
584         for ( p = e->patches; p; p = p->next )
585         {
586                 for ( i = 0; i < ( p->mesh.width * p->mesh.height ); i++ )
587                         AddPointToBounds( p->mesh.verts[i].xyz, mins, maxs );
588         }
589
590         /* ydnar: lightgrid mins/maxs */
591         if ( lgMins[ 0 ] < 99999 ) {
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         bspModel_t  *mod;
622
623
624         /* note it */
625         Sys_FPrintf( SYS_VRB, "--- EndModel ---\n" );
626
627         /* emit the bsp */
628         mod = &bspModels[ numBSPModels ];
629         EmitDrawNode_r( headnode );
630
631         /* set surfaces and brushes */
632         mod->numBSPSurfaces = numBSPDrawSurfaces - mod->firstBSPSurface;
633         mod->firstBSPBrush = e->firstBrush;
634         mod->numBSPBrushes = e->numBrushes;
635
636         /* increment model count */
637         numBSPModels++;
638 }