From 722e58d7ad86b14a2e9dd137722b22f466f8ee0f Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Tue, 26 Dec 2023 21:28:25 -0600 Subject: [PATCH] implemented birth (child) --- client/client.lua | 63 ++++++++++++++++++++++++++++++---------- server/src/mine.rs | 5 ++-- server/src/turtle_api.rs | 18 ++++++++++++ 3 files changed, 68 insertions(+), 18 deletions(-) diff --git a/client/client.lua b/client/client.lua index b6634e9..1aae16d 100644 --- a/client/client.lua +++ b/client/client.lua @@ -5,6 +5,21 @@ else endpoint = endpoint .. "48228" end +local startpos = nil + +if fs.exists("/disk/pos") then + local posfile = fs.open("/disk/pos", "r") + local direction = posfile.readLine() + local x = tonumber(posfile.readLine()) + local y = tonumber(posfile.readLine()) + local z = tonumber(posfile.readLine()) + + startpos = { + pos = {x, y, z}, + dir = direction + } +end + local args = {...} local function update() if args[1] == "nested" then @@ -44,8 +59,8 @@ local function iteminfo(slot) end local function restartfront() - front = peripheral.wrap("front") - if not front.shutdown then + local front = peripheral.wrap("front") + if not front or not front.shutdown then return false end front.shutdown() @@ -150,26 +165,29 @@ local backoff = 0; if not idfile then local fuel = turtle.getFuelLevel() local maxfuel = turtle.getFuelLimit() - if fs.exists("/disk/pos") then - io.input("/disk/pos") - else + if not startpos then io.input(io.stdin) + local startpos = io.input() + print("Direction (North, South, East, West):") + local direction = startpos:read("l") + print("X:") + local x = tonumber(startpos:read("l")) + print("Y:") + local y = tonumber(startpos:read("l")) + print("Z:") + local z = tonumber(startpos:read("l")) + + startpos = { + pos = {x, y, z}, + dir = direction + } end - local startpos = io.input() - print("Direction (North, South, East, West):") - local direction = startpos:read("l") - print("X:") - local x = tonumber(startpos:read("l")) - print("Y:") - local y = tonumber(startpos:read("l")) - print("Z:") - local z = tonumber(startpos:read("l")) local info = { fuel = fuel, fuellimit = maxfuel, - position = {x, y, z}, - facing = direction, + position = startpos.pos, + facing = startpos.dir, } ::request:: local turtleinfo = http.post( @@ -193,6 +211,19 @@ if not idfile then else id = idfile.readAll() idfile.close() + + if startpos then + local rsp = http.post( + endpoint .. "/turtle/" .. id .. "/setPosition", + textutils.serializeJSON(startpos), + { ["Content-Type"] = "application/json" } + ) + if rsp then + rsp.readAll() + else + print("failed to update position") -- blink and you'll miss it + end + end end term.clear() diff --git a/server/src/mine.rs b/server/src/mine.rs index 73ff8a7..71c505f 100644 --- a/server/src/mine.rs +++ b/server/src/mine.rs @@ -76,7 +76,7 @@ pub async fn mine_chunk_and_sweep(turtle: TurtleCommander, pos: Vec3, chunk: Vec async fn devore(turtle: &TurtleCommander) { let turtles: Vec = turtle.inventory().await.into_iter().enumerate() .filter(|(_,b)| b.as_ref().is_some_and(|b| b.name.contains("turtle"))) - .map(|(i,_)| i as u32).collect(); + .map(|(i,_)| (i + 1) as u32).collect(); if turtles.is_empty() { return; @@ -90,7 +90,8 @@ async fn devore(turtle: &TurtleCommander) { let staging = position.pos - position.dir.unit(); turtle.goto(Position::new(staging, position.dir)).await; - turtle.execute(Select(i as u32)).await; + warn!("devoring {i}"); + turtle.execute(Select(i)).await; turtle.execute(Place).await; turtle.execute(CycleFront).await; loop { diff --git a/server/src/turtle_api.rs b/server/src/turtle_api.rs index e7dace0..1ade78b 100644 --- a/server/src/turtle_api.rs +++ b/server/src/turtle_api.rs @@ -1,3 +1,4 @@ +use tracing::error; use tracing::trace; use tokio; use blocks::Vec3; @@ -40,6 +41,7 @@ pub fn turtle_api() -> Router { Router::new() .route("/new", post(create_turtle)) .route("/:id/update", post(command)) + .route("/:id/setPosition", post(update_position)) .route("/client.lua", get(client)) .route("/:id/setGoal", post(set_goal)) .route("/:id/cancelTask", post(cancel)) @@ -55,6 +57,22 @@ pub fn turtle_api() -> Router { .route("/updateAll", get(update_turtles)) } +pub(crate) async fn update_position( + Path(id): Path, + State(state): State, + Json(req): Json, +) -> &'static str { + let state = &mut state.read().await; + let turtle = state.turtles.get(id as usize); + if let Some(turtle) = turtle { + turtle.write().await.position = req; + info!("updated position"); + } else { + error!("position update failed"); + } + + "ACK" +} pub(crate) async fn register_turtle( Path(id): Path, State(state): State,