Advertisement
jakedacatman

Windows™ for Fusion Reactors (CONTROLLER, REQUIRES OPUS OS!)

May 29th, 2020
1,729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.13 KB | None | 0 0
  1. --[[
  2. This is Windows™ For Fusion Reactors.  
  3. I am not responsible for the following misfortunes that may come as a result of using this program:
  4. -Loss of life
  5. -Bone loss
  6. -Accidental fusion of body parts
  7. -Vaporised teeth
  8. -Incineration by plasma
  9. and others.
  10. ]]--
  11.  
  12. local UI = require "opus.ui"
  13. local Event = require "opus.event"
  14. local Util  = require "opus.util"
  15.  
  16. local combos = {
  17.     ["hydrogen-hydrogen"] = 4430000000,
  18.     ["hydrogen-deuterium"] = 1245000000,
  19.     ["hydrogen-tritium"] = 6049000000,
  20.     ["hydrogen-helium3"] = 3339000000,
  21.     ["hydrogen-lithium6"] = 7278000000,
  22.     ["hydrogen-lithium7"] = 5071000000,
  23.     ["hydrogen-boron11"] = 16370000000,
  24.     ["deuterium-deuterium"] = 1156000000,
  25.     ["deuterium-tritium"] = 816000000,
  26.     ["deuterium-helium3"] = 2632000000,
  27.     ["deuterium-lithium6"] = 4818000000,
  28.     ["deuterium-lithium7"] = 5034000000,
  29.     ["deuterium-boron11"] = 16883000000,
  30.     ["tritium-tritium"] = 897000000,
  31.     ["tritium-helium3"] = 2604000000,
  32.     ["tritium-lithium6"] = 4971000000,
  33.     ["tritium-lithium7"] = 5511000000,
  34.     ["tritium-boron11"] = 33215000000,
  35.     ["helium3-helium3"] = 6605000000,
  36.     ["helium3-lithium6"] = 9506000000,
  37.     ["helium3-lithium7"] = 9673000000,
  38.     ["helium3-boron11"] = 29574000000,
  39.     ["lithium6-lithium6"] = 13732000000,
  40.     ["lithium6-lithium7"] = 14536000000,
  41.     ["lithium6-boron11"] = 37048000000,
  42.     ["lithium7-lithium7"] = 16611000000,
  43.     ["lithium7-boron11"] = 202000000000,
  44.     ["boron11-boron11"] = 358000000000,
  45.     ["Out of fuel"] = math.huge
  46. }
  47.  
  48. local fuels = {
  49.     ["hydrogen"] = "1H",
  50.     ["deuterium"] = "2H",
  51.     ["tritium"] = "3H",
  52.     ["helium3"] = "3He",
  53.     ["lithium6"] = "6Li",
  54.     ["lithium7"] = "7Li",
  55.     ["boron11"] = "11B",
  56.     ["none"] = "none"
  57. }
  58.  
  59. local termSize = { term.getSize() }
  60.  
  61. local controllers = { peripheral.find "computer" }
  62. local modem = peripheral.find "modem"
  63.  
  64. modem.open(1)
  65.  
  66. for i,v in pairs(controllers) do
  67.     v.turnOn()
  68. end
  69.  
  70. local reactors = {}
  71. local reactorsLifetime = {}
  72. for i,v in ipairs(controllers) do
  73.     modem.transmit(i, 1, "data")
  74.     local e = { os.pullEvent "modem_message" }
  75.     local data = e[5]
  76.     local currData = {
  77.         id = i,
  78.         efficiency = data.efficiency > 100 and 100 or data.efficiency,
  79.         temperature = data.temperature,
  80.         power = data.power,
  81.         fuelOne = data.fuelOne,
  82.         fuelTwo = data.fuelTwo,
  83.         problem = data.problem
  84.     }
  85.     local lifetimeData = {
  86.         efficiency = { data.efficiency > 100 and 100 or data.efficiency },
  87.         temperature = { data.temperature },
  88.         power = { data.power },
  89.         fuelOne = { data.fuelOne },
  90.         fuelTwo = { data.fuelTwo },
  91.         problem = { data.problem }
  92.     }
  93.     table.insert(reactors, currData)
  94.     table.insert(reactorsLifetime, lifetimeData)
  95. end
  96.  
  97. local function average(t)
  98.     local total = 0
  99.     for i, v in ipairs(t) do
  100.        total = total + v
  101.     end
  102.     return total / #t
  103. end
  104.  
  105. local function sum(t)
  106.     local total = 0
  107.     for i,v in ipairs(t) do
  108.         total = total + v
  109.     end
  110.     return total
  111. end
  112.  
  113. local function round(n, place)
  114.     if place then return round(n / place) * place end
  115.     return n % 1 >= 0.5 and math.ceil(n) or math.floor(n)
  116. end
  117.  
  118. local function prettyTemp(t, place)
  119.     local roundedTemp = round(t, place)
  120.     if roundedTemp < 1000 then return roundedTemp.."K"
  121.     elseif roundedTemp / 1000 < 1000 then return round(roundedTemp / 1000, 0.001).."kK"
  122.     elseif roundedTemp / 1000000 < 1000 then return round(roundedTemp / 1000000, 0.001).."MK"
  123.     elseif roundedTemp / 1000000000 < 1000 then return round(roundedTemp / 1000000000, 0.001).."GK"
  124.     elseif roundedTemp / 1000000000000 < 1000 then return round(roundedTemp / 1000000000000, 0.001).."TK" end
  125. end
  126.  
  127. local function prettyPow(t, place)
  128.     local roundedPow = place ~= 0 and round(t, place) or t
  129.     if roundedPow < 1000 then return roundedPow.."RF/t"
  130.     elseif roundedPow / 1000 < 1000 then return round(roundedPow / 1000, 0.001).."kRF/t"
  131.     elseif roundedPow / 1000000 < 1000 then return round(roundedPow / 1000000, 0.001).."MRF/t"
  132.     elseif roundedPow / 1000000000 < 1000 then return round(roundedPow / 1000000000, 0.001).."GRF/t"
  133.     elseif roundedPow / 1000000000000 < 1000 then return round(roundedPow / 1000000000000, 0.001).."TRF/t" end
  134. end
  135.  
  136. local page = UI.Page {
  137.     paused = false,
  138.     index = 1,
  139.     notification = UI.Notification { },
  140.    
  141.     menuBar = UI.MenuBar {
  142.         buttons = menuButtons
  143.     },
  144.    
  145.     reactorGrid = UI.ScrollingGrid {
  146.         y = 1,
  147.         reactorCount = #controllers,
  148.         sortColumn = "id",
  149.         values = reactors,
  150.         columns = {
  151.             { heading = "ID", key = "id", width = 2, align = "left" },
  152.             { heading = "Temp", key = "temperature", width = 10, align = "left" },
  153.             { heading = "Power", key = "power", width = 15, align = "left" },
  154.             { heading = "Efficiency", key = "efficiency", width = 10, align = "left" }
  155.         },
  156.         autospace = true,
  157.         getDisplayValues = function(_, row)
  158.             row = Util.shallowCopy(row)
  159.             if row.temperature then
  160.                 row.temperature = prettyTemp(row.temperature, 0.001)
  161.             end
  162.             if row.power then
  163.                 row.power = prettyPow(row.power, 0.001)
  164.             end
  165.             if row.efficiency then
  166.                 row.efficiency = round(row.efficiency, 0.001).."%"
  167.             end
  168.             return row
  169.         end
  170.     },
  171.    
  172.     reactorSlide = UI.SlideOut {
  173.         titleBar = UI.TitleBar {
  174.             title = "Reactor Stats",
  175.             event = "reactor_close"
  176.         },
  177.         accelerators = {
  178.             ["backspace"] = "reactor_close"
  179.         },
  180.         reactorMeta = UI.Grid {
  181.             x = 1, y = 1, ex = termSize[1], height = termSize[2]-2,
  182.             inactive = true,
  183.             columns = {
  184.                 { key = "text", width = 12 },
  185.                 { key = "value", align = "left", textColor = colors.yellow }
  186.             },
  187.             values = {
  188.                 temperature = { text = "Temperature" },
  189.                 efficiency = { text = "Efficiency" },
  190.                 power = { text = "Power" },
  191.                 fuel = { text = "Fuel" },
  192.                 problem = { text = "Problem" }
  193.             }
  194.         }
  195.     }
  196. }
  197.  
  198. function page.reactorSlide:setReactor(reactor)
  199.     self.currReactor = reactor
  200.     local thisLifetimeData = reactorsLifetime[page.reactorSlide.currReactor.id]
  201.     self.reactorMeta.values.temperature.value = round(page.reactorSlide.currReactor.temperature, 1).."K ("..round(average(thisLifetimeData.temperature), 1).."K avg)"
  202.     self.reactorMeta.values.efficiency.value = round(page.reactorSlide.currReactor.efficiency, 0.000001).."% ("..round(average(thisLifetimeData.efficiency), 0.000001).."% avg)"
  203.     self.reactorMeta.values.power.value = round(page.reactorSlide.currReactor.power, 0.001).."RF/t ("..round(average(thisLifetimeData.power), 0.001).."RF/t avg)"
  204.     self.reactorMeta.values.fuel.value = fuels[page.reactorSlide.currReactor.fuelOne].."-"..fuels[page.reactorSlide.currReactor.fuelTwo]
  205.     self.reactorMeta.values.problem.value = page.reactorSlide.currReactor.problem or "none"
  206. end
  207.  
  208. function page.reactorSlide:show(reactor)
  209.     self:setReactor(reactor)
  210.     UI.SlideOut.show(self)
  211. end
  212.  
  213. function page.reactorSlide:eventHandler(event)
  214.     if event.type == "reactor_close" then
  215.         self:hide()
  216.         page:setFocus(page.reactorGrid)
  217.     else return UI.SlideOut.eventHandler(self, event) end
  218.     return true
  219. end
  220.  
  221. function page:eventHandler(event)
  222.     if event.type == "grid_select" then
  223.         self.reactorSlide:show(event.selected)
  224.     else return UI.Page.eventHandler(self, event) end
  225.     return true
  226. end
  227.  
  228. Event.onInterval(5, function()
  229.     for i,v in ipairs(controllers) do
  230.         modem.transmit(i, 1, "data")
  231.         local e = { os.pullEvent "modem_message" }
  232.        
  233.         local data = e[5]
  234.         local combo = { "Out of fuel" }
  235.         if data.fuelOne and data.fuelTwo then
  236.             combo = { data.fuelOne.."-"..data.fuelTwo, data.fuelTwo.."-"..data.fuelOne }
  237.         end
  238.        
  239.         local currTemp = data.temperature
  240.         local optimalTemp = combos[combo[1]] or combos[combo[2]]
  241.         local isAboveTemp = (currTemp and optimalTemp) and currTemp - optimalTemp > 0 or false
  242.        
  243.         if isAboveTemp then
  244.             modem.transmit(i, 1, "on")
  245.         else
  246.             modem.transmit(i, 1, "off")
  247.         end
  248.        
  249.         local currData = {
  250.             id = i,
  251.             efficiency = data.efficiency > 100 and 100 or data.efficiency,
  252.             temperature = data.temperature,
  253.             power = data.power,
  254.             fuelOne = data.fuelOne,
  255.             fuelTwo = data.fuelTwo,
  256.             problem = data.problem
  257.         }
  258.         reactors[i] = currData
  259.         for k, v in pairs(currData) do
  260.             if reactorsLifetime[i][k] then
  261.                 table.insert(reactorsLifetime[i][k], v)
  262.             end
  263.         end
  264.     end
  265.     page.reactorGrid:update()
  266.     page.reactorGrid:draw()
  267.     page:sync()
  268. end)
  269.  
  270. UI:setPage(page)
  271. UI:start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement