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