Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Item contents refactor (holster refactor 1 of 2) #36656

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
dd8510e
class definitions for item_pocket and item_contents
KorGgenT Dec 29, 2019
4e18770
clarify comments
KorGgenT Jan 4, 2020
bc61ae9
fix type checking
KorGgenT Jan 4, 2020
c33a085
enum class
KorGgenT Jan 4, 2020
ad982ce
Update item_pocket.cpp
KorGgenT Jan 4, 2020
d3c25d3
remove some string_format
KorGgenT Jan 4, 2020
c08e24e
migrate item::contents to new item_contents object
KorGgenT Dec 29, 2019
f57b589
some code review changes
KorGgenT Jan 4, 2020
cde77ca
use data.read and remove was_loaded
KorGgenT Jan 4, 2020
95eedbb
modify helper functions for pocket iteminfo
KorGgenT Jan 4, 2020
12d9fec
change pointer to reference
KorGgenT Jan 4, 2020
b8d6386
remove unecessary string_format
KorGgenT Jan 4, 2020
130f89b
actually use helper function
KorGgenT Jan 4, 2020
e74043d
move _to_info() functions to same file as iteminfo
KorGgenT Jan 4, 2020
523a6a7
modify translated strings
KorGgenT Jan 4, 2020
70b4049
fix compile errors
KorGgenT Jan 5, 2020
1494d49
fix loading and all_items_ptr
KorGgenT Jan 5, 2020
c451ae5
move gunmod weight calculateion to item pocket
KorGgenT Jan 5, 2020
f19866a
fix toolmods()
KorGgenT Jan 5, 2020
eccb0c1
fix setting ammo of none
KorGgenT Jan 6, 2020
27b998d
move contents loading to beginning of constructor
KorGgenT Jan 7, 2020
ba8bdc7
remove "this" capture
KorGgenT Jan 7, 2020
8a03924
simplify bool return statement
KorGgenT Jan 7, 2020
2fe4301
change remove_all_mods to actually remove all mods
KorGgenT Jan 8, 2020
6ebe3e2
Update item_pocket.cpp
KorGgenT Jan 8, 2020
e3dffbb
save pocket type
KorGgenT Jan 11, 2020
ffd9033
fix rebase
KorGgenT Jan 21, 2020
15397d2
fix rebase
KorGgenT Jan 24, 2020
ef45a96
all_items sanity check
KorGgenT Jan 25, 2020
b411ca5
fix holster draw
KorGgenT Jan 25, 2020
942d287
fix rebase
KorGgenT Jan 25, 2020
8982fc1
astyle
KorGgenT Jan 25, 2020
babd03a
fix loading holsters
KorGgenT Jan 27, 2020
7c82e00
Update iuse_actor.cpp
KorGgenT Jan 30, 2020
b4246b6
Update item.cpp
KorGgenT Feb 1, 2020
913d715
Update iuse_actor.cpp
KorGgenT Feb 1, 2020
2e8966e
Update item.cpp
KorGgenT Feb 1, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ void activity_handlers::butcher_finish( player_activity *act, player *p )
}

