diff options
Diffstat (limited to 'server/bot/src')
-rw-r--r-- | server/bot/src/algos/customer.rs | 12 | ||||
-rw-r--r-- | server/bot/src/algos/simple.rs | 2 | ||||
-rw-r--r-- | server/bot/src/algos/test.rs | 4 | ||||
-rw-r--r-- | server/bot/src/algos/waiter.rs | 2 | ||||
-rw-r--r-- | server/bot/src/pathfinding.rs | 31 |
5 files changed, 33 insertions, 18 deletions
diff --git a/server/bot/src/algos/customer.rs b/server/bot/src/algos/customer.rs index 556f5ce5..9c0ce210 100644 --- a/server/bot/src/algos/customer.rs +++ b/server/bot/src/algos/customer.rs @@ -58,7 +58,6 @@ impl Default for Customer { impl BotAlgo for Customer { fn tick(&mut self, me: PlayerID, game: &Game, dt: f32) -> BotInput { - let _ = (me, game, dt); let Some(playerdata) = game.players.get(&me) else { return BotInput::default(); }; @@ -106,9 +105,14 @@ impl BotAlgo for Customer { }], ..Default::default() } + } else if path.is_stuck() { + if let Some(path) = find_path(&game.walkable, pos.as_ivec2(), *origin) { + *self = Customer::Exiting { path }; + } + BotInput::default() } else { BotInput { - direction: path.next_direction(pos) * 0.5, + direction: path.next_direction(pos, dt) * 0.5, ..Default::default() } } @@ -252,7 +256,7 @@ impl BotAlgo for Customer { } } Customer::Exiting { path } => { - if path.is_done() { + if path.is_done() || path.is_stuck() { info!("{me:?} -> leave"); BotInput { leave: true, @@ -260,7 +264,7 @@ impl BotAlgo for Customer { } } else { BotInput { - direction: path.next_direction(pos) * 0.5, + direction: path.next_direction(pos, dt) * 0.5, ..Default::default() } } diff --git a/server/bot/src/algos/simple.rs b/server/bot/src/algos/simple.rs index 6092e772..1be2cddc 100644 --- a/server/bot/src/algos/simple.rs +++ b/server/bot/src/algos/simple.rs @@ -54,7 +54,7 @@ impl BotAlgo for Simple { } if let Some((path, target, down)) = &mut self.path { - let direction = path.next_direction(pos); + let direction = path.next_direction(pos, dt); let arrived = path.is_done(); let target = *target; if arrived { diff --git a/server/bot/src/algos/test.rs b/server/bot/src/algos/test.rs index dee4cb88..a47befa9 100644 --- a/server/bot/src/algos/test.rs +++ b/server/bot/src/algos/test.rs @@ -29,14 +29,14 @@ pub struct Test { } impl BotAlgo for Test { - fn tick(&mut self, me: PlayerID, game: &Game, _dt: f32) -> BotInput { + fn tick(&mut self, me: PlayerID, game: &Game, dt: f32) -> BotInput { let Some(player) = game.players.get(&me) else { return BotInput::default(); }; let pos = player.movement.position; if let Some(path) = &mut self.path { - let direction = path.next_direction(pos); + let direction = path.next_direction(pos, dt); return BotInput { direction, boost: false, diff --git a/server/bot/src/algos/waiter.rs b/server/bot/src/algos/waiter.rs index ced3097b..adeded23 100644 --- a/server/bot/src/algos/waiter.rs +++ b/server/bot/src/algos/waiter.rs @@ -42,7 +42,7 @@ impl BotAlgo for Waiter { } if let Some((path, target, down)) = &mut self.path { - let direction = path.next_direction(pos); + let direction = path.next_direction(pos, dt); let arrived = path.is_done(); let target = *target; if arrived { 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., + }) } |