Skip to content

Commit

Permalink
Make get_wielded_item() return a ref instead of a pointer (CleverRa…
Browse files Browse the repository at this point in the history
  • Loading branch information
Fris0uman authored Sep 14, 2021
1 parent 920a088 commit aa69568
Show file tree
Hide file tree
Showing 53 changed files with 371 additions and 371 deletions.
4 changes: 2 additions & 2 deletions src/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,9 +732,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 &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.is_gun() || weapon.has_flag( flag_REACH_ATTACK ) ) {
action_weightings[ACTION_FIRE] = 350;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/activity_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ item *aim_activity_actor::get_weapon()
} 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;
item &weapon = get_player_character().get_wielded_item();
return weapon.is_null() ? nullptr : &weapon;
}
}

Expand Down Expand Up @@ -3873,7 +3873,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
12 changes: 6 additions & 6 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1808,19 +1808,19 @@ 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 &weapon = you->get_wielded_item();
// 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( weapon.damage_melee( damage_type::CUT ),
weapon.damage_melee( damage_type::STAB ) / 2 );

///\EFFECT_STR increases pulping power, with diminishing returns
float pulp_power = std::sqrt( ( you->str_cur + weapon->damage_melee( damage_type::BASH ) ) *
float pulp_power = std::sqrt( ( you->str_cur + weapon.damage_melee( damage_type::BASH ) ) *
( cut_power + 1.0f ) );
float pulp_effort = you->str_cur + weapon->damage_melee( damage_type::BASH );
float pulp_effort = you->str_cur + weapon.damage_melee( damage_type::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;
const int mess_radius = weapon.has_flag( flag_MESSY ) ? 2 : 1;

int moves = 0;
// use this to collect how many corpse are pulped
Expand Down
14 changes: 7 additions & 7 deletions src/advanced_inv_pane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ std::vector<advanced_inv_listitem> avatar::get_AIM_inventory( const advanced_inv
}
}
}
item *weapon = get_wielded_item();
if( weapon->is_container() ) {
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( *this, &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 @@ -181,9 +181,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 &weapon = u.get_wielded_item();
if( !weapon.is_null() ) {
advanced_inv_listitem it( item_location( u, &weapon ), 0, 1, square.id, false );
if( !is_filtered( *it.items.front() ) ) {
square.volume += it.volume;
square.weight += it.weight;
Expand Down
18 changes: 9 additions & 9 deletions src/avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1329,8 +1329,8 @@ bool avatar::wield( item &target, const int obtain_cost )
return true;
}

item *weapon = get_wielded_item();
if( weapon->has_item( target ) ) {
item &weapon = get_wielded_item();
if( 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 @@ -1339,7 +1339,7 @@ bool avatar::wield( item &target, const int obtain_cost )
return false;
}

bool combine_stacks = target.can_combine( *weapon );
bool combine_stacks = target.can_combine( weapon );
if( !combine_stacks && !unwield() ) {
return false;
}
Expand All @@ -1366,28 +1366,28 @@ 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();
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
44 changes: 22 additions & 22 deletions src/avatar_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ 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 ) ) &&
if( ( you.get_working_leg_count() < 2 && !you.get_wielded_item().has_flag( flag_CRUTCHES ) ) &&
!you.is_prone() ) {
you.set_movement_mode( move_mode_id( "prone" ) );
you.add_msg_if_player( m_bad,
Expand Down Expand Up @@ -187,20 +187,20 @@ bool avatar_action::move( avatar &you, map &m, const tripoint &d )
via_ramp = true;
}

item *weapon = you.get_wielded_item();
item &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.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 @@ -667,8 +667,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 &weapon = you.get_wielded_item();
int reach = weapon.reach_range( you );
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 @@ -794,18 +794,18 @@ 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 &weapon = you.get_wielded_item();
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 @@ -849,7 +849,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 = item_location( you, &you.get_wielded_item() );
} else {
add_msg( m_info, _( "You're not wielding anything." ) );
return;
Expand Down Expand Up @@ -1027,8 +1027,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 &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 @@ -1040,8 +1040,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
36 changes: 18 additions & 18 deletions src/bionics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ void npc::check_or_use_weapon_cbm( const bionic_id &cbm_id )
}
bionic &bio = ( *my_bionics )[index];

item *weapon = get_wielded_item();
item &weapon = get_wielded_item();
if( bio.info().has_flag( json_flag_BIONIC_GUN ) ) {
if( !bio.has_weapon() ) {
debugmsg( "NPC tried to activate gun bionic \"%s\" without fake_weapon",
Expand All @@ -598,19 +598,19 @@ void npc::check_or_use_weapon_cbm( const bionic_id &cbm_id )
return;
}

int ammo_count = weapon->ammo_remaining( this );
const int ups_drain = weapon->get_gun_ups_drain();
int ammo_count = weapon.ammo_remaining( this );
const int ups_drain = weapon.get_gun_ups_drain();
if( ups_drain > 0 ) {
ammo_count = ammo_count / ups_drain;
}
const int cbm_ammo = free_power / bio.info().power_activate;

if( weapon_value( *weapon, ammo_count ) < weapon_value( cbm_weapon, cbm_ammo ) ) {
real_weapon = *weapon;
if( weapon_value( weapon, ammo_count ) < weapon_value( cbm_weapon, cbm_ammo ) ) {
real_weapon = weapon;
set_wielded_item( cbm_weapon );
cbm_weapon_index = index;
}
} else if( bio.info().has_flag( json_flag_BIONIC_WEAPON ) && !weapon->has_flag( flag_NO_UNWIELD ) &&
} else if( bio.info().has_flag( json_flag_BIONIC_WEAPON ) && !weapon.has_flag( flag_NO_UNWIELD ) &&
free_power > bio.info().power_activate ) {

if( !bio.has_weapon() ) {
Expand All @@ -620,7 +620,7 @@ void npc::check_or_use_weapon_cbm( const bionic_id &cbm_id )
}

if( is_armed() ) {
stow_item( *weapon );
stow_item( weapon );
}
add_msg_if_player_sees( pos(), m_info, _( "%s activates their %s." ),
disp_name(), bio.info().name );
Expand All @@ -640,7 +640,7 @@ void npc::check_or_use_weapon_cbm( const bionic_id &cbm_id )
bool Character::activate_bionic( int b, bool eff_only, bool *close_bionics_ui )
{

item *weapon = get_wielded_item();
item &weapon = get_wielded_item();
bionic &bio = ( *my_bionics )[b];
const bool mounted = is_mounted();
if( bio.incapacitated_time > 0_turns ) {
Expand All @@ -650,9 +650,9 @@ bool Character::activate_bionic( int b, bool eff_only, bool *close_bionics_ui )
}

// Special compatibility code for people who updated saves with their claws out
if( ( weapon->typeId().str() == bio_claws_weapon.str() &&
if( ( weapon.typeId().str() == bio_claws_weapon.str() &&
bio.id == bio_claws_weapon ) ||
( weapon->typeId().str() == bio_blade_weapon.str() &&
( weapon.typeId().str() == bio_blade_weapon.str() &&
bio.id == bio_blade_weapon ) ) {
return deactivate_bionic( b );
}
Expand Down Expand Up @@ -728,7 +728,7 @@ bool Character::activate_bionic( int b, bool eff_only, bool *close_bionics_ui )
return false;
}

if( weapon->has_flag( flag_NO_UNWIELD ) ) {
if( weapon.has_flag( flag_NO_UNWIELD ) ) {
cata::optional<int> active_bio_weapon_index = active_bionic_weapon_index();
if( active_bio_weapon_index && deactivate_bionic( *active_bio_weapon_index, eff_only ) ) {
// restore state and try again
Expand All @@ -738,15 +738,15 @@ bool Character::activate_bionic( int b, bool eff_only, bool *close_bionics_ui )
return activate_bionic( b, eff_only, close_bionics_ui );
}

add_msg_if_player( m_info, _( "Deactivate your %s first!" ), weapon->tname() );
add_msg_if_player( m_info, _( "Deactivate your %s first!" ), weapon.tname() );
refund_power();
bio.powered = false;
return false;
}

if( !weapon->is_null() ) {
const std::string query = string_format( _( "Stop wielding %s?" ), weapon->tname() );
if( !dispose_item( item_location( *this, weapon ), query ) ) {
if( !weapon.is_null() ) {
const std::string query = string_format( _( "Stop wielding %s?" ), weapon.tname() );
if( !dispose_item( item_location( *this, &weapon ), query ) ) {
refund_power();
bio.powered = false;
return false;
Expand All @@ -756,7 +756,7 @@ bool Character::activate_bionic( int b, bool eff_only, bool *close_bionics_ui )
add_msg_activate();

set_wielded_item( bio.get_weapon() );
get_wielded_item()->invlet = '#';
get_wielded_item().invlet = '#';
//if( bio.ammo_count > 0 ) {
// weapon.ammo_set( bio.ammo_loaded, bio.ammo_count );
// avatar_action::fire_wielded_weapon( player_character );
Expand Down Expand Up @@ -1215,7 +1215,7 @@ bool Character::deactivate_bionic( int b, bool eff_only )
bio.powered = false;
add_msg_if_player( m_neutral, _( "You deactivate your %s." ), bio.info().name );
}
const item &w_weapon = *get_wielded_item();
const item &w_weapon = get_wielded_item();
// Deactivation effects go here
if( bio.info().has_flag( json_flag_BIONIC_WEAPON ) && !bio.info().fake_weapon.is_empty() ) {
if( w_weapon.typeId() == bio.info().fake_weapon ) {
Expand All @@ -1227,7 +1227,7 @@ bool Character::deactivate_bionic( int b, bool eff_only )
add_msg_if_npc( m_info, _( "<npcname> withdraws her %s." ), w_weapon.tname() );
}
}
bio.set_weapon( *get_wielded_item() );
bio.set_weapon( get_wielded_item() );
set_wielded_item( item() );
}
} else if( bio.id == bio_cqb ) {
Expand Down
Loading

0 comments on commit aa69568

Please sign in to comment.