Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local m = peripheral.find("modem")
- local senderID = os.getComputerLabel() == nil and "ID:" .. os.getComputerID() or os.getComputerLabel()
- local channels = {
- ["stdout"] = 1000,
- ["stdin"] = 1001,
- ["witherout"] = 1010,
- ["witherin"] = 1011
- }
- m.open(channels.stdin)
- local xTime = os.clock()
- local function checkStop()
- os.startTimer(0.15)
- local a, b, c, d, e
- repeat
- evt, face, sendCh, replyCh, msg, dist = os.pullEvent()
- if evt == "modem_message" and msg.type == "cmd" and msg.cmd == "sigterm" and (msg.receiver == nil or (type(msg.receiver) == "string" and string.match(senderID, msg.receiver))) then
- return true
- end
- until evt == "timer"
- return false
- end
- local function log(...)
- m.transmit(channels.stdout, channels.stdin,
- {
- ["program"] = "control",
- ["type"] = "info",
- ["text"] = {...},
- ["sender"] = senderID
- })
- end
- local function error(...)
- m.transmit(channels.stdout, channels.stdin,
- {
- ["program"] = "control",
- ["type"] = "error",
- ["text"] = {...},
- ["sender"] = senderID
- })
- end
- local dig = false
- local digup = false
- local digdown = false
- local persist = false
- local function tryRefuel(amount)
- amount = type(amount) == "number" and amount or 1
- if turtle.getFuelLevel() < amount then
- for i = 1, 16 do
- turtle.select(i)
- turtle.refuel(1)
- if turtle.getFuelLevel() >= amount then
- break
- end
- end
- turtle.select(1)
- if turtle.getFuelLevel() < amount then
- error("Out of fuel.")
- return false
- end
- end
- return true
- end
- local function up()
- while not turtle.up() do
- if dig then
- turtle.digUp()
- elseif not persist or checkStop() then
- return false
- end
- end
- return true
- end
- local function down()
- while not turtle.down() do
- if digdown then
- turtle.digDown()
- elseif not persist or checkStop() then
- return false
- end
- end
- return true
- end
- local function forward()
- while not turtle.forward() do
- if dig then
- turtle.dig()
- elseif not persist or checkStop() then
- return false
- end
- end
- if digup then
- turtle.digUp()
- end
- if digdown then
- turtle.digDown()
- end
- return true
- end
- local function back()
- while not turtle.back() do
- if not persist or checkStop() then
- return false
- end
- end
- return true
- end
- while true do
- evt, face, sendCh, replyCh, msg, dist = os.pullEvent("modem_message")
- if type(msg) == "table" and msg.program == "control" and (msg.receiver == nil or (type(msg.receiver) == "string" and string.match(senderID, msg.receiver))) then
- if msg.type == "cmd" then
- if msg.cmd == "run" then
- shell.run(unpack(msg.text))
- elseif msg.cmd == "set" then
- if msg.text[1] == "dig" then
- dig = msg.text[2] == "on"
- elseif msg.text[1] == "digup" then
- digup = msg.text[2] == "on"
- elseif msg.text[1] == "digdown" then
- digdown = msg.text[2] == "on"
- elseif msg.text[1] == "persist" then
- persist = msg.text[2] == "on"
- end
- elseif msg.cmd == "move" then
- if msg.text[1] == "up" and tryRefuel(msg.text[2]) then
- for i = 1, msg.text[2] do
- if not up() then
- break
- end
- end
- elseif msg.text[1] == "down" and tryRefuel(msg.text[2]) then
- for i = 1, msg.text[2] do
- if not down() then
- break
- end
- end
- elseif msg.text[1] == "frwd" and tryRefuel(msg.text[2]) then
- for i = 1, msg.text[2] do
- if not forward() then
- break
- end
- end
- elseif msg.text[1] == "back" and tryRefuel(msg.text[2]) then
- for i = 1, msg.text[2] do
- if not back() then
- break
- end
- end
- elseif msg.text[1] == "left" then
- turtle.turnLeft()
- elseif msg.text[1] == "right" then
- turtle.turnRight()
- end
- elseif msg.cmd == "get" then
- if msg.text[1] == "fuel" then
- log("Fuel:"..turtle.getFuelLevel().." "..turtle.getItemCount(1))
- end
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment