Guest User

Untitled

a guest
Oct 28th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1. -- Reactor/Capacitor Management ComputerCraft script
  2. -- by viveleroi 2014
  3. -- Automatically checks for wired attachements for EnderIO capacitor bank, monitor, big reactor
  4. -- Will automatically active reactor when capacitor bank too low, or deactive when too high
  5. -- Connected devices take priority over modem devices
  6. -- Requires 3x3 monitors (2x3 if less than 4 reactors)
  7.  
  8. -- based on code: http://pastebin.com/rjfa4ymR
  9.  
  10. local version = 0.2
  11.  
  12. print("Reactor/Capacitor Manager v"..version)
  13.  
  14. local file = fs.open("startup", "w")
  15. file.write("shell.run(\"capcheck\")")
  16. file.close()
  17.  
  18. local upper = 0.95 --Upper limit for computer to stop transmitting redstone signal. 0.90=90% full.
  19. local lower = 0.20 --Lower limit for computer to start transmitting redstone signal.
  20.  
  21. function round2(num, idp)
  22. local mult = 10^(idp or 0)
  23. return math.floor(num * mult + 0.5) / mult
  24. end
  25.  
  26. --Device detection
  27. isError=0
  28.  
  29. function detectDevice(DeviceName)
  30. deviceFace="none"
  31. for k,v in pairs(redstone.getSides()) do
  32. if peripheral.getType(v)==DeviceName then
  33. deviceFace = v
  34. break
  35. end
  36. end
  37. return(deviceFace)
  38. end
  39.  
  40. cell = "none"
  41. monitor = "none"
  42. local peripheralList = peripheral.getNames()
  43.  
  44. -- detect energy storage cell
  45. capacitorFace=detectDevice("cofh_thermalexpansion_energycell")
  46. if capacitorFace~="none" then
  47. cell=peripheral.wrap(capacitorFace)
  48. print ("TE Energy cell on the " .. capacitorFace .. " connected.")
  49. else
  50. capacitorFace=detectDevice("tile_enderio_blockcapacitorbank_name")
  51. if capacitorFace~="none" then
  52. cell=peripheral.wrap(capacitorFace)
  53. print ("EnderIO capacitorbank on the " .. capacitorFace .. " connected.")
  54. else
  55. for Index = 1, #peripheralList do
  56. if string.find(peripheralList[Index], "cofh_thermalexpansion_energycell") then
  57. cell=peripheral.wrap(peripheralList[Index])
  58. print ("TE Energy cell on wired modem: "..peripheralList[Index].." connected.")
  59. elseif string.find(peripheralList[Index], "tile_enderio_blockcapacitorbank_name") then
  60. cell=peripheral.wrap(peripheralList[Index])
  61. print ("EnderIO capacitorbank on wired modem: "..peripheralList[Index].." connected.")
  62. end
  63. end
  64. if cell == "none" then
  65. print("No Energy storage found. Halting script!")
  66. return
  67. end
  68. end
  69. end
  70.  
  71. -- detect reactors
  72. reactors = {}
  73. reactorFace=detectDevice("BigReactors-Reactor")
  74. if reactorFace~="none" then
  75. table.insert(reactors,peripheral.wrap(reactorFace))
  76. print ("Reactor on the " .. reactorFace .. " connected.")
  77. else
  78. for Index = 1, #peripheralList do
  79. if string.find(peripheralList[Index], "Reactor") then
  80. table.insert(reactors,peripheral.wrap(peripheralList[Index]))
  81. print ("Reactor on wired modem: "..peripheralList[Index].." connected.")
  82. end
  83. end
  84. if table.getn(reactors) == 0 then
  85. print("No reactors found. Halting script!")
  86. return
  87. end
  88. end
  89.  
  90. -- detect monitor
  91. MonitorSide=detectDevice("monitor")
  92. if MonitorSide~="none" then
  93. monitor=peripheral.wrap(MonitorSide)
  94. print ("Monitor on the " .. MonitorSide .. " connected.")
  95. else
  96. for Index = 1, #peripheralList do
  97. if string.find(peripheralList[Index], "monitor") then
  98. monitor=peripheral.wrap(peripheralList[Index])
  99. print ("Monitor on wired modem: "..peripheralList[Index].." connected.")
  100. end
  101. end
  102. if monitor == "none" then
  103. print("No monitor found. Halting script!")
  104. return
  105. end
  106. end
  107.  
  108. -- default to no redstone signal
  109. redstone.setOutput("back", false)
  110.  
  111. function setReactorState( state )
  112. for i, reactor in ipairs(reactors) do
  113. reactor.setActive(state)
  114. end
  115. end
  116.  
  117. -- turn all reactors off
  118. setReactorState(false)
  119.  
  120. -- --If monitor is attached, write data on monitor
  121. monitor.clear()
  122.  
  123. monitor.setCursorPos(1,6)
  124. monitor.write("Reactors")
  125.  
  126. monitor.setCursorPos(10,8)
  127. monitor.write("Buff")
  128.  
  129. monitor.setCursorPos(16,8)
  130. monitor.write("Temp")
  131.  
  132. monitor.setCursorPos(22,8)
  133. monitor.write("Output")
  134.  
  135. eLast = 0;
  136.  
  137. while true do
  138.  
  139. -- check energy
  140. eNow = cell.getEnergyStored("unknown")
  141. eMax = cell.getMaxEnergyStored("unknown")
  142. fill = (eNow / eMax)
  143. perc = fill*100
  144. diff = eNow - eLast
  145. eLast = eNow;
  146.  
  147. displayStorage = math.ceil(eNow)
  148. displayCapacity = math.ceil(eMax)
  149. displayFill = round2(perc,2).."%"
  150. displayChange = round2(diff,2)
  151.  
  152. -- over a billion
  153. if eMax >= 1000000000 then
  154. displayCapacity = math.ceil(eMax/1000000000).."bRF"
  155. end
  156.  
  157. -- over a million
  158. if eMax >= 1000000 then
  159. displayStorage = math.ceil(eNow/1000).."kRF"
  160. displayCapacity = math.ceil(eMax/1000).."kRF"
  161. displayChange = round2(diff/1000,2).."k"
  162. end
  163.  
  164. monitor.setCursorPos(1,1)
  165. monitor.write("Storage:")
  166. monitor.setCursorPos(1,2)
  167. monitor.clearLine()
  168. monitor.setBackgroundColour((colours.blue))
  169. monitor.write(displayStorage)
  170. monitor.setBackgroundColour((colours.black))
  171. monitor.setCursorPos(1,3)
  172. monitor.write("Capacity:")
  173. monitor.setCursorPos(1,4)
  174. monitor.clearLine()
  175. monitor.setBackgroundColour((colours.blue))
  176. monitor.write(displayCapacity)
  177. monitor.setBackgroundColour((colours.black))
  178. monitor.setCursorPos(16,1)
  179. monitor.write("Fill:")
  180. monitor.setCursorPos(16,2)
  181. monitor.setBackgroundColour((colours.cyan))
  182. monitor.write(displayFill)
  183. monitor.setBackgroundColour((colours.black))
  184. monitor.setCursorPos(16,3)
  185. monitor.write("Change:")
  186. monitor.setCursorPos(16,4)
  187. monitor.setBackgroundColour((colours.cyan))
  188. monitor.write(displayChange)
  189. monitor.setBackgroundColour((colours.black))
  190.  
  191. if fill > upper then
  192. redstone.setOutput("back", false)
  193. setReactorState(false)
  194. elseif fill < lower then
  195. redstone.setOutput("back", true)
  196. setReactorState(true)
  197. end
  198.  
  199. cursorY = 9
  200.  
  201. -- reactor status
  202. for i, reactor in ipairs(reactors) do
  203. monitor.setCursorPos(1,cursorY)
  204. monitor.clearLine()
  205. monitor.write(i..":")
  206. monitor.setCursorPos(4,cursorY)
  207.  
  208. if reactor.getActive() then
  209. monitor.setBackgroundColour((colours.green))
  210. monitor.write(" ON ")
  211. monitor.setBackgroundColour((colours.black))
  212. else
  213. monitor.setBackgroundColour((colours.red))
  214. monitor.write(" OFF ")
  215. monitor.setBackgroundColour((colours.black))
  216. end
  217.  
  218. -- internal buffer
  219. displayEnergyBuffer = math.floor((reactor.getEnergyStored()/10000000)*100).."%"
  220. monitor.setCursorPos(10,cursorY)
  221. monitor.write(displayEnergyBuffer)
  222.  
  223. -- temp
  224. displayTemperature = math.ceil(reactor.getCasingTemperature()).."c"
  225. if reactor.getCasingTemperature() > 1000 then
  226. displayTemperature = round2(reactor.getCasingTemperature()/1000,2).."k c"
  227. end
  228.  
  229. monitor.setCursorPos(16,cursorY)
  230. monitor.write(displayTemperature)
  231.  
  232. -- energy produced
  233. displayEnergyProduced = round2(reactor.getEnergyProducedLastTick(),2).."RF"
  234. if reactor.getEnergyProducedLastTick() > 1000 then
  235. displayEnergyProduced = round2(reactor.getEnergyProducedLastTick()/1000,2).."kRF"
  236. end
  237. monitor.setCursorPos(22,cursorY)
  238. monitor.write(displayEnergyProduced)
  239.  
  240. cursorY = cursorY + 1
  241.  
  242. end
  243.  
  244. sleep(1)
  245.  
  246. end
Add Comment
Please, Sign In to add comment