From d3fe69cc43ba45839c5a6971550cc7571306075a Mon Sep 17 00:00:00 2001 From: Davi Date: Tue, 10 Dec 2019 05:57:43 -0500 Subject: [PATCH] Add serialization for consumption history So that monotony penalties carry over upon save/load. --- src/character.h | 11 ++++++++--- src/savegame_json.cpp | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/character.h b/src/character.h index a5d436b457736..64f16bd158a2a 100644 --- a/src/character.h +++ b/src/character.h @@ -182,13 +182,15 @@ struct social_modifiers { struct consumption_event { time_point time; itype_id type_id; - uint64_t component_hash = 0; + uint64_t component_hash; - consumption_event( const item &food ) { - time = calendar::turn; + consumption_event() = default; + consumption_event( const item &food ) : time( calendar::turn ) { type_id = food.typeId(); component_hash = food.make_component_hash(); } + void serialize( JsonOut &json ) const; + void deserialize( JsonIn &jsin ); }; inline social_modifiers operator+( social_modifiers lhs, const social_modifiers &rhs ) @@ -681,6 +683,9 @@ class Character : public Creature, public visitable const item &new_item ) const; std::array, num_bp> mut_drench; + + void serialize_consumption_history( JsonOut jsout ) const; + void deserialize_consumption_history( JsonArray jarr ); public: // recalculates enchantment cache by iterating through all held, worn, and wielded items void recalculate_enchantment_cache(); diff --git a/src/savegame_json.cpp b/src/savegame_json.cpp index 3f84d235addc4..1e1eccab26641 100644 --- a/src/savegame_json.cpp +++ b/src/savegame_json.cpp @@ -390,6 +390,23 @@ void Character::trait_data::deserialize( JsonIn &jsin ) data.read( "powered", powered ); } +void consumption_event::serialize( JsonOut &json ) const +{ + json.start_object(); + json.member( "time", time ); + json.member( "type_id", type_id ); + json.member( "component_hash", component_hash ); + json.end_object(); +} + +void consumption_event::deserialize( JsonIn &jsin ) +{ + JsonObject jo = jsin.get_object(); + jo.read( "time", time ); + jo.read( "type_id", type_id ); + jo.read( "component_hash", component_hash ); +} + /** * Gather variables for saving. These variables are common to both the avatar and NPCs. */ @@ -453,6 +470,7 @@ void Character::load( const JsonObject &data ) lvl = std::max( std::min( lvl, v.first.obj().max() ), v.first.obj().min() ); vitamin_levels[v.first] = lvl; } + data.read( "consumption_history", consumption_history ); data.read( "activity", activity ); data.read( "destination_activity", destination_activity ); data.read( "stashed_outbounds_activity", stashed_outbounds_activity ); @@ -693,6 +711,7 @@ void Character::store( JsonOut &json ) const json.member( "vitamin_levels", vitamin_levels ); json.member( "pkill", pkill ); json.member( "omt_path", omt_path ); + json.member( "consumption_history", consumption_history ); // crafting etc json.member( "destination_activity", destination_activity );