Skip to content

Commit ee2a5e3

Browse files
authored
std::variant ter_furn_id (#82802)
adds ter_furn_id::deserialize
1 parent 23aab34 commit ee2a5e3

File tree

5 files changed

+78
-43
lines changed

5 files changed

+78
-43
lines changed

src/mapdata.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,37 @@ int_id<furn_t>::int_id( const string_id<furn_t> &id ) : _id( id.id() )
149149
{
150150
}
151151

152+
ter_furn_id::ter_furn_id()
153+
{
154+
ter_furn = ter_str_id::NULL_ID().id();
155+
}
156+
157+
ter_furn_id::ter_furn_id( const std::string &name )
158+
{
159+
resolve( name );
160+
}
161+
162+
bool ter_furn_id::resolve( const std::string &name )
163+
{
164+
ter_str_id temp_ter( name );
165+
furn_str_id temp_furn( name );
166+
bool resolved = false;
167+
if( temp_ter.is_valid() && !temp_ter.is_null() ) {
168+
ter_furn = ter_id( name );
169+
resolved = true;
170+
} else if( temp_furn.is_valid() && !temp_furn.is_null() ) {
171+
ter_furn = furn_id( name );
172+
resolved = true;
173+
}
174+
return resolved;
175+
}
176+
177+
void ter_furn_id::deserialize( const JsonValue &jo )
178+
{
179+
std::string name = jo.get_string();
180+
resolve( name );
181+
}
182+
152183
namespace io
153184
{
154185

src/mapdata.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <string>
1111
#include <string_view>
1212
#include <utility>
13+
#include <variant>
1314
#include <vector>
1415

1516
#include "calendar.h"
@@ -27,6 +28,7 @@
2728

2829
class Character;
2930
class JsonObject;
31+
class JsonValue;
3032
struct connect_group;
3133
struct furn_t;
3234
struct itype;
@@ -769,6 +771,18 @@ struct furn_t : map_data_common_t {
769771
void check() const override;
770772
};
771773

774+
//holds either a ter_id OR furn_id (not both!), for loading JSON
775+
struct ter_furn_id {
776+
std::variant<ter_id, furn_id> ter_furn;
777+
void deserialize( const JsonValue &jo );
778+
ter_furn_id();
779+
explicit ter_furn_id( const std::string &name );
780+
bool operator==( const ter_furn_id &rhs ) const {
781+
return ter_furn == rhs.ter_furn;
782+
}
783+
bool resolve( const std::string &name );
784+
};
785+
772786
void load_furniture( const JsonObject &jo, const std::string &src );
773787
void finalize_furniture();
774788
void load_terrain( const JsonObject &jo, const std::string &src );

src/mapgen_functions.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <string>
1212
#include <unordered_set>
1313
#include <utility>
14+
#include <variant>
1415
#include <vector>
1516

1617
#include "calendar.h"
@@ -1038,7 +1039,7 @@ void mapgen_forest( mapgendata &dat )
10381039
* @param p the point to check to place a feature at.
10391040
*/
10401041
const auto get_feathered_feature = [&no_ter_furn, &max_factor, &factor, &self_biome,
1041-
&adjacent_biomes, &nesw_weights, &get_feathered_groundcover, &unify_all_borders,
1042+
&adjacent_biomes, &nesw_weights, &unify_all_borders,
10421043
&dat]( const point & p ) {
10431044
std::array<float, 4> adj_weights;
10441045
float net_weight = nesw_weights( p, factor, adj_weights );
@@ -1079,9 +1080,6 @@ void mapgen_forest( mapgendata &dat )
10791080
}
10801081
break;
10811082
}
1082-
if( feature.ter == no_ter_furn.ter ) {
1083-
feature.ter = get_feathered_groundcover( p );
1084-
}
10851083
return feature;
10861084
};
10871085

@@ -1118,10 +1116,24 @@ void mapgen_forest( mapgendata &dat )
11181116
// Lay groundcover, place a feature, and place terrain dependent furniture.
11191117
for( int x = 0; x < SEEX * 2; x++ ) {
11201118
for( int y = 0; y < SEEY * 2; y++ ) {
1121-
const ter_furn_id feature = get_feathered_feature( point( x, y ) );
1122-
m->ter_set( point_bub_ms( x, y ), feature.ter );
1123-
m->furn_set( point_bub_ms( x, y ), feature.furn );
1124-
set_terrain_dependent_furniture( feature.ter, point_bub_ms( x, y ) );
1119+
const point pos_raw = point( x, y );
1120+
const point_bub_ms pos = point_bub_ms( x, y );
1121+
1122+
ter_furn_id feature = get_feathered_feature( pos_raw );
1123+
ter_id groundcover = get_feathered_groundcover( pos_raw );
1124+
1125+
const ter_id *is_ter = std::get_if<ter_id>( &feature.ter_furn );
1126+
const furn_id *is_furniture = std::get_if<furn_id>( &feature.ter_furn );
1127+
ter_id resolved_ter = is_ter == nullptr ? groundcover : *is_ter;
1128+
const furn_id resolved_furn = is_furniture == nullptr ? furn_str_id::NULL_ID().id() : *is_furniture;
1129+
1130+
if( resolved_ter == ter_str_id::NULL_ID().id() ) {
1131+
resolved_ter = groundcover;
1132+
}
1133+
1134+
m->ter_set( pos, resolved_ter );
1135+
m->furn_set( pos, resolved_furn );
1136+
set_terrain_dependent_furniture( resolved_ter, pos );
11251137
}
11261138
}
11271139

