Skip to content

Commit

Permalink
change return type of Character::get_wielded_item to item_location
Browse files Browse the repository at this point in the history
  • Loading branch information
mqrause committed Jul 8, 2022
1 parent 414c57e commit 9ada1c5
Show file tree
Hide file tree
Showing 72 changed files with 821 additions and 739 deletions.
4 changes: 2 additions & 2 deletions src/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -747,9 +747,9 @@ action_id handle_action_menu()
if( !player_character.controlling_vehicle ) {
action_weightings[ACTION_CYCLE_MOVE] = 400;
}
const item &weapon = player_character.get_wielded_item();
const item_location weapon = player_character.get_wielded_item();
// Only prioritize fire weapon options if we're wielding a ranged weapon.
if( weapon.is_gun() || weapon.has_flag( flag_REACH_ATTACK ) ) {
if( weapon && ( weapon->is_gun() || weapon->has_flag( flag_REACH_ATTACK ) ) ) {
action_weightings[ACTION_FIRE] = 350;
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/activity_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ void aim_activity_actor::do_turn( player_activity &act, Character &who )
}
avatar &you = get_avatar();

item *weapon = get_weapon();
item_location weapon = get_weapon();
if( !weapon || !avatar_action::can_fire_weapon( you, get_map(), *weapon ) ) {
aborted = true;
act.moves_left = 0;
Expand Down Expand Up @@ -305,7 +305,7 @@ void aim_activity_actor::finish( player_activity &act, Character &who )
{
act.set_to_null();
restore_view();
item *weapon = get_weapon();
item_location weapon = get_weapon();
if( !weapon ) {
return;
}
Expand Down Expand Up @@ -377,16 +377,16 @@ std::unique_ptr<activity_actor> aim_activity_actor::deserialize( JsonValue &jsin
return actor.clone();
}

item *aim_activity_actor::get_weapon()
item_location aim_activity_actor::get_weapon()
{
if( fake_weapon.has_value() ) {
// TODO: check if the player lost relevant bionic/mutation
return &fake_weapon.value();
return item_location( get_player_character(), &fake_weapon.value() );
} else {
// Check for lost gun (e.g. yanked by zombie technician)
// TODO: check that this is the same gun that was used to start aiming
item &weapon = get_player_character().get_wielded_item();
return weapon.is_null() ? nullptr : &weapon;
return get_player_character().get_wielded_item();

}
}

Expand All @@ -405,7 +405,7 @@ bool aim_activity_actor::load_RAS_weapon()
{
// TODO: use activity for fetching ammo and loading weapon
Character &you = get_avatar();
item *weapon = get_weapon();
item_location weapon = get_weapon();
gun_mode gun = weapon->gun_current_mode();
const auto ammo_location_is_valid = [&]() -> bool {
if( !you.ammo_location )
Expand All @@ -422,8 +422,8 @@ bool aim_activity_actor::load_RAS_weapon()
}
return true;
};
item::reload_option opt = ammo_location_is_valid() ? item::reload_option( &you, weapon,
weapon, you.ammo_location ) : you.select_ammo( *gun );
item::reload_option opt = ammo_location_is_valid() ? item::reload_option( &you, &*weapon,
&*weapon, you.ammo_location ) : you.select_ammo( *gun );
if( !opt ) {
// Menu canceled
return false;
Expand All @@ -450,7 +450,7 @@ void aim_activity_actor::unload_RAS_weapon()
{
// Unload reload-and-shoot weapons to avoid leaving bows pre-loaded with arrows
avatar &you = get_avatar();
item *weapon = get_weapon();
item_location weapon = get_weapon();
if( !weapon ) {
return;
}
Expand Down Expand Up @@ -4035,7 +4035,7 @@ void reload_activity_actor::finish( player_activity &act, Character &who )
reloadable_name );
if( who.has_wield_conflicts( reloadable ) ) {
reload_query.addentry( 1, wield_check, 'w',
_( "Dispose of %s and wield %s" ), who.get_wielded_item().display_name(),
_( "Dispose of %s and wield %s" ), who.get_wielded_item()->display_name(),
reloadable_name );
} else {
reload_query.addentry( 1, wield_check, 'w', _( "Wield %s" ), reloadable_name );
Expand Down
2 changes: 1 addition & 1 deletion src/activity_actor_definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class aim_activity_actor : public activity_actor
void serialize( JsonOut &jsout ) const override;
static std::unique_ptr<activity_actor> deserialize( JsonValue &jsin );

item *get_weapon();
item_location get_weapon();
void restore_view();
// Load/unload a RELOAD_AND_SHOOT weapon
bool load_RAS_weapon();
Expand Down
26 changes: 18 additions & 8 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1598,20 +1598,30 @@ void activity_handlers::pulp_do_turn( player_activity *act, Character *you )
map &here = get_map();
const tripoint &pos = here.getlocal( act->placement );

const item &weapon = you->get_wielded_item();
const item_location weapon = you->get_wielded_item();
int weap_cut = 0;
int weap_stab = 0;
int weap_bash = 0;
int mess_radius = 1;

if( weapon ) {
weap_cut = weapon->damage_melee( damage_type::CUT );
weap_stab = weapon->damage_melee( damage_type::STAB );
weap_bash = weapon->damage_melee( damage_type::BASH );
if( weapon->has_flag( flag_MESSY ) ) {
mess_radius = 2;
}
}

// Stabbing weapons are a lot less effective at pulping
const int cut_power = std::max( weapon.damage_melee( damage_type::CUT ),
weapon.damage_melee( damage_type::STAB ) / 2 );
const int cut_power = std::max( weap_cut, weap_stab / 2 );

///\EFFECT_STR increases pulping power, with diminishing returns
float pulp_power = std::sqrt( ( you->get_arm_str() + weapon.damage_melee( damage_type::BASH ) ) *
( cut_power + 1.0f ) );
float pulp_effort = you->str_cur + weapon.damage_melee( damage_type::BASH );
float pulp_power = std::sqrt( ( you->get_arm_str() + weap_bash ) * ( cut_power + 1.0f ) );
float pulp_effort = you->str_cur + weap_bash;
// Multiplier to get the chance right + some bonus for survival skill
pulp_power *= 40 + you->get_skill_level( skill_survival ) * 5;

const int mess_radius = weapon.has_flag( flag_MESSY ) ? 2 : 1;

int moves = 0;
// use this to collect how many corpse are pulped
int &num_corpses = act->index;
Expand Down
15 changes: 7 additions & 8 deletions src/advanced_inv_pane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,10 @@ std::vector<advanced_inv_listitem> avatar::get_AIM_inventory( const advanced_inv
std::vector<advanced_inv_listitem> items = worn.get_AIM_inventory( item_index, *this, pane,
square );

item &weapon = get_wielded_item();
if( weapon.is_container() ) {
for( const std::vector<item_location> &it_stack : item_list_to_stack(
item_location( *this, &weapon ),
weapon.all_items_top( item_pocket::pocket_type::CONTAINER ) ) ) {
item_location weapon = get_wielded_item();
if( weapon->is_container() ) {
for( const std::vector<item_location> &it_stack : item_list_to_stack( weapon,
weapon->all_items_top( item_pocket::pocket_type::CONTAINER ) ) ) {
advanced_inv_listitem adv_it( it_stack, item_index++, square.id, false );
if( !pane.is_filtered( *adv_it.items.front() ) ) {
square.volume += adv_it.volume;
Expand Down Expand Up @@ -205,9 +204,9 @@ void advanced_inventory_pane::add_items_from_area( advanced_inv_area &square,
square.volume = 0_ml;
square.weight = 0_gram;

item &weapon = u.get_wielded_item();
if( !weapon.is_null() ) {
advanced_inv_listitem it( item_location( u, &weapon ), 0, 1, square.id, false );
item_location weapon = u.get_wielded_item();
if( weapon ) {
advanced_inv_listitem it( weapon, 0, 1, square.id, false );
if( !is_filtered( *it.items.front() ) ) {
square.volume += it.volume;
square.weight += it.weight;
Expand Down
22 changes: 12 additions & 10 deletions src/avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ void avatar::rebuild_aim_cache()
}

// calc steadiness with player recoil (like they are taking a regular shot not careful etc.
float range = 3.0f - 2.8f * calc_steadiness( *this, &get_wielded_item(),
float range = 3.0f - 2.8f * calc_steadiness( *this, &*get_wielded_item(),
last_target_pos.value(), recoil );

// pin between pi and negative pi
Expand Down Expand Up @@ -1335,8 +1335,8 @@ bool avatar::wield( item &target, const int obtain_cost )
return true;
}

item &weapon = get_wielded_item();
if( weapon.has_item( target ) ) {
item_location weapon = get_wielded_item();
if( weapon && weapon->has_item( target ) ) {
add_msg( m_info, _( "You need to put the bag away before trying to wield something from it." ) );
return false;
}
Expand All @@ -1345,7 +1345,7 @@ bool avatar::wield( item &target, const int obtain_cost )
return false;
}

bool combine_stacks = target.can_combine( weapon );
bool combine_stacks = weapon && target.can_combine( *weapon );
if( !combine_stacks && !unwield() ) {
return false;
}
Expand All @@ -1372,28 +1372,30 @@ bool avatar::wield( item &target, const int obtain_cost )
if( has_item( target ) ) {
item removed = i_rem( &target );
if( combine_stacks ) {
weapon.combine( removed );
weapon->combine( removed );
} else {
set_wielded_item( removed );

}
} else {
if( combine_stacks ) {
weapon.combine( target );
weapon->combine( target );
} else {
set_wielded_item( target );
}
}

last_item = weapon.typeId();
// set_wielded_item invalidates the weapon item_location, so get it again
weapon = get_wielded_item();
last_item = weapon->typeId();
recoil = MAX_RECOIL;

weapon.on_wield( *this );
weapon->on_wield( *this );

get_event_bus().send<event_type::character_wields_item>( getID(), last_item );

inv->update_invlet( weapon );
inv->update_cache_with_item( weapon );
inv->update_invlet( *weapon );
inv->update_cache_with_item( *weapon );

return true;
}
Expand Down
53 changes: 29 additions & 24 deletions src/avatar_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ bool avatar_action::move( avatar &you, map &m, const tripoint &d )
}

// If any leg broken without crutches and not already on the ground topple over
if( ( you.get_working_leg_count() < 2 && !you.get_wielded_item().has_flag( flag_CRUTCHES ) ) &&
!you.is_prone() ) {
if( ( you.get_working_leg_count() < 2 && !you.is_prone() &&
!( you.get_wielded_item() && you.get_wielded_item()->has_flag( flag_CRUTCHES ) ) ) ) {
you.set_movement_mode( move_mode_prone );
you.add_msg_if_player( m_bad,
_( "Your broken legs can't hold your weight and you fall down in pain." ) );
Expand Down Expand Up @@ -195,20 +195,20 @@ bool avatar_action::move( avatar &you, map &m, const tripoint &d )
via_ramp = true;
}

item &weapon = you.get_wielded_item();
item_location weapon = you.get_wielded_item();
if( m.has_flag( ter_furn_flag::TFLAG_MINEABLE, dest_loc ) && g->mostseen == 0 &&
get_option<bool>( "AUTO_FEATURES" ) && get_option<bool>( "AUTO_MINING" ) &&
!m.veh_at( dest_loc ) && !you.is_underwater() && !you.has_effect( effect_stunned ) &&
!is_riding && !you.has_effect( effect_incorporeal ) ) {
if( weapon.has_flag( flag_DIG_TOOL ) ) {
if( weapon.type->can_use( "JACKHAMMER" ) &&
weapon.ammo_sufficient( &you ) ) {
you.invoke_item( &weapon, "JACKHAMMER", dest_loc );
if( weapon && weapon->has_flag( flag_DIG_TOOL ) ) {
if( weapon->type->can_use( "JACKHAMMER" ) &&
weapon->ammo_sufficient( &you ) ) {
you.invoke_item( &*weapon, "JACKHAMMER", dest_loc );
// don't move into the tile until done mining
you.defer_move( dest_loc );
return true;
} else if( weapon.type->can_use( "PICKAXE" ) ) {
you.invoke_item( &weapon, "PICKAXE", dest_loc );
} else if( weapon->type->can_use( "PICKAXE" ) ) {
you.invoke_item( &*weapon, "PICKAXE", dest_loc );
// don't move into the tile until done mining
you.defer_move( dest_loc );
return true;
Expand Down Expand Up @@ -664,7 +664,7 @@ static float rate_critter( const Creature &c )
{
const npc *np = dynamic_cast<const npc *>( &c );
if( np != nullptr ) {
return np->weapon_value( np->get_wielded_item() );
return np->weapon_value( *np->get_wielded_item() );
}

const monster *m = dynamic_cast<const monster *>( &c );
Expand All @@ -673,8 +673,8 @@ static float rate_critter( const Creature &c )

void avatar_action::autoattack( avatar &you, map &m )
{
const item &weapon = you.get_wielded_item();
int reach = weapon.reach_range( you );
const item_location weapon = you.get_wielded_item();
int reach = weapon ? weapon->reach_range( you ) : 1;
std::vector<Creature *> critters = you.get_targetable_creatures( reach, true );
critters.erase( std::remove_if( critters.begin(), critters.end(), [&you,
reach]( const Creature * c ) {
Expand Down Expand Up @@ -800,18 +800,23 @@ static bool can_fire_turret( avatar &you, const map &m, const turret_data &turre

void avatar_action::fire_wielded_weapon( avatar &you )
{
const item &weapon = you.get_wielded_item();
if( weapon.is_gunmod() ) {
const item_location weapon = you.get_wielded_item();

if( !weapon ) {
return;
}

if( weapon->is_gunmod() ) {
add_msg( m_info,
_( "The %s must be attached to a gun, it can not be fired separately." ),
weapon.tname() );
weapon->tname() );
return;
} else if( !weapon.is_gun() ) {
} else if( !weapon->is_gun() ) {
return;
} else if( weapon.ammo_data() &&
!weapon.ammo_types().count( weapon.loaded_ammo().ammo_type() ) ) {
} else if( weapon->ammo_data() &&
!weapon->ammo_types().count( weapon->loaded_ammo().ammo_type() ) ) {
add_msg( m_info, _( "The %s can't be fired while loaded with incompatible ammunition %s" ),
weapon.tname(), weapon.ammo_current()->nname( 1 ) );
weapon->tname(), weapon->ammo_current()->nname( 1 ) );
return;
}

Expand Down Expand Up @@ -855,7 +860,7 @@ void avatar_action::mend( avatar &you, item_location loc )

if( !loc ) {
if( you.is_armed() ) {
loc = item_location( you, &you.get_wielded_item() );
loc = you.get_wielded_item();
} else {
add_msg( m_info, _( "You're not wielding anything." ) );
return;
Expand Down Expand Up @@ -1031,8 +1036,8 @@ void avatar_action::plthrow( avatar &you, item_location loc,

g->temp_exit_fullscreen();

item &weapon = you.get_wielded_item();
target_handler::trajectory trajectory = target_handler::mode_throw( you, weapon,
item_location weapon = you.get_wielded_item();
target_handler::trajectory trajectory = target_handler::mode_throw( you, *weapon,
blind_throw_from_pos.has_value() );

// If we previously shifted our position, put ourselves back now that we've picked our target.
Expand All @@ -1044,8 +1049,8 @@ void avatar_action::plthrow( avatar &you, item_location loc,
return;
}

if( weapon.count_by_charges() && weapon.charges > 1 ) {
weapon.mod_charges( -1 );
if( weapon->count_by_charges() && weapon->charges > 1 ) {
weapon->mod_charges( -1 );
thrown.charges = 1;
} else {
you.remove_weapon();
Expand Down
Loading

0 comments on commit 9ada1c5

Please sign in to comment.