Advertisement
Guest User

reactor.lua

a guest
Apr 24th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.11 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.  
  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.  
  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 = 39, height= 33, title = "  INFO  "}
  60.   sections["controls"] = { x = 47, y = 3, width = 40, height = 20, title = "  CONTROLS  "}
  61.   sections["info"] = { x = 47, y = 25, width = 40, height= 11 , title = "  STATS  "}
  62. end
  63.  
  64. function setGraphs()
  65.   graphs["tick"] = { x = 8, y = 5, width = 10, height= 28, title = "FUEL"}
  66.   graphs["stored"] = { x = 20, y = 5, width = 10, height = 28, title = "ENERGY"}
  67.   graphs["rods"] = { x = 32, y = 5, width = 10, height= 28, title = "HEAT"}
  68. end
  69.  
  70. function setInfos()
  71.    infos["tick"] = { x = 49, y = 27, width = 73, height= 1, title = "RF PER TICK : ", unit = " RF"}
  72.    infos["heatlevel"] = { x = 49, y = 34, width = 73, height = 1, title = "HEAT: ", unit = " HU/t"}
  73.    infos["stored"] = { x = 49, y = 28, width = 73, height = 1, title = "ENERGY STORED : ", unit = " RF"}
  74.    infos["efficiency"] = { x = 49, y = 29, width = 73, height = 1, title = "EFFICIENCY : ", unit = "%"}
  75.    infos["fuelname"] = { x = 49, y = 30, width = 73, height = 1, title = "FUEL NAME: ", unit = " "}
  76.    infos["fueldecaytime"] = { x = 49, y = 31, width = 73, height = 1, title = "FUEL USED: ", unit = " "}
  77.    infos["fueltotaltime"] = { x = 49, y = 32, width = 73, height = 1, title = "FUEL TOTAL: ", unit = " "}
  78.    infos["heat"] = { x = 49, y = 33, width = 73, height = 1, title = "HEAT: ", unit = " HU/t"}
  79.  end
  80.  
  81. function debugInfos()  
  82.   debug["print"] = { x = 1, y = 38, width = 73, height= 1, title = "DBG : "}
  83. end
  84.  
  85. function setButtons()
  86.   API.setTable("ON", powerOn, 50, 5, 65, 7,"ON", {on = colors.green, off = colors.green})
  87.   API.setTable("OFF", powerOff, 68, 5, 84, 7,"OFF", {on = colors.red, off = colors.red})
  88.  
  89.   API.setTable("lowerMinLimit", lowerMinLimit, 50, 15, 65, 17,"-10", {on = colors.blue, off = colors.blue})
  90.   API.setTable("lowerMaxLimit", lowerMaxLimit, 68, 15, 84, 17,"-10", {on = colors.purple, off = colors.purple})
  91.  
  92.   API.setTable("augmentMinLimit", augmentMinLimit, 50, 19, 65, 21,"+10", {on = colors.blue, off = colors.blue})
  93.   API.setTable("augmentMaxLimit", augmentMaxLimit, 68, 19, 84, 21,"+10", {on = colors.purple, off = colors.purple})
  94. end
  95.  
  96.  
  97.  
  98. function printBorders(sectionName)
  99.   local s = sections[sectionName]
  100.  
  101.   -- set border
  102.   gpu.setBackground(colors.blue)
  103.   gpu.fill(s.x, s.y, s.width, 1, " ")
  104.   gpu.fill(s.x, s.y, 1, s.height, " ")
  105.   gpu.fill(s.x, s.y + s.height, s.width, 1, " ")
  106.   gpu.fill(s.x + s.width, s.y, 1, s.height + 1, " ")
  107.  
  108.   -- set title
  109.   gpu.setBackground(colors.black)
  110.   gpu.set(s.x + 2, s.y, s.title)
  111. end
  112.  
  113. function printGraphs(graphName)
  114.   local g = graphs[graphName]
  115.  
  116.   -- set graph
  117.   gpu.setBackground(colors.green)
  118.   gpu.fill(g.x, g.y, g.width, g.height, " ")
  119.  
  120.   -- set title
  121.   gpu.setBackground(colors.black)
  122.   gpu.set(g.x, g.y +g.height + 1, g.title)
  123. end
  124.  
  125. function printActiveGraphs(activeGraph)
  126.   local g = activeGraph
  127.  
  128.   -- set graph
  129.   gpu.setBackground(colors.lightGrey)
  130.   gpu.fill(g.x, g.y, g.width, g.height, " ")
  131.   gpu.setBackground(colors.black)
  132. end
  133.  
  134. function printStaticControlText()
  135.   gpu.setForeground(colors.blue)
  136.   gpu.set(56,12, "MIN")
  137.   gpu.setForeground(colors.purple)
  138.   gpu.set(74,12, "MAX")
  139.   gpu.setForeground(colors.white)
  140.   gpu.set(61,10, "AUTO-CONTROL")
  141.   gpu.set(66,13, "--")
  142. end
  143.  
  144. function printControlInfos()
  145.   gpu.setForeground(colors.blue)
  146.   gpu.set(97,13, minPowerRod .. "% ")
  147.   gpu.setForeground(colors.purple)
  148.   gpu.set(116,13, maxPowerRod .. "% ")
  149.   gpu.setForeground(colors.white)
  150. end
  151.  
  152. function printInfos(infoName)
  153.   local maxLength = 15
  154.   local i = infos[infoName]
  155.   local spaces = string.rep(" ", maxLength - string.len(reactor.stats[infoName] .. i.unit))
  156.   gpu.set(i.x, i.y , i.title .. reactor.stats[infoName] .. i.unit .. spaces)
  157. end
  158.  
  159. function getInfoFromReactor()
  160.   local reactorEnergyStats = reactor.getEnergyStats()
  161.   currentRf = reactor.stats["stored"]
  162. end
  163.  
  164. function getInfoFromReactorOLD()
  165.      reactor.stats["tick"] = toint(math.floor(reactor.getReactorProcessPower()))
  166.  
  167.      --Fuel Information
  168.      reactor.stats["fuelname"] = tostring(reactor.getFissionFuelName())
  169.      reactor.stats["efficiency"] = toint(reactor.getEfficiency())
  170.      reactor.stats["fueldecaytime"] = toint(reactor.getCurrentProcessTime())
  171.      reactor.stats["fueltimeused"] = reactor.getReactorProcessTime()
  172.      reactor.stats["fueltotaltime"] = reactor.getFissionFuelTime()
  173.  
  174.      --Energy Information
  175.      reactor.stats["stored"] = toint(reactor.getEnergyStored())
  176.      reactor.stats["maxenergy"] = toint(reactor.getMaxEnergyStored())
  177.  
  178.      --Heat Information
  179.      reactor.stats["heat"] = toint(reactor.getReactorProcessHeat())
  180.      reactor.stats["heatlevel"] = toint(reactor.getHeatLevel())
  181.  
  182.      --Graph Information
  183.      CurrentFuelTime = math.floor(reactor.stats["fueldecaytime"])
  184.      TotalFuelTime   = math.floor(reactor.stats["fueltotaltime"])
  185.  
  186.      currentRf = reactor.stats["stored"]
  187.      MaximumStorage = reactor.stats["maxenergy"]
  188. end
  189.  
  190. function augmentMinLimit()
  191.   modifyRods("min", 10)
  192. end
  193.  
  194. function lowerMinLimit()
  195.   reactor.activate()
  196. end
  197.  
  198. function augmentMaxLimit()
  199.   modifyRods("max", 10)
  200. end
  201.  
  202. function lowerMaxLimit()
  203.   modifyRods("max", -10)
  204. end
  205.  
  206. function powerOn()
  207.   reactor.activate()
  208. end
  209.  
  210. function powerOff()
  211.   reactor.deactivate()
  212. end
  213.  
  214. function modifyRods(limit, number)
  215.     local tempLevel = 0
  216.  
  217.     if limit == "min" then
  218.         tempLevel = minPowerRod + number
  219.         if tempLevel <= 0 then
  220.             minPowerRod = 0
  221.         end
  222.  
  223.         if tempLevel >= maxPowerRod then
  224.             minPowerRod = maxPowerRod -10
  225.         end
  226.  
  227.         if tempLevel < maxPowerRod and tempLevel > 0 then
  228.             minPowerRod = tempLevel
  229.         end
  230.     else
  231.         tempLevel = maxPowerRod + number
  232.         if tempLevel <= minPowerRod then
  233.             maxPowerRod = minPowerRod +10
  234.         end
  235.  
  236.         if tempLevel >= 100 then
  237.             maxPowerRod = 100
  238.         end
  239.  
  240.         if tempLevel > minPowerRod and tempLevel < 100 then
  241.             maxPowerRod = tempLevel
  242.         end
  243.     end
  244.  
  245.   setInfoToFile()
  246.   calculateAdjustRodsLevel()
  247. end
  248.  
  249. -- Calculate and adjusts the level of the rods
  250. --[[function calculateAdjustRodsLevel()
  251.     local rfTotalMax = 10000000
  252.   currentRf = reactor.stats["stored"]
  253.  
  254.     differenceMinMax = maxPowerRod - minPowerRod
  255.  
  256.     local maxPower = (rfTotalMax/100) * maxPowerRod
  257.     local minPower = (rfTotalMax/100) * minPowerRod
  258.  
  259.     if currentRf >= maxPower then
  260.         currentRf = maxPower
  261.     end
  262.  
  263.     if currentRf <= minPower then
  264.         currentRf = minPower
  265.     end
  266.  
  267.     currentRf = toint(currentRf - (rfTotalMax/100) * minPowerRod)
  268.     local rfInBetween = (rfTotalMax/100) * differenceMinMax
  269.   local rodLevel = toint(math.ceil((currentRf/rfInBetween)*100))
  270.  
  271.   if versionType == "NEW" then
  272.     AdjustRodsLevel(rodLevel)
  273.   else
  274.     AdjustRodsLevelOLD(rodLevel)
  275.   end
  276. end
  277.  
  278. function AdjustRodsLevel(rodLevel)
  279.   for key,value in pairs(reactorRodsLevel) do
  280.     --reactorRodsLevel[key] = rodLevel
  281.     reactor.setControlRodLevel(key, rodLevel)
  282.   end
  283.   --reactor.setControlRodsLevels(reactorRodsLevel)
  284. end
  285.  
  286. function AdjustRodsLevelOLD(rodLevel)
  287.   reactor.setAllControlRodLevels(rodLevel)
  288. end]]--
  289.  
  290. function printDebug()  
  291.   local maxLength = 132
  292.   local i = debug["print"]
  293.   local rodsvalues = ""
  294.  
  295.   rodsvalues = "[0]" .. reactorRodsLevel[0] .. "[1]" .. reactorRodsLevel[1] .. "[2]" .. reactorRodsLevel[2] .. "[Z]" .. reactor.stats["rods"]
  296.  
  297.   local debugInformations = "maxRF:" .. maxRF .. ", RodsLev:" .. rodsvalues .. ", curRodLev:" .. currentRodLevel .. ", curRf:" .. currentRf .. ", curRfT:" .. currentRfTick .. ", min-max:" .. minPowerRod .. "-" .. maxPowerRod
  298.   local spaces = string.rep(" ", maxLength - string.len(debugInformations))
  299.   gpu.set(i.x, i.y , i.title .. debugInformations .. spaces)
  300. end
  301.  
  302. function draw()
  303.  
  304.   if CurrentFuelTime ~= reactor.stats["fueldecaytime"] then
  305.     currentRfTick = reactor.stats["fueldecaytime"]-1
  306.     local max = math.floor(graphs["tick"].height - math.floor(graphs["tick"].height * (currentRfTick/TotalFuelTime)))
  307.     local currentRFTickObj = {x = graphs["tick"].x, y = graphs["tick"].y, width = graphs["tick"].width, height = max -1}
  308.     printInfos("tick")
  309.     printGraphs("tick")
  310.     printActiveGraphs(currentRFTickObj)
  311.    end
  312.  
  313.   if currentRF ~= reactor.stats["stored"] then
  314.     currentRF = reactor.stats["stored"] - 1
  315.     local max = math.floor(graphs["stored"].height - math.floor(graphs["stored"].height * (currentRF/MaximumStorage)))
  316.     local currentRFObj = {x = graphs["stored"].x, y = graphs["stored"].y , width = graphs["stored"].width, height = max-1}
  317.     printInfos("stored")
  318.     printInfos("fueldecaytime")
  319.     printGraphs("stored")
  320.     printActiveGraphs(currentRFObj)
  321.   end
  322. end
  323.  
  324.  
  325. function startup()
  326.   getInfoFromFile()
  327.    if 1 == 1 then
  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 1 == 1 then
  406.     getInfoFromReactorOLD()
  407.    end
  408.   draw()
  409.   local event, address, arg1, arg2, arg3 = event.pull(1)
  410.   if type(address) == "string" and component.isPrimary(address) then
  411.     if event == "key_down" and arg2 == keyboard.keys.q then
  412.       os.exit()
  413.     end
  414.   end
  415. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement