diff --git a/server/src/paths.rs b/server/src/paths.rs index f9eb540..b2e7cfe 100644 --- a/server/src/paths.rs +++ b/server/src/paths.rs @@ -1,18 +1,30 @@ use std::rc::Rc; use crate::{ - blocks::{Block, World, Position, Direction, Vec3, WorldReadLock}, + blocks::{Block, World, Position, Direction, Vec3, WorldReadLock}, turtle::TurtleCommand, }; use pathfinding::prelude::astar; +pub async fn route_facing(from: Position, to: Position, world: &World) -> Option> { + let facing = |p: &Position| { + let ahead = p.dir.unit() + p.pos; + to.pos == ahead + }; + route_to(from, to.pos, facing, world).await +} pub async fn route(from: Position, to: Position, world: &World) -> Option> { + route_to(from, to.pos, |p| p == &to, world).await +} + +async fn route_to(from: Position, to: Vec3, done: D, world: &World) -> Option> +where D: FnMut(&Position) -> bool { // lock once, we'll be doing a lot of lookups let world = world.clone().lock().await; // attempt at not crashing by looking infinitely into the abyss if world - .locate_at_point(&to.pos.into()) + .locate_at_point(&to.into()) .is_some_and(|b| difficulty(&b.name).is_none()) { return None; @@ -20,8 +32,8 @@ pub async fn route(from: Position, to: Position, world: &World) -> Option