From a868f49b41c30daca83de86f982ffed431d3e891 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Thu, 15 Aug 2024 20:26:15 +0200 Subject: customers leave if they get stuck walking --- server/bot/src/pathfinding.rs | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'server/bot/src/pathfinding.rs') diff --git a/server/bot/src/pathfinding.rs b/server/bot/src/pathfinding.rs index e6457cff..593b5900 100644 --- a/server/bot/src/pathfinding.rs +++ b/server/bot/src/pathfinding.rs @@ -23,14 +23,19 @@ use std::{ }; #[derive(Debug, Clone)] -pub struct Path(Vec); +pub struct Path { + segments: Vec, + seg_time: f32, +} impl Path { - pub fn next_direction(&mut self, position: Vec2) -> Vec2 { - if let Some(next) = self.0.last().copied() { + pub fn next_direction(&mut self, position: Vec2, dt: f32) -> Vec2 { + if let Some(next) = self.segments.last().copied() { trace!("next {next}"); - if next.distance(position) < if self.0.len() == 1 { 0.1 } else { 0.6 } { - self.0.pop(); + self.seg_time += dt; + if next.distance(position) < if self.segments.len() == 1 { 0.1 } else { 0.6 } { + self.seg_time = 0.; + self.segments.pop(); } (next - position).normalize_or_zero() } else { @@ -38,7 +43,10 @@ impl Path { } } pub fn is_done(&self) -> bool { - self.0.is_empty() + self.segments.is_empty() + } + pub fn is_stuck(&self) -> bool { + self.seg_time > 5. } } @@ -52,7 +60,7 @@ pub fn find_path_to_neighbour(walkable: &HashSet, from: IVec2, to: IVec2) } } } - paths.into_iter().min_by_key(|p| p.0.len()) + paths.into_iter().min_by_key(|p| p.segments.len()) } pub fn find_path(walkable: &HashSet, from: IVec2, to: IVec2) -> Option { #[derive(Debug, PartialEq, Eq)] @@ -94,15 +102,18 @@ pub fn find_path(walkable: &HashSet, from: IVec2, to: IVec2) -> Option