Advertisement
Guest User

br

a guest
Jan 24th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.80 KB | None | 0 0
  1. local args = {...}
  2.  
  3. local data = {}
  4.  
  5. if args[1] == "clean" then
  6.     print("Cleaning...")
  7.     fs.delete("reactor/settings.dat")
  8.     sleep(1)
  9.     term.clear()
  10. end
  11.  
  12. function saveFile()
  13.     local file = fs.open("reactor/settings.dat", "w")
  14.     file.write( textutils.serialize(data) )
  15.     file.close()
  16. end
  17.  
  18. if fs.exists("reactor/settings.dat") then
  19.     local myData = fs.open("reactor/settings.dat", "r")
  20.    
  21.     data = textutils.unserialize(myData.readAll())
  22.    
  23.     myData.close()
  24.     print("Loaded file settings")
  25. else
  26.     fs.makeDir("reactor")
  27.    
  28.     local defaults = {}
  29.    
  30.     -- Set defaults
  31.     defaults["enabled"] = true --Should the automation stuff work?
  32.     defaults["turn_on"] = 50.00 -- When should the reactor turn on
  33.     defaults["turn_off"] = 100.00 -- When should it turn off
  34.    
  35.     local periphs = peripheral.getNames()
  36.     for i in ipairs( periphs ) do
  37.         local name = periphs[i]
  38.        
  39.         local m = string.match(name, "BigReactors%-Reactor_%d")
  40.         if m ~= nil then
  41.             print("Set " .. m .. " as default reactor")
  42.             data["reactor"] = name
  43.             break
  44.         end
  45.     end
  46.     data["automate"] = defaults
  47.    
  48.     -- Save to file
  49.     saveFile()
  50.    
  51.     print("Written new file settings")
  52. end
  53.  
  54. local reactor = peripheral.wrap( data["reactor"] )
  55.  
  56. local selected = 1
  57. local runProgram = true
  58.  
  59. local menu = {
  60.     [1] = {
  61.         name = "Turn on reactor",
  62.         func = function() toggleReactor(true) end
  63.     },
  64.     [2] = {
  65.         name = "Turn off reactor",
  66.         func = function() toggleReactor(false) end
  67.     },
  68.     [3] = {
  69.         name = "Toggle automation",
  70.         func = function() toggleAutomate() end
  71.     },
  72.     [4] = {
  73.         name = "Exit program",
  74.         func = function() runProgram = false end
  75.     }
  76. }
  77.  
  78. function mainLoop ()
  79.     clear()
  80.     while runProgram do
  81.         drawMenu()
  82.        
  83.         parallel.waitForAny(doMenu, automate)
  84.        
  85.         sleep(0)
  86.         clear()
  87.     end
  88. end
  89.  
  90. function doMenu()
  91.     local event, key = os.pullEvent("key")
  92.        
  93.     if key == keys.up then
  94.         selected = selected - 1
  95.    
  96.     elseif key == keys.down then
  97.         selected = selected + 1
  98.    
  99.     elseif key == keys.enter then
  100.         menu[selected].func()
  101.        
  102.     end
  103.    
  104.     if selected <= 0 then
  105.         selected = #menu
  106.     elseif selected > #menu then
  107.         selected = 1
  108.     end
  109. end
  110.  
  111. function drawMenu()
  112.     term.write("Active: " .. tostring( reactor.getActive()) )
  113.  
  114.     local autoStr = "Auotmated: " .. tostring( data["automate"]["enabled"])
  115.     local w, h = term.getSize()
  116.     term.setCursorPos( w - autoStr:len(), 1)
  117.     term.write(autoStr)
  118.    
  119.     local cX, cY = term.getCursorPos()
  120.    
  121.     for i in pairs(menu) do
  122.         local word = menu[i].name
  123.         if selected == i then
  124.             word = ">" .. menu[i].name .. "<"
  125.         end
  126.        
  127.         term.setCursorPos( math.ceil((w /2) - (word:len()/2)), cY + i )
  128.         term.write( word )
  129.     end
  130.    
  131.     term.setCursorPos(1, h)
  132.     term.write("Power: " .. ((reactor.getEnergyStored() / 10000000) * 100) )   
  133. end
  134.  
  135. function clear()
  136.     term.clear()
  137.     term.setCursorPos(1,1)
  138. end
  139.  
  140. function round( num, n )
  141.     local mult = 10^(n or 0)
  142.     return math.floor(num * mult + 0.5) / mult
  143. end
  144.  
  145. function writeToMonitor( m, e )
  146.     -- Reset the display
  147.     m.clear()
  148.     m.setCursorPos(1,1)
  149.    
  150.     local cX, cY = m.getCursorPos()
  151.     local w, h = m.getSize()
  152.    
  153.     m.write("Power:")
  154.     cY = cY + 1
  155.    
  156.     local pwrStr =  tostring(round(e,  2)) .. '%'
  157.    
  158.     m.setCursorPos( math.ceil((w /2) - (pwrStr:len()/2)), cY )
  159.    
  160.     if e >= data["automate"]["turn_off"] then
  161.         m.setTextColor(colors.green)
  162.     else
  163.         m.setTextColor(colors.red)
  164.     end
  165.     m.write(pwrStr)
  166.    
  167.     cY = cY + 1
  168.     if cY > h then
  169.         return
  170.     end
  171.    
  172.     m.setCursorPos(1, cY)
  173.     m.setTextColor(colors.white)
  174.    
  175.     m.write("Automated:")
  176.     local autoStr = tostring( data["automate"]["enabled"])
  177.     cY = cY + 1
  178.     m.setCursorPos(math.ceil((w /2) - (autoStr:len()/2)), cY)
  179.    
  180.     if data["automate"]["enabled"] then
  181.         m.setTextColor(colors.green)
  182.     else
  183.         m.setTextColor(colors.red)
  184.     end
  185.     m.write(autoStr)   
  186.     m.setTextColor(colors.white)
  187.    
  188. end
  189.  
  190. function automate()
  191.     if reactor == nil then
  192.         sleep(10)
  193.         return
  194.     end
  195.    
  196.     local maxEnergy = 10000000
  197.     local energyPercent = (reactor.getEnergyStored() / maxEnergy) * 100
  198.    
  199.     -- Draw onto monitors
  200.     local periphs = peripheral.getNames()
  201.     for i in ipairs( periphs ) do
  202.         local name = periphs[i]
  203.        
  204.         local m = string.match(name, "monitor_%d")
  205.         if m ~= nil then
  206.             local mon = peripheral.wrap(name)
  207.             mon.setTextScale(2)
  208.            
  209.             writeToMonitor(mon, energyPercent)
  210.         end
  211.     end
  212.    
  213.     if not reactor.getConnected() then
  214.         sleep(10)
  215.         return
  216.     end
  217.     if not data["automate"]["enabled"] then
  218.         sleep(10)
  219.         return
  220.     end
  221.    
  222.    
  223.     if reactor.getActive() then
  224.         if energyPercent >= data["automate"]["turn_off"] then
  225.             reactor.setActive(false)
  226.         end
  227.     else
  228.         if energyPercent <= data["automate"]["turn_on"] then
  229.             reactor.setActive(true)
  230.         end
  231.     end
  232.     sleep(1)
  233.    
  234. end
  235.  
  236. function toggleReactor( active )
  237.     reactor.setActive(active)
  238. end
  239.  
  240.  
  241. function toggleAutomate()
  242.     data["automate"]["enabled"] = not data["automate"]["enabled"]
  243.     saveFile()
  244. end
  245.  
  246. mainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement