Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --computercraft
- --reactor controller for AE fission reactor by drPepper
- --Requires a turtle below the reactor block, a chest below it, and an empty space in front of it
- -- "THE BEER-WARE LICENSE"
- -- drPepper@KOPROKUBACH wrote this file. As long as you retain this notice you
- -- can do whatever you want with this stuff. If we meet some day, in-game or IRL,
- -- and you think this stuff is worth it, you can give me a beer in return
- --resume table
- rt = {}
- rt.reactorEmpty = false
- rt.turtleEmpty = false
- rt.chestEmpty = false
- rt.needAssistance = false
- --make sure reactor is working
- rt.setReactorWorking = false
- rt.guiMessage = ""
- monitor = nil
- PASTEBIN = "SVKJqCE0"
- --time to re-check reactor state
- REFRESH_TIME = 1.0
- function DetectPeripheral(name)
- local sides = {"top" , "bottom" , "front" , "left" , "right" , "back"}
- for i = 1, 6 do
- if peripheral.isPresent(sides[i]) and peripheral.getType(sides[i]) == name then
- print("found "..tostring(name)..": "..tostring(sides[i]))
- return sides[i]
- end
- end
- return nil
- end
- function WriteResume()
- local file = fs.open("resume.f","w")
- local sT = textutils.serialize(rt)
- file.write(sT)
- file.close()
- return
- end
- function ReadResume()
- if not fs.exists("resume.f") then
- WriteResume()
- return
- end
- local file = fs.open("resume.f","r")
- local sT = file.readAll()
- rt = textutils.unserialize(sT)
- file.close()
- return
- end
- --params are boolean
- function setVarReactorEmpty(empty)
- rt.reactorEmpty = empty
- WriteResume()
- print("ReactorEmpty: "..tostring(empty))
- end
- function setVarTurtleEmpty(empty)
- rt.turtleEmpty = empty
- WriteResume()
- print("TurtleEmpty: "..tostring(empty))
- end
- function setVarChestEmpty(empty)
- rt.chestEmpty = empty
- WriteResume()
- print("ChestEmpty: "..tostring(empty))
- end
- --fills reactor chamber and fully prepares for the next fill
- --returns true on success: reactor filled
- function fillReactor()
- turtle.select(1)
- if turtle.getItemCount(1) ~= 0 then
- setVarTurtleEmpty(false)
- if turtle.dropUp() then
- setVarReactorEmpty(false)
- setVarTurtleEmpty(true)
- if turtle.suckDown() then
- setVarTurtleEmpty(false)
- return true
- else
- setVarChestEmpty(true)
- return true
- end
- else
- setVarReactorEmpty(false)
- return true
- end
- else
- setVarTurtleEmpty(true)
- if turtle.suckDown() then
- setVarTurtleEmpty(false)
- else
- setVarChestEmpty(true)
- end
- return false --to start again
- end
- end
- --empties the reactor and tries to preserve used (if any) in the turtle and return unused cell to the chest (in this case)
- function emptyReactor()
- turtle.select(1)
- if turtle.getItemCount(1) == 0 then
- setVarTurtleEmpty(true)
- if turtle.suckUp() then
- setVarReactorEmpty(true)
- setVarTurtleEmpty(false)
- return
- else
- setVarReactorEmpty(true)
- return
- end
- else
- setVarTurtleEmpty(false)
- if turtle.dropDown() then
- setVarTurtleEmpty(true)
- if turtle.suckUp() then
- setVarTurtleEmpty(false)
- end
- setVarReactorEmpty(true)
- return
- else
- print("chest overfilled")
- rt.needAssistance = true
- turtle.drop()
- setVarTurtleEmpty(true)
- if turtle.suckUp() then
- setVarTurtleEmpty(false)
- end
- setVarReactorEmpty(true)
- return
- end
- end
- end
- function ProcessClick(cx, cy)
- if cy == 3 and cx < 5 then
- rt.chestEmpty = false
- elseif cy == 4 and rt.needAssistance then
- rt.needAssistance = false
- elseif cy ~= 4 and cx > 5 and cy < 10 then
- --start
- rt.guiMessage = " starting... "
- elseif cy ~= 4 and cx > 11 then
- --stop
- rt.guiMessage = " stopping... "
- end
- end
- function DrawGUI()
- --RCTR #### ####
- --TRTL #### ####
- --CHST #### ####
- --messagemessage
- if monitor == nil then
- return
- end
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- monitor.setTextColor(colors.white)
- monitor.setCursorPos(1,1)
- if rt.reactorEmpty then
- monitor.setBackgroundColor(colors.red)
- else
- monitor.setBackgroundColor(colors.green)
- end
- monitor.write("RCTR")
- monitor.setCursorPos(1,2)
- if rt.turtleEmpty then
- monitor.setBackgroundColor(colors.red)
- else
- monitor.setBackgroundColor(colors.green)
- end
- monitor.write("TRTL")
- monitor.setCursorPos(1,3)
- if rt.chestEmpty then
- monitor.setBackgroundColor(colors.red)
- else
- monitor.setBackgroundColor(colors.green)
- end
- monitor.write("CHST")
- local i = 1
- monitor.setBackgroundColor(colors.green)
- for i=1,3 do
- monitor.setCursorPos(6,i)
- monitor.write(" ")
- end
- monitor.setBackgroundColor(colors.red)
- for i=1,3 do
- monitor.setCursorPos(11,i)
- monitor.write(" ")
- end
- if rt.needAssistance then
- monitor.setBackgroundColor(colors.orange)
- monitor.setTextColor(colors.black)
- end
- monitor.setCursorPos(1,4)
- monitor.write(rt.guiMessage)
- end
- ReadResume()
- monitor = peripheral.wrap(DetectPeripheral("monitor"))
- termSizeX, termSizeY = term.getSize()
- while true do
- DrawGUI()
- --text UI
- term.clear()
- term.setCursorPos(1,1)
- term.write("REACTOR [")
- if rt.reactorEmpty then
- term.write(" ]")
- else
- term.write("X]")
- end
- term.setCursorPos(1,2)
- term.write("TURTLE [")
- if rt.turtleEmpty then
- term.write(" ]")
- else
- term.write("X]")
- end
- term.setCursorPos(1,3)
- term.write("CHEST [")
- if rt.chestEmpty then
- print(" ]")
- else
- print("X]")
- end
- if rt.needAssistance then
- term.setCursorPos(1,4)
- term.write("\nGET A HAZMAT SUIT AND GO CHECK THE REACTOR!\n")
- end
- term.setCursorPos(14,1)
- term.write("last update:")
- term.setCursorPos(14,2)
- term.write(tostring(os.time()))
- term.setCursorPos(14,3)
- if rt.setReactorWorking then
- print("ONLINE")
- else
- print("STOPPED")
- end
- term.setCursorPos(1,termSizeY)
- os.startTimer(REFRESH_TIME)
- print("H - help")
- local sEvent, param = os.pullEvent()
- if sEvent == "key" then
- if param == 31 then
- --TODO start
- if not rt.needAssistance then
- print("Starting...")
- if fillReactor() then
- print("Success!")
- sleep(1)
- else
- print("Failure!")
- sleep(1)
- end
- end
- elseif param == 30 then
- --TODO abort
- print("Stopping...")
- emptyReactor()
- print("Done.")
- sleep(1)
- elseif param == 22 then
- --update
- fs.delete("resume.f")
- shell.run("rm", "startup")
- shell.run("pastebin", "get "..PASTEBIN.." startup")
- os.reboot()
- elseif param == 35 then
- --help
- print("S - start, A - abort, R - reset all warnings. Always leave some space free in the chest.")
- elseif param == 19 then
- --reset
- setVarChestEmpty(false)
- rt.needAssistance = false
- end
- elseif sEvent == "timer" then
- if rt.setReactorWorking then
- if not rt.needAssistance then
- fillReactor()
- end
- else
- emptyReactor()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement