Advertisement
2016mfransen

OpenReactors

Mar 15th, 2015
5,846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.90 KB | None | 0 0
  1. --#requires
  2.  
  3. local fs = require("filesystem")
  4. local term = require("term")
  5. local serialization = require("serialization")
  6. local component = require("component")
  7. local event = require("event")
  8. local component = require("component")
  9. local colors = require("colors")
  10.  
  11. --#variables
  12. local gpu = component.gpu
  13. local config = {}
  14. local reactor = nil
  15. local running = true
  16. local screen = "main"
  17. --#install
  18.  
  19. function install()
  20.   screen = "install"
  21.   term.clear()
  22.   print("Requirements:")
  23.   print("Tier 2 screen")
  24.   print("Tier 2 graphics card")
  25.   print("A BigReactor connected by cable from computer port to computer")
  26.   print("A keyboard for install only, optional for practical use")
  27.   print()
  28.   print("Are all requirements met? (y/n)")
  29.   --21,49
  30.   local result = false
  31.   while not result do
  32.     local name, adress, char, code, player = event.pull("key_down")
  33.     if code == 21 then
  34.       result = true
  35.     elseif code == 49 then
  36.       os.exit()
  37.     else
  38.       print("Invalid response")
  39.     end
  40.   end
  41.   --set resolution and continue
  42.   gpu.setResolution(80,25)
  43.   gpu.setForeground(0x000000)
  44.   term.clear()
  45.   gpu.setBackground(0x0000BB)
  46.   term.clear()
  47.   gpu.setBackground(0x808080)
  48.   gpu.fill(20,9,40,6," ")
  49.   term.setCursor(20,9)
  50.   print("Thank you for downloading")
  51.   term.setCursor(20,10)
  52.   print("OpenReactor")
  53.   term.setCursor(20,11)
  54.   print("press ok to continue")
  55.   term.setCursor(20,12)
  56.   print("press cancel to cancel the installation")
  57.   gpu.setBackground(0x008000)
  58.   gpu.fill(20,14,20,1," ")
  59.   term.setCursor(29,14)
  60.   print("ok")
  61.   gpu.setBackground(0x800000)
  62.   gpu.fill(40,14,20,1," ")
  63.   term.setCursor(48,14)
  64.   print("cancel")
  65.   local event_running = true
  66.   while event_running do
  67.     local name, address, x, y, button, player = event.pull("touch")
  68.     if x >= 20 and x <= 39 and y == 14 then
  69.       print("ok")
  70.       event_running = false
  71.     elseif x>=40 and x <= 59 and y == 14 then
  72.       os.exit()
  73.     end
  74.   end
  75.   install_pick_reactor()
  76.   set_color_scheme()
  77.   save_config()
  78.   main()
  79. end
  80.  
  81. --#main
  82.  
  83. function main()
  84.   screen = "main"
  85.   gpu.setResolution(80,25)
  86.   read_config()
  87.   reactor = component.proxy(config.reactor)
  88.   event.listen("touch",listen)
  89.   while running do
  90.     if config.auto_power.enabled == true then
  91.       if reactor.getEnergyStored()/10^5<config.auto_power.start_percent then
  92.         reactor.setActive(true)
  93.       elseif reactor.getEnergyStored()/10^5>config.auto_power.stop_percent then
  94.         reactor.setActive(false)
  95.       end
  96.     end
  97.     gpu.setBackground(config.color_scheme.background)
  98.     term.clear()
  99.     draw_menubar()
  100.     if screen == "main" then
  101.       draw_main()
  102.     elseif screen == "config" then
  103.       draw_config()
  104.     end
  105.     os.sleep(.05)
  106.   end
  107. end
  108.  
  109. --#draw_menubar
  110.  
  111. function draw_menubar()
  112.   term.setCursor(1,1)
  113.   gpu.setBackground(config.color_scheme.menubar.background)
  114.   gpu.setForeground(config.color_scheme.menubar.foreground)
  115.   term.clearLine()
  116.   term.setCursor(1,1)
  117.   term.write("Status: ")
  118.   if reactor.getActive() then
  119.     gpu.setForeground(config.color_scheme.success)
  120.     term.write("Online ")
  121.   else
  122.     gpu.setForeground(config.color_scheme.error)
  123.     term.write("Offline ")
  124.   end
  125.   if config.auto_power.enabled then
  126.     gpu.setForeground(config.color_scheme.menubar.foreground)
  127.     term.write("(")
  128.     gpu.setForeground(config.color_scheme.info)
  129.     term.write("Auto")
  130.     gpu.setForeground(config.color_scheme.menubar.foreground)
  131.     term.write(") ")
  132.   end
  133.   gpu.setForeground(config.color_scheme.menubar.foreground)
  134.   term.write(" Fuel Temperature: ")
  135.   gpu.setForeground(config.color_scheme.info)
  136.   term.write(round(reactor.getFuelTemperature()).."C ")
  137.   gpu.setForeground(config.color_scheme.menubar.foreground)
  138.   term.write(" Casing Temperature: ")
  139.   gpu.setForeground(config.color_scheme.info)
  140.   term.write(round(reactor.getCasingTemperature()).."C ")
  141.   term.setCursor(74,1)
  142.   gpu.setForeground(config.color_scheme.menubar.foreground)
  143.   term.write("[")
  144.   gpu.setForeground(config.color_scheme.error)
  145.   term.write("Exit")
  146.   gpu.setForeground(config.color_scheme.menubar.foreground)
  147.   term.write("]")
  148. end
  149.  
  150. --#save_config
  151.  
  152. function save_config()
  153.   local file = io.open("/etc/open-reactors.cfg","w")
  154.   file:write(serialization.serialize(config,false))
  155.   file:close()
  156. end
  157.  
  158. --#read_config
  159.  
  160. function read_config()
  161.   local file = io.open("/etc/open-reactors.cfg","r")
  162.   local c = serialization.unserialize(file:read(fs.size("/etc/open-reactors.cfg")))
  163.   file:close()
  164.   for k,v in pairs(c) do
  165.     config[k] = v
  166.   end
  167. end
  168.  
  169. --#set_color_scheme
  170.  
  171. function set_color_scheme()
  172.   config.color_scheme = {}
  173.   config.color_scheme.background = 0x0000BB
  174.   config.color_scheme.button = 0x606060
  175.   config.color_scheme.button_disabled = 0xC0C0C0
  176.   config.color_scheme.foreground = 0x000000
  177.   config.color_scheme.progressBar = {}
  178.   config.color_scheme.progressBar.background = 0x000000
  179.   config.color_scheme.progressBar.foreground = 0xFFFFFF
  180.   config.color_scheme.menubar={}
  181.   config.color_scheme.menubar.background = 0x000000
  182.   config.color_scheme.menubar.foreground = 0xFFFFFF
  183.   config.color_scheme.success = 0x008000
  184.   config.color_scheme.error = 0x800000
  185.   config.color_scheme.info = 0x808000
  186.   config.auto_power = {}
  187.   config.auto_power.enabled = false
  188.   config.auto_power.start_percent = 15
  189.   config.auto_power.stop_percent = 80
  190. end
  191.  
  192. --#install_pick_reactor
  193.  
  194. function install_pick_reactor()
  195.   gpu.setBackground(0x0000BB)
  196.   term.clear()
  197.   gpu.setBackground(0x808080)
  198.   local reactors = component.list("br_reactor")
  199.   local len = 3
  200.   for k,v in pairs(reactors) do
  201.     if len<#k then
  202.       len = #k
  203.     end
  204.   end
  205.   local s_x = 40-len/2
  206.   local s_y = 13-round(countTable(reactors)/2)
  207.   gpu.fill(s_x-1,s_y-2,len+2,countTable(reactors)+3," ")
  208.   term.setCursor(s_x+9,s_y-2)
  209.   print("select a reactor")
  210.   local i = s_y
  211.   for k,v in pairs(reactors) do
  212.     term.setCursor(s_x,i)
  213.     print(k)
  214.     i=i+1
  215.   end
  216.   local event_running = true
  217.   while event_running do
  218.     local name, address, x, y, button, player = event.pull("touch")
  219.     print(y-s_y)
  220.     if x>=s_x and x <= s_x+len and y>=s_y and y<= s_y+countTable(reactors) then
  221.       event_running = false
  222.       local i = y-s_y
  223.       for k,v in pairs(reactors) do
  224.         if i == 0 then
  225.           config.reactor = k
  226.         end
  227.         i=i-1
  228.       end
  229.     end
  230.   end
  231. end
  232.  
  233. --#draw_main
  234.  
  235. function draw_main()
  236.   if config.auto_power.enabled then
  237.     gpu.setBackground(config.color_scheme.button)
  238.     gpu.setForeground(0xFFFFFF)
  239.     gpu.fill(1,2,69,3," ")
  240.     term.setCursor(25,3)
  241.     term.write("Disable Auto-Power")
  242.     gpu.setBackground(0x153F3F)
  243.     gpu.fill(70,2,11,3," ")
  244.     term.setCursor(71,3)
  245.     term.write("Configure")
  246.   else
  247.     gpu.setBackground(config.color_scheme.button)
  248.     gpu.setForeground(0xFFFFFF)
  249.     gpu.fill(1,2,69,3," ")
  250.     term.setCursor(25,3)
  251.     term.write("Enable Auto-Power")
  252.     gpu.setBackground(0x153F3F)
  253.     gpu.fill(70,2,11,3," ")
  254.     term.setCursor(73,3)
  255.     term.write("Power")
  256.   end
  257.   gpu.setForeground(0xFFFFFF)
  258.   gpu.setBackground(config.color_scheme.button)
  259.   gpu.fill(1,8,13,3," ")
  260.   gpu.fill(1,14,13,3," ")
  261.   gpu.fill(1,20,13,3," ")
  262.   term.setCursor(1,9)
  263.   term.write("Energy Buffer")
  264.   term.setCursor(2,15)
  265.   term.write("Fuel Level")
  266.   term.setCursor(2,21)
  267.   term.write("Reactivity")
  268.   drawProgressBar(14,8,65,3,reactor.getEnergyStored()/10^7)
  269.   drawProgressBar(14,14,65,3,reactor.getFuelAmount()/reactor.getFuelAmountMax())
  270.   drawProgressBar(14,20,65,3,reactor.getFuelReactivity()/10^2)
  271.   if config.auto_power.enabled then
  272.     gpu.setBackground(config.color_scheme.success)
  273.     gpu.fill(14+65*config.auto_power.start_percent/10^2,8,1,3," ")
  274.     gpu.setBackground(config.color_scheme.error)
  275.     gpu.fill(14+65*config.auto_power.stop_percent/10^2,8,1,3," ")
  276.   end
  277. end
  278.  
  279. --#draw_config
  280.  
  281. function draw_config()
  282.   gpu.setBackground(config.color_scheme.button)
  283.   gpu.fill(5,9,71,9," ")
  284.   gpu.setForeground(0xFFFFFF)
  285.   term.setCursor(36,9)
  286.   term.write("Configure")
  287.   term.setCursor(35,10)
  288.   term.write("Start: "..config.auto_power.start_percent.."%")
  289.   term.setCursor(36,11)
  290.   term.write("Stop: "..config.auto_power.stop_percent.."%")
  291.   drawProgressBar(8,12,65,3,reactor.getEnergyStored()/10^7)
  292.   gpu.setBackground(config.color_scheme.success)
  293.   gpu.fill(8+65*config.auto_power.start_percent/100,12,1,3," ")
  294.   gpu.setBackground(config.color_scheme.error)
  295.   gpu.fill(8+65*config.auto_power.stop_percent/100,12,1,3," ")
  296.   gpu.setBackground(config.color_scheme.button)
  297.   gpu.setForeground(0xFFFFFF)
  298.   term.setCursor(37+#("Start: "..config.auto_power.start_percent.."%"),10)
  299.   term.write("[")
  300.   gpu.setForeground(config.color_scheme.error)
  301.   term.write("-")
  302.   gpu.setForeground(0xFFFFFF)
  303.   term.write("]  [")
  304.   gpu.setForeground(config.color_scheme.success)
  305.   term.write("+")
  306.   gpu.setForeground(0xFFFFFF)
  307.   term.write("]")
  308.   term.setCursor(38+#("Stop: "..config.auto_power.stop_percent.."#"),11)
  309.   term.write("[")
  310.   gpu.setForeground(config.color_scheme.error)
  311.   term.write("-")
  312.   gpu.setForeground(0xFFFFFF)
  313.   term.write("]  [")
  314.   gpu.setForeground(config.color_scheme.success)
  315.   term.write("+")
  316.   gpu.setForeground(0xFFFFFF)
  317.   term.write("]")
  318.   term.setCursor(5,9)
  319.   term.write("[")
  320.   gpu.setForeground(config.color_scheme.info)
  321.   term.write("back")
  322.   gpu.setForeground(0xFFFFFF)
  323.   term.write("]")
  324. end
  325.  
  326. --#drawProgressBar
  327.  
  328. function drawProgressBar(x,y,w,h,percent)
  329.   gpu.setBackground(config.color_scheme.progressBar.background)
  330.   gpu.fill(x,y,w,h," ")
  331.   gpu.setBackground(config.color_scheme.progressBar.foreground)
  332.   gpu.fill(x,y,w*percent,h," ")
  333. end
  334.  
  335. --#listen
  336.  
  337. function listen(name,address,x,y,button,player)
  338.   if x >= 74 and x <= 80 and y == 1 then
  339.     running = false
  340.   end
  341.   if screen == "main" then
  342.     if x >= 70 and y >=2 and x <= 80 and y <= 4 and config.auto_power.enabled ~= true then
  343.       reactor.setActive(not reactor.getActive())
  344.     elseif x >= 1 and y >=2 and x <= 69 and y <= 4 then
  345.       config.auto_power.enabled = not config.auto_power.enabled
  346.       save_config()
  347.     elseif x >= 70 and y >= 2 and x <= 80 and y <= 4 and config.auto_power.enabled then
  348.       screen = "config"
  349.     end
  350.   elseif screen=="config" then
  351.     if x>= 5 and x <= 10 and y == 9 then
  352.       screen="main"
  353.     elseif x >= 37 + #("Start: "..config.auto_power.start_percent.."%") and x <= 40+#("Start: "..config.auto_power.start_percent.."%") and y == 10 and config.auto_power.start_percent ~= 0 then
  354.       config.auto_power.start_percent = config.auto_power.start_percent-1
  355.       save_config()
  356.     elseif x >= 43 + #("Start: "..config.auto_power.start_percent.."%") and x <= 46+#("Start: "..config.auto_power.start_percent.."%") and y == 10 and config.auto_power.start_percent+1 ~= config.auto_power.stop_percent then
  357.       config.auto_power.start_percent = config.auto_power.start_percent+1
  358.       save_config()
  359.     elseif x >= 38 + #("Stop: "..config.auto_power.stop_percent.."%") and x <= 41 + #("Stop: "..config.auto_power.stop_percent.."%") and y == 11 and config.auto_power.stop_percent - 1 ~= config.auto_power.start_percent then
  360.       config.auto_power.stop_percent = config.auto_power.stop_percent - 1
  361.       save_config()
  362.     elseif x >= 44 + #("Stop: "..config.auto_power.stop_percent.."%") and x <= 47 + #("Stop: "..config.auto_power.stop_percent.."%") and y == 11 and config.auto_power.stop_percent ~= 100 then
  363.       config.auto_power.stop_percent = config.auto_power.stop_percent + 1
  364.       save_config()
  365.     end
  366.   end
  367. end
  368.  
  369. --#countTable
  370.  
  371. function countTable(table)
  372. local result = 0
  373.   for k,v in pairs(table) do
  374.     result = result+1
  375.   end
  376. return result
  377. end
  378.  
  379. --#round
  380.  
  381. function round(num,idp)
  382.   local mult = 10^(idp or 0)
  383.   return math.floor(num*mult+0.5)/mult
  384. end
  385.  
  386. --#init
  387. if not fs.exists("/etc/open-reactors.cfg") then
  388.   install()
  389. else
  390.   main()
  391. end
  392. event.ignore("touch",listen)
  393. gpu.setBackground(0x000000)
  394. gpu.setForeground(0xFFFFFF)
  395. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement