Advertisement
TomMo54

OpenComputers Extreme Reactor Control

Apr 28th, 2020
2,732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.45 KB | None | 0 0
  1. -- Def local variables
  2. local component = require("component")
  3. local event = require("event")
  4. local gpu = component.gpu
  5. local br = component.br_reactor
  6.  
  7. local width = 50
  8. local height = 25
  9.  
  10. local max_fuel = br.getFuelAmountMax()
  11. local max_energy = br.getEnergyCapacity()
  12.  
  13. local autoControl = true
  14. local isActivated = nil
  15. local fuelPct = nil
  16. local energyPct = nil
  17. local prod = nil
  18. local cons = nil
  19. local coreTemp = nil
  20. local casingTemp = nil
  21.  
  22. -- Setup screen
  23. gpu.setResolution(width, height)
  24. gpu.setBackground(0x000000)
  25. gpu.fill(1, 1, width, height, " ")
  26.  
  27. -- Create Button object
  28. local Button = {}
  29.  
  30. function Button.draw(self, color, text, textPos)
  31.     bWidth = self.x2 - self.x1 + 1
  32.     bHeight = self.y2 - self.y1 + 1
  33.  
  34.     gpu.setBackground(color)
  35.     gpu.fill(self.x1, self.y1, bWidth, bHeight, " ")
  36.     gpu.set(self.x1 + textPos, self.y1 + math.ceil(bHeight / 2) - 1, text)
  37.  
  38.     gpu.setBackground(0x000000)
  39. end
  40.  
  41. function Button.isClicked(self, x, y)
  42.     return x >= self.x1 and x <= self.x2 and y >= self.y1 and y <= self.y2
  43. end
  44.  
  45. -- Create ProgressBar object
  46. local ProgressBar = {}
  47.  
  48. function ProgressBar.draw(self)
  49.     barValue = math.floor((self.size * self.value) / 100)
  50.  
  51.     -- Draw empty bar
  52.     gpu.setBackground(0xBBBBBB)
  53.     gpu.fill(self.x, self.y, 5, self.size + 2, " ")
  54.     gpu.setBackground(0x000000)
  55.     gpu.fill(self.x + 1, self.y + 1, 3, self.size, " ")
  56.  
  57.     -- Draw bar
  58.     gpu.setBackground(self.color)
  59.     gpu.fill(self.x + 1, self.y + (self.size - barValue) + 1, 3, barValue, " ")
  60.  
  61.     gpu.setBackground(0x000000)
  62. end
  63.  
  64. -- Get amount of fuel stored in reactor in percent
  65. local function getFuelPct()
  66.     fuel = br.getFuelAmount()
  67.     pct = math.floor((fuel * 100) / max_fuel)
  68.     return pct
  69. end
  70.  
  71. -- Get amount of energy stored in reactor in percent
  72. local function getEnergyPct()
  73.     energy = br.getEnergyStored()
  74.     pct = math.floor((energy * 100) / max_energy)
  75.     return pct
  76. end
  77.  
  78. -- Get amount of produced energy last tick
  79. local function getFormatedProd()
  80.     production = br.getEnergyProducedLastTick()
  81.  
  82.     if production >= 1000 then
  83.         production = string.format("%." .. 2 .. "f", production / 1000) .. " kRF/t "
  84.     else
  85.         production = tostring(math.floor(production)) .. " RF/t  "
  86.     end
  87.  
  88.     return production
  89. end
  90.  
  91. local function getFormatedConsumption()
  92.     consumption = string.format("%." .. 3 .. "f", br.getFuelConsumedLastTick()) .. " mB/t "
  93.  
  94.     return consumption
  95. end
  96.  
  97. local function getFormatedTemperature(temp)
  98.     fTemp = math.floor(temp) .. "°C  "
  99.  
  100.     return fTemp
  101. end
  102.  
  103. -- Turn the auto control on/off
  104. local function autoCtrlClicked()
  105.     autoControl = not autoControl
  106. end
  107.  
  108. -- Turn the reactor on/off
  109. local function activateClicked()
  110.     br.setActive(not isActivated)
  111.     isActivated = not isActivated
  112. end
  113.  
  114. -- Create elements
  115. local autoCtrlButton = {x1 = 14, x2 = 25, y1 = 21, y2 = 23, draw = Button.draw, isClicked = Button.isClicked, onClick = autoCtrlClicked}
  116. local activateButton = {x1 = 27, x2 = 38, y1 = 21, y2 = 23, draw = Button.draw, isClicked = Button.isClicked, onClick = activateClicked}
  117. local fuelProgressBar = {x = 2, y = 2, size = 20, value = 0, color = 0xFFFF00, draw = ProgressBar.draw}
  118. local energyStoredProgressBar = {x = 8, y = 2, size = 20, value = 0, color = 0xFF0000, draw = ProgressBar.draw}
  119.  
  120. -- Update reactor values
  121. local function updateValues()
  122.     isActivated = br.getActive()
  123.     fuelPct = getFuelPct()
  124.     energyPct = getEnergyPct()
  125.     prod = getFormatedProd()
  126.     cons = getFormatedConsumption()
  127.     coreTemp = getFormatedTemperature(br.getFuelTemperature())
  128.     casingTemp = getFormatedTemperature(br.getCasingTemperature())
  129.  
  130.     -- Auto control
  131.     if autoControl and energyPct >= 80 then
  132.         isActivated = false
  133.         br.setActive(false)
  134.     end
  135.     if autoControl and energyPct <= 20 then
  136.         isActivated = true
  137.         br.setActive(true)
  138.     end
  139.  
  140.     fuelProgressBar.value = fuelPct
  141.     energyStoredProgressBar.value = energyPct
  142. end
  143.  
  144. -- Update every screen's elements
  145. local function updateScreen()
  146.     -- Update auto control button
  147.     if autoControl then
  148.         autoCtrlButton.draw(autoCtrlButton, 0x00FF00, "Auto ctrl", 2)
  149.     else
  150.         autoCtrlButton.draw(autoCtrlButton, 0xFF0000, "Auto ctrl", 2)
  151.     end
  152.  
  153.     -- Update activate button
  154.     if isActivated then
  155.         activateButton.draw(activateButton, 0x00FF00, "ON", 5)
  156.     else
  157.         activateButton.draw(activateButton, 0xFF0000, "OFF", 5)
  158.     end
  159.  
  160.     -- Update energy stored progress bar
  161.     fuelProgressBar.draw(fuelProgressBar)
  162.     energyStoredProgressBar.draw(energyStoredProgressBar)
  163.  
  164.     -- Update displayed values
  165.     gpu.set(36, 4, prod)
  166.     gpu.set(36, 6, cons)
  167.     gpu.set(36, 8, coreTemp)
  168.     gpu.set(36, 10, casingTemp)
  169. end
  170.  
  171. local function screenClicked(_, _, x, y)
  172.     -- Check if auto control button is clicked
  173.     if autoCtrlButton.isClicked(autoCtrlButton, x, y) then
  174.         autoCtrlButton.onClick()
  175.     end
  176.  
  177.     -- Check if activate button is clicked
  178.     if activateButton.isClicked(activateButton, x, y) then
  179.         activateButton.onClick()
  180.     end
  181. end
  182.  
  183. -- Draw static text
  184. gpu.set(2, 1, "Fuel  Ener.")
  185. gpu.set(14, 4, "Production :")
  186. gpu.set(14, 6, "Consommation :")
  187. gpu.set(14, 8, "Temp. du coeur :")
  188. gpu.set(14, 10, "Temp. exterieure :")
  189.  
  190. -- Register events
  191. event.listen("touch", screenClicked)
  192.  
  193. -- Main loop
  194. while event.pull(0.5, "interrupted") == nil do
  195.     updateValues()
  196.     updateScreen()
  197. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement