Advertisement
Guest User

Untitled

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