Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --sys2_turtle_unit
- os.loadAPI("sys/syslib")
- if peripheral.isPresent("left") and peripheral.getType("left") == "modem" then
- rednet.open("left")
- elseif peripheral.isPresent("right") and peripheral.getType("right") == "modem" then
- rednet.open("right")
- else
- error("there is no modem connected to this device")
- end
- print("<turtleUnit>")
- local rootId
- function identify()
- local label = os.getComputerLabel()
- if label == nill then
- label = "<unnamed>"
- end
- rednet.broadcast("@root:identifyTurtleUnit," .. label)
- end
- function main()
- local id, msg = syslib.receiveRednet()
- local tag, pts = syslib.getMsgData(msg)
- if tag == "@turtle" then
- if pts[1] == "initialise" then
- rootId = id
- identify()
- elseif pts[1] == "reboot" then
- if pts[2] == nill then
- os.reboot()
- else
- if os.getComputerID() == tonumber(pts[2]) then
- syslib.log("root", "rebooting turtle " .. os.getComputerID() .. "(label '" .. os.getComputerLabel() .. "')")
- os.reboot()
- end
- end
- elseif pts[1] == "action" then
- if pts[2] == "setStartup" then
- shell.run("delete", "startup")
- local f = fs.open("startup", "w")
- f.write("shell.run(\"delete\",\"unitHandler\")\n")
- f.write("shell.run(\"pastebin\",\"get\",\"" .. pts[3] .. "\",\"unitHandler\")\n")
- f.write("shell.run(\"unitHandler\")\n")
- f.close()
- elseif pts[2] == "setLabel" then
- syslib.log("root", "successfully renamed turtle '" .. os.getComputerLabel() .. "' to '" .. pts[3] .. "'")
- os.setComputerLabel(pts[3])
- syslib.sendRednet(rootId, "@root:answer,renamedTurtle," .. pts[3])
- elseif pts[2] == "fwd" then
- turtle.forward() --TODO Functions from old dengs
- syslib.sendRednet(id,"moved fwd")
- elseif pts[2] == "left" then
- turtle.turnLeft() --TODO Functions from old dengs
- syslib.sendRednet(id,"turned left")
- elseif pts[2] == "right" then
- turtle.turnRight() --TODO Functions from old dengs
- syslib.sendRednet(id,"turned right")
- else print("unknown message>action: ", msg)
- end
- else print("unknown message: ", msg)
- end
- end
- end
- identify()
- local errInRow = 0
- while true do
- local status, err = pcall(main)
- if err == "Terminated" then
- break
- elseif not status then
- errInRow = errInRow + 1
- print("error thrown: " .. err)
- rednet.broadcast("@err:" .. os.getComputerID() .. "," .. os.getComputerLabel() .. ",serviceUnit")
- if errInRow > 5 then
- error("errors are looping!")
- end
- else errInRow = 0
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement