Advertisement
Guest User

ReactorMonitor

a guest
May 19th, 2019
1,703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.83 KB | None | 0 0
  1. rednet.open("top")
  2.  
  3. local m = peripheral.wrap("right")
  4. m.setTextScale(0.5)
  5. m.clear()
  6.  
  7. local w, h = m.getSize()
  8. local isActive = false
  9. local euStored = 0
  10. local euCapacity = 0
  11. local heat = 0
  12. local heatMax = 0
  13. local euOutput = 0
  14. local autoShutoff = 0
  15. local lastAutoShutoff = 0
  16. local autoShutoffBar = "[                                 ]"
  17. local autoShutoffBarLength = 33
  18. local autoShutoffBarPercentPerChar = autoShutoffBarLength / 100.0
  19. local relayID = 18
  20. local sentOffSignal = false
  21.  
  22. function DrawInfoBox()
  23.     term.redirect(m)
  24.     paintutils.drawLine(3,3, 4, 3, colors.white)
  25.     paintutils.drawLine(11,3, ((w/5)*3) - 2, 3, colors.white)
  26.     paintutils.drawLine(3,3, 3, h - 2, colors.white)
  27.     paintutils.drawLine(3,h - 2, ((w/5)*3) - 2, h - 2, colors.white)
  28.     paintutils.drawLine(((w/5)*3) - 2,3, ((w/5)*3) - 2, h - 2, colors.white)
  29.     m.setBackgroundColor(colors.black)
  30.     m.setCursorPos(6,3)
  31.     m.write("INFO")
  32. end
  33.  
  34. function DrawControlsBox()
  35.     term.redirect(m)
  36.     paintutils.drawLine(((w/5)*3), 3, ((w/5)*3)+1, 3, colors.white)
  37.     paintutils.drawLine(((w/5)*3), 3, ((w/5)*3), ((h/5)*3) - 2, colors.white)
  38.     paintutils.drawLine(((w/5)*3), ((h/5)*3) - 2, w - 2, ((h/5)*3) - 2, colors.white)
  39.     paintutils.drawLine(((w/5)*3) + 12, 3, w - 2, 3, colors.white)
  40.     paintutils.drawLine(w - 2, 3, w - 2, ((h/5)*3) - 2, colors.white)
  41.     m.setBackgroundColor(colors.black)
  42.     m.setCursorPos(((w/5)*3)+3, 3)
  43.     m.write("CONTROLS")
  44. end
  45.  
  46. function DrawStatsBox()
  47.     term.redirect(m)
  48.     paintutils.drawLine(((w/5)*3), ((h/5)*3), ((w/5)*3)+1, ((h/5)*3), colors.white)
  49.     paintutils.drawLine(((w/5)*3) + 9, ((h/5)*3), w - 2, ((h/5)*3), colors.white)
  50.     paintutils.drawLine(((w/5)*3), ((h/5)*3), (w/5)*3, 35, colors.white)
  51.     paintutils.drawLine(((w/5)*3), h - 2, w - 2, h - 2, colors.white)
  52.     paintutils.drawLine(w - 2, ((h/5)*3), w - 2, h - 2, colors.white)
  53.     m.setBackgroundColor(colors.black)
  54.     m.setCursorPos(((w/5)*3) + 3, ((h/5)*3))
  55.     m.write("STATS")
  56.     m.setCursorPos(4, 4)
  57. end
  58.  
  59. local onButtonX = ((w/5)*3) + 2
  60. local onButtonY = 5
  61. local onButtonXX = w - (((w-2) - ((w/5)*3))/2) - 3
  62. local onButtonYY = 7
  63.  
  64. function DrawOnButton()
  65.     term.redirect(m)
  66.     local x = ((w/5)*3) + 2
  67.     local y = 5
  68.     local xx = w - (((w-2) - ((w/5)*3))/2) - 3
  69.     local yy = 7
  70.     local color = 0
  71.     if isActive then
  72.         color = colors.green
  73.     else
  74.         color = colors.red
  75.     end
  76.     paintutils.drawFilledBox(onButtonX, onButtonY, onButtonXX, onButtonYY, color)
  77.     m.setTextColor(colors.white)
  78.     m.setCursorPos(onButtonX + 8, onButtonY + 1)
  79.     m.write("ON")
  80.     m.setBackgroundColor(colors.black)
  81. end
  82.  
  83. local offButtonX = ((w/5)*3) + (((w-2) - ((w/5)*3))/2) + 1
  84. local offButtonY = 5
  85. local offButtonXX = w - 4
  86. local offButtonYY = 7
  87.  
  88. function DrawOffButton()
  89.     term.redirect(m)
  90.     color = 0
  91.     if isActive then
  92.         color = colors.red
  93.     else
  94.         color = colors.green
  95.     end
  96.     paintutils.drawFilledBox(offButtonX, offButtonY, offButtonXX, offButtonYY, color)
  97.     m.setTextColor(colors.white)
  98.     m.setCursorPos(offButtonX + 7, offButtonY + 1)
  99.     m.write("OFF")
  100.     m.setBackgroundColor(colors.black)
  101. end
  102.  
  103. function DrawStats()
  104.     term.redirect(m)
  105.     local x = ((w/5)*3) + 2
  106.     local y = ((h/5)*3) + 2
  107.     m.setCursorPos(x, y)
  108.     m.write("ENERGY OUTPUT:      " .. euOutput)
  109.     m.setCursorPos(x + 31, y)
  110.     m.write("EU/t")
  111.     m.setCursorPos(x, y+3)  
  112.     m.write("ENERGY STORED: " .. euStored .. "/" .. euCapacity .. "")
  113.     m.setCursorPos(x+33, y+3)  
  114.     m.write("EU")
  115.     m.setCursorPos(x, y+6)
  116.     m.write("HEAT         :      " .. heat)
  117.     m.setCursorPos(x+33, y+6)
  118.     m.write("hU")
  119.     m.setCursorPos(x, y+9)
  120.     m.write("MAX HEAT     :      " .. heatMax)
  121.     m.setCursorPos(x+33, y+9)
  122.     m.write("hU")
  123. end
  124.  
  125. function DrawHeatBar()
  126.     term.redirect(m)
  127.     local x = 5
  128.     local y = 6
  129.     local xx = ((w/5)*3) - 4
  130.     local yy = 12
  131.     local barLength = 51.0;
  132.     m.setCursorPos(x, y-1)
  133.     m.setBackgroundColor(colors.black)
  134.     m.write("HEAT / MAX")
  135.    
  136.     paintutils.drawFilledBox(x, y, xx, yy, colors.lightGray)
  137.    
  138.     if heat > 0 and heatMax > 0 then
  139.         pixelsPerHeatUnit = barLength / heatMax;
  140.         progressX = math.ceil(pixelsPerHeatUnit * heat)
  141.         paintutils.drawFilledBox(x, y, x+progressX, yy, colors.green)
  142.     end
  143.    
  144.     m.setBackgroundColor(colors.black)
  145. end
  146.  
  147. function DrawEUOutputBar()
  148.     term.redirect(m)
  149.     local x = 5
  150.     local y = 16
  151.     local xx = ((w/5)*3) - 4
  152.     local yy = 22
  153.     local barLength = 51.0
  154.     pixelsPerEU = barLength / 2048.0
  155.     m.setCursorPos(x, y-1)
  156.    
  157.     m.setBackgroundColor(colors.black)
  158.     m.write("EU OUTPUT / TICK")
  159.    
  160.     paintutils.drawFilledBox(x, y, xx, yy, colors.lightGray)
  161.     if euOutput > 0 then
  162.         progressX = math.ceil(euOutput * pixelsPerEU)
  163.         paintutils.drawFilledBox(x, y, x+progressX, yy, colors.green)
  164.     end
  165.    
  166.     m.setBackgroundColor(colors.black)
  167. end
  168.  
  169. function DrawEUProgressBar()
  170.     term.redirect(m)
  171.     local x = 5
  172.     local y = 27
  173.     local xx = ((w/5)*3) - 4
  174.     local yy = 33
  175.     local barLength = 51.0
  176.     m.setCursorPos(x, y-1)
  177.    
  178.     m.setBackgroundColor(colors.black)
  179.     m.write("EU STORED / CAPACITY")
  180.    
  181.     paintutils.drawFilledBox(x, y, xx, yy, colors.lightGray)
  182.     if euStored > 0 and euCapacity > 0 then
  183.         pixelsPerEU = barLength / euCapacity
  184.         progressX = math.ceil(euStored * pixelsPerEU)
  185.         paintutils.drawFilledBox(x, y, x+progressX, yy, colors.green)
  186.     end
  187.     m.setBackgroundColor(colors.black)
  188. end
  189.  
  190. local shutoffButtonY, shutoffButtonYY = 0,0
  191. local minusTenX, minusTenXX = 0,0
  192. local minusFiveX, minusFiveXX = 0,0
  193. local plusFiveX, plusFiveXX = 0,0
  194. local plusTenX, plusTenXX = 0,0
  195.  
  196. function DrawAutoShutoff()
  197.     local x = ((w/5)*3) + (((w-2) - ((w/5)*3))/4) + 4
  198.     local y = 11
  199.     local xx = w - (((w-2) - ((w/5)*3))/2) - 13
  200.     local yy = 13
  201.    
  202.     m.setCursorPos(x, y-2)
  203.     m.setBackgroundColor(colors.black)
  204.     m.write("AUTO-SHUTOFF")
  205.     x = ((w/5)*3) + 3
  206.        
  207.     shutoffButtonY = y
  208.     shutoffButtonYY = yy
  209.     minusTenX = x
  210.     minusTenXX = xx
  211.     minusFiveX = xx + 4
  212.     minusFiveXX = xx + (xx - x) + 4
  213.     plusFiveX = xx + (xx - x) + 8
  214.     plusFiveXX = xx + ((xx - x)*3) + 3
  215.     plusTenX = xx + ((xx - x)*3) + 7
  216.     plusTenXX = xx + ((xx - x)*5) + 2
  217.    
  218.     paintutils.drawFilledBox(minusTenX, shutoffButtonY, minusTenXX, shutoffButtonYY, colors.purple)
  219.     m.setCursorPos(minusTenX + 1, shutoffButtonY + 1)
  220.     m.write("-10%")
  221.    
  222.     paintutils.drawFilledBox(minusFiveX, shutoffButtonY, minusFiveXX, shutoffButtonYY, colors.purple)
  223.     m.setCursorPos(minusFiveX + 1, shutoffButtonY + 1)
  224.     m.write("-5%")
  225.    
  226.     paintutils.drawFilledBox(plusFiveX, shutoffButtonY, plusFiveXX, shutoffButtonYY, colors.purple)
  227.     m.setCursorPos(plusFiveX + 1, shutoffButtonY + 1)
  228.     m.write("+5%")
  229.    
  230.     paintutils.drawFilledBox(plusTenX, shutoffButtonY, plusTenXX, shutoffButtonYY, colors.purple)
  231.     m.setCursorPos(plusTenX + 1, shutoffButtonY + 1)
  232.     m.write("+10%")
  233.    
  234.     m.setBackgroundColor(colors.black)
  235.     m.setCursorPos(minusFiveXX + 1, shutoffButtonYY + 3)
  236.     m.write(autoShutoff .. " %")
  237.    
  238.     HandleAutoShutoffProgressBar()
  239.     m.setCursorPos(x - 1, shutoffButtonYY + 4)
  240.     m.write(autoShutoffBar)
  241. end
  242.  
  243. function HandleAutoShutoffProgressBar()
  244.     if lastAutoShutoff ~= autoShutoff then
  245.         local progress = math.ceil(autoShutoffBarPercentPerChar * autoShutoff) + 1
  246.         local newBar = ""
  247.         local toConcat
  248.        
  249.        
  250.         for i = 1, autoShutoffBarLength + 2 do
  251.             if i == 1 then
  252.                 toConcat = '['
  253.             elseif i <= progress then
  254.                 toConcat = '-'
  255.             elseif i == autoShutoffBarLength + 2 then
  256.                 toConcat = ']'
  257.             else
  258.                 toConcat = ' '
  259.             end
  260.            
  261.             newBar = (newBar .. toConcat)
  262.         end
  263.        
  264.         autoShutoffBar = newBar
  265.     end
  266. end
  267.  
  268. function HandleTouch()
  269.     while true do
  270.         local _, _, x, y = os.pullEvent("monitor_touch")
  271.         m.setCursorPos(4,4)
  272.         if x >= onButtonX and x <= onButtonXX and y >= onButtonY and y <= onButtonYY then
  273.             rednet.send(relayID, "<on>")
  274.         elseif x >= offButtonX and x <= offButtonXX and y >= offButtonY and y <= offButtonYY then
  275.             rednet.send(relayID, "<off>")
  276.         elseif x >= minusTenX and x <= minusTenXX and y >= shutoffButtonY and y <= shutoffButtonYY then
  277.             if autoShutoff - 10 < 0 then autoShutoff = 0
  278.             else autoShutoff = autoShutoff - 10 end
  279.         elseif x >= minusFiveX and x <= minusFiveXX and y >= shutoffButtonY and y <= shutoffButtonYY then
  280.             if autoShutoff - 5 < 0 then autoShutoff = 0
  281.             else autoShutoff = autoShutoff - 5 end
  282.         elseif x >= plusFiveX and x <= plusFiveXX and y >= shutoffButtonY and y <= shutoffButtonYY then
  283.             if autoShutoff + 5 > 100 then autoShutoff = 100
  284.             else autoShutoff = autoShutoff + 5 end
  285.         elseif x >= plusTenX and x <= plusTenXX and y >= shutoffButtonY and y <= shutoffButtonYY then
  286.             if autoShutoff + 10 > 100 then autoShutoff = 100
  287.             else autoShutoff = autoShutoff + 10 end
  288.         end
  289.     end
  290. end
  291.  
  292. function AutoShutoff()
  293.     if not isActive then sentOffSignal = false end
  294.     if heat >= ((autoShutoff * 0.01) * heatMax) and isActive and not sentOffSignal then
  295.         rednet.send(relayID, "<off>")
  296.         sentOffSignal = true
  297.     end
  298. end
  299.  
  300. function HandleReceivedData()
  301.     local sender, message = rednet.receive();
  302.     m.clear()
  303.    
  304.     if string.find(message, "<active>") then
  305.         if string.find(message, "false") then
  306.             isActive = false
  307.         else
  308.             isActive = true
  309.         end
  310.     elseif string.find(message, "<eustored>") then
  311.         euStored = tonumber(tostring(string.gsub(message, "<eustored>", "")))  
  312.     elseif string.find(message, "<eucapacity>") then
  313.         euCapacity = tonumber(tostring(string.gsub(message, "<eucapacity>", "")))
  314.     elseif string.find(message, "<euoutput>") then
  315.         euOutput = tonumber(tostring(string.gsub(message, "<euoutput>", "")))
  316.     elseif string.find(message, "<heat>") then
  317.         heat = tonumber(tostring(string.gsub(message, "<heat>", "")))
  318.     elseif string.find(message, "<heatmax>") then
  319.         heatMax = tonumber(tostring(string.gsub(message, "<heatmax>", "")))
  320.     end
  321. end
  322.  
  323. function MainLoop()
  324.     while true do
  325.         parallel.waitForAny(HandleReceivedData, HandleTouch)
  326.         Draw()
  327.         AutoShutoff()
  328.         lastAutoShutoff = autoShutoff
  329.     end
  330. end
  331.  
  332. function Draw()
  333.     DrawInfoBox()
  334.     DrawHeatBar()
  335.     DrawEUOutputBar()
  336.     DrawEUProgressBar()
  337.     DrawControlsBox()
  338.     DrawOnButton()
  339.     DrawOffButton()
  340.     DrawAutoShutoff()
  341.     DrawStatsBox()
  342.     DrawStats()
  343. end
  344.  
  345. Draw()
  346. MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement