Skip to content

Commit

Permalink
make new pocket_data class
Browse files Browse the repository at this point in the history
  • Loading branch information
KorGgenT committed Jan 4, 2020
1 parent e5050f6 commit 0b22039
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 70 deletions.
19 changes: 5 additions & 14 deletions src/item_contents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -368,10 +363,6 @@ static void insert_separation_line( std::vector<iteminfo> &info )

void item_contents::info( std::vector<iteminfo> &info ) const
{
if( !nestable ) {
info.emplace_back( "DESCRIPTION",
_( "* This item <bad>cannot be placed into other containers</bad>." ) );
}
int pocket_number = 1;
std::vector<iteminfo> contents_info;
std::vector<item_pocket> found_pockets;
Expand Down
11 changes: 6 additions & 5 deletions src/item_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<pocket_data> &pockets ) {
for( const pocket_data &data : pockets ) {
contents.push_back( item_pocket( &data ) );
}
add_legacy_pocket();
}

// for usage with loading to aid migration
Expand Down Expand Up @@ -88,7 +92,6 @@ class item_contents

void info( std::vector<iteminfo> &info ) const;

void load( const JsonObject &jo );
void serialize( JsonOut &json ) const;
void deserialize( JsonIn &jsin );

Expand All @@ -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<item_pocket> contents;
};
Expand Down
82 changes: 36 additions & 46 deletions src/item_pocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ std::string enum_to_string<item_pocket::pocket_type>( 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() );
Expand All @@ -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 &&
Expand Down Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -206,7 +196,7 @@ bool item_pocket::use_amount( const itype_id &it, int &quantity, std::list<item>

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 ) {
Expand Down Expand Up @@ -316,51 +306,51 @@ void item_pocket::general_info( std::vector<iteminfo> &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 <info>rigid</info>." ) );
}
if( min_item_volume > 0_ml ) {
if( data->min_item_volume > 0_ml ) {
info.emplace_back( "DESCRIPTION",
_( string_format( "Minimum volume of item allowed: <neutral>%s</neutral>",
vol_to_string( min_item_volume ) ) ) );
vol_to_string( data->min_item_volume ) ) ) );
}
info.emplace_back( "DESCRIPTION",
_( string_format( "Volume Capacity: <neutral>%s</neutral>",
vol_to_string( max_contains_volume ) ) ) );
vol_to_string( data->max_contains_volume ) ) ) );
info.emplace_back( "DESCRIPTION",
_( string_format( "Weight Capacity: <neutral>%s</neutral>",
weight_to_string( max_contains_weight ) ) ) );
weight_to_string( data->max_contains_weight ) ) ) );

info.emplace_back( "DESCRIPTION",
_( string_format( "This pocket takes <neutral>%d</neutral> base moves to take an item out.",
moves ) ) );
data->moves ) ) );

if( watertight ) {
if( data->watertight ) {
info.emplace_back( "DESCRIPTION",
_( "This pocket can <info>contain a liquid</info>." ) );
}
if( gastight ) {
if( data->gastight ) {
info.emplace_back( "DESCRIPTION",
_( "This pocket can <info>contain a gas</info>." ) );
}
if( open_container ) {
if( data->open_container ) {
info.emplace_back( "DESCRIPTION",
_( "This pocket will <bad>spill</bad> if placed into another item or worn." ) );
}
if( fire_protection ) {
if( data->fire_protection ) {
info.emplace_back( "DESCRIPTION",
_( "This pocket <info>protects its contents from fire</info>." ) );
}
if( spoil_multiplier != 1.0f ) {
if( data->spoil_multiplier != 1.0f ) {
info.emplace_back( "DESCRIPTION",
string_format(
_( "This pocket makes contained items spoil at <neutral>%.0f%%</neutral> 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 <neutral>%.0f%%</neutral> their original weight." ),
weight_multiplier * 100 ) );
data->weight_multiplier * 100 ) );
}
}
}
Expand All @@ -380,10 +370,10 @@ void item_pocket::contents_info( std::vector<iteminfo> &info, int pocket_number,
}
info.emplace_back( "DESCRIPTION",
_( string_format( "Volume: <neutral>%s / %s</neutral>",
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: <neutral>%s / %s</neutral>",
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 ) {
Expand Down Expand Up @@ -425,31 +415,31 @@ ret_val<item_pocket::contain_code> item_pocket::can_contain( const item &it ) co
if( type == pocket_type::LEGACY_CONTAINER ) {
return ret_val<item_pocket::contain_code>::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<item_pocket::contain_code>::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<item_pocket::contain_code>::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<item_pocket::contain_code>::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<item_pocket::contain_code>::make_failure(
contain_code::ERR_TOO_HEAVY, _( "item is too heavy" ) );
}
if( it.weight() > remaining_weight() ) {
return ret_val<item_pocket::contain_code>::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<item_pocket::contain_code>::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<item_pocket::contain_code>::make_failure(
contain_code::ERR_TOO_BIG, _( "item too big" ) );
}
Expand Down Expand Up @@ -558,7 +548,7 @@ ret_val<item_pocket::contain_code> 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;
}
Expand Down Expand Up @@ -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();
}
22 changes: 17 additions & 5 deletions src/item_pocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Character;
class item;
class item_location;
class player;
class pocket_data;

struct iteminfo;
struct itype;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -123,15 +124,24 @@ class item_pocket
void general_info( std::vector<iteminfo> &info, int pocket_number, bool disp_pocket_number ) const;
void contents_info( std::vector<iteminfo> &info, int pocket_number, bool disp_pocket_number ) const;

void load( const JsonObject &jo );
void serialize( JsonOut &json ) const;
void deserialize( JsonIn &jsin );

bool operator==( const item_pocket &rhs ) const;

bool was_loaded;
private:
pocket_type type = CONTAINER;
const pocket_data *data = nullptr;
// the items inside the pocket
std::list<item> 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
Expand All @@ -157,8 +167,10 @@ class item_pocket
std::vector<std::string> flag_restriction;
// container's size and encumbrance does not change based on contents.
bool rigid = false;
// the items inside the pocket
std::list<item> contents;

bool operator==( const pocket_data &rhs ) const;

void load( const JsonObject &jo );
};

template<>
Expand Down

0 comments on commit 0b22039

Please sign in to comment.