aboutsummaryrefslogtreecommitdiff
path: root/server/bot/src/pathfinding.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/bot/src/pathfinding.rs')
-rw-r--r--server/bot/src/pathfinding.rs31
1 files changed, 21 insertions, 10 deletions
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<Vec2>);
+pub struct Path {
+ segments: Vec<Vec2>,
+ 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<IVec2>, 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<IVec2>, from: IVec2, to: IVec2) -> Option<Path> {
#[derive(Debug, PartialEq, Eq)]
@@ -94,15 +102,18 @@ pub fn find_path(walkable: &HashSet<IVec2>, from: IVec2, to: IVec2) -> Option<Pa
}
}
- let mut path = Vec::new();
+ let mut segments = Vec::new();
let mut c = to;
loop {
- path.push(c.as_vec2() + 0.5);
+ segments.push(c.as_vec2() + 0.5);
let cn = visited[&c];
if cn == c {
break;
}
c = cn
}
- Some(Path(path))
+ Some(Path {
+ segments,
+ seg_time: 0.,
+ })
}