Advertisement
Guest User

reactor.lua

a guest
Apr 23rd, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.86 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.nc_fission_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.  
  153.   reactor.stats["tick"] = toint(math.ceil(reactorEnergyStats["energyProducedLastTick"]))
  154.   reactor.stats["stored"] = toint(reactorEnergyStats["energyStored"])
  155.   reactor.stats["fuel"] = round(reactorFuelStats["fuelConsumedLastTick"], 2)
  156.   currentRf = reactor.stats["stored"]
  157. end
  158.  
  159. function getInfoFromReactorOLD()
  160.   reactor.stats["tick"] = toint(math.floor(reactor.getFissionFuelPower()))
  161.   reactor.stats["stored"] = toint(reactor.getEnergyStored())
  162.   reactor.stats["fuel"] = tostring(reactor.getFissionFuelName())
  163.   currentRf = reactor.stats["stored"]
  164. end
  165.  
  166. function augmentMinLimit()
  167.   modifyRods("min", 10)
  168. end
  169.  
  170. function lowerMinLimit()
  171.   modifyRods("min", -10)
  172. end
  173.  
  174. function augmentMaxLimit()
  175.   modifyRods("max", 10)
  176. end
  177.  
  178. function lowerMaxLimit()
  179.   modifyRods("max", -10)
  180. end
  181.  
  182. function powerOn()
  183.   reactor.setActive(true)
  184. end
  185.  
  186. function powerOff()
  187.   reactor.setActive(false)
  188. end
  189.  
  190. function modifyRods(limit, number)
  191.     local tempLevel = 0
  192.  
  193.     if limit == "min" then
  194.         tempLevel = minPowerRod + number
  195.         if tempLevel <= 0 then
  196.             minPowerRod = 0
  197.         end
  198.  
  199.         if tempLevel >= maxPowerRod then
  200.             minPowerRod = maxPowerRod -10
  201.         end
  202.  
  203.         if tempLevel < maxPowerRod and tempLevel > 0 then
  204.             minPowerRod = tempLevel
  205.         end
  206.     else
  207.         tempLevel = maxPowerRod + number
  208.         if tempLevel <= minPowerRod then
  209.             maxPowerRod = minPowerRod +10
  210.         end
  211.  
  212.         if tempLevel >= 100 then
  213.             maxPowerRod = 100
  214.         end
  215.  
  216.         if tempLevel > minPowerRod and tempLevel < 100 then
  217.             maxPowerRod = tempLevel
  218.         end
  219.     end
  220.  
  221.   setInfoToFile()
  222.   calculateAdjustRodsLevel()
  223. end
  224.  
  225. -- Calculate and adjusts the level of the rods
  226. function calculateAdjustRodsLevel()
  227.     local rfTotalMax = 10000000
  228.   currentRf = reactor.stats["stored"]
  229.  
  230.     differenceMinMax = maxPowerRod - minPowerRod
  231.  
  232.     local maxPower = (rfTotalMax/100) * maxPowerRod
  233.     local minPower = (rfTotalMax/100) * minPowerRod
  234.  
  235.     if currentRf >= maxPower then
  236.         currentRf = maxPower
  237.     end
  238.  
  239.     if currentRf <= minPower then
  240.         currentRf = minPower
  241.     end
  242.  
  243.     currentRf = toint(currentRf - (rfTotalMax/100) * minPowerRod)
  244.     local rfInBetween = (rfTotalMax/100) * differenceMinMax
  245.   local rodLevel = toint(math.ceil((currentRf/rfInBetween)*100))
  246.  
  247.   if versionType == "NEW" then
  248.     AdjustRodsLevel(rodLevel)
  249.   else
  250.     AdjustRodsLevelOLD(rodLevel)
  251.   end
  252. end
  253.  
  254. function AdjustRodsLevel(rodLevel)
  255.   for key,value in pairs(reactorRodsLevel) do
  256.     --reactorRodsLevel[key] = rodLevel
  257.     reactor.setControlRodLevel(key, rodLevel)
  258.   end
  259.   --reactor.setControlRodsLevels(reactorRodsLevel)
  260. end
  261.  
  262. function AdjustRodsLevelOLD(rodLevel)
  263.   reactor.setAllControlRodLevels(rodLevel)
  264. end
  265.  
  266. function printDebug()  
  267.   local maxLength = 132
  268.   local i = debug["print"]
  269.   local rodsvalues = ""
  270.  
  271.   rodsvalues = "[0]" .. reactorRodsLevel[0] .. "[1]" .. reactorRodsLevel[1] .. "[2]" .. reactorRodsLevel[2] .. "[Z]" .. reactor.stats["rods"]
  272.  
  273.   local debugInformations = "maxRF:" .. maxRF .. ", RodsLev:" .. rodsvalues .. ", curRodLev:" .. currentRodLevel .. ", curRf:" .. currentRf .. ", curRfT:" .. currentRfTick .. ", min-max:" .. minPowerRod .. "-" .. maxPowerRod
  274.   local spaces = string.rep(" ", maxLength - string.len(debugInformations))
  275.   gpu.set(i.x, i.y , i.title .. debugInformations .. spaces)
  276. end
  277.  
  278. function draw()
  279.   if maxRF < reactor.stats["tick"] then
  280.     maxRF = reactor.stats["tick"]
  281.   end
  282.  
  283.   if currentRfTick ~= reactor.stats["tick"] then
  284.     currentRfTick = reactor.stats["tick"]
  285.     local max = math.ceil(graphs["tick"].width * (currentRfTick/maxRF))
  286.     local currentRFTickObj = {x = graphs["tick"].x, y = graphs["tick"].y, width = max, height = graphs["tick"].height}
  287.     printInfos("tick")
  288.     printGraphs("tick")
  289.     printActiveGraphs(currentRFTickObj)
  290.   end
  291.  
  292.   if currentRF ~= reactor.stats["stored"] then
  293.     currentRF = reactor.stats["stored"]
  294.     local max = math.ceil(graphs["stored"].width * (currentRF/10000000))
  295.     local currentRFObj = {x = graphs["stored"].x, y = graphs["stored"].y, width = max, height = graphs["stored"].height}
  296.     printInfos("stored")
  297.     printGraphs("stored")
  298.     printActiveGraphs(currentRFObj)
  299.   end
  300.  
  301.   if currentRodLevel ~= reactor.stats["rods"] then
  302.     currentRodLevel = reactor.stats["rods"]
  303.     local max = math.ceil(graphs["rods"].width * (currentRodLevel/100))
  304.     local currentRodObj = {x = graphs["rods"].x, y = graphs["rods"].y, width = max, height = graphs["rods"].height}
  305.     printInfos("rods")
  306.     printGraphs("rods")
  307.     printActiveGraphs(currentRodObj)
  308.   end
  309.  
  310.   if currenFuel ~= reactor.stats["fuel"] then
  311.     currenFuel = reactor.stats["fuel"]
  312.     printInfos("fuel")
  313.   end
  314.   printControlInfos()
  315.   if DEBUG == true then
  316.     printDebug()
  317.   end
  318. end
  319.  
  320. function startup()
  321.   getInfoFromFile()
  322.   if versionType == "NEW" then
  323.     getInfoFromReactor()
  324.   else
  325.     getInfoFromReactorOLD()
  326.   end
  327.   setSections()
  328.   setGraphs()
  329.   setInfos()
  330.   setButtons()
  331.   if DEBUG == true then
  332.     debugInfos()
  333.     printDebug()
  334.   end
  335.  
  336.   for name, data in pairs(sections) do
  337.     printBorders(name)
  338.   end
  339.   for name, data in pairs(graphs) do
  340.     printGraphs(name)
  341.   end
  342.   for name, data in pairs(infos) do
  343.     printInfos(name)
  344.   end
  345.   printStaticControlText()
  346.  
  347.  
  348. end
  349.  
  350.  
  351. -- helpers
  352. function round(val, decimal)
  353.   if (decimal) then
  354.     return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  355.   else
  356.     return math.floor(val+0.5)
  357.   end
  358. end
  359.  
  360. function file_exists(name)
  361.    local f=io.open(name,"r")
  362.    if f~=nil then io.close(f) return false else return true end
  363. end
  364.  
  365. function getInfoFromFile()
  366.      if file_exists("reactor.txt") then
  367.         file = io.open("reactor.txt","w")
  368.     file:write("0", "\n")
  369.     file:write("100", "\n")
  370.     file:close()
  371.     else
  372.         file = io.open("reactor.txt","r")
  373.         minPowerRod = tonumber(file:read("*l"))
  374.         maxPowerRod = tonumber(file:read("*l"))
  375.     file:close()
  376.     end
  377. end
  378.  
  379. function setInfoToFile()
  380.   file = io.open("reactor.txt","w")
  381.   file:write(minPowerRod, "\n")
  382.   file:write(maxPowerRod, "\n")
  383.   file:flush()
  384.   file:close()
  385. end
  386.  
  387. function testVersion()
  388.   reactor.getEnergyStats()
  389. end
  390.  
  391. function setOldVersion()
  392.   versionType = "OLD"
  393. end
  394. -- starting
  395. xpcall(testVersion, setOldVersion)
  396. startup()
  397. API.screen()
  398.  
  399. event.listen("touch", API.checkxy)
  400.  
  401. while event.pull(0.1, "interrupted") == nil do
  402.   if versionType == "NEW" then
  403.     if reactor.mbIsConnected() == true and reactor.mbIsAssembled() == true then
  404.       getInfoFromReactor()
  405.     end
  406.   else
  407.     getInfoFromReactorOLD()
  408.   end
  409.   calculateAdjustRodsLevel()
  410.   draw()
  411.   local event, address, arg1, arg2, arg3 = event.pull(1)
  412.   if type(address) == "string" and component.isPrimary(address) then
  413.     if event == "key_down" and arg2 == keyboard.keys.q then
  414.       os.exit()
  415.     end
  416.   end
  417. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement