From 63afb0f037c3104028a47a2af07f02926b757bab Mon Sep 17 00:00:00 2001 From: scarf Date: Wed, 20 Sep 2023 04:00:11 +0900 Subject: [PATCH] fix: turrets aiming moving vehicle it cannot see (#3180) * refactor: unnest ifs * fix: turrets aiming moving vehicle it cannot see --- src/mattack_actors.cpp | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/mattack_actors.cpp b/src/mattack_actors.cpp index 2fd194a790ae..5d9741bad7c6 100644 --- a/src/mattack_actors.cpp +++ b/src/mattack_actors.cpp @@ -486,37 +486,45 @@ int gun_actor::get_max_range() const } return max_range; } +namespace +{ -static std::optional find_target_vehicle( monster &z, int range ) +auto find_target_vehicle( monster &z, int range ) -> std::optional { + const auto is_different_plane = []( const wrapped_vehicle & v, const monster & m ) -> bool { + return !fov_3d && v.pos.z != m.pos().z; + }; + map &here = get_map(); bool found = false; tripoint aim_at; for( wrapped_vehicle &v : here.get_vehicles() ) { - if( ( !fov_3d && v.pos.z != z.pos().z ) || v.v->velocity == 0 ) { + if( is_different_plane( v, z ) || v.v->velocity == 0 ) { continue; } bool found_controls = false; for( const vpart_reference &vp : v.v->get_avail_parts( "CONTROLS" ) ) { - if( z.sees( vp.pos() ) ) { - int new_dist = rl_dist( z.pos(), vp.pos() ); - if( new_dist <= range ) { - - aim_at = vp.pos(); - range = new_dist; - found = true; - found_controls = true; - } + if( !z.sees( vp.pos() ) ) { + continue; + } + + int new_dist = rl_dist( z.pos(), vp.pos() ); + if( new_dist <= range ) { + aim_at = vp.pos(); + range = new_dist; + found = true; + found_controls = true; } } + if( !found_controls ) { std::vector line = here.find_clear_path( z.pos(), v.v->global_pos3() ); tripoint prev_point = z.pos(); for( tripoint &i : line ) { - if( here.floor_between( prev_point, i ) ) { + if( !z.sees( i ) || here.floor_between( prev_point, i ) ) { break; } optional_vpart_position vp = here.veh_at( i ); @@ -543,6 +551,9 @@ static std::optional find_target_vehicle( monster &z, int range ) } } +} // namespace + + bool gun_actor::call( monster &z ) const { Creature *target;