@@ -1327,14 +1327,38 @@ struct veh_proto_part_def_reader : generic_typed_reader<veh_proto_part_def_reade
1327
1327
1328
1328
};
1329
1329
1330
- void vehicle_prototype::load ( const JsonObject &jo, std::string_view )
1330
+ struct veh_spawn_item_reader : generic_typed_reader<veh_spawn_item_reader> {
1331
+ std::pair<itype_id, std::string> get_next ( const JsonValue &jv ) const {
1332
+ if ( jv.test_string () ) {
1333
+ return std::make_pair ( itype_id ( jv.get_string () ), " " );
1334
+ }
1335
+ JsonObject jo = jv.get_object ();
1336
+ std::pair<itype_id, std::string> ret;
1337
+ mandatory ( jo, false , " id" , ret.first );
1338
+ optional ( jo, false , " variant" , ret.second );
1339
+ return ret;
1340
+ }
1341
+ };
1342
+
1343
+ void vehicle_item_spawn::deserialize ( const JsonObject &jo )
1331
1344
{
1332
- vgroups[ vgroup_id ( id. str () )]. add_vehicle ( id, 100 );
1333
- optional ( jo, was_loaded , " name " , name );
1345
+ mandatory ( jo, false , " x " , pos. x () );
1346
+ mandatory ( jo, false , " y " , pos. y () );
1334
1347
1348
+ mandatory ( jo, false , " chance" , chance, numeric_bound_reader{ 0 , 100 } );
1335
1349
1350
+ // constrain both with_magazine and with_ammo to [0-100]
1351
+ optional ( jo, false , " magazine" , with_magazine, numeric_bound_reader{ 0 , 100 }, 0 );
1352
+ optional ( jo, false , " ammo" , with_ammo, numeric_bound_reader{ 0 , 100 }, 0 );
1336
1353
1354
+ optional ( jo, false , " items" , item_ids, veh_spawn_item_reader{} );
1355
+ optional ( jo, false , " item_groups" , item_groups, string_id_reader<Item_spawn_data> {} );
1356
+ }
1337
1357
1358
+ void vehicle_prototype::load ( const JsonObject &jo, std::string_view )
1359
+ {
1360
+ vgroups[vgroup_id ( id.str () )].add_vehicle ( id, 100 );
1361
+ optional ( jo, was_loaded, " name" , name );
1338
1362
1339
1363
if ( jo.has_member ( " blueprint" ) ) {
1340
1364
// currently unused, read to suppress unvisited members warning
@@ -1351,46 +1375,7 @@ void vehicle_prototype::load( const JsonObject &jo, std::string_view )
1351
1375
parts.insert ( parts.end (), tmp.begin (), tmp.end () );
1352
1376
}
1353
1377
1354
- for ( JsonObject spawn_info : jo.get_array ( " items" ) ) {
1355
- vehicle_item_spawn next_spawn;
1356
- next_spawn.pos .x () = spawn_info.get_int ( " x" );
1357
- next_spawn.pos .y () = spawn_info.get_int ( " y" );
1358
-
1359
- next_spawn.chance = spawn_info.get_int ( " chance" );
1360
- if ( next_spawn.chance <= 0 || next_spawn.chance > 100 ) {
1361
- debugmsg ( " Invalid spawn chance in %s (%d, %d): %d%%" ,
1362
- name, next_spawn.pos .x (), next_spawn.pos .y (), next_spawn.chance );
1363
- }
1364
-
1365
- // constrain both with_magazine and with_ammo to [0-100]
1366
- next_spawn.with_magazine = std::max ( std::min ( spawn_info.get_int ( " magazine" ,
1367
- next_spawn.with_magazine ), 100 ), 0 );
1368
- next_spawn.with_ammo = std::max ( std::min ( spawn_info.get_int ( " ammo" ,
1369
- next_spawn.with_ammo ), 100 ), 0 );
1370
-
1371
- if ( spawn_info.has_array ( " items" ) ) {
1372
- // Array of items that all spawn together (i.e. jack+tire)
1373
- spawn_info.read ( " items" , next_spawn.item_ids , true );
1374
- } else if ( spawn_info.has_string ( " items" ) ) {
1375
- // Treat single item as array
1376
- // And read the gun variant (if it exists)
1377
- if ( spawn_info.has_string ( " variant" ) ) {
1378
- const std::string variant = spawn_info.get_string ( " variant" );
1379
- next_spawn.variant_ids .emplace_back ( itype_id ( spawn_info.get_string ( " items" ) ), variant );
1380
- } else {
1381
- next_spawn.item_ids .emplace_back ( spawn_info.get_string ( " items" ) );
1382
- }
1383
- }
1384
- if ( spawn_info.has_array ( " item_groups" ) ) {
1385
- // Pick from a group of items, just like map::place_items
1386
- for ( const std::string line : spawn_info.get_array ( " item_groups" ) ) {
1387
- next_spawn.item_groups .emplace_back ( line );
1388
- }
1389
- } else if ( spawn_info.has_string ( " item_groups" ) ) {
1390
- next_spawn.item_groups .emplace_back ( spawn_info.get_string ( " item_groups" ) );
1391
- }
1392
- item_spawns.push_back ( std::move ( next_spawn ) );
1393
- }
1378
+ optional ( jo, was_loaded, " items" , item_spawns );
1394
1379
1395
1380
for ( JsonObject jzi : jo.get_array ( " zones" ) ) {
1396
1381
zone_type_id zone_type ( jzi.get_member ( " type" ).get_string () );
@@ -1687,8 +1672,8 @@ void vehicles::finalize_prototypes()
1687
1672
proto.name , i.pos .x (), i.pos .y (), i.chance );
1688
1673
}
1689
1674
for ( auto &j : i.item_ids ) {
1690
- if ( !item::type_is_defined ( j ) ) {
1691
- debugmsg ( " unknown item %s in spawn list of %s" , j.c_str (), proto.id .str () );
1675
+ if ( !item::type_is_defined ( j. first ) ) {
1676
+ debugmsg ( " unknown item %s in spawn list of %s" , j.first . c_str (), proto.id .str () );
1692
1677
}
1693
1678
}
1694
1679
for ( auto &j : i.item_groups ) {
0 commit comments