Skip to content

Commit

Permalink
Merge pull request CleverRaven#32866 from Qrox/fix-stacking
Browse files Browse the repository at this point in the history
Fix stacking of magazines and items counted by charge
  • Loading branch information
kevingranade authored Aug 3, 2019
2 parents 5e13831 + 53081e1 commit 598a1d9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/advanced_inv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ static itemstack i_stacked( T items )
// check to see if it stacks with each item in a stack, not just front()
for( auto &idx : iter->second ) {
for( auto &it : stacks[idx] ) {
if( ( got_stacked = it->stacks_with( elem ) ) ) {
if( ( got_stacked = it->display_stacked_with( elem ) ) ) {
stacks[idx].push_back( &elem );
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/inventory_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ static std::vector<std::list<item *>> restack_items( const std::list<item>::cons
for( auto it = from; it != to; ++it ) {
auto match = std::find_if( res.begin(), res.end(),
[ &it, check_components ]( const std::list<item *> &e ) {
return it->stacks_with( *const_cast<item *>( e.back() ), check_components );
return it->display_stacked_with( *const_cast<item *>( e.back() ), check_components );
} );

if( match != res.end() ) {
Expand All @@ -1045,7 +1045,7 @@ static std::vector<std::list<item *>> restack_items( const item_stack::const_ite
for( auto it = from; it != to; ++it ) {
auto match = std::find_if( res.begin(), res.end(),
[ &it, check_components ]( const std::list<item *> &e ) {
return it->stacks_with( *const_cast<item *>( e.back() ), check_components );
return it->display_stacked_with( *const_cast<item *>( e.back() ), check_components );
} );

if( match != res.end() ) {
Expand Down
20 changes: 17 additions & 3 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,11 @@ int item::charges_per_volume( const units::volume &vol ) const
}
}

bool item::display_stacked_with( const item &rhs, bool check_components ) const
{
return !count_by_charges() && stacks_with( rhs, check_components );
}

bool item::stacks_with( const item &rhs, bool check_components ) const
{
if( type != rhs.type ) {
Expand Down Expand Up @@ -6588,10 +6593,19 @@ bool item::reload( player &u, item_location loc, int qty )
}
}

contents.emplace_back( *ammo );
contents.back().charges = qty;
item to_reload = *ammo;
to_reload.charges = qty;
ammo->charges -= qty;

bool merged = false;
for( auto &it : contents ) {
if( it.merge_charges( to_reload ) ) {
merged = true;
break;
}
}
if( !merged ) {
contents.emplace_back( to_reload );
}
} else if( is_watertight_container() ) {
if( !ammo->made_of_from_type( LIQUID ) ) {
debugmsg( "Tried to reload liquid container with non-liquid." );
Expand Down
8 changes: 8 additions & 0 deletions src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,14 @@ class item : public visitable<item>
*/
int price( bool practical ) const;

/**
* Whether two items should stack when displayed in a inventory menu.
* This is different from stacks_with, when two previously non-stackable
* items are now stackable and mergeable because, for example, they
* reaches the same temperature. This is necessary to avoid misleading
* stacks like "3 items-count-by-charge (5)".
*/
bool display_stacked_with( const item &rhs, bool check_components = false ) const;
bool stacks_with( const item &rhs, bool check_components = false ) const;
/**
* Merge charges of the other item into this item.
Expand Down
2 changes: 1 addition & 1 deletion src/pickup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ void Pickup::pick_up( const tripoint &p, int min, from_where get_items_from )
for( item_stack::iterator it : here ) {
bool found_stack = false;
for( std::list<item_stack::iterator> &stack : stacked_here ) {
if( stack.front()->stacks_with( *it ) ) {
if( stack.front()->display_stacked_with( *it ) ) {
stack.push_back( it );
found_stack = true;
break;
Expand Down

0 comments on commit 598a1d9

Please sign in to comment.