Skip to content

Commit

Permalink
Merge pull request #52708 from RoyBerube/item-activation-problem
Browse files Browse the repository at this point in the history
Fixes improper activation of items that have pockets
  • Loading branch information
Rivet-the-Zombie authored Nov 10, 2021
2 parents 3df2f1d + a382caf commit 2d60627
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
9 changes: 7 additions & 2 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1800,10 +1800,15 @@ int game::inventory_item_menu( item_location locThisItem,
case 'a': {
contents_change_handler handler;
handler.unseal_pocket_containing( locThisItem );
if( !locThisItem.get_item()->is_container() ) {
if( locThisItem.get_item()->type->has_use() &&
!locThisItem.get_item()->item_has_uses_recursive( true ) ) {
// Item has uses and none of its contents (if any) has uses.
avatar_action::use_item( u, locThisItem );
} else {
} else if( locThisItem.get_item()->item_has_uses_recursive() ) {
game::item_action_menu( locThisItem );
} else {
add_msg( m_info, _( "You can't use a %s there." ), locThisItem->tname() );
break;
}
handler.handle_by( u );
break;
Expand Down
6 changes: 5 additions & 1 deletion src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,11 @@ class item : public visitable
item in_container( const itype_id &container_type, int qty = INFINITE_CHARGES,
bool sealed = true ) const;

bool item_has_uses_recursive() const;
/**
* True if item and its contents have any uses.
* @param contents_only Set to true to ignore the item itself and check only its contents.
*/
bool item_has_uses_recursive( bool contents_only = false ) const;

/*@{*/
/**
Expand Down
18 changes: 11 additions & 7 deletions src/item_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,9 @@ item_action_generator::item_action_generator() = default;

item_action_generator::~item_action_generator() = default;

// Get use methods of this item and its contents
bool item::item_has_uses_recursive() const
bool item::item_has_uses_recursive( bool contents_only ) const
{
if( !type->use_methods.empty() ) {
if( !contents_only && !type->use_methods.empty() ) {
return true;
}

Expand Down Expand Up @@ -264,6 +263,9 @@ void game::item_action_menu( item_location loc )
pseudos.push_back( const_cast<item *>( pseudo ) );
}
} else {
if( loc.get_item()->type->has_use() ) {
pseudos.push_back( const_cast< item * >( loc.get_item() ) );
}
loc.get_item()->visit_contents( [&pseudos]( item * node, item * ) {
if( node->type->use_methods.empty() ) {
return VisitResponse::NEXT;
Expand Down Expand Up @@ -327,10 +329,12 @@ void game::item_action_menu( item_location loc )
} );
// Sort mapped actions.
sort_menu( menu_items.begin(), menu_items.end() );
// Add unmapped but binded actions to the menu vector.
for( const auto &elem : item_actions ) {
if( key_bound_to( ctxt, elem.first ).has_value() && !assigned_action( elem.first ) ) {
menu_items.emplace_back( elem.first, gen.get_action_name( elem.first ), "-" );
if( !loc ) {
// Add unmapped but binded actions to the menu vector.
for( const auto &elem : item_actions ) {
if( key_bound_to( ctxt, elem.first ).has_value() && !assigned_action( elem.first ) ) {
menu_items.emplace_back( elem.first, gen.get_action_name( elem.first ), "-" );
}
}
}
// Sort unmapped actions.
Expand Down

0 comments on commit 2d60627

Please sign in to comment.