SHARE
TWEET

reactor.lua

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