From 207f3b76714f41f68e4ea41b4d74515557661953 Mon Sep 17 00:00:00 2001 From: Llywelwyn Date: Wed, 30 Aug 2023 06:15:29 +0100 Subject: [PATCH] fixes traffic jams - if path is blocked, npcs will recalc path to dest --- src/ai/default_move_system.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/ai/default_move_system.rs b/src/ai/default_move_system.rs index ef2514c..b56987c 100644 --- a/src/ai/default_move_system.rs +++ b/src/ai/default_move_system.rs @@ -110,6 +110,19 @@ impl<'a> System<'a> for DefaultAI { is_telepath.dirty = true; } path.remove(0); + } else { + // If the path is blocked, recalculate a new path to the same waypoint. + let path = rltk::a_star_search( + map.xy_idx(pos.x, pos.y) as i32, + map.xy_idx( + (path[path.len() - 1] as i32) % map.width, + (path[path.len() - 1] as i32) / map.width + ) as i32, + &mut *map + ); + if path.success && path.steps.len() > 1 { + move_mode.mode = Movement::RandomWaypoint { path: Some(path.steps) }; + } } } else { move_mode.mode = Movement::RandomWaypoint { path: None };