Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.sleep(5)
- --------------------------INIT BEGIN-----------------------------------------------------
- --init libraries
- local component = require("component")
- local term = require("term")
- local computer = require("computer")
- local thread = require("thread")
- local event = require("event")
- --Set GPU's and screens
- local gpu = component.proxy(component.get("40a"))
- local termgpu = component.proxy(component.get("92a"))
- local screen_address_main = component.get("1fe")
- local screen_address_term = component.get("e8b")
- --Prepare main screen and sets up terminal bind
- gpu.bind(screen_address_main)
- gpu.setResolution(90,25)
- local w,h = gpu.getResolution()
- gpu.fill(1,1,w,h," ")
- termgpu.bind(screen_address_term)
- term.bind(termgpu)
- --Init reactors and static variables
- local r1 = component.proxy("0c60c163-e924-47d3-ac83-6c686e854f04")
- local r2 = component.proxy("0ef58c7f-2984-4f8f-88b7-3474767dbb7a")
- local r3 = component.proxy("255aa1b0-b6dd-4e8c-8339-c04d548eea57")
- local r1gen, r2gen, r3gen = 31680, 304128, 916692
- local close = false
- local matrix = component.induction_matrix
- local maxEnergy = matrix.getMaxEnergy() / 2.5
- --------------------------INIT END-------------------------------------------------------
- ---------------------------FUNCTION BEGIN------------------------------------------------
- function r1a()
- r1.activate()
- end
- function r1d()
- r1.deactivate()
- end
- function r2a()
- r2.activate()
- end
- function r2d()
- r2.deactivate()
- end
- function r3a()
- r3.activate()
- end
- function r3d()
- r3.deactivate()
- end
- function activate_all()
- r1.activate()
- r2.activate()
- r3.activate()
- end
- function deactivate_all()
- r1.deactivate()
- r2.deactivate()
- r3.deactivate()
- end
- function write(x,y,z,colour)
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- gpu.set(x,y," ")
- gpu.setForeground(colour)
- gpu.set(x,y,z)
- gpu.setForeground(0xFFFFFF)
- end
- function exitprogram(_,_,_,z,_)
- if z == 207 then
- event.ignore("key_down", exitprogram)
- io.write("Program \n")
- close = true
- end
- end
- ------------------------------FUNCTION END-----------------------------------------------
- --Events register
- event.listen("key_down", exitprogram)
- ----------------------CONTROL THREAD BEGIN-----------------------------------------------
- --init thread
- local control_thread = thread.create(function()
- while true do
- local output = matrix.getOutput() / 2.5
- local current = matrix.getEnergy() / 2.5
- --Check if energy cell capacity exceeds 80%
- if current/maxEnergy > 0.8 then
- --Turn off all reactors if they aren't already
- if r1.isProcessing() or r2.isProcessing() or r3.isProcessing() == true then
- deactivate_all()
- end
- end
- if current/maxEnergy < 0.8 then
- --Check if energy cell capacity is less than 80% and then checks current output compared to reactor rates and current reactor status
- local r1flagproc = r1.isProcessing()
- local r2flagproc = r2.isProcessing()
- local r3flagproc = r3.isProcessing()
- local r1flag,r2flag,r3flag = false,false,false
- --Set flags to determine which reactors need to be turned on
- if output > r1gen + r2gen then
- r1flag,r2flag,r3flag = true,true,true
- elseif output > r1gen then
- r1flag,r2flag = true,true
- else
- r1flag = true
- end
- --Checks set flags and adjusts the reactors status if need be. No unneccessary on/off commands are sent
- if r1flag == true and r1flagproc == false then
- r1a()
- elseif r1flag == false and r1flagproc == true then
- r1d()
- else
- end
- if r2flag == true and r2flagproc == false then
- r2a()
- elseif r2flag == false and r2flagproc == true then
- r2d()
- else
- end
- if r3flag == true and r3flagproc == false then
- r3a()
- elseif r3flag == false and r3flagproc == true then
- r3d()
- else
- end
- end
- os.sleep(30)
- end
- end)
- -----------------------------CONTROL THREAD END------------------------------------------
- ----------------------------SCREEN THREAD BEGIN------------------------------------------
- -----------------------------SCREEN THREAD END-------------------------------------------
- ------------------------------IO THREAD BEGIN--------------------------------------------
- local io_thread = thread.create(function()
- while true do
- --terminal thread to allow commands to be executed
- term.clear()
- term.setCursor(1,1)
- term.write("Reactor Control active \n")
- term.write("Awaiting Input")
- term.setCursor(1,5)
- term.write("Press END to exit program")
- term.setCursor(1,3)
- local input = term.read()
- load(input)()
- end
- end)
- -----------------------------IO THREAD END-----------------------------------------------
- ----------------------------KILL THREAD BEGIN--------------------------------------------
- local kill_thread = thread.create(function()
- while true do
- if close == true then
- break
- end
- os.sleep(1)
- end
- end)
- ---------------------------KILL THREAD END-----------------------------------------------
- thread.waitForAny({control_thread,io_thread,kill_thread})
- io.write("Program closing \n")
- term.clear()
- os.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement