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"
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()

View file

@ -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<u32> = 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 {

View file

@ -1,3 +1,4 @@
use tracing::error;
use tracing::trace;
use tokio;
use blocks::Vec3;
@ -40,6 +41,7 @@ pub fn turtle_api() -> Router<SharedControl> {
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<SharedControl> {
.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(
Path(id): Path<u32>,
State(state): State<SharedControl>,