Skip to content

Commit

Permalink
Fix issue with auto_aim (#40662)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramza13 authored May 22, 2020
1 parent 639685b commit ac61da2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/avatar_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ static float rate_critter( const Creature &c )
void avatar_action::autoattack( avatar &you, map &m )
{
int reach = you.weapon.reach_range( you );
std::vector<Creature *> critters = you.get_targetable_creatures( reach );
std::vector<Creature *> critters = you.get_targetable_creatures( reach, true );
critters.erase( std::remove_if( critters.begin(), critters.end(), []( const Creature * c ) {
if( !c->is_npc() ) {
return false;
Expand Down
14 changes: 9 additions & 5 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10498,15 +10498,19 @@ std::vector<Creature *> Character::get_visible_creatures( const int range ) cons
} );
}

std::vector<Creature *> Character::get_targetable_creatures( const int range ) const
std::vector<Creature *> Character::get_targetable_creatures( const int range, bool melee ) const
{
return g->get_creatures_if( [this, range]( const Creature & critter ) -> bool {
bool can_see = sees( critter ) || sees_with_infrared( critter );
if( can_see ) //handles the case where we can see something with glass in the way or a mutation lets us see through walls
return g->get_creatures_if( [this, range, melee]( const Creature & critter ) -> bool {
//the call to map.sees is to make sure that even if we can see it through walls
//via a mutation or cbm we only attack targets with a line of sight
bool can_see = ( ( sees( critter ) || sees_with_infrared( critter ) ) && g->m.sees( pos(), critter.pos(), 100 ) );
if( can_see && melee ) //handles the case where we can see something with glass in the way for melee attacks
{
std::vector<tripoint> path = g->m.find_clear_path( pos(), critter.pos() );
for( const tripoint &point : path ) {
if( g->m.impassable( point ) ) {
if( g->m.impassable( point ) &&
!( weapon.has_flag( "SPEAR" ) && // Fences etc. Spears can stab through those
g->m.has_flag( "THIN_OBSTACLE", point ) ) ) { //this mirrors melee.cpp function reach_attack
can_see = false;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -1895,7 +1895,7 @@ class Character : public Creature, public visitable<Character>
* As above, but includes all creatures the player can detect well enough to target
* with ranged weapons, e.g. with infrared vision.
*/
std::vector<Creature *> get_targetable_creatures( int range ) const;
std::vector<Creature *> get_targetable_creatures( int range, bool melee ) const;
/** Returns an enumeration of visible mutations with colors */
std::string visible_mutations( int visibility_cap ) const;
player_activity get_destination_activity() const;
Expand Down
4 changes: 1 addition & 3 deletions src/ranged.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2332,9 +2332,7 @@ void target_ui::update_target_list( player &pc )
}

// Get targets in range and sort them by distance (targets[0] is the closest)
// FIXME: get_targetable_creatures does not consider some of the visible creatures
// as targets (e.g. those behind fences), but you can still see and shoot them
targets = pc.get_targetable_creatures( range );
targets = pc.get_targetable_creatures( range, mode == TargetMode::Reach );
std::sort( targets.begin(), targets.end(), [&]( const Creature * lhs, const Creature * rhs ) {
return rl_dist_exact( lhs->pos(), pc.pos() ) < rl_dist_exact( rhs->pos(), pc.pos() );
} );
Expand Down

0 comments on commit ac61da2

Please sign in to comment.