Vaerys_Dawn

turtle_status.lua

Nov 24th, 2020 (edited)
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.36 KB | None | 0 0
  1. local args = {...}
  2. turtles = {}
  3.  
  4. if #args ~= 1 then
  5.   print("Please give me a name")
  6.   error()
  7. end
  8.  
  9. function getModemSide()
  10.   sides = peripheral.getNames()
  11.   for k, v in pairs(sides) do
  12.     if peripheral.getType(v) == "modem" then return v end
  13.   end
  14.   return nil
  15. end
  16.  
  17. function getMonitor()
  18.   sides = peripheral.getNames()
  19.   for k, v in pairs(sides) do
  20.     if peripheral.getType(v) == "monitor" then return v end
  21.   end
  22.   return nil
  23. end
  24.  
  25. function openRednet()
  26.   side = getModemSide()
  27.   if not side then
  28.     error("Could not find modem")
  29.   end
  30.   rednet.open(side, 255)
  31.   rednet.host("turtle_status", args[1])
  32. end
  33.  
  34. function pushToMonitor()
  35.   local monitor = getMonitor()
  36.   if monitor then
  37.     term.redirect(peripheral.wrap(monitor))
  38.   end
  39. end
  40.  
  41. function readRednet()
  42.   while true do
  43.     local id, message, protocol = rednet.receive()
  44.     if message == "request_connect" then
  45.       turtles[id] = {connected = true, status = "connected"}
  46.     elseif type(message) == "table" then
  47.       -- do later
  48.     else
  49.       if turtles[id] then
  50.         turtles[id].status = message
  51.       end
  52.     end
  53.     updateStatus()
  54.   end
  55. end
  56.  
  57. function updateStatus()
  58.   pushToMonitor()
  59.   term.clear()
  60.   term.setCursorPos(1,1)
  61.   print("-- Turtle Status --")
  62.   for k,v in pairs(turtles or {}) do
  63.     print(k..": ".. v.status)
  64.   end
  65. end
  66.  
  67. openRednet()
  68. readRednet()
  69.  
Add Comment
Please, Sign In to add comment