Advertisement
Florian86

Reactor_OpenComputers

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