]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/bsp.c
9164c4c24f1e98ca8c60bde8195ab65b2b02a256
[xonotic/netradiant.git] / tools / quake3 / q3map2 / bsp.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 BSP_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /* -------------------------------------------------------------------------------
42
43    functions
44
45    ------------------------------------------------------------------------------- */
46
47
48 /*
49    ProcessAdvertisements()
50    copies advertisement info into the BSP structures
51  */
52
53 static void ProcessAdvertisements( void ) {
54         int i;
55         const char*         className;
56         const char*         modelKey;
57         int modelNum;
58         bspModel_t*         adModel;
59         bspDrawSurface_t*   adSurface;
60
61         Sys_FPrintf( SYS_VRB, "--- ProcessAdvertisements ---\n" );
62
63         for ( i = 0; i < numEntities; i++ ) {
64
65                 /* is an advertisement? */
66                 className = ValueForKey( &entities[ i ], "classname" );
67
68                 if ( !Q_stricmp( "advertisement", className ) ) {
69
70                         modelKey = ValueForKey( &entities[ i ], "model" );
71
72                         if ( strlen( modelKey ) > MAX_QPATH - 1 ) {
73                                 Error( "Model Key for entity exceeds ad struct string length." );
74                         }
75                         else {
76                                 if ( numBSPAds < MAX_MAP_ADVERTISEMENTS ) {
77                                         bspAds[numBSPAds].cellId = IntForKey( &entities[ i ], "cellId" );
78                                         strncpy( bspAds[numBSPAds].model, modelKey, sizeof( bspAds[numBSPAds].model ) );
79
80                                         modelKey++;
81                                         modelNum = atoi( modelKey );
82                                         adModel = &bspModels[modelNum];
83
84                                         if ( adModel->numBSPSurfaces != 1 ) {
85                                                 Error( "Ad cell id %d has more than one surface.", bspAds[numBSPAds].cellId );
86                                         }
87
88                                         adSurface = &bspDrawSurfaces[adModel->firstBSPSurface];
89
90                                         // store the normal for use at run time.. all ad verts are assumed to
91                                         // have identical normals (because they should be a simple rectangle)
92                                         // so just use the first vert's normal
93                                         VectorCopy( bspDrawVerts[adSurface->firstVert].normal, bspAds[numBSPAds].normal );
94
95                                         // store the ad quad for quick use at run time
96                                         if ( adSurface->surfaceType == MST_PATCH ) {
97                                                 int v0 = adSurface->firstVert + adSurface->patchHeight - 1;
98                                                 int v1 = adSurface->firstVert + adSurface->numVerts - 1;
99                                                 int v2 = adSurface->firstVert + adSurface->numVerts - adSurface->patchWidth;
100                                                 int v3 = adSurface->firstVert;
101                                                 VectorCopy( bspDrawVerts[v0].xyz, bspAds[numBSPAds].rect[0] );
102                                                 VectorCopy( bspDrawVerts[v1].xyz, bspAds[numBSPAds].rect[1] );
103                                                 VectorCopy( bspDrawVerts[v2].xyz, bspAds[numBSPAds].rect[2] );
104                                                 VectorCopy( bspDrawVerts[v3].xyz, bspAds[numBSPAds].rect[3] );
105                                         }
106                                         else {
107                                                 Error( "Ad cell %d has an unsupported Ad Surface type.", bspAds[numBSPAds].cellId );
108                                         }
109
110                                         numBSPAds++;
111                                 }
112                                 else {
113                                         Error( "Maximum number of map advertisements exceeded." );
114                                 }
115                         }
116                 }
117         }
118
119         Sys_FPrintf( SYS_VRB, "%9d in-game advertisements\n", numBSPAds );
120 }
121
122 /*
123    SetCloneModelNumbers() - ydnar
124    sets the model numbers for brush entities
125  */
126
127 static void SetCloneModelNumbers( void ){
128         int i, j;
129         int models;
130         char modelValue[ 10 ];
131         const char  *value, *value2, *value3;
132
133
134         /* start with 1 (worldspawn is model 0) */
135         models = 1;
136         for ( i = 1; i < numEntities; i++ )
137         {
138                 /* only entities with brushes or patches get a model number */
139                 if ( entities[ i ].brushes == NULL && entities[ i ].patches == NULL ) {
140                         continue;
141                 }
142
143                 /* is this a clone? */
144                 value = ValueForKey( &entities[ i ], "_ins" );
145                 if ( value[ 0 ] == '\0' ) {
146                         value = ValueForKey( &entities[ i ], "_instance" );
147                 }
148                 if ( value[ 0 ] == '\0' ) {
149                         value = ValueForKey( &entities[ i ], "_clone" );
150                 }
151                 if ( value[ 0 ] != '\0' ) {
152                         continue;
153                 }
154
155                 /* add the model key */
156                 sprintf( modelValue, "*%d", models );
157                 SetKeyValue( &entities[ i ], "model", modelValue );
158
159                 /* increment model count */
160                 models++;
161         }
162
163         /* fix up clones */
164         for ( i = 1; i < numEntities; i++ )
165         {
166                 /* only entities with brushes or patches get a model number */
167                 if ( entities[ i ].brushes == NULL && entities[ i ].patches == NULL ) {
168                         continue;
169                 }
170
171                 /* is this a clone? */
172                 value = ValueForKey( &entities[ i ], "_ins" );
173                 if ( value[ 0 ] == '\0' ) {
174                         value = ValueForKey( &entities[ i ], "_instance" );
175                 }
176                 if ( value[ 0 ] == '\0' ) {
177                         value = ValueForKey( &entities[ i ], "_clone" );
178                 }
179                 if ( value[ 0 ] == '\0' ) {
180                         continue;
181                 }
182
183                 /* find an entity with matching clone name */
184                 for ( j = 0; j < numEntities; j++ )
185                 {
186                         /* is this a clone parent? */
187                         value2 = ValueForKey( &entities[ j ], "_clonename" );
188                         if ( value2[ 0 ] == '\0' ) {
189                                 continue;
190                         }
191
192                         /* do they match? */
193                         if ( strcmp( value, value2 ) == 0 ) {
194                                 /* get the model num */
195                                 value3 = ValueForKey( &entities[ j ], "model" );
196                                 if ( value3[ 0 ] == '\0' ) {
197                                         Sys_FPrintf( SYS_WRN, "WARNING: Cloned entity %s referenced entity without model\n", value2 );
198                                         continue;
199                                 }
200                                 models = atoi( &value2[ 1 ] );
201
202                                 /* add the model key */
203                                 sprintf( modelValue, "*%d", models );
204                                 SetKeyValue( &entities[ i ], "model", modelValue );
205
206                                 /* nuke the brushes/patches for this entity (fixme: leak!) */
207                                 entities[ i ].brushes = NULL;
208                                 entities[ i ].patches = NULL;
209                         }
210                 }
211         }
212 }
213
214
215
216 /*
217    FixBrushSides() - ydnar
218    matches brushsides back to their appropriate drawsurface and shader
219  */
220
221 static void FixBrushSides( entity_t *e ){
222         int i;
223         mapDrawSurface_t    *ds;
224         sideRef_t           *sideRef;
225         bspBrushSide_t      *side;
226
227
228         /* note it */
229         Sys_FPrintf( SYS_VRB, "--- FixBrushSides ---\n" );
230
231         /* walk list of drawsurfaces */
232         for ( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
233         {
234                 /* get surface and try to early out */
235                 ds = &mapDrawSurfs[ i ];
236                 if ( ds->outputNum < 0 ) {
237                         continue;
238                 }
239
240                 /* walk sideref list */
241                 for ( sideRef = ds->sideRef; sideRef != NULL; sideRef = sideRef->next )
242                 {
243                         /* get bsp brush side */
244                         if ( sideRef->side == NULL || sideRef->side->outputNum < 0 ) {
245                                 continue;
246                         }
247                         side = &bspBrushSides[ sideRef->side->outputNum ];
248
249                         /* set drawsurface */
250                         side->surfaceNum = ds->outputNum;
251                         //%     Sys_FPrintf( SYS_VRB, "DS: %7d Side: %7d     ", ds->outputNum, sideRef->side->outputNum );
252
253                         /* set shader */
254                         if ( strcmp( bspShaders[ side->shaderNum ].shader, ds->shaderInfo->shader ) ) {
255                                 //%     Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", bspShaders[ side->shaderNum ].shader, ds->shaderInfo->shader );
256                                 side->shaderNum = EmitShader( ds->shaderInfo->shader, &ds->shaderInfo->contentFlags, &ds->shaderInfo->surfaceFlags );
257                         }
258                 }
259         }
260 }
261
262
263
264 /*
265    ProcessWorldModel()
266    creates a full bsp + surfaces for the worldspawn entity
267  */
268
269 void ProcessWorldModel( const char *portalFilePath, const char *lineFilePath ){
270         int i, s;
271         entity_t    *e;
272         tree_t      *tree;
273         face_t      *faces;
274         qboolean ignoreLeaks, leaked;
275         xmlNodePtr polyline, leaknode;
276         char level[ 2 ], shader[ 1024 ];
277         const char  *value;
278         int leakStatus;
279
280         /* sets integer blockSize from worldspawn "_blocksize" key if it exists */
281         value = ValueForKey( &entities[ 0 ], "_blocksize" );
282         if ( value[ 0 ] == '\0' ) {
283                 value = ValueForKey( &entities[ 0 ], "blocksize" );
284         }
285         if ( value[ 0 ] == '\0' ) {
286                 value = ValueForKey( &entities[ 0 ], "chopsize" );  /* sof2 */
287         }
288         if ( value[ 0 ] != '\0' ) {
289                 /* scan 3 numbers */
290                 s = sscanf( value, "%d %d %d", &blockSize[ 0 ], &blockSize[ 1 ], &blockSize[ 2 ] );
291
292                 /* handle legacy case */
293                 if ( s == 1 ) {
294                         blockSize[ 1 ] = blockSize[ 0 ];
295                         blockSize[ 2 ] = blockSize[ 0 ];
296                 }
297         }
298         Sys_Printf( "block size = { %d %d %d }\n", blockSize[ 0 ], blockSize[ 1 ], blockSize[ 2 ] );
299
300         /* sof2: ignore leaks? */
301         value = ValueForKey( &entities[ 0 ], "_ignoreleaks" );  /* ydnar */
302         if ( value[ 0 ] == '\0' ) {
303                 value = ValueForKey( &entities[ 0 ], "ignoreleaks" );
304         }
305         if ( value[ 0 ] == '1' ) {
306                 ignoreLeaks = qtrue;
307         }
308         else{
309                 ignoreLeaks = qfalse;
310         }
311
312         /* begin worldspawn model */
313         BeginModel();
314         e = &entities[ 0 ];
315         e->firstDrawSurf = 0;
316
317         /* ydnar: gs mods */
318         ClearMetaTriangles();
319
320         /* check for patches with adjacent edges that need to lod together */
321         PatchMapDrawSurfs( e );
322
323         /* build an initial bsp tree using all of the sides of all of the structural brushes */
324         faces = MakeStructuralBSPFaceList( entities[ 0 ].brushes );
325         tree = FaceBSP( faces );
326         MakeTreePortals( tree );
327         FilterStructuralBrushesIntoTree( e, tree );
328
329         /* see if the bsp is completely enclosed */
330         leakStatus = FloodEntities( tree );
331         if ( ignoreLeaks ) {
332                 if ( leakStatus == FLOODENTITIES_LEAKED ) {
333                         leakStatus = FLOODENTITIES_GOOD;
334                 }
335         }
336
337         if ( leakStatus == FLOODENTITIES_GOOD ) {
338                 leaked = qfalse;
339         }
340         else
341         {
342                 leaked = qtrue;
343
344                 Sys_FPrintf( SYS_NOXML, "**********************\n" );
345                 Sys_FPrintf( SYS_NOXML, "******* leaked *******\n" );
346                 Sys_FPrintf( SYS_NOXML, "**********************\n" );
347                 polyline = LeakFile( tree, lineFilePath );
348                 leaknode = xmlNewNode( NULL, (xmlChar*)"message" );
349                 xmlNodeSetContent( leaknode, (xmlChar*)"MAP LEAKED\n" );
350                 xmlAddChild( leaknode, polyline );
351                 level[0] = (int) '0' + SYS_ERR;
352                 level[1] = 0;
353                 xmlSetProp( leaknode, (xmlChar*)"level", (xmlChar*) &level );
354                 xml_SendNode( leaknode );
355                 if ( leaktest ) {
356                         Sys_Printf( "--- MAP LEAKED, ABORTING LEAKTEST ---\n" );
357                         exit( 0 );
358                 }
359         }
360
361         if ( leakStatus != FLOODENTITIES_EMPTY ) { /* if no entities exist, this would accidentally the whole map, and that IS bad */
362                 /* rebuild a better bsp tree using only the sides that are visible from the inside */
363                 FillOutside( tree->headnode );
364
365                 /* chop the sides to the convex hull of their visible fragments, giving us the smallest polygons */
366                 ClipSidesIntoTree( e, tree );
367
368                 /* build a visible face tree (same thing as the initial bsp tree but after reducing the faces) */
369                 faces = MakeVisibleBSPFaceList( entities[ 0 ].brushes );
370                 FreeTree( tree );
371                 tree = FaceBSP( faces );
372                 MakeTreePortals( tree );
373                 FilterStructuralBrushesIntoTree( e, tree );
374
375                 /* ydnar: flood again for skybox */
376                 if ( skyboxPresent ) {
377                         FloodEntities( tree );
378                 }
379         }
380
381         /* save out information for visibility processing */
382         NumberClusters( tree );
383         if ( !leaked ) {
384                 WritePortalFile( tree, portalFilePath );
385         }
386
387         /* flood from entities */
388         FloodAreas( tree );
389
390         /* create drawsurfs for triangle models */
391         AddTriangleModels( e );
392
393         /* create drawsurfs for surface models */
394         AddEntitySurfaceModels( e );
395
396         /* generate bsp brushes from map brushes */
397         EmitBrushes( e->brushes, &e->firstBrush, &e->numBrushes );
398
399         /* add references to the detail brushes */
400         FilterDetailBrushesIntoTree( e, tree );
401
402         /* drawsurfs that cross fog boundaries will need to be split along the fog boundary */
403         if ( !nofog ) {
404                 FogDrawSurfaces( e );
405         }
406
407         /* subdivide each drawsurf as required by shader tesselation */
408         if ( !nosubdivide ) {
409                 SubdivideFaceSurfaces( e, tree );
410         }
411
412         /* add in any vertexes required to fix t-junctions */
413         if ( !notjunc ) {
414                 FixTJunctions( e );
415         }
416
417         /* ydnar: classify the surfaces */
418         ClassifyEntitySurfaces( e );
419
420         /* ydnar: project decals */
421         MakeEntityDecals( e );
422
423         /* ydnar: meta surfaces */
424         MakeEntityMetaTriangles( e );
425         SmoothMetaTriangles();
426         FixMetaTJunctions();
427         MergeMetaTriangles();
428
429         /* ydnar: debug portals */
430         if ( debugPortals ) {
431                 MakeDebugPortalSurfs( tree );
432         }
433
434         /* ydnar: fog hull */
435         value = ValueForKey( &entities[ 0 ], "_foghull" );
436         if ( value[ 0 ] != '\0' ) {
437                 sprintf( shader, "textures/%s", value );
438                 MakeFogHullSurfs( e, tree, shader );
439         }
440
441         /* ydnar: bug 645: do flares for lights */
442         for ( i = 0; i < numEntities && emitFlares; i++ )
443         {
444                 entity_t    *light, *target;
445                 const char  *value, *flareShader;
446                 vec3_t origin, targetOrigin, normal, color;
447                 int lightStyle;
448
449
450                 /* get light */
451                 light = &entities[ i ];
452                 value = ValueForKey( light, "classname" );
453                 if ( !strcmp( value, "light" ) ) {
454                         /* get flare shader */
455                         flareShader = ValueForKey( light, "_flareshader" );
456                         value = ValueForKey( light, "_flare" );
457                         if ( flareShader[ 0 ] != '\0' || value[ 0 ] != '\0' ) {
458                                 /* get specifics */
459                                 GetVectorForKey( light, "origin", origin );
460                                 GetVectorForKey( light, "_color", color );
461                                 lightStyle = IntForKey( light, "_style" );
462                                 if ( lightStyle == 0 ) {
463                                         lightStyle = IntForKey( light, "style" );
464                                 }
465
466                                 /* handle directional spotlights */
467                                 value = ValueForKey( light, "target" );
468                                 if ( value[ 0 ] != '\0' ) {
469                                         /* get target light */
470                                         target = FindTargetEntity( value );
471                                         if ( target != NULL ) {
472                                                 GetVectorForKey( target, "origin", targetOrigin );
473                                                 VectorSubtract( targetOrigin, origin, normal );
474                                                 VectorNormalize( normal, normal );
475                                         }
476                                 }
477                                 else{
478                                         //%     VectorClear( normal );
479                                         VectorSet( normal, 0, 0, -1 );
480                                 }
481
482                                 if ( colorsRGB ) {
483                                         color[0] = Image_LinearFloatFromsRGBFloat( color[0] );
484                                         color[1] = Image_LinearFloatFromsRGBFloat( color[1] );
485                                         color[2] = Image_LinearFloatFromsRGBFloat( color[2] );
486                                 }
487
488                                 /* create the flare surface (note shader defaults automatically) */
489                                 DrawSurfaceForFlare( mapEntityNum, origin, normal, color, flareShader, lightStyle );
490                         }
491                 }
492         }
493
494         /* add references to the final drawsurfs in the apropriate clusters */
495         FilterDrawsurfsIntoTree( e, tree );
496
497         /* match drawsurfaces back to original brushsides (sof2) */
498         FixBrushSides( e );
499
500         /* finish */
501         EndModel( e, tree->headnode );
502         FreeTree( tree );
503 }
504
505
506
507 /*
508    ProcessSubModel()
509    creates bsp + surfaces for other brush models
510  */
511
512 void ProcessSubModel( void ){
513         entity_t    *e;
514         tree_t      *tree;
515         brush_t     *b, *bc;
516         node_t      *node;
517
518
519         /* start a brush model */
520         BeginModel();
521         e = &entities[ mapEntityNum ];
522         e->firstDrawSurf = numMapDrawSurfs;
523
524         /* ydnar: gs mods */
525         ClearMetaTriangles();
526
527         /* check for patches with adjacent edges that need to lod together */
528         PatchMapDrawSurfs( e );
529
530         /* allocate a tree */
531         node = AllocNode();
532         node->planenum = PLANENUM_LEAF;
533         tree = AllocTree();
534         tree->headnode = node;
535
536         /* add the sides to the tree */
537         ClipSidesIntoTree( e, tree );
538
539         /* ydnar: create drawsurfs for triangle models */
540         AddTriangleModels( e );
541
542         /* create drawsurfs for surface models */
543         AddEntitySurfaceModels( e );
544
545         /* generate bsp brushes from map brushes */
546         EmitBrushes( e->brushes, &e->firstBrush, &e->numBrushes );
547
548         /* just put all the brushes in headnode */
549         for ( b = e->brushes; b; b = b->next )
550         {
551                 bc = CopyBrush( b );
552                 bc->next = node->brushlist;
553                 node->brushlist = bc;
554         }
555
556         /* subdivide each drawsurf as required by shader tesselation */
557         if ( !nosubdivide ) {
558                 SubdivideFaceSurfaces( e, tree );
559         }
560
561         /* add in any vertexes required to fix t-junctions */
562         if ( !notjunc ) {
563                 FixTJunctions( e );
564         }
565
566         /* ydnar: classify the surfaces and project lightmaps */
567         ClassifyEntitySurfaces( e );
568
569         /* ydnar: project decals */
570         MakeEntityDecals( e );
571
572         /* ydnar: meta surfaces */
573         MakeEntityMetaTriangles( e );
574         SmoothMetaTriangles();
575         FixMetaTJunctions();
576         MergeMetaTriangles();
577
578         /* add references to the final drawsurfs in the apropriate clusters */
579         FilterDrawsurfsIntoTree( e, tree );
580
581         /* match drawsurfaces back to original brushsides (sof2) */
582         FixBrushSides( e );
583
584         /* finish */
585         EndModel( e, node );
586         FreeTree( tree );
587 }
588
589
590
591 /*
592    ProcessModels()
593    process world + other models into the bsp
594  */
595
596 void ProcessModels( const char *portalFilePath, const char *lineFilePath ){
597         qboolean oldVerbose;
598         entity_t    *entity;
599
600
601         /* preserve -v setting */
602         oldVerbose = verbose;
603
604         /* start a new bsp */
605         BeginBSPFile();
606
607         /* create map fogs */
608         CreateMapFogs();
609
610         /* walk entity list */
611         for ( mapEntityNum = 0; mapEntityNum < numEntities; mapEntityNum++ )
612         {
613                 /* get entity */
614                 entity = &entities[ mapEntityNum ];
615                 if ( entity->brushes == NULL && entity->patches == NULL ) {
616                         continue;
617                 }
618
619                 /* process the model */
620                 Sys_FPrintf( SYS_VRB, "############### model %i ###############\n", numBSPModels );
621                 if ( mapEntityNum == 0 ) {
622                         ProcessWorldModel(portalFilePath, lineFilePath);
623                 }
624                 else{
625                         ProcessSubModel();
626                 }
627
628                 /* potentially turn off the deluge of text */
629                 verbose = verboseEntities;
630         }
631
632         /* restore -v setting */
633         verbose = oldVerbose;
634
635         /* write fogs */
636         EmitFogs();
637
638         /* vortex: emit meta stats */
639         EmitMetaStats();
640 }
641
642
643
644 /*
645    OnlyEnts()
646    this is probably broken unless teamed with a radiant version that preserves entity order
647  */
648
649 void OnlyEnts( const char *BSPFilePath ){
650         char save_cmdline[1024], save_version[1024], save_gridsize[1024];
651         const char *p;
652
653         /* note it */
654         Sys_Printf( "--- OnlyEnts ---\n" );
655
656         LoadBSPFile( BSPFilePath );
657
658         ParseEntities();
659         p = ValueForKey( &entities[0], "_q3map2_cmdline" );
660         strncpy( save_cmdline, p, sizeof( save_cmdline ) );
661         save_cmdline[sizeof( save_cmdline ) - 1] = 0;
662         p = ValueForKey( &entities[0], "_q3map2_version" );
663         strncpy( save_version, p, sizeof( save_version ) );
664         save_version[sizeof( save_version ) - 1] = 0;
665         p = ValueForKey( &entities[0], "gridsize" );
666         strncpy( save_gridsize, p, sizeof( save_gridsize ) );
667         save_gridsize[sizeof( save_gridsize ) - 1] = 0;
668
669         numEntities = 0;
670
671         LoadShaderInfo();
672         LoadMapFile( name, qfalse, qfalse );
673         SetModelNumbers();
674         SetLightStyles();
675
676         if ( *save_cmdline ) {
677                 SetKeyValue( &entities[0], "_q3map2_cmdline", save_cmdline );
678         }
679         if ( *save_version ) {
680                 SetKeyValue( &entities[0], "_q3map2_version", save_version );
681         }
682         if ( *save_gridsize ) {
683                 SetKeyValue( &entities[0], "gridsize", save_gridsize );
684         }
685
686         numBSPEntities = numEntities;
687         UnparseEntities();
688
689         WriteBSPFile( BSPFilePath );
690 }
691
692
693
694 /*
695    BSPMain() - ydnar
696    handles creation of a bsp from a map file
697  */
698
699 int BSPMain( int argc, char **argv ){
700         int i;
701         char path[ 1024 ], tempSource[ 1024 ];
702         qboolean onlyents = qfalse;
703         char BSPFilePath [ 1024 ];
704         char lineFilePath [ 1024 ];
705         char portalFilePath [ 1024 ];
706         char surfaceFilePath [ 1024 ];
707         BSPFilePath[0] = 0;
708         lineFilePath[0] = 0;
709         portalFilePath[0] = 0;
710         surfaceFilePath[0] = 0;
711
712
713         /* note it */
714         Sys_Printf( "--- BSP ---\n" );
715
716         SetDrawSurfacesBuffer();
717         mapDrawSurfs = safe_malloc( sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
718         memset( mapDrawSurfs, 0, sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
719         numMapDrawSurfs = 0;
720
721         tempSource[ 0 ] = '\0';
722         globalCelShader[0] = 0;
723
724         /* set standard game flags */
725         maxSurfaceVerts = game->maxSurfaceVerts;
726         maxSurfaceIndexes = game->maxSurfaceIndexes;
727         emitFlares = game->emitFlares;
728         texturesRGB = game->texturesRGB;
729         colorsRGB = game->colorsRGB;
730
731         /* process arguments */
732         for ( i = 1; i < ( argc - 1 ); i++ )
733         {
734                 if ( !strcmp( argv[ i ], "-onlyents" ) ) {
735                         Sys_Printf( "Running entity-only compile\n" );
736                         onlyents = qtrue;
737                 }
738                 else if ( !strcmp( argv[ i ], "-tempname" ) ) {
739                         strcpy( tempSource, argv[ ++i ] );
740                 }
741                 else if ( !strcmp( argv[ i ], "-tmpout" ) ) {
742                         strcpy( outbase, "/tmp" );
743                 }
744                 else if ( !strcmp( argv[ i ],  "-nowater" ) ) {
745                         Sys_Printf( "Disabling water\n" );
746                         nowater = qtrue;
747                 }
748                 else if ( !strcmp( argv[ i ], "-keeplights" ) ) {
749                         keepLights = qtrue;
750                         Sys_Printf( "Leaving light entities on map after compile\n" );
751                 }
752                 else if ( !strcmp( argv[ i ],  "-nodetail" ) ) {
753                         Sys_Printf( "Ignoring detail brushes\n" ) ;
754                         nodetail = qtrue;
755                 }
756                 else if ( !strcmp( argv[ i ],  "-fulldetail" ) ) {
757                         Sys_Printf( "Turning detail brushes into structural brushes\n" );
758                         fulldetail = qtrue;
759                 }
760                 else if ( !strcmp( argv[ i ],  "-nofog" ) ) {
761                         Sys_Printf( "Fog volumes disabled\n" );
762                         nofog = qtrue;
763                 }
764                 else if ( !strcmp( argv[ i ],  "-nosubdivide" ) ) {
765                         Sys_Printf( "Disabling brush face subdivision\n" );
766                         nosubdivide = qtrue;
767                 }
768                 else if ( !strcmp( argv[ i ],  "-leaktest" ) ) {
769                         Sys_Printf( "Leaktest enabled\n" );
770                         leaktest = qtrue;
771                 }
772                 else if ( !strcmp( argv[ i ],  "-verboseentities" ) ) {
773                         Sys_Printf( "Verbose entities enabled\n" );
774                         verboseEntities = qtrue;
775                 }
776                 else if ( !strcmp( argv[ i ], "-nocurves" ) ) {
777                         Sys_Printf( "Ignoring curved surfaces (patches)\n" );
778                         noCurveBrushes = qtrue;
779                 }
780                 else if ( !strcmp( argv[ i ], "-notjunc" ) ) {
781                         Sys_Printf( "T-junction fixing disabled\n" );
782                         notjunc = qtrue;
783                 }
784                 else if ( !strcmp( argv[ i ], "-fakemap" ) ) {
785                         Sys_Printf( "Generating fakemap.map\n" );
786                         fakemap = qtrue;
787                 }
788                 else if ( !strcmp( argv[ i ],  "-samplesize" ) ) {
789                         sampleSize = atoi( argv[ i + 1 ] );
790                         if ( sampleSize < 1 ) {
791                                 sampleSize = 1;
792                         }
793                         i++;
794                         Sys_Printf( "Lightmap sample size set to %dx%d units\n", sampleSize, sampleSize );
795                 }
796                 else if ( !strcmp( argv[ i ], "-minsamplesize" ) ) {
797                         minSampleSize = atoi( argv[ i + 1 ] );
798                         if ( minSampleSize < 1 ) {
799                                 minSampleSize = 1;
800                         }
801                         i++;
802                         Sys_Printf( "Minimum lightmap sample size set to %dx%d units\n", minSampleSize, minSampleSize );
803                 }
804                 else if ( !strcmp( argv[ i ],  "-custinfoparms" ) ) {
805                         Sys_Printf( "Custom info parms enabled\n" );
806                         useCustomInfoParms = qtrue;
807                 }
808
809                 /* sof2 args */
810                 else if ( !strcmp( argv[ i ], "-rename" ) ) {
811                         Sys_Printf( "Appending _bsp suffix to misc_model shaders (SOF2)\n" );
812                         renameModelShaders = qtrue;
813                 }
814
815                 /* ydnar args */
816                 else if ( !strcmp( argv[ i ],  "-ne" ) ) {
817                         normalEpsilon = atof( argv[ i + 1 ] );
818                         i++;
819                         Sys_Printf( "Normal epsilon set to %f\n", normalEpsilon );
820                 }
821                 else if ( !strcmp( argv[ i ],  "-de" ) ) {
822                         distanceEpsilon = atof( argv[ i + 1 ] );
823                         i++;
824                         Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
825                 }
826                 else if ( !strcmp( argv[ i ],  "-mv" ) ) {
827                         maxLMSurfaceVerts = atoi( argv[ i + 1 ] );
828                         if ( maxLMSurfaceVerts < 3 ) {
829                                 maxLMSurfaceVerts = 3;
830                         }
831                         if ( maxLMSurfaceVerts > maxSurfaceVerts ) {
832                                 maxSurfaceVerts = maxLMSurfaceVerts;
833                         }
834                         i++;
835                         Sys_Printf( "Maximum lightmapped surface vertex count set to %d\n", maxLMSurfaceVerts );
836                 }
837                 else if ( !strcmp( argv[ i ],  "-mi" ) ) {
838                         maxSurfaceIndexes = atoi( argv[ i + 1 ] );
839                         if ( maxSurfaceIndexes < 3 ) {
840                                 maxSurfaceIndexes = 3;
841                         }
842                         i++;
843                         Sys_Printf( "Maximum per-surface index count set to %d\n", maxSurfaceIndexes );
844                 }
845                 else if ( !strcmp( argv[ i ], "-np" ) ) {
846                         npDegrees = atof( argv[ i + 1 ] );
847                         if ( npDegrees < 0.0f ) {
848                                 npDegrees = 0.0f;
849                         }
850                         else if ( npDegrees > 0.0f ) {
851                                 Sys_Printf( "Forcing nonplanar surfaces with a breaking angle of %f degrees\n", npDegrees );
852                         }
853                         i++;
854                 }
855                 else if ( !strcmp( argv[ i ],  "-snap" ) ) {
856                         bevelSnap = atoi( argv[ i + 1 ] );
857                         if ( bevelSnap < 0 ) {
858                                 bevelSnap = 0;
859                         }
860                         i++;
861                         if ( bevelSnap > 0 ) {
862                                 Sys_Printf( "Snapping brush bevel planes to %d units\n", bevelSnap );
863                         }
864                 }
865                 else if ( !strcmp( argv[ i ],  "-texrange" ) ) {
866                         texRange = atoi( argv[ i + 1 ] );
867                         if ( texRange < 0 ) {
868                                 texRange = 0;
869                         }
870                         i++;
871                         Sys_Printf( "Limiting per-surface texture range to %d texels\n", texRange );
872                 }
873                 else if ( !strcmp( argv[ i ], "-nohint" ) ) {
874                         Sys_Printf( "Hint brushes disabled\n" );
875                         noHint = qtrue;
876                 }
877                 else if ( !strcmp( argv[ i ], "-flat" ) ) {
878                         Sys_Printf( "Flatshading enabled\n" );
879                         flat = qtrue;
880                 }
881                 else if ( !strcmp( argv[ i ], "-celshader" ) ) {
882                         ++i;
883                         if ( argv[i][0] ) {
884                                 sprintf( globalCelShader, "textures/%s", argv[ i ] );
885                         }
886                         else{
887                                 *globalCelShader = 0;
888                         }
889                         Sys_Printf( "Global cel shader set to \"%s\"\n", globalCelShader );
890                 }
891                 else if ( !strcmp( argv[ i ], "-meta" ) ) {
892                         Sys_Printf( "Creating meta surfaces from brush faces\n" );
893                         meta = qtrue;
894                 }
895                 else if ( !strcmp( argv[ i ], "-metaadequatescore" ) ) {
896                         metaAdequateScore = atoi( argv[ i + 1 ] );
897                         if ( metaAdequateScore < 0 ) {
898                                 metaAdequateScore = -1;
899                         }
900                         i++;
901                         if ( metaAdequateScore >= 0 ) {
902                                 Sys_Printf( "Setting ADEQUATE meta score to %d (see surface_meta.c)\n", metaAdequateScore );
903                         }
904                 }
905                 else if ( !strcmp( argv[ i ], "-metagoodscore" ) ) {
906                         metaGoodScore = atoi( argv[ i + 1 ] );
907                         if ( metaGoodScore < 0 ) {
908                                 metaGoodScore = -1;
909                         }
910                         i++;
911                         if ( metaGoodScore >= 0 ) {
912                                 Sys_Printf( "Setting GOOD meta score to %d (see surface_meta.c)\n", metaGoodScore );
913                         }
914                 }
915                 else if ( !strcmp( argv[ i ], "-metamaxbboxdistance" ) ) {
916                         metaMaxBBoxDistance = atof( argv[ i + 1 ] );
917                         if ( metaMaxBBoxDistance < 0 ) {
918                                 metaMaxBBoxDistance = -1;
919                         }
920                         i++;
921                         if ( metaMaxBBoxDistance >= 0 ) {
922                                 Sys_Printf( "Setting meta maximum bounding box distance to %f\n", metaMaxBBoxDistance );
923                         }
924                 }
925                 else if ( !strcmp( argv[ i ], "-patchmeta" ) ) {
926                         Sys_Printf( "Creating meta surfaces from patches\n" );
927                         patchMeta = qtrue;
928                 }
929                 else if ( !strcmp( argv[ i ], "-flares" ) ) {
930                         Sys_Printf( "Flare surfaces enabled\n" );
931                         emitFlares = qtrue;
932                 }
933                 else if ( !strcmp( argv[ i ], "-noflares" ) ) {
934                         Sys_Printf( "Flare surfaces disabled\n" );
935                         emitFlares = qfalse;
936                 }
937                 else if ( !strcmp( argv[ i ], "-skyfix" ) ) {
938                         Sys_Printf( "GL_CLAMP sky fix/hack/workaround enabled\n" );
939                         skyFixHack = qtrue;
940                 }
941                 else if ( !strcmp( argv[ i ], "-debugsurfaces" ) ) {
942                         Sys_Printf( "emitting debug surfaces\n" );
943                         debugSurfaces = qtrue;
944                 }
945                 else if ( !strcmp( argv[ i ], "-debuginset" ) ) {
946                         Sys_Printf( "Debug surface triangle insetting enabled\n" );
947                         debugInset = qtrue;
948                 }
949                 else if ( !strcmp( argv[ i ], "-debugportals" ) ) {
950                         Sys_Printf( "Debug portal surfaces enabled\n" );
951                         debugPortals = qtrue;
952                 }
953                 else if ( !strcmp( argv[ i ], "-sRGBtex" ) ) {
954                         texturesRGB = qtrue;
955                         Sys_Printf( "Textures are in sRGB\n" );
956                 }
957                 else if ( !strcmp( argv[ i ], "-nosRGBtex" ) ) {
958                         texturesRGB = qfalse;
959                         Sys_Printf( "Textures are linear\n" );
960                 }
961                 else if ( !strcmp( argv[ i ], "-sRGBcolor" ) ) {
962                         colorsRGB = qtrue;
963                         Sys_Printf( "Colors are in sRGB\n" );
964                 }
965                 else if ( !strcmp( argv[ i ], "-nosRGBcolor" ) ) {
966                         colorsRGB = qfalse;
967                         Sys_Printf( "Colors are linear\n" );
968                 }
969                 else if ( !strcmp( argv[ i ], "-nosRGB" ) ) {
970                         texturesRGB = qfalse;
971                         Sys_Printf( "Textures are linear\n" );
972                         colorsRGB = qfalse;
973                         Sys_Printf( "Colors are linear\n" );
974                 }
975                 else if ( !strcmp( argv[ i ], "-altsplit" ) ) {
976                         Sys_Printf( "Alternate BSP splitting (by 27) enabled\n" );
977                         bspAlternateSplitWeights = qtrue;
978                 }
979                 else if ( !strcmp( argv[ i ], "-deep" ) ) {
980                         Sys_Printf( "Deep BSP tree generation enabled\n" );
981                         deepBSP = qtrue;
982                 }
983                 else if ( !strcmp( argv[ i ], "-maxarea" ) ) {
984                         Sys_Printf( "Max Area face surface generation enabled\n" );
985                         maxAreaFaceSurface = qtrue;
986                 }
987                 else if ( !strcmp( argv[ i ], "-bspfile" ) )
988                 {
989                         strcpy( BSPFilePath, argv[i + 1] );
990                         i++;
991                         Sys_Printf( "Use %s as bsp file\n", BSPFilePath );
992                 }
993                 else if ( !strcmp( argv[ i ], "-linfile" ) )
994                 {
995                         strcpy( lineFilePath, argv[i + 1] );
996                         i++;
997                         Sys_Printf( "Use %s as line file\n", lineFilePath );
998                 }
999                 else if ( !strcmp( argv[ i ], "-prtfile" ) )
1000                 {
1001                         strcpy( portalFilePath, argv[i + 1] );
1002                         i++;
1003                         Sys_Printf( "Use %s as portal file\n", portalFilePath );
1004                 }
1005                 else if ( !strcmp( argv[ i ], "-srffile" ) )
1006                 {
1007                         strcpy( surfaceFilePath, argv[i + 1] );
1008                         i++;
1009                         Sys_Printf( "Use %s as surface file\n", surfaceFilePath );
1010                 }
1011                 else if ( !strcmp( argv[ i ], "-bsp" ) ) {
1012                         Sys_Printf( "-bsp argument unnecessary\n" );
1013                 }
1014                 else{
1015                         Sys_FPrintf( SYS_WRN, "WARNING: Unknown option \"%s\"\n", argv[ i ] );
1016                 }
1017         }
1018
1019         /* fixme: print more useful usage here */
1020         if ( i != ( argc - 1 ) ) {
1021                 Error( "usage: q3map [options] mapfile" );
1022         }
1023
1024         /* copy source name */
1025         strcpy( source, ExpandArg( argv[ i ] ) );
1026         StripExtension( source );
1027
1028         /* ydnar: set default sample size */
1029         SetDefaultSampleSize( sampleSize );
1030
1031         if (!BSPFilePath[0]) {
1032                 sprintf( BSPFilePath, "%s.bsp", source );
1033         }
1034         if (!lineFilePath[0]) {
1035                 sprintf( lineFilePath, "%s.lin", source );
1036         }
1037         if (!portalFilePath[0]) {
1038                 sprintf( portalFilePath, "%s.prt", source );
1039         }
1040         if (!surfaceFilePath[0]) {
1041                 sprintf( surfaceFilePath, "%s.srf", source );
1042         }
1043
1044         /* delete portal, line and surface files */
1045         remove( portalFilePath );
1046         remove( lineFilePath );
1047         //%     remove( surfaceFilePath )       /* ydnar */
1048
1049         /* expand mapname */
1050         strcpy( name, ExpandArg( argv[ i ] ) );
1051         if ( strcmp( name + strlen( name ) - 4, ".reg" ) ) {
1052                 /* if we are doing a full map, delete the last saved region map */
1053                 sprintf( path, "%s.reg", source );
1054                 remove( path );
1055                 DefaultExtension( name, ".map" );   /* might be .reg */
1056         }
1057
1058         /* if onlyents, just grab the entites and resave */
1059         if ( onlyents ) {
1060                 OnlyEnts( BSPFilePath );
1061                 return 0;
1062         }
1063
1064         /* load shaders */
1065         LoadShaderInfo();
1066
1067         /* load original file from temp spot in case it was renamed by the editor on the way in */
1068         if ( strlen( tempSource ) > 0 ) {
1069                 LoadMapFile( tempSource, qfalse, qfalse );
1070         }
1071         else{
1072                 LoadMapFile( name, qfalse, qfalse );
1073         }
1074
1075         /* div0: inject command line parameters */
1076         InjectCommandLine( argv, 1, argc - 1 );
1077
1078         /* ydnar: decal setup */
1079         ProcessDecals();
1080
1081         /* ydnar: cloned brush model entities */
1082         SetCloneModelNumbers();
1083
1084         /* process world and submodels */
1085         ProcessModels( portalFilePath, lineFilePath );
1086
1087         /* set light styles from targetted light entities */
1088         SetLightStyles();
1089
1090         /* process in game advertisements */
1091         ProcessAdvertisements();
1092
1093         /* finish and write bsp */
1094         EndBSPFile( qtrue, BSPFilePath, surfaceFilePath );
1095
1096         /* remove temp map source file if appropriate */
1097         if ( strlen( tempSource ) > 0 ) {
1098                 remove( tempSource );
1099         }
1100
1101         /* return to sender */
1102         return 0;
1103 }