]> git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/bsp.c
Q3map2:
[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_Printf( "WARNING: Cloned entity %s referenced entity without model\n", value2 );
197                                         continue;
198                                 }
199                                 models = atoi( &value3[ 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( void ){
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         if ( debugClip ) {
323                 AddTriangleModels( e );
324         }
325
326         /* build an initial bsp tree using all of the sides of all of the structural brushes */
327         faces = MakeStructuralBSPFaceList( entities[ 0 ].brushes );
328         tree = FaceBSP( faces );
329         MakeTreePortals( tree );
330         FilterStructuralBrushesIntoTree( e, tree );
331
332         /* see if the bsp is completely enclosed */
333         leakStatus = FloodEntities( tree );
334         if ( ignoreLeaks ) {
335                 if ( leakStatus == FLOODENTITIES_LEAKED ) {
336                         leakStatus = FLOODENTITIES_GOOD;
337                 }
338         }
339
340         if ( leakStatus == FLOODENTITIES_GOOD ) {
341                 leaked = qfalse;
342         }
343         else
344         {
345                 leaked = qtrue;
346
347                 Sys_FPrintf( SYS_NOXML, "**********************\n" );
348                 Sys_FPrintf( SYS_NOXML, "******* leaked *******\n" );
349                 Sys_FPrintf( SYS_NOXML, "**********************\n" );
350                 polyline = LeakFile( tree );
351                 leaknode = xmlNewNode( NULL, (xmlChar*)"message" );
352                 xmlNodeSetContent( leaknode, (xmlChar*)"MAP LEAKED\n" );
353                 xmlAddChild( leaknode, polyline );
354                 level[0] = (int) '0' + SYS_ERR;
355                 level[1] = 0;
356                 xmlSetProp( leaknode, (xmlChar*)"level", (xmlChar*) &level );
357                 xml_SendNode( leaknode );
358                 if ( leaktest ) {
359                         Sys_Printf( "--- MAP LEAKED, ABORTING LEAKTEST ---\n" );
360                         exit( 0 );
361                 }
362         }
363
364         if ( leakStatus != FLOODENTITIES_EMPTY ) { /* if no entities exist, this would accidentally the whole map, and that IS bad */
365                 /* rebuild a better bsp tree using only the sides that are visible from the inside */
366                 FillOutside( tree->headnode );
367
368                 /* chop the sides to the convex hull of their visible fragments, giving us the smallest polygons */
369                 ClipSidesIntoTree( e, tree );
370
371                 /* build a visible face tree (same thing as the initial bsp tree but after reducing the faces) */
372                 faces = MakeVisibleBSPFaceList( entities[ 0 ].brushes );
373                 FreeTree( tree );
374                 tree = FaceBSP( faces );
375                 MakeTreePortals( tree );
376                 FilterStructuralBrushesIntoTree( e, tree );
377
378                 /* ydnar: flood again for skybox */
379                 if ( skyboxPresent ) {
380                         FloodEntities( tree );
381                 }
382         }
383
384         /* save out information for visibility processing */
385         NumberClusters( tree );
386         if ( !leaked ) {
387                 WritePortalFile( tree );
388         }
389
390         /* flood from entities */
391         FloodAreas( tree );
392
393         /* create drawsurfs for triangle models */
394         if ( !debugClip ) {
395                 AddTriangleModels( e );
396         }
397
398         /* create drawsurfs for surface models */
399         AddEntitySurfaceModels( e );
400
401         /* generate bsp brushes from map brushes */
402         EmitBrushes( e->brushes, &e->firstBrush, &e->numBrushes );
403
404         /* add references to the detail brushes */
405         FilterDetailBrushesIntoTree( e, tree );
406
407         /* drawsurfs that cross fog boundaries will need to be split along the fog boundary */
408         if ( !nofog ) {
409                 FogDrawSurfaces( e );
410         }
411
412         /* subdivide each drawsurf as required by shader tesselation */
413         if ( !nosubdivide ) {
414                 SubdivideFaceSurfaces( e, tree );
415         }
416
417         /* add in any vertexes required to fix t-junctions */
418         if ( !notjunc ) {
419                 FixTJunctions( e );
420         }
421
422         /* ydnar: classify the surfaces */
423         ClassifyEntitySurfaces( e );
424
425         /* ydnar: project decals */
426         MakeEntityDecals( e );
427
428         /* ydnar: meta surfaces */
429         MakeEntityMetaTriangles( e );
430         SmoothMetaTriangles();
431         FixMetaTJunctions();
432         MergeMetaTriangles();
433
434         /* ydnar: debug portals */
435         if ( debugPortals ) {
436                 MakeDebugPortalSurfs( tree );
437         }
438
439         /* ydnar: fog hull */
440         value = ValueForKey( &entities[ 0 ], "_foghull" );
441         if ( value[ 0 ] != '\0' ) {
442                 sprintf( shader, "textures/%s", value );
443                 MakeFogHullSurfs( e, tree, shader );
444         }
445
446         /* ydnar: bug 645: do flares for lights */
447         for ( i = 0; i < numEntities && emitFlares; i++ )
448         {
449                 entity_t    *light, *target;
450                 const char  *value, *flareShader;
451                 vec3_t origin, targetOrigin, normal, color;
452                 int lightStyle;
453
454
455                 /* get light */
456                 light = &entities[ i ];
457                 value = ValueForKey( light, "classname" );
458                 if ( !strcmp( value, "light" ) ) {
459                         /* get flare shader */
460                         flareShader = ValueForKey( light, "_flareshader" );
461                         value = ValueForKey( light, "_flare" );
462                         if ( flareShader[ 0 ] != '\0' || value[ 0 ] != '\0' ) {
463                                 /* get specifics */
464                                 GetVectorForKey( light, "origin", origin );
465                                 GetVectorForKey( light, "_color", color );
466                                 lightStyle = IntForKey( light, "_style" );
467                                 if ( lightStyle == 0 ) {
468                                         lightStyle = IntForKey( light, "style" );
469                                 }
470
471                                 /* handle directional spotlights */
472                                 value = ValueForKey( light, "target" );
473                                 if ( value[ 0 ] != '\0' ) {
474                                         /* get target light */
475                                         target = FindTargetEntity( value );
476                                         if ( target != NULL ) {
477                                                 GetVectorForKey( target, "origin", targetOrigin );
478                                                 VectorSubtract( targetOrigin, origin, normal );
479                                                 VectorNormalize( normal, normal );
480                                         }
481                                 }
482                                 else{
483                                         //%     VectorClear( normal );
484                                         VectorSet( normal, 0, 0, -1 );
485                                 }
486
487                                 if ( colorsRGB ) {
488                                         color[0] = Image_LinearFloatFromsRGBFloat( color[0] );
489                                         color[1] = Image_LinearFloatFromsRGBFloat( color[1] );
490                                         color[2] = Image_LinearFloatFromsRGBFloat( color[2] );
491                                 }
492
493                                 /* create the flare surface (note shader defaults automatically) */
494                                 DrawSurfaceForFlare( mapEntityNum, origin, normal, color, flareShader, lightStyle );
495                         }
496                 }
497         }
498
499         /* add references to the final drawsurfs in the apropriate clusters */
500         FilterDrawsurfsIntoTree( e, tree );
501
502         /* match drawsurfaces back to original brushsides (sof2) */
503         FixBrushSides( e );
504
505         /* finish */
506         EndModel( e, tree->headnode );
507         FreeTree( tree );
508 }
509
510
511
512 /*
513    ProcessSubModel()
514    creates bsp + surfaces for other brush models
515  */
516
517 void ProcessSubModel( void ){
518         entity_t    *e;
519         tree_t      *tree;
520         brush_t     *b, *bc;
521         node_t      *node;
522
523
524         /* start a brush model */
525         BeginModel();
526         e = &entities[ mapEntityNum ];
527         e->firstDrawSurf = numMapDrawSurfs;
528
529         /* ydnar: gs mods */
530         ClearMetaTriangles();
531
532         /* check for patches with adjacent edges that need to lod together */
533         PatchMapDrawSurfs( e );
534
535         /* allocate a tree */
536         node = AllocNode();
537         node->planenum = PLANENUM_LEAF;
538         tree = AllocTree();
539         tree->headnode = node;
540
541         /* add the sides to the tree */
542         ClipSidesIntoTree( e, tree );
543
544         /* ydnar: create drawsurfs for triangle models */
545         AddTriangleModels( e );
546
547         /* create drawsurfs for surface models */
548         AddEntitySurfaceModels( e );
549
550         /* generate bsp brushes from map brushes */
551         EmitBrushes( e->brushes, &e->firstBrush, &e->numBrushes );
552
553         /* just put all the brushes in headnode */
554         for ( b = e->brushes; b; b = b->next )
555         {
556                 bc = CopyBrush( b );
557                 bc->next = node->brushlist;
558                 node->brushlist = bc;
559         }
560
561         /* subdivide each drawsurf as required by shader tesselation */
562         if ( !nosubdivide ) {
563                 SubdivideFaceSurfaces( e, tree );
564         }
565
566         /* add in any vertexes required to fix t-junctions */
567         if ( !notjunc ) {
568                 FixTJunctions( e );
569         }
570
571         /* ydnar: classify the surfaces and project lightmaps */
572         ClassifyEntitySurfaces( e );
573
574         /* ydnar: project decals */
575         MakeEntityDecals( e );
576
577         /* ydnar: meta surfaces */
578         MakeEntityMetaTriangles( e );
579         SmoothMetaTriangles();
580         FixMetaTJunctions();
581         MergeMetaTriangles();
582
583         /* add references to the final drawsurfs in the apropriate clusters */
584         FilterDrawsurfsIntoTree( e, tree );
585
586         /* match drawsurfaces back to original brushsides (sof2) */
587         FixBrushSides( e );
588
589         /* finish */
590         EndModel( e, node );
591         FreeTree( tree );
592 }
593
594
595
596 /*
597    ProcessModels()
598    process world + other models into the bsp
599  */
600
601 void ProcessModels( void ){
602         qboolean oldVerbose;
603         entity_t    *entity;
604
605
606         /* preserve -v setting */
607         oldVerbose = verbose;
608
609         /* start a new bsp */
610         BeginBSPFile();
611
612         /* create map fogs */
613         CreateMapFogs();
614
615         /* walk entity list */
616         for ( mapEntityNum = 0; mapEntityNum < numEntities; mapEntityNum++ )
617         {
618                 /* get entity */
619                 entity = &entities[ mapEntityNum ];
620                 if ( entity->brushes == NULL && entity->patches == NULL ) {
621                         continue;
622                 }
623
624                 /* process the model */
625                 Sys_FPrintf( SYS_VRB, "############### model %i ###############\n", numBSPModels );
626                 if ( mapEntityNum == 0 ) {
627                         ProcessWorldModel();
628                 }
629                 else{
630                         ProcessSubModel();
631                 }
632
633                 /* potentially turn off the deluge of text */
634                 verbose = verboseEntities;
635         }
636
637         /* restore -v setting */
638         verbose = oldVerbose;
639
640         /* write fogs */
641         EmitFogs();
642
643         /* vortex: emit meta stats */
644         EmitMetaStats();
645 }
646
647
648
649 /*
650    OnlyEnts()
651    this is probably broken unless teamed with a radiant version that preserves entity order
652  */
653
654 void OnlyEnts( void ){
655         char out[ 1024 ];
656
657         char save_cmdline[1024], save_version[1024], save_gridsize[1024];
658         const char *p;
659
660         /* note it */
661         Sys_Printf( "--- OnlyEnts ---\n" );
662
663         sprintf( out, "%s.bsp", source );
664         LoadBSPFile( out );
665
666         ParseEntities();
667         p = ValueForKey( &entities[0], "_q3map2_cmdline" );
668         strncpy( save_cmdline, p, sizeof( save_cmdline ) );
669         save_cmdline[sizeof( save_cmdline ) - 1] = 0;
670         p = ValueForKey( &entities[0], "_q3map2_version" );
671         strncpy( save_version, p, sizeof( save_version ) );
672         save_version[sizeof( save_version ) - 1] = 0;
673         p = ValueForKey( &entities[0], "gridsize" );
674         strncpy( save_gridsize, p, sizeof( save_gridsize ) );
675         save_gridsize[sizeof( save_gridsize ) - 1] = 0;
676
677         numEntities = 0;
678
679         LoadShaderInfo();
680         LoadMapFile( name, qfalse, qfalse );
681         SetModelNumbers();
682         SetLightStyles();
683
684         if ( *save_cmdline ) {
685                 SetKeyValue( &entities[0], "_q3map2_cmdline", save_cmdline );
686         }
687         if ( *save_version ) {
688                 SetKeyValue( &entities[0], "_q3map2_version", save_version );
689         }
690         if ( *save_gridsize ) {
691                 SetKeyValue( &entities[0], "gridsize", save_gridsize );
692         }
693
694         numBSPEntities = numEntities;
695         UnparseEntities();
696
697         WriteBSPFile( out );
698 }
699
700
701
702 /*
703    BSPMain() - ydnar
704    handles creation of a bsp from a map file
705  */
706
707 int BSPMain( int argc, char **argv ){
708         int i;
709         char path[ 1024 ], tempSource[ 1024 ];
710         qboolean onlyents = qfalse;
711
712
713         /* note it */
714         Sys_Printf( "--- BSP ---\n" );
715
716         doingBSP = qtrue;
717         SetDrawSurfacesBuffer();
718         mapDrawSurfs = safe_malloc( sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
719         memset( mapDrawSurfs, 0, sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
720         numMapDrawSurfs = 0;
721
722         tempSource[ 0 ] = '\0';
723         globalCelShader[0] = 0;
724
725         /* set standard game flags */
726         maxSurfaceVerts = game->maxSurfaceVerts;
727         maxSurfaceIndexes = game->maxSurfaceIndexes;
728         emitFlares = game->emitFlares;
729         texturesRGB = game->texturesRGB;
730         colorsRGB = game->colorsRGB;
731
732         /* process arguments */
733         for ( i = 1; i < ( argc - 1 ); i++ )
734         {
735                 if ( !strcmp( argv[ i ], "-onlyents" ) ) {
736                         Sys_Printf( "Running entity-only compile\n" );
737                         onlyents = qtrue;
738                 }
739                 else if ( !strcmp( argv[ i ], "-tempname" ) ) {
740                         strcpy( tempSource, argv[ ++i ] );
741                 }
742                 else if ( !strcmp( argv[ i ], "-tmpout" ) ) {
743                         strcpy( outbase, "/tmp" );
744                 }
745                 else if ( !strcmp( argv[ i ],  "-nowater" ) ) {
746                         Sys_Printf( "Disabling water\n" );
747                         nowater = qtrue;
748                 }
749                 else if ( !strcmp( argv[ i ], "-keeplights" ) ) {
750                         keepLights = qtrue;
751                         Sys_Printf( "Leaving light entities on map after compile\n" );
752                 }
753                 else if ( !strcmp( argv[ i ],  "-nodetail" ) ) {
754                         Sys_Printf( "Ignoring detail brushes\n" ) ;
755                         nodetail = qtrue;
756                 }
757                 else if ( !strcmp( argv[ i ],  "-fulldetail" ) ) {
758                         Sys_Printf( "Turning detail brushes into structural brushes\n" );
759                         fulldetail = qtrue;
760                 }
761                 else if ( !strcmp( argv[ i ],  "-nofog" ) ) {
762                         Sys_Printf( "Fog volumes disabled\n" );
763                         nofog = qtrue;
764                 }
765                 else if ( !strcmp( argv[ i ],  "-nosubdivide" ) ) {
766                         Sys_Printf( "Disabling brush face subdivision\n" );
767                         nosubdivide = qtrue;
768                 }
769                 else if ( !strcmp( argv[ i ],  "-leaktest" ) ) {
770                         Sys_Printf( "Leaktest enabled\n" );
771                         leaktest = qtrue;
772                 }
773                 else if ( !strcmp( argv[ i ],  "-verboseentities" ) ) {
774                         Sys_Printf( "Verbose entities enabled\n" );
775                         verboseEntities = qtrue;
776                 }
777                 else if ( !strcmp( argv[ i ], "-nocurves" ) ) {
778                         Sys_Printf( "Ignoring curved surfaces (patches)\n" );
779                         noCurveBrushes = qtrue;
780                 }
781                 else if ( !strcmp( argv[ i ], "-notjunc" ) ) {
782                         Sys_Printf( "T-junction fixing disabled\n" );
783                         notjunc = qtrue;
784                 }
785                 else if ( !strcmp( argv[ i ], "-fakemap" ) ) {
786                         Sys_Printf( "Generating fakemap.map\n" );
787                         fakemap = qtrue;
788                 }
789                 else if ( !strcmp( argv[ i ],  "-samplesize" ) ) {
790                         sampleSize = atoi( argv[ i + 1 ] );
791                         if ( sampleSize < 1 ) {
792                                 sampleSize = 1;
793                         }
794                         i++;
795                         Sys_Printf( "Lightmap sample size set to %dx%d units\n", sampleSize, sampleSize );
796                 }
797                 else if ( !strcmp( argv[ i ], "-minsamplesize" ) ) {
798                         minSampleSize = atoi( argv[ i + 1 ] );
799                         if ( minSampleSize < 1 ) {
800                                 minSampleSize = 1;
801                         }
802                         i++;
803                         Sys_Printf( "Minimum lightmap sample size set to %dx%d units\n", minSampleSize, minSampleSize );
804                 }
805                 else if ( !strcmp( argv[ i ],  "-custinfoparms" ) ) {
806                         Sys_Printf( "Custom info parms enabled\n" );
807                         useCustomInfoParms = qtrue;
808                 }
809
810                 /* sof2 args */
811                 else if ( !strcmp( argv[ i ], "-rename" ) ) {
812                         Sys_Printf( "Appending _bsp suffix to misc_model shaders (SOF2)\n" );
813                         renameModelShaders = qtrue;
814                 }
815
816                 /* ydnar args */
817                 else if ( !strcmp( argv[ i ],  "-ne" ) ) {
818                         normalEpsilon = atof( argv[ i + 1 ] );
819                         i++;
820                         Sys_Printf( "Normal epsilon set to %f\n", normalEpsilon );
821                 }
822                 else if ( !strcmp( argv[ i ],  "-de" ) ) {
823                         distanceEpsilon = atof( argv[ i + 1 ] );
824                         i++;
825                         Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
826                 }
827                 else if ( !strcmp( argv[ i ],  "-mv" ) ) {
828                         maxLMSurfaceVerts = atoi( argv[ i + 1 ] );
829                         if ( maxLMSurfaceVerts < 3 ) {
830                                 maxLMSurfaceVerts = 3;
831                         }
832                         if ( maxLMSurfaceVerts > maxSurfaceVerts ) {
833                                 maxSurfaceVerts = maxLMSurfaceVerts;
834                         }
835                         i++;
836                         Sys_Printf( "Maximum lightmapped surface vertex count set to %d\n", maxLMSurfaceVerts );
837                 }
838                 else if ( !strcmp( argv[ i ],  "-mi" ) ) {
839                         maxSurfaceIndexes = atoi( argv[ i + 1 ] );
840                         if ( maxSurfaceIndexes < 3 ) {
841                                 maxSurfaceIndexes = 3;
842                         }
843                         i++;
844                         Sys_Printf( "Maximum per-surface index count set to %d\n", maxSurfaceIndexes );
845                 }
846                 else if ( !strcmp( argv[ i ], "-np" ) ) {
847                         npDegrees = atof( argv[ i + 1 ] );
848                         if ( npDegrees < 0.0f ) {
849                                 npDegrees = 0.0f;
850                         }
851                         else if ( npDegrees > 0.0f ) {
852                                 Sys_Printf( "Forcing nonplanar surfaces with a breaking angle of %f degrees\n", npDegrees );
853                         }
854                         i++;
855                 }
856                 else if ( !strcmp( argv[ i ],  "-snap" ) ) {
857                         bevelSnap = atoi( argv[ i + 1 ] );
858                         if ( bevelSnap < 0 ) {
859                                 bevelSnap = 0;
860                         }
861                         i++;
862                         if ( bevelSnap > 0 ) {
863                                 Sys_Printf( "Snapping brush bevel planes to %d units\n", bevelSnap );
864                         }
865                 }
866                 else if ( !strcmp( argv[ i ],  "-texrange" ) ) {
867                         texRange = atoi( argv[ i + 1 ] );
868                         if ( texRange < 0 ) {
869                                 texRange = 0;
870                         }
871                         i++;
872                         Sys_Printf( "Limiting per-surface texture range to %d texels\n", texRange );
873                 }
874                 else if ( !strcmp( argv[ i ], "-nohint" ) ) {
875                         Sys_Printf( "Hint brushes disabled\n" );
876                         noHint = qtrue;
877                 }
878                 else if ( !strcmp( argv[ i ], "-flat" ) ) {
879                         Sys_Printf( "Flatshading enabled\n" );
880                         flat = qtrue;
881                 }
882                 else if ( !strcmp( argv[ i ], "-celshader" ) ) {
883                         ++i;
884                         if ( argv[i][0] ) {
885                                 sprintf( globalCelShader, "textures/%s", argv[ i ] );
886                         }
887                         else{
888                                 *globalCelShader = 0;
889                         }
890                         Sys_Printf( "Global cel shader set to \"%s\"\n", globalCelShader );
891                 }
892                 else if ( !strcmp( argv[ i ], "-meta" ) ) {
893                         Sys_Printf( "Creating meta surfaces from brush faces\n" );
894                         meta = qtrue;
895                 }
896                 else if ( !strcmp( argv[ i ], "-metaadequatescore" ) ) {
897                         metaAdequateScore = atoi( argv[ i + 1 ] );
898                         if ( metaAdequateScore < 0 ) {
899                                 metaAdequateScore = -1;
900                         }
901                         i++;
902                         if ( metaAdequateScore >= 0 ) {
903                                 Sys_Printf( "Setting ADEQUATE meta score to %d (see surface_meta.c)\n", metaAdequateScore );
904                         }
905                 }
906                 else if ( !strcmp( argv[ i ], "-metagoodscore" ) ) {
907                         metaGoodScore = atoi( argv[ i + 1 ] );
908                         if ( metaGoodScore < 0 ) {
909                                 metaGoodScore = -1;
910                         }
911                         i++;
912                         if ( metaGoodScore >= 0 ) {
913                                 Sys_Printf( "Setting GOOD meta score to %d (see surface_meta.c)\n", metaGoodScore );
914                         }
915                 }
916                 else if ( !strcmp( argv[ i ], "-metamaxbboxdistance" ) ) {
917                         metaMaxBBoxDistance = atof( argv[ i + 1 ] );
918                         if ( metaMaxBBoxDistance < 0 ) {
919                                 metaMaxBBoxDistance = -1;
920                         }
921                         i++;
922                         if ( metaMaxBBoxDistance >= 0 ) {
923                                 Sys_Printf( "Setting meta maximum bounding box distance to %f\n", metaMaxBBoxDistance );
924                         }
925                 }
926                 else if ( !strcmp( argv[ i ], "-patchmeta" ) ) {
927                         Sys_Printf( "Creating meta surfaces from patches\n" );
928                         patchMeta = qtrue;
929                 }
930                 else if ( !strcmp( argv[ i ], "-flares" ) ) {
931                         Sys_Printf( "Flare surfaces enabled\n" );
932                         emitFlares = qtrue;
933                 }
934                 else if ( !strcmp( argv[ i ], "-noflares" ) ) {
935                         Sys_Printf( "Flare surfaces disabled\n" );
936                         emitFlares = qfalse;
937                 }
938                 else if ( !strcmp( argv[ i ], "-skyfix" ) ) {
939                         Sys_Printf( "GL_CLAMP sky fix/hack/workaround enabled\n" );
940                         skyFixHack = qtrue;
941                 }
942                 else if ( !strcmp( argv[ i ], "-debugsurfaces" ) ) {
943                         Sys_Printf( "emitting debug surfaces\n" );
944                         debugSurfaces = qtrue;
945                 }
946                 else if ( !strcmp( argv[ i ], "-debuginset" ) ) {
947                         Sys_Printf( "Debug surface triangle insetting enabled\n" );
948                         debugInset = qtrue;
949                 }
950                 else if ( !strcmp( argv[ i ], "-debugportals" ) ) {
951                         Sys_Printf( "Debug portal surfaces enabled\n" );
952                         debugPortals = qtrue;
953                 }
954                 else if ( !strcmp( argv[ i ], "-debugclip" ) ) {
955                         Sys_Printf( "Debug model clip enabled\n" );
956                         debugClip = qtrue;
957                 }
958                 else if ( !strcmp( argv[ i ], "-snapmodelclip" ) ) {
959                         Sys_Printf( "Snapping model clip enabled\n" );
960                         snapModelClip = qtrue;
961                 }
962                 else if ( !strcmp( argv[ i ], "-sRGBtex" ) ) {
963                         texturesRGB = qtrue;
964                         Sys_Printf( "Textures are in sRGB\n" );
965                 }
966                 else if ( !strcmp( argv[ i ], "-nosRGBtex" ) ) {
967                         texturesRGB = qfalse;
968                         Sys_Printf( "Textures are linear\n" );
969                 }
970                 else if ( !strcmp( argv[ i ], "-sRGBcolor" ) ) {
971                         colorsRGB = qtrue;
972                         Sys_Printf( "Colors are in sRGB\n" );
973                 }
974                 else if ( !strcmp( argv[ i ], "-nosRGBcolor" ) ) {
975                         colorsRGB = qfalse;
976                         Sys_Printf( "Colors are linear\n" );
977                 }
978                 else if ( !strcmp( argv[ i ], "-nosRGB" ) ) {
979                         texturesRGB = qfalse;
980                         Sys_Printf( "Textures are linear\n" );
981                         colorsRGB = qfalse;
982                         Sys_Printf( "Colors are linear\n" );
983                 }
984                 else if ( !strcmp( argv[ i ], "-altsplit" ) ) {
985                         Sys_Printf( "Alternate BSP splitting (by 27) enabled\n" );
986                         bspAlternateSplitWeights = qtrue;
987                 }
988                 else if ( !strcmp( argv[ i ], "-deep" ) ) {
989                         Sys_Printf( "Deep BSP tree generation enabled\n" );
990                         deepBSP = qtrue;
991                 }
992                 else if ( !strcmp( argv[ i ], "-maxarea" ) ) {
993                         Sys_Printf( "Max Area face surface generation enabled\n" );
994                         maxAreaFaceSurface = qtrue;
995                 }
996                 else if ( !strcmp( argv[ i ], "-bsp" ) ) {
997                         Sys_Printf( "-bsp argument unnecessary\n" );
998                 }
999                 else if ( !strcmp( argv[ i ], "-noob" ) ) {
1000                         Sys_Printf( "No oBs!\n" );
1001                         noob = qtrue;
1002                 }
1003                 else
1004                 {
1005                         Sys_Printf( "WARNING: Unknown option \"%s\"\n", argv[ i ] );
1006                 }
1007         }
1008
1009         /* fixme: print more useful usage here */
1010         if ( i != ( argc - 1 ) ) {
1011                 Error( "usage: q3map [options] mapfile" );
1012         }
1013
1014         /* copy source name */
1015         strcpy( source, ExpandArg( argv[ i ] ) );
1016         StripExtension( source );
1017
1018         /* ydnar: set default sample size */
1019         SetDefaultSampleSize( sampleSize );
1020
1021         /* delete portal, line and surface files */
1022         sprintf( path, "%s.prt", source );
1023         remove( path );
1024         sprintf( path, "%s.lin", source );
1025         remove( path );
1026         //%     sprintf( path, "%s.srf", source );      /* ydnar */
1027         //%     remove( path );
1028
1029         /* expand mapname */
1030         strcpy( name, ExpandArg( argv[ i ] ) );
1031         if ( strcmp( name + strlen( name ) - 4, ".reg" ) ) {
1032                 /* if we are doing a full map, delete the last saved region map */
1033                 sprintf( path, "%s.reg", source );
1034                 remove( path );
1035                 DefaultExtension( name, ".map" );   /* might be .reg */
1036         }
1037
1038         /* if onlyents, just grab the entites and resave */
1039         if ( onlyents ) {
1040                 OnlyEnts();
1041                 return 0;
1042         }
1043
1044         /* load shaders */
1045         LoadShaderInfo();
1046
1047         /* load original file from temp spot in case it was renamed by the editor on the way in */
1048         if ( strlen( tempSource ) > 0 ) {
1049                 LoadMapFile( tempSource, qfalse, qfalse );
1050         }
1051         else{
1052                 LoadMapFile( name, qfalse, qfalse );
1053         }
1054
1055         /* div0: inject command line parameters */
1056         InjectCommandLine( argv, 1, argc - 1 );
1057
1058         /* ydnar: decal setup */
1059         ProcessDecals();
1060
1061         /* ydnar: cloned brush model entities */
1062         SetCloneModelNumbers();
1063
1064         /* process world and submodels */
1065         ProcessModels();
1066
1067         /* set light styles from targetted light entities */
1068         SetLightStyles();
1069
1070         /* process in game advertisements */
1071         ProcessAdvertisements();
1072
1073         /* finish and write bsp */
1074         EndBSPFile( qtrue );
1075
1076         /* remove temp map source file if appropriate */
1077         if ( strlen( tempSource ) > 0 ) {
1078                 remove( tempSource );
1079         }
1080
1081         /* return to sender */
1082         return 0;
1083 }