SHARE
TWEET

reactor.lua

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