1
Fork 0

implemented birth (child)

This commit is contained in:
Andy Killorin 2023-12-26 21:28:25 -06:00
parent 6f01b486ae
commit 722e58d7ad
Signed by: ank
GPG key ID: B6241CA3B552BCA4
3 changed files with 68 additions and 18 deletions

View file

@ -5,6 +5,21 @@ else
endpoint = endpoint .. "48228" endpoint = endpoint .. "48228"
end 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 args = {...}
local function update() local function update()
if args[1] == "nested" then if args[1] == "nested" then
@ -44,8 +59,8 @@ local function iteminfo(slot)
end end
local function restartfront() local function restartfront()
front = peripheral.wrap("front") local front = peripheral.wrap("front")
if not front.shutdown then if not front or not front.shutdown then
return false return false
end end
front.shutdown() front.shutdown()
@ -150,26 +165,29 @@ local backoff = 0;
if not idfile then if not idfile then
local fuel = turtle.getFuelLevel() local fuel = turtle.getFuelLevel()
local maxfuel = turtle.getFuelLimit() local maxfuel = turtle.getFuelLimit()
if fs.exists("/disk/pos") then if not startpos then
io.input("/disk/pos")
else
io.input(io.stdin) 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 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 = { local info = {
fuel = fuel, fuel = fuel,
fuellimit = maxfuel, fuellimit = maxfuel,
position = {x, y, z}, position = startpos.pos,
facing = direction, facing = startpos.dir,
} }
::request:: ::request::
local turtleinfo = http.post( local turtleinfo = http.post(
@ -193,6 +211,19 @@ if not idfile then
else else
id = idfile.readAll() id = idfile.readAll()
idfile.close() 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 end
term.clear() term.clear()

View file

@ -76,7 +76,7 @@ pub async fn mine_chunk_and_sweep(turtle: TurtleCommander, pos: Vec3, chunk: Vec
async fn devore(turtle: &TurtleCommander) { async fn devore(turtle: &TurtleCommander) {
let turtles: Vec<u32> = turtle.inventory().await.into_iter().enumerate() let turtles: Vec<u32> = turtle.inventory().await.into_iter().enumerate()
.filter(|(_,b)| b.as_ref().is_some_and(|b| b.name.contains("turtle"))) .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() { if turtles.is_empty() {
return; return;
@ -90,7 +90,8 @@ async fn devore(turtle: &TurtleCommander) {
let staging = position.pos - position.dir.unit(); let staging = position.pos - position.dir.unit();
turtle.goto(Position::new(staging, position.dir)).await; 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(Place).await;
turtle.execute(CycleFront).await; turtle.execute(CycleFront).await;
loop { loop {

View file

@ -1,3 +1,4 @@
use tracing::error;
use tracing::trace; use tracing::trace;
use tokio; use tokio;
use blocks::Vec3; use blocks::Vec3;
@ -40,6 +41,7 @@ pub fn turtle_api() -> Router<SharedControl> {
Router::new() Router::new()
.route("/new", post(create_turtle)) .route("/new", post(create_turtle))
.route("/:id/update", post(command)) .route("/:id/update", post(command))
.route("/:id/setPosition", post(update_position))
.route("/client.lua", get(client)) .route("/client.lua", get(client))
.route("/:id/setGoal", post(set_goal)) .route("/:id/setGoal", post(set_goal))
.route("/:id/cancelTask", post(cancel)) .route("/:id/cancelTask", post(cancel))
@ -55,6 +57,22 @@ pub fn turtle_api() -> Router<SharedControl> {
.route("/updateAll", get(update_turtles)) .route("/updateAll", get(update_turtles))
} }
pub(crate) async fn update_position(
Path(id): Path<u32>,
State(state): State<SharedControl>,
Json(req): Json<Position>,
) -> &'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( pub(crate) async fn register_turtle(
Path(id): Path<u32>, Path(id): Path<u32>,
State(state): State<SharedControl>, State(state): State<SharedControl>,