Skip to content

Commit

Permalink
Make weapon private (CleverRaven#50286)
Browse files Browse the repository at this point in the history
* Make `weapon` private

* Update tests

* Rebase

* Appley suggestions

* Fix weapon dorping

* fix weapon saving

* Rename to get_wielded_item

* rename to set_wielded_item

* fixes

* Post rebase fixes

* fixes

* fix weapon saving

* fixes

* more rebase and fixes

* Apply suggestions

* post rebase fixes
  • Loading branch information
Fris0uman authored Sep 12, 2021
1 parent 21435d2 commit da590d8
Show file tree
Hide file tree
Showing 60 changed files with 503 additions and 412 deletions.
3 changes: 2 additions & 1 deletion src/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +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();
// Only prioritize fire weapon options if we're wielding a ranged weapon.
if( player_character.weapon.is_gun() || player_character.weapon.has_flag( flag_REACH_ATTACK ) ) {
if( weapon->is_gun() || weapon->has_flag( flag_REACH_ATTACK ) ) {
action_weightings[ACTION_FIRE] = 350;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/activity_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ 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().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.weapon.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
11 changes: 6 additions & 5 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1808,18 +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();
// Stabbing weapons are a lot less effective at pulping
const int cut_power = std::max( you->weapon.damage_melee( damage_type::CUT ),
you->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 + you->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 + you->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 = you->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
13 changes: 7 additions & 6 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
}
}
}

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,8 +181,9 @@ void advanced_inventory_pane::add_items_from_area( advanced_inv_area &square,
square.volume = 0_ml;
square.weight = 0_gram;

if( !u.weapon.is_null() ) {
advanced_inv_listitem it( item_location( u, &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
22 changes: 12 additions & 10 deletions src/avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,8 @@ bool avatar::wield( item &target, const int obtain_cost )
return true;
}

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 @@ -1328,7 +1329,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 @@ -1355,27 +1356,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 {
weapon = removed;
set_wielded_item( removed );

}
} else {
if( combine_stacks ) {
weapon.combine( target );
weapon->combine( target );
} else {
weapon = target;
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
42 changes: 23 additions & 19 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.weapon.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,19 +187,20 @@ bool avatar_action::move( avatar &you, map &m, const tripoint &d )
via_ramp = true;
}

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( you.weapon.has_flag( flag_DIG_TOOL ) ) {
if( you.weapon.type->can_use( "JACKHAMMER" ) &&
you.weapon.ammo_sufficient( &you ) ) {
you.invoke_item( &you.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( you.weapon.type->can_use( "PICKAXE" ) ) {
you.invoke_item( &you.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 @@ -657,7 +658,7 @@ static float rate_critter( const Creature &c )
{
const npc *np = dynamic_cast<const npc *>( &c );
if( np != nullptr ) {
return np->weapon_value( np->weapon );
return np->weapon_value( np->get_wielded_item() );
}

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

void avatar_action::autoattack( avatar &you, map &m )
{
int reach = you.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 @@ -792,17 +794,18 @@ static bool can_fire_turret( avatar &you, const map &m, const turret_data &turre

void avatar_action::fire_wielded_weapon( avatar &you )
{
item &weapon = you.weapon;
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 @@ -846,7 +849,7 @@ void avatar_action::mend( avatar &you, item_location loc )

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

g->temp_exit_fullscreen();

target_handler::trajectory trajectory = target_handler::mode_throw( you, 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 @@ -1036,8 +1040,8 @@ void avatar_action::plthrow( avatar &you, item_location loc,
return;
}

if( you.weapon.count_by_charges() && you.weapon.charges > 1 ) {
you.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 da590d8

Please sign in to comment.