diff --git a/tools/quake3/q3map2/bsp.cpp b/tools/quake3/q3map2/bsp.cpp index 15876dc0..5ac897a1 100644 --- a/tools/quake3/q3map2/bsp.cpp +++ b/tools/quake3/q3map2/bsp.cpp @@ -146,13 +146,11 @@ static void ProcessAdvertisements() { */ static void SetCloneModelNumbers(){ - int models; - char modelValue[ 16 ]; const char *value, *value2, *value3; /* start with 1 (worldspawn is model 0) */ - models = 1; + int models = 1; for ( std::size_t i = 1; i < entities.size(); ++i ) { /* only entities with brushes or patches get a model number */ @@ -165,11 +163,7 @@ static void SetCloneModelNumbers(){ continue; /* add the model key */ - sprintf( modelValue, "*%d", models ); - entities[ i ].setKeyValue( "model", modelValue ); - - /* increment model count */ - models++; + entities[ i ].setKeyValue( "model", models++, "*%i" ); } /* fix up clones */ @@ -202,8 +196,7 @@ static void SetCloneModelNumbers(){ models = atoi( &value3[ 1 ] ); /* add the model key */ - sprintf( modelValue, "*%d", models ); - entities[ i ].setKeyValue( "model", modelValue ); + entities[ i ].setKeyValue( "model", models, "*%i" ); /* nuke the brushes/patches for this entity (fixme: leak!) */ auto *leak = new brushlist_t( std::move( entities[ i ].brushes ) ); // are brushes referenced elsewhere, so we do not nuke them really? diff --git a/tools/quake3/q3map2/bspfile_abstract.cpp b/tools/quake3/q3map2/bspfile_abstract.cpp index 2f58ec29..8619b7d3 100644 --- a/tools/quake3/q3map2/bspfile_abstract.cpp +++ b/tools/quake3/q3map2/bspfile_abstract.cpp @@ -545,6 +545,24 @@ const char *entity_t::valueForKey( const char *key ) const { return ""; } + +void entity_t::setKeyValue( const char *key, int value, const char *format /* = "%i" */ ){ + char buf[ 16 ]; + std::snprintf( buf, std::size( buf ), format, value ); + setKeyValue( key, buf ); +} +void entity_t::setKeyValue( const char *key, float value ){ + char buf[ 32 ]; + std::snprintf( buf, std::size( buf ), "%f", value ); + setKeyValue( key, buf ); +} +void entity_t::setKeyValue( const char *key, const Vector3& value ){ + char buf[ 128 ]; + std::snprintf( buf, std::size( buf ), "%f %f %f", value[0], value[1], value[2] ); + setKeyValue( key, buf ); +} + + bool entity_t::read_keyvalue_( bool &bool_value, std::initializer_list&& keys ) const { for( const char* key : keys ){ const char* value = valueForKey( key ); diff --git a/tools/quake3/q3map2/convert_bsp.cpp b/tools/quake3/q3map2/convert_bsp.cpp index 910a8486..280451b5 100644 --- a/tools/quake3/q3map2/convert_bsp.cpp +++ b/tools/quake3/q3map2/convert_bsp.cpp @@ -349,7 +349,6 @@ int ScaleBSPMain( Args& args ){ float f, a; Vector3 scale; Vector3 vec; - char str[ 1024 ]; int axis; bool texscale; std::vector old_xyzst; @@ -414,8 +413,7 @@ int ScaleBSPMain( Args& args ){ if ( e.classname_prefixed( "info_player_" ) ) { vec[2] -= spawn_ref; } - sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] ); - e.setKeyValue( "origin", str ); + e.setKeyValue( "origin", vec ); } a = e.floatForKey( "angle" ); @@ -432,15 +430,13 @@ int ScaleBSPMain( Args& args ){ /* scale door lip */ if ( e.read_keyvalue( f, "lip" ) ) { f *= scale[axis]; - sprintf( str, "%f", f ); - e.setKeyValue( "lip", str ); + e.setKeyValue( "lip", f ); } /* scale plat height */ if ( e.read_keyvalue( f, "height" ) ) { f *= scale[2]; - sprintf( str, "%f", f ); - e.setKeyValue( "height", str ); + e.setKeyValue( "height", f ); } // TODO maybe allow a definition file for entities to specify which values are scaled how? @@ -543,8 +539,7 @@ int ScaleBSPMain( Args& args ){ vec = gridSize; } vec *= scale; - sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] ); - entities[ 0 ].setKeyValue( "gridsize", str ); + entities[ 0 ].setKeyValue( "gridsize", vec ); /* inject command line parameters */ InjectCommandLine( "-scale", argsToInject ); @@ -567,7 +562,6 @@ int ScaleBSPMain( Args& args ){ int ShiftBSPMain( Args& args ){ Vector3 shift; Vector3 vec; - char str[ 1024 ]; /* arg checking */ @@ -608,8 +602,7 @@ int ShiftBSPMain( Args& args ){ /* shift origin */ if ( e.read_keyvalue( vec, "origin" ) ) { // fixme: this doesn't consider originless point entities; group entities with origin will be wrong too vec += shift; - sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] ); - e.setKeyValue( "origin", str ); + e.setKeyValue( "origin", vec ); } } @@ -773,7 +766,7 @@ int MergeBSPMain( Args& args ){ { const char *model = e.valueForKey( "model" ); if( model[0] == '*' ){ - e.setKeyValue( "model", StringStream<8>( '*', atoi( model + 1 ) + bspModels.size() - 1 ) ); // -1 : minus world + e.setKeyValue( "model", atoi( model + 1 ) + bspModels.size() - 1, "*%i" ); // -1 : minus world } } /* make target/targetname names unique */ @@ -942,7 +935,6 @@ int MergeBSPMain( Args& args ){ */ static void PseudoCompileBSP( bool need_tree ){ int models = 1; - char modelValue[16]; facelist_t faces; tree_t tree{}; @@ -959,8 +951,7 @@ static void PseudoCompileBSP( bool need_tree ){ } if ( entityNum != 0 ) { - sprintf( modelValue, "*%d", models++ ); - entity.setKeyValue( "model", modelValue ); + entity.setKeyValue( "model", models++, "*%i" ); } /* process the model */ diff --git a/tools/quake3/q3map2/map.cpp b/tools/quake3/q3map2/map.cpp index 80ccadc9..37bb0d68 100644 --- a/tools/quake3/q3map2/map.cpp +++ b/tools/quake3/q3map2/map.cpp @@ -753,15 +753,12 @@ void AddBrushBevels(){ static void MergeOrigin( entity_t& ent, const Vector3& origin ){ - char string[128]; - /* we have not parsed the brush completely yet... */ ent.origin = ent.vectorForKey( "origin" ) + origin - ent.originbrush_origin; ent.originbrush_origin = origin; - sprintf( string, "%f %f %f", ent.origin[0], ent.origin[1], ent.origin[2] ); - ent.setKeyValue( "origin", string ); + ent.setKeyValue( "origin", ent.origin ); } static void FixAreaportalBrush( brush_t& brush, const entity_t& mapEnt ){ diff --git a/tools/quake3/q3map2/q3map2.h b/tools/quake3/q3map2/q3map2.h index 7ae772d9..70638fb2 100644 --- a/tools/quake3/q3map2/q3map2.h +++ b/tools/quake3/q3map2/q3map2.h @@ -923,6 +923,9 @@ struct entity_t Vector3 originbrush_origin{ 0 }; void setKeyValue( const char *key, const char *value ); + void setKeyValue( const char *key, int value, const char *format = "%i" ); + void setKeyValue( const char *key, float value ); + void setKeyValue( const char *key, const Vector3& value ); const char *valueForKey( const char *key ) const; template diff --git a/tools/quake3/q3map2/writebsp.cpp b/tools/quake3/q3map2/writebsp.cpp index b9c0c073..2e70835c 100644 --- a/tools/quake3/q3map2/writebsp.cpp +++ b/tools/quake3/q3map2/writebsp.cpp @@ -208,10 +208,7 @@ void SetModelNumbers(){ int models = 1; for ( std::size_t i = 1; i < entities.size(); ++i ) { if ( !entities[i].brushes.empty() || !entities[i].patches.empty() ) { - char value[16]; - sprintf( value, "*%i", models ); - models++; - entities[i].setKeyValue( "model", value ); + entities[i].setKeyValue( "model", models++, "*%i" ); } } } @@ -226,7 +223,6 @@ void SetModelNumbers(){ void SetLightStyles(){ int j, numStyles; - char value[ 10 ]; char lightTargets[ MAX_SWITCHED_LIGHTS ][ 64 ]; int lightStyles[ MAX_SWITCHED_LIGHTS ]; int numStrippedLights = 0; @@ -283,13 +279,11 @@ void SetLightStyles(){ } /* set explicit style */ - sprintf( value, "%d", MAX_SWITCHED_LIGHTS + j ); - e.setKeyValue( "style", value ); + e.setKeyValue( "style", MAX_SWITCHED_LIGHTS + j ); /* set old style */ if ( style != LS_NORMAL ) { - sprintf( value, "%d", style ); - e.setKeyValue( "switch_style", value ); + e.setKeyValue( "switch_style", style ); } } @@ -301,9 +295,7 @@ void SetLightStyles(){ void UnSetLightStyles(){ for ( entity_t& e : entities ){ if ( e.classname_prefixed( "light" ) && !strEmpty( e.valueForKey( "targetname" ) ) && !strEmpty( e.valueForKey( "style" ) ) ) { - char value[ 10 ]; - sprintf( value, "%d", e.intForKey( "switch_style" ) ); // value or 0, latter is fine too - e.setKeyValue( "style", value ); + e.setKeyValue( "style", e.intForKey( "switch_style" ) ); // value or 0, latter is fine too } } }