]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/q3map2/writebsp.c
fix crash in -bsp
[xonotic/netradiant.git] / tools / quake3 / q3map2 / writebsp.c
index 70a382dd4d0b1f491062efdf3487ad5fb1cbe85b..5321d068e755d910ea042d058007239320ca6503 100644 (file)
@@ -116,6 +116,7 @@ void EmitPlanes( void )
        mp = mapplanes;
        for( i = 0; i < nummapplanes; i++, mp++ )
        {
+               AUTOEXPAND_BY_REALLOC_BSP(Planes, 1024);
                bp = &bspPlanes[ numBSPPlanes ];
                VectorCopy( mp->normal, bp->normal );
                bp->dist = mp->dist;
@@ -199,7 +200,7 @@ recursively emit the bsp nodes
 int EmitDrawNode_r( node_t *node )
 {
        bspNode_t       *n;
-       int                     i;
+       int                     i, n0;
        
        
        /* check for leafnode */
@@ -211,7 +212,8 @@ int EmitDrawNode_r( node_t *node )
        
        /* emit a node */
        AUTOEXPAND_BY_REALLOC_BSP(Nodes, 1024);
-       n = &bspNodes[ numBSPNodes ];
+       n0 = numBSPNodes;
+       n = &bspNodes[ n0 ];
        numBSPNodes++;
        
        VectorCopy (node->mins, n->mins);
@@ -235,6 +237,8 @@ int EmitDrawNode_r( node_t *node )
                {
                        n->children[i] = numBSPNodes;   
                        EmitDrawNode_r (node->children[i]);
+                       // n may have become invalid here, so...
+                       n = &bspNodes[ n0 ];
                }
        }
 
@@ -276,18 +280,23 @@ sets style keys for entity lights
 void SetLightStyles( void )
 {
        int                     i, j, style, numStyles;
-       qboolean        keepLights;
        const char      *t;
        entity_t        *e;
        epair_t         *ep, *next;
        char            value[ 10 ];
        char            lightTargets[ MAX_SWITCHED_LIGHTS ][ 64 ];
        int                     lightStyles[ MAX_SWITCHED_LIGHTS ];
-       
-       
+
+       /* -keeplights option: force lights to be kept and ignore what the map file says */
+       if (keepLights)
+               SetKeyValue(&entities[0], "_keepLights", "1");
+
        /* ydnar: determine if we keep lights in the bsp */
-       t = ValueForKey( &entities[ 0 ], "_keepLights" );
-       keepLights = (t[ 0 ] == '1') ? qtrue : qfalse;
+       if (KeyExists(&entities[ 0 ], "_keepLights") == qtrue)
+       {
+               t = ValueForKey( &entities[ 0 ], "_keepLights" );
+               keepLights = (t[ 0 ] == '1') ? qtrue : qfalse;
+       }
        
        /* any light that is controlled (has a targetname) must have a unique style number generated for it */
        numStyles = 0;
@@ -379,6 +388,7 @@ void BeginBSPFile( void )
        
        /* ydnar: gs mods: set the first 6 drawindexes to 0 1 2 2 1 3 for triangles and quads */
        numBSPDrawIndexes = 6;
+       AUTOEXPAND_BY_REALLOC_BSP(DrawIndexes, 1024);
        bspDrawIndexes[ 0 ] = 0;
        bspDrawIndexes[ 1 ] = 1;
        bspDrawIndexes[ 2 ] = 2;
@@ -394,7 +404,7 @@ EndBSPFile()
 finishes a new bsp and writes to disk
 */
 
-void EndBSPFile( void )
+void EndBSPFile(qboolean do_write)
 {
        char    path[ 1024 ];
        
@@ -406,13 +416,16 @@ void EndBSPFile( void )
        numBSPEntities = numEntities;
        UnparseEntities();
        
-       /* write the surface extra file */
-       WriteSurfaceExtraFile( source );
-       
-       /* write the bsp */
-       sprintf( path, "%s.bsp", source );
-       Sys_Printf( "Writing %s\n", path );
-       WriteBSPFile( path );
+       if(do_write)
+       {
+               /* write the surface extra file */
+               WriteSurfaceExtraFile( source );
+
+               /* write the bsp */
+               sprintf( path, "%s.bsp", source );
+               Sys_Printf( "Writing %s\n", path );
+               WriteBSPFile( path );
+       }
 }