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()
2024-01-12 22:04:57 +03:00
wlan.transmit(9998, 9999, {
2024-01-12 21:15:52 +03:00
["_"] = "WakeUp",
["from"] = ID
})
parallel.waitForAll(function()
while true do
local _, side, chan, rchan, data, dist = os.pullEvent("modem_message")
2024-01-12 22:04:57 +03:00
if chan == 9999 and rchan == 9998 then
2024-01-12 21:15:52 +03:00
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)