Advertisement
gaussfire

Display Reactor Energy 1.8

Aug 10th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.70 KB | None | 0 0
  1. --== Init ==--
  2. local monitor = peripheral.wrap("monitor_1")
  3. local reactor = peripheral.wrap("BigReactors-Reactor_0")
  4. local echest = peripheral.wrap("ender_chest_1")
  5. local matrix = peripheral.wrap("back")
  6.  
  7. term.redirect(monitor)
  8. term.setBackgroundColor(colors.black)
  9. term.clear()
  10. monitor.setTextScale(.5)
  11. local mw, mh
  12. local refreshRate = 0.15
  13. local refreshTimer = os.startTimer(refreshRate)
  14. local counter = 0
  15.  
  16. local running = true
  17. local mouseDown = false
  18. local mouseX = -1
  19. local mouseY = -1
  20.  
  21. local fillBar --object used for drawing fill bars
  22. local xOffset = 0
  23. local yOffset = 0
  24.  
  25. local rReservesAmount = 0
  26. local rReservesCapacity = echest.getInventorySize() * 64
  27.  
  28.  
  29. --== Functions ==--
  30. local function joulesToRF(value)
  31. return value*0.4
  32. end
  33.  
  34. local function drawFillBar()
  35. --Uses fillBar object vars to draw a fill bar
  36. local fb = fillBar
  37. local ratio = fb.value/fb.valueMax
  38. local tempvar = -1
  39. if (fb.x2 < fb.x1) then
  40. tempvar = fb.x2
  41. fb.x2 = fb.x1
  42. fb.x1 = tempvar
  43. end
  44. if (fb.y2 < fb.y1) then
  45. tempvar = fb.y2
  46. fb.y2 = fb.y1
  47. fb.y1 = tempvar
  48. end
  49. local width = fb.x2 - fb.x1
  50. local height = fb.y2 - fb.y1
  51.  
  52. if (fb.bordered) then
  53. paintutils.drawBox(fb.x1, fb.y1, fb.x2, fb.y2, fb.colBorder)
  54. end
  55. if (fb.dir == 0) then
  56. if (fb.bordered) then
  57. paintutils.drawFilledBox(fb.x1+1, fb.y1+1, fb.x2-1, fb.y2-1, fb.colBG)
  58. if (ratio ~= 0) then
  59. paintutils.drawFilledBox(fb.x1+1, fb.y1+1, fb.x1+1+math.floor((width-2)*ratio), fb.y2-1, fb.colValue)
  60. end
  61. else
  62. paintutils.drawFilledBox(fb.x1, fb.y1, fb.x2, fb.y2, fb.colBG)
  63. if (ratio ~= 0) then
  64. paintutils.drawFilledBox(fb.x1, fb.y1, fb.x1+math.floor(width*ratio), fb.y2, fb.colValue)
  65. end
  66. end
  67. elseif (fb.dir == 1) then
  68. if (fb.bordered) then
  69. paintutils.drawFilledBox(fb.x1+1, fb.y2-1, fb.x2-1, fb.y1+1, fb.colBG)
  70. if (ratio ~= 0) then
  71. paintutils.drawFilledBox(fb.x1+1, fb.y2-1, fb.x2-1, fb.y2-1-math.floor((height-2)*ratio), fb.colValue)
  72. end
  73. else
  74. paintutils.drawFilledBox(fb.x1, fb.y2, fb.x2, fb.y1, fb.colBG)
  75. if (ratio ~= 0) then
  76. paintutils.drawFilledBox(fb.x1, fb.y2, fb.x1, fb.y2-math.floor(height*value), fb.colValue)
  77. end
  78. end
  79. elseif (fb.dir == 2) then
  80. --not needed
  81. elseif (fb.dir == 3) then
  82. --not needed
  83. end
  84. end
  85.  
  86. local function countItemInInventory(periph, itemName)
  87. --Returns the amount of a certain item in a peripheral's inventory
  88. local invSize = periph.getInventorySize()
  89. local amount = 0
  90. for i=1,invSize do
  91. local stack = periph.getStackInSlot(i)
  92. if (stack ~= nil) then
  93. if (stack.name == itemName) then
  94. amount = amount + stack.qty
  95. end
  96. end
  97. end
  98. return amount
  99. end
  100.  
  101. local function drawMultiText(x, y, strTable)
  102. for i=1,#strTable do
  103. term.setCursorPos(x,y+(i-1))
  104. term.write(strTable[i])
  105. end
  106. end
  107.  
  108. local function clearTable(t)
  109. local k = next(t)
  110. while k do
  111. t[k] = nil
  112. k = next(t)
  113. end
  114. end
  115.  
  116. local function handleInput()
  117. mouseDown = false
  118. local event,p1,p2,p3 = os.pullEvent()
  119. if (event == "key") then
  120. --stop program
  121. if (p1 == keys.backspace) then
  122. running = false
  123. end
  124. elseif (event == "monitor_touch") then
  125. mouseDown = true
  126. mouseX = p2
  127. mouseY = p3
  128. elseif (event == "timer" and p1 == refreshTimer) then
  129. refreshTimer = os.startTimer(refreshRate)
  130. end
  131. end
  132.  
  133.  
  134. --== Main ==--
  135. while running do
  136.  
  137. term.setBackgroundColor(colors.black)
  138. term.clear()
  139. mw,mh = term.getSize() --check for resize
  140. fillBar = {x1, y1, x2, y2, value, valueMin, valueMax, dir, bordered, colBG, colValue, colBorder}
  141. --x1,y1,x2,y2: coords for drawing
  142. --value: amount to fill the bar
  143. --valueMin,valueMax: lower&upper bounds for value to fill
  144. --dir: (0,1,2,3) direction the bar will fill towards
  145. --bordered: (true/false) draws a border around the fill bar (inward from the coords given)
  146. --colBG,colValue,colBorder: which color to draw said element of the fill bar
  147.  
  148. --ASCII TEXT FORMATTING--
  149. local multitext = {}
  150. multitext[1] = " _ _ _ ___ _ _"
  151. multitext[2] = "|_) |_ /\\ / | / \\ |_)"
  152. multitext[3] = "| \\ |_ /--\\ \\_ | \\_/ | \\"
  153. drawMultiText(1,1,multitext)
  154. clearTable(multitext)
  155.  
  156. xOffset = math.floor(mw/2)
  157. for i=1,mh do
  158. term.setCursorPos(xOffset,i)
  159. term.write("|")
  160. end
  161. multitext[1] = " ___ _ ___"
  162. multitext[2] = "|\\/| /\\ | |_) | \\/"
  163. multitext[3] = "| | /--\\ | | \\ _|_ /\\"
  164. drawMultiText(2+xOffset,1,multitext)
  165. clearTable(multitext)
  166.  
  167. --REACTOR--
  168.  
  169. --Status
  170. if (mouseDown and mouseX >= 2 and mouseX <= 6 and mouseY >= 6 and mouseY <= 9) then
  171. if (reactor.getActive()) then
  172. reactor.setActive(false)
  173. else
  174. reactor.setActive(true)
  175. end
  176. end
  177. xOffset = 0
  178. term.setCursorPos(8,6)
  179. term.setTextColor(colors.white)
  180. term.write("Status:")
  181. term.setCursorPos(8,7)
  182. if (reactor.getActive()) then
  183. term.setTextColor(colors.lime)
  184. term.write("Active")
  185. paintutils.drawBox(2,6,6,9,colors.green)
  186. paintutils.drawFilledBox(3,7,5,8,colors.lime)
  187. paintutils.drawLine(2,6,6,6,colors.white)
  188. else
  189. term.setTextColor(colors.red)
  190. term.write("Not Active")
  191. paintutils.drawBox(2,6,6,9,colors.gray)
  192. paintutils.drawFilledBox(3,7,5,8,colors.red)
  193. paintutils.drawLine(2,6,6,6,colors.white)
  194. end
  195.  
  196. --Energy Output
  197. xOffset = 0
  198. term.setBackgroundColor(colors.black)
  199. term.setCursorPos(xOffset+2,12)
  200. term.setTextColor(colors.white)
  201. term.write("Energy Output:")
  202. term.setCursorPos(xOffset+17,12)
  203. term.setTextColor(colors.lime)
  204. term.write(math.floor(reactor.getEnergyProducedLastTick()).." RF/t")
  205.  
  206. --Fuel
  207. term.setCursorPos(xOffset+2,14)
  208. term.setTextColor(colors.lightGray)
  209. term.write("Fuel")
  210. --Usage
  211. term.setCursorPos(xOffset+2,15)
  212. term.setTextColor(colors.white)
  213. term.write("Consumption:")
  214. term.setCursorPos(xOffset+15,15)
  215. term.setTextColor(colors.red)
  216. term.write((math.floor(reactor.getFuelConsumedLastTick()*100)/100).." mB/t")
  217. --Reactivity
  218. term.setCursorPos(xOffset+2,16)
  219. term.setTextColor(colors.white)
  220. term.write("Reactivity:")
  221. term.setCursorPos(xOffset+15,16)
  222. term.setTextColor(colors.yellow)
  223. term.write(math.floor(reactor.getFuelReactivity()).."%")
  224.  
  225. --Heat
  226. xOffset = math.floor(mw/2)
  227. term.setCursorPos(xOffset-20,14)
  228. term.setTextColor(colors.lightGray)
  229. term.write("Heat")
  230. --Casing
  231. term.setCursorPos(xOffset-20,15)
  232. term.setTextColor(colors.white)
  233. term.write("Casing:")
  234. term.setCursorPos(xOffset-20+8,15)
  235. term.setTextColor(colors.orange)
  236. term.write((math.ceil(reactor.getCasingTemperature()*100)/100).." 'C")
  237. --Core
  238. term.setCursorPos(xOffset-20,16)
  239. term.setTextColor(colors.white)
  240. term.write("Core:")
  241. term.setCursorPos(xOffset-20+8,16)
  242. term.setTextColor(colors.orange)
  243. term.write((math.ceil(reactor.getFuelTemperature()*100)/100).." 'C")
  244.  
  245. --Control Rod Insertion
  246. xOffset = 0
  247. term.setCursorPos(xOffset+2,19)
  248. term.setTextColor(colors.white)
  249. term.write("Control Rod Insertion ("..reactor.getNumberOfControlRods().." in total):")
  250. term.setCursorPos(xOffset+2,20)
  251. term.setTextColor(colors.lightGray)
  252. term.write(tostring(reactor.getControlRodLevel(0)).."%")
  253.  
  254. --Fuel Reserves
  255. xOffset = 0
  256. if (counter % 10 == 0) then
  257. --only update reserves every 10 frames; less lag
  258. rReservesAmount = countItemInInventory(echest, "BRIngot")
  259. end
  260. term.setCursorPos(xOffset+2, mh-6)
  261. term.setBackgroundColor(colors.black)
  262. term.setTextColor(colors.white)
  263. term.write("Fuel Reserves:")
  264. term.setCursorPos(xOffset+15+2, mh-6)
  265. term.setTextColor(colors.yellow)
  266. term.write(rReservesAmount.." / "..rReservesCapacity.." Yellorium")
  267. fillBar = {
  268. x1 = xOffset + 2,
  269. y1 = yOffset + mh-5,
  270. x2 = xOffset + math.floor(mw/2) - 2,
  271. y2 = yOffset + mh-1,
  272. value = rReservesAmount,
  273. valueMin = 0,
  274. valueMax = rReservesCapacity,
  275. dir = 0,
  276. bordered = true,
  277. colBG = colors.black,
  278. colValue = colors.yellow,
  279. colBorder = colors.gray,
  280. }
  281. drawFillBar()
  282.  
  283. --
  284.  
  285. --MATRIX--
  286. xOffset = math.floor(mw/2)
  287. --Input
  288. local matrixInput = matrix.getInput()
  289. local matrixCap = matrix.getTransferCap()
  290. term.setCursorPos(xOffset+2, 6)
  291. term.setBackgroundColor(colors.black)
  292. term.setTextColor(colors.white)
  293. term.write("Input:")
  294. term.setCursorPos(xOffset+9, 6)
  295. term.setBackgroundColor(colors.black)
  296. term.setTextColor(colors.blue)
  297. term.write(tostring(joulesToRF(matrixInput)) .." RF/t")
  298. fillBar = {
  299. x1 = xOffset + 2,
  300. y1 = yOffset + 7,
  301. x2 = xOffset + xOffset - 1,
  302. y2 = yOffset + 13,
  303. value = matrixInput,
  304. valueMin = 0,
  305. valueMax = matrixCap,
  306. dir = 0,
  307. bordered = true,
  308. colBG = colors.black,
  309. colValue = colors.blue,
  310. colBorder = colors.gray,
  311. }
  312. drawFillBar()
  313.  
  314. --Energy Stored
  315. term.setCursorPos(xOffset+2, 16)
  316. term.setBackgroundColor(colors.black)
  317. term.setTextColor(colors.white)
  318. term.write("Energy:")
  319. term.setCursorPos(xOffset+10, 16)
  320. term.setBackgroundColor(colors.black)
  321. term.setTextColor(colors.lime)
  322. term.write(joulesToRF(matrix.getEnergy()) .." / ".. joulesToRF(matrix.getMaxEnergy()) .." RF")
  323. fillBar = {
  324. x1 = xOffset + 2,
  325. y1 = yOffset + 17,
  326. x2 = xOffset + xOffset - 1,
  327. y2 = yOffset + 27,
  328. value = matrix.getEnergy(),
  329. valueMin = 0,
  330. valueMax = matrix.getMaxEnergy(),
  331. dir = 0,
  332. bordered = true,
  333. colBG = colors.black,
  334. colValue = colors.lime,
  335. colBorder = colors.gray,
  336. }
  337. drawFillBar()
  338. term.setCursorPos(xOffset+math.floor(xOffset/2)-2, 27)
  339. term.setBackgroundColor(colors.gray)
  340. term.setTextColor(colors.lightGray)
  341. term.write(string.sub(math.floor((matrix.getEnergy()/matrix.getMaxEnergy())*100), 1, 4).."%")
  342.  
  343. --Output
  344. local matrixOutput = matrix.getOutput()
  345. term.setCursorPos(xOffset+2, 30)
  346. term.setBackgroundColor(colors.black)
  347. term.setTextColor(colors.white)
  348. term.write("Output:")
  349. term.setCursorPos(xOffset+10, 30)
  350. term.setBackgroundColor(colors.black)
  351. term.setTextColor(colors.red)
  352. term.write(tostring(joulesToRF(matrixOutput)) .." RF/t")
  353. fillBar = {
  354. x1 = xOffset + 2,
  355. y1 = yOffset + 31,
  356. x2 = xOffset + xOffset - 1,
  357. y2 = yOffset + 37,
  358. value = matrixOutput,
  359. valueMin = 0,
  360. valueMax = matrixCap,
  361. dir = 0,
  362. bordered = true,
  363. colBG = colors.black,
  364. colValue = colors.red,
  365. colBorder = colors.gray,
  366. }
  367. drawFillBar()
  368.  
  369.  
  370. --REFRESH--
  371. term.setTextColor(colors.white)
  372. counter = counter + 1
  373. handleInput()
  374.  
  375. end
  376.  
  377. --local energyStr = tostring(fillBar.value)
  378. --local percent = fillbar.value / fillBar.valueMax
  379. --local percentStr = tostring(percent*100)
  380. --percentStr = string.sub(percentStr,1,5).."%"
  381. --local colPercent = colors.white
  382.  
  383. --Energy Text
  384. --term.setBackgroundColor(colors.black)
  385. --term.setTextColor(colEnergy)
  386. --term.setCursorPos(math.ceil(x1+(width/2)-((#energyStr)/2)),mh)
  387. --term.write(energyStr)
  388.  
  389. --Percent Text
  390. --term.setTextColor(colPercent)
  391. --term.setCursorPos(math.ceil(x1+(width/2)-((#percentStr)/2)),mh-1)
  392. --term.write(percentStr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement