Weastwood

energy store

Aug 29th, 2021
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.08 KB | None | 0 0
  1. ----------- Made BY MJRLegends (Please dont claim as your own code) -----------
  2. version = "1.5.2"
  3.  
  4. --------------------------------------------------------------
  5. --You can change these:
  6. local upper = 0.98 --Upper limit for computer to stop transmitting redstone signal. 0.98=98% full.
  7. local lower = 0.05 --Lower limit for computer to start transmitting redstone signal.
  8. local redstoneSide = "front" -- Change this to the side you want to output the redstone signal to. ["left","right","top","bottom","front","back","none"]
  9. 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.
  10. --------------------------------------------------------------
  11.  
  12. cellCount = 0
  13. connectedCells = {}
  14. connectedMekanisms = {}
  15. connectedOPCapBank = ""
  16. monitorCount = 0
  17. connectedMonitors = {}
  18. TE4Cell = 0 EIOCell = 0 OPCapBank = 0 Mekanism = 0
  19. periList = peripheral.getNames()
  20. validPeripherals = {
  21. "tile_thermalexpansion_cell",
  22. "powered_tile",
  23. "tile_blockcapacitorbank_name",
  24. "capacitor_bank",
  25. "monitor",
  26. "BigReactors%-Turbine",
  27. "BigReactors%-Reactor",
  28. "mekanism_machine",
  29. "Induction Matrix",
  30. "Energy Cube"
  31. }
  32. term.setTextColor(colors.blue)
  33. term.setCursorPos(1,1)
  34. term.write("MJRLegends Energy Management")
  35. term.setCursorPos(1,2)
  36. term.write("Version: " .. version)
  37. term.setTextColor(colors.white)
  38. term.setCursorPos(1,3)
  39.  
  40. function checkValidity(periName)
  41. for n,b in pairs(validPeripherals) do
  42. if periName:find(b) then return b end
  43. end
  44. return false
  45. end
  46.  
  47. for i,v in ipairs(periList) do
  48. local periFunctions = {
  49. ["tile_thermalexpansion_cell"] = function()
  50. cellCount = cellCount + 1
  51. TE4Cell= TE4Cell + 1
  52. connectedCells[cellCount] = periList[i]
  53. end,
  54. ["powered_tile"] = function()
  55. cellCount = cellCount + 1
  56. TE4Cell= TE4Cell + 1
  57. connectedCells[cellCount] = periList[i]
  58. end,
  59. ["tile_blockcapacitorbank_name"] = function()
  60. EIOCell = EIOCell + 1
  61. OPCapBank = OPCapBank + 1
  62. connectedOPCapBank = periList[i]
  63. end,
  64. ["capacitor_bank"] = function()
  65. cellCount = cellCount + 1
  66. EIOCell = EIOCell + 1
  67. connectedCells[cellCount] = periList[i]
  68. end,
  69. ["monitor"] = function()
  70. monitorCount = monitorCount + 1
  71. connectedMonitors[monitorCount] = periList[i]
  72. end,
  73. ["BigReactors%-Turbine"] = function()
  74. turbine = peripheral.wrap(periList[i])
  75. end,
  76. ["BigReactors%-Reactor"] = function()
  77. reactor = peripheral.wrap(periList[i])
  78. end,
  79. ["mekanism_machine"] = function()
  80. cellCount = cellCount + 1
  81. Mekanism = Mekanism + 1
  82. connectedMekanisms[cellCount] = periList[i]
  83. end,
  84. ["Induction Matrix"] = function()
  85. cellCount = cellCount + 1
  86. Mekanism = Mekanism + 1
  87. connectedMekanisms[cellCount] = periList[i]
  88. end,
  89. ["Energy Cube"] = function()
  90. cellCount = cellCount + 1
  91. Mekanism = Mekanism + 1
  92. connectedMekanisms[cellCount] = periList[i]
  93. end
  94. }
  95.  
  96. local isValid = checkValidity(peripheral.getType(v))
  97. if isValid then periFunctions[isValid]() end
  98. end
  99.  
  100. --Check for storage cells and monitors before continuing
  101. if cellCount == 0 and OPCapBank == 0 then
  102. term.setTextColor(colors.red)
  103. print("No RF storage found. Exiting script!")
  104. term.setTextColor(colors.white)
  105. return
  106. end
  107. if monitorCount == 0 then
  108. term.setTextColor(colors.red)
  109. print("No Monitor found. Exiting script!")
  110. term.setTextColor(colors.white)
  111. return
  112. end
  113. --Compatibility with OpenPeripherals
  114. if OPCapBank > 1 then
  115. print("Error: Without Computronics this script can only support a maximum of one Capacitor Bank. Exiting Script!")
  116. return
  117. elseif OPCapBank == 1 and capacitorBankBlocks == 0 then
  118. 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'.")
  119. elseif OPCapBank == 1 then
  120. print("Warning: OpenPeripherals does not fully support Capacitor Banks, numbers may not be fully accurate.")
  121. end
  122.  
  123. function getMonitorSize(x, y)
  124. return "large"
  125. end
  126.  
  127. for i = 1, #connectedMonitors do
  128. local monitor = peripheral.wrap(connectedMonitors[i])
  129. if getMonitorSize(monitor.getSize()) == nil then
  130. return
  131. end
  132. end
  133.  
  134. term.setTextColor(colors.green)
  135. print("Peripherals connected:")
  136. term.setTextColor(colors.white)
  137. if monitorCount > 1 then print(monitorCount.." Monitors") else print(monitorCount.." Monitor") end
  138. if TE4Cell ~= 1 then print(TE4Cell.." TE Energy Cells") else print(TE4Cell.." TE Energy Cell") end
  139. if Mekanism ~= 1 then print(Mekanism.." Mekanism Cubes/Induction Matrixs") else print(Mekanism.." Mekanism Cube/Induction Matrix") end
  140. if EIOCell ~= 1 then print(EIOCell.." Capacitor Banks") else print(EIOCell.." Capacitor Bank") end
  141. if turbine ~= nil then print ("1 Turbine") else print ("0 Turbines") end
  142. if reactor ~= nil then print ("1 Reactor") else print ("0 Reactors") end
  143.  
  144. if redstoneSide ~= "none" then redstone.setOutput(redstoneSide, false) end
  145. if turbine ~= nil then turbine.setActive(false) end
  146. if reactor ~= nil then reactor.setActive(false) end
  147.  
  148. lastTickPower = 0
  149. while true do
  150. local eNow = 0 eMax = 0 cellLoops = 0 mekanismLoops = 0
  151. for i = 1, #connectedCells do
  152. cell = peripheral.wrap(connectedCells[i])
  153. eNow = eNow + cell.getEnergyStored()
  154. eMax = eMax + cell.getMaxEnergyStored()
  155. cellLoops = i
  156. end
  157. for i = 1, #connectedMekanisms do
  158. cell = peripheral.wrap(connectedMekanisms[i])
  159. if cell.getEnergyStored then
  160. eNow = eNow + (cell.getEnergyStored() / 2.5)
  161. eMax = eMax + (cell.getMaxEnergyStored() / 2.5)
  162. else
  163. eNow = eNow + (cell.getEnergy() / 2.5)
  164. eMax = eMax + (cell.getMaxEnergy() / 2.5)
  165. end
  166. mekanismLoops = i
  167. end
  168. --Compatibility with OpenPeripherals
  169. if OPCapBank == 1 and cellLoops == #connectedCells then
  170. cell = peripheral.wrap(connectedOPCapBank)
  171. eNow = (eNow + cell.getEnergyStored()) * capacitorBankBlocks
  172. eMax = (eMax + cell.getMaxEnergyStored()) * capacitorBankBlocks
  173. end
  174. local fill = eNow / eMax
  175.  
  176. if eNow >= 1000000000 then eNowScale = "billion"
  177. elseif eNow >= 1000000 then eNowScale = "million"
  178. else eNowScale = "none" end
  179. if eMax >= 1000000000 then eMaxScale = "billion"
  180. elseif eMax >= 1000000 then eMaxScale = "million"
  181. else eMaxScale = "none" end
  182.  
  183. if eNowScale == "billion" then eNowValue = math.ceil(eNow / 1000000)
  184. elseif eNowScale == "million" then eNowValue = math.ceil(eNow / 1000)
  185. else eNowValue = math.ceil(eNow) end
  186. if eMaxScale == "billion" then eMaxValue = math.ceil(eMax / 1000000)
  187. elseif eMaxScale == "million" then eMaxValue = math.ceil(eMax / 1000)
  188. else eMaxValue = math.ceil(eMax) end
  189.  
  190. if eNowScale == "billion" then eNowSuffixLarge = "m RF" eNowSuffixSmall = "mRF"
  191. elseif eNowScale == "million" then eNowSuffixLarge = "k RF" eNowSuffixSmall = "kRF"
  192. else eNowSuffixLarge = " RF" eNowSuffixSmall = " RF" end
  193. if eMaxScale == "billion" then eMaxSuffixLarge = "m RF" eMaxSuffixSmall = "mRF"
  194. elseif eMaxScale == "million" then eMaxSuffixLarge = "k RF" eMaxSuffixSmall = "kRF"
  195. else eMaxSuffixLarge = " RF" eMaxSuffixSmall = " RF" end
  196.  
  197. local eNowDigitCount = 0 eMaxDigitCount = 0
  198. for digit in string.gmatch(eNowValue, "%d") do eNowDigitCount = eNowDigitCount + 1 end
  199. for digit in string.gmatch(eMaxValue, "%d") do eMaxDigitCount = eMaxDigitCount + 1 end
  200.  
  201. for i = 1, #connectedMonitors do
  202. local monitor=peripheral.wrap(connectedMonitors[i])
  203. if getMonitorSize(monitor.getSize()) == "large" then
  204. monitor.clear()
  205. monitor.setCursorPos(1,2)
  206. monitor.write("Storage:")
  207. monitor.setCursorPos(1,4)
  208. monitor.write(eNowValue.."/")
  209. monitor.setCursorPos((string.len(""..eNowValue) * 1) + 2,4)
  210. monitor.write(eMaxValue..eMaxSuffixLarge)
  211. monitor.setCursorPos(1,6)
  212. monitor.write("I/O:")
  213. monitor.setCursorPos(6,6)
  214. monitor.write(math.floor((eNow - lastTickPower) / 20))
  215. lastTickPower = eNow;
  216.  
  217. for i = 1, math.ceil(fill * 10) do
  218. monitor.setBackgroundColour((colours.green))
  219. monitor.setCursorPos(24,12-i)
  220. monitor.write(" ")
  221. monitor.setBackgroundColour((colours.black))
  222. end
  223. for i = 1, 10 - math.ceil(fill * 10) do
  224. monitor.setBackgroundColour((colours.red))
  225. monitor.setCursorPos(24,1+i)
  226. monitor.write(" ")
  227. monitor.setBackgroundColour((colours.black))
  228. end
  229. end
  230. end
  231. sleep(1)
  232. end
Advertisement
Add Comment
Please, Sign In to add comment