Guest User

Untitled

a guest
May 29th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.68 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.10 --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 = 300 -- 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. rednet.open("right")
  23. cellCount = 0
  24. connectedCells = {}
  25. connectedOPCapBank = ""
  26. monitorCount = 0
  27. connectedMonitors = {}
  28. TE4Cell = 0 EIOCell = 0 OPCapBank = 0 VEIOCell = 0
  29. periList = peripheral.getNames()
  30. validPeripherals = {
  31. -- "tile_thermalexpansion_cell",
  32. -- "powered_tile",
  33. "tile_blockcapacitorbank_name",
  34. "capacitor_bank",
  35. "io_config_tile",
  36. "monitor",
  37. "BigReactors%-Turbine",
  38. "BigReactors%-Reactor"
  39. }
  40.  
  41. function checkValidity(periName)
  42. for n,b in pairs(validPeripherals) do
  43. if periName:find(b) then return b end
  44. end
  45. return false
  46. end
  47.  
  48. for i,v in ipairs(periList) do
  49. local periFunctions = {
  50. -- ["tile_thermalexpansion_cell"] = function()
  51. -- cellCount = cellCount + 1
  52. -- TE4Cell= TE4Cell + 1
  53. -- connectedCells[cellCount] = periList[i]
  54. -- end,
  55. -- ["powered_tile"] = function()
  56. -- cellCount = cellCount + 1
  57. -- TE4Cell= TE4Cell + 1
  58. -- connectedCells[cellCount] = periList[i]
  59. -- end,
  60. ["tile_blockcapacitorbank_name"] = function()
  61. EIOCell = EIOCell + 1
  62. OPCapBank = OPCapBank + 1
  63. connectedOPCapBank = periList[i]
  64. end,
  65. ["capacitor_bank"] = function()
  66. cellCount = cellCount + 1
  67. EIOCell = EIOCell + 1
  68. connectedCells[cellCount] = periList[i]
  69. end,
  70. ["io_config_tile"] = function()
  71. cellCount = cellCount + 1
  72. VEIOCell = VEIOCell + 1
  73. connectedCells[cellCount] = periList[i]
  74. end,
  75. ["monitor"] = function()
  76. monitorCount = monitorCount + 1
  77. connectedMonitors[monitorCount] = periList[i]
  78. end,
  79. ["BigReactors%-Turbine"] = function()
  80. turbine = peripheral.wrap(periList[i])
  81. end,
  82. ["BigReactors%-Reactor"] = function()
  83. reactor = peripheral.wrap(periList[i])
  84. end
  85. }
  86.  
  87. local isValid = checkValidity(peripheral.getType(v))
  88. if isValid then periFunctions[isValid]() end
  89. end
  90.  
  91. --Compatibility with OpenPeripherals
  92. if OPCapBank > 1 then
  93. print("Error: Without Computronics this script can only support a maximum of one Capacitor Bank. Exiting Script!")
  94. return
  95. elseif OPCapBank == 1 and capacitorBankBlocks == 0 then
  96. 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'.")
  97. elseif OPCapBank == 1 then
  98. print("Warning: OpenPeripherals does not fully support Capacitor Banks, numbers may not be fully accurate.")
  99. end
  100.  
  101. --Check for storage cells and monitors before continuing
  102. if cellCount == 0 and OPCapBank == 0 then
  103. print("No RF storage found. Exiting script!")
  104. return
  105. end
  106. if monitorCount == 0 then
  107. print("No Monitor found. Exiting script!")
  108. return
  109. end
  110.  
  111. --Function to set monitor sizes
  112. function getMonitorSize(x, y)
  113. if x == 18 and y == 5 then
  114. return "small"
  115. elseif x == 29 and y == 12 then
  116. return "large"
  117. else
  118. print("Invalid monitor size detected. Exiting script!")
  119. return
  120. end
  121. end
  122.  
  123. --Check monitor sizes before continuing
  124. for i = 1, #connectedMonitors do
  125. local monitor = peripheral.wrap(connectedMonitors[i])
  126. if getMonitorSize(monitor.getSize()) == nil then
  127. return
  128. end
  129. end
  130.  
  131. --Print connected peripherals
  132. print("Peripherals connected:")
  133. if monitorCount > 1 then print(monitorCount.." Monitors") else print(monitorCount.." Monitor") end
  134. if TE4Cell ~= 1 then print(TE4Cell.." TE Energy Cells") else print(TE4Cell.." TE Energy Cell") end
  135. if EIOCell ~= 1 then print(EIOCell.." Capacitor Banks") else print(EIOCell.." Capacitor Bank") end
  136. if VEIOCell ~= 1 then print(VEIOCell.." Vibrant Capacitor Banks") else print(VEIOCell.." Vibrant Capacitor Bank") end
  137. if turbine ~= nil then print ("1 Turbine") else print ("0 Turbines") end
  138. if reactor ~= nil then print ("1 Reactor") else print ("0 Reactors") end
  139.  
  140. --Main code
  141.  
  142. --Set default output states to off
  143. if redstoneSide ~= "none" then redstone.setOutput(redstoneSide, false) end
  144. if turbine ~= nil then turbine.setActive(false) end
  145. -- if reactor ~= nil then reactor.setActive(false) end
  146. if reactor ~= nil then reactor.setAllControlRodLevels(100) end
  147.  
  148. --Write default engine status to all attached monitors
  149. for i = 1, #connectedMonitors do
  150. local monitor = peripheral.wrap(connectedMonitors[i])
  151. if getMonitorSize(monitor.getSize()) == "large" then
  152. monitor.clear()
  153. monitor.setBackgroundColour((colours.grey))
  154. monitor.setCursorPos(11,4)
  155. monitor.write(" ON ")
  156. monitor.setBackgroundColour((colours.red))
  157. monitor.setCursorPos(15,4)
  158. monitor.write(" OFF ")
  159. monitor.setBackgroundColour((colours.black))
  160. end
  161. end
  162.  
  163.  
  164. --Main loop
  165. while true do
  166.  
  167. --Get all dynamic values
  168. --Get storage values
  169. local eNow = 0 eMax = 0 cellLoops = 0
  170. for i = 1, #connectedCells do
  171. cell = peripheral.wrap(connectedCells[i])
  172. eNow = eNow + cell.getEnergyStored()
  173. eMax = eMax + cell.getMaxEnergyStored()
  174. cellLoops = i
  175. end
  176. --Compatibility with OpenPeripherals
  177. -- if OPCapBank == 1 and cellLoops == #connectedCells then
  178. -- cell = peripheral.wrap(connectedOPCapBank)
  179. -- eNow = (eNow + cell.getEnergyStored()) * capacitorBankBlocks
  180. -- eMax = (eMax + cell.getMaxEnergyStored()) * capacitorBankBlocks
  181. -- end
  182.  
  183. if VEIOCell >= 1 then
  184. eNow = eNow * capacitorBankBlocks
  185. eMax = eMax * capacitorBankBlocks
  186. end
  187.  
  188. --Compute engine activation ratio
  189. local fill = eNow / eMax
  190.  
  191. --Set storage scale
  192. if eNow >= 1000000000 then eNowScale = "billion"
  193. elseif eNow >= 1000000 then eNowScale = "million"
  194. else eNowScale = "none" end
  195. if eMax >= 1000000000 then eMaxScale = "billion"
  196. elseif eMax >= 1000000 then eMaxScale = "million"
  197. else eMaxScale = "none" end
  198.  
  199. --Adjust number to scale
  200. if eNowScale == "billion" then eNowValue = math.ceil(eNow / 1000000)
  201. elseif eNowScale == "million" then eNowValue = math.ceil(eNow / 1000)
  202. else eNowValue = math.ceil(eNow) end
  203. if eMaxScale == "billion" then eMaxValue = math.ceil(eMax / 1000000)
  204. elseif eMaxScale == "million" then eMaxValue = math.ceil(eMax / 1000)
  205. else eMaxValue = math.ceil(eMax) end
  206.  
  207. --Adjust suffix to scale
  208. if eNowScale == "billion" then eNowSuffixLarge = " Mrd RF" eNowSuffixSmall = "Mio RF"
  209. elseif eNowScale == "million" then eNowSuffixLarge = " Mio RF" eNowSuffixSmall = "Ki RF"
  210. else eNowSuffixLarge = " RF" eNowSuffixSmall = " RF" end
  211. if eMaxScale == "billion" then eMaxSuffixLarge = " Mrd RF" eMaxSuffixSmall = "Mio RF"
  212. elseif eMaxScale == "million" then eMaxSuffixLarge = " Mio RF" eMaxSuffixSmall = "Ki RF"
  213. else eMaxSuffixLarge = " RF" eMaxSuffixSmall = " RF" end
  214.  
  215. --Get number of digits to write
  216. local eNowDigitCount = 0 eMaxDigitCount = 0
  217. for digit in string.gmatch(eNowValue, "%d") do eNowDigitCount = eNowDigitCount + 1 end
  218. for digit in string.gmatch(eMaxValue, "%d") do eMaxDigitCount = eMaxDigitCount + 1 end
  219.  
  220. function comma_value(eNowValue)
  221. local formatted = eNowValue
  222. while true do
  223. formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  224. if (k==0) then
  225. break
  226. end
  227. end
  228. return formatted
  229. end
  230.  
  231. function comma_value(eMaxValue)
  232. local formatted = eMaxValue
  233. while true do
  234. formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  235. if (k==0) then
  236. break
  237. end
  238. end
  239. return formatted
  240. end
  241.  
  242. --Get location to write
  243. if eNowSuffixLarge ~= " RF" then eNowXLarge = 13 - eNowDigitCount
  244. else eNowXLarge = 17 - eNowDigitCount end
  245. eNowXSmall = 15 - eNowDigitCount
  246. if eMaxSuffixLarge ~= " RF" then eMaxXLarge = 13 - eMaxDigitCount
  247. else eMaxXLarge = 17 - eMaxDigitCount end
  248. eMaxXSmall = 15 - eMaxDigitCount
  249.  
  250. --Loop to write to every monitor
  251. for i = 1, #connectedMonitors do
  252. local monitor=peripheral.wrap(connectedMonitors[i])
  253.  
  254. if getMonitorSize(monitor.getSize()) == "large" then
  255. --Erase old data
  256. monitor.setCursorPos(10,9)
  257. monitor.write(" ")
  258. monitor.setCursorPos(10,11)
  259. monitor.write(" ")
  260. --Write constant/new data
  261. monitor.setCursorPos(12,2)
  262. monitor.write("Reaktor:")
  263. monitor.setCursorPos(9,7)
  264. monitor.write("Stromspeicher:")
  265. monitor.setCursorPos(eNowXLarge,9)
  266. monitor.write(comma_value(eNowValue)..eNowSuffixLarge)
  267. monitor.setCursorPos(eMaxXLarge,10)
  268. monitor.setCursorPos(12.5,10)
  269. monitor.write("von:")
  270. monitor.setCursorPos(eMaxXLarge,11)
  271. monitor.write(comma_value(eMaxValue)..eMaxSuffixLarge)
  272. rednet.open("right")
  273. if fill > upper then
  274. --Energy level is over upper level, turning redstone/reactors off
  275. if redstoneSide ~= "none" then redstone.setOutput(redstoneSide, false) end
  276. if turbine ~= nil then turbine.setActive(false) end
  277. -- if reactor ~= nil then reactor.setActive(false) end
  278. if reactor ~= nil then reactor.setAllControlRodLevels(100) end
  279. rednet.broadcast("an")
  280. monitor.setBackgroundColour((colours.grey))
  281. monitor.setCursorPos(11,4)
  282. monitor.write(" ON ")
  283. monitor.setBackgroundColour((colours.red))
  284. monitor.setCursorPos(15,4)
  285. monitor.write(" OFF ")
  286. monitor.setBackgroundColour((colours.black))
  287. elseif fill < lower then
  288. --Energy level is below lower limit, turning redstone/reactors on
  289. if redstoneSide ~= "none" then redstone.setOutput(redstoneSide, true) end
  290. if turbine ~= nil then turbine.setActive(true) end
  291. -- if reactor ~= nil then reactor.setActive(true) end
  292. if reactor ~= nil then reactor.setAllControlRodLevels(0) end
  293. rednet.broadcast("aus")
  294. monitor.setBackgroundColour((colours.green))
  295. monitor.setCursorPos(11,4)
  296. monitor.write(" ON ")
  297. monitor.setBackgroundColour((colours.grey))
  298. monitor.setCursorPos(15,4)
  299. monitor.write(" OFF ")
  300. monitor.setBackgroundColour((colours.black))
  301. end
  302. for i = 1, math.ceil(fill * 10) do
  303. monitor.setBackgroundColour((colours.green))
  304. monitor.setCursorPos(24,12-i)
  305. monitor.write(" ")
  306. monitor.setBackgroundColour((colours.black))
  307. end
  308. for i = 1, 10 - math.ceil(fill * 10) do
  309. monitor.setBackgroundColour((colours.red))
  310. monitor.setCursorPos(24,1+i)
  311. monitor.write(" ")
  312. monitor.setBackgroundColour((colours.black))
  313. end
  314. end
  315. end
  316. sleep(1)
  317. end --while
Add Comment
Please, Sign In to add comment