From f95c9cea352ecbfa89af77942583592fcacde096 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Wed, 20 Dec 2023 02:09:31 -0600 Subject: [PATCH] goto more robust --- server/src/turtle.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/server/src/turtle.rs b/server/src/turtle.rs index 96450c7..3f9bea5 100644 --- a/server/src/turtle.rs +++ b/server/src/turtle.rs @@ -213,13 +213,26 @@ impl TurtleCommander { if world.occupied(next_position.pos).await { if world.garbage(next_position.pos).await { - self.execute(dbg!(recent.dig(next_position.pos))?).await; + match recent.dig(next_position.pos) { + Some(command) => self.execute(command).await, + None => break 'route, + }; } else { break 'route; } } - let state = self.execute(command).await; + let state = self.execute(command.clone()).await; + + if let TurtleCommandResponse::Failure = state.ret { + if let TurtleCommand::Backward(_) = command { + // turn around if you bump your rear on something + self.execute(TurtleCommand::Left).await; + recent = self.execute(TurtleCommand::Left).await.pos; + } + break 'route; + } + recent = state.pos; } } @@ -246,7 +259,11 @@ impl TurtleCommander { 'route: for (next_position, command) in route.into_iter().skip(1).zip(steps) { if world.occupied(next_position.pos).await { if world.garbage(next_position.pos).await { - self.execute(dbg!(recent.dig(next_position.pos))?).await; + let command = dbg!(recent.dig(next_position.pos)); + match command { + Some(command) => self.execute(command).await, + None => break 'route, + }; } else { break 'route; }