Guest User

Untitled

a guest
May 29th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.24 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. else
  161. monitor.clear()
  162. monitor.setBackgroundColour((colours.grey))
  163. monitor.setCursorPos(1,4)
  164. monitor.write(" ON ")
  165. monitor.setBackgroundColour((colours.green))
  166. monitor.setCursorPos(5,4)
  167. monitor.write(" OFF ")
  168. monitor.setBackgroundColour((colours.black))
  169. end
  170. end
  171.  
  172.  
  173. --Main loop
  174. while true do
  175.  
  176. --Get all dynamic values
  177. --Get storage values
  178. local eNow = 0 eMax = 0 cellLoops = 0
  179. for i = 1, #connectedCells do
  180. cell = peripheral.wrap(connectedCells[i])
  181. eNow = eNow + cell.getEnergyStored()
  182. eMax = eMax + cell.getMaxEnergyStored()
  183. cellLoops = i
  184. end
  185. --Compatibility with OpenPeripherals
  186. -- if OPCapBank == 1 and cellLoops == #connectedCells then
  187. -- cell = peripheral.wrap(connectedOPCapBank)
  188. -- eNow = (eNow + cell.getEnergyStored()) * capacitorBankBlocks
  189. -- eMax = (eMax + cell.getMaxEnergyStored()) * capacitorBankBlocks
  190. -- end
  191.  
  192. if VEIOCell >= 1 then
  193. eNow = eNow * capacitorBankBlocks
  194. eMax = eMax * capacitorBankBlocks
  195. end
  196.  
  197. --Compute engine activation ratio
  198. local fill = eNow / eMax
  199.  
  200. --Set storage scale
  201. if eNow >= 1000000000 then eNowScale = "billion"
  202. elseif eNow >= 1000000 then eNowScale = "million"
  203. else eNowScale = "none" end
  204. if eMax >= 1000000000 then eMaxScale = "billion"
  205. elseif eMax >= 1000000 then eMaxScale = "million"
  206. else eMaxScale = "none" end
  207.  
  208. --Adjust number to scale
  209. if eNowScale == "billion" then eNowValue = math.ceil(eNow / 1000000)
  210. elseif eNowScale == "million" then eNowValue = math.ceil(eNow / 1000)
  211. else eNowValue = math.ceil(eNow) end
  212. if eMaxScale == "billion" then eMaxValue = math.ceil(eMax / 1000000)
  213. elseif eMaxScale == "million" then eMaxValue = math.ceil(eMax / 1000)
  214. else eMaxValue = math.ceil(eMax) end
  215.  
  216. --Adjust suffix to scale
  217. if eNowScale == "billion" then eNowSuffixLarge = " Mrd RF" eNowSuffixSmall = "Mio RF"
  218. elseif eNowScale == "million" then eNowSuffixLarge = " Mio RF" eNowSuffixSmall = "Ki RF"
  219. else eNowSuffixLarge = " RF" eNowSuffixSmall = " RF" end
  220. if eMaxScale == "billion" then eMaxSuffixLarge = " Mrd RF" eMaxSuffixSmall = "Mio RF"
  221. elseif eMaxScale == "million" then eMaxSuffixLarge = " Mio RF" eMaxSuffixSmall = "Ki RF"
  222. else eMaxSuffixLarge = " RF" eMaxSuffixSmall = " RF" end
  223.  
  224. --Get number of digits to write
  225. local eNowDigitCount = 0 eMaxDigitCount = 0
  226. for digit in string.gmatch(eNowValue, "%d") do eNowDigitCount = eNowDigitCount + 1 end
  227. for digit in string.gmatch(eMaxValue, "%d") do eMaxDigitCount = eMaxDigitCount + 1 end
  228.  
  229. function comma_value(eNowValue)
  230. local formatted = eNowValue
  231. while true do
  232. formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  233. if (k==0) then
  234. break
  235. end
  236. end
  237. return formatted
  238. end
  239.  
  240. function comma_value(eMaxValue)
  241. local formatted = eMaxValue
  242. while true do
  243. formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  244. if (k==0) then
  245. break
  246. end
  247. end
  248. return formatted
  249. end
  250.  
  251. --Get location to write
  252. if eNowSuffixLarge ~= " RF" then eNowXLarge = 13 - eNowDigitCount
  253. else eNowXLarge = 17 - eNowDigitCount end
  254. eNowXSmall = 15 - eNowDigitCount
  255. if eMaxSuffixLarge ~= " RF" then eMaxXLarge = 13 - eMaxDigitCount
  256. else eMaxXLarge = 17 - eMaxDigitCount end
  257. eMaxXSmall = 15 - eMaxDigitCount
  258.  
  259. --Loop to write to every monitor
  260. for i = 1, #connectedMonitors do
  261. local monitor=peripheral.wrap(connectedMonitors[i])
  262.  
  263. if getMonitorSize(monitor.getSize()) == "large" then
  264. --Erase old data
  265. monitor.setCursorPos(10,9)
  266. monitor.write(" ")
  267. monitor.setCursorPos(10,11)
  268. monitor.write(" ")
  269. --Write constant/new data
  270. monitor.setCursorPos(12,2)
  271. monitor.write("Reaktor:")
  272. monitor.setCursorPos(9,7)
  273. monitor.write("Stromspeicher:")
  274. monitor.setCursorPos(eNowXLarge,9)
  275. monitor.write(comma_value(eNowValue)..eNowSuffixLarge)
  276. monitor.setCursorPos(eMaxXLarge,10)
  277. monitor.setCursorPos(12.5,10)
  278. monitor.write("von:")
  279. monitor.setCursorPos(eMaxXLarge,11)
  280. monitor.write(comma_value(eMaxValue)..eMaxSuffixLarge)
  281. if fill > upper then
  282. --Energy level is over upper level, turning redstone/reactors off
  283. if redstoneSide ~= "none" then redstone.setOutput(redstoneSide, false) end
  284. if turbine ~= nil then turbine.setActive(false) end
  285. -- if reactor ~= nil then reactor.setActive(false) end
  286. if reactor ~= nil then reactor.setAllControlRodLevels(100) end
  287. rednet.broadcast("an")
  288. monitor.setBackgroundColour((colours.grey))
  289. monitor.setCursorPos(11,4)
  290. monitor.write(" ON ")
  291. monitor.setBackgroundColour((colours.red))
  292. monitor.setCursorPos(15,4)
  293. monitor.write(" OFF ")
  294. monitor.setBackgroundColour((colours.black))
  295. elseif fill < lower then
  296. --Energy level is below lower limit, turning redstone/reactors on
  297. if redstoneSide ~= "none" then redstone.setOutput(redstoneSide, true) end
  298. if turbine ~= nil then turbine.setActive(true) end
  299. -- if reactor ~= nil then reactor.setActive(true) end
  300. if reactor ~= nil then reactor.setAllControlRodLevels(0) end
  301. rednet.broadcast("aus")
  302. monitor.setBackgroundColour((colours.green))
  303. monitor.setCursorPos(11,4)
  304. monitor.write(" ON ")
  305. monitor.setBackgroundColour((colours.grey))
  306. monitor.setCursorPos(15,4)
  307. monitor.write(" OFF ")
  308. monitor.setBackgroundColour((colours.black))
  309. end
  310. for i = 1, math.ceil(fill * 10) do
  311. monitor.setBackgroundColour((colours.green))
  312. monitor.setCursorPos(24,12-i)
  313. monitor.write(" ")
  314. monitor.setBackgroundColour((colours.black))
  315. end
  316. for i = 1, 10 - math.ceil(fill * 10) do
  317. monitor.setBackgroundColour((colours.red))
  318. monitor.setCursorPos(24,1+i)
  319. monitor.write(" ")
  320. monitor.setBackgroundColour((colours.black))
  321. end
  322. elseif getMonitorSize(monitor.getSize()) == "small" then
  323. --erase old data
  324. monitor.setCursorPos(10,3)
  325. monitor.write(" ")
  326. monitor.setCursorPos(10,5)
  327. monitor.write(" ")
  328. --write constant/new data
  329. monitor.setCursorPos(2,2)
  330. monitor.write("Engines:")
  331. monitor.setCursorPos(11,2)
  332. monitor.write("Storage:")
  333. monitor.setCursorPos(eNowXSmall,3)
  334. monitor.write(eNowValue..eNowSuffixSmall)
  335. monitor.setCursorPos(eMaxXSmall,4)
  336. monitor.write("of:")
  337. monitor.setCursorPos(eMaxXSmall,5)
  338. monitor.write(eMaxValue..eMaxSuffixSmall)
  339. if fill > upper then
  340. --Energy level is over upper level, turning redstone/reactors off
  341. if redstoneSide ~= "none" then redstone.setOutput(redstoneSide, false) end
  342. if turbine ~= nil then turbine.setActive(false) end
  343. -- if reactor ~= nil then reactor.setActive(false) end
  344. if reactor ~= nil then reactor.setAllControlRodLevels(100) end
  345. monitor.setBackgroundColour((colours.grey))
  346. monitor.setCursorPos(1,4)
  347. monitor.write(" ON ")
  348. monitor.setBackgroundColour((colours.green))
  349. monitor.setCursorPos(5,4)
  350. monitor.write(" OFF ")
  351. monitor.setBackgroundColour((colours.black))
  352. elseif fill < lower then
  353. --Energy level is below lower limit, turning redstone/reactors on
  354. if redstoneSide ~= "none" then redstone.setOutput(redstoneSide, true) end
  355. if turbine ~= nil then turbine.setActive(true) end
  356. -- if reactor ~= nil then reactor.setActive(true) end
  357. if reactor ~= nil then reactor.setAllControlRodLevels(0) end
  358. monitor.setBackgroundColour((colours.green))
  359. monitor.setCursorPos(1,4)
  360. monitor.write(" ON ")
  361. monitor.setBackgroundColour((colours.grey))
  362. monitor.setCursorPos(5,4)
  363. monitor.write(" OFF ")
  364. monitor.setBackgroundColour((colours.black))
  365. end
  366. end
  367. end
  368. sleep(1)
  369. end --while
Add Comment
Please, Sign In to add comment