Advertisement
Guest User

reactor.lua

a guest
Aug 14th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.90 KB | None | 0 0
  1. BUTTON = require("buttonAPI")
  2.  
  3. local component = require("component")
  4. local event = require("event")
  5. local keyboard = require("keyboard")
  6. local gpu = component.gpu
  7. if not component.isAvailable("br_reactor") then
  8.   print("No reactor connected.")
  9.   os.exit()
  10. end
  11. local reactor = component.br_reactor
  12.  
  13. local colors = { red = 0xC14141, green = 0xDA841, grey = 0x47494C, lightGrey = 0xBBBBBB, black = 0x000000, white = 0xFFFFFF, blue = 0x4286F4 }
  14.  
  15. local infos = {}
  16. local sections = {}
  17. local energy = { x = 102, y = 4, w = 27, h = 36 }
  18.  
  19. reactor["stats"] = {}
  20. local programRunning = true
  21. local reactorRunning = false
  22. local autoTempControl = false
  23. local autoBufferControl = false
  24. local currentStored = 0
  25. local currentProduce = 0
  26. local currentCasetemp = 0
  27. local currentFueltemp = 0
  28. local currentFuelAmount = 0
  29. local maxCaseTemp = 0
  30. local maxFuelTemp = 0
  31. local minBuffer = 0
  32. local maxBuffer = 0
  33.  
  34. function setInfos()
  35.   infos["online"] = { x = 7, y = 29, w = 73, h = 1, title = "Reactor status: ", unit = "" }
  36.   infos["produce"] = { x = 7, y = 31, w = 73, h = 1, title = "RF per tick:    ", unit = " RF" }
  37.   infos["stored"] = { x = 7, y = 33, w = 73, h = 1, title = "Energy Stored:  ", unit = " RF" }
  38.   infos["casetemp"] = { x = 7, y = 35, w = 73, h = 1, title = "Case temp:      ", unit = " C" }
  39.   infos["fueltemp"] = { x = 7, y = 37, w = 73, h = 1, title = "Fuel temp:      ", unit = " C" }
  40.   infos["fuelamount"] = { x = 7, y = 39, w = 73, h = 1, title = "Fuel remaining: ", unit = " mB" }
  41. end
  42.  
  43. function setSections()
  44.   sections["control"] = { x = 2, y = 2, w = 95, h = 23, title = " CONTROL " }
  45.   sections["status"] = { x = 2, y = 27, w = 95, h = 14, title = " STATUS " }
  46.   sections["stored"] = { x = 100, y = 2, w = 30, h = 39, title = " BUFFER " }
  47. end
  48.  
  49. function setButtons()
  50.   BUTTON.setTable("reactor", toggleReactor, 7, 5, 27, 7, true, {on = "Reactor On", off = "Reactor Off"}, {on = colors.green, off = colors.red})
  51.   BUTTON.setTable("autoTemp", toggleAutoTemp, 7, 10, 27, 12, true, {on = "Temp Control ON", off = "Temp Control OFF"}, {on = colors.green, off = colors.red})
  52.   BUTTON.setTable("autoBuffer", toggleAutoBuffer, 7, 15, 27, 17, true, {on = "Buffer Control ON", off = "Buffer Control OFF"}, {on = colors.green, off = colors.red})
  53.   BUTTON.setTable("quitProgram", quitProgram, 7, 20, 27, 22, false, "Exit", colors.blue)
  54.  
  55.   BUTTON.setTable("addCaseTemp", addCaseTemp, 58, 5, 68, 7, false, "+100", colors.blue)
  56.   BUTTON.setTable("decCaseTemp", decCaseTemp, 82, 5, 92, 7, false, "-100", colors.blue)
  57.  
  58.   BUTTON.setTable("addFuelTemp", addFuelTemp, 58, 10, 68, 12, false, "+100", colors.blue)
  59.   BUTTON.setTable("decFuelTemp", decFuelTemp, 82, 10, 92, 12, false, "-100", colors.blue)
  60.  
  61.   BUTTON.setTable("addMaxBuffer", addMaxBuffer, 58, 15, 68, 17, false, "+10", colors.blue)
  62.   BUTTON.setTable("decMaxBuffer", decMaxBuffer, 82, 15, 92, 17, false, "-10", colors.blue)
  63.  
  64.   BUTTON.setTable("addMinBuffer", addMinBuffer, 58, 20, 68, 22, false, "+10", colors.blue)
  65.   BUTTON.setTable("decMinBuffer", decMinBuffer, 82, 20, 92, 22, false, "-10", colors.blue)
  66. end
  67.  
  68. function printInfos(infoName)
  69.   local info = infos[infoName]
  70.   if infoName == "online" then
  71.     gpu.set(info.x, info.y, info.title)
  72.     if reactorRunning then
  73.       gpu.setForeground(colors.green)
  74.       gpu.set(info.x + string.len(info.title), info.y, reactor.stats[infoName] .. spaceout(" ", 10))
  75.     else
  76.       gpu.setForeground(colors.red)
  77.       gpu.set(info.x + string.len(info.title), info.y, reactor.stats[infoName] .. spaceout(" ", 10))
  78.     end
  79.     gpu.setForeground(colors.white)
  80.   else
  81.     gpu.set(info.x, info.y, info.title .. reactor.stats[infoName] .. info.unit .. spaceout(" ", 10))
  82.   end
  83. end
  84.  
  85. function printBorders(sectionName)
  86.   local section = sections[sectionName]
  87.   gpu.setBackground(colors.grey)
  88.   gpu.fill(section.x, section.y, section.w, 1, " ")
  89.   gpu.fill(section.x, section.y, 1, section.h, " ")
  90.   gpu.fill(section.x, section.y + section.h, section.w, 1, " ")
  91.   gpu.fill(section.x + section.w, section.y, 1, section.h + 1, " ")
  92.  
  93.   gpu.setBackground(colors.black)
  94.   gpu.set(section.x + 1, section.y, section.title)
  95. end
  96.  
  97. function printEnergyBar()
  98.   gpu.setBackground(colors.green)
  99.   gpu.fill(energy.x, energy.y, energy.w, energy.h, " ")
  100.   gpu.setBackground(colors.black)
  101. end
  102.  
  103. function printEnergyBarOverlay(bar)
  104.   gpu.setBackground(colors.lightGrey)
  105.   gpu.fill(bar.x, bar.y, bar.w, bar.h, " ")
  106.   gpu.setBackground(colors.black)
  107. end
  108.  
  109. function printControlStatics()
  110.   gpu.set(43, 6, "Max Case Temp")
  111.   gpu.set(43, 11, "Max Fuel Temp")
  112.   gpu.set(43, 16, "   Max Buffer")
  113.   gpu.set(43, 21, "   Min Buffer")
  114. end
  115.  
  116. function printControlInfos()
  117.   gpu.set(73, 6, spaceout(maxCaseTemp, 4) .. maxCaseTemp .. " C")
  118.   gpu.set(73, 11, spaceout(maxFuelTemp, 4) .. maxFuelTemp .. " C")
  119.   gpu.set(72, 16, spaceout (maxBuffer, 4) .. maxBuffer .. " %")
  120.   gpu.set(72, 21, spaceout (minBuffer, 4) ..  minBuffer .. " %")
  121.  
  122.   gpu.set(58, 29, "Auto temp control:   ")
  123.   if autoTempControl then
  124.     gpu.setForeground(colors.green)
  125.     gpu.set(79, 29, "Enabled" .. spaceout(" ", 4))
  126.   else
  127.     gpu.setForeground(colors.red)
  128.     gpu.set(79, 29, "Disabled" .. spaceout(" ", 4))
  129.   end
  130.   gpu.setForeground(colors.white)
  131.  
  132.   gpu.set(58, 31, "Stop at core temp:   " .. maxCaseTemp .. " C" .. spaceout(" ", 4))
  133.   gpu.set(58, 33, "Stop at fuel temp:   " .. maxFuelTemp .. " C" .. spaceout(" ", 4))
  134.  
  135.   gpu.set(58, 35, "Auto buffer control: ")
  136.   if autoBufferControl then
  137.     gpu.setForeground(colors.green)
  138.     gpu.set(79, 35, "Enabled" .. spaceout(" ", 4))
  139.   else
  140.     gpu.setForeground(colors.red)
  141.     gpu.set(79, 35, "Disabled" .. spaceout(" ", 4))
  142.   end
  143.   gpu.setForeground(colors.white)
  144.  
  145.   gpu.set(58, 37, "Stop if buffer:      " .. maxBuffer .. " %" .. spaceout(" ", 4))
  146.   gpu.set(58, 39, "Start if buffer:     " .. minBuffer .. " %" .. spaceout(" ", 4))
  147. end
  148.  
  149. function toggleReactor(name)
  150.   if not reactorRunning then
  151.     startReactor()
  152.   else
  153.     stopReactor()
  154.   end
  155. end
  156.  
  157. function startReactor()
  158.   BUTTON.setActive("reactor", true)
  159.   reactor.setActive(true)
  160. end
  161.  
  162. function stopReactor()
  163.   BUTTON.setActive("reactor", false)
  164.   reactor.setActive(false)
  165. end
  166.  
  167. function toggleAutoBuffer(name)
  168.   local status = BUTTON.getActive(name)
  169.   BUTTON.toggleButton(name)
  170.   if not status then
  171.     autoBufferControl = true
  172.   else
  173.     autoBufferControl = false
  174.   end
  175.   writeToFile()
  176.   printControlInfos()
  177. end
  178.  
  179. function toggleAutoTemp(name)
  180.   local status = BUTTON.getActive(name)
  181.   BUTTON.toggleButton(name)
  182.   if not status then
  183.     autoTempControl = true
  184.   else
  185.     autoTempControl = false
  186.   end
  187.   writeToFile()
  188.   printControlInfos()
  189. end
  190.  
  191. function quitProgram()
  192.   programRunning = false
  193. end
  194.  
  195. function addCaseTemp()
  196.   modifyCaseTemp("add")
  197. end
  198.  
  199. function decCaseTemp()
  200.   modifyCaseTemp("dec")
  201. end
  202.  
  203. function addFuelTemp()
  204.   modifyFuelTemp("add")
  205. end
  206.  
  207. function decFuelTemp()
  208.   modifyFuelTemp("dec")
  209. end
  210.  
  211. function addMinBuffer()
  212.   modifyMinBuffer("add")
  213. end
  214.  
  215. function decMinBuffer()
  216.   modifyMinBuffer("dec")
  217. end
  218.  
  219. function addMaxBuffer()
  220.   modifyMaxBuffer("add")
  221. end
  222.  
  223. function decMaxBuffer()
  224.   modifyMaxBuffer("dec")
  225. end
  226.  
  227. function modifyCaseTemp(func)
  228.   if func == "add" then
  229.     maxCaseTemp = maxCaseTemp + 100
  230.     if maxCaseTemp >= 2000 then
  231.       maxCaseTemp = 2000
  232.     end
  233.   else
  234.     maxCaseTemp = maxCaseTemp - 100
  235.     if maxCaseTemp <= 0 then
  236.       maxCaseTemp = 0
  237.     end
  238.   end
  239.   writeToFile()
  240.   printControlInfos()
  241. end
  242.  
  243. function modifyFuelTemp(func)
  244.   if func == "add" then
  245.     maxFuelTemp = maxFuelTemp + 100
  246.     if maxFuelTemp >= 2000 then
  247.       maxFuelTemp = 2000
  248.     end
  249.   else
  250.     maxFuelTemp = maxFuelTemp - 100
  251.     if maxFuelTemp <= 0 then
  252.       maxFuelTemp = 0
  253.     end
  254.   end
  255.   writeToFile()
  256.   printControlInfos()
  257. end
  258.  
  259. function modifyMinBuffer(func)
  260.   if func == "add" then
  261.     minBuffer = minBuffer + 10
  262.     if minBuffer >= 100 then
  263.       minBuffer = 100
  264.     end
  265.   else
  266.     minBuffer = minBuffer - 10
  267.     if minBuffer <= 0 then
  268.       minBuffer = 0
  269.     end
  270.   end
  271.   writeToFile()
  272.   printControlInfos()
  273. end
  274.  
  275. function modifyMaxBuffer(func)
  276.   if func == "add" then
  277.     maxBuffer = maxBuffer + 10
  278.     if maxBuffer >= 100 then
  279.       maxBuffer = 100
  280.     end
  281.   else
  282.     maxBuffer = maxBuffer - 10
  283.     if maxBuffer <= 0 then
  284.       maxBuffer = 0
  285.     end
  286.   end
  287.   writeToFile()
  288.   printControlInfos()
  289. end
  290.  
  291. function isBufferFull()
  292.   return reactor.stats["stored"] == 10000000
  293. end
  294.  
  295. function isMaxBuf()
  296.   if not autoBufferControl then
  297.     return false
  298.   end
  299.   local maxBuf = maxBuffer * 100000
  300.   return maxBuf <= reactor.stats["stored"]
  301. end
  302.  
  303. function isMinBuf()
  304.   if not autoBufferControl then
  305.     return true
  306.   end
  307.   local minBuf = minBuffer * 100000
  308.   return minBuf >= reactor.stats["stored"]
  309. end
  310.  
  311. function isMaxTemp()
  312.   if not autoTempControl then
  313.     return false
  314.   end
  315.   return maxCaseTemp < reactor.stats["casetemp"] or maxFuelTemp < reactor.stats["fueltemp"]
  316. end
  317.  
  318. function setActiveButtons()
  319.   BUTTON.setActive("autoTemp", autoTempControl)
  320.   BUTTON.setActive("autoBuffer", autoBufferControl)
  321. end
  322.  
  323. function getInfoFromReactor()
  324.   local online = ""
  325.   if reactor.getActive() then
  326.     online = "Running"
  327.   else
  328.     online = "Stopped"
  329.   end
  330.   reactor.stats["running"] = reactor.getActive()
  331.   reactor.stats["online"] = online
  332.   reactor.stats["produce"] = toint(math.ceil(reactor.getEnergyProducedLastTick()))
  333.   reactor.stats["stored"] = toint(reactor.getEnergyStored())
  334.   reactor.stats["casetemp"] = toint(reactor.getCasingTemperature())
  335.   reactor.stats["fueltemp"] = toint(reactor.getFuelTemperature())
  336.   reactor.stats["fuelamount"] = reactor.getFuelAmount()
  337. end
  338.  
  339. function doCycle()
  340.   if reactorRunning ~= reactor.stats["running"] then
  341.     reactorRunning = reactor.stats["running"]
  342.     printInfos("online")
  343.   end
  344.  
  345.   if currentProduce ~= reactor.stats["produce"] then
  346.     currentProduce = reactor.stats["produce"]
  347.     printInfos("produce")
  348.   end
  349.  
  350.   if currentCasetemp ~= reactor.stats["casetemp"] then
  351.     currentCasetemp = reactor.stats["casetemp"]
  352.     printInfos("casetemp")
  353.   end
  354.  
  355.   if currentFueltemp ~= reactor.stats["fueltemp"] then
  356.     currentFueltemp = reactor.stats["fueltemp"]
  357.     printInfos("fueltemp")
  358.   end
  359.  
  360.   if currentFuelAmount ~= reactor.stats["fuelamount"] then
  361.     currentFuelAmount = reactor.stats["fuelamount"]
  362.     printInfos("fuelamount")
  363.   end
  364.  
  365.   if currentStored ~= reactor.stats["stored"] then
  366.     currentStored = reactor.stats["stored"]
  367.     printInfos("stored")
  368.     local height = math.ceil(energy.h - energy.h * (currentStored/10000000))
  369.     local bar = { x = energy.x, y = energy.y, w = energy.w, h = height }
  370.     printEnergyBar()
  371.     printEnergyBarOverlay(bar)
  372.   end
  373.  
  374.   if isBufferFull() then
  375.     stopReactor()
  376.   end
  377.  
  378.   if autoTempControl then
  379.     if reactorRunning then
  380.       if isMaxTemp() then
  381.         stopReactor()
  382.       end
  383.     else
  384.       if not isMaxTemp() and isMinBuf() and not isBufferFull() then
  385.         startReactor()
  386.       end
  387.     end
  388.   end
  389.  
  390.   if autoBufferControl then
  391.     if reactorRunning then
  392.       if isMaxBuf() then
  393.         stopReactor()
  394.       end
  395.     else
  396.       if isMinBuf() and not isMaxTemp() and not isBufferFull() then
  397.         startReactor()
  398.       end
  399.     end
  400.   end
  401. end
  402.  
  403. function toint(input)
  404.   local str = tostring(input)
  405.   local num, dec = str:find('%.')
  406.   if num then
  407.     return tonumber(str:sub(1, num - 1))
  408.   else
  409.     return input
  410.   end
  411. end
  412.  
  413. function spaceout(str, length)
  414.   return string.rep(" ", length - string.len(str))
  415. end
  416.  
  417. function file_exists(name)
  418.   local file = io.open(name, "r")
  419.   if file ~= nil then io.close(file) return false else return true end
  420. end
  421.  
  422. function loadFromFile()
  423.   if file_exists("reactor.conf") then
  424.     writeToFile()
  425.   else
  426.     file = io.open("reactor.conf", "r")
  427.     maxCaseTemp = tonumber(file:read("*l"))
  428.     maxFuelTemp = tonumber(file:read("*l"))
  429.     minBuffer = tonumber(file:read("*l"))
  430.     maxBuffer = tonumber(file:read("*l"))
  431.     autoTempControl = tostring(file:read("*l")) == "true"
  432.     autoBufferControl = tostring(file:read("*l")) == "true"
  433.     file:close()
  434.   end
  435. end
  436.  
  437. function writeToFile()
  438.   file = io.open("reactor.conf","w")
  439.   file:write(maxCaseTemp, "\n")
  440.   file:write(maxFuelTemp, "\n")
  441.   file:write(minBuffer, "\n")
  442.   file:write(maxBuffer, "\n")
  443.   file:write(tostring(autoTempControl), "\n")
  444.   file:write(tostring(autoBufferControl), "\n")
  445.   file:close()
  446. end
  447.  
  448. function clearScreen()
  449.   local w, h = gpu.getResolution()
  450.   gpu.fill(1, 1, w, h, " ")
  451. end
  452.  
  453. function startup()
  454.   loadFromFile()
  455.   getInfoFromReactor()
  456.   BUTTON.clearTable()
  457.  
  458.   setSections()
  459.   setButtons()
  460.   setInfos()
  461.  
  462.   for name, data in pairs(sections) do
  463.     printBorders(name)
  464.   end
  465.  
  466.   for name, data in pairs(infos) do
  467.     printInfos(name)
  468.   end
  469.  
  470.   setActiveButtons()
  471.   BUTTON.screen()
  472.   printEnergyBar()
  473.   printEnergyBarOverlay(energy)
  474.   printControlInfos()
  475.   printControlStatics()
  476. end
  477.  
  478. gpu.setResolution(132, 42)
  479. clearScreen()
  480. startup()
  481.  
  482. event.listen("touch", BUTTON.checkxy)
  483.  
  484. while event.pull(0.1, "interrupted") == nil do
  485.   getInfoFromReactor()
  486.   doCycle()
  487.   local event, address, _, key, _ = event.pull(1)
  488.   if type(address) == "string" and component.isPrimary(address) then
  489.     if event == "key_down" and key == keyboard.keys.q then
  490.       reactor.setActive(false)
  491.       clearScreen()
  492.       os.exit()
  493.     end
  494.   end
  495.   if not programRunning then
  496.     reactor.setActive(false)
  497.     clearScreen()
  498.     os.exit()
  499.   end
  500. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement