1
0
Fork 0
cc-stuff/turtos/main.lua

61 lines
2.4 KiB
Lua
Raw Normal View History

2024-01-12 21:15:52 +03:00
local wlan = peripheral.find("modem", function(addr, modem)
return modem.isWireless()
end)
if not wlan then error("no wireless interface") end
wlan.open(9999)
local ID = os.getComputerID()
wlan.transmit(9999, 9999, {
["_"] = "WakeUp",
["from"] = ID
})
parallel.waitForAll(function()
while true do
local _, side, chan, rchan, data, dist = os.pullEvent("modem_message")
if chan == 9999 and chan == rchan then
if data._ == "move" then
wlan.transmit(rchan, chan, { ["_"] = "Ack", ["from"] = ID })
if data.tgt == nil or data.tgt == ID then
local out = { false, "Invalid direction: " .. data.dir }
if data.dig then
if data.dir == "fwd" and turtle.detect() then turtle.dig()
elseif data.dir == "down" and turtle.detectDown() then turtle.digDown()
elseif data.dir == "up" and turtle.detectUp() then turtle.digUp()
end
end
if data.dir == "fwd" then out = { turtle.forward() }
elseif data.dir == "bck" then out = { turtle.back() }
elseif data.dir == "up" then out = { turtle.up() }
elseif data.dir == "down" then out = { turtle.down() }
elseif data.dir == "rotl" then out = { turtle.turnLeft() }
elseif data.dir == "rotr" then out = { turtle.turnRight() }
end
if not out[1] then
wlan.transmit(rchan, chan, {
["_"] = "Error",
["from"] = ID,
["error"] = out[2]
})
else
wlan.transmit(rchan, chan, {
["_"] = "Result",
["from"] = ID,
["out"] = out
})
end
end
elseif data._ == "shutdown" then
wlan.transmit(rchan, chan, { ["_"] = "Ack", ["from"] = ID })
os.shutdown()
elseif data._ == "reboot" then
wlan.transmit(rchan, chan, { ["_"] = "Ack", ["from"] = ID })
os.reboot()
end
end
end
end)