Skip to content

Commit fd3a4b2

Browse files
committed
vpslot_rotor and vpslot_wheel optional
optional/mandatory have better support for extended JSON features and this cleans up the loading code.
1 parent 8735733 commit fd3a4b2

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

src/veh_type.cpp

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -375,25 +375,14 @@ void vpart_info::load( const JsonObject &jo, const std::string &src )
375375
if( !wheel_info ) {
376376
wheel_info.emplace();
377377
}
378-
379-
assign( jo, "rolling_resistance", wheel_info->rolling_resistance, strict );
380-
assign( jo, "contact_area", wheel_info->contact_area, strict );
381-
assign( jo, "wheel_offroad_rating", wheel_info->offroad_rating, strict );
382-
if( const std::optional<JsonValue> jo_termod = jo.get_member_opt( "wheel_terrain_modifiers" ) ) {
383-
wheel_info->terrain_modifiers.clear();
384-
for( const JsonMember jo_mod : static_cast<JsonObject>( *jo_termod ) ) {
385-
const JsonArray jo_mod_values = jo_mod.get_array();
386-
veh_ter_mod mod { jo_mod.name(), jo_mod_values.get_int( 0 ), jo_mod_values.get_int( 1 ) };
387-
wheel_info->terrain_modifiers.emplace_back( std::move( mod ) );
388-
}
389-
}
378+
wheel_info->deserialize( jo );
390379
}
391380

392381
if( has_flag( "ROTOR" ) ) {
393382
if( !rotor_info ) {
394383
rotor_info.emplace();
395384
}
396-
assign( jo, "rotor_diameter", rotor_info->rotor_diameter, strict );
385+
rotor_info->deserialize( jo );
397386
}
398387

399388
if( has_flag( "WORKBENCH" ) ) {
@@ -445,6 +434,38 @@ void vpslot_engine::deserialize( const JsonObject &jo )
445434
was_loaded = true;
446435
}
447436

437+
void veh_ter_mod::deserialize( const JsonValue &jv )
438+
{
439+
if( !jv.is_member() ) {
440+
jv.throw_error( "Invalid format" );
441+
}
442+
443+
const JsonMember &jm = dynamic_cast<const JsonMember &>( jv );
444+
JsonArray ja = jm.get_array();
445+
446+
terrain_flag = jm.name();
447+
move_override = ja.get_int( 0 );
448+
move_penalty = ja.get_int( 1 );
449+
}
450+
451+
void vpslot_wheel::deserialize( const JsonObject &jo )
452+
{
453+
optional( jo, was_loaded, "rolling_resistance", rolling_resistance, 1.f );
454+
optional( jo, was_loaded, "contact_area", contact_area, 1 );
455+
optional( jo, was_loaded, "wheel_offroad_rating", offroad_rating, 0.5f );
456+
optional( jo, was_loaded, "wheel_terrain_modifiers", terrain_modifiers,
457+
json_read_reader<veh_ter_mod> {} );
458+
459+
was_loaded = true;
460+
}
461+
462+
void vpslot_rotor::deserialize( const JsonObject &jo )
463+
{
464+
optional( jo, was_loaded, "rotor_diameter", rotor_diameter, 1 );
465+
466+
was_loaded = true;
467+
}
468+
448469
void vpart_info::set_flag( const std::string &flag )
449470
{
450471
flags.insert( flag );

src/veh_type.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
class Character;
2828
class JsonObject;
2929
class JsonOut;
30+
class JsonValue;
3031
class vehicle;
3132
class vpart_info;
3233
struct vehicle_prototype;
@@ -140,17 +141,31 @@ struct veh_ter_mod {
140141
std::string terrain_flag; // terrain flag this mod block applies to
141142
int move_override; // override when on flagged terrain, ignored if 0
142143
int move_penalty; // penalty added when not on flagged terrain, ignored if 0
144+
145+
void deserialize( const JsonValue &jv );
146+
// for generic_factory delete/extend
147+
bool operator==( const veh_ter_mod &rhs ) const {
148+
return terrain_flag == rhs.terrain_flag;
149+
}
143150
};
144151

145152
struct vpslot_wheel {
153+
bool was_loaded = false;
154+
146155
float rolling_resistance = 1.0f;
147156
int contact_area = 1;
148157
std::vector<veh_ter_mod> terrain_modifiers;
149158
float offroad_rating = 0.5f;
159+
160+
void deserialize( const JsonObject &jo );
150161
};
151162

152163
struct vpslot_rotor {
164+
bool was_loaded = false;
165+
153166
int rotor_diameter = 1;
167+
168+
void deserialize( const JsonObject &jo );
154169
};
155170

156171
struct vpslot_workbench {

0 commit comments

Comments
 (0)