Skip to content

Commit

Permalink
Fix dangling reference in Character::gen_aim_mods_cache() (CleverRave…
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettDong authored Nov 21, 2023
1 parent 8e02002 commit d40bba2
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 13 deletions.
8 changes: 3 additions & 5 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -938,14 +938,12 @@ int Character::point_shooting_limit( const item &gun )const
aim_mods_cache Character::gen_aim_mods_cache( const item &gun )const
{
parallax_cache parallaxes{ get_character_parallax( true ), get_character_parallax( false ) };
auto parallaxes_opt = std::make_optional( std::ref( parallaxes ) );
aim_mods_cache aim_cache = { get_modifier( character_modifier_aim_speed_skill_mod, gun.gun_skill() ), get_modifier( character_modifier_aim_speed_dex_mod ), get_modifier( character_modifier_aim_speed_mod ), most_accurate_aiming_method_limit( gun ), aim_factor_from_volume( gun ), aim_factor_from_length( gun ), parallaxes_opt };
return aim_cache;
return { get_modifier( character_modifier_aim_speed_skill_mod, gun.gun_skill() ), get_modifier( character_modifier_aim_speed_dex_mod ), get_modifier( character_modifier_aim_speed_mod ), most_accurate_aiming_method_limit( gun ), aim_factor_from_volume( gun ), aim_factor_from_length( gun ), parallaxes };
}

double Character::fastest_aiming_method_speed( const item &gun, double recoil,
const Target_attributes target_attributes,
const std::optional<std::reference_wrapper<parallax_cache>> parallax_cache ) const
const std::optional<std::reference_wrapper<const parallax_cache>> parallax_cache ) const
{
// Get fastest aiming method that can be used to improve aim further below @ref recoil.

Expand Down Expand Up @@ -1098,7 +1096,7 @@ double Character::aim_per_move( const item &gun, double recoil,
}
bool use_cache = aim_cache.has_value();
double sight_speed_modifier = fastest_aiming_method_speed( gun, recoil, target_attributes,
use_cache ? aim_cache.value().get().parallaxes : std::nullopt );
use_cache ? std::make_optional( std::ref( aim_cache.value().get().parallaxes ) ) : std::nullopt );
int limit = use_cache ? aim_cache.value().get().limit :
most_accurate_aiming_method_limit( gun );
if( sight_speed_modifier == INT_MIN ) {
Expand Down
4 changes: 2 additions & 2 deletions src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ struct aim_mods_cache {
int limit;
double aim_factor_from_volume;
double aim_factor_from_length;
std::optional<std::reference_wrapper<parallax_cache>> parallaxes;
parallax_cache parallaxes;
};

struct special_attack {
Expand Down Expand Up @@ -768,7 +768,7 @@ class Character : public Creature, public visitable
int point_shooting_limit( const item &gun ) const;
double fastest_aiming_method_speed( const item &gun, double recoil,
Target_attributes target_attributes = Target_attributes(),
std::optional<std::reference_wrapper<parallax_cache>> parallax_cache = std::nullopt ) const;
std::optional<std::reference_wrapper<const parallax_cache>> parallax_cache = std::nullopt ) const;
int most_accurate_aiming_method_limit( const item &gun ) const;
double aim_factor_from_volume( const item &gun ) const;
double aim_factor_from_length( const item &gun ) const;
Expand Down
5 changes: 2 additions & 3 deletions src/npcmove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2521,13 +2521,12 @@ void npc::aim( const Target_attributes &target_attributes )
{
const item_location weapon = get_wielded_item();
double aim_amount = weapon ? aim_per_move( *weapon, recoil ) : 0.0;
const aim_mods_cache &aim_cache = gen_aim_mods_cache( *weapon );
auto aim_cache_opt = std::make_optional( std::ref( aim_cache ) );
const aim_mods_cache aim_cache = gen_aim_mods_cache( *weapon );
while( aim_amount > 0 && recoil > 0 && moves > 0 ) {
moves--;
recoil -= aim_amount;
recoil = std::max( 0.0, recoil );
aim_amount = aim_per_move( *weapon, recoil, target_attributes, aim_cache_opt );
aim_amount = aim_per_move( *weapon, recoil, target_attributes, { std::ref( aim_cache ) } );
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/ranged.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,10 +593,9 @@ int Character::gun_engagement_moves( const item &gun, int target, int start,
{
int mv = 0;
double penalty = start;
const aim_mods_cache &aim_cache = gen_aim_mods_cache( gun );
auto aim_cache_opt = std::make_optional( std::ref( aim_cache ) );
const aim_mods_cache aim_cache = gen_aim_mods_cache( gun );
while( penalty > target ) {
double adj = aim_per_move( gun, penalty, attributes, aim_cache_opt );
const double adj = aim_per_move( gun, penalty, attributes, { std::ref( aim_cache ) } );
if( adj <= MIN_RECOIL_IMPROVEMENT ) {
break;
}
Expand Down

0 comments on commit d40bba2

Please sign in to comment.