Advertisement
Antwanr942

[Tony's Reactor Automation]

Jul 18th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.06 KB | None | 0 0
  1. API = require("buttonAPI")
  2. local filesystem = require("filesystem")
  3. local component = require("component")
  4. local keyboard = require("keyboard")
  5. local event = require("event")
  6. local gpu = component.gpu
  7. local reactor = component.br_reactor
  8.  
  9. local DEBUG = false
  10. local debugList = {}
  11. local debugVars = {}
  12.  
  13.  
  14. local colors = { blue = 0x4286F4, purple = 0xB673d6, red = 0xC14141, green = 0xDA841,
  15.   black = 0x000000, white = 0xFFFFFF, grey = 0x47494C, lightGrey = 0xBBBBBB, warnorange = 0xff6700, warnyellow = 0xffae42  }
  16. -- set size of the screen for lvl 3
  17. gpu.setResolution(160,38)
  18. gpu.setBackground(colors.black)
  19. gpu.fill(1, 1, 160, 38, " ")
  20.  
  21. local sections = {}
  22. local graphs = {}
  23. local infos = {}
  24.  
  25.  
  26. -- defninitions
  27. reactor["stats"] = {}
  28. local running = true
  29. local maxRF = 0
  30. local reactorRodsLevel = {}
  31. local currentRodLevel = 0
  32. local currentRf = 0
  33. local currentRfTick = 0
  34. local currenFuel = 0
  35. local active = "false"
  36. local fuelreactivity = 0
  37. local totalfuel = 0
  38. local totalwaste = 0
  39. local totalfuelandwaste = 0
  40. local casingtemperature = 0
  41. local fueltemperature = 0
  42.  
  43. local minPowerRod = 0
  44. local maxPowerRod = 100
  45.  
  46.  
  47. -- functions
  48.  
  49. function toint(n)
  50.     local s = tostring(n)
  51.     local i, j = s:find('%.')
  52.     if i then
  53.         return tonumber(s:sub(1, i-1))
  54.     else
  55.         return n
  56.     end
  57. end
  58.  
  59. function setSections()
  60.   sections["graph"] = { x = 2, y = 3, width = 78, height= 33, title = "  INFOS  "}
  61.   sections["controls"] = { x = 82, y = 3, width = 40, height = 33, title = "  CONTROLS  "}
  62.     sections["info"] = { x =  124, y = 3, width = 35, height= 33, title = "  STATISTICS  "}
  63.     sections["info2"] = { x =  127, y = 15, width = 29, height= 10, title = "  FUEL/WASTE  "}
  64.     sections["info3"] = { x =  127, y = 27, width = 29, height= 6, title = "  TEMPERATURES  "}
  65. end
  66.  
  67. function setGraphs()
  68.   graphs["tick"] = { x = 6, y = 6, width = 73, height= 8, title = "ENERGY LAST TICK"}
  69.   graphs["stored"] = { x = 6, y = 16, width = 73, height = 8, title = "ENERGY STORED"}
  70.   graphs["rods"] = { x = 6, y = 26, width = 73, height= 8, title = "CONTROL RODS INSERTION"}
  71. end
  72.  
  73. function setInfos()
  74.   infos["active"] = { x = 127, y = 5, width = 73, height= 1, title = "STATUS : ", unit = " "}
  75.   infos["tick"] = { x = 127, y = 7, width = 73, height= 1, title = "RF PER TICK : ", unit = " RF"}
  76.   infos["stored"] = { x = 127, y = 9, width = 73, height = 1, title = "ENERGY STORED : ", unit = " RF"}
  77.   infos["rods"] = { x = 127, y = 11, width = 73, height= 1, title = "CONTROL ROD INSERTION : ", unit = " %"}
  78.     infos["activity"] = { x = 127, y = 13, width = 73, height= 1, title = "CORE REACTIVITY : ", unit = " %"}
  79.    
  80.     infos["fuel"] = { x = 130, y = 17, width = 73, height= 1, title = "FUEL USAGE : ", unit = " Mb/t"}
  81.     infos["totalfuel"] = { x = 130, y = 19, width = 73, height= 1, title = "TOTAL FUEL : ", unit = " Mb"}
  82.     infos["totalwaste"] = { x = 130, y = 21, width = 73, height= 1, title = "TOTAL WASTE : ", unit = " Mb"}
  83.     infos["total"] = { x = 130, y = 23, width = 73, height= 1, title = "MAX : ", unit = " Mb"}
  84.  
  85.     infos["casingtemp"] = { x = 130, y = 29, width = 73, height= 1, title = "CASING : ", unit = " C°"}
  86.     infos["fueltemp"] = { x = 130, y = 31, width = 73, height= 1, title = "FUEL : ", unit = " C°"}
  87. end
  88.  
  89. function debugInfos()  
  90.   debug["print"] = { x = 1, y = 38, width = 73, height= 1, title = "DBG : "}
  91. end
  92.  
  93. function setButtons()
  94.   API.setTable("ON", powerOn, 86, 5, 99, 7,"ON", {on = colors.green, off = colors.green})
  95.   API.setTable("AZ-5", powerOff, 105, 5, 118, 7,"AZ-5", {on = colors.red, off = colors.red})
  96.  
  97.   API.setTable("lowerMinLimit", lowerMinLimit, 86, 15, 99, 17,"-10", {on = colors.warnyellow, off = colors.warnyellow})
  98.   API.setTable("lowerMaxLimit", lowerMaxLimit, 105, 15, 118, 17,"-10", {on = colors.warnyellow, off = colors.warnyellow})
  99.  
  100.   API.setTable("augmentMinLimit", augmentMinLimit, 86, 19, 99, 21,"+10", {on = colors.warnyellow, off = colors.warnyellow})
  101.   API.setTable("augmentMaxLimit", augmentMaxLimit, 105, 19, 118, 21,"+10", {on = colors.warnyellow, off = colors.warnyellow})
  102.  
  103.   API.setTable("EJECT WASTE", ejectWaste, 86, 23, 118, 25,"EJECT WASTE", {on = colors.warnorange, off = colors.warnorange})
  104. end
  105.  
  106. function printBorders(sectionName)
  107.   local s = sections[sectionName]
  108.  
  109.   -- set border
  110.   gpu.setBackground(colors.grey)
  111.   gpu.fill(s.x, s.y, s.width, 1, " ")
  112.   gpu.fill(s.x, s.y, 1, s.height, " ")
  113.   gpu.fill(s.x, s.y + s.height, s.width, 1, " ")
  114.   gpu.fill(s.x + s.width, s.y, 1, s.height + 1, " ")
  115.  
  116.   -- set title
  117.   gpu.setBackground(colors.black)
  118.   gpu.set(s.x + 2, s.y, s.title)
  119. end
  120.  
  121. function printGraphs(graphName)
  122.   local g = graphs[graphName]
  123.  
  124.   -- set graph
  125.   gpu.setBackground(colors.lightGrey)
  126.   gpu.fill(g.x, g.y, g.width, g.height, " ")
  127.  
  128.   -- set title
  129.   gpu.setBackground(colors.black)
  130.   gpu.set(g.x, g.y - 1, g.title)
  131. end
  132.  
  133. function printActiveGraphs(activeGraph)
  134.   local g = activeGraph
  135.  
  136.   -- set graph
  137.   gpu.setBackground(colors.green)
  138.   gpu.fill(g.x, g.y, g.width, g.height, " ")
  139.   gpu.setBackground(colors.black)
  140. end
  141.  
  142. function printActiveGraphStore(graphObj)
  143.     local g = graphObj
  144.    
  145.     if g.stored > 7500000 then
  146.     gpu.setBackground(colors.green)
  147.         gpu.fill(g.x, g.y, g.width, g.height, " ")
  148.         gpu.setBackground(colors.black)
  149.     elseif g.stored > 5000000 then
  150.         gpu.setBackground(colors.warnyellow)
  151.         gpu.fill(g.x, g.y, g.width, g.height, " ")
  152.         gpu.setBackground(colors.black)
  153.     elseif g.stored > 2500000 then
  154.         gpu.setBackground(colors.warnorange)
  155.         gpu.fill(g.x, g.y, g.width, g.height, " ")
  156.         gpu.setBackground(colors.black)
  157.     else
  158.         gpu.setBackground(colors.red)
  159.         gpu.fill(g.x, g.y, g.width, g.height, " ")
  160.         gpu.setBackground(colors.black)
  161.     end
  162. end
  163.  
  164. function printStaticControlText()
  165.   gpu.setForeground(colors.white)
  166.   gpu.set(89,12, "MIN")
  167.   gpu.setForeground(colors.white)
  168.   gpu.set(108,12, "MAX")
  169.   gpu.setForeground(colors.white)
  170.   gpu.set(97,10, "AUTO-CONTROL")
  171.   gpu.set(101.5,13, "--")
  172. end
  173.  
  174. function printControlInfos()
  175.   gpu.setForeground(colors.white)
  176.   gpu.set(94,12, minPowerRod .. "% ")
  177.   gpu.setForeground(colors.white)
  178.   gpu.set(113,12, maxPowerRod .. "% ")
  179.   gpu.setForeground(colors.white)
  180. end
  181.  
  182. function printInfos(infoName)
  183.   local maxLength = 15
  184.   local i = infos[infoName]
  185.   local spaces = string.rep(" ", maxLength - string.len(reactor.stats[infoName] .. i.unit))
  186.   --gpu.set(i.x, i.y , i.title .. reactor.stats[infoName] .. i.unit .. spaces)
  187.   gpu.set(i.x, i.y , i.title .. reactor.stats[infoName] .. i.unit)
  188. end
  189.  
  190. function printInfos2()
  191.     local i = infos["active"]
  192.     if reactor.stats["active"] == "false" then
  193.       gpu.set(i.x, i.y , i.title .. "Offline")
  194.     else
  195.       gpu.set(i.x, i.y , i.title .. "Online")
  196.     end
  197. end
  198.    
  199.  
  200. function getInfoFromReactor()
  201.   local reactorEnergyStats = reactor.getEnergyStats()
  202.   local reactorFuelStats = reactor.getFuelStats()
  203.     local reactorStatus = reactor.getActive()
  204.     local reactorReactivity = reactor.getFuelReactivity()
  205.     local reactorTotalFuel = reactor.getFuelAmount()
  206.     local reactorTotalWaste = reactor.getWasteAmount()
  207.     local reactorTotalWasteAndFuel = reactor.getFuelAmountMax()
  208.     local reactorCasingTemperature = reactor.getCasingTemperature()
  209.     local reactorFuelTemperature = reactor.getFuelTemperature()
  210.     reactorRodsLevel = reactor.getControlRodsLevels()
  211.  
  212.     currentRf = reactor.stats["stored"]
  213.  
  214.   reactor.stats["active"] = tostring(reactorStatus)
  215.   reactor.stats["tick"] = toint(math.ceil(reactorEnergyStats["energyProducedLastTick"]))
  216.   reactor.stats["stored"] = toint(reactorEnergyStats["energyStored"])
  217.   reactor.stats["rods"] = toint(reactorRodsLevel[0])
  218.     reactor.stats["fuel"] = round(reactorFuelStats["fuelConsumedLastTick"], 2)
  219.     reactor.stats["activity"] = toint(math.ceil(reactorReactivity))
  220.  
  221.     reactor.stats["totalfuel"] = reactorTotalFuel
  222.     reactor.stats["totalwaste"] = reactorTotalWaste
  223.     reactor.stats["total"] = reactorTotalWasteAndFuel
  224.  
  225.     reactor.stats["casingtemp"] = toint(math.ceil(reactorCasingTemperature))
  226.     reactor.stats["fueltemp"] = toint(math.ceil(reactorFuelTemperature))
  227. end
  228.  
  229. function augmentMinLimit()
  230.   modifyRods("min", 10)
  231. end
  232.  
  233. function lowerMinLimit()
  234.   modifyRods("min", -10)
  235. end
  236.  
  237. function augmentMaxLimit()
  238.   modifyRods("max", 10)
  239. end
  240.  
  241. function lowerMaxLimit()
  242.   modifyRods("max", -10)
  243. end
  244.  
  245. function powerOn()
  246.   reactor.setActive(true)
  247. end
  248.  
  249. function powerOff()
  250.   reactor.setActive(false)
  251. end
  252.  
  253. function ejectWaste()
  254.   reactor.doEjectWaste()
  255. end
  256.  
  257. function modifyRods(limit, number)
  258.     local tempLevel = 0
  259.  
  260.     if limit == "min" then
  261.         tempLevel = minPowerRod + number
  262.         if tempLevel <= 0 then
  263.                 minPowerRod = 0
  264.         end
  265.  
  266.         if tempLevel >= maxPowerRod then
  267.                 minPowerRod = maxPowerRod -10
  268.         end
  269.  
  270.         if tempLevel < maxPowerRod and tempLevel > 0 then
  271.                 minPowerRod = tempLevel
  272.         end
  273.     else
  274.         tempLevel = maxPowerRod + number
  275.         if tempLevel <= minPowerRod then
  276.                 maxPowerRod = minPowerRod +10
  277.         end
  278.  
  279.         if tempLevel >= 100 then
  280.                 maxPowerRod = 100
  281.         end
  282.  
  283.         if tempLevel > minPowerRod and tempLevel < 100 then
  284.                 maxPowerRod = tempLevel
  285.         end
  286.     end
  287.  
  288.     setInfoToFile()
  289.     calculateAdjustRodsLevel()
  290. end
  291.  
  292. -- Calculate and adjusts the level of the rods
  293. function calculateAdjustRodsLevel()
  294.     local rfTotalMax = 10000000
  295.     currentRf = reactor.stats["stored"]
  296.  
  297.     differenceMinMax = 100
  298.  
  299.     local maxPower = (rfTotalMax/100) * 100
  300.     local minPower = (rfTotalMax/100) * 0
  301.  
  302.     if currentRf >= maxPower then
  303.             currentRf = maxPower
  304.     end
  305.  
  306.     if currentRf <= minPower then
  307.             currentRf = minPower
  308.     end
  309.  
  310.     currentRf = toint(currentRf - (rfTotalMax/100) * minPowerRod)
  311.     local rfInBetween = (rfTotalMax/100) * differenceMinMax
  312.     local rodLevel = toint(math.ceil((currentRf/rfInBetween)*100))
  313.  
  314.     AdjustRodsLevel(rodLevel)
  315. end
  316.  
  317. function AdjustRodsLevel(rodLevel)
  318.     for key,value in pairs(reactorRodsLevel) do
  319.         reactor.setControlRodLevel(key, rodLevel)
  320.     end
  321. end
  322.  
  323. function printDebug()  
  324.   local maxLength = 132
  325.   local i = debug["print"]
  326.   local rodsvalues = ""
  327.  
  328.   rodsvalues = "[0]" .. reactorRodsLevel[0] .. "[1]" .. reactorRodsLevel[1] .. "[2]" .. reactorRodsLevel[2] .. "[Z]" .. reactor.stats["rods"]
  329.  
  330.   local debugInformations = "maxRF:" .. maxRF .. ", RodsLev:" .. rodsvalues .. ", curRodLev:" .. currentRodLevel .. ", curRf:" .. currentRf .. ", curRfT:" .. currentRfTick .. ", min-max:" .. minPowerRod .. "-" .. maxPowerRod
  331.   local spaces = string.rep(" ", maxLength - string.len(debugInformations))
  332.   gpu.set(i.x, i.y , i.title .. debugInformations .. spaces)
  333. end
  334.  
  335. function draw()
  336.   if maxRF < reactor.stats["tick"] then
  337.     maxRF = reactor.stats["tick"]
  338.   end
  339.  
  340.   if currentRfTick ~= reactor.stats["tick"] then
  341.     currentRfTick = reactor.stats["tick"]
  342.     local max = math.ceil(graphs["tick"].width * (currentRfTick/maxRF))
  343.     local currentRFTickObj = {x = graphs["tick"].x, y = graphs["tick"].y, width = max, height = graphs["tick"].height}
  344.     printInfos("tick")
  345.     printGraphs("tick")
  346.     printActiveGraphs(currentRFTickObj)
  347.   end
  348.  
  349.   if currentRF ~= reactor.stats["stored"] then
  350.     currentRF = reactor.stats["stored"]
  351.     local max = math.ceil(graphs["stored"].width * (currentRF/10000000))
  352.     local currentRFObj = {x = graphs["stored"].x, y = graphs["stored"].y, width = max, height = graphs["stored"].height, stored = currentRF}
  353.     printInfos("stored")
  354.     printGraphs("stored")
  355.     printActiveGraphStore(currentRFObj)
  356.   end
  357.  
  358.     if currentRodLevel ~= reactor.stats["rods"] then
  359.         currentRodLevel = reactor.stats["rods"]
  360.         local max = math.ceil(graphs["rods"].width * (currentRodLevel/100))
  361.         local currentRodObj = {x = graphs["rods"].x, y = graphs["rods"].y, width = max, height = graphs["rods"].height}
  362.         printInfos("rods")
  363.         printGraphs("rods")
  364.         printActiveGraphs(currentRodObj)
  365.   end
  366.  
  367.   if currenFuel ~= reactor.stats["fuel"] then
  368.     currenFuel = reactor.stats["fuel"]
  369.     printInfos("fuel")
  370.   end
  371.  
  372.   if active ~= reactor.stats["active"] then
  373.     active = reactor.stats["active"]
  374.       printInfos2("active")
  375.     end
  376.    
  377.     if fuelreactivity ~= reactor.stats["activity"] then
  378.         fuelreactivity = reactor.stats["activity"]
  379.         printInfos("activity")
  380.     end
  381.  
  382.     if totalfuel ~= reactor.stats["totalfuel"] then
  383.         totalfuel = reactor.stats["totalfuel"]
  384.         printInfos("totalfuel")
  385.     end
  386.  
  387.     if totalwaste ~= reactor.stats["totalwaste"] then
  388.         totalwaste = reactor.stats["totalwaste"]
  389.         printInfos("totalwaste")
  390.     end
  391.  
  392.     if casingtemperature ~= reactor.stats["casingtemp"] then
  393.         casingtemperature = reactor.stats["casingtemp"]
  394.         printInfos("casingtemp")
  395.     end
  396.  
  397.     if fueltemperature ~= reactor.stats["fueltemp"] then
  398.         fueltemperature = reactor.stats["fueltemp"]
  399.         printInfos("fueltemp")
  400.     end
  401.   printControlInfos()
  402.   if DEBUG == true then
  403.     printDebug()
  404.   end
  405. end
  406.  
  407. function startup()
  408.   getInfoFromFile()
  409.   getInfoFromReactor()
  410.   setSections()
  411.   setGraphs()
  412.   setInfos()
  413.   setButtons()
  414.   if DEBUG == true then
  415.     debugInfos()
  416.     printDebug()
  417.   end
  418.  
  419.   for name, data in pairs(sections) do
  420.     printBorders(name)
  421.   end
  422.   for name, data in pairs(graphs) do
  423.     printGraphs(name)
  424.   end
  425.   for name, data in pairs(infos) do
  426.     printInfos(name)
  427.   end
  428.   printInfos2("active")
  429.   printStaticControlText()
  430. end
  431.  
  432.  
  433. -- helpers
  434. function round(val, decimal)
  435.   if (decimal) then
  436.     return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  437.   else
  438.     return math.floor(val+0.5)
  439.   end
  440. end
  441.  
  442. function file_exists(name)
  443.   local f=io.open(name,"r")
  444.     if f~=nil then
  445.         io.close(f)
  446.         return false
  447.     else
  448.         return true
  449.     end
  450. end
  451.  
  452. function getInfoFromFile()
  453.     if file_exists("reactor.txt") then
  454.       file = io.open("reactor.txt","w")
  455.         file:write("0", "\n")
  456.         file:write("100", "\n")
  457.         file:close()
  458.     else
  459.       file = io.open("reactor.txt","r")
  460.       minPowerRod = tonumber(file:read("*l"))
  461.       maxPowerRod = tonumber(file:read("*l"))
  462.         file:close()
  463.     end
  464. end
  465.  
  466. function setInfoToFile()
  467.   file = io.open("reactor.txt","w")
  468.   file:write(minPowerRod, "\n")
  469.   file:write(maxPowerRod, "\n")
  470.   file:flush()
  471.   file:close()
  472. end
  473.  
  474. function testVersion()
  475.   reactor.getEnergyStats()
  476. end
  477.  
  478. -- starting
  479. xpcall(testVersion, setOldVersion)
  480. startup()
  481. API.screen()
  482.  
  483. event.listen("touch", API.checkxy)
  484.  
  485. while event.pull(0.1, "interrupted") == nil do
  486.   if reactor.mbIsConnected() == true and reactor.mbIsAssembled() == true then
  487.     getInfoFromReactor()
  488.   end
  489.   calculateAdjustRodsLevel()
  490.   draw()
  491.   local event, address, arg1, arg2, arg3 = event.pull(1)
  492.   if type(address) == "string" and component.isPrimary(address) then
  493.     if event == "key_down" and arg2 == keyboard.keys.q then
  494.       os.exit()
  495.     end
  496.   end
  497. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement