Advertisement
Guest User

trcontrol

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