From 0b22039dbdc1401c62024a4a84875eb854b2b328 Mon Sep 17 00:00:00 2001 From: KorGgenT Date: Fri, 3 Jan 2020 22:17:34 -0500 Subject: [PATCH] make new pocket_data class --- src/item_contents.cpp | 19 +++------- src/item_contents.h | 11 +++--- src/item_pocket.cpp | 82 +++++++++++++++++++------------------------ src/item_pocket.h | 22 +++++++++--- 4 files changed, 64 insertions(+), 70 deletions(-) diff --git a/src/item_contents.cpp b/src/item_contents.cpp index a46b53a3533db..a7f57756e80dc 100644 --- a/src/item_contents.cpp +++ b/src/item_contents.cpp @@ -9,20 +9,15 @@ namespace fake_item { -static item_pocket none_pocket( item_pocket::pocket_type::LAST ); +static item_pocket none_pocket( nullptr ); +static pocket_data legacy_pocket_data; +static item_pocket legacy_pocket( &legacy_pocket_data ); } // namespace fake_item -void item_contents::load( const JsonObject &jo ) -{ - optional( jo, was_loaded, "nestable", nestable, true ); - mandatory( jo, was_loaded, "contents", contents ); -} - void item_contents::serialize( JsonOut &json ) const { json.start_object(); - json.member( "nestable", nestable ); json.member( "contents", contents ); json.end_object(); @@ -31,7 +26,7 @@ void item_contents::serialize( JsonOut &json ) const void item_contents::deserialize( JsonIn &jsin ) { JsonObject data = jsin.get_object(); - load( data ); + optional( data, was_loaded, "contents", contents ); } void item_contents::add_legacy_pocket() @@ -42,7 +37,7 @@ void item_contents::add_legacy_pocket() return; } } - contents.emplace_front( item_pocket( item_pocket::pocket_type::LEGACY_CONTAINER ) ); + contents.emplace_front( fake_item::legacy_pocket ); } bool item_contents::stacks_with( const item_contents &rhs ) const @@ -368,10 +363,6 @@ static void insert_separation_line( std::vector &info ) void item_contents::info( std::vector &info ) const { - if( !nestable ) { - info.emplace_back( "DESCRIPTION", - _( "* This item cannot be placed into other containers." ) ); - } int pocket_number = 1; std::vector contents_info; std::vector found_pockets; diff --git a/src/item_contents.h b/src/item_contents.h index 647102519bbaf..e7ac9f7ffe57a 100644 --- a/src/item_contents.h +++ b/src/item_contents.h @@ -24,8 +24,12 @@ class item_contents add_legacy_pocket(); } - bool is_nestable() const { - return nestable; + // used for loading itype + item_contents( const std::vector &pockets ) { + for( const pocket_data &data : pockets ) { + contents.push_back( item_pocket( &data ) ); + } + add_legacy_pocket(); } // for usage with loading to aid migration @@ -88,7 +92,6 @@ class item_contents void info( std::vector &info ) const; - void load( const JsonObject &jo ); void serialize( JsonOut &json ) const; void deserialize( JsonIn &jsin ); @@ -97,8 +100,6 @@ class item_contents // gets the pocket described as legacy, or creates one item_pocket &legacy_pocket(); const item_pocket &legacy_pocket() const; - // container can be placed into other containers - bool nestable = true; std::list contents; }; diff --git a/src/item_pocket.cpp b/src/item_pocket.cpp index 9a600442567c3..d456ddd0f7371 100644 --- a/src/item_pocket.cpp +++ b/src/item_pocket.cpp @@ -31,9 +31,9 @@ std::string enum_to_string( item_pocket::pocket_type d // *INDENT-ON* } // namespace io -void item_pocket::load( const JsonObject &jo ) +void pocket_data::load( const JsonObject &jo ) { - optional( jo, was_loaded, "pocket_type", type, CONTAINER ); + optional( jo, was_loaded, "pocket_type", type, item_pocket::pocket_type::CONTAINER ); optional( jo, was_loaded, "min_item_volume", min_item_volume, volume_reader(), 0_ml ); mandatory( jo, was_loaded, "max_contains_volume", max_contains_volume, volume_reader() ); mandatory( jo, was_loaded, "max_contains_weight", max_contains_weight, mass_reader() ); @@ -52,26 +52,17 @@ void item_pocket::serialize( JsonOut &json ) const { json.start_object(); - json.member( "pocket_type", type ); - json.member( "min_item_volume", min_item_volume ); - json.member( "max_contains_volume", max_contains_volume ); - json.member( "max_contains_weight", max_contains_weight ); - json.member( "spoil_multiplier", spoil_multiplier ); - json.member( "weight_multiplier", weight_multiplier ); - json.member( "moves", moves ); - json.member( "fire_protection", fire_protection ); - json.member( "watertight", watertight ); - json.member( "gastight", gastight ); - json.member( "open_container", open_container ); - json.member( "flag_restriction", flag_restriction ); - json.member( "rigid", rigid ); - json.member( "contents", contents ); json.end_object(); } bool item_pocket::operator==( const item_pocket &rhs ) const +{ + return *data == *rhs.data; +} + +bool pocket_data::operator==( const pocket_data &rhs ) const { return rigid == rhs.rigid && watertight == rhs.watertight && @@ -101,7 +92,6 @@ bool item_pocket::stacks_with( const item_pocket &rhs ) const void item_pocket::deserialize( JsonIn &jsin ) { JsonObject data = jsin.get_object(); - load( data ); optional( data, was_loaded, "contents", contents ); } @@ -137,17 +127,17 @@ size_t item_pocket::size() const units::volume item_pocket::volume_capacity() const { - return max_contains_volume; + return data->max_contains_volume; } units::volume item_pocket::remaining_volume() const { - return max_contains_volume - contains_volume(); + return data->max_contains_volume - contains_volume(); } units::volume item_pocket::item_size_modifier() const { - if( rigid ) { + if( data->rigid ) { return 0_ml; } units::volume total_vol = 0_ml; @@ -161,7 +151,7 @@ units::mass item_pocket::item_weight_modifier() const { units::mass total_mass = 0_gram; for( const item &it : contents ) { - total_mass += it.weight() * weight_multiplier; + total_mass += it.weight() * data->weight_multiplier; } return total_mass; } @@ -206,7 +196,7 @@ bool item_pocket::use_amount( const itype_id &it, int &quantity, std::list bool item_pocket::will_explode_in_a_fire() const { - if( fire_protection ) { + if( data->fire_protection ) { return false; } return std::any_of( contents.begin(), contents.end(), []( const item & it ) { @@ -316,51 +306,51 @@ void item_pocket::general_info( std::vector &info, int pocket_number, if( disp_pocket_number ) { info.emplace_back( "DESCRIPTION", _( string_format( "Pocket %d:", pocket_number ) ) ); } - if( rigid ) { + if( data->rigid ) { info.emplace_back( "DESCRIPTION", _( "This pocket is rigid." ) ); } - if( min_item_volume > 0_ml ) { + if( data->min_item_volume > 0_ml ) { info.emplace_back( "DESCRIPTION", _( string_format( "Minimum volume of item allowed: %s", - vol_to_string( min_item_volume ) ) ) ); + vol_to_string( data->min_item_volume ) ) ) ); } info.emplace_back( "DESCRIPTION", _( string_format( "Volume Capacity: %s", - vol_to_string( max_contains_volume ) ) ) ); + vol_to_string( data->max_contains_volume ) ) ) ); info.emplace_back( "DESCRIPTION", _( string_format( "Weight Capacity: %s", - weight_to_string( max_contains_weight ) ) ) ); + weight_to_string( data->max_contains_weight ) ) ) ); info.emplace_back( "DESCRIPTION", _( string_format( "This pocket takes %d base moves to take an item out.", - moves ) ) ); + data->moves ) ) ); - if( watertight ) { + if( data->watertight ) { info.emplace_back( "DESCRIPTION", _( "This pocket can contain a liquid." ) ); } - if( gastight ) { + if( data->gastight ) { info.emplace_back( "DESCRIPTION", _( "This pocket can contain a gas." ) ); } - if( open_container ) { + if( data->open_container ) { info.emplace_back( "DESCRIPTION", _( "This pocket will spill if placed into another item or worn." ) ); } - if( fire_protection ) { + if( data->fire_protection ) { info.emplace_back( "DESCRIPTION", _( "This pocket protects its contents from fire." ) ); } - if( spoil_multiplier != 1.0f ) { + if( data->spoil_multiplier != 1.0f ) { info.emplace_back( "DESCRIPTION", string_format( _( "This pocket makes contained items spoil at %.0f%% their original rate." ), - spoil_multiplier * 100 ) ); + data->spoil_multiplier * 100 ) ); } - if( weight_multiplier != 1.0f ) { + if( data->weight_multiplier != 1.0f ) { info.emplace_back( "DESCRIPTION", string_format( _( "Items in this pocket weigh %.0f%% their original weight." ), - weight_multiplier * 100 ) ); + data->weight_multiplier * 100 ) ); } } } @@ -380,10 +370,10 @@ void item_pocket::contents_info( std::vector &info, int pocket_number, } info.emplace_back( "DESCRIPTION", _( string_format( "Volume: %s / %s", - vol_to_string( contains_volume() ), vol_to_string( max_contains_volume ) ) ) ); + vol_to_string( contains_volume() ), vol_to_string( data->max_contains_volume ) ) ) ); info.emplace_back( "DESCRIPTION", _( string_format( "Weight: %s / %s", - weight_to_string( contains_weight() ), weight_to_string( max_contains_weight ) ) ) ); + weight_to_string( contains_weight() ), weight_to_string( data->max_contains_weight ) ) ) ); bool contents_header = false; for( const item &contents_item : contents ) { @@ -425,19 +415,19 @@ ret_val item_pocket::can_contain( const item &it ) co if( type == pocket_type::LEGACY_CONTAINER ) { return ret_val::make_failure( contain_code::ERR_LEGACY_CONTAINER ); } - if( it.made_of( phase_id::LIQUID ) && !watertight ) { + if( it.made_of( phase_id::LIQUID ) && !data->watertight ) { return ret_val::make_failure( contain_code::ERR_LIQUID, _( "can't contain liquid" ) ); } - if( it.made_of( phase_id::GAS ) && !gastight ) { + if( it.made_of( phase_id::GAS ) && !data->gastight ) { return ret_val::make_failure( contain_code::ERR_GAS, _( "can't contain gas" ) ); } - if( it.volume() < min_item_volume ) { + if( it.volume() < data->min_item_volume ) { return ret_val::make_failure( contain_code::ERR_TOO_SMALL, _( "item is too small" ) ); } - if( it.weight() > max_contains_weight ) { + if( it.weight() > data->max_contains_weight ) { return ret_val::make_failure( contain_code::ERR_TOO_HEAVY, _( "item is too heavy" ) ); } @@ -445,11 +435,11 @@ ret_val item_pocket::can_contain( const item &it ) co return ret_val::make_failure( contain_code::ERR_CANNOT_SUPPORT, _( "pocket is holding too much weight" ) ); } - if( !flag_restriction.empty() && !it.has_any_flag( flag_restriction ) ) { + if( !data->flag_restriction.empty() && !it.has_any_flag( data->flag_restriction ) ) { return ret_val::make_failure( contain_code::ERR_FLAG, _( "item does not have correct flag" ) ); } - if( it.volume() > max_contains_volume ) { + if( it.volume() > data->max_contains_volume ) { return ret_val::make_failure( contain_code::ERR_TOO_BIG, _( "item too big" ) ); } @@ -558,7 +548,7 @@ ret_val item_pocket::insert_item( const item &it ) int item_pocket::obtain_cost( const item &it ) const { if( has_item( it ) ) { - return moves; + return data->moves; } return 0; } @@ -588,5 +578,5 @@ units::mass item_pocket::contains_weight() const units::mass item_pocket::remaining_weight() const { - return max_contains_weight - contains_weight(); + return data->max_contains_weight - contains_weight(); } diff --git a/src/item_pocket.h b/src/item_pocket.h index afb4002ed9a06..dd558bd20952b 100644 --- a/src/item_pocket.h +++ b/src/item_pocket.h @@ -17,6 +17,7 @@ class Character; class item; class item_location; class player; +class pocket_data; struct iteminfo; struct itype; @@ -58,7 +59,7 @@ class item_pocket }; item_pocket() = default; - item_pocket( pocket_type ptype ) : type( ptype ) {} + item_pocket( const pocket_data *data ) : data( data ) {} bool stacks_with( const item_pocket &rhs ) const; @@ -123,7 +124,6 @@ class item_pocket void general_info( std::vector &info, int pocket_number, bool disp_pocket_number ) const; void contents_info( std::vector &info, int pocket_number, bool disp_pocket_number ) const; - void load( const JsonObject &jo ); void serialize( JsonOut &json ) const; void deserialize( JsonIn &jsin ); @@ -131,7 +131,17 @@ class item_pocket bool was_loaded; private: - pocket_type type = CONTAINER; + const pocket_data *data = nullptr; + // the items inside the pocket + std::list contents; +}; + +class pocket_data +{ + public: + bool was_loaded; + + item_pocket::pocket_type type = item_pocket::pocket_type::LEGACY_CONTAINER; // max volume of stuff the pocket can hold units::volume max_contains_volume = 0_ml; // min volume of item that can be contained, otherwise it spills @@ -157,8 +167,10 @@ class item_pocket std::vector flag_restriction; // container's size and encumbrance does not change based on contents. bool rigid = false; - // the items inside the pocket - std::list contents; + + bool operator==( const pocket_data &rhs ) const; + + void load( const JsonObject &jo ); }; template<>