From e6e873c91d87c87f781231c774aab1e012f25cae Mon Sep 17 00:00:00 2001 From: ZhilkinSerg Date: Sun, 20 Sep 2020 06:00:57 +0300 Subject: [PATCH] Do not move monster that can't move (#44242) --- src/monmove.cpp | 50 ++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/src/monmove.cpp b/src/monmove.cpp index 952f873f7fd47..becf95e81e4e6 100644 --- a/src/monmove.cpp +++ b/src/monmove.cpp @@ -821,29 +821,41 @@ void monster::move() bool moved = false; tripoint destination; + bool try_to_move = false; + for( const tripoint &dest : here.points_in_radius( pos(), 1 ) ) { + if( dest != pos() ) { + if( can_move_to( dest ) && + g->critter_at( dest, true ) == nullptr ) { + try_to_move = true; + break; + } + } + } // If true, don't try to greedily avoid locally bad paths bool pathed = false; - if( !wander() ) { - while( !path.empty() && path.front() == pos() ) { - path.erase( path.begin() ); - } + if( try_to_move ) { + if( !wander() ) { + while( !path.empty() && path.front() == pos() ) { + path.erase( path.begin() ); + } - const auto &pf_settings = get_pathfinding_settings(); - if( pf_settings.max_dist >= rl_dist( pos(), goal ) && - ( path.empty() || rl_dist( pos(), path.front() ) >= 2 || path.back() != goal ) ) { - // We need a new path - path = here.route( pos(), goal, pf_settings, get_path_avoid() ); - } + const auto &pf_settings = get_pathfinding_settings(); + if( pf_settings.max_dist >= rl_dist( pos(), goal ) && + ( path.empty() || rl_dist( pos(), path.front() ) >= 2 || path.back() != goal ) ) { + // We need a new path + path = here.route( pos(), goal, pf_settings, get_path_avoid() ); + } - // Try to respect old paths, even if we can't pathfind at the moment - if( !path.empty() && path.back() == goal ) { - destination = path.front(); - moved = true; - pathed = true; - } else { - // Straight line forward, probably because we can't pathfind (well enough) - destination = goal; - moved = true; + // Try to respect old paths, even if we can't pathfind at the moment + if( !path.empty() && path.back() == goal ) { + destination = path.front(); + moved = true; + pathed = true; + } else { + // Straight line forward, probably because we can't pathfind (well enough) + destination = goal; + moved = true; + } } } if( !moved && has_flag( MF_SMELLS ) ) {