monster010

CC - DebiTRControl

Apr 20th, 2016
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.96 KB | None | 0 0
  1. -- DebiTRControl - (c) monster010
  2.  
  3.  
  4. ----------------------------------
  5. ----- DONT CHANGE UNDER THIS -----
  6. ----------------------------------
  7. os.loadAPI("/debitrcontrol/api/config")
  8. os.loadAPI("/debitrcontrol/api/monster010")
  9. os.loadAPI("/debitrcontrol/api/btn")
  10. os.loadAPI("/debitrcontrol/api/lbl")
  11.  
  12.  
  13.  
  14. local software = "DebiTRControl"
  15. local monitor
  16. local cfg = {}
  17. local button = {}
  18. local ns = {}
  19. local tbs = {}
  20. local reactor = {}
  21. local ball = {}
  22.  
  23. local consum = 0
  24. local produced = 0
  25.  
  26. local labels = {
  27.     rod1 = {txt = "Rod 1", x = 2, y = 3},
  28.     rod2 = {txt = "Rod 2", x = 2, y = 5},
  29.     rod3 = {txt = "Rod 3", x = 2, y = 7},
  30.     rod4 = {txt = "Rod 4", x = 2, y = 9},
  31.     rod5 = {txt = "Rod 5", x = 2, y = 11},
  32. }
  33.  
  34.  
  35.  
  36.  
  37. function addButtons()
  38.     for name, data in pairs(button) do
  39.         if name == "ctr" then
  40.             btn.add(name, data["name"], toggleCControl, data["args"], data["x"], data["y"], data["w"], data["h"])
  41.         elseif name == "reac" then
  42.             btn.add(name, data["name"], toggleReactor, data["args"], data["x"], data["y"], data["w"], data["h"], colors.red, colors.lime)
  43.         elseif string.find(name, "tb") then
  44.             btn.add(name, data["name"], toggleTurbine, data["args"], data["x"], data["y"], data["w"], data["h"], colors.red, colors.lime)
  45.         elseif string.find(name, "rd") and string.find(name, "up") then
  46.             btn.add(name, data["name"], setRodProzendUp, data["args"], data["x"], data["y"], data["w"], data["h"])
  47.         elseif string.find(name, "rd") and string.find(name, "down") then
  48.             btn.add(name, data["name"], setRodProzendDown, data["args"], data["x"], data["y"], data["w"], data["h"])
  49.         else
  50.             btn.add(name, data["name"], false, data["args"], data["x"], data["y"], data["w"], data["h"])
  51.         end
  52.     end
  53.  
  54.     btn.add("notstop", "Notstopp", notstop, false, ns["x"], ns["y"], ns["w"], ns["h"], colors.red, colors.lime)
  55.  
  56.     btn.screen()
  57. end
  58.  
  59. function addLabels()
  60.     for name, data in pairs(labels) do
  61.         lbl.add(name, data["txt"], data["x"], data["y"])
  62.     end
  63.  
  64.     lbl.screen()
  65. end
  66.  
  67.  
  68.  
  69.  
  70.  
  71. function connect()
  72.     for name, data in pairs(tbs) do
  73.         data["tb"] = peripheral.wrap(data["name"])
  74.     end
  75.  
  76.     reactor["rc"] = peripheral.wrap(reactor["name"])
  77.     ball["bl"] = peripheral.wrap(ball["name"])
  78. end
  79.  
  80. function getProdInTPS()
  81.     for name, data in pairs(tbs) do
  82.         produced = produced + data["tb"].getEnergyProducedLastTick()
  83.     end
  84. end
  85.  
  86. function getConsumInTPS()
  87.     local oldStand = ball["bl"].getEnergyStored()
  88.     local newStand
  89.     local myTimer = os.startTimer(1)
  90.  
  91.     while true do
  92.         local event, args = os.pullEvent()
  93.  
  94.         if event == "timer" and args == myTimer then
  95.             consum = 0
  96.  
  97.             newStand = ball["bl"].getEnergyStored()
  98.  
  99.             consum = ((oldStand - newStand) - produced) / cfg["tps"]
  100.  
  101.             produced = 0
  102.            
  103.             consum = math.floor(consum)
  104.  
  105.             print(consum)
  106.  
  107.             break
  108.         end
  109.     end
  110. end
  111.  
  112. function cControlTurbines()
  113.     local number = consum / cfg["tbpt"]
  114.     local i = 0
  115.  
  116.     for name, data in pairs(tbs) do
  117.         if i < number then
  118.             data["tb"].setActive(true)
  119.         else
  120.             data["tb"].setActive(false)
  121.         end
  122.  
  123.         i = i + 1
  124.     end
  125. end
  126.  
  127. function cControlReactor()
  128.     if consum > 0 then
  129.         reactor["rc"].setActive(true)
  130.     else
  131.         reactor["rc"].setActive(false)
  132.     end
  133. end
  134.  
  135. function cControlRods()
  136.     local pro = 100 - (consum / (cfg["tbpt"] / 10))
  137.  
  138.     reactor["rc"].setAllControlRodLevels(math.floor(pro))
  139. end
  140.  
  141. function computerControl()
  142.     while true do
  143.         if getDifferenceBall() > 0 and consum < 0 then
  144.             fillBallFull()
  145.             getConsumInTPS()
  146.         elseif not button["ctr"]["active"] then
  147.             getProdInTPS()
  148.             getConsumInTPS()
  149.             cControlReactor()
  150.             cControlRods()
  151.             cControlTurbines()
  152.         else
  153.             getConsumInTPS()
  154.         end
  155.  
  156.         monster010.saveFile('/debitrcontrol/save', button, true)
  157.     end
  158. end
  159.  
  160. function getDifferenceBall()
  161.     return ball["bl"].getMaxEnergyStored() - ball["bl"].getEnergyStored()
  162. end
  163.  
  164. function fillBallFull()
  165.     if not button["ctr"]["active"] then
  166.         local myTimer = os.startTimer(1)
  167.  
  168.         while true do
  169.             local event, args = os.pullEvent()
  170.  
  171.             if event == "timer" and args == myTimer then
  172.                 controlReactor(true)
  173.                 controlTurbine(1, true)
  174.                 controlRods(10)
  175.  
  176.                 break
  177.             end
  178.         end
  179.     end
  180. end
  181.  
  182. function controlTurbine(i, stat)
  183.     tbs["tb"..i]["tb"].setActive(stat)
  184. end
  185.  
  186. function controlTurbines(stat)
  187.     for name, data in pairs(tbs) do
  188.         data["tb"].setActive(false)
  189.     end
  190. end
  191.  
  192. function controlReactor(stat)
  193.     reactor["rc"].setActive(stat)
  194. end
  195.  
  196. function controlRods(p)
  197.     p = 100 - p
  198.     reactor["rc"].setAllControlRodLevels(p)
  199. end
  200.  
  201. function controlRod(i, p)
  202.     reactor["rc"].setControllRodLevel(i, p)
  203. end
  204.  
  205. function toggleCControl()
  206.     if not ns["active"] then
  207.         button["ctr"]["active"] = not button["ctr"]["active"]
  208.         btn.set("ctr", button["ctr"]["active"], true)
  209.     end
  210. end
  211.  
  212. function checkStats()
  213.     local myTimer = os.startTimer(5)
  214.  
  215.     while true do
  216.         local event, args = os.pullEvent()
  217.  
  218.         if event == "timer" and args == myTimer then
  219.             for name, data in pairs(tbs) do
  220.                 button[name]["active"] = data["tb"].getActive()
  221.                 btn.set(name, button[name]["active"])
  222.             end
  223.  
  224.             button["reac"]["active"] = reactor["rc"].getActive()
  225.             btn.set("reac", button["reac"]["active"])
  226.  
  227.             btn.screen()
  228.             break
  229.         end
  230.     end
  231. end
  232.  
  233. function toggleTurbine(tb)
  234.     if not ns["active"] then
  235.         button[tb]["active"] = not button[tb]["active"]
  236.         btn.set(tb, button[tb]["active"], true)
  237.         tbs[tb]["tb"].setActive(button[tb]["active"])
  238.     end
  239. end
  240.  
  241. function toggleReactor()
  242.     if not ns["active"] then
  243.         button["reac"]["active"] = not button["reac"]["active"]
  244.         btn.set("reac", button["reac"]["active"], true)
  245.         reactor["rc"].setActive(button["reac"]["active"])
  246.     end
  247. end
  248.  
  249. function setRodProzendUp(args)
  250.     if not ns["active"] then
  251.         reactor["rc"].setControlRodLevel(args, checkControlRodLevel(args, "-10"))
  252.         btn.flash("rd"..(args + 1).."up")
  253.     end
  254. end
  255.  
  256. function setRodProzendDown(args)
  257.     if not ns["active"] then
  258.         reactor["rc"].setControlRodLevel(args, checkControlRodLevel(args, "+10"))
  259.         btn.flash("rd"..(args + 1).."down")
  260.     end
  261. end
  262.  
  263. function checkControlRodLevel(id, arg)
  264.     local pro = reactor["rc"].getControlRodLevel(id)
  265.  
  266.     if pro == 100 and arg == "-10" then
  267.         return (pro - 10)
  268.     elseif pro == 0 and arg == "+10" then
  269.         return (pro + 10)
  270.     elseif pro == 100 and arg == "+10" then
  271.         return pro
  272.     elseif pro == 0 and arg == "-10" then
  273.         return pro
  274.     elseif arg == "-10" then
  275.         return (pro - 10)
  276.     elseif arg == "+10" then
  277.         return (pro + 10)
  278.     end
  279. end
  280.  
  281. function notstop()
  282.     ns["active"] = not ns["active"]
  283.     btn.toggle("notstop")
  284.  
  285.     if ns["active"] then
  286.         controlReactor(false)
  287.         controlTurbines(false)
  288.         button["ctr"]["active"] = true
  289.         btn.set("ctr", true, true)
  290.     else
  291.         button["ctr"]["active"] = false
  292.         btn.set("ctr", false, true)
  293.     end
  294.  
  295.     monster010.saveFile('/debitrcontrol/savens', ns, true)
  296. end
  297.  
  298.  
  299.  
  300.  
  301. function load()
  302.     if fs.exists("/debitrcontrol/save") then
  303.         button = monster010.loadFile("/debitrcontrol/save", true)
  304.     end
  305.  
  306.     if fs.exists("/debitrcontrol/savens") then
  307.         ns = monster010.loadFile("/debitrcontrol/savens", true)
  308.     end
  309.  
  310.     addButtons()
  311.  
  312.     if ns["active"] then
  313.         btn.set("notstop", true)
  314.  
  315.         controlReactor(false)
  316.         controlTurbines(false)
  317.         btn.set("ctr", true)
  318.  
  319.         btn.screen()
  320.     else
  321.         for name, data in pairs(tbs) do
  322.             data["tb"].setActive(button[name]["active"])
  323.         end
  324.     end
  325. end
  326.  
  327. function loadCFG()
  328.     tbs = config.get("tbs")
  329.     reactor = config.get("reactor")
  330.     ball = config.get("deball")
  331.  
  332.     for name, data in pairs(tbs) do
  333.         data["tb"] = {}
  334.     end
  335.  
  336.     ball["bl"] = {}
  337.     reactor["rc"] = {}
  338. end
  339.  
  340. function launchRestart()
  341.     while true do
  342.         local event, args = os.pullEvent("key")
  343.  
  344.         if args == 19 then
  345.             os.reboot()
  346.         end
  347.     end
  348. end
  349.  
  350.  
  351.  
  352. config.load("/debitrcontrol/cfg")
  353. cfg = config.getConfig()
  354.  
  355. monster010.construct(cfg["monSide"], software)
  356. monster010.startUp()
  357. monster010.heading(software)
  358. monitor = monster010.getMonitor()
  359. btn.construct(monitor)
  360. lbl.construct(monitor)
  361.  
  362.  
  363. loadCFG()
  364. connect()
  365. load()
  366. addLabels()
  367.  
  368. monster010.startUpDone()
  369.  
  370.  
  371.  
  372. while true do
  373.     parallel.waitForAny(btn.getClick, computerControl, checkStats, launchRestart)
  374. end
Add Comment
Please, Sign In to add comment