Skip to content

Commit

Permalink
refactor game::unload (#36125)
Browse files Browse the repository at this point in the history
  • Loading branch information
KorGgenT authored and kevingranade committed Dec 17, 2019
1 parent e6a160d commit 347a0ae
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 37 deletions.
22 changes: 22 additions & 0 deletions src/avatar_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
2 changes: 2 additions & 0 deletions src/avatar_action.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<tripoint> &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 );
Expand Down
35 changes: 1 addition & 34 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 ) {
Expand Down
1 change: 0 additions & 1 deletion src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/handle_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1922,7 +1922,7 @@ bool game::handle_action()
break;

case ACTION_UNLOAD:
unload();
avatar_action::unload( u );
break;

case ACTION_MEND:
Expand Down
3 changes: 2 additions & 1 deletion tests/reloading_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit 347a0ae

Please sign in to comment.