src/regional_settings.cpp

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
#include <string>
99
#include <string_view>
1010
#include <utility>
11+
#include <variant>
1112

1213
#include "debug.h"
1314
#include "enum_conversions.h"
1415
#include "flexbuffer_json.h"
1516
#include "generic_factory.h"
17+
#include "mapdata.h"
1618
#include "map_extras.h"
1719
#include "options.h"
1820
#include "output.h"
@@ -22,8 +24,6 @@
2224

2325
class mapgendata;
2426

25-
ter_furn_id::ter_furn_id() : ter( ter_str_id::NULL_ID().id() ),
26-
furn( furn_str_id::NULL_ID().id() ) { }
2727

2828
template<typename T>
2929
void read_and_set_or_throw( const JsonObject &jo, const std::string &member, T &target,
@@ -894,18 +894,14 @@ void groundcover_extra::finalize() // FIXME: return bool for failure
894894

895895
for( std::map<std::string, double>::const_iterator it = percent_str.begin();
896896
it != percent_str.end(); ++it ) {
897-
tf_id.ter = ter_str_id::NULL_ID().id();
898-
tf_id.furn = furn_str_id::NULL_ID();
897+
tf_id = ter_furn_id();
899898
if( it->second < 0.0001 ) {
900899
continue;
901900
}
902-
const ter_str_id tid( it->first );
903-
const furn_str_id fid( it->first );
904-
if( tid.is_valid() ) {
905-
tf_id.ter = tid.id();
906-
} else if( fid.is_valid() ) {
907-
tf_id.furn = fid.id();
908-
} else {
901+
902+
bool resolved = tf_id.resolve( it->first );
903+
904+
if( !resolved ) {
909905
debugmsg( "No clue what '%s' is! No such terrain or furniture", it->first.c_str() );
910906
continue;
911907
}
@@ -915,19 +911,15 @@ void groundcover_extra::finalize() // FIXME: return bool for failure
915911

916912
for( std::map<std::string, double>::const_iterator it = boosted_percent_str.begin();
917913
it != boosted_percent_str.end(); ++it ) {
918-
tf_id.ter = ter_str_id::NULL_ID().id();
919-
tf_id.furn = furn_str_id::NULL_ID();
914+
tf_id = ter_furn_id();
920915
if( it->second < 0.0001 ) {
921916
continue;
922917
}
923918
const ter_str_id tid( it->first );
924919
const furn_str_id fid( it->first );
925920

926-
if( tid.is_valid() ) {
927-
tf_id.ter = tid.id();
928-
} else if( fid.is_valid() ) {
929-
tf_id.furn = fid.id();
930-
} else {
921+
bool resolved = tf_id.resolve( it->first );
922+
if( !resolved ) {
931923
debugmsg( "No clue what '%s' is! No such terrain or furniture", it->first.c_str() );
932924
continue;
933925
}
@@ -956,8 +948,7 @@ void groundcover_extra::finalize() // FIXME: return bool for failure
956948
debugmsg( "boosted plant coverage total (%s=%de-4) exceeds 100%%", ss.str(), btotal );
957949
}
958950

959-
tf_id.furn = furn_str_id::NULL_ID();
960-
tf_id.ter = default_ter;
951+
tf_id.ter_furn = default_ter;
961952
weightlist[ 1000000 ] = tf_id;
962953
boosted_weightlist[ 1000000 ] = tf_id;
963954

@@ -977,15 +968,8 @@ void forest_biome_component::finalize()
977968
{
978969
for( const std::pair<const std::string, int> &pr : unfinalized_types ) {
979970
ter_furn_id tf_id;
980-
tf_id.ter = ter_str_id::NULL_ID().id();
981-
tf_id.furn = furn_str_id::NULL_ID();
982-
const ter_str_id tid( pr.first );
983-
const furn_str_id fid( pr.first );
984-
if( tid.is_valid() ) {
985-
tf_id.ter = tid.id();
986-
} else if( fid.is_valid() ) {
987-
tf_id.furn = fid.id();
988-
} else {
971+
bool resolved = tf_id.resolve( pr.first );
972+
if( !resolved ) {
989973
continue;
990974
}
991975
types.add( tf_id, pr.second );

src/regional_settings.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ struct city_settings {
8181
void finalize();
8282
};
8383

84-
struct ter_furn_id {
85-
ter_id ter;
86-
furn_id furn;
87-
ter_furn_id();
88-
};
89-
9084
/*
9185
* template for random bushes and such.
9286
* supports occasional boost to a single ter/furn type (clustered blueberry bushes for example)

0 commit comments

Comments
 (0)