/** *Caches a vehicle definition from a JsonObject to be loaded after itypes is initialized. */ void vehicle_prototype::load(JsonObject &jo) { vehicle_prototype &vproto = vtypes[ vproto_id( jo.get_string( "id" ) ) ]; // If there are already parts defined, this vehicle prototype overrides an existing one. // If the json contains a name, it means a completely new prototype (replacing the // original one), therefor the old data has to be cleared. // If the json does not contain a name (the prototype would have no name), it means appending // to the existing prototype (the parts are not cleared). if( !vproto.parts.empty() && jo.has_string( "name" ) ) { vproto = std::move( vehicle_prototype() ); } if( vproto.parts.empty() ) { vproto.name = jo.get_string( "name" ); } vgroups[vgroup_id(jo.get_string("id"))].add_vehicle(vproto_id(jo.get_string("id")), 100); JsonArray parts = jo.get_array("parts"); while (parts.has_more()) { JsonObject part = parts.next_object(); const point pxy( part.get_int("x"), part.get_int("y") ); const vpart_str_id pid( part.get_string( "part" ) ); vproto.parts.emplace_back( pxy, pid ); } JsonArray items = jo.get_array("items"); while(items.has_more()) { JsonObject spawn_info = items.next_object(); vehicle_item_spawn next_spawn; next_spawn.pos.x = spawn_info.get_int("x"); next_spawn.pos.y = spawn_info.get_int("y"); next_spawn.chance = spawn_info.get_int("chance"); if(next_spawn.chance <= 0 || next_spawn.chance > 100) { debugmsg("Invalid spawn chance in %s (%d, %d): %d%%", vproto.name.c_str(), next_spawn.pos.x, next_spawn.pos.y, next_spawn.chance); } if(spawn_info.has_array("items")) { //Array of items that all spawn together (ie jack+tire) JsonArray item_group = spawn_info.get_array("items"); while(item_group.has_more()) { next_spawn.item_ids.push_back(item_group.next_string()); } } else if(spawn_info.has_string("items")) { //Treat single item as array next_spawn.item_ids.push_back(spawn_info.get_string("items")); } if(spawn_info.has_array("item_groups")) { //Pick from a group of items, just like map::place_items JsonArray item_group_names = spawn_info.get_array("item_groups"); while(item_group_names.has_more()) { next_spawn.item_groups.push_back(item_group_names.next_string()); } } else if(spawn_info.has_string("item_groups")) { next_spawn.item_groups.push_back(spawn_info.get_string("item_groups")); } vproto.item_spawns.push_back( std::move( next_spawn ) ); } }
/** *Caches a vehicle definition from a JsonObject to be loaded after itypes is initialized. */ void vehicle_prototype::load( JsonObject &jo ) { vehicle_prototype &vproto = vtypes[ vproto_id( jo.get_string( "id" ) ) ]; // If there are already parts defined, this vehicle prototype overrides an existing one. // If the json contains a name, it means a completely new prototype (replacing the // original one), therefore the old data has to be cleared. // If the json does not contain a name (the prototype would have no name), it means appending // to the existing prototype (the parts are not cleared). if( !vproto.parts.empty() && jo.has_string( "name" ) ) { vproto = vehicle_prototype(); } if( vproto.parts.empty() ) { vproto.name = jo.get_string( "name" ); } vgroups[vgroup_id( jo.get_string( "id" ) )].add_vehicle( vproto_id( jo.get_string( "id" ) ), 100 ); const auto add_part_obj = [&]( JsonObject part, point pos ) { part_def pt; pt.pos = pos; pt.part = vpart_id( part.get_string( "part" ) ); assign( part, "ammo", pt.with_ammo, true, 0, 100 ); assign( part, "ammo_types", pt.ammo_types, true ); assign( part, "ammo_qty", pt.ammo_qty, true, 0 ); assign( part, "fuel", pt.fuel, true ); vproto.parts.push_back( pt ); }; const auto add_part_string = [&]( std::string part, point pos ) { part_def pt; pt.pos = pos; pt.part = vpart_id( part ); vproto.parts.push_back( pt ); }; JsonArray parts = jo.get_array( "parts" ); while( parts.has_more() ) { JsonObject part = parts.next_object(); point pos = point( part.get_int( "x" ), part.get_int( "y" ) ); if( part.has_string( "part" ) ) { add_part_obj( part, pos ); } else if( part.has_array( "parts" ) ) { JsonArray subparts = part.get_array( "parts" ); while( subparts.has_more() ) { if( subparts.test_string() ) { std::string part_name = subparts.next_string(); add_part_string( part_name, pos ); } else { JsonObject subpart = subparts.next_object(); add_part_obj( subpart, pos ); } } } } JsonArray items = jo.get_array( "items" ); while( items.has_more() ) { JsonObject spawn_info = items.next_object(); vehicle_item_spawn next_spawn; next_spawn.pos.x = spawn_info.get_int( "x" ); next_spawn.pos.y = spawn_info.get_int( "y" ); next_spawn.chance = spawn_info.get_int( "chance" ); if( next_spawn.chance <= 0 || next_spawn.chance > 100 ) { debugmsg( "Invalid spawn chance in %s (%d, %d): %d%%", vproto.name.c_str(), next_spawn.pos.x, next_spawn.pos.y, next_spawn.chance ); } // constrain both with_magazine and with_ammo to [0-100] next_spawn.with_magazine = std::max( std::min( spawn_info.get_int( "magazine", next_spawn.with_magazine ), 100 ), 0 ); next_spawn.with_ammo = std::max( std::min( spawn_info.get_int( "ammo", next_spawn.with_ammo ), 100 ), 0 ); if( spawn_info.has_array( "items" ) ) { //Array of items that all spawn together (i.e. jack+tire) JsonArray item_group = spawn_info.get_array( "items" ); while( item_group.has_more() ) { next_spawn.item_ids.push_back( item_group.next_string() ); } } else if( spawn_info.has_string( "items" ) ) { //Treat single item as array next_spawn.item_ids.push_back( spawn_info.get_string( "items" ) ); } if( spawn_info.has_array( "item_groups" ) ) { //Pick from a group of items, just like map::place_items JsonArray item_group_names = spawn_info.get_array( "item_groups" ); while( item_group_names.has_more() ) { next_spawn.item_groups.push_back( item_group_names.next_string() ); } } else if( spawn_info.has_string( "item_groups" ) ) { next_spawn.item_groups.push_back( spawn_info.get_string( "item_groups" ) ); } vproto.item_spawns.push_back( std::move( next_spawn ) ); } }