Advertisement
Umaroth

Umaroth's Power Monitor - Official Version

Mar 28th, 2015
7,294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.76 KB | None | 0 0
  1. --[[
  2. Umaroth's Power Monitor v1.3
  3.  
  4. Monitors TE4 Energy Cells and EnderIO Capacitor Banks and outputs a redstone signal once energy storage drops below set limits.
  5. Will automatically detect any compatible energy storage and monitors and connect to all of them. They can be either directly adjacent to the computer or connected through a wired modem network. Monitor size must be 2x3 or 1x2.
  6. Redstone signal output for the engines is off by default but can be changed easily on line 18.
  7. Also supports BigReactors Reactors and Turbines. Maximum 1 of each.
  8.  
  9. Requires either OpenPeripheralsIntegration or Computronics or both. Computronics is recommended because OpenP doesn't fully support Capacitor Banks. If you have OpenPeripherals but not Computronics you must use the workaround on line 19 for Capacitor Banks to work.
  10.  
  11. Script overhaul by Umaroth. Original script located here: http://tinyurl.com/kuaxv2p
  12. Changelog/README: http://pastebin.com/erPf3t4t
  13. --]]
  14.  
  15. --You can change these:
  16. local upper = 0.98 --Upper limit for computer to stop transmitting redstone signal. 0.98=98% full.
  17. local lower = 0.05 --Lower limit for computer to start transmitting redstone signal.
  18. local redstoneSide = "none" -- Change this to the side you want to output the redstone signal to. ["left","right","top","bottom","front","back","none"]
  19. local capacitorBankBlocks = 0 -- If you have OpenPeripherals without Computronics you need to specify how many blocks your Capacitor Bank contains. Only works properly for one Capacitor Bank. If you have Computronics, this variable won't do anything.
  20.  
  21. --Don't change these:
  22. cellCount = 0
  23. connectedCells = {}
  24. connectedOPCapBank = ""
  25. monitorCount = 0
  26. connectedMonitors = {}
  27. TE4Cell = 0 EIOCell = 0 OPCapBank = 0
  28. periList = peripheral.getNames()
  29. validPeripherals = {
  30.     "tile_thermalexpansion_cell",
  31.     "powered_tile",
  32.     "tile_blockcapacitorbank_name",
  33.     "capacitor_bank",
  34.     "monitor",
  35.     "BigReactors%-Turbine",
  36.     "BigReactors%-Reactor"
  37. }
  38.  
  39. function checkValidity(periName)
  40.     for n,b in pairs(validPeripherals) do
  41.         if periName:find(b) then return b end
  42.     end
  43.     return false
  44. end
  45.  
  46. for i,v in ipairs(periList) do
  47.     local periFunctions = {
  48.         ["tile_thermalexpansion_cell"] = function()
  49.             cellCount = cellCount + 1
  50.             TE4Cell= TE4Cell + 1
  51.             connectedCells[cellCount] = periList[i]
  52.         end,
  53.         ["powered_tile"] = function()
  54.             cellCount = cellCount + 1
  55.             TE4Cell= TE4Cell + 1
  56.             connectedCells[cellCount] = periList[i]
  57.         end,
  58.         ["tile_blockcapacitorbank_name"] = function()
  59.             EIOCell = EIOCell + 1
  60.             OPCapBank = OPCapBank + 1
  61.             connectedOPCapBank = periList[i]
  62.         end,
  63.         ["capacitor_bank"] = function()
  64.             cellCount = cellCount + 1
  65.             EIOCell = EIOCell + 1
  66.             connectedCells[cellCount] = periList[i]
  67.         end,
  68.         ["monitor"] = function()
  69.             monitorCount = monitorCount + 1
  70.             connectedMonitors[monitorCount] = periList[i]
  71.         end,
  72.         ["BigReactors%-Turbine"] = function()
  73.             turbine = peripheral.wrap(periList[i])
  74.         end,
  75.         ["BigReactors%-Reactor"] = function()
  76.             reactor = peripheral.wrap(periList[i])
  77.         end
  78.     }
  79.  
  80.     local isValid = checkValidity(peripheral.getType(v))
  81.     if isValid then periFunctions[isValid]() end
  82. end
  83.  
  84. --Check for storage cells and monitors before continuing
  85. if cellCount == 0 and OPCapBank == 0 then
  86.     print("No RF storage found. Exiting script!")
  87.     return
  88. end
  89. if monitorCount == 0 then
  90.     print("No Monitor found. Exiting script!")
  91.     return
  92. end
  93.     --Compatibility with OpenPeripherals
  94. if OPCapBank > 1 then
  95.     print("Error: Without Computronics this script can only support a maximum of one Capacitor Bank. Exiting Script!")
  96.     return
  97. elseif OPCapBank == 1 and capacitorBankBlocks == 0 then
  98.     print("Warning: You have not entered how many blocks your Capacitor Bank contains, the script will not return the correct numbers. Please fix this by editing the script and changing the variable 'capacitorBankBlocks'.")
  99. elseif OPCapBank == 1 then
  100.     print("Warning: OpenPeripherals does not fully support Capacitor Banks, numbers may not be fully accurate.")
  101. end
  102.  
  103. --Function to set monitor sizes
  104. function getMonitorSize(x, y)
  105.     if x == 18 and y == 5 then
  106.         return "small"
  107.     elseif x == 29 and y == 12 then
  108.         return "large"
  109.     else
  110.         print("Invalid monitor size detected. Exiting script!")
  111.         return
  112.     end
  113. end
  114.  
  115. --Check monitor sizes before continuing
  116. for i = 1, #connectedMonitors do
  117.     local monitor = peripheral.wrap(connectedMonitors[i])
  118.     if getMonitorSize(monitor.getSize()) == nil then
  119.         return
  120.     end
  121. end
  122.  
  123. --Print connected peripherals
  124. print("Peripherals connected:")
  125. if monitorCount > 1 then print(monitorCount.." Monitors") else print(monitorCount.." Monitor") end
  126. if TE4Cell ~= 1 then print(TE4Cell.." TE Energy Cells") else print(TE4Cell.." TE Energy Cell") end
  127. if EIOCell ~= 1 then print(EIOCell.." Capacitor Banks") else print(EIOCell.." Capacitor Bank") end
  128. if turbine ~= nil then print ("1 Turbine") else print ("0 Turbines") end
  129. if reactor ~= nil then print ("1 Reactor") else print ("0 Reactors") end
  130.  
  131. --Main code
  132.  
  133. --Set default output states to off
  134. if redstoneSide ~= "none" then redstone.setOutput(redstoneSide, false) end
  135. if turbine ~= nil then turbine.setActive(false) end
  136. if reactor ~= nil then reactor.setActive(false) end
  137.  
  138. --Write default engine status to all attached monitors
  139. for i = 1, #connectedMonitors do
  140.     local monitor = peripheral.wrap(connectedMonitors[i])
  141.     if getMonitorSize(monitor.getSize()) == "large" then
  142.         monitor.clear()
  143.         monitor.setBackgroundColour((colours.grey))
  144.         monitor.setCursorPos(11,4)
  145.         monitor.write(" ON ")
  146.         monitor.setBackgroundColour((colours.green))
  147.         monitor.setCursorPos(15,4)
  148.         monitor.write(" OFF ")
  149.         monitor.setBackgroundColour((colours.black))
  150.     else
  151.         monitor.clear()
  152.         monitor.setBackgroundColour((colours.grey))
  153.         monitor.setCursorPos(1,4)
  154.         monitor.write(" ON ")
  155.         monitor.setBackgroundColour((colours.green))
  156.         monitor.setCursorPos(5,4)
  157.         monitor.write(" OFF ")
  158.         monitor.setBackgroundColour((colours.black))
  159.     end
  160. end
  161.  
  162.  
  163. --Main loop
  164. while true do
  165.  
  166.   --Get all dynamic values
  167.     --Get storage values
  168.     local eNow = 0 eMax = 0 cellLoops = 0
  169.     for i = 1, #connectedCells do
  170.         cell = peripheral.wrap(connectedCells[i])
  171.         eNow = eNow + cell.getEnergyStored()
  172.         eMax = eMax + cell.getMaxEnergyStored()
  173.         cellLoops = i
  174.     end
  175.     --Compatibility with OpenPeripherals
  176.     if OPCapBank == 1 and cellLoops == #connectedCells then
  177.         cell = peripheral.wrap(connectedOPCapBank)
  178.         eNow = (eNow + cell.getEnergyStored()) * capacitorBankBlocks
  179.         eMax = (eMax + cell.getMaxEnergyStored()) * capacitorBankBlocks
  180.     end
  181.  
  182.     --Compute engine activation ratio
  183.     local fill = eNow / eMax
  184.  
  185.     --Set storage scale
  186.     if eNow >= 1000000000 then eNowScale = "billion"
  187.     elseif eNow >= 1000000 then eNowScale = "million"
  188.     else eNowScale = "none" end
  189.     if eMax >= 1000000000 then eMaxScale = "billion"
  190.     elseif eMax >= 1000000 then eMaxScale = "million"
  191.     else eMaxScale = "none" end
  192.  
  193.     --Adjust number to scale
  194.     if eNowScale == "billion" then eNowValue = math.ceil(eNow / 1000000)
  195.     elseif eNowScale == "million" then eNowValue = math.ceil(eNow / 1000)
  196.     else eNowValue = math.ceil(eNow) end
  197.     if eMaxScale == "billion" then eMaxValue = math.ceil(eMax / 1000000)
  198.     elseif eMaxScale == "million" then eMaxValue = math.ceil(eMax / 1000)
  199.     else eMaxValue = math.ceil(eMax) end
  200.  
  201.     --Adjust suffix to scale
  202.     if eNowScale == "billion" then eNowSuffixLarge = "m RF" eNowSuffixSmall = "mRF"
  203.     elseif eNowScale == "million" then eNowSuffixLarge = "k RF" eNowSuffixSmall = "kRF"
  204.     else eNowSuffixLarge = " RF" eNowSuffixSmall = " RF" end
  205.     if eMaxScale == "billion" then eMaxSuffixLarge = "m RF" eMaxSuffixSmall = "mRF"
  206.     elseif eMaxScale == "million" then eMaxSuffixLarge = "k RF" eMaxSuffixSmall = "kRF"
  207.     else eMaxSuffixLarge = " RF" eMaxSuffixSmall = " RF" end
  208.  
  209.     --Get number of digits to write
  210.     local eNowDigitCount = 0 eMaxDigitCount = 0
  211.     for digit in string.gmatch(eNowValue, "%d") do eNowDigitCount = eNowDigitCount + 1 end
  212.     for digit in string.gmatch(eMaxValue, "%d") do eMaxDigitCount = eMaxDigitCount + 1 end
  213.  
  214.     --Get location to write
  215.     if eNowSuffixLarge ~= " RF" then eNowXLarge = 17 - eNowDigitCount
  216.     else eNowXLarge = 18 - eNowDigitCount end
  217.     eNowXSmall = 16 - eNowDigitCount
  218.     if eMaxSuffixLarge ~= " RF" then eMaxXLarge = 17 - eMaxDigitCount
  219.     else eMaxXLarge = 18 - eMaxDigitCount end
  220.     eMaxXSmall = 16 - eMaxDigitCount
  221.  
  222.     --Loop to write to every monitor
  223.     for i = 1, #connectedMonitors do
  224.         local monitor=peripheral.wrap(connectedMonitors[i])
  225.  
  226.         if getMonitorSize(monitor.getSize()) == "large" then
  227.             --Erase old data
  228.             monitor.setCursorPos(10,9)
  229.             monitor.write("       ")
  230.             monitor.setCursorPos(10,11)
  231.             monitor.write("       ")
  232.             --Write constant/new data
  233.             monitor.setCursorPos(12,2)
  234.             monitor.write("Engines:")
  235.             monitor.setCursorPos(12,7)
  236.             monitor.write("Storage:")
  237.             monitor.setCursorPos(eNowXLarge,9)
  238.             monitor.write(eNowValue..eNowSuffixLarge)
  239.             monitor.setCursorPos(eMaxXLarge,10)
  240.             monitor.write("of:")
  241.             monitor.setCursorPos(eMaxXLarge,11)
  242.             monitor.write(eMaxValue..eMaxSuffixLarge)
  243.             if fill > upper then
  244.                 --Energy level is over upper level, turning redstone/reactors off
  245.                 if redstoneSide ~= "none" then redstone.setOutput(redstoneSide, false) end
  246.                 if turbine ~= nil then turbine.setActive(false) end
  247.                 if reactor ~= nil then reactor.setActive(false) end
  248.                 monitor.setBackgroundColour((colours.grey))
  249.                 monitor.setCursorPos(11,4)
  250.                 monitor.write(" ON ")
  251.                 monitor.setBackgroundColour((colours.green))
  252.                 monitor.setCursorPos(15,4)
  253.                 monitor.write(" OFF ")
  254.                 monitor.setBackgroundColour((colours.black))
  255.             elseif fill < lower then
  256.                 --Energy level is below lower limit, turning redstone/reactors on
  257.                 if redstoneSide ~= "none" then redstone.setOutput(redstoneSide, true) end
  258.                 if turbine ~= nil then turbine.setActive(true) end
  259.                 if reactor ~= nil then reactor.setActive(true) end
  260.                 monitor.setBackgroundColour((colours.green))
  261.                 monitor.setCursorPos(11,4)
  262.                 monitor.write(" ON ")
  263.                 monitor.setBackgroundColour((colours.grey))
  264.                 monitor.setCursorPos(15,4)
  265.                 monitor.write(" OFF ")
  266.                 monitor.setBackgroundColour((colours.black))
  267.             end
  268.             for i = 1, math.ceil(fill * 10) do
  269.                 monitor.setBackgroundColour((colours.green))
  270.                 monitor.setCursorPos(24,12-i)
  271.                 monitor.write(" ")
  272.                 monitor.setBackgroundColour((colours.black))
  273.             end
  274.             for i = 1, 10 - math.ceil(fill * 10) do
  275.                 monitor.setBackgroundColour((colours.red))
  276.                 monitor.setCursorPos(24,1+i)
  277.                 monitor.write(" ")
  278.                 monitor.setBackgroundColour((colours.black))
  279.             end
  280.         elseif getMonitorSize(monitor.getSize()) == "small" then
  281.             --erase old data
  282.             monitor.setCursorPos(10,3)
  283.             monitor.write("       ")
  284.             monitor.setCursorPos(10,5)
  285.             monitor.write("       ")
  286.             --write constant/new data
  287.             monitor.setCursorPos(2,2)
  288.             monitor.write("Engines:")
  289.             monitor.setCursorPos(11,2)
  290.             monitor.write("Storage:")
  291.             monitor.setCursorPos(eNowXSmall,3)
  292.             monitor.write(eNowValue..eNowSuffixSmall)
  293.             monitor.setCursorPos(eMaxXSmall,4)
  294.             monitor.write("of:")
  295.             monitor.setCursorPos(eMaxXSmall,5)
  296.             monitor.write(eMaxValue..eMaxSuffixSmall)
  297.             if fill > upper then
  298.                 --Energy level is over upper level, turning redstone/reactors off
  299.                 if redstoneSide ~= "none" then redstone.setOutput(redstoneSide, false) end
  300.                 if turbine ~= nil then turbine.setActive(false) end
  301.                 if reactor ~= nil then reactor.setActive(false) end
  302.                 monitor.setBackgroundColour((colours.grey))
  303.                 monitor.setCursorPos(1,4)
  304.                 monitor.write(" ON ")
  305.                 monitor.setBackgroundColour((colours.green))
  306.                 monitor.setCursorPos(5,4)
  307.                 monitor.write(" OFF ")
  308.                 monitor.setBackgroundColour((colours.black))
  309.             elseif fill < lower then
  310.                 --Energy level is below lower limit, turning redstone/reactors on
  311.                 if redstoneSide ~= "none" then redstone.setOutput(redstoneSide, true) end
  312.                 if turbine ~= nil then turbine.setActive(true) end
  313.                 if reactor ~= nil then reactor.setActive(true) end
  314.                 monitor.setBackgroundColour((colours.green))
  315.                 monitor.setCursorPos(1,4)
  316.                 monitor.write(" ON ")
  317.                 monitor.setBackgroundColour((colours.grey))
  318.                 monitor.setCursorPos(5,4)
  319.                 monitor.write(" OFF ")
  320.                 monitor.setBackgroundColour((colours.black))
  321.             end
  322.         end
  323.     end
  324.     sleep(1)
  325. end --while
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement