Advertisement
MaximumFrank

reactor

Mar 26th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.70 KB | None | 0 0
  1. local r = peripheral.find("BigReactors-Reactor")
  2. local tempEn = 0
  3. local targOut = -1
  4. local buttonColL = colors.lightGray
  5. local buttonColR = colors.lightGray
  6. local justStarted = false
  7. local starter = os.startTimer(2)
  8. local upperBound = 10000000 * 0.95
  9. local lowerBound = 10000000 * 0.75
  10.  
  11. if not r.getActive() then
  12. r.setActive(true)
  13. end
  14.  
  15. function calibrate()
  16. term.clear()
  17. term.setCursorPos(1, 1)
  18. print("Sleeping 20 seconds for calibration test #1")
  19. r.setAllControlRodLevels(0)
  20. sleep(20)
  21. local temp = r.getEnergyProducedLastTick()
  22. if temp > targOut then
  23. if temp - targOut > 25 then
  24. term.clear()
  25. term.setCursorPos(1, 1)
  26. print("Sleeping 20 seconds for calibration test #2")
  27. r.setAllControlRodLevels(50)
  28. sleep(20)
  29. local temp2 = r.getEnergyProducedLastTick()
  30.  
  31. r.setAllControlRodLevels((-0.000326859908360141 * targOut^2) + (0.0181019409748607 * targOut) + 83.7118241059938)
  32. sleep(20)
  33. end
  34. end
  35. end
  36.  
  37. function drawRect(x1, y1, x2, y2, bgCol)
  38. local i = 0
  39. local ii = 0
  40.  
  41. term.setBackgroundColor(bgCol)
  42.  
  43. for ii = y1, y2 do
  44. for i = x1, x2 do
  45. term.setCursorPos(i, ii)
  46. term.write(" ")
  47. end
  48. end
  49. end
  50.  
  51. function drawBarY(x1, y1, x2, y2, max, cur, col1, col2)
  52. local i = 0
  53. local ii = 0
  54.  
  55. term.setBackgroundColor(col1)
  56.  
  57. for i = y1, y2 do
  58. term.setCursorPos(x1, i)
  59. term.write(" ")
  60. end
  61.  
  62. if cur ~= 0 then
  63. term.setBackgroundColor(col2)
  64.  
  65. local perPoint = y1 + math.ceil((y2 - y1) / (max / cur))
  66.  
  67. for ii = y1, y2 do
  68. for i = x1, x2 do
  69. if ii <= perPoint then
  70. term.setCursorPos(i, ii)
  71. term.write(" ")
  72. end
  73. end
  74. end
  75. end
  76. end
  77.  
  78. function drawBarX(x1, y1, x2, y2, max, cur, col1, col2)
  79. local i = 0
  80. local ii = 0
  81.  
  82. term.setBackgroundColor(col1)
  83.  
  84. for i = x1, x2 do
  85. term.setCursorPos(i, y1)
  86. term.write(" ")
  87. end
  88.  
  89. term.setBackgroundColor(col2)
  90.  
  91. local perPoint = x1 + math.ceil((x2 - x1) / (max / cur))
  92.  
  93. for ii = y1, y2 do
  94. for i = x1, x2 do
  95. if i <= perPoint then
  96. term.setCursorPos(i, ii)
  97. term.write(" ")
  98. end
  99. end
  100. end
  101. end
  102.  
  103. function drawFuelBar(x1, y1, x2, y2, max, fCur, wCur, bgCol, fCol, wCol)
  104. drawBarX(x1, y1, x2, y2, max, fCur + wCur, bgCol, fCol)
  105.  
  106. term.setBackgroundColor(wCol)
  107.  
  108. if wCur ~= 0 then
  109. local temp = x1 + math.ceil((x2 - x1) / (max / wCur))
  110.  
  111. for ii = y1, y2 do
  112. for i = x1, x2 do
  113. if i <= temp then
  114. term.setCursorPos(i, ii)
  115. term.write(" ")
  116. end
  117. end
  118. end
  119. end
  120. end
  121.  
  122. function buttons(timer)
  123. local doit = true
  124.  
  125. while doit do
  126. local event, side, x, y = os.pullEvent()
  127. if event == "monitor_touch" then
  128. if x > 18 and x < 22 and y == 10 then
  129. if targOut > 50 then
  130. targOut = targOut - 50
  131. else
  132. targOut = -1
  133. calibrate()
  134. end
  135. os.cancelTimer(timer)
  136. buttonColL = colors.gray
  137. doit = false
  138. elseif targOut ~= -1 then
  139. if x > 21 + string.len(" " .. tostring(targOut) .. " ") and x < 21 + string.len(" " .. tostring(targOut) .. " ") + 4 and y == 10 then
  140. targOut = targOut + 50
  141. buttonColR = colors.gray
  142. doit = false
  143. os.cancelTimer(timer)
  144. end
  145. elseif targOut == -1 then
  146. if x > 21 + string.len(" Unlimited ") and x < 21 + string.len(" Unlimited ") + 4 and y == 10 then
  147. targOut = 50
  148. buttonColR = colors.gray
  149. doit = false
  150. os.cancelTimer(timer)
  151. end
  152. end
  153. elseif event == "timer" then
  154. if side == "starter" then
  155. justStarted = false
  156. else
  157. doit = false
  158. os.cancelTimer(timer)
  159. end
  160. end
  161. end
  162. end
  163.  
  164. local mon = peripheral.find("monitor")
  165.  
  166. term.redirect(mon)
  167. mon.setTextScale(0.5)
  168.  
  169. if targOut ~= -1 then
  170. calibrate()
  171. else
  172. r.setAllControlRodLevels(0)
  173. end
  174.  
  175. local timer = os.startTimer(0.05)
  176.  
  177. local w, h = term.getSize()
  178.  
  179. --set control rods to uniform level
  180. local controlNum = r.getNumberOfControlRods()
  181.  
  182. local rodLevel = 0
  183.  
  184. run = true
  185.  
  186. while run do
  187. term.clear()
  188. --gather basic information
  189. local isActive = r.getActive()
  190. local storedEn = r.getEnergyStored()
  191.  
  192. rodLevel = r.getControlRodLevel(0)
  193.  
  194. if tempEn == 0 then
  195. tempEn = r.getEnergyStored()
  196. end
  197.  
  198. local fuelTemp = math.ceil(r.getFuelTemperature() * 100) * 0.01
  199. local caseTemp = math.ceil(r.getCasingTemperature() * 100) * 0.01
  200.  
  201. local fuelAmnt = r.getFuelAmount()
  202. local fuelMax = r.getFuelAmountMax()
  203. local wasteAmnt = r.getWasteAmount()
  204.  
  205. local produce = (tempEn - storedEn)
  206.  
  207. if produce <= targOut + 25 and produce >= targOut - 25 and isActive then
  208. justStarted = false
  209. end
  210.  
  211. local fuelUse = math.ceil(r.getFuelConsumedLastTick() * 100) * 0.01
  212.  
  213. --top left
  214. drawRect(2, 2, w - 20, 2, colors.lightGray)
  215.  
  216. term.setBackgroundColor(colors.black)
  217. term.setTextColor(colors.white)
  218. term.setCursorPos((2 + (w - 20) - string.len(" General Information ")) / 2, 2)
  219. term.write(" General Information ")
  220.  
  221. drawRect(2, 2, 2, 12, colors.lightGray)
  222. drawRect(2, 12, w - 20, 12, colors.lightGray)
  223. drawRect(w - 20, 2, w - 20, 12, colors.lightGray)
  224.  
  225. --middle left
  226. drawRect(2, 14, w - 20, 14, colors.lightGray)
  227.  
  228. term.setBackgroundColor(colors.black)
  229. term.setTextColor(colors.white)
  230. term.setCursorPos((2 + (w - 20) - string.len(" Fuel Information ")) / 2, 14)
  231. term.write(" Fuel Information ")
  232.  
  233. drawRect(2, 14, 2, h - 1, colors.lightGray)
  234. drawRect(2, h - 1, w - 20, h - 1, colors.lightGray)
  235. drawRect(w - 20, 14, w - 20, h - 1, colors.lightGray)
  236.  
  237. --right
  238. drawRect(w - 18, 2, w - 1, 2, colors.lightGray)
  239.  
  240. term.setBackgroundColor(colors.black)
  241. term.setTextColor(colors.white)
  242.  
  243. if controlNum > 1 then
  244. term.setCursorPos((w - 18 + (w - 1) - string.len(" Control Rods ")) / 2, 2)
  245. term.write(" Control Rods ")
  246. else
  247. term.setCursorPos((w - 18 + (w - 1) - string.len(" Control Rod ")) / 2, 2)
  248. term.write(" Control Rod ")
  249. end
  250.  
  251. drawRect(w - 18, 2, w - 18, h - 1, colors.lightGray)
  252. drawRect(w - 18, h - 1, w - 1, h - 1, colors.lightGray)
  253. drawRect(w - 1, 2, w - 1, h - 1, colors.lightGray)
  254.  
  255. term.setBackgroundColor(colors.black)
  256. term.setTextColor(colors.white)
  257.  
  258. term.setCursorPos(4, 4)
  259. write("Reactor is ")
  260. if isActive then
  261. term.setTextColor(colors.green)
  262. write("On")
  263. else
  264. term.setTextColor(colors.red)
  265. write("Off")
  266. end
  267.  
  268. term.setCursorPos(4, 5)
  269. term.setTextColor(colors.white)
  270. write("Core Temperature: ")
  271. term.setTextColor(colors.yellow)
  272. write(fuelTemp)
  273. term.setTextColor(colors.white)
  274. write(" C")
  275.  
  276. term.setCursorPos(4, 6)
  277. term.setTextColor(colors.white)
  278. write("Case Temperature: ")
  279. term.setTextColor(colors.yellow)
  280. write(caseTemp)
  281. term.setTextColor(colors.white)
  282. write(" C")
  283.  
  284. term.setCursorPos(4, 7)
  285. term.setTextColor(colors.white)
  286. write("Stored Energy: ")
  287. if storedEn >= 5000000 then
  288. term.setTextColor(colors.green)
  289. elseif storedEn >= 2500000 then
  290. term.setTextColor(colors.orange)
  291. else
  292. term.setTextColor(colors.red)
  293. end
  294. write(storedEn)
  295. term.setTextColor(colors.white)
  296. write(" RF")
  297.  
  298. term.setCursorPos(4, 8)
  299. term.setTextColor(colors.white)
  300. write("Energy Per Tick: ")
  301.  
  302. if produce < 0 then
  303. term.setTextColor(colors.green)
  304. write(math.abs(produce))
  305. else
  306. if produce == 0 and storedEn >= upperBound and isActive then
  307. term.setTextColor(colors.green)
  308. write(produce)
  309. r.setActive(false)
  310. justStarted = true
  311. starter = os.startTimer(20)
  312. else
  313. term.setTextColor(colors.red)
  314. write(produce)
  315. end
  316. end
  317.  
  318. term.setTextColor(colors.white)
  319. write(" RF")
  320.  
  321. term.setCursorPos(4, 10)
  322. term.setTextColor(colors.white)
  323. write("Target Output: ")
  324. term.setBackgroundColor(buttonColL)
  325. term.setTextColor(colors.black)
  326. write(" < ")
  327. term.setTextColor(colors.yellow)
  328. term.setBackgroundColor(colors.black)
  329. if targOut ~= -1 then
  330. write(" " .. tostring(targOut) .. " ")
  331. else
  332. write(" Unlimited ")
  333. end
  334.  
  335. term.setBackgroundColor(buttonColR)
  336. term.setTextColor(colors.black)
  337. write(" > ")
  338.  
  339. term.setBackgroundColor(colors.black)
  340.  
  341. term.setTextColor(colors.blue)
  342. term.setCursorPos(4, 19)
  343. write("Waste: ")
  344. if wasteAmnt >= fuelMax / 2 then
  345. term.setTextColor(colors.red)
  346. write(wasteAmnt)
  347. elseif wasteAmnt >= fuelMax / 4 then
  348. term.setTextColor(colors.orange)
  349. write(wasteAmnt)
  350. else
  351. term.setTextColor(colors.green)
  352. write(wasteAmnt)
  353. end
  354.  
  355. term.setTextColor(colors.white)
  356. write(" mB")
  357.  
  358. term.setTextColor(colors.cyan)
  359. term.setCursorPos(4, 20)
  360. write("Fuel: ")
  361. if fuelAmnt >= fuelMax / 2 then
  362. term.setTextColor(colors.green)
  363. write(fuelAmnt)
  364. elseif fuelAmnt >= fuelMax / 4 then
  365. term.setTextColor(colors.orange)
  366. write(fuelAmnt)
  367. else
  368. term.setTextColor(colors.red)
  369. write(fuelAmnt)
  370. end
  371.  
  372. term.setTextColor(colors.white)
  373. write(" mB")
  374.  
  375. term.setTextColor(colors.white)
  376. term.setCursorPos(4, 21)
  377. write("Free Space: ")
  378. term.setTextColor(colors.yellow)
  379. write(fuelMax - (fuelAmnt + wasteAmnt))
  380.  
  381. if storedEn <= lowerBound and isActive == false then
  382. r.setActive(true)
  383. end
  384.  
  385. term.setTextColor(colors.white)
  386. write(" mB")
  387.  
  388. drawFuelBar(4, 17, w - 22, 17, fuelMax, fuelAmnt, wasteAmnt, colors.white, colors.cyan, colors.blue)
  389.  
  390. drawRect(w - 7, 4, w - 4, h - 3, colors.lightGray)
  391. drawBarY(w - 5, 5, w - 5, h - 4, 100, rodLevel, colors.cyan, colors.blue)
  392. drawBarY(w - 6, 5, w - 6, h - 4, 100, rodLevel, colors.cyan, colors.blue)
  393.  
  394. term.setBackgroundColor(colors.black)
  395. term.setTextColor(colors.white)
  396.  
  397. term.setCursorPos(w - 17 + (((w - 9) - (w - 17)) - string.len(tostring(rodLevel) .. "%")) / 2, h / 2)
  398. write(rodLevel .. "%")
  399.  
  400. buttonColL = colors.lightGray
  401. buttonColR = colors.lightGray
  402.  
  403. tempEn = r.getEnergyStored()
  404.  
  405. if (r.getEnergyProducedLastTick() < targOut - 25 or r.getEnergyProducedLastTick() > targOut + 25) and isActive and justStarted == false and targOut ~= -1 then
  406. calibrate()
  407. timer = os.startTimer(0.05)
  408. end
  409.  
  410. buttons(timer)
  411.  
  412. timer = os.startTimer(0.05)
  413. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement