Advertisement
krakaen

new ER OC functions

Mar 2nd, 2018
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.45 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.   currentRf = reactor.stats["stored"]
  232.  
  233.     differenceMinMax = maxPowerRod - minPowerRod
  234.  
  235.     local maxPower = (rfTotalMax/100) * maxPowerRod
  236.     local minPower = (rfTotalMax/100) * minPowerRod
  237.  
  238.     if currentRf >= maxPower then
  239.         currentRf = maxPower
  240.     end
  241.  
  242.     if currentRf <= minPower then
  243.         currentRf = minPower
  244.     end
  245.  
  246.     currentRf = toint(currentRf - (rfTotalMax/100) * minPowerRod)
  247.     local rfInBetween = (rfTotalMax/100) * differenceMinMax
  248.   local rodLevel = toint(math.ceil((currentRf/rfInBetween)*100))
  249.  
  250.   if versionType == "NEW" then
  251.     AdjustRodsLevel(rodLevel)
  252.   else
  253.     AdjustRodsLevelOLD(rodLevel)
  254.   end
  255. end
  256.  
  257. function AdjustRodsLevel(rodLevel)
  258.   for key,value in pairs(reactorRodsLevel) do
  259.     --reactorRodsLevel[key] = rodLevel
  260.     reactor.setControlRodLevel(key, rodLevel)
  261.   end
  262.   --reactor.setControlRodsLevels(reactorRodsLevel)
  263. end
  264.  
  265. function AdjustRodsLevelOLD(rodLevel)
  266.   reactor.setAllControlRodLevels(rodLevel)
  267. end
  268.  
  269. function printDebug()  
  270.   local maxLength = 132
  271.   local i = debug["print"]
  272.   local rodsvalues = ""
  273.  
  274.   rodsvalues = "[0]" .. reactorRodsLevel[0] .. "[1]" .. reactorRodsLevel[1] .. "[2]" .. reactorRodsLevel[2] .. "[Z]" .. reactor.stats["rods"]
  275.  
  276.   local debugInformations = "maxRF:" .. maxRF .. ", RodsLev:" .. rodsvalues .. ", curRodLev:" .. currentRodLevel .. ", curRf:" .. currentRf .. ", curRfT:" .. currentRfTick .. ", min-max:" .. minPowerRod .. "-" .. maxPowerRod
  277.   local spaces = string.rep(" ", maxLength - string.len(debugInformations))
  278.   gpu.set(i.x, i.y , i.title .. debugInformations .. spaces)
  279. end
  280.  
  281. function draw()
  282.   if maxRF < reactor.stats["tick"] then
  283.     maxRF = reactor.stats["tick"]
  284.   end
  285.  
  286.   if currentRfTick ~= reactor.stats["tick"] then
  287.     currentRfTick = reactor.stats["tick"]
  288.     local max = math.ceil(graphs["tick"].width * (currentRfTick/maxRF))
  289.     local currentRFTickObj = {x = graphs["tick"].x, y = graphs["tick"].y, width = max, height = graphs["tick"].height}
  290.     printInfos("tick")
  291.     printGraphs("tick")
  292.     printActiveGraphs(currentRFTickObj)
  293.   end
  294.  
  295.   if currentRF ~= reactor.stats["stored"] then
  296.     currentRF = reactor.stats["stored"]
  297.     local max = math.ceil(graphs["stored"].width * (currentRF/10000000))
  298.     local currentRFObj = {x = graphs["stored"].x, y = graphs["stored"].y, width = max, height = graphs["stored"].height}
  299.     printInfos("stored")
  300.     printGraphs("stored")
  301.     printActiveGraphs(currentRFObj)
  302.   end
  303.  
  304.   if currentRodLevel ~= reactor.stats["rods"] then
  305.     currentRodLevel = reactor.stats["rods"]
  306.     local max = math.ceil(graphs["rods"].width * (currentRodLevel/100))
  307.     local currentRodObj = {x = graphs["rods"].x, y = graphs["rods"].y, width = max, height = graphs["rods"].height}
  308.     printInfos("rods")
  309.     printGraphs("rods")
  310.     printActiveGraphs(currentRodObj)
  311.   end
  312.  
  313.   if currenFuel ~= reactor.stats["fuel"] then
  314.     currenFuel = reactor.stats["fuel"]
  315.     printInfos("fuel")
  316.   end
  317.   printControlInfos()
  318.   if DEBUG == true then
  319.     printDebug()
  320.   end
  321. end
  322.  
  323. function startup()
  324.   getInfoFromFile()
  325.   if versionType == "NEW" then
  326.     getInfoFromReactor()
  327.   else
  328.     getInfoFromReactorOLD()
  329.   end
  330.   setSections()
  331.   setGraphs()
  332.   setInfos()
  333.   setButtons()
  334.   if DEBUG == true then
  335.     debugInfos()
  336.     printDebug()
  337.   end
  338.  
  339.   for name, data in pairs(sections) do
  340.     printBorders(name)
  341.   end
  342.   for name, data in pairs(graphs) do
  343.     printGraphs(name)
  344.   end
  345.   for name, data in pairs(infos) do
  346.     printInfos(name)
  347.   end
  348.   printStaticControlText()
  349.  
  350.  
  351. end
  352.  
  353.  
  354. -- helpers
  355. function round(val, decimal)
  356.   if (decimal) then
  357.     return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  358.   else
  359.     return math.floor(val+0.5)
  360.   end
  361. end
  362.  
  363. function file_exists(name)
  364.    local f=io.open(name,"r")
  365.    if f~=nil then io.close(f) return false else return true end
  366. end
  367.  
  368. function getInfoFromFile()
  369.      if file_exists("reactor.txt") then
  370.         file = io.open("reactor.txt","w")
  371.     file:write("0", "\n")
  372.     file:write("100", "\n")
  373.     file:close()
  374.     else
  375.         file = io.open("reactor.txt","r")
  376.         minPowerRod = tonumber(file:read("*l"))
  377.         maxPowerRod = tonumber(file:read("*l"))
  378.     file:close()
  379.     end
  380. end
  381.  
  382. function setInfoToFile()
  383.   file = io.open("reactor.txt","w")
  384.   file:write(minPowerRod, "\n")
  385.   file:write(maxPowerRod, "\n")
  386.   file:flush()
  387.   file:close()
  388. end
  389.  
  390. function testVersion()
  391.   reactor.getEnergyStats()
  392. end
  393.  
  394. function setOldVersion()
  395.   versionType = "OLD"
  396. end
  397. -- starting
  398. xpcall(testVersion, setOldVersion)
  399. startup()
  400. API.screen()
  401.  
  402. event.listen("touch", API.checkxy)
  403.  
  404. while event.pull(0.1, "interrupted") == nil do
  405.   if versionType == "NEW" then
  406.     if reactor.mbIsConnected() == true and reactor.mbIsAssembled() == true then
  407.       getInfoFromReactor()
  408.     end
  409.   else
  410.     getInfoFromReactorOLD()
  411.   end
  412.   calculateAdjustRodsLevel()
  413.   draw()
  414.   local event, address, arg1, arg2, arg3 = event.pull(1)
  415.   if type(address) == "string" and component.isPrimary(address) then
  416.     if event == "key_down" and arg2 == keyboard.keys.q then
  417.       os.exit()
  418.     end
  419.   end
  420. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement