From 347a0ae7e12344a98448dff9c705ae408ba06d36 Mon Sep 17 00:00:00 2001 From: Curtis Merrill Date: Mon, 16 Dec 2019 21:14:46 -0500 Subject: [PATCH] refactor game::unload (#36125) --- src/avatar_action.cpp | 22 ++++++++++++++++++++++ src/avatar_action.h | 2 ++ src/game.cpp | 35 +---------------------------------- src/game.h | 1 - src/handle_action.cpp | 2 +- tests/reloading_test.cpp | 3 ++- 6 files changed, 28 insertions(+), 37 deletions(-) diff --git a/src/avatar_action.cpp b/src/avatar_action.cpp index 5259d1e031052..5551e85f654aa 100644 --- a/src/avatar_action.cpp +++ b/src/avatar_action.cpp @@ -1121,3 +1121,25 @@ void avatar_action::use_item( avatar &you, item_location &loc ) you.invalidate_crafting_inventory(); } + +// Opens up a menu to Unload a container, gun, or tool +// If it's a gun, some gunmods can also be loaded +void avatar_action::unload( avatar &you ) +{ + item_location loc; + + loc = g->inv_map_splice( [&you]( const item & it ) { + return you.rate_action_unload( it ) == HINT_GOOD; + }, _( "Unload item" ), 1, _( "You have nothing to unload." ) ); + + if( !loc ) { + add_msg( _( "Never mind." ) ); + return; + } + + if( you.unload( *loc ) ) { + if( loc->has_flag( "MAG_DESTROY" ) && loc->ammo_remaining() == 0 ) { + loc.remove_item(); + } + } +} diff --git a/src/avatar_action.h b/src/avatar_action.h index 19b37fbab115a..6292eded4e732 100644 --- a/src/avatar_action.h +++ b/src/avatar_action.h @@ -68,6 +68,8 @@ bool fire( avatar &you, map &m, item &weapon, int bp_cost = 0 ); void plthrow( avatar &you, int pos = INT_MIN, const cata::optional &blind_throw_from_pos = cata::nullopt ); +void unload( avatar &you ); + // Use item; also tries E,R,W 'a' void use_item( avatar &you, item_location &loc ); void use_item( avatar &you ); diff --git a/src/game.cpp b/src/game.cpp index e316a9fb68996..d3fc2f2f9cde9 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2098,7 +2098,7 @@ int game::inventory_item_menu( int pos, int iStartX, int iWidth, u.drop( pos, u.pos() ); break; case 'U': - unload( pos ); + unload( oThisItem ); break; case 'r': reload( locThisItem ); @@ -8517,39 +8517,6 @@ void game::reload_weapon( bool try_everything ) reload_item(); } -// Unload a container, gun, or tool -// If it's a gun, some gunmods can also be loaded -void game::unload( int pos ) -{ - item *it = nullptr; - item_location item_loc; - - if( pos == INT_MIN ) { - item_loc = inv_map_splice( [&]( const item & it ) { - return u.rate_action_unload( it ) == HINT_GOOD; - }, _( "Unload item" ), 1, _( "You have nothing to unload." ) ); - it = item_loc.get_item(); - - if( it == nullptr ) { - add_msg( _( "Never mind." ) ); - return; - } - } else { - it = &u.i_at( pos ); - if( it->is_null() ) { - debugmsg( "Tried to unload non-existent item" ); - return; - } - item_loc = item_location( u, it ); - } - - if( u.unload( *it ) ) { - if( it->has_flag( "MAG_DESTROY" ) && it->ammo_remaining() == 0 ) { - item_loc.remove_item(); - } - } -} - void game::mend( int pos ) { if( pos == INT_MIN ) { diff --git a/src/game.h b/src/game.h index b7c7f7e19f4ad..3a8177fa6500a 100644 --- a/src/game.h +++ b/src/game.h @@ -787,7 +787,6 @@ class game void place_player_overmap( const tripoint &om_dest ); bool unload( item &it ); // Unload a gun/tool 'U' - void unload( int pos = INT_MIN ); unsigned int get_seed() const; diff --git a/src/handle_action.cpp b/src/handle_action.cpp index bc232965454a4..b7cbb8c732645 100644 --- a/src/handle_action.cpp +++ b/src/handle_action.cpp @@ -1922,7 +1922,7 @@ bool game::handle_action() break; case ACTION_UNLOAD: - unload(); + avatar_action::unload( u ); break; case ACTION_MEND: diff --git a/tests/reloading_test.cpp b/tests/reloading_test.cpp index 23071748f49de..292b15dbcd229 100644 --- a/tests/reloading_test.cpp +++ b/tests/reloading_test.cpp @@ -100,8 +100,9 @@ TEST_CASE( "reload_gun_with_swappable_magazine", "[reload],[gun]" ) int gun_pos = dummy.inv.position_by_type( "glock_19" ); REQUIRE( gun_pos != INT_MIN ); + item &glock = dummy.i_at( gun_pos ); // We're expecting the magazine to end up in the inventory. - g->unload( gun_pos ); + g->unload( glock ); int magazine_pos = dummy.inv.position_by_type( "glockmag" ); REQUIRE( magazine_pos != INT_MIN ); item &magazine = dummy.inv.find_item( magazine_pos );