Advertisement
Flawedspirit

Reactor Client Test

Jun 11th, 2014
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.93 KB | None | 0 0
  1. --[[
  2. Button API: JKjUZpfN
  3. Bar API: HXXY5T0Q
  4. ]]--
  5.  
  6. os.loadAPI("button")
  7. os.loadAPI("bar")
  8.  
  9. local monWidth, monheight
  10.  
  11. --Color shortforms
  12. local lblue   = colors.lightBlue
  13. local cyan    = colors.cyan
  14. local lime    = colors.lime
  15. local yellow  = colors.yellow
  16. local orange  = colors.orange
  17. local red     = colors.red
  18. local white   = colors.white
  19. local black   = colors.black
  20. local gray    = colors.gray
  21.  
  22. local bgColor = black
  23. local textColor = white
  24.  
  25. --Default settings
  26. local reactorState = "off"
  27. local fuelRodState = 0
  28. local currentPage = 1
  29.  
  30. local fuel
  31. local burnRate
  32. local waste
  33. local capacity
  34. local temp
  35. local reactivity
  36. local steamLevel
  37. local steamOut
  38. local coolantLevel
  39. local status
  40. local controlRodLevel
  41.  
  42. local serverChannel
  43.  
  44. args = {...}
  45.  
  46. function detect()
  47.   for per, side in pairs(rs.getSides()) do
  48.     if peripheral.isPresent(side) then
  49.       if peripheral.getType(side) == "monitor" then
  50.         mon = peripheral.wrap(side)
  51.       elseif peripheral.getType(side) == "modem" then
  52.         modem = side
  53.       end
  54.     end
  55.   end
  56.   return mon, modem
  57. end
  58.  
  59. --[Misc functions]-------------------------------
  60. function round(num, places)
  61.   local multi = 10 ^ (places or 0)
  62.   return math.floor(num * multi + 0.5) / multi
  63. end
  64.  
  65. function toPercent(val, maxval)
  66.   return (val / maxval) * 100
  67. end
  68.  
  69. function readSettingsFile()
  70.  
  71. end
  72.  
  73. function writeSettingsFile()
  74.  
  75. end
  76.  
  77. function updateInfo()
  78.   mon.clear()
  79.   local sender, message, dist = rednet.receive("FSRC-S>C")
  80.   serverChannel = sender
  81.   local data = textutils.unserialize(message)
  82.  
  83.   reactorState = data._reactorState
  84.   fuel = data._fuel
  85.   burnRate = data._burnRate
  86.   waste = data._waste
  87.   capacity = data._capacity
  88.   temp = data._temp
  89.   reactivity = data._reactivity
  90.   steamLevel = data._steamLevel
  91.   coolantLevel = data._coolantLevel
  92.   steamOut = data._steamOut
  93.   controlRodLevel = data._controlRodLevel
  94.  
  95.   fuelMsg = tostring(fuel) .. " mB (" .. round(toPercent(fuel, capacity), 1) .. "%)"
  96.   wasteMsg = tostring(waste) .. " mB (" .. round(toPercent(waste, capacity), 1) .. "%)"
  97. end
  98.  
  99. function sendData(command)
  100.   rednet.send(serverChannel, command, "FSRC-C>S")
  101. end
  102. --[End]------------------------------------------
  103.  
  104. --[Non-API graphical functions]------------------
  105. function initDisplay(_textColor, _bgColor)
  106.   mon.setTextScale(1)
  107.   bgColor = _bgColor
  108.   textColor = _textColor
  109.   mon.setTextColor(textColor)
  110.   mon.setBackgroundColor(bgColor)
  111. end
  112.  
  113. function header()
  114.   label("Reactor Status:", 1, 1, white)
  115.   drawActiveState(reactorState)
  116.   hLine(2)
  117. end
  118.  
  119. function hLine(row)
  120.   for i = 1, monWidth do
  121.     mon.setCursorPos(i, row)
  122.     mon.write("-")
  123.   end
  124. end
  125.  
  126. function label(text, x, y, color)
  127.   mon.setCursorPos(x, y)
  128.   mon.setTextColor(color)
  129.   mon.write(text)
  130.   mon.setTextColor(textColor)
  131. end
  132. --[End]------------------------------------------
  133.  
  134. function pageOneButtons()
  135.   button.new("NEXT >", changePage, false, monWidth - 9, monWidth - 1, monHeight, monHeight)
  136.   button.new("Power", toggleReactor, false, 2, 8, monHeight, monHeight)
  137. end
  138.  
  139. function pageTwoButtons()
  140.   button.new("< BACK", changePage, false, 2, 9, monHeight, monHeight)
  141.   --All rods
  142.   button.new("-", raiseRods, false, monWidth - 12, monWidth - 10, 3, 3)
  143.   button.new("+", lowerRods, false, monWidth - 3, monWidth - 1, 3, 3)
  144. end
  145.  
  146. --[Button functions]-----------------------------
  147. function toggleReactor()
  148.   button.flash("Power")
  149.   if reactorState == "off" then
  150.     reactorState = "on"
  151.     sendData("turnon")
  152.   else
  153.     reactorState = "off"
  154.     sendData("shutdown")
  155.   end
  156. end
  157.  
  158. function raiseRods()
  159.   if fuelRodState > 0 then
  160.     fuelRodState = fuelRodState - 10
  161.   else
  162.     fuelRodState = 0
  163.   end
  164. end
  165.  
  166. function lowerRods()
  167.   if fuelRodState < 100 then
  168.     fuelRodState = fuelRodState + 10
  169.   else
  170.     fuelRodState = 100
  171.   end
  172. end
  173. --[End]------------------------------------------
  174.  
  175. function getClick()
  176.   event, side, x, y = os.pullEvent()
  177.  
  178.   if event == "monitor_touch" then
  179.     button.getButtonClickEvent(x, y)
  180.   end
  181. end
  182.  
  183. function drawActiveState(state)
  184.   if state == "on" then
  185.     mon.setCursorPos(monWidth - 1, 1)
  186.     mon.setTextColor(lime)
  187.     mon.write("ON")
  188.   elseif state == "off" then
  189.     mon.setCursorPos(monWidth - 2, 1)
  190.     mon.setTextColor(red)
  191.     mon.write("OFF")
  192.   elseif state == "auto" then
  193.     mon.setCursorPos(monWidth - 3, 1)
  194.     mon.setTextColor(orange)
  195.     mon.write("AUTO")
  196.   end
  197.   mon.setTextColor(textColor)
  198. end
  199.  
  200. function page1()
  201.   button.update()
  202.   header()
  203.  
  204.   label("Fuel  : ", 1, 3, lblue)
  205.   mon.write(fuelMsg)
  206.   label("Waste : ", 1, 4, lblue)
  207.   mon.write(wasteMsg)
  208.   --Fuel bar is always at 100% because waste bar will be
  209.   --printed on top to notify user of fuel/waste ratio
  210.   bar.singleColor(100, monWidth / 2 - 10, 5, yellow)
  211.   bar.singleColor(round(toPercent(waste, capacity), 0), monWidth / 2 - 10, 5, cyan)
  212.   hLine(6)
  213.  
  214.   label(round(temp, 0) .. " C", 1, 7, red)
  215.   label(":", 9, 7, lblue)
  216.   label(round(burnRate, 2).. " mB/t", 11, 7, white)
  217.   label("Steam   : ", 1, 8, lblue)
  218.   mon.write(steamLevel .. " mB")
  219.   label("Coolant : ", 1, 9, lblue)
  220.   mon.write(coolantLevel .. " mB")
  221.   label("Output  : ", 1, 10, lblue)
  222.   mon.write(steamOut .. " mB/t")
  223.  
  224.   hLine(monHeight - 1)
  225. end
  226.  
  227. function page2()
  228.   button.update()
  229.   header()
  230.  
  231.   label("Control Rods :", 1, 3, lblue)
  232.   label(fuelRodState .. "%", (monWidth - 6) - string.len(fuelRodState) / 2, 3, white)
  233. end
  234.  
  235. function changePage()
  236.   mon.clear()
  237.   if currentPage == 1 then
  238.     currentPage = 2
  239.     button.clear()
  240.     pageTwoButtons()
  241.   elseif currentPage == 2 then
  242.     currentPage = 1
  243.     button.clear()
  244.     pageOneButtons()
  245.   end
  246. end
  247.  
  248. mon, modem = detect()
  249. initDisplay(white, black)
  250. monWidth, monHeight = mon.getSize()
  251. rednet.open(modem)
  252. button.setBGColor(colors.black)
  253. pageOneButtons()
  254.  
  255. while true do
  256.   updateInfo()
  257.   if currentPage == 1 then
  258.     page1()
  259.   elseif currentPage == 2 then
  260.     page2()
  261.   end
  262.  
  263.   getClick()
  264. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement