Skip to content

Commit

Permalink
refactor Item_factory::load(islot_ammo) to islot_ammo::load (#39899)
Browse files Browse the repository at this point in the history
  • Loading branch information
KorGgenT authored Apr 28, 2020
1 parent a3a3deb commit f9b3841
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
5 changes: 0 additions & 5 deletions data/mods/Magiclysm/items/fuel.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
55 changes: 36 additions & 19 deletions src/item_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -1462,33 +1465,47 @@ 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 )
{
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<islot_ammo>();
def.ammo->was_loaded = true;
}
} else {
def.ammo = cata::make_value<islot_ammo>();
}
def.ammo->load( jo );
load_basic_info( jo, def, src );
}
}
Expand Down Expand Up @@ -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 );
Expand Down
1 change: 0 additions & 1 deletion src/item_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
8 changes: 8 additions & 0 deletions src/itype.h
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,11 @@ struct islot_ammo : common_ranged_data {
* Set this to make it show as combat ammo anyway
*/
cata::optional<bool> force_stat_display;

bool was_loaded;

void load( const JsonObject &jo );
void deserialize( JsonIn &jsin );
};

struct islot_bionic {
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit f9b3841

Please sign in to comment.