Skip to content

Commit ea5635a

Browse files
committed
vp_control_req optional/mandatory
optional/mandatory have better support for extended JSON features, and this cleans up the loading code.
1 parent 737c759 commit ea5635a

File tree

2 files changed

+8
-23
lines changed

2 files changed

+8
-23
lines changed

src/veh_type.cpp

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "wcwidth.h"
5151

5252
class emit;
53+
class proficiency;
5354

5455
namespace
5556
{
@@ -204,28 +205,10 @@ static void parse_vp_reqs( const JsonObject &obj, const vpart_id &id, const std:
204205
}
205206
}
206207

207-
static void parse_vp_control_reqs( const JsonObject &obj, const vpart_id &id,
208-
std::string_view key,
209-
vp_control_req &req )
208+
void vp_control_req::deserialize( const JsonObject &jo )
210209
{
211-
if( !obj.has_object( key ) ) {
212-
return;
213-
}
214-
JsonObject src = obj.get_object( key );
215-
216-
JsonArray sk = src.get_array( "skills" );
217-
if( !sk.empty() ) {
218-
req.skills.clear();
219-
for( JsonArray cur : sk ) {
220-
if( cur.size() != 2 ) {
221-
debugmsg( "vpart '%s' has requirement with invalid skill entry", id.str() );
222-
continue;
223-
}
224-
req.skills.emplace( skill_id( cur.get_string( 0 ) ), cur.get_int( 1 ) );
225-
}
226-
}
227-
228-
optional( src, false, "proficiencies", req.proficiencies );
210+
optional( jo, false, "skills", skills, weighted_string_id_reader<skill_id, int> { std::nullopt} );
211+
optional( jo, false, "proficiencies", proficiencies, string_id_reader<proficiency> {} );
229212
}
230213

231214
void vehicles::parts::load( const JsonObject &jo, const std::string &src )
@@ -327,8 +310,8 @@ void vpart_info::load( const JsonObject &jo, const std::string &src )
327310
if( jo.has_member( "control_requirements" ) ) {
328311
JsonObject reqs = jo.get_object( "control_requirements" );
329312

330-
parse_vp_control_reqs( reqs, id, "air", control_air );
331-
parse_vp_control_reqs( reqs, id, "land", control_land );
313+
optional( reqs, false, "air", control_air );
314+
optional( reqs, false, "land", control_land );
332315
}
333316

334317
optional( jo, was_loaded, "looks_like", looks_like, looks_like );

src/veh_type.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ struct vpslot_terrain_transform {
201201
struct vp_control_req {
202202
std::set<std::pair<skill_id, int>> skills;
203203
std::set<proficiency_id> proficiencies;
204+
205+
void deserialize( const JsonObject &jo );
204206
};
205207

206208
class vpart_category

0 commit comments

Comments
 (0)