Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- DebiTRControl - (c) monster010
- ----------------------------------
- ----- DONT CHANGE UNDER THIS -----
- ----------------------------------
- os.loadAPI("/debitrcontrol/api/config")
- os.loadAPI("/debitrcontrol/api/monster010")
- os.loadAPI("/debitrcontrol/api/btn")
- os.loadAPI("/debitrcontrol/api/lbl")
- local software = "DebiTRControl"
- local monitor
- local cfg = {}
- local button = {}
- local ns = {}
- local tbs = {}
- local reactor = {}
- local ball = {}
- local consum = 0
- local produced = 0
- local labels = {
- rod1 = {txt = "Rod 1", x = 2, y = 3},
- rod2 = {txt = "Rod 2", x = 2, y = 5},
- rod3 = {txt = "Rod 3", x = 2, y = 7},
- rod4 = {txt = "Rod 4", x = 2, y = 9},
- rod5 = {txt = "Rod 5", x = 2, y = 11},
- rod6 = {txt = "Rod 6", x = 2, y = 13},
- rod7 = {txt = "Rod 7", x = 2, y = 15},
- rod8 = {txt = "Rod 8", x = 2, y = 17},
- rod9 = {txt = "Rod 9", x = 2, y = 19},
- rod10 = {txt = "Rod 10", x = 2, y = 21},
- rod11 = {txt = "Rod 11", x = 14, y = 3},
- rod12 = {txt = "Rod 12", x = 14, y = 5},
- rod13 = {txt = "Rod 13", x = 14, y = 7},
- rod14 = {txt = "Rod 14", x = 14, y = 9},
- rod15 = {txt = "Rod 15", x = 14, y = 11},
- rod16 = {txt = "Rod 16", x = 14, y = 13},
- rod17 = {txt = "Rod 17", x = 14, y = 15},
- rod18 = {txt = "Rod 18", x = 14, y = 17},
- rod19 = {txt = "Rod 19", x = 14, y = 19},
- rod20 = {txt = "Rod 20", x = 14, y = 21}
- }
- function addButtons()
- for name, data in pairs(button) do
- if name == "ctr" then
- btn.add(name, data["name"], toggleCControl, data["args"], data["x"], data["y"], data["w"], data["h"])
- elseif name == "reac" then
- btn.add(name, data["name"], toggleReactor, data["args"], data["x"], data["y"], data["w"], data["h"], colors.red, colors.lime)
- elseif string.find(name, "tb") then
- btn.add(name, data["name"], toggleTurbine, data["args"], data["x"], data["y"], data["w"], data["h"], colors.red, colors.lime)
- elseif string.find(name, "rd") and string.find(name, "up") then
- btn.add(name, data["name"], setRodProzendUp, data["args"], data["x"], data["y"], data["w"], data["h"])
- elseif string.find(name, "rd") and string.find(name, "down") then
- btn.add(name, data["name"], setRodProzendDown, data["args"], data["x"], data["y"], data["w"], data["h"])
- else
- btn.add(name, data["name"], false, data["args"], data["x"], data["y"], data["w"], data["h"])
- end
- end
- btn.add("notstop", "Notstopp", notstop, false, ns["x"], ns["y"], ns["w"], ns["h"], colors.red, colors.lime)
- btn.screen()
- end
- function addLabels()
- for name, data in pairs(labels) do
- lbl.add(name, data["txt"], data["x"], data["y"])
- end
- lbl.screen()
- end
- function connect()
- for name, data in pairs(tbs) do
- data["tb"] = peripheral.wrap(data["name"])
- end
- reactor["rc"] = peripheral.wrap(reactor["name"])
- ball["bl"] = peripheral.wrap(ball["name"])
- end
- function getProdInTPS()
- for name, data in pairs(tbs) do
- produced = produced + data["tb"].getEnergyProducedLastTick()
- end
- end
- function getConsumInTPS()
- local oldStand = ball["bl"].getEnergyStored()
- local newStand
- local myTimer = os.startTimer(1)
- while true do
- local event, args = os.pullEvent()
- if event == "timer" and args == myTimer then
- consum = 0
- newStand = ball["bl"].getEnergyStored()
- consum = ((oldStand - newStand) - produced) / cfg["tps"]
- produced = 0
- consum = math.floor(consum)
- print(consum)
- break
- end
- end
- end
- function cControlTurbines()
- local number = consum / cfg["tbpt"]
- local i = 0
- for name, data in pairs(tbs) do
- if i < number then
- data["tb"].setActive(true)
- else
- data["tb"].setActive(false)
- end
- i = i + 1
- end
- end
- function cControlReactor()
- if consum > 0 then
- reactor["rc"].setActive(true)
- else
- reactor["rc"].setActive(false)
- end
- end
- function cControlRods()
- --local pro = 100 - (consum / (cfg["tbpt"] / 10))
- reactor["rc"].setAllControlRodLevels(100)--math.floor(pro))
- end
- function computerControl()
- while true do
- if getDifferenceBall() > 0 and consum < 0 then
- fillBallFull()
- getConsumInTPS()
- elseif not button["ctr"]["active"] then
- getProdInTPS()
- getConsumInTPS()
- cControlReactor()
- cControlRods()
- cControlTurbines()
- else
- getConsumInTPS()
- end
- monster010.saveFile('/debitrcontrol/save', button, true)
- end
- end
- function getDifferenceBall()
- return ball["bl"].getMaxEnergyStored() - ball["bl"].getEnergyStored()
- end
- function fillBallFull()
- if not button["ctr"]["active"] then
- local myTimer = os.startTimer(1)
- while true do
- local event, args = os.pullEvent()
- if event == "timer" and args == myTimer then
- controlReactor(true)
- controlTurbine(1, true)
- controlRods(10)
- break
- end
- end
- end
- end
- function controlTurbine(i, stat)
- tbs["tb"..i]["tb"].setActive(stat)
- end
- function controlTurbines(stat)
- for name, data in pairs(tbs) do
- data["tb"].setActive(false)
- end
- end
- function controlReactor(stat)
- reactor["rc"].setActive(stat)
- end
- function controlRods(p)
- p = 100 - p
- reactor["rc"].setAllControlRodLevels(p)
- end
- function controlRod(i, p)
- reactor["rc"].setControllRodLevel(i, p)
- end
- function toggleCControl()
- if not ns["active"] then
- button["ctr"]["active"] = not button["ctr"]["active"]
- btn.set("ctr", button["ctr"]["active"], true)
- end
- end
- function checkStats()
- local myTimer = os.startTimer(5)
- while true do
- local event, args = os.pullEvent()
- if event == "timer" and args == myTimer then
- for name, data in pairs(tbs) do
- button[name]["active"] = data["tb"].getActive()
- btn.set(name, button[name]["active"])
- end
- button["reac"]["active"] = reactor["rc"].getActive()
- btn.set("reac", button["reac"]["active"])
- btn.screen()
- break
- end
- end
- end
- function toggleTurbine(tb)
- if not ns["active"] then
- button[tb]["active"] = not button[tb]["active"]
- btn.set(tb, button[tb]["active"], true)
- tbs[tb]["tb"].setActive(button[tb]["active"])
- end
- end
- function toggleReactor()
- if not ns["active"] then
- button["reac"]["active"] = not button["reac"]["active"]
- btn.set("reac", button["reac"]["active"], true)
- reactor["rc"].setActive(button["reac"]["active"])
- end
- end
- function setRodProzendUp(args)
- if not ns["active"] then
- reactor["rc"].setControlRodLevel(args, checkControlRodLevel(args, "-10"))
- btn.flash("rd"..(args + 1).."up")
- end
- end
- function setRodProzendDown(args)
- if not ns["active"] then
- reactor["rc"].setControlRodLevel(args, checkControlRodLevel(args, "+10"))
- btn.flash("rd"..(args + 1).."down")
- end
- end
- function checkControlRodLevel(id, arg)
- local pro = reactor["rc"].getControlRodLevel(id)
- if pro == 100 and arg == "-10" then
- return (pro - 10)
- elseif pro == 0 and arg == "+10" then
- return (pro + 10)
- elseif pro == 100 and arg == "+10" then
- return pro
- elseif pro == 0 and arg == "-10" then
- return pro
- elseif arg == "-10" then
- return (pro - 10)
- elseif arg == "+10" then
- return (pro + 10)
- end
- end
- function notstop()
- ns["active"] = not ns["active"]
- btn.toggle("notstop")
- if ns["active"] then
- controlReactor(false)
- controlTurbines(false)
- button["ctr"]["active"] = true
- btn.set("ctr", true, true)
- else
- button["ctr"]["active"] = false
- btn.set("ctr", false, true)
- end
- monster010.saveFile('/debitrcontrol/savens', ns, true)
- end
- function load()
- if fs.exists("/debitrcontrol/save") then
- button = monster010.loadFile("/debitrcontrol/save", true)
- end
- if fs.exists("/debitrcontrol/savens") then
- ns = monster010.loadFile("/debitrcontrol/savens", true)
- end
- addButtons()
- if ns["active"] then
- btn.set("notstop", true)
- controlReactor(false)
- controlTurbines(false)
- btn.set("ctr", true)
- btn.screen()
- else
- for name, data in pairs(tbs) do
- data["tb"].setActive(button[name]["active"])
- end
- end
- end
- function loadCFG()
- tbs = config.get("tbs")
- reactor = config.get("reactor")
- ball = config.get("deball")
- for name, data in pairs(tbs) do
- data["tb"] = {}
- end
- ball["bl"] = {}
- reactor["rc"] = {}
- end
- function launchRestart()
- while true do
- local event, args = os.pullEvent("key")
- if args == 19 then
- os.reboot()
- end
- end
- end
- config.load("/debitrcontrol/cfg")
- cfg = config.getConfig()
- monster010.construct(cfg["monSide"], software)
- monster010.startUp()
- monster010.heading(software)
- monitor = monster010.getMonitor()
- btn.construct(monitor)
- lbl.construct(monitor)
- loadCFG()
- connect()
- load()
- addLabels()
- monster010.startUpDone()
- while true do
- parallel.waitForAny(btn.getClick, computerControl, checkStats, launchRestart)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement