Advertisement
melzneni

sys2_turtle_unit

Feb 19th, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. --sys2_turtle_unit
  2. os.loadAPI("sys/syslib")
  3.  
  4. if peripheral.isPresent("left") and peripheral.getType("left") == "modem" then
  5. rednet.open("left")
  6. elseif peripheral.isPresent("right") and peripheral.getType("right") == "modem" then
  7. rednet.open("right")
  8. else
  9. error("there is no modem connected to this device")
  10. end
  11.  
  12. print("<turtleUnit>")
  13.  
  14. local rootId
  15.  
  16. function identify()
  17. local label = os.getComputerLabel()
  18. if label == nill then
  19. label = "<unnamed>"
  20. end
  21. rednet.broadcast("@root:identifyTurtleUnit," .. label)
  22. end
  23.  
  24. function main()
  25. local id, msg = syslib.receiveRednet()
  26. local tag, pts = syslib.getMsgData(msg)
  27. if tag == "@turtle" then
  28. if pts[1] == "initialise" then
  29. rootId = id
  30. identify()
  31. elseif pts[1] == "reboot" then
  32. if pts[2] == nill then
  33. os.reboot()
  34. else
  35. if os.getComputerID() == tonumber(pts[2]) then
  36. syslib.log("root", "rebooting turtle " .. os.getComputerID() .. "(label '" .. os.getComputerLabel() .. "')")
  37. os.reboot()
  38. end
  39. end
  40. elseif pts[1] == "action" then
  41. if pts[2] == "setStartup" then
  42. shell.run("delete", "startup")
  43. local f = fs.open("startup", "w")
  44. f.write("shell.run(\"delete\",\"unitHandler\")\n")
  45. f.write("shell.run(\"pastebin\",\"get\",\"" .. pts[3] .. "\",\"unitHandler\")\n")
  46. f.write("shell.run(\"unitHandler\")\n")
  47. f.close()
  48. elseif pts[2] == "setLabel" then
  49. syslib.log("root", "successfully renamed turtle '" .. os.getComputerLabel() .. "' to '" .. pts[3] .. "'")
  50. os.setComputerLabel(pts[3])
  51. syslib.sendRednet(rootId, "@root:answer,renamedTurtle," .. pts[3])
  52. elseif pts[2] == "fwd" then
  53. turtle.forward() --TODO Functions from old dengs
  54. syslib.sendRednet(id,"moved fwd")
  55. elseif pts[2] == "left" then
  56. turtle.turnLeft() --TODO Functions from old dengs
  57. syslib.sendRednet(id,"turned left")
  58. elseif pts[2] == "right" then
  59. turtle.turnRight() --TODO Functions from old dengs
  60. syslib.sendRednet(id,"turned right")
  61. else print("unknown message>action: ", msg)
  62. end
  63. else print("unknown message: ", msg)
  64. end
  65. end
  66. end
  67.  
  68. identify()
  69. local errInRow = 0
  70. while true do
  71. local status, err = pcall(main)
  72. if err == "Terminated" then
  73. break
  74. elseif not status then
  75. errInRow = errInRow + 1
  76. print("error thrown: " .. err)
  77. rednet.broadcast("@err:" .. os.getComputerID() .. "," .. os.getComputerLabel() .. ",serviceUnit")
  78. if errInRow > 5 then
  79. error("errors are looping!")
  80. end
  81. else errInRow = 0
  82. end
  83. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement