Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.50 KB | None | 0 0
  1. -- -------------------------------------------------------------------------- --
  2. -- ReactorHelp : ZRRgin05
  3. --
  4. -- ComputerCraft script to regulate BigReactor control rods based on power drain
  5. -- to minimise fuel use.
  6. --
  7. -- -------------------------------------------------------------------------- --
  8. --
  9. -- -------------------------------------------------------------------------- --
  10.  
  11. -- -------------------------------------------------------------------------- --
  12. --
  13. -- The reactor, adjust name to suit.
  14. --
  15. local reactor = peripheral.wrap( 'BigReactors-Reactor_2' )
  16.  
  17. -- -------------------------------------------------------------------------- --
  18. --
  19. -- Variables
  20. --
  21. local running = true -- Loop control
  22. local rodCount = reactor.getNumberOfControlRods() -- Number of control rods
  23. local bufferState -- Energy in the buffer
  24. local fuelAvailable -- Available fuel
  25. local cntIndex
  26. local cntTarget
  27. local cntSelect
  28.  
  29. -- -------------------------------------------------------------------------- --
  30. --
  31. -- Functions
  32. --
  33.  
  34. --[[
  35. *
  36. * Return a "HH:MM:SS" timestmap
  37. *
  38. ]]--
  39. local function showTime()
  40. local ts = os.time()
  41.  
  42. local hh = math.floor( ts )
  43. ts = math.floor( ( ts - hh ) * ( 60 * 60 ) )
  44.  
  45. local mm = math.floor( ts / 60 )
  46. local ss = ts - ( mm * 60 )
  47.  
  48. return io.write( string.format( "%02d:%02d:%02d", hh, mm, ss ) )
  49. end -- showTime
  50.  
  51. --[[
  52. *
  53. * Return "OFF" or "ON " in Red or Green...
  54. *
  55. ]]--
  56. local function showActive()
  57. if reactor.getActive() then
  58. term.setTextColor( colors.green ); io.write( "ON" )
  59. else
  60. term.setTextColor( colors.red ); io.write( "OFF" )
  61. end
  62. end -- showActive
  63.  
  64. --[[
  65. *
  66. * Return reaction rate in Red, Green or Orange
  67. *
  68. ]]--
  69. local function showReaction()
  70. local x = reactor.getFuelReactivity()
  71. if x > 200 then
  72. if x > 300 then
  73. term.setTextColor( colors.green ); io.write( string.format( "%03d%%", x ) )
  74. else
  75. term.setTextColor( colors.orange ); io.write( string.format( "%03d%%", x ) )
  76. end
  77. else
  78. term.setTextColor( colors.red ); io.write( string.format( "%03d%%", x ) )
  79. end
  80. end -- showActive
  81.  
  82. --[[
  83. *
  84. * Return the core temperature
  85. *
  86. ]]--
  87. local function showTemperature()
  88. return io.write( string.format( "%d oC", math.floor( math.floor( reactor.getFuelTemperature() * 100 ) / 100 ) ) )
  89. end -- showTemperature
  90.  
  91. --[[
  92. *
  93. * Return the output rate in RF/t (with SI prefix, "XXXx")
  94. *
  95. ]]--
  96. local function showPowerRate()
  97. local z = math.floor( reactor.getEnergyProducedLastTick() * 1000 )
  98. local y = 1
  99.  
  100. --[[
  101. 1 : 0.999 : 999mRF
  102. 2 : 9.990 : 9 RF
  103. 3 : 99.900 : 99 RF
  104. 4 : 999.000 : 999 RF
  105. 5 : 9,990.000 : 9 kRF
  106. 6 : 99,900.000 : 99 k
  107. 7 : 999,000.000 : 999 k
  108. 8 : 9,990,000.000 : 9 M
  109. 9 : 99,900,000.000 : 99 M
  110. 10 : 999,000,000.000 : 999 M
  111. 11 : 9,990,000,000.000 : 9 G
  112. 12 : 99,900,000,000.000 : 99 G
  113. ]]--
  114. while z > 999 do
  115. z = z / 10
  116. y = y + 1
  117. end
  118.  
  119. if y == 1 then
  120. --[[ 0-999 m B/t ]]--
  121. term.setTextColor( colors.lightGray )
  122. return io.write( string.format( "%3dm", z ) )
  123. elseif y < 5 then
  124. --[[ 0-999 B/t ]]--
  125. term.setTextColor( colors.yellow )
  126. return io.write( string.format( "%3d ", z ) )
  127. elseif y < 8 then
  128. --[[ 0-999 k B/t ]]--
  129. term.setTextColor( colors.orange )
  130. return io.write( string.format( "%3dk", z ) )
  131. elseif y < 11 then
  132. --[[ 0-999 M B/t ]]--
  133. term.setTextColor( colors.red )
  134. return io.write( string.format( "%3dM", z ) )
  135. else
  136. --[[ >0 G B/t ]]--
  137. term.setTextColor( colors.white )
  138. return io.write( string.format( "%3dG", z ) )
  139. end
  140. end -- showPowerRate
  141.  
  142. --[[
  143. *
  144. * Return the control rate
  145. *
  146. ]]--
  147. local function showControlRate( x )
  148. local y = math.floor( x )
  149. if y == 0 then
  150. term.setTextColor( colors.lightGray )
  151. elseif y < 30 then
  152. term.setTextColor( colors.lightBlue )
  153. elseif y < 60 then
  154. term.setTextColor( colors.yellow )
  155. elseif y < 80 then
  156. term.setTextColor( colors.orange )
  157. elseif y < 90 then
  158. term.setTextColor( colors.red )
  159. else
  160. term.setTextColor( colors.white )
  161. end
  162. return io.write( string.format( "%3d", y ) )
  163. end -- showControlRate
  164.  
  165. --[[
  166. *
  167. * Return the fuel burn rate in B/t (with SI prefix)
  168. *
  169. ]]--
  170. local function showBurnRate()
  171. local z = math.floor( reactor.getFuelConsumedLastTick() * 1000 )
  172. local y = 1
  173.  
  174. --[[
  175. 1 : 0.999 : 999mRF
  176. 2 : 9.990 : 9 RF
  177. 3 : 99.900 : 99 RF
  178. 4 : 999.000 : 999 RF
  179. 5 : 9,990.000 : 9 kRF
  180. 6 : 99,900.000 : 99 k
  181. 7 : 999,000.000 : 999 k
  182. 8 : 9,990,000.000 : 9 M
  183. 9 : 99,900,000.000 : 99 M
  184. 10 : 999,000,000.000 : 999 M
  185. 11 : 9,990,000,000.000 : 9 G
  186. 12 : 99,900,000,000.000 : 99 G
  187. ]]--
  188. while z > 999 do
  189. z = z / 10
  190. y = y + 1
  191. end
  192.  
  193. if y == 1 then
  194. --[[ 0-999 m RF/t ]]--
  195. term.setTextColor( colors.lightGray )
  196. return io.write( string.format( "%3dm", z ) )
  197. elseif y < 5 then
  198. --[[ 0-999 RF/t ]]--
  199. term.setTextColor( colors.yellow )
  200. return io.write( string.format( "%3d ", z ) )
  201. elseif y < 8 then
  202. --[[ 0-999 k RF/t ]]--
  203. term.setTextColor( colors.orange )
  204. return io.write( string.format( "%3dk", z ) )
  205. elseif y < 11 then
  206. --[[ 0-999 M RF/t ]]--
  207. term.setTextColor( colors.red )
  208. return io.write( string.format( "%3dM", z ) )
  209. else
  210. --[[ >0 G RF/t ]]--
  211. term.setTextColor( colors.white )
  212. return io.write( string.format( "%3dG", z ) )
  213. end
  214. end -- showBurnRate
  215.  
  216. --[[
  217. *
  218. * Draw a bar graph of the control factor, x...
  219. *
  220. ]]--
  221. local function showControlBar( x )
  222. local n = math.floor( 30 * ( x / 100 ) )
  223. local t = ""
  224.  
  225. for i=0,(n-1) do t = t .. " " end
  226. term.setBackgroundColor( colors.lightBlue )
  227. term.setTextColor( colors.lightGray )
  228. return io.write( t )
  229. end -- showControlBar
  230.  
  231. --[[
  232. *
  233. * Draw a bar graph of the fuel buffer
  234. *
  235. ]]--
  236. local function showFuelBar()
  237. local n = math.floor( 30 * ( reactor.getFuelAmount() / reactor.getFuelAmountMax() ) )
  238. local t = ""
  239.  
  240. for i=0,(n-1) do t = t .. " " end
  241. term.setBackgroundColor( colors.cyan )
  242. term.setTextColor( colors.lightGray )
  243. return io.write( t )
  244. end -- showFuelBar
  245.  
  246. --[[
  247. *
  248. * Draw a bar graph of the energy buffer
  249. *
  250. ]]--
  251. local function showPowerBar()
  252. local n = math.floor( 30 * ( reactor.getEnergyStored() / 10000000 ) )
  253. local t = ""
  254.  
  255. for i=0,(n-1) do t = t .. " " end
  256. term.setBackgroundColor( colors.blue )
  257. term.setTextColor( colors.lightGray )
  258. return io.write( t )
  259. end -- showPowerBar
  260.  
  261. -- -------------------------------------------------------------------------- --
  262. -- -------------------------------------------------------------------------- --
  263. -- -------------------------------------------------------------------------- --
  264.  
  265.  
  266.  
  267. -- -------------------------------------------------------------------------- --
  268. --
  269. -- Sanity checks...
  270. --
  271. if not term.isColor() then
  272. term.clear()
  273. term.setCursorPos( 1, 1 )
  274. write( "==[ Need Advanced Computer! ]==" )
  275. running = false
  276. end
  277. if not reactor.getConnected() then
  278. term.clear()
  279. term.setBackgroundColor( colors.orange )
  280. term.setTextColor( colors.red )
  281. term.setCursorPos( 1, 1 )
  282. write( "==[ Not Connected to Reactor ]==" )
  283. term.setBackgroundColor( colors.black )
  284. term.setTextColor( colors.white )
  285. running = false
  286. end
  287.  
  288. -- -------------------------------------------------------------------------- --
  289. --
  290. -- Main loop
  291. --
  292. while running do
  293.  
  294. --[[
  295. *
  296. * Collect information
  297. *
  298. ]]--
  299. bufferState = reactor.getEnergyStored()
  300. fuelAvailable = reactor.getFuelAmount()
  301.  
  302. --[[
  303. *
  304. * Draw the screen.
  305. *
  306. ]]--
  307. term.setBackgroundColor( colors.black ); term.clear()
  308. term.setTextColor( colors.cyan )
  309. term.setCursorPos( 1, 1 ); write( "==[" )
  310. term.setCursorPos( 17, 1 ); write( ".:." )
  311. term.setCursorPos( 30, 1 ); write( "]=====[" )
  312. term.setCursorPos( 42, 1 ); write( "|" )
  313. term.setCursorPos( 49, 1 ); write( "]==" )
  314. term.setCursorPos( 1, 2 ); write( "===================================================" )
  315. term.setCursorPos( 1, 13 ); write( "==============================================[Q]==" )
  316. term.setTextColor( colors.white )
  317. term.setCursorPos( 5, 1 ); write( "ReactorHelp" )
  318. term.setCursorPos( 21, 1 ); showTime()
  319. term.setCursorPos( 2, 9 ); write( "Core Temp. " ); showTemperature()
  320. term.setCursorPos( 2, 10 ); write( "Rod Count "..rodCount )
  321. term.setTextColor( colors.lime )
  322. term.setCursorPos( 11, 4 ); write( "0% . . . . . 100%" )
  323. term.setCursorPos( 2, 5 ); write( "BUFFER : : RF/t" )
  324. term.setCursorPos( 2, 6 ); write( "CONTROL : : %" )
  325. term.setCursorPos( 2, 7 ); write( "FUEL : : B/t" )
  326.  
  327. -- If we're out of fuel we need to quit
  328. if fuelAvailable < 1000 then
  329. term.setBackgroundColor( colors.red )
  330. term.setTextColor( colors.black )
  331. term.setCursorPos( 3, 14 )
  332. write( "*** Out of Fuel! ***" )
  333. term.setBackgroundColor( colors.black )
  334. reactor.setActive( false )
  335. running = false
  336. else
  337. reactor.setActive( true )
  338. end
  339.  
  340. -- Control is proportional to energy deffecit when we drop below 8MRF.
  341. cntTarget = 100 - math.floor( 100 * ( bufferState / 10000000 ) )
  342. cntSelect = ( cntTarget / 100 ) * rodCount
  343. for cntIndex = 0, rodCount-1 do
  344. if cntIndex < cntSelect then
  345. reactor.setControlRodLevel( cntIndex, 0 )
  346. term.setCursorPos( 13 + ( cntIndex * 3 ), 11 ); write( " "..cntIndex.. " " )
  347. term.setCursorPos( 13 + ( cntIndex * 3 ), 12 ); write( " . " )
  348. elseif cntSelect <= cntIndex then
  349. reactor.setControlRodLevel( cntIndex, 100 )
  350. term.setCursorPos( 13 + ( cntIndex * 3 ), 11 ); write( " "..cntIndex.." " )
  351. term.setCursorPos( 13 + ( cntIndex * 3 ), 12 ); write( " O " )
  352. else
  353. reactor.setControlRodLevel( cntIndex, cntSelect )
  354. term.setCursorPos( 13 + ( cntIndex * 3 ), 11 ); write( " "..cntIndex.." " )
  355. term.setCursorPos( 13 + ( cntIndex * 3 ), 12 ); write( " o " )
  356. end
  357. end
  358. term.setBackgroundColor( colors.blue )
  359. term.setTextColor( colors.white )
  360. term.setCursorPos( 3, 14 )
  361. write( "*** ["..cntSelect.."] = "..cntTarget.."%***" )
  362. term.setBackgroundColor( colors.black )
  363.  
  364. --[[
  365. *
  366. * Draw dynamic stuff.
  367. *
  368. ]]--
  369. term.setCursorPos( 38, 1 ); showActive()
  370. term.setCursorPos( 44, 1 ); showReaction()
  371.  
  372. term.setCursorPos( 11, 5 ); showPowerBar(); term.setBackgroundColor( colors.black )
  373. term.setCursorPos( 42, 5 ); showPowerRate()
  374.  
  375. term.setCursorPos( 11, 6 ); showControlBar( cntTarget ); term.setBackgroundColor( colors.black )
  376. term.setCursorPos( 43, 6 ); showControlRate( cntTarget )
  377.  
  378. term.setCursorPos( 11, 7 ); showFuelBar(); term.setBackgroundColor( colors.black )
  379. term.setCursorPos( 42, 7 ); showBurnRate()
  380.  
  381.  
  382. os.startTimer( 1.5 )
  383. e,k = os.pullEvent()
  384. if e == "key" and k == keys.q then
  385. running = false
  386. else
  387. running = true
  388. end
  389. end -- main loop
  390.  
  391. term.setBackgroundColor( colors.black ); term.setTextColor( colors.white ); term.setCursorPos( 1, 15 )
  392.  
  393. --[[
  394. local isactive = reactor.getActive();
  395. print( isactive );
  396. local buffer = reactor.getEnergyStored()
  397. reactor.setActive(0)
  398. reactor.getNumberOfControlRods()
  399. reactor.setAllControlRodLevels(0)
  400. reactor.setControlRodLevel(0)
  401. reactor.getControlRodLevel(0)
  402. reactor.getFuelTemperature()
  403. reactor.setAllControlRodLevels(0)
  404. ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement