Advertisement
Umaroth

Umaroth's Power Monitor - Development Version

Apr 1st, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.94 KB | None | 0 0
  1. --[[
  2. This is a development version of Umaroth's Power Monitor. I upload testing versions here for easy migration into CC terminals for testing.
  3.  
  4. You're free to use development versions but they may not work properly.
  5. --]]
  6.  
  7. --You can change these:
  8. local upper = 0.98 --Upper limit for computer to stop transmitting redstone signal. 0.98=98% full.
  9. local lower = 0.05 --Lower limit for computer to start transmitting redstone signal.
  10. local redstoneSide = "none" -- Change this to the side you want to output the redstone signal to. ["left","right","top","bottom","front","back","none"]
  11.  
  12. --Don't change these:
  13. cellCount = 0
  14. connectedCells = {}
  15. monitorCount = 0
  16. connectedMonitors = {}
  17. TE4Cell = 0 EIOCell = 0
  18. periList = peripheral.getNames()
  19.  
  20. --Get all peripherals
  21. for i = 1, #periList do
  22.     validPeripherals = {["powered_tile"]=true,["capacitor_bank"]=true,["monitor"]=true,["BigReactors_Turbine"]=true,["BigReactors-Reactor"]=true}
  23.     periFunctions = {
  24.     ["powered_tile"] = function()
  25.         cellCount = cellCount + 1
  26.         TE4Cell= TE4Cell + 1
  27.         connectedCells[cellCount] = periList[i]
  28.     end,
  29.     ["capacitor_bank"] = function()
  30.         cellCount = cellCount + 1
  31.         EIOCell = EIOCell + 1
  32.         connectedCells[cellCount] = periList[i]
  33.     end,
  34.     ["monitor"] = function()
  35.         monitorCount = monitorCount + 1
  36.         connectedMonitors[monitorCount] = periList[i]
  37.     end,
  38.     ["BigReactors_Turbine"] = function()
  39.         turbine = peripheral.wrap(periList[i])
  40.     end,
  41.     ["BigReactors-Reactor"] = function()
  42.         reactor = peripheral.wrap(periList[i])
  43.     end
  44.     }
  45.  
  46.     if validPeripherals[peripheral.getType(periList[i])] then periFunctions[peripheral.getType(periList[i])]() end
  47. end
  48.  
  49. --Check for storage cells and monitors before continuing
  50. if cellCount == 0 then
  51.     print("No RF storage found. Exiting script!")
  52.     return
  53. end
  54. if monitorCount == 0 then
  55.     print ("No Monitor found. Exiting script!")
  56.     return
  57. end
  58.  
  59. --Function to set monitor sizes
  60. function getMonitorSize(x, y)
  61.     if x == 18 and y == 5 then
  62.         return "small"
  63.     elseif x == 29 and y == 12 then
  64.         return "large"
  65.     else
  66.         print("Invalid monitor size detected. Exiting script!")
  67.         return
  68.     end
  69. end
  70.  
  71. --Check monitor sizes before continuing
  72. for i = 1, #connectedMonitors do
  73.     monitor = peripheral.wrap(connectedMonitors[i])
  74.     if getMonitorSize(monitor.getSize()) == nil then
  75.         return
  76.     end
  77. end
  78.  
  79. --Print connected peripherals
  80. print("Peripherals connected:")
  81. if monitorCount > 1 then print(monitorCount.." Monitors") else print(monitorCount.." Monitor") end
  82. if TE4Cell == 0 or TE4Cell > 1 then print(TE4Cell.." TE Energy Cells") else print(TE4Cell.." TE Energy Cell") end
  83. if EIOCell == 0 or EIOCell > 1 then print(EIOCell.." Capacitor Banks") else print(EIOCell.." Capacitor Bank") end
  84. if turbine ~= nil then print ("1 Turbine") else print ("0 Turbines") end
  85. if reactor ~= nil then print ("1 Reactor") else print ("0 Reactors") end
  86.  
  87. --Main code
  88.  
  89. --Set default output states to off
  90. if redstoneSide ~= "none" then redstone.setOutput(redstoneSide, false) end
  91. if turbine ~= nil then turbine.setActive(false) end
  92. if reactor ~= nil then reactor.setActive(false) end
  93.  
  94. --Write default engine status to all attached monitors
  95. for i = 1, #connectedMonitors do
  96.     monitor = peripheral.wrap(connectedMonitors[i])
  97.     if getMonitorSize(monitor.getSize()) == "large" then
  98.         monitor.clear()
  99.         monitor.setBackgroundColour((colours.grey))
  100.         monitor.setCursorPos(11,4)
  101.         monitor.write(" ON ")
  102.         monitor.setBackgroundColour((colours.green))
  103.         monitor.setCursorPos(15,4)
  104.         monitor.write(" OFF ")
  105.         monitor.setBackgroundColour((colours.black))
  106.     else
  107.         monitor.clear()
  108.         monitor.setBackgroundColour((colours.grey))
  109.         monitor.setCursorPos(1,4)
  110.         monitor.write(" ON ")
  111.         monitor.setBackgroundColour((colours.green))
  112.         monitor.setCursorPos(5,4)
  113.         monitor.write(" OFF ")
  114.         monitor.setBackgroundColour((colours.black))
  115.     end
  116. end
  117.  
  118.  
  119. --Main loop
  120. while true do
  121.  
  122.     --Get all dynamic values
  123.         --Get storage values
  124.     eNow = 0 eMax = 0
  125.     for i = 1, #connectedCells do
  126.         cell=peripheral.wrap(connectedCells[i])
  127.         eNow = eNow + cell.getEnergyStored()
  128.         eMax = eMax + cell.getMaxEnergyStored()
  129.     end
  130.  
  131.         --Compute engine activation ratio
  132.     fill = eNow / eMax
  133.  
  134.         --Set storage scale
  135.     if eNow >= 1000000000 then eNowScale = "billion"
  136.     elseif eNow >= 1000000 then eNowScale = "million"
  137.     else eNowScale = "none" end
  138.     if eMax >= 1000000000 then eMaxScale = "billion"
  139.     elseif eMax >= 1000000 then eMaxScale = "million"
  140.     else eMaxScale = "none" end
  141.  
  142.         --Adjust number to scale
  143.     if eNowScale == "billion" then eNowValue = math.ceil(eNow / 1000000)
  144.     elseif eNowScale == "million" then eNowValue = math.ceil(eNow / 1000)
  145.     else eNowValue = math.ceil(eNow) end
  146.     if eMaxScale == "billion" then eMaxValue = math.ceil(eMax / 1000000)
  147.     elseif eMaxScale == "million" then eMaxValue = math.ceil(eMax / 1000)
  148.     else eMaxValue = math.ceil(eMax) end
  149.  
  150.         --Adjust suffix to scale
  151.     if eNowScale == "billion" then eNowSuffixLarge = "m RF"
  152.     elseif eNowScale == "million" then eNowSuffixLarge = "k RF"
  153.     else eNowSuffixLarge = " RF" end
  154.     if eNowScale == "billion" then eNowSuffixSmall = "mRF"
  155.     elseif eNowScale == "million" then eNowSuffixSmall = "kRF"
  156.     else eNowSuffixSmall = " RF" end
  157.     if eMaxScale == "billion" then eMaxSuffixLarge = "m RF"
  158.     elseif eMaxScale == "million" then eMaxSuffixLarge = "k RF"
  159.     else eMaxSuffixLarge = " RF" end
  160.     if eMaxScale == "billion" then eMaxSuffixSmall = "mRF"
  161.     elseif eMaxScale == "million" then eMaxSuffixSmall = "kRF"
  162.     else eMaxSuffixSmall = " RF" end
  163.  
  164.         --Get number of digits to write
  165.     eNowDigitCount = 0 eMaxDigitCount = 0
  166.     for digit in string.gmatch(eNowValue, "%d") do eNowDigitCount = eNowDigitCount + 1 end
  167.     for digit in string.gmatch(eMaxValue, "%d") do eMaxDigitCount = eMaxDigitCount + 1 end
  168.  
  169.         --Get location to write
  170.     if eNowSuffixLarge == "m RF" or eNowSuffixLarge == "k RF" then eNowXLarge = 17 - eNowDigitCount
  171.     else eNowXLarge = 18 - eNowDigitCount end
  172.     eNowXSmall = 16 - eNowDigitCount
  173.     if eMaxSuffixLarge == "m RF" or eMaxSuffixLarge == "k RF" then eMaxXLarge = 17 - eMaxDigitCount
  174.     else eMaxXLarge = 18 - eMaxDigitCount end
  175.     eMaxXSmall = 16 - eMaxDigitCount
  176.  
  177.     --Loop for every monitor
  178.     for i = 1, #connectedMonitors do
  179.         monitor=peripheral.wrap(connectedMonitors[i])
  180.  
  181.         --Write data to all attached monitors
  182.         monitor=peripheral.wrap(connectedMonitors[i])
  183.         if getMonitorSize(monitor.getSize()) == "large" then
  184.             --Erase old data
  185.             monitor.setCursorPos(10,9)
  186.             monitor.write("       ")
  187.             monitor.setCursorPos(10,11)
  188.             monitor.write("       ")
  189.             --Write constant/new data
  190.             monitor.setCursorPos(12,2)
  191.             monitor.write("Engines:")
  192.             monitor.setCursorPos(12,7)
  193.             monitor.write("Storage:")
  194.             monitor.setCursorPos(eNowXLarge,9)
  195.             monitor.write(eNowValue..eNowSuffixLarge)
  196.             monitor.setCursorPos(eMaxXLarge,10)
  197.             monitor.write("of:")
  198.             monitor.setCursorPos(eMaxXLarge,11)
  199.             monitor.write(eMaxValue..eMaxSuffixLarge)
  200.             if fill > upper then
  201.                 --Energy level is over upper level, turning redstone/reactors off
  202.                 if redstoneSide ~= "none" then redstone.setOutput(redstoneSide, false) end
  203.                 if turbine ~= nil then turbine.setActive(false) end
  204.                 if reactor ~= nil then reactor.setActive(false) end
  205.                 monitor.setBackgroundColour((colours.grey))
  206.                 monitor.setCursorPos(11,4)
  207.                 monitor.write(" ON ")
  208.                 monitor.setBackgroundColour((colours.green))
  209.                 monitor.setCursorPos(15,4)
  210.                 monitor.write(" OFF ")
  211.                 monitor.setBackgroundColour((colours.black))
  212.             elseif fill < lower then
  213.                 --Energy level is below lower limit, turning redstone/reactors on
  214.                 if redstoneSide ~= "none" then redstone.setOutput(redstoneSide, true) end
  215.                 if turbine ~= nil then turbine.setActive(true) end
  216.                 if reactor ~= nil then reactor.setActive(true) end
  217.                 monitor.setBackgroundColour((colours.green))
  218.                 monitor.setCursorPos(11,4)
  219.                 monitor.write(" ON ")
  220.                 monitor.setBackgroundColour((colours.grey))
  221.                 monitor.setCursorPos(15,4)
  222.                 monitor.write(" OFF ")
  223.                 monitor.setBackgroundColour((colours.black))
  224.             end
  225.         elseif getMonitorSize(monitor.getSize()) == "small" then
  226.             --erase old data
  227.             monitor.setCursorPos(10,3)
  228.             monitor.write("       ")
  229.             monitor.setCursorPos(10,5)
  230.             monitor.write("       ")
  231.             --write constant/new data
  232.             monitor.setCursorPos(2,2)
  233.             monitor.write("Engines:")
  234.             monitor.setCursorPos(11,2)
  235.             monitor.write("Storage:")
  236.             monitor.setCursorPos(eNowXSmall,3)
  237.             monitor.write(eNowValue..eNowSuffixSmall)
  238.             monitor.setCursorPos(eMaxXSmall,4)
  239.             monitor.write("of:")
  240.             monitor.setCursorPos(eMaxXSmall,5)
  241.             monitor.write(eMaxValue..eMaxSuffixSmall)
  242.             if fill > upper then
  243.                 --Energy level is over upper level, turning redstone/reactors off
  244.                 if redstoneSide ~= "none" then redstone.setOutput(redstoneSide, false) end
  245.                 if turbine ~= nil then turbine.setActive(false) end
  246.                 if reactor ~= nil then reactor.setActive(false) end
  247.                 monitor.setBackgroundColour((colours.grey))
  248.                 monitor.setCursorPos(1,4)
  249.                 monitor.write(" ON ")
  250.                 monitor.setBackgroundColour((colours.green))
  251.                 monitor.setCursorPos(5,4)
  252.                 monitor.write(" OFF ")
  253.                 monitor.setBackgroundColour((colours.black))
  254.             elseif fill < lower then
  255.                 --Energy level is below lower limit, turning redstone/reactors on
  256.                 if redstoneSide ~= "none" then redstone.setOutput(redstoneSide, true) end
  257.                 if turbine ~= nil then turbine.setActive(true) end
  258.                 if reactor ~= nil then reactor.setActive(true) end
  259.                 monitor.setBackgroundColour((colours.green))
  260.                 monitor.setCursorPos(1,4)
  261.                 monitor.write(" ON ")
  262.                 monitor.setBackgroundColour((colours.grey))
  263.                 monitor.setCursorPos(5,4)
  264.                 monitor.write(" OFF ")
  265.                 monitor.setBackgroundColour((colours.black))
  266.             end
  267.         end
  268.     end
  269.     sleep(1)
  270. end --while
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement