Advertisement
Guest User

Untitled

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