From f9b384181215e3981cde65a271d7b094e33c9b5f Mon Sep 17 00:00:00 2001 From: Curtis Merrill Date: Tue, 28 Apr 2020 11:55:49 -0400 Subject: [PATCH] refactor Item_factory::load(islot_ammo) to islot_ammo::load (#39899) --- data/mods/Magiclysm/items/fuel.json | 5 --- src/item_factory.cpp | 55 +++++++++++++++++++---------- src/item_factory.h | 1 - src/itype.h | 8 +++++ 4 files changed, 44 insertions(+), 25 deletions(-) diff --git a/data/mods/Magiclysm/items/fuel.json b/data/mods/Magiclysm/items/fuel.json index b062ac2050b6c..c1d63ecec580e 100644 --- a/data/mods/Magiclysm/items/fuel.json +++ b/data/mods/Magiclysm/items/fuel.json @@ -57,11 +57,6 @@ "ammo_type": "blood", "fuel": { "energy": 1 } }, - { - "type": "AMMO", - "id": "mana_infused_blood", - "name": "mana infused blood" - }, { "id": "mana_infused_blood", "type": "COMESTIBLE", diff --git a/src/item_factory.cpp b/src/item_factory.cpp index e3d99768022c9..4fe5765ef3827 100644 --- a/src/item_factory.cpp +++ b/src/item_factory.cpp @@ -26,6 +26,7 @@ #include "explosion.h" #include "flat_set.h" #include "game_constants.h" +#include "generic_factory.h" #include "init.h" #include "item.h" #include "item_contents.h" @@ -1428,6 +1429,7 @@ bool Item_factory::load_definition( const JsonObject &jo, const std::string &src if( base != m_templates.end() ) { def = base->second; def.looks_like = jo.get_string( "copy-from" ); + def.was_loaded = true; return true; } @@ -1437,6 +1439,7 @@ bool Item_factory::load_definition( const JsonObject &jo, const std::string &src if( def.looks_like.empty() ) { def.looks_like = jo.get_string( "copy-from" ); } + def.was_loaded = true; return true; } @@ -1462,25 +1465,29 @@ void Item_factory::load( islot_artifact &slot, const JsonObject &jo, const std:: load_optional_enum_array( slot.effects_worn, jo, "effects_worn" ); } -void Item_factory::load( islot_ammo &slot, const JsonObject &jo, const std::string &src ) +void islot_ammo::load( const JsonObject &jo ) { - bool strict = src == "dda"; - - assign( jo, "ammo_type", slot.type, strict ); - assign( jo, "casing", slot.casing, strict ); - assign( jo, "drop", slot.drop, strict ); - assign( jo, "drop_chance", slot.drop_chance, strict, 0.0f, 1.0f ); - assign( jo, "drop_active", slot.drop_active, strict ); + mandatory( jo, was_loaded, "ammo_type", type ); + optional( jo, was_loaded, "casing", casing, cata::nullopt ); + optional( jo, was_loaded, "drop", drop, "null" ); + optional( jo, was_loaded, "drop_chance", drop_chance, 1.0f ); + optional( jo, was_loaded, "drop_active", drop_active, true ); // Damage instance assign reader handles pierce and prop_damage - assign( jo, "damage", slot.damage, strict ); - assign( jo, "range", slot.range, strict, 0 ); - assign( jo, "dispersion", slot.dispersion, strict, 0 ); - assign( jo, "recoil", slot.recoil, strict, 0 ); - assign( jo, "count", slot.def_charges, strict, 1 ); - assign( jo, "loudness", slot.loudness, strict, 0 ); - assign( jo, "effects", slot.ammo_effects, strict ); - assign( jo, "critical_multiplier", slot.critical_multiplier, strict ); - assign( jo, "show_stats", slot.force_stat_display, strict ); + assign( jo, "damage", damage ); + optional( jo, was_loaded, "range", range, 0 ); + optional( jo, was_loaded, "dispersion", dispersion, 0 ); + optional( jo, was_loaded, "recoil", recoil, 0 ); + optional( jo, was_loaded, "count", def_charges, 1 ); + optional( jo, was_loaded, "loudness", loudness, -1 ); + optional( jo, was_loaded, "effects", ammo_effects ); + optional( jo, was_loaded, "critical_multiplier", critical_multiplier, 2.0 ); + optional( jo, was_loaded, "show_stats", force_stat_display, cata::nullopt ); +} + +void islot_ammo::deserialize( JsonIn &jsin ) +{ + const JsonObject jo = jsin.get_object(); + load( jo ); } void Item_factory::load_ammo( const JsonObject &jo, const std::string &src ) @@ -1488,7 +1495,17 @@ void Item_factory::load_ammo( const JsonObject &jo, const std::string &src ) itype def; if( load_definition( jo, src, def ) ) { assign( jo, "stack_size", def.stack_size, src == "dda", 1 ); - load_slot( def.ammo, jo, src ); + if( def.was_loaded ) { + if( def.ammo ) { + def.ammo->was_loaded = true; + } else { + def.ammo = cata::make_value(); + def.ammo->was_loaded = true; + } + } else { + def.ammo = cata::make_value(); + } + def.ammo->load( jo ); load_basic_info( jo, def, src ); } } @@ -2385,7 +2402,7 @@ void Item_factory::load_basic_info( const JsonObject &jo, itype &def, const std: load_slot_optional( def.book, jo, "book_data", src ); load_slot_optional( def.gun, jo, "gun_data", src ); load_slot_optional( def.bionic, jo, "bionic_data", src ); - load_slot_optional( def.ammo, jo, "ammo_data", src ); + assign( jo, "ammo_data", def.ammo, src == "dda" ); load_slot_optional( def.seed, jo, "seed_data", src ); load_slot_optional( def.artifact, jo, "artifact_data", src ); load_slot_optional( def.brewable, jo, "brewable", src ); diff --git a/src/item_factory.h b/src/item_factory.h index 2f9cf31473e07..7a8d0298cfbd9 100644 --- a/src/item_factory.h +++ b/src/item_factory.h @@ -295,7 +295,6 @@ class Item_factory void load( islot_magazine &slot, const JsonObject &jo, const std::string &src ); void load( islot_battery &slot, const JsonObject &jo, const std::string &src ); void load( islot_bionic &slot, const JsonObject &jo, const std::string &src ); - void load( islot_ammo &slot, const JsonObject &jo, const std::string &src ); void load( islot_seed &slot, const JsonObject &jo, const std::string &src ); void load( islot_artifact &slot, const JsonObject &jo, const std::string &src ); void load( relic &slot, const JsonObject &jo, const std::string &src ); diff --git a/src/itype.h b/src/itype.h index e6fc7c9fff798..5fbfbde682ba2 100644 --- a/src/itype.h +++ b/src/itype.h @@ -720,6 +720,11 @@ struct islot_ammo : common_ranged_data { * Set this to make it show as combat ammo anyway */ cata::optional force_stat_display; + + bool was_loaded; + + void load( const JsonObject &jo ); + void deserialize( JsonIn &jsin ); }; struct islot_bionic { @@ -861,6 +866,9 @@ struct itype { return count_by_charges() ? 0 : damage_max_; } + // used for generic_factory for copy-from + bool was_loaded = false; + // a hint for tilesets: if it doesn't have a tile, what does it look like? std::string looks_like;