Skip to content

Commit 8735733

Browse files
committed
vpslot_engine to optional/mandatory
optional/mandatory have better support for extended JSON features. It looks like all the vpslots would be better served as having an "slot": { ... } field, instead of the SLOT flag enabling more fields in the base part. extend/delete could provide the necessary extension features. But that's a much larger overhaul. The was_loaded per slot is necessary for it to correctly initialize it - it cannot use the same as the vpart_info, because even if a vpart_info was loaded, the engine slot may not have been.
1 parent a298e82 commit 8735733

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/veh_type.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -363,18 +363,12 @@ void vpart_info::load( const JsonObject &jo, const std::string &src )
363363
damage_reduction = load_damage_map( dred );
364364
}
365365

366+
// TODO?: move this into an object?
366367
if( has_flag( "ENGINE" ) ) {
367368
if( !engine_info ) {
368369
engine_info.emplace();
369370
}
370-
assign( jo, "backfire_threshold", engine_info->backfire_threshold, strict );
371-
assign( jo, "backfire_freq", engine_info->backfire_freq, strict );
372-
assign( jo, "noise_factor", engine_info->noise_factor, strict );
373-
assign( jo, "damaged_power_factor", engine_info->damaged_power_factor, strict );
374-
assign( jo, "m2c", engine_info->m2c, strict );
375-
assign( jo, "muscle_power_factor", engine_info->muscle_power_factor, strict );
376-
assign( jo, "exclusions", engine_info->exclusions, strict );
377-
assign( jo, "fuel_options", engine_info->fuel_opts, strict );
371+
engine_info->deserialize( jo );
378372
}
379373

380374
if( has_flag( "WHEEL" ) ) {
@@ -437,6 +431,20 @@ void vpart_info::load( const JsonObject &jo, const std::string &src )
437431
}
438432
}
439433

434+
void vpslot_engine::deserialize( const JsonObject &jo )
435+
{
436+
optional( jo, was_loaded, "backfire_threshold", backfire_threshold, 0.f );
437+
optional( jo, was_loaded, "backfire_freq", backfire_freq, 1 );
438+
optional( jo, was_loaded, "noise_factor", noise_factor, 0 );
439+
optional( jo, was_loaded, "damaged_power_factor", damaged_power_factor, 0.f );
440+
optional( jo, was_loaded, "m2c", m2c, 100 );
441+
optional( jo, was_loaded, "muscle_power_factor", muscle_power_factor, 0 );
442+
optional( jo, was_loaded, "exclusions", exclusions, string_reader{} );
443+
optional( jo, was_loaded, "fuel_options", fuel_opts, string_id_reader<itype> {} );
444+
445+
was_loaded = true;
446+
}
447+
440448
void vpart_info::set_flag( const std::string &flag )
441449
{
442450
flags.insert( flag );

src/veh_type.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ enum vpart_bitflags : int {
122122
};
123123

124124
struct vpslot_engine {
125+
bool was_loaded = false;
126+
125127
float backfire_threshold = 0.0f;
126128
int backfire_freq = 1;
127129
int muscle_power_factor = 0;
@@ -130,6 +132,8 @@ struct vpslot_engine {
130132
int m2c = 100;
131133
std::vector<std::string> exclusions;
132134
std::vector<itype_id> fuel_opts;
135+
136+
void deserialize( const JsonObject &jo );
133137
};
134138

135139
struct veh_ter_mod {

0 commit comments

Comments
 (0)