Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Def local variables
- local component = require("component")
- local event = require("event")
- local gpu = component.gpu
- local br = component.br_reactor
- local width = 50
- local height = 25
- local max_fuel = br.getFuelAmountMax()
- local max_energy = br.getEnergyCapacity()
- local autoControl = true
- local isActivated = nil
- local fuelPct = nil
- local energyPct = nil
- local prod = nil
- local cons = nil
- local coreTemp = nil
- local casingTemp = nil
- -- Setup screen
- gpu.setResolution(width, height)
- gpu.setBackground(0x000000)
- gpu.fill(1, 1, width, height, " ")
- -- Create Button object
- local Button = {}
- function Button.draw(self, color, text, textPos)
- bWidth = self.x2 - self.x1 + 1
- bHeight = self.y2 - self.y1 + 1
- gpu.setBackground(color)
- gpu.fill(self.x1, self.y1, bWidth, bHeight, " ")
- gpu.set(self.x1 + textPos, self.y1 + math.ceil(bHeight / 2) - 1, text)
- gpu.setBackground(0x000000)
- end
- function Button.isClicked(self, x, y)
- return x >= self.x1 and x <= self.x2 and y >= self.y1 and y <= self.y2
- end
- -- Create ProgressBar object
- local ProgressBar = {}
- function ProgressBar.draw(self)
- barValue = math.floor((self.size * self.value) / 100)
- -- Draw empty bar
- gpu.setBackground(0xBBBBBB)
- gpu.fill(self.x, self.y, 5, self.size + 2, " ")
- gpu.setBackground(0x000000)
- gpu.fill(self.x + 1, self.y + 1, 3, self.size, " ")
- -- Draw bar
- gpu.setBackground(self.color)
- gpu.fill(self.x + 1, self.y + (self.size - barValue) + 1, 3, barValue, " ")
- gpu.setBackground(0x000000)
- end
- -- Get amount of fuel stored in reactor in percent
- local function getFuelPct()
- fuel = br.getFuelAmount()
- pct = math.floor((fuel * 100) / max_fuel)
- return pct
- end
- -- Get amount of energy stored in reactor in percent
- local function getEnergyPct()
- energy = br.getEnergyStored()
- pct = math.floor((energy * 100) / max_energy)
- return pct
- end
- -- Get amount of produced energy last tick
- local function getFormatedProd()
- production = br.getEnergyProducedLastTick()
- if production >= 1000 then
- production = string.format("%." .. 2 .. "f", production / 1000) .. " kRF/t "
- else
- production = tostring(math.floor(production)) .. " RF/t "
- end
- return production
- end
- local function getFormatedConsumption()
- consumption = string.format("%." .. 3 .. "f", br.getFuelConsumedLastTick()) .. " mB/t "
- return consumption
- end
- local function getFormatedTemperature(temp)
- fTemp = math.floor(temp) .. "°C "
- return fTemp
- end
- -- Turn the auto control on/off
- local function autoCtrlClicked()
- autoControl = not autoControl
- end
- -- Turn the reactor on/off
- local function activateClicked()
- br.setActive(not isActivated)
- isActivated = not isActivated
- end
- -- Create elements
- local autoCtrlButton = {x1 = 14, x2 = 25, y1 = 21, y2 = 23, draw = Button.draw, isClicked = Button.isClicked, onClick = autoCtrlClicked}
- local activateButton = {x1 = 27, x2 = 38, y1 = 21, y2 = 23, draw = Button.draw, isClicked = Button.isClicked, onClick = activateClicked}
- local fuelProgressBar = {x = 2, y = 2, size = 20, value = 0, color = 0xFFFF00, draw = ProgressBar.draw}
- local energyStoredProgressBar = {x = 8, y = 2, size = 20, value = 0, color = 0xFF0000, draw = ProgressBar.draw}
- -- Update reactor values
- local function updateValues()
- isActivated = br.getActive()
- fuelPct = getFuelPct()
- energyPct = getEnergyPct()
- prod = getFormatedProd()
- cons = getFormatedConsumption()
- coreTemp = getFormatedTemperature(br.getFuelTemperature())
- casingTemp = getFormatedTemperature(br.getCasingTemperature())
- -- Auto control
- if autoControl and energyPct >= 80 then
- isActivated = false
- br.setActive(false)
- end
- if autoControl and energyPct <= 20 then
- isActivated = true
- br.setActive(true)
- end
- fuelProgressBar.value = fuelPct
- energyStoredProgressBar.value = energyPct
- end
- -- Update every screen's elements
- local function updateScreen()
- -- Update auto control button
- if autoControl then
- autoCtrlButton.draw(autoCtrlButton, 0x00FF00, "Auto ctrl", 2)
- else
- autoCtrlButton.draw(autoCtrlButton, 0xFF0000, "Auto ctrl", 2)
- end
- -- Update activate button
- if isActivated then
- activateButton.draw(activateButton, 0x00FF00, "ON", 5)
- else
- activateButton.draw(activateButton, 0xFF0000, "OFF", 5)
- end
- -- Update energy stored progress bar
- fuelProgressBar.draw(fuelProgressBar)
- energyStoredProgressBar.draw(energyStoredProgressBar)
- -- Update displayed values
- gpu.set(36, 4, prod)
- gpu.set(36, 6, cons)
- gpu.set(36, 8, coreTemp)
- gpu.set(36, 10, casingTemp)
- end
- local function screenClicked(_, _, x, y)
- -- Check if auto control button is clicked
- if autoCtrlButton.isClicked(autoCtrlButton, x, y) then
- autoCtrlButton.onClick()
- end
- -- Check if activate button is clicked
- if activateButton.isClicked(activateButton, x, y) then
- activateButton.onClick()
- end
- end
- -- Draw static text
- gpu.set(2, 1, "Fuel Ener.")
- gpu.set(14, 4, "Production :")
- gpu.set(14, 6, "Consommation :")
- gpu.set(14, 8, "Temp. du coeur :")
- gpu.set(14, 10, "Temp. exterieure :")
- -- Register events
- event.listen("touch", screenClicked)
- -- Main loop
- while event.pull(0.5, "interrupted") == nil do
- updateValues()
- updateScreen()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement