From b3348d20cea6e6c983ebe1226b96bdaf29f15ed2 Mon Sep 17 00:00:00 2001 From: KorGgenT Date: Sat, 14 Dec 2019 12:25:16 -0500 Subject: [PATCH] change parameter of plthrow from int to item_location --- src/avatar_action.cpp | 34 +++++++++++++--------------------- src/avatar_action.h | 2 +- src/game.cpp | 5 +++-- src/handle_action.cpp | 6 ++++-- 4 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/avatar_action.cpp b/src/avatar_action.cpp index 4154b5c2e9e4c..8501eb4bce654 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 19b37fbab115a..560dba500ca32 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 ); // Use item; also tries E,R,W 'a' diff --git a/src/game.cpp b/src/game.cpp index ac2b442bb5056..0a07f466a493b 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 8f7c91e13a1fe..a33b5559a6c39 100644 --- a/src/handle_action.cpp +++ b/src/handle_action.cpp @@ -1924,9 +1924,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();