Advertisement
augustclear

turtlelib

Nov 20th, 2020 (edited)
803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.29 KB | None | 0 0
  1. --Turtle Library
  2.  
  3. local x, y, z
  4. local bossid
  5. local name
  6.  
  7. local function open()
  8.     name = os.getComputerLabel() or os.getComputerID() .. ""
  9.     rednet.host("workers",name)
  10.     rednet.open("right")
  11.     bossid = rednet.lookup("workers","boss")
  12.     return bossid
  13. end
  14.  
  15. local function close()
  16.  
  17. end
  18.  
  19. local function get_bossid()
  20.     local id;
  21.    
  22.     for i=1,10 do
  23.         id = rednet.lookup("boss","boss")
  24.         if id ~= nil then
  25.             return id
  26.         end
  27.         os.sleep(5)
  28.     end
  29.     return -1
  30. end
  31.  
  32. local function init()
  33.     open()
  34.     x, y, z = gps.locate(5)
  35.     if x == nil then
  36.         do return -1 end
  37.     end
  38.     return 0
  39. end
  40.  
  41. local function send(s)
  42.     bossid = get_bossid()
  43.     return rednet.send(bossid, s, "workers")
  44. end
  45.  
  46. local function send_heartbeat()
  47.     x, y, z = gps.locate(5)
  48.     bossid = get_bossid()
  49.     rednet.send(bossid,os.getComputerLabel() .. ": " .. x .. "," .. y .. "," .. z, "heartbeat")
  50.     return x, y, z
  51. end
  52.  
  53. local function take_orders()
  54.     local sender, msg, protocol = rednet.receive("workers")
  55.     if msg == "close" then
  56.         close()
  57.     else
  58.         shell.run(msg);
  59.     end
  60.     return msg
  61. end
  62.  
  63. return {open = open, close = close, init = init, send = send, send_heartbeat = send_heartbeat, take_orders = take_orders}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement