1
Fork 0

added deque for moves

This commit is contained in:
Andy Killorin 2023-12-17 10:15:32 -06:00
parent 3f5a258ea9
commit 1f4a5a2c9b
Signed by: ank
GPG key ID: B6241CA3B552BCA4
2 changed files with 24 additions and 10 deletions

View file

@ -123,7 +123,7 @@ repeat
} }
local rsp = http.post( local rsp = http.post(
endpoint .. "/turtle/update/" .. id, endpoint .. "/turtle/" .. id .. "/update" ,
textutils.serializeJSON(info), textutils.serializeJSON(info),
{ ["Content-Type"] = "application/json" } { ["Content-Type"] = "application/json" }
) )

View file

@ -63,7 +63,7 @@ impl Direction {
} }
} }
#[derive(Serialize, Deserialize, Clone)] #[derive(Serialize, Deserialize)]
struct Turtle { struct Turtle {
name: Name, name: Name,
fuel: usize, fuel: usize,
@ -72,11 +72,12 @@ struct Turtle {
position: Position, position: Position,
goal: Option<Position>, goal: Option<Position>,
pending_update: bool, pending_update: bool,
moves: VecDeque<TurtleCommand>,
} }
impl Turtle { impl Turtle {
fn new(id: u32, position: Vec3, facing: Direction, fuel: usize) -> Self { fn new(id: u32, position: Vec3, facing: Direction, fuel: usize) -> Self {
Self { name: Name::from_num(id), fuel, queued_movement: Vec3::new(0, 0, 0), position: (position, facing), goal: None, pending_update: true } Self { name: Name::from_num(id), fuel, queued_movement: Vec3::new(0, 0, 0), position: (position, facing), goal: None, pending_update: true, moves: VecDeque::new() }
} }
} }
@ -109,10 +110,10 @@ async fn main() -> Result<(), Error> {
let serv = Router::new() let serv = Router::new()
.route("/turtle/new", post(create_turtle)) .route("/turtle/new", post(create_turtle))
.route("/turtle/update/:id", post(command)) .route("/turtle/:id/update", post(command))
.route("/turtle/client.lua", get(client)) .route("/turtle/client.lua", get(client))
.route("/turtle/setGoal/:id", post(set_goal)) .route("/turtle/:id/setGoal", post(set_goal))
.route("/turtle/info/:id", get(turtle_info)) .route("/turtle/:id/info", get(turtle_info))
.route("/turtle/updateAll", get(update_turtles)) .route("/turtle/updateAll", get(update_turtles))
.route("/flush", get(flush)) .route("/flush", get(flush))
.with_state(state.clone()); .with_state(state.clone());
@ -227,8 +228,22 @@ async fn turtle_info(
State(state): State<SharedControl>, State(state): State<SharedControl>,
) -> Json<Turtle> { ) -> Json<Turtle> {
let state = &mut state.read().await; let state = &mut state.read().await;
let turtle = &state.turtles[id as usize];
Json(state.turtles[id as usize].clone()) let mut pseudomoves: VecDeque<TurtleCommand> = VecDeque::new();
turtle.moves.front().map(|m| pseudomoves.push_front(m.clone()));
let cloned = Turtle {
name: turtle.name.clone(),
fuel: turtle.fuel,
queued_movement: turtle.queued_movement.clone(),
position: turtle.position.clone(),
goal: turtle.goal.clone(),
pending_update: turtle.pending_update,
moves: pseudomoves,
};
Json(cloned)
} }
async fn command( async fn command(
@ -266,6 +281,7 @@ fn process_turtle_update(
turtle.fuel = update.fuel; turtle.fuel = update.fuel;
turtle.position.0 += turtle.queued_movement; turtle.position.0 += turtle.queued_movement;
turtle.queued_movement = Vec3::zeros();
} }
let above = Block { let above = Block {
@ -289,8 +305,6 @@ fn process_turtle_update(
world.remove_at_point(&below.pos.into()); world.remove_at_point(&below.pos.into());
world.insert(below); world.insert(below);
turtle.queued_movement = turtle.position.1.clone().unit();
if turtle.goal.is_some_and(|g| g == turtle.position) { if turtle.goal.is_some_and(|g| g == turtle.position) {
turtle.goal = None; turtle.goal = None;
} }
@ -373,7 +387,7 @@ enum TurtleMineMethod {
Strip, Strip,
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize, Clone)]
enum TurtleCommand { enum TurtleCommand {
Wait, Wait,
Forward, Forward,