From a382caf571d5eb283a156a12f91ab0463749561f Mon Sep 17 00:00:00 2001 From: RoyBerube Date: Sun, 7 Nov 2021 22:57:36 -0700 Subject: [PATCH] Item activation from examine menu --- src/game.cpp | 9 +++++++-- src/item.h | 6 +++++- src/item_action.cpp | 18 +++++++++++------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 8b190c883860e..efc75f5a33d01 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -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; diff --git a/src/item.h b/src/item.h index 74d3907025ae3..6e6b214bcd197 100644 --- a/src/item.h +++ b/src/item.h @@ -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; /*@{*/ /** diff --git a/src/item_action.cpp b/src/item_action.cpp index 636a21214f3bd..d0e2902e98474 100644 --- a/src/item_action.cpp +++ b/src/item_action.cpp @@ -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; } @@ -264,6 +263,9 @@ void game::item_action_menu( item_location loc ) pseudos.push_back( const_cast( 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; @@ -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.