item &corpse_item = *target;
std::list<item> contents = corpse_item.contents;
const std::list<item> contents = corpse_item.contents.all_items();
const mtype *corpse = corpse_item.get_mtype();
const field_type_id type_blood = corpse->bloodType();
const field_type_id type_gib = corpse->gibType();
Expand Down Expand Up @@ -1120,7 +1120,7 @@ void activity_handlers::butcher_finish( player_activity *act, player *p )
// therefore operations on this activities targets and values may be invalidated.
// reveal hidden items / hidden content
if( action != F_DRESS && action != SKIN ) {
for( auto &content : contents ) {
for( const item &content : contents ) {
if( ( roll_butchery() + 10 ) * 5 > rng( 0, 100 ) ) {
//~ %1$s - item name, %2$s - monster name
p->add_msg_if_player( m_good, _( "You discover a %1$s in the %2$s!" ), content.tname(),
Expand Down Expand Up @@ -2624,7 +2624,7 @@ void activity_handlers::gunmod_add_finish( player_activity *act, player *p )
if( rng( 0, 100 ) <= roll ) {
add_msg( m_good, _( "You successfully attached the %1$s to your %2$s." ), mod.tname(),
gun.tname() );
gun.contents.push_back( p->i_rem( &mod ) );
gun.contents.insert_legacy( p->i_rem( &mod ) );

} else if( rng( 0, 100 ) <= risk ) {
if( gun.inc_damage() ) {
Expand Down Expand Up @@ -2659,7 +2659,7 @@ void activity_handlers::toolmod_add_finish( player_activity *act, player *p )
p->add_msg_if_player( m_good, _( "You successfully attached the %1$s to your %2$s." ),
mod.tname(), tool.tname() );
mod.item_tags.insert( "IRREMOVABLE" );
tool.contents.push_back( mod );
tool.contents.insert_legacy( mod );
act->targets[1].remove_item();
}

Expand Down Expand Up @@ -3935,14 +3935,13 @@ void activity_handlers::unload_mag_finish( player_activity *act, player *p )
item &it = *act->targets[ 0 ];

// remove the ammo leads in the belt
it.contents.erase( std::remove_if( it.contents.begin(),
it.contents.end(), [&]( item & e ) {
it.contents.remove_items_if( [&]( item & e ) {
if( !p->add_or_drop_with_msg( e, true ) ) {
return false;
}
qty += e.charges;
return true;
} ), it.contents.end() );
} );

// remove the belt linkage
if( it.is_ammo_belt() ) {
Expand Down Expand Up @@ -4552,6 +4551,6 @@ void activity_handlers::mind_splicer_finish( player_activity *act, player *p )
item &data_card = *act->targets[0];
p->add_msg_if_player( m_info, _( "…you finally find the memory banks." ) );
p->add_msg_if_player( m_info, _( "The kit makes a copy of the data inside the bionic." ) );
data_card.contents.clear();
data_card.contents.clear_items();
data_card.put_in( item( "mind_scan_robofac" ) );
}
6 changes: 3 additions & 3 deletions src/advanced_inv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,7 @@ bool advanced_inventory::move_content( item &src_container, item &dest_container
return false;
}

item &src_contents = src_container.contents.front();
item &src_contents = src_container.contents.legacy_front();

if( !src_contents.made_of( LIQUID ) ) {
popup( _( "You can unload only liquids into target container." ) );
Expand All @@ -1617,9 +1617,9 @@ bool advanced_inventory::move_content( item &src_container, item &dest_container
}
dest_container.fill_with( src_contents, amount );

uistate.adv_inv_container_content_type = dest_container.contents.front().typeId();
uistate.adv_inv_container_content_type = dest_container.contents.legacy_front().typeId();
if( src_contents.charges <= 0 ) {
src_container.contents.clear();
src_container.contents.clear_items();
}

return true;
Expand Down
4 changes: 2 additions & 2 deletions src/advanced_inv_area.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ void advanced_inv_area::set_container( const advanced_inv_listitem *advitem )
uistate.adv_inv_container_index = advitem->idx;
uistate.adv_inv_container_type = it->typeId();
uistate.adv_inv_container_content_type = !it->is_container_empty() ?
it->contents.front().typeId() : "null";
it->contents.legacy_front().typeId() : "null";
set_container_position();
} else {
uistate.adv_inv_container_location = -1;
Expand All @@ -354,7 +354,7 @@ bool advanced_inv_area::is_container_valid( const item *it ) const
return true;
}
} else {
if( it->contents.front().typeId() == uistate.adv_inv_container_content_type ) {
if( it->contents.legacy_front().typeId() == uistate.adv_inv_container_content_type ) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/advanced_inv_pane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void advanced_inventory_pane::add_items_from_area( advanced_inv_area &square,
if( cont != nullptr ) {
if( !cont->is_container_empty() ) {
// filtering does not make sense for liquid in container
item *it = &square.get_container( in_vehicle() )->contents.front();
item *it = &square.get_container( in_vehicle() )->contents.legacy_front();
advanced_inv_listitem ait( it, 0, 1, square.id, in_vehicle() );
square.volume += ait.volume;
square.weight += ait.weight;
Expand Down
2 changes: 1 addition & 1 deletion src/avatar_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ void avatar_action::eat( avatar &you, item_location loc )

} else if( you.consume_item( *it ) ) {
if( it->is_food_container() || !you.can_consume_as_is( *it ) ) {
it->contents.erase( it->contents.begin() );
it->contents.remove_item( it->contents.legacy_front() );
add_msg( _( "You leave the empty %s." ), it->tname() );
} else {
loc.remove_item();
Expand Down
4 changes: 2 additions & 2 deletions src/ballistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static void drop_or_embed_projectile( const dealt_projectile_attack &attack )
add_msg( _( "The %s shatters!" ), drop_item.tname() );
}

for( const item &i : drop_item.contents ) {
for( const item &i : drop_item.contents.all_items() ) {
g->m.add_item_or_charges( pt, i );
}
// TODO: Non-glass breaking
Expand All @@ -68,7 +68,7 @@ static void drop_or_embed_projectile( const dealt_projectile_attack &attack )
add_msg( _( "The %s bursts!" ), drop_item.tname() );
}

for( const item &i : drop_item.contents ) {
for( const item &i : drop_item.contents.all_items() ) {
g->m.add_item_or_charges( pt, i );
}

Expand Down
16 changes: 16 additions & 0 deletions src/cata_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ double convert_weight( const units::mass &weight )
return ret;
}

std::string weight_to_string( const units::mass &weight )
{
const double converted_weight = convert_weight( weight );
return string_format( "%.2f %s", converted_weight, weight_units() );
}

double convert_volume( int volume )
{
return convert_volume( volume, nullptr );
Expand All @@ -281,6 +287,16 @@ double convert_volume( int volume, int *out_scale )
return ret;
}

std::string vol_to_string( const units::volume &vol )
{
int converted_volume_scale = 0;
const double converted_volume =
convert_volume( vol.value(),
&converted_volume_scale );

return string_format( "%.3f %s", converted_volume, volume_units_abbr() );
}

double temp_to_celsius( double fahrenheit )
{
return ( ( fahrenheit - 32.0 ) * 5.0 / 9.0 );
Expand Down
6 changes: 6 additions & 0 deletions src/cata_utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ double convert_velocity( int velocity, units_type vel_units );
*/
double convert_weight( const units::mass &weight );

// convert a mass unit to a string readable by a human
std::string weight_to_string( const units::mass &weight );

/**
* Convert volume from ml to units defined by user.
*/
Expand All @@ -243,6 +246,9 @@ double convert_volume( int volume );
*/
double convert_volume( int volume, int *out_scale );

// convert a volume unit to a string readable by a human
std::string vol_to_string( const units::volume &vol );

/**
* Convert a temperature from degrees Fahrenheit to degrees Celsius.
*
Expand Down
16 changes: 9 additions & 7 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1523,7 +1523,7 @@ int Character::i_add_to_container( const item &it, const bool unloading )

const itype_id item_type = it.typeId();
auto add_to_container = [&it, &charges]( item & container ) {
auto &contained_ammo = container.contents.front();
item &contained_ammo = container.contents.legacy_front();
if( contained_ammo.charges < container.ammo_capacity() ) {
const int diff = container.ammo_capacity() - contained_ammo.charges;
//~ %1$s: item name, %2$s: container name
Expand All @@ -1540,7 +1540,8 @@ int Character::i_add_to_container( const item &it, const bool unloading )
};

visit_items( [ & ]( item * item ) {
if( charges > 0 && item->is_ammo_container() && item_type == item->contents.front().typeId() ) {
if( charges > 0 && item->is_ammo_container() &&
item_type == item->contents.legacy_front().typeId() ) {
charges = add_to_container( *item );
item->handle_pickup_ownership( *this );
}
Expand Down Expand Up @@ -1662,7 +1663,7 @@ item Character::i_rem( const item *it )

void Character::i_rem_keep_contents( const int pos )
{
for( auto &content : i_rem( pos ).contents ) {
for( item &content : i_rem( pos ).contents.all_items() ) {
i_add_or_drop( content );
}
}
Expand Down Expand Up @@ -1792,7 +1793,7 @@ void find_ammo_helper( T &src, const item &obj, bool empty, Output out, bool nes
{
if( obj.is_watertight_container() ) {
if( !obj.is_container_empty() ) {
auto contents_id = obj.contents.front().typeId();
itype_id contents_id = obj.contents.legacy_front().typeId();

// Look for containers with the same type of liquid as that already in our container
src.visit_items( [&src, &nested, &out, &contents_id, &obj]( item * node ) {
Expand All @@ -1806,7 +1807,7 @@ void find_ammo_helper( T &src, const item &obj, bool empty, Output out, bool nes
}

if( node->is_container() && !node->is_container_empty() &&
node->contents.front().typeId() == contents_id ) {
node->contents.legacy_front().typeId() == contents_id ) {
out = item_location( src, node );
}
return nested ? VisitResponse::NEXT : VisitResponse::SKIP;
Expand Down Expand Up @@ -1842,7 +1843,7 @@ void find_ammo_helper( T &src, const item &obj, bool empty, Output out, bool nes
if( node->is_ammo_container() && !node->contents.empty() &&
!node->contents_made_of( SOLID ) ) {
for( const ammotype &at : ammo ) {
if( node->contents.front().ammo_type() == at ) {
if( node->contents.legacy_front().ammo_type() == at ) {
out = item_location( src, node );
}
}
Expand Down Expand Up @@ -7355,7 +7356,8 @@ void Character::absorb_hit( body_part bp, damage_instance &dam )
destroyed_armor_msg( *this, pre_damage_name );
armor_destroyed = true;
armor.on_takeoff( *this );
worn_remains.insert( worn_remains.end(), armor.contents.begin(), armor.contents.end() );
std::list<item> armor_contents = armor.contents.all_items();
worn_remains.insert( worn_remains.end(), armor_contents.begin(), armor_contents.end() );
// decltype is the type name of the iterator, note that reverse_iterator::base returns the
// iterator to the next element, not the one the revers_iterator points to.
// http://stackoverflow.com/questions/1830158/how-to-call-erase-with-a-reverse-iterator
Expand Down
14 changes: 7 additions & 7 deletions src/computer_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ void computer_session::action_sample()
capa = std::min( sewage.charges, capa );
if( elem.contents.empty() ) {
elem.put_in( sewage );
elem.contents.front().charges = capa;
elem.contents.legacy_front().charges = capa;
} else {
elem.contents.front().charges += capa;
elem.contents.legacy_front().charges += capa;
}
found_item = true;
break;
Expand Down Expand Up @@ -690,7 +690,7 @@ void computer_session::action_download_software()
g->u.moves -= 30;
item software( miss->get_item_id(), 0 );
software.mission_id = comp.mission_id;
usb->contents.clear();
usb->contents.clear_items();
usb->put_in( software );
print_line( _( "Software downloaded." ) );
} else {
Expand All @@ -711,10 +711,10 @@ void computer_session::action_blood_anal()
print_error( _( "ERROR: Please remove all but one sample from centrifuge." ) );
} else if( items.only_item().contents.empty() ) {
print_error( _( "ERROR: Please only use container with blood sample." ) );
} else if( items.only_item().contents.front().typeId() != "blood" ) {
} else if( items.only_item().contents.legacy_front().typeId() != "blood" ) {
print_error( _( "ERROR: Please only use blood samples." ) );
} else { // Success!
const item &blood = items.only_item().contents.front();
const item &blood = items.only_item().contents.legacy_front();
const mtype *mt = blood.get_mtype();
if( mt == nullptr || mt->id == mtype_id::NULL_ID() ) {
print_line( _( "Result: Human blood, no pathogens found." ) );
Expand All @@ -728,7 +728,7 @@ void computer_session::action_blood_anal()
if( query_bool( _( "Download data?" ) ) ) {
if( item *const usb = pick_usb() ) {
item software( "software_blood_data", 0 );
usb->contents.clear();
usb->contents.clear_items();
usb->put_in( software );
print_line( _( "Software downloaded." ) );
} else {
Expand Down Expand Up @@ -1315,7 +1315,7 @@ void computer_session::failure_destroy_blood()
print_error( _( "ERROR: Please use blood-contained samples." ) );
} else if( items.only_item().contents.empty() ) {
print_error( _( "ERROR: Blood draw kit, empty." ) );
} else if( items.only_item().contents.front().typeId() != "blood" ) {
} else if( items.only_item().contents.legacy_front().typeId() != "blood" ) {
print_error( _( "ERROR: Please only use blood samples." ) );
} else {
print_error( _( "ERROR: Blood sample destroyed." ) );
Expand Down
10 changes: 5 additions & 5 deletions src/consumption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ std::pair<nutrients, nutrients> Character::compute_nutrient_range(
nutrients this_max;

item result_it = rec->create_result();
if( result_it.contents.size() == 1 ) {
const item alt_result = result_it.contents.front();
if( result_it.contents.legacy_size() == 1 ) {
const item alt_result = result_it.contents.legacy_front();
if( alt_result.typeId() == comest_it.typeId() ) {
result_it = alt_result;
}
Expand Down Expand Up @@ -1488,13 +1488,13 @@ bool Character::can_consume( const item &it ) const
}
// Checking NO_RELOAD to prevent consumption of `battery` when contained in `battery_car` (#20012)
return !it.is_container_empty() && !it.has_flag( flag_NO_RELOAD ) &&
can_consume_as_is( it.contents.front() );
can_consume_as_is( it.contents.legacy_front() );
}

item &Character::get_consumable_from( item &it ) const
{
if( !it.is_container_empty() && can_consume_as_is( it.contents.front() ) ) {
return it.contents.front();
if( !it.is_container_empty() && can_consume_as_is( it.contents.legacy_front() ) ) {
return it.contents.legacy_front();
} else if( can_consume_as_is( it ) ) {
return it;
}
Expand Down
25 changes: 8 additions & 17 deletions src/crafting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@

class basecamp;

void drop_or_handle( const item &newit, player &p );

static bool crafting_allowed( const player &p, const recipe &rec )
{
if( p.morale_crafting_speed_multiplier( rec ) <= 0.0f ) {
Expand Down Expand Up @@ -376,8 +374,8 @@ bool player::check_eligible_containers_for_crafting( const recipe &rec, int batc
}

if( !cont->is_container_empty() ) {
if( cont->contents.front().typeId() == prod.typeId() ) {
charges_to_store -= cont->get_remaining_capacity_for_liquid( cont->contents.front(), true );
if( cont->contents.legacy_front().typeId() == prod.typeId() ) {
charges_to_store -= cont->get_remaining_capacity_for_liquid( cont->contents.legacy_front(), true );
}
} else {
charges_to_store -= cont->get_remaining_capacity_for_liquid( prod, true );
Expand Down Expand Up @@ -1064,7 +1062,7 @@ void player::complete_craft( item &craft, const tripoint &loc )
// Points to newit unless newit is a non-empty container, then it points to newit's contents.
// Necessary for things like canning soup; sometimes we want to operate on the soup, not the can.
item &food_contained = ( newit.is_container() && !newit.contents.empty() ) ?
newit.contents.back() : newit;
newit.contents.legacy_back() : newit;

// messages, learning of recipe, food spoilage calculation only once
if( first ) {
Expand Down Expand Up @@ -1539,11 +1537,11 @@ static void empty_buckets( player &p )
return it.is_bucket_nonempty() && &it != &p.weapon;
}, INT_MAX );
for( auto &it : buckets ) {
for( const item &in : it.contents ) {
drop_or_handle( in, p );
for( const item *in : it.contents.all_items_ptr() ) {
drop_or_handle( *in, p );
}

it.contents.clear();
it.contents.clear_items();
drop_or_handle( it, p );
}
}
Expand Down Expand Up @@ -2259,7 +2257,7 @@ void remove_ammo( std::list<item> &dis_items, player &p )
}
}

void drop_or_handle( const item &newit, player &p )
void drop_or_handle( const item &newit, Character &p )
{
if( newit.made_of( LIQUID ) && p.is_player() ) { // TODO: what about NPCs?
liquid_handler::handle_all_liquid( newit, PICKUP_RANGE );
Expand All @@ -2271,14 +2269,7 @@ void drop_or_handle( const item &newit, player &p )

void remove_ammo( item &dis_item, player &p )
{
for( auto iter = dis_item.contents.begin(); iter != dis_item.contents.end(); ) {
if( iter->is_irremovable() ) {
iter++;
continue;
}
drop_or_handle( *iter, p );
iter = dis_item.contents.erase( iter );
}
dis_item.contents.remove_all_ammo( p );

if( dis_item.has_flag( flag_NO_UNLOAD ) ) {
return;
Expand Down
Loading