Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local data = {}
- if args[1] == "clean" then
- print("Cleaning...")
- fs.delete("reactor/settings.dat")
- sleep(1)
- term.clear()
- end
- function saveFile()
- local file = fs.open("reactor/settings.dat", "w")
- file.write( textutils.serialize(data) )
- file.close()
- end
- if fs.exists("reactor/settings.dat") then
- local myData = fs.open("reactor/settings.dat", "r")
- data = textutils.unserialize(myData.readAll())
- myData.close()
- print("Loaded file settings")
- else
- fs.makeDir("reactor")
- local defaults = {}
- -- Set defaults
- defaults["enabled"] = true --Should the automation stuff work?
- defaults["turn_on"] = 50.00 -- When should the reactor turn on
- defaults["turn_off"] = 100.00 -- When should it turn off
- local periphs = peripheral.getNames()
- for i in ipairs( periphs ) do
- local name = periphs[i]
- local m = string.match(name, "BigReactors%-Reactor_%d")
- if m ~= nil then
- print("Set " .. m .. " as default reactor")
- data["reactor"] = name
- break
- end
- end
- data["automate"] = defaults
- -- Save to file
- saveFile()
- print("Written new file settings")
- end
- local reactor = peripheral.wrap( data["reactor"] )
- local selected = 1
- local runProgram = true
- local menu = {
- [1] = {
- name = "Turn on reactor",
- func = function() toggleReactor(true) end
- },
- [2] = {
- name = "Turn off reactor",
- func = function() toggleReactor(false) end
- },
- [3] = {
- name = "Toggle automation",
- func = function() toggleAutomate() end
- },
- [4] = {
- name = "Exit program",
- func = function() runProgram = false end
- }
- }
- function mainLoop ()
- clear()
- while runProgram do
- drawMenu()
- parallel.waitForAny(doMenu, automate)
- sleep(0)
- clear()
- end
- end
- function doMenu()
- local event, key = os.pullEvent("key")
- if key == keys.up then
- selected = selected - 1
- elseif key == keys.down then
- selected = selected + 1
- elseif key == keys.enter then
- menu[selected].func()
- end
- if selected <= 0 then
- selected = #menu
- elseif selected > #menu then
- selected = 1
- end
- end
- function drawMenu()
- term.write("Active: " .. tostring( reactor.getActive()) )
- local autoStr = "Auotmated: " .. tostring( data["automate"]["enabled"])
- local w, h = term.getSize()
- term.setCursorPos( w - autoStr:len(), 1)
- term.write(autoStr)
- local cX, cY = term.getCursorPos()
- for i in pairs(menu) do
- local word = menu[i].name
- if selected == i then
- word = ">" .. menu[i].name .. "<"
- end
- term.setCursorPos( math.ceil((w /2) - (word:len()/2)), cY + i )
- term.write( word )
- end
- term.setCursorPos(1, h)
- term.write("Power: " .. ((reactor.getEnergyStored() / 10000000) * 100) )
- end
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- function round( num, n )
- local mult = 10^(n or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- function writeToMonitor( m, e )
- -- Reset the display
- m.clear()
- m.setCursorPos(1,1)
- local cX, cY = m.getCursorPos()
- local w, h = m.getSize()
- m.write("Power:")
- cY = cY + 1
- local pwrStr = tostring(round(e, 2)) .. '%'
- m.setCursorPos( math.ceil((w /2) - (pwrStr:len()/2)), cY )
- if e >= data["automate"]["turn_off"] then
- m.setTextColor(colors.green)
- else
- m.setTextColor(colors.red)
- end
- m.write(pwrStr)
- cY = cY + 1
- if cY > h then
- return
- end
- m.setCursorPos(1, cY)
- m.setTextColor(colors.white)
- m.write("Automated:")
- local autoStr = tostring( data["automate"]["enabled"])
- cY = cY + 1
- m.setCursorPos(math.ceil((w /2) - (autoStr:len()/2)), cY)
- if data["automate"]["enabled"] then
- m.setTextColor(colors.green)
- else
- m.setTextColor(colors.red)
- end
- m.write(autoStr)
- m.setTextColor(colors.white)
- end
- function automate()
- if reactor == nil then
- sleep(10)
- return
- end
- local maxEnergy = 10000000
- local energyPercent = (reactor.getEnergyStored() / maxEnergy) * 100
- -- Draw onto monitors
- local periphs = peripheral.getNames()
- for i in ipairs( periphs ) do
- local name = periphs[i]
- local m = string.match(name, "monitor_%d")
- if m ~= nil then
- local mon = peripheral.wrap(name)
- mon.setTextScale(2)
- writeToMonitor(mon, energyPercent)
- end
- end
- if not reactor.getConnected() then
- sleep(10)
- return
- end
- if not data["automate"]["enabled"] then
- sleep(10)
- return
- end
- if reactor.getActive() then
- if energyPercent >= data["automate"]["turn_off"] then
- reactor.setActive(false)
- end
- else
- if energyPercent <= data["automate"]["turn_on"] then
- reactor.setActive(true)
- end
- end
- sleep(1)
- end
- function toggleReactor( active )
- reactor.setActive(active)
- end
- function toggleAutomate()
- data["automate"]["enabled"] = not data["automate"]["enabled"]
- saveFile()
- end
- mainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement