diff --git a/data/json/items/tool_armor.json b/data/json/items/tool_armor.json index cc76c9e79f770..3c093c871706b 100644 --- a/data/json/items/tool_armor.json +++ b/data/json/items/tool_armor.json @@ -835,7 +835,6 @@ "CLIMATE_CONTROL" ], "turns_per_charge": 18, - "charges_per_use": 0, "revert_to": "rm13_armor", "use_action": "RM13ARMOR_ON", "environmental_protection": 40, diff --git a/src/item_factory.cpp b/src/item_factory.cpp index 1ef8fef19f9d2..330e06294a70b 100644 --- a/src/item_factory.cpp +++ b/src/item_factory.cpp @@ -1602,7 +1602,17 @@ void Item_factory::load_armor( const JsonObject &jo, const std::string &src ) { itype def; if( load_definition( jo, src, def ) ) { - load_slot( def.armor, jo, src ); + if( def.was_loaded ) { + if( def.armor ) { + def.armor->was_loaded = true; + } else { + def.armor = cata::make_value(); + def.armor->was_loaded = true; + } + } else { + def.armor = cata::make_value(); + } + def.armor->load( jo ); load_basic_info( jo, def, src ); } } @@ -1611,42 +1621,60 @@ void Item_factory::load_pet_armor( const JsonObject &jo, const std::string &src { itype def; if( load_definition( jo, src, def ) ) { - load_slot( def.pet_armor, jo, src ); + if( def.was_loaded ) { + if( def.pet_armor ) { + def.pet_armor->was_loaded = true; + } else { + def.pet_armor = cata::make_value(); + def.pet_armor->was_loaded = true; + } + } else { + def.pet_armor = cata::make_value(); + } + def.pet_armor->load( jo ); load_basic_info( jo, def, src ); } } -void Item_factory::load( islot_armor &slot, const JsonObject &jo, const std::string &src ) +void islot_armor::load( const JsonObject &jo ) { - bool strict = src == "dda"; - - assign( jo, "encumbrance", slot.encumber, strict, 0 ); - assign( jo, "max_encumbrance", slot.max_encumber, strict, slot.encumber ); - assign( jo, "coverage", slot.coverage, strict, 0, 100 ); - assign( jo, "material_thickness", slot.thickness, strict, 0 ); - assign( jo, "environmental_protection", slot.env_resist, strict, 0 ); - assign( jo, "environmental_protection_with_filter", slot.env_resist_w_filter, strict, 0 ); - assign( jo, "warmth", slot.warmth, strict, 0 ); - assign( jo, "weight_capacity_modifier", slot.weight_capacity_modifier ); - assign( jo, "weight_capacity_bonus", slot.weight_capacity_bonus, strict, 0_gram ); - assign( jo, "power_armor", slot.power_armor, strict ); - assign( jo, "valid_mods", slot.valid_mods, strict ); + optional( jo, was_loaded, "encumbrance", encumber, 0 ); + optional( jo, was_loaded, "max_encumbrance", max_encumber, encumber ); + optional( jo, was_loaded, "coverage", coverage, 0 ); + optional( jo, was_loaded, "material_thickness", thickness, 0 ); + optional( jo, was_loaded, "environmental_protection", env_resist, 0 ); + optional( jo, was_loaded, "environmental_protection_with_filter", env_resist_w_filter, 0 ); + optional( jo, was_loaded, "warmth", warmth, 0 ); + optional( jo, was_loaded, "weight_capacity_modifier", weight_capacity_bonus, + mass_reader{}, 0_gram ); + optional( jo, was_loaded, "weight_capacity_bonus", weight_capacity_bonus, mass_reader{}, 0_gram ); + optional( jo, was_loaded, "power_armor", power_armor, false ); + optional( jo, was_loaded, "valid_mods", valid_mods ); + assign_coverage_from_json( jo, "covers", covers, sided ); +} - assign_coverage_from_json( jo, "covers", slot.covers, slot.sided ); +void islot_armor::deserialize( JsonIn &jsin ) +{ + const JsonObject jo = jsin.get_object(); + load( jo ); } -void Item_factory::load( islot_pet_armor &slot, const JsonObject &jo, const std::string &src ) +void islot_pet_armor::load( const JsonObject &jo ) { - bool strict = src == "dda"; + optional( jo, was_loaded, "material_thickness", thickness, 0 ); + optional( jo, was_loaded, "max_pet_vol", max_vol, volume_reader{}, 0_ml ); + optional( jo, was_loaded, "min_pet_vol", min_vol, volume_reader{}, 0_ml ); + optional( jo, was_loaded, "pet_bodytype", bodytype ); + optional( jo, was_loaded, "environmental_protection", env_resist, 0 ); + optional( jo, was_loaded, "environmental_protection_with_filter", env_resist_w_filter, 0 ); + optional( jo, was_loaded, "storage", storage, volume_reader{}, 0_ml ); + optional( jo, was_loaded, "power_armor", power_armor, false ); +} - assign( jo, "material_thickness", slot.thickness, strict, 0 ); - assign( jo, "max_pet_vol", slot.max_vol, strict, 0_ml ); - assign( jo, "min_pet_vol", slot.min_vol, strict, 0_ml ); - assign( jo, "pet_bodytype", slot.bodytype, strict ); - assign( jo, "environmental_protection", slot.env_resist, strict, 0 ); - assign( jo, "environmental_protection_with_filter", slot.env_resist_w_filter, strict, 0 ); - assign( jo, "storage", slot.storage, strict, 0_ml ); - assign( jo, "power_armor", slot.power_armor, strict ); +void islot_pet_armor::deserialize( JsonIn &jsin ) +{ + const JsonObject jo = jsin.get_object(); + load( jo ); } void Item_factory::load( islot_tool &slot, const JsonObject &jo, const std::string &src ) @@ -1741,35 +1769,50 @@ void Item_factory::load_tool_armor( const JsonObject &jo, const std::string &src itype def; if( load_definition( jo, src, def ) ) { load_slot( def.tool, jo, src ); - load_slot( def.armor, jo, src ); - load_basic_info( jo, def, src ); + load_armor( jo, src ); } } -void Item_factory::load( islot_book &slot, const JsonObject &jo, const std::string &src ) +void islot_book::load( const JsonObject &jo ) { - bool strict = src == "dda"; + optional( jo, was_loaded, "max_level", level, 0 ); + optional( jo, was_loaded, "required_level", req, 0 ); + optional( jo, was_loaded, "fun", fun, 0 ); + optional( jo, was_loaded, "intelligence", intel, 0 ); - assign( jo, "max_level", slot.level, strict, 0, MAX_SKILL ); - assign( jo, "required_level", slot.req, strict, 0, MAX_SKILL ); - assign( jo, "fun", slot.fun, strict ); - assign( jo, "intelligence", slot.intel, strict, 0 ); if( jo.has_int( "time" ) ) { - slot.time = jo.get_int( "time" ); + time = jo.get_int( "time" ); } else if( jo.has_string( "time" ) ) { - slot.time = to_minutes( read_from_json_string( *jo.get_raw( "time" ), - time_duration::units ) ); + time = to_minutes( read_from_json_string( *jo.get_raw( "time" ), + time_duration::units ) ); } - assign( jo, "skill", slot.skill, strict ); - assign( jo, "martial_art", slot.martial_art, strict ); - assign( jo, "chapters", slot.chapters, strict, 0 ); + + optional( jo, was_loaded, "skill", skill, skill_id::NULL_ID() ); + optional( jo, was_loaded, "martial_art", martial_art, matype_id::NULL_ID() ); + optional( jo, was_loaded, "chapters", chapters, 0 ); +} + +void islot_book::deserialize( JsonIn &jsin ) +{ + const JsonObject jo = jsin.get_object(); + load( jo ); } void Item_factory::load_book( const JsonObject &jo, const std::string &src ) { itype def; if( load_definition( jo, src, def ) ) { - load_slot( def.book, jo, src ); + if( def.was_loaded ) { + if( def.book ) { + def.book->was_loaded = true; + } else { + def.book = cata::make_value(); + def.book->was_loaded = true; + } + } else { + def.book = cata::make_value(); + } + def.book->load( jo ); load_basic_info( jo, def, src ); } } @@ -2393,9 +2436,9 @@ void Item_factory::load_basic_info( const JsonObject &jo, itype &def, const std: } } - load_slot_optional( def.armor, jo, "armor_data", src ); - load_slot_optional( def.pet_armor, jo, "pet_armor_data", src ); - load_slot_optional( def.book, jo, "book_data", src ); + assign( jo, "armor_data", def.armor, src == "dda" ); + assign( jo, "pet_armor_data", def.pet_armor, src == "dda" ); + assign( jo, "book_data", def.book, src == "dda" ); load_slot_optional( def.gun, jo, "gun_data", src ); load_slot_optional( def.bionic, jo, "bionic_data", src ); assign( jo, "ammo_data", def.ammo, src == "dda" ); diff --git a/src/item_factory.h b/src/item_factory.h index bed2d1a709f30..71799c09e1ac1 100644 --- a/src/item_factory.h +++ b/src/item_factory.h @@ -291,9 +291,6 @@ class Item_factory void load( islot_tool &slot, const JsonObject &jo, const std::string &src ); void load( islot_comestible &slot, const JsonObject &jo, const std::string &src ); void load( islot_brewable &slot, const JsonObject &jo, const std::string &src ); - void load( islot_armor &slot, const JsonObject &jo, const std::string &src ); - void load( islot_pet_armor &slot, const JsonObject &jo, const std::string &src ); - void load( islot_book &slot, const JsonObject &jo, const std::string &src ); void load( islot_mod &slot, const JsonObject &jo, const std::string &src ); void load( islot_engine &slot, const JsonObject &jo, const std::string &src ); void load( islot_wheel &slot, const JsonObject &jo, const std::string &src ); diff --git a/src/itype.h b/src/itype.h index dbf59211aada1..e509e49824620 100644 --- a/src/itype.h +++ b/src/itype.h @@ -259,6 +259,11 @@ struct islot_armor { * Restricted clothing mods must be listed here by id to be compatible. */ std::vector valid_mods; + + bool was_loaded = false; + + void load( const JsonObject &jo ); + void deserialize( JsonIn &jsin ); }; struct islot_pet_armor { @@ -294,6 +299,11 @@ struct islot_pet_armor { * Whether this is a power armor item. */ bool power_armor = false; + + bool was_loaded = false; + + void load( const JsonObject &jo ); + void deserialize( JsonIn &jsin ); }; struct islot_book { @@ -359,6 +369,11 @@ struct islot_book { }; using recipe_list_t = std::set; recipe_list_t recipes; + + bool was_loaded = false; + + void load( const JsonObject &jo ); + void deserialize( JsonIn &jsin ); }; struct islot_mod {