diff --git a/tools/quake3/q3map2/bspfile_abstract.cpp b/tools/quake3/q3map2/bspfile_abstract.cpp index 4b6e9f7a..f92e81c7 100644 --- a/tools/quake3/q3map2/bspfile_abstract.cpp +++ b/tools/quake3/q3map2/bspfile_abstract.cpp @@ -187,7 +187,7 @@ void SwapBSPFile( void ){ SwapBlock( bspPlanes ); /* nodes */ - SwapBlock( (int*) bspNodes, numBSPNodes * sizeof( bspNodes[ 0 ] ) ); + SwapBlock( bspNodes ); /* leafs */ SwapBlock( bspLeafs ); @@ -478,8 +478,8 @@ void PrintBSPFileSizes( void ){ entities.size(), bspEntData.size() ); Sys_Printf( "\n" ); - Sys_Printf( "%9d nodes %9d\n", - numBSPNodes, (int) ( numBSPNodes * sizeof( bspNode_t ) ) ); + Sys_Printf( "%9zu nodes %9zu\n", + bspNodes.size(), bspNodes.size() * sizeof( bspNodes[0] ) ); Sys_Printf( "%9zu leafs %9zu\n", bspLeafs.size(), bspLeafs.size() * sizeof( bspLeafs[0] ) ); Sys_Printf( "%9d leafsurfaces %9d\n", diff --git a/tools/quake3/q3map2/bspfile_ibsp.cpp b/tools/quake3/q3map2/bspfile_ibsp.cpp index d6e60822..e3c54af9 100644 --- a/tools/quake3/q3map2/bspfile_ibsp.cpp +++ b/tools/quake3/q3map2/bspfile_ibsp.cpp @@ -441,7 +441,7 @@ void LoadIBSPFile( const char *filename ){ CopyLump( (bspHeader_t*) header, LUMP_LEAFS, bspLeafs ); - numBSPNodes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_NODES, (void **) &bspNodes, sizeof( bspNode_t ), &allocatedBSPNodes ); + CopyLump( (bspHeader_t*) header, LUMP_NODES, bspNodes ); numBSPLeafSurfaces = CopyLump_Allocate( (bspHeader_t*) header, LUMP_LEAFSURFACES, (void **) &bspLeafSurfaces, sizeof( bspLeafSurfaces[ 0 ] ), &allocatedBSPLeafSurfaces ); @@ -554,7 +554,7 @@ void WriteIBSPFile( const char *filename ){ AddLump( file, header->lumps[LUMP_SHADERS], bspShaders ); AddLump( file, header->lumps[LUMP_PLANES], bspPlanes ); AddLump( file, header->lumps[LUMP_LEAFS], bspLeafs ); - AddLump( file, (bspHeader_t*) header, LUMP_NODES, bspNodes, numBSPNodes * sizeof( bspNode_t ) ); + AddLump( file, header->lumps[LUMP_NODES], bspNodes ); AddLump( file, (bspHeader_t*) header, LUMP_BRUSHES, bspBrushes, numBSPBrushes * sizeof( bspBrush_t ) ); AddBrushSidesLump( file, header ); AddLump( file, (bspHeader_t*) header, LUMP_LEAFSURFACES, bspLeafSurfaces, numBSPLeafSurfaces * sizeof( bspLeafSurfaces[ 0 ] ) ); diff --git a/tools/quake3/q3map2/bspfile_rbsp.cpp b/tools/quake3/q3map2/bspfile_rbsp.cpp index 89e8979a..27b4246b 100644 --- a/tools/quake3/q3map2/bspfile_rbsp.cpp +++ b/tools/quake3/q3map2/bspfile_rbsp.cpp @@ -232,7 +232,7 @@ void LoadRBSPFile( const char *filename ){ CopyLump( (bspHeader_t*) header, LUMP_LEAFS, bspLeafs ); - numBSPNodes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_NODES, (void **) &bspNodes, sizeof( bspNode_t ), &allocatedBSPNodes ); + CopyLump( (bspHeader_t*) header, LUMP_NODES, bspNodes ); numBSPLeafSurfaces = CopyLump_Allocate( (bspHeader_t*) header, LUMP_LEAFSURFACES, (void **) &bspLeafSurfaces, sizeof( bspLeafSurfaces[ 0 ] ), &allocatedBSPLeafSurfaces ); @@ -307,7 +307,7 @@ void WriteRBSPFile( const char *filename ){ AddLump( file, header->lumps[LUMP_SHADERS], bspShaders ); AddLump( file, header->lumps[LUMP_PLANES], bspPlanes ); AddLump( file, header->lumps[LUMP_LEAFS], bspLeafs ); - AddLump( file, (bspHeader_t*) header, LUMP_NODES, bspNodes, numBSPNodes * sizeof( bspNode_t ) ); + AddLump( file, header->lumps[LUMP_NODES], bspNodes ); AddLump( file, (bspHeader_t*) header, LUMP_BRUSHES, bspBrushes, numBSPBrushes * sizeof( bspBrush_t ) ); AddLump( file, (bspHeader_t*) header, LUMP_BRUSHSIDES, bspBrushSides, numBSPBrushSides * sizeof( bspBrushSides[ 0 ] ) ); AddLump( file, (bspHeader_t*) header, LUMP_LEAFSURFACES, bspLeafSurfaces, numBSPLeafSurfaces * sizeof( bspLeafSurfaces[ 0 ] ) ); diff --git a/tools/quake3/q3map2/convert_bsp.cpp b/tools/quake3/q3map2/convert_bsp.cpp index ae21f996..da196fb2 100644 --- a/tools/quake3/q3map2/convert_bsp.cpp +++ b/tools/quake3/q3map2/convert_bsp.cpp @@ -485,10 +485,10 @@ int ScaleBSPMain( Args& args ){ } /* scale nodes */ - for ( i = 0; i < numBSPNodes; i++ ) + for ( bspNode_t& node : bspNodes ) { - bspNodes[ i ].minmax.mins *= scale; - bspNodes[ i ].minmax.maxs *= scale; + node.minmax.mins *= scale; + node.minmax.maxs *= scale; } /* scale leafs */ @@ -654,10 +654,10 @@ int ShiftBSPMain( Args& args ){ } /* shift nodes */ - for ( i = 0; i < numBSPNodes; i++ ) + for ( bspNode_t& node : bspNodes ) { - bspNodes[ i ].minmax.mins += shift; - bspNodes[ i ].minmax.maxs += shift; + node.minmax.mins += shift; + node.minmax.maxs += shift; } /* shift leafs */ diff --git a/tools/quake3/q3map2/convert_json.cpp b/tools/quake3/q3map2/convert_json.cpp index 11793c23..f21d2849 100644 --- a/tools/quake3/q3map2/convert_json.cpp +++ b/tools/quake3/q3map2/convert_json.cpp @@ -205,7 +205,7 @@ static void write_json( const char *directory ){ } { doc.RemoveAllMembers(); - for_indexed( auto&& node : Span( bspNodes, numBSPNodes ) ){ + for_indexed( const auto& node : bspNodes ){ rapidjson::Value value( rapidjson::kObjectType ); value.AddMember( "planeNum", node.planeNum, all ); value.AddMember( "children", value_for_array( node.children, all ), all ); @@ -423,17 +423,14 @@ static void read_json( const char *directory ){ } { const auto doc = load_json( StringOutputStream( 256 )( directory, "nodes.json" ) ); - static std::vector items; for( auto&& obj : doc.GetObj() ){ - auto&& item = items.emplace_back(); + auto&& item = bspNodes.emplace_back(); item.planeNum = obj.value["planeNum"].GetInt(); item.children[0] = obj.value["children"].operator[]( 0 ).GetInt(); item.children[1] = obj.value["children"].operator[]( 1 ).GetInt(); value_to( obj.value["minmax"].GetObj().operator[]("mins"), item.minmax.mins ); value_to( obj.value["minmax"].GetObj().operator[]("maxs"), item.minmax.maxs ); } - bspNodes = items.data(); - numBSPNodes = items.size(); } { const auto doc = load_json( StringOutputStream( 256 )( directory, "LeafSurfaces.json" ) ); diff --git a/tools/quake3/q3map2/light_ydnar.cpp b/tools/quake3/q3map2/light_ydnar.cpp index 70ea61ef..e3aa920f 100644 --- a/tools/quake3/q3map2/light_ydnar.cpp +++ b/tools/quake3/q3map2/light_ydnar.cpp @@ -3320,7 +3320,6 @@ bool ChopBounds( MinMax& minmax, const Vector3& origin, const Vector3& normal ){ #define LIGHT_NUDGE 2.0f void SetupEnvelopes( bool forGrid, bool fastFlag ){ - int i, x, y, z, x1, y1, z1; float radius, intensity; @@ -3361,18 +3360,18 @@ void SetupEnvelopes( bool forGrid, bool fastFlag ){ /* invalid cluster? */ if ( light->cluster < 0 ) { /* nudge the sample point around a bit */ - for ( x = 0; x < 4; x++ ) + for ( int x = 0; x < 4; x++ ) { /* two's complement 0, 1, -1, 2, -2, etc */ - x1 = ( ( x >> 1 ) ^ ( x & 1 ? -1 : 0 ) ) + ( x & 1 ); + const int x1 = ( ( x >> 1 ) ^ ( x & 1 ? -1 : 0 ) ) + ( x & 1 ); - for ( y = 0; y < 4; y++ ) + for ( int y = 0; y < 4; y++ ) { - y1 = ( ( y >> 1 ) ^ ( y & 1 ? -1 : 0 ) ) + ( y & 1 ); + const int y1 = ( ( y >> 1 ) ^ ( y & 1 ? -1 : 0 ) ) + ( y & 1 ); - for ( z = 0; z < 4; z++ ) + for ( int z = 0; z < 4; z++ ) { - z1 = ( ( z >> 1 ) ^ ( z & 1 ? -1 : 0 ) ) + ( z & 1 ); + const int z1 = ( ( z >> 1 ) ^ ( z & 1 ? -1 : 0 ) ) + ( z & 1 ); /* nudge origin */ const Vector3 origin = light->origin + Vector3( x1, y1, z1 ) * LIGHT_NUDGE; diff --git a/tools/quake3/q3map2/q3map2.h b/tools/quake3/q3map2/q3map2.h index ce0b93cc..0be95c93 100644 --- a/tools/quake3/q3map2/q3map2.h +++ b/tools/quake3/q3map2/q3map2.h @@ -2344,9 +2344,7 @@ Q_EXTERN std::vector bspLeafs; // MAX_MAP_LEAFS Q_EXTERN std::vector bspPlanes; -Q_EXTERN int numBSPNodes Q_ASSIGN( 0 ); -Q_EXTERN int allocatedBSPNodes Q_ASSIGN( 0 ); -Q_EXTERN bspNode_t* bspNodes Q_ASSIGN( NULL ); +Q_EXTERN std::vector bspNodes; Q_EXTERN int numBSPLeafSurfaces Q_ASSIGN( 0 ); Q_EXTERN int allocatedBSPLeafSurfaces Q_ASSIGN( 0 ); diff --git a/tools/quake3/q3map2/writebsp.cpp b/tools/quake3/q3map2/writebsp.cpp index 286a37fe..0fb60ff1 100644 --- a/tools/quake3/q3map2/writebsp.cpp +++ b/tools/quake3/q3map2/writebsp.cpp @@ -171,10 +171,6 @@ void EmitLeaf( node_t *node ){ */ int EmitDrawNode_r( node_t *node ){ - bspNode_t *n; - int i, n0; - - /* check for leafnode */ if ( node->planenum == PLANENUM_LEAF ) { EmitLeaf( node ); @@ -182,38 +178,37 @@ int EmitDrawNode_r( node_t *node ){ } /* emit a node */ - AUTOEXPAND_BY_REALLOC_BSP( Nodes, 1024 ); - n0 = numBSPNodes; - n = &bspNodes[ n0 ]; - numBSPNodes++; + const int id = bspNodes.size(); + { + bspNode_t& bnode = bspNodes.emplace_back(); - n->minmax.mins = node->minmax.mins; - n->minmax.maxs = node->minmax.maxs; + bnode.minmax.mins = node->minmax.mins; + bnode.minmax.maxs = node->minmax.maxs; - if ( node->planenum & 1 ) { - Error( "WriteDrawNodes_r: odd planenum" ); + if ( node->planenum & 1 ) { + Error( "WriteDrawNodes_r: odd planenum" ); + } + bnode.planeNum = node->planenum; } - n->planeNum = node->planenum; // // recursively output the other nodes // - for ( i = 0; i < 2; i++ ) + for ( int i = 0; i < 2; i++ ) { + // reference node by id, as it may be reallocated if ( node->children[i]->planenum == PLANENUM_LEAF ) { - n->children[i] = -int( bspLeafs.size() + 1 ); + bspNodes[id].children[i] = -int( bspLeafs.size() + 1 ); EmitLeaf( node->children[i] ); } else { - n->children[i] = numBSPNodes; + bspNodes[id].children[i] = bspNodes.size(); EmitDrawNode_r( node->children[i] ); - // n may have become invalid here, so... - n = &bspNodes[ n0 ]; } } - return n - bspNodes; + return id; } @@ -326,7 +321,7 @@ void SetLightStyles( void ){ void BeginBSPFile( void ){ /* these values may actually be initialized if the file existed when loaded, so clear them explicitly */ bspModels.clear(); - numBSPNodes = 0; + bspNodes.clear(); numBSPBrushSides = 0; numBSPLeafSurfaces = 0; numBSPLeafBrushes = 0;