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