1
Fork 0

overhauled turtle client

This commit is contained in:
Andy Killorin 2023-12-19 19:21:08 -06:00
parent 25322aab0e
commit 5e9a99e030
Signed by: ank
GPG key ID: B6241CA3B552BCA4
3 changed files with 59 additions and 51 deletions

View file

@ -1,23 +1,8 @@
local function refuel()
turtle.select(16)
turtle.dropUp()
while turtle.getFuelLevel() ~= turtle.getFuelLimit() do
turtle.suck()
turtle.refuel()
end
end
local function dump()
for i = 1, 16, 1 do
turtle.select(i)
turtle.drop()
end
end
local port = "48228" local port = "48228"
local endpoint = "http://" .. ipaddr .. ":" .. port local endpoint = "http://" .. ipaddr .. ":" .. port
local function update(args) local args = {...}
local function update()
if args[1] == "nested" then if args[1] == "nested" then
-- no exec = stack overflow -- no exec = stack overflow
return false return false
@ -49,6 +34,49 @@ local function cycle(func, n)
return true return true
end end
local function cyclefn(fn)
return function (n)
cycle(fn, n)
end
end
local function iteminfo(slot)
return { ["Item"] = turtle.getItemDetail(slot) }
end
local function inventoryinfo()
return { ["Inventory"] = peripheral.wrap("front").list() }
end
local commands = {
["Wait"] = sleep,
["Forward"] = cyclefn(turtle.forward),
["Backward"] = cyclefn(turtle.backward),
["Up"] = cyclefn(turtle.up),
["Down"] = cyclefn(turtle.down),
["DropFront"] = turtle.dropfront,
["DropUp"] = turtle.dropup,
["DropDown"] = turtle.dropdown,
["SuckFront"] = turtle.suckfront,
["SuckUp"] = turtle.suckup,
["SuckDown"] = turtle.suckdown,
["Select"] = turtle.select,
["Refuel"] = turtle.refuel,
["ItemInfo"] = iteminfo,
["InventoryInfo"] = inventoryinfo,
["Left"] = turtle.turnLeft,
["Right"] = turtle.turnRight,
["Dig"] = turtle.dig,
["DigUp"] = turtle.digUp,
["DigDown"] = turtle.digDown,
["PlaceUp"] = turtle.placeUp,
["Place"] = turtle.place,
["PlaceDown"] = turtle.placeDown,
["Update"] = update,
["Poweroff"] = os.shutdown,
["GetFuelLimit"] = turtle.getFuelLimit,
};
if not ipaddr then if not ipaddr then
if fs.exists("/disk/ip") then if fs.exists("/disk/ip") then
local ipfile = fs.open("/disk/ip") local ipfile = fs.open("/disk/ip")
@ -116,36 +144,12 @@ repeat
local ret = nil local ret = nil
if command == "Wait" then if command then
sleep(args) ret = commands[command](args)
elseif command == "Forward" then end
ret = cycle(turtle.forward, args)
elseif command == "Backward" then if command == "Update" and ret == false then
ret = cycle(turtle.back, args) break
elseif command == "Left" then
ret = turtle.turnLeft()
elseif command == "Right" then
ret = turtle.turnRight()
elseif command == "Up" then
ret = cycle(turtle.up, args)
elseif command == "Down" then
ret = cycle(turtle.down, args)
elseif command == "Dig" then
ret = turtle.dig()
elseif command == "DigUp" then
ret = turtle.digUp()
elseif command == "DigDown" then
ret = turtle.digDown()
elseif command == "ItemInfo" then
ret = { Item = turtle.getItemDetail(args) }
elseif command == "Refuel" then
refuel()
elseif command == "Dump" then
dump()
elseif command == "Update" then
if not update({...}) then
break
end
end end
command = nil command = nil
@ -190,7 +194,6 @@ repeat
below = below, below = below,
ret = ret_table, ret = ret_table,
} }
print(info.ret)
local rsp = http.post( local rsp = http.post(
endpoint .. "/turtle/" .. id .. "/update" , endpoint .. "/turtle/" .. id .. "/update" ,
@ -206,3 +209,6 @@ repeat
backoff = backoff + 1 backoff = backoff + 1
end end
until command == "Poweroff" until command == "Poweroff"
::done:: -- I hate that this exists. What is this, NASM?
print("exited")

View file

@ -200,7 +200,7 @@ async fn cancel(
} }
async fn update_turtles(State(state): State<SharedControl>) -> &'static str { async fn update_turtles(State(state): State<SharedControl>) -> &'static str {
for turtle in state .write().await.turtles.iter_mut() { for turtle in state.read().await.turtles.iter() {
turtle.write().await.pending_update = true; turtle.write().await.pending_update = true;
} }

View file

@ -246,7 +246,7 @@ pub(crate) async fn process_turtle_update(
) -> anyhow::Result<TurtleCommand> { ) -> anyhow::Result<TurtleCommand> {
let mut turtle = state let mut turtle = state
.turtles .turtles
.get_mut(id as usize) .get(id as usize)
.context("nonexisting turtle")?.write().await; .context("nonexisting turtle")?.write().await;
let world = &mut state.world; let world = &mut state.world;
@ -329,13 +329,15 @@ pub(crate) enum TurtleCommand {
DropFront(u32), DropFront(u32),
DropUp(u32), DropUp(u32),
DropDown(u32), DropDown(u32),
SuckFront(u32),
SuckUp(u32),
SuckDown(u32),
Select(u32), Select(u32),
/// Slot in inventory /// Slot in inventory
ItemInfo(u32), ItemInfo(u32),
Update, Update,
Poweroff, Poweroff,
Refuel, Refuel,
Dump,
} }
#[derive(Serialize, Deserialize, Clone, Debug)] #[derive(Serialize, Deserialize, Clone, Debug)]