Advertisement
Guest User

reactor.lua

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