diff --git a/src/avatar_action.cpp b/src/avatar_action.cpp index ef0a55b791574..a7a85adff0fa1 100644 --- a/src/avatar_action.cpp +++ b/src/avatar_action.cpp @@ -935,7 +935,7 @@ void avatar_action::eat( avatar &you, item_location loc ) } } -void avatar_action::plthrow( avatar &you, int pos, +void avatar_action::plthrow( avatar &you, item_location loc, const cata::optional &blind_throw_from_pos ) { if( you.has_active_mutation( trait_SHELL2 ) ) { @@ -943,7 +943,7 @@ void avatar_action::plthrow( avatar &you, int pos, return; } if( you.is_mounted() ) { - auto mons = g->u.mounted_creature.get(); + monster *mons = g->u.mounted_creature.get(); if( mons->has_flag( MF_RIDEABLE_MECH ) ) { if( !mons->check_mech_powered() ) { add_msg( m_bad, _( "Your %s refuses to move as its batteries have been drained." ), @@ -953,18 +953,18 @@ void avatar_action::plthrow( avatar &you, int pos, } } - if( pos == INT_MIN ) { - pos = you.get_item_position( game_menus::inv::titled_menu( you, _( "Throw item" ), - _( "You don't have any items to throw." ) ).get_item() ); + if( !loc ) { + loc = game_menus::inv::titled_menu( you, _( "Throw item" ), + _( "You don't have any items to throw." ) ); g->refresh_all(); } - if( pos == INT_MIN ) { + if( !loc ) { add_msg( _( "Never mind." ) ); return; } - item thrown = you.i_at( pos ); + item &thrown = *loc; int range = you.throw_range( thrown ); if( range < 0 ) { add_msg( m_info, _( "You don't have that item." ) ); @@ -974,7 +974,7 @@ void avatar_action::plthrow( avatar &you, int pos, return; } - if( pos == -1 && thrown.has_flag( "NO_UNWIELD" ) ) { + if( you.is_wielding( thrown ) && thrown.has_flag( "NO_UNWIELD" ) ) { // pos == -1 is the weapon, NO_UNWIELD is used for bio_claws_weapon add_msg( m_info, _( "That's part of your body, you can't throw that!" ) ); return; @@ -990,24 +990,16 @@ void avatar_action::plthrow( avatar &you, int pos, } } // if you're wearing the item you need to be able to take it off - if( pos < -1 ) { - auto ret = you.can_takeoff( you.i_at( pos ) ); + if( you.is_wearing( thrown.typeId() ) ) { + ret_val ret = you.can_takeoff( thrown ); if( !ret.success() ) { add_msg( m_info, "%s", ret.c_str() ); return; } } // you must wield the item to throw it - if( pos != -1 ) { - you.i_rem( pos ); - if( !you.wield( thrown ) ) { - // We have to remove the item before checking for wield because it - // can invalidate our pos index. Which means we have to add it - // back if the player changed their mind about unwielding their - // current item - you.i_add( thrown ); - return; - } + if( !you.is_wielding( thrown ) ) { + you.wield( thrown ); } // Shift our position to our "peeking" position, so that the UI @@ -1038,7 +1030,7 @@ void avatar_action::plthrow( avatar &you, int pos, } if( thrown.count_by_charges() && thrown.charges > 1 ) { - you.i_at( -1 ).charges--; + you.weapon.mod_charges( -1 ); thrown.charges = 1; } else { you.i_rem( -1 ); diff --git a/src/avatar_action.h b/src/avatar_action.h index 6292eded4e732..f02c023cab253 100644 --- a/src/avatar_action.h +++ b/src/avatar_action.h @@ -65,7 +65,7 @@ bool fire( avatar &you, map &m ); */ bool fire( avatar &you, map &m, item &weapon, int bp_cost = 0 ); // Throw an item 't' -void plthrow( avatar &you, int pos = INT_MIN, +void plthrow( avatar &you, item_location loc, const cata::optional &blind_throw_from_pos = cata::nullopt ); void unload( avatar &you ); diff --git a/src/game.cpp b/src/game.cpp index a5df20ff55c33..7a725b3f2dac8 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2086,7 +2086,7 @@ int game::inventory_item_menu( int pos, int iStartX, int iWidth, wield( locThisItem ); break; case 't': - avatar_action::plthrow( u, pos ); + avatar_action::plthrow( u, locThisItem ); break; case 'c': change_side( pos ); @@ -5671,7 +5671,8 @@ void game::peek( const tripoint &p ) u.setpos( prev ); if( result.peek_action && *result.peek_action == PA_BLIND_THROW ) { - avatar_action::plthrow( u, INT_MIN, p ); + item_location loc; + avatar_action::plthrow( u, loc, p ); } m.invalidate_map_cache( p.z ); diff --git a/src/handle_action.cpp b/src/handle_action.cpp index bc4e630cf9dbd..f25e85c1cd331 100644 --- a/src/handle_action.cpp +++ b/src/handle_action.cpp @@ -1929,9 +1929,11 @@ bool game::handle_action() mend(); break; - case ACTION_THROW: - avatar_action::plthrow( g->u ); + case ACTION_THROW: { + item_location loc; + avatar_action::plthrow( g->u, loc ); break; + } case ACTION_FIRE: fire();