turbine.lua

                Never    
Lua
       
-- Components and APIs

API = require("buttonAPI")

local component = require("component")
local event = require("event")
local term = require("term")
local shell = require("shell")
local colors = require("colors")

local gpu = component.gpu
local turbine = component.br_turbine

-- Variables

local running = true

-- Colors
local colorGreen = 0x00AA00
local colorOrange = 0xFF6B00
local colorYellow = 0xFFFF00
local colorRed = 0xAA0000
local colorWhite = 0xFFFFFF
local colorBlack = 0x000000
local colorGray = 0x0F0F0F

-- Reactor Stats

local turbineSteamCapacity = 4000
local turbineMaxEnergyStored = 1000000

local turbineActive = false
local turbneEnergyStored = 0
local turbineEnergyStoredPercent = 0
local turbineFluidAmountMax = 0
local turbineFluidFlowRate = 0
local turbineMaxFluidFlowRate = 0
local turbineFluidAmountPercent = 0 
local turbineRotorSpeed = 0
local turbineEnergyProducedLastTick = 0
local turbineInductorEngaged = false

local function refresh()
  turbineActive = turbine.getActive()
  turbineEnergyStored = turbine.getEnergyStored()
  turbineEnergyStoredPercent = math.ceil(100*(turbineEnergyStored/turbineMaxEnergyStored))
  turbineFluidAmountMax = turbine.getFluidAmountMax()
  turbineFluidFlowRate = turbine.getFluidFlowRate()
  turbineMaxFluidFlowRate = turbine.getFluidFlowRateMaxMax()
  turbineFluidAmountPercent = math.ceil(100*((turbineFluidAmountMax-turbineFluidFlowRate)/turbineFluidAmountMax))
  turbineRotorSpeed = math.ceil(turbine.getRotorSpeed())
  turbineEnergyProducedLastTick = math.ceil(turbine.getEnergyProducedLastTick())
  turbineInductorEngaged = turbine.getInductorEngaged()
end

-- Functions

local function center(row, msg)
  local mLen = string.len(msg)
  term.setCursor(1, row)
  term.clearLine()
  w, h = gpu.getResolution()
  gpu.set((w - mLen)/2,row,msg)
end

-- Buttons

function API.fillTable()
  API.setTable("Turbine ON", powerOn, 30, 60, 3, 7)
  API.setTable("Turbine OFF", powerOff, 72, 102, 3, 7)

  API.setTable("Flow Rate +100", augmentFlowRate100, 10, 30, 21, 23) 
  API.setTable("Flow Rate -100", lowerFlowRate100, 40, 60, 21, 23)
  API.setTable("Flow Rate +10", augmentFlowRate10, 68, 88,21, 23)
  API.setTable("Flow Rate -10", lowerFlowRate10, 98, 118, 21, 23)

  API.setTable("Coil: Engage", coilOn, 30, 60, 12, 14)
  API.setTable("Coil: Disengage", coilOff, 72, 102, 12, 14)
  
  local bData = {}
  bData["xmin"] = 30
  bData["xmax"] = 60
  bData["ymin"] = 3
  bData["ymax"] = 7

  local bData2 = {}
  bData2["xmin"] = 30
  bData2["xmax"] = 60
  bData2["ymin"] = 12
  bData2["ymax"] = 14

  API.screen()

  API.fill("Turbine ON", colorGreen, bData)  
  API.fill("Coil: Engage", colorGreen, bData2)  

end

function getClick()
  local _, _, x, y = event.pull(1,touch)
  if x == nil or y == nil then
    local h, w = gpu.getResolution()
    gpu.set(h, w, ".")
    gpu.set(h, w, " ")
  else
    API.checkxy(x,y)
  end
end

function powerOn()
  turbine.setActive(true)
end

function powerOff()
  turbine.setActive(false)
end

function coilOn()
  turbine.setInductorEngaged(true)
end

function coilOff()
  turbine.setInductorEngaged(false)
end

function augmentFlowRate100()
  if(turbineFluidFlowRate+100 <= turbineMaxFluidFlowRate) then
    turbine.setFluidFlowRateMax(turbineFluidFlowRate+100)
  else
    turbine.setFluidFlowRateMax(turbineMaxFluidFlowRate)
  end
end

function augmentFlowRate10()
  if(turbineFluidFlowRate+10 <= turbineMaxFluidFlowRate) then
    turbine.setFluidFlowRateMax(turbineFluidFlowRate+10)
  else
    turbine.setFluidFlowRateMax(turbineMaxFluidFlowRate)
  end
end

function lowerFlowRate100()
  if(turbineFluidFlowRate-100 >= 0) then
    turbine.setFluidFlowRateMax(turbineFluidFlowRate-100)
  else
    turbine.setFluidFlowRateMax(0)
  end
end

function lowerFlowRate10()
  if(turbineFluidFlowRate-10 >= 0) then
    turbine.setFluidFlowRateMax(turbineFluidFlowRate-10)
  else
    turbine.setFluidFlowRateMax(0)
  end
end


-- Code

term.setCursorBlink(false)
gpu.setResolution(132, 38)

API.clear()
API.fillTable()

while running do
  -- Start

  getClick()
  refresh()
  gpu.setForeground(colorWhite)
  gpu.setBackground(colorBlack)
 
  -- Info
 
  if(turbineActive) then
    gpu.setForeground(colorGreen)
    center(10, "Status: Online")
  else
    gpu.setForeground(colorRed)
    center(10, "Status: Offline")
  end

  if(turbineInductorEngaged) then
    gpu.setForeground(colorGreen)
    center(17, "Coil: Engaged")
  else
    gpu.setForeground(colorRed)
    center(17, "Coil: Disengaged")
  end
  
  gpu.setForeground(colorWhite)

  center(19, "Generation: " .. turbineEnergyProducedLastTick .. "RF/t")
 
  center(25, "Flow rate: " .. turbineFluidFlowRate .. "mb/t")

  if(turbineRotorSpeed < 900) then
    gpu.setForeground(colorWhite)
  elseif(turbineRotorSpeed >= 900 and turbineRotorSpeed <= 1800) then
    gpu.setForeground(colorGreen)
  else
    gpu.setForeground(colorRed)
  end

  center(26, "Rotor speed: " .. turbineRotorSpeed .. "RPM")

  gpu.setForeground(colorWhite)

  -- Graph Steam

  center(30, "Steam: " .. turbineFluidAmountMax-turbineFluidFlowRate .. "mb")
 
  gpu.setBackground(colorGray)
  gpu.fill(19, 32, 100, 5, " ")
 
  if(turbineFluidAmountPercent >= 50) then
    gpu.setBackground(colorGreen)
  elseif(turbineFluidAmountPercent >= 30) then
    gpu.setBackground(colorOrange)
  else
    gpu.setBackground(colorRed)
  end
 
  gpu.fill(19, 32, turbineFluidAmountPercent, 5, " ")
 
  gpu.setBackground(colorBlack)
 
  os.sleep(1)
 
end

Raw Text