Skip to content

Commit 16d3aaa

Browse files
committed
vehicle prototype parts to optional/mandatory
1 parent a5e5a13 commit 16d3aaa

File tree

3 files changed

+43
-45
lines changed

3 files changed

+43
-45
lines changed

src/veh_type.cpp

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,59 +1297,58 @@ static std::pair<std::string, std::string> get_vpart_str_variant( const std::str
12971297
: std::make_pair( vpid.substr( 0, loc ), vpid.substr( loc + 1 ) );
12981298
}
12991299

1300+
struct veh_proto_part_def_reader : generic_typed_reader<veh_proto_part_def_reader> {
1301+
point_rel_ms pos;
1302+
1303+
explicit veh_proto_part_def_reader( const point_rel_ms &p ) : pos( p ) {}
1304+
1305+
vehicle_prototype::part_def get_next( const JsonValue &jv ) const {
1306+
vehicle_prototype::part_def ret;
1307+
if( jv.test_string() ) {
1308+
const auto [id, variant] = get_vpart_str_variant( jv.get_string() );
1309+
ret.part = vpart_id( id );
1310+
ret.variant = variant;
1311+
ret.pos = pos;
1312+
return ret;
1313+
}
1314+
JsonObject jo = jv.get_object();
1315+
const auto [id, variant] = get_vpart_str_variant( jo.get_string( "part" ) );
1316+
ret.part = vpart_id( id );
1317+
ret.variant = variant;
1318+
ret.pos = pos;
1319+
1320+
optional( jo, false, "ammo", ret.with_ammo, numeric_bound_reader{0, 100}, 0 );
1321+
optional( jo, false, "ammo_types", ret.ammo_types, string_id_reader<itype> {} );
1322+
optional( jo, false, "ammo_qty", ret.ammo_qty, { -1, -1 } );
1323+
optional( jo, false, "fuel", ret.fuel, itype_id::NULL_ID() );
1324+
optional( jo, false, "tools", ret.tools, string_id_reader<itype> {} );
1325+
return ret;
1326+
}
1327+
1328+
};
1329+
13001330
void vehicle_prototype::load( const JsonObject &jo, std::string_view )
13011331
{
13021332
vgroups[vgroup_id( id.str() )].add_vehicle( id, 100 );
13031333
optional( jo, was_loaded, "name", name );
13041334

1305-
const auto add_part_obj = [&]( const JsonObject & part, point_rel_ms pos ) {
1306-
const auto [id, variant] = get_vpart_str_variant( part.get_string( "part" ) );
1307-
part_def pt;
1308-
pt.part = vpart_id( id );
1309-
pt.variant = variant;
1310-
pt.pos = pos;
13111335

1312-
assign( part, "ammo", pt.with_ammo, true, 0, 100 );
1313-
assign( part, "ammo_types", pt.ammo_types, true );
1314-
assign( part, "ammo_qty", pt.ammo_qty, true, 0 );
1315-
assign( part, "fuel", pt.fuel, true );
1316-
assign( part, "tools", pt.tools, true );
13171336

1318-
parts.emplace_back( pt );
1319-
};
13201337

1321-
const auto add_part_string = [&]( const std::string & part, point_rel_ms pos ) {
1322-
const auto [id, variant] = get_vpart_str_variant( part );
1323-
part_def pt;
1324-
pt.part = vpart_id( id );
1325-
pt.variant = variant;
1326-
pt.pos = pos;
1327-
parts.emplace_back( pt );
1328-
};
13291338

13301339
if( jo.has_member( "blueprint" ) ) {
13311340
// currently unused, read to suppress unvisited members warning
13321341
jo.get_array( "blueprint" );
13331342
}
13341343

1344+
std::vector<part_def> tmp;
13351345
for( JsonObject part : jo.get_array( "parts" ) ) {
1336-
point_rel_ms pos{ part.get_int( "x" ), part.get_int( "y" ) };
1337-
1338-
if( part.has_string( "part" ) ) {
1339-
add_part_obj( part, pos );
1340-
debugmsg( "vehicle prototype '%s' uses deprecated string definition for part"
1341-
" '%s', use 'parts' array instead", id.str(), part.get_string( "part" ) );
1342-
} else if( part.has_array( "parts" ) ) {
1343-
for( const JsonValue entry : part.get_array( "parts" ) ) {
1344-
if( entry.test_string() ) {
1345-
std::string part_name = entry.get_string();
1346-
add_part_string( part_name, pos );
1347-
} else {
1348-
JsonObject subpart = entry.get_object();
1349-
add_part_obj( subpart, pos );
1350-
}
1351-
}
1352-
}
1346+
point_rel_ms pos;
1347+
mandatory( part, false, "x", pos.x() );
1348+
mandatory( part, false, "y", pos.y() );
1349+
mandatory( part, false, "parts", tmp, veh_proto_part_def_reader{ pos } );
1350+
// gross
1351+
parts.insert( parts.end(), tmp.begin(), tmp.end() );
13531352
}
13541353

13551354
for( JsonObject spawn_info : jo.get_array( "items" ) ) {

src/veh_type.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,12 @@ struct vehicle_prototype {
527527
std::pair<int, int> ammo_qty = { -1, -1 };
528528
itype_id fuel = itype_id::NULL_ID();
529529
std::vector<itype_id> tools;
530+
531+
bool operator==( const part_def &other ) const {
532+
return pos == other.pos && part == other.part && variant == other.variant &&
533+
with_ammo == other.with_ammo && ammo_types == other.ammo_types && ammo_qty == other.ammo_qty &&
534+
fuel == other.fuel && tools == other.tools;
535+
}
530536
};
531537

532538
struct zone_def {

tests/vehicle_export_test.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ static bool operator==( const vehicle_prototype::zone_def &l, const vehicle_prot
2525
return l.filter == r.filter && l.name == r.name && l.pt == r.pt && l.zone_type == r.zone_type;
2626
}
2727

28-
static bool operator==( const vehicle_prototype::part_def &l, const vehicle_prototype::part_def &r )
29-
{
30-
return l.ammo_qty == r.ammo_qty && l.part == r.part && l.variant == r.variant &&
31-
l.with_ammo == r.with_ammo && l.ammo_types == r.ammo_types && l.ammo_qty == r.ammo_qty &&
32-
l.fuel == r.fuel && l.tools == r.tools;
33-
}
34-
3528
static bool operator==( const vehicle_item_spawn &l, const vehicle_item_spawn &r )
3629
{
3730
return l.pos == r.pos && l.chance == r.chance &&

0 commit comments

Comments
 (0)