Flawedspirit icon

Reactor Client Test

Flawedspirit | PRO | 06/12/14 12:05:04 AM UTC | 0 ⭐ | 229 👁️ | Never ⏰ | []
Lua |

5.93 KB

|

None

|

0 👍

/

0 👎

--[[
Button API: JKjUZpfN
Bar API: HXXY5T0Q
]]--
 
os.loadAPI("button")
os.loadAPI("bar")
 
local monWidth, monheight
 
--Color shortforms
local lblue   = colors.lightBlue
local cyan    = colors.cyan
local lime    = colors.lime
local yellow  = colors.yellow
local orange  = colors.orange
local red     = colors.red
local white   = colors.white
local black   = colors.black
local gray    = colors.gray
 
local bgColor = black
local textColor = white
 
--Default settings
local reactorState = "off"
local fuelRodState = 0
local currentPage = 1
 
local fuel
local burnRate
local waste
local capacity
local temp
local reactivity
local steamLevel
local steamOut
local coolantLevel
local status
local controlRodLevel
 
local serverChannel
 
args = {...}
 
function detect()
  for per, side in pairs(rs.getSides()) do
    if peripheral.isPresent(side) then
      if peripheral.getType(side) == "monitor" then
        mon = peripheral.wrap(side)
      elseif peripheral.getType(side) == "modem" then
        modem = side
      end
    end
  end
  return mon, modem
end
 
--[Misc functions]-------------------------------
function round(num, places)
  local multi = 10 ^ (places or 0)
  return math.floor(num * multi + 0.5) / multi
end
 
function toPercent(val, maxval)
  return (val / maxval) * 100
end
 
function readSettingsFile()
 
end
 
function writeSettingsFile()
 
end
 
function updateInfo()
  mon.clear()
  local sender, message, dist = rednet.receive("FSRC-S>C")
  serverChannel = sender
  local data = textutils.unserialize(message)
 
  reactorState = data._reactorState
  fuel = data._fuel
  burnRate = data._burnRate
  waste = data._waste
  capacity = data._capacity
  temp = data._temp
  reactivity = data._reactivity
  steamLevel = data._steamLevel
  coolantLevel = data._coolantLevel
  steamOut = data._steamOut
  controlRodLevel = data._controlRodLevel
 
  fuelMsg = tostring(fuel) .. " mB (" .. round(toPercent(fuel, capacity), 1) .. "%)"
  wasteMsg = tostring(waste) .. " mB (" .. round(toPercent(waste, capacity), 1) .. "%)"
end
 
function sendData(command)
  rednet.send(serverChannel, command, "FSRC-C>S")
end
--[End]------------------------------------------
 
--[Non-API graphical functions]------------------
function initDisplay(_textColor, _bgColor)
  mon.setTextScale(1)
  bgColor = _bgColor
  textColor = _textColor
  mon.setTextColor(textColor)
  mon.setBackgroundColor(bgColor)
end
 
function header()
  label("Reactor Status:", 1, 1, white)
  drawActiveState(reactorState)
  hLine(2)
end
 
function hLine(row)
  for i = 1, monWidth do
    mon.setCursorPos(i, row)
    mon.write("-")
  end
end
 
function label(text, x, y, color)
  mon.setCursorPos(x, y)
  mon.setTextColor(color)
  mon.write(text)
  mon.setTextColor(textColor)
end
--[End]------------------------------------------
 
function pageOneButtons()
  button.new("NEXT >", changePage, false, monWidth - 9, monWidth - 1, monHeight, monHeight)
  button.new("Power", toggleReactor, false, 2, 8, monHeight, monHeight)
end
 
function pageTwoButtons()
  button.new("< BACK", changePage, false, 2, 9, monHeight, monHeight)
  --All rods
  button.new("-", raiseRods, false, monWidth - 12, monWidth - 10, 3, 3)
  button.new("+", lowerRods, false, monWidth - 3, monWidth - 1, 3, 3)
end
 
--[Button functions]-----------------------------
function toggleReactor()
  button.flash("Power")
  if reactorState == "off" then
    reactorState = "on"
    sendData("turnon")
  else
    reactorState = "off"
    sendData("shutdown")
  end
end
 
function raiseRods()
  if fuelRodState > 0 then
    fuelRodState = fuelRodState - 10
  else
    fuelRodState = 0
  end
end
 
function lowerRods()
  if fuelRodState < 100 then
    fuelRodState = fuelRodState + 10
  else
    fuelRodState = 100
  end
end
--[End]------------------------------------------
 
function getClick()
  event, side, x, y = os.pullEvent()
  
  if event == "monitor_touch" then
    button.getButtonClickEvent(x, y)
  end
end
 
function drawActiveState(state)
  if state == "on" then
    mon.setCursorPos(monWidth - 1, 1)
    mon.setTextColor(lime)
    mon.write("ON")
  elseif state == "off" then
    mon.setCursorPos(monWidth - 2, 1)
    mon.setTextColor(red)
    mon.write("OFF")
  elseif state == "auto" then
    mon.setCursorPos(monWidth - 3, 1)
    mon.setTextColor(orange)
    mon.write("AUTO")
  end
  mon.setTextColor(textColor)
end
 
function page1()
  button.update()
  header()
  
  label("Fuel  : ", 1, 3, lblue)
  mon.write(fuelMsg)
  label("Waste : ", 1, 4, lblue)
  mon.write(wasteMsg)
  --Fuel bar is always at 100% because waste bar will be
  --printed on top to notify user of fuel/waste ratio
  bar.singleColor(100, monWidth / 2 - 10, 5, yellow)
  bar.singleColor(round(toPercent(waste, capacity), 0), monWidth / 2 - 10, 5, cyan)
  hLine(6)
  
  label(round(temp, 0) .. " C", 1, 7, red)
  label(":", 9, 7, lblue)
  label(round(burnRate, 2).. " mB/t", 11, 7, white)
  label("Steam   : ", 1, 8, lblue)
  mon.write(steamLevel .. " mB")
  label("Coolant : ", 1, 9, lblue)
  mon.write(coolantLevel .. " mB")
  label("Output  : ", 1, 10, lblue)
  mon.write(steamOut .. " mB/t")
  
  hLine(monHeight - 1)
end
 
function page2()
  button.update()
  header()
 
  label("Control Rods :", 1, 3, lblue)
  label(fuelRodState .. "%", (monWidth - 6) - string.len(fuelRodState) / 2, 3, white)
end
 
function changePage()
  mon.clear()
  if currentPage == 1 then
    currentPage = 2
    button.clear()
    pageTwoButtons()
  elseif currentPage == 2 then
    currentPage = 1
    button.clear()
    pageOneButtons()
  end
end
 
mon, modem = detect()
initDisplay(white, black)
monWidth, monHeight = mon.getSize()
rednet.open(modem)
button.setBGColor(colors.black)
pageOneButtons()
 
while true do
  updateInfo()
  if currentPage == 1 then
    page1()
  elseif currentPage == 2 then
    page2()
  end
  
  getClick()
end

Comments