Advertisement
dadragon84

Minor Monitor

Nov 13th, 2013 (edited)
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.50 KB | None | 0 0
  1. --[[
  2. Name: MinerMonitor
  3. Version: 1.3
  4. Description: Receives information from mining turtles and displays it on an attached monitor.
  5.  
  6. Date: 04/27/2023
  7. Author: MrJohnDowe
  8.  
  9. ]]--
  10.  
  11.  
  12. computerID = os.getComputerID()
  13. -- Active Turtles
  14. activeMiners = {}
  15. --[[Section: Helpers]]--
  16. modemLocation = nil
  17. consoleName = nil
  18. consoleID = 0
  19. monitorID = 0
  20.  
  21.  
  22. -- Determine where the modem is.
  23. monitorSide = nil
  24. print("Looking for modem,")
  25. if peripheral.isPresent("left") and peripheral.getType("left")=="modem" then
  26.   modemLocation = "left"
  27. elseif peripheral.isPresent("right") and peripheral.getType("right")=="modem" then
  28.   modemLocation = "right"
  29. elseif peripheral.isPresent("top") and peripheral.getType("top")=="modem" then
  30.   modemLocation = "top"
  31. elseif peripheral.isPresent("back") and peripheral.getType("back")=="modem" then
  32.   modemLocation = "back"
  33. elseif peripheral.isPresent("bottom") and peripheral.getType("bottom")=="modem" then
  34.   modemLocation = "bottom"
  35. else
  36.   print("No modem")
  37.   return
  38. end
  39. print("Modem found on "..modemLocation)
  40.  
  41.  
  42.  
  43.  
  44. -- Determine where monitor is.
  45. monitorSide = nil
  46. if peripheral.isPresent("left") and peripheral.getType("left")=="monitor" then
  47.   monitorSide = "left"
  48. elseif peripheral.isPresent("right") and peripheral.getType("right")=="monitor" then
  49.   monitorSide = "right"
  50. elseif peripheral.isPresent("top") and peripheral.getType("top")=="monitor" then
  51.   monitorSide = "top"
  52. elseif peripheral.isPresent("back") and peripheral.getType("back")=="monitor" then
  53.   monitorSide = "back"
  54. elseif peripheral.isPresent("bottom") and peripheral.getType("bottom")=="monitor" then
  55.   monitorSide = "bottom"
  56. end
  57. if monitorSide ~= nil then
  58.   monitor = peripheral.wrap(monitorSide)
  59.   print("Monitor found on "..monitorSide)
  60.   monitor.setTextScale(.7)
  61. else
  62.   monitor = nil
  63. end
  64.  
  65. function printLeft(dev, msg, height)
  66.   dev.setCursorPos(1, height)
  67.   dev.clearLine()
  68.   dev.write(msg)
  69. end
  70.  
  71. function printIndented(dev, msg, indent, height)
  72.   dev.setCursorPos(indent, height)
  73.   dev.clearLine()
  74.   dev.write(msg)
  75. end
  76.  
  77. function printCentered(dev, msg, height)
  78.   w,h = dev.getSize()
  79.   dev.setCursorPos(w/2 - #msg/2, tonumber(height))
  80.   dev.clearLine()
  81.   dev.write(msg)
  82. end
  83.  
  84. function printRight(dev, msg, height)
  85.   w,h = dev.getSize()
  86.   dev.setCursorPos(w-#msg)
  87.   dev.clearLine()
  88.   dev.write(msg)
  89. end
  90.  
  91. function wprintOffCenter(dev, msg, height, width, offset)
  92.     local inc = 0
  93.     local ops = 1
  94.     while #msg - ops > width do
  95.         local nextspace = 0
  96.         while string.find(msg, " ", ops + nextspace) and
  97.                 string.find(msg, " ", ops + nextspace) - ops < width do
  98.             nextspace = string.find(msg, " ", nextspace + ops) + 1 - ops
  99.         end
  100.         dev.setCursorPos(width/2 - (nextspace)/2 + offset, height + inc)
  101.         inc = inc + 1
  102.         dev.write(string.sub(msg, ops, nextspace + ops - 1))
  103.         ops = ops + nextspace
  104.     end
  105.     dev.write(string.sub(msg, ops))
  106.    
  107.     return inc + 1
  108. end
  109.  
  110. --[[Section: Menu State Functions]]--
  111.  
  112. function printHeader()
  113.   term.clear()
  114.   w,h = term.getSize()
  115.     printCentered(term, "Miner Monitor System  Computer ID:"..computerID, 1)
  116.     printCentered(term, string.rep("-", w), 2)
  117.   row = 3
  118.   for key,value in pairs( activeMiners ) do
  119.     printIndented(term, value, 1, row)
  120.     row = row + 1
  121.   end
  122.     printCentered(term, string.rep("-", w), h-2)
  123.     printIndented(term, "Last update: "..updateTime, 3, h-1)
  124.   if monitor ~= nil then
  125.     printMonitor()
  126.   end
  127. end
  128.  
  129. function printMonitor()
  130.   monitor.clear()
  131.   w,h = monitor.getSize()
  132.  
  133.   w,h = term.getSize()
  134.     printCentered(monitor, "ID: "..computerID.."  Miner Monitor System", 1)
  135.     printCentered(monitor, string.rep("-", w), 2)
  136.   row = 3
  137.   for key,value in pairs( activeMiners ) do
  138.     printIndented(monitor, value, 1, row)
  139.     row = row + 1
  140.   end
  141.     printCentered(monitor, string.rep("-", w), h-2)
  142.     printIndented(monitor, "Last update: "..updateTime, 3, h-1)
  143. end
  144.  
  145. function getCurrentMessages()
  146.   updateTime = textutils.formatTime(os.time())
  147.   printHeader()
  148.     while true do
  149.         local id, msg = rednet.receive()
  150.        
  151.     if id ~= nil then
  152.       if string.sub(msg,1,3) == "MM:" then
  153.         local minerIdx = string.find(msg,":")
  154.         if minerIdx ~= nil then
  155.           updateTime = textutils.formatTime(os.time())
  156.           local minerName = string.sub(msg,4,minerIdx-1)
  157.           local minerMsg = string.sub(msg,minerIdx+1)
  158.           activeMiners[id] = minerMsg
  159.         end
  160.         printHeader(term)
  161.        
  162.       end
  163.     end
  164.    
  165.     return true
  166.    
  167.     end
  168. end
  169.  
  170.  
  171.  
  172.  
  173. while true do
  174.     getCurrentMessages()
  175.    
  176. end
  177.  
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement