make goto fallible
This commit is contained in:
parent
1a232d5c08
commit
7cb74f83f8
2 changed files with 52 additions and 6 deletions
|
@ -9,7 +9,7 @@ use tokio::sync::{RwLock, OwnedRwLockReadGuard};
|
||||||
|
|
||||||
use crate::{turtle::TurtleCommand, paths::{self, TRANSPARENT}};
|
use crate::{turtle::TurtleCommand, paths::{self, TRANSPARENT}};
|
||||||
|
|
||||||
const CHUNK_SIZE: usize = 4;
|
const CHUNK_SIZE: usize = 8;
|
||||||
const CHUNK_VOLUME: usize = CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE;
|
const CHUNK_VOLUME: usize = CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE;
|
||||||
const CHUNK_VEC: Vec3 = Vec3::new(CHUNK_SIZE as i32, CHUNK_SIZE as i32, CHUNK_SIZE as i32);
|
const CHUNK_VEC: Vec3 = Vec3::new(CHUNK_SIZE as i32, CHUNK_SIZE as i32, CHUNK_SIZE as i32);
|
||||||
|
|
||||||
|
@ -362,4 +362,35 @@ mod tests {
|
||||||
fn positive_many_bench(b: &mut Bencher) {
|
fn positive_many_bench(b: &mut Bencher) {
|
||||||
b.iter(||many(Vec3::new(1212,100,1292), Vec3::new(50, 50, 50)));
|
b.iter(||many(Vec3::new(1212,100,1292), Vec3::new(50, 50, 50)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[bench]
|
||||||
|
fn positive_many_get(b: &mut Bencher) {
|
||||||
|
let point = Vec3::new(1212,100,1292);
|
||||||
|
let size = Vec3::new(50,50,50);
|
||||||
|
let mut world = World::new();
|
||||||
|
for i in 0..size.product() {
|
||||||
|
let block = fill(size, i) + point;
|
||||||
|
world.set(Block { name: i.to_string(), pos: block});
|
||||||
|
}
|
||||||
|
|
||||||
|
b.iter(|| {
|
||||||
|
for i in 0..size.product() {
|
||||||
|
let block = fill(size, i) + point;
|
||||||
|
assert_eq!(i.to_string(), world.get(block).unwrap().name)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[bench]
|
||||||
|
fn positive_many_set(b: &mut Bencher) {
|
||||||
|
let point = Vec3::new(1212,100,1292);
|
||||||
|
let size = Vec3::new(50,50,50);
|
||||||
|
b.iter(|| {
|
||||||
|
for i in 0..size.product() {
|
||||||
|
let mut world = World::new();
|
||||||
|
let block = fill(size, i) + point;
|
||||||
|
world.set(Block { name: i.to_string(), pos: block});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,6 +41,8 @@ use super::paths::route;
|
||||||
const COMMAND_TIMEOUT: u64 = 0o372;
|
const COMMAND_TIMEOUT: u64 = 0o372;
|
||||||
/// Time (s) between turtle polls when idle
|
/// Time (s) between turtle polls when idle
|
||||||
pub const IDLE_TIME: u32 = 3;
|
pub const IDLE_TIME: u32 = 3;
|
||||||
|
/// Times to attempt a route before giving up
|
||||||
|
pub const RETRIES: usize = 42;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub(crate) struct Turtle {
|
pub(crate) struct Turtle {
|
||||||
|
@ -253,14 +255,20 @@ impl TurtleCommander {
|
||||||
pub async fn goto(&self, pos: Position) -> Option<()> {
|
pub async fn goto(&self, pos: Position) -> Option<()> {
|
||||||
let mut recent = self.pos().await;
|
let mut recent = self.pos().await;
|
||||||
let world = self.world.clone();
|
let world = self.world.clone();
|
||||||
|
let mut attempts = RETRIES + 1;
|
||||||
loop {
|
loop {
|
||||||
if recent == pos {
|
if recent == pos {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// easiest way to not eventually take over all memory
|
attempts -= 1;
|
||||||
let routing = timeout(Duration::from_secs(2), route(recent, pos, &world));
|
if attempts == 0 {
|
||||||
let route = routing.await.ok()??;
|
error!("goto {pos:?} failed");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
let routing = route(recent, pos, &world);
|
||||||
|
let route = routing.await?;
|
||||||
|
|
||||||
trace!("using route: {route:#?}");
|
trace!("using route: {route:#?}");
|
||||||
|
|
||||||
|
@ -302,6 +310,7 @@ impl TurtleCommander {
|
||||||
pub async fn goto_adjacent(&self, pos: Vec3) -> Option<Position> {
|
pub async fn goto_adjacent(&self, pos: Vec3) -> Option<Position> {
|
||||||
let mut recent = self.pos().await;
|
let mut recent = self.pos().await;
|
||||||
let world = self.world.clone();
|
let world = self.world.clone();
|
||||||
|
let mut attempts = RETRIES +1;
|
||||||
loop {
|
loop {
|
||||||
|
|
||||||
if pos == recent.dir.unit() + recent.pos
|
if pos == recent.dir.unit() + recent.pos
|
||||||
|
@ -311,8 +320,14 @@ impl TurtleCommander {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
let routing = timeout(Duration::from_secs(1), route_facing(recent, pos, &world));
|
attempts -= 1;
|
||||||
let route = routing.await.ok()??;
|
if attempts == 0 {
|
||||||
|
error!("goto {pos:?} failed");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
let routing = route_facing(recent, pos, &world);
|
||||||
|
let route = routing.await?;
|
||||||
|
|
||||||
let steps: Vec<TurtleCommand> = route.iter().map_windows(|[from,to]| from.difference(**to).unwrap()).collect();
|
let steps: Vec<TurtleCommand> = route.iter().map_windows(|[from,to]| from.difference(**to).unwrap()).collect();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue