Advertisement
Entityreborn

Untitled

Jan 23rd, 2022
907
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local t = peripheral.find("turbine")
  2. local r = peripheral.find("fissionReactor")
  3. local i = peripheral.find("inductionMatrix")
  4. local m = peripheral.find("monitor")
  5.  
  6. local w, h = m.getSize()
  7.  
  8. local onCooldown = false
  9. local energyFull = false
  10.  
  11. local buttons = {}
  12. buttons[1] = {"Auto burnrate", true, colors.white}
  13.  
  14. local function formatNumber(count, aNumber)
  15.     return ("%0." .. count .. "f"):format(aNumber)
  16. end
  17.  
  18. local function formatBigNumber(work, append, sizes)
  19.     if (work == 0) then return work .. append end
  20.     local i = math.floor(math.log(work) / math.log(1000)) + 1
  21.     return tostring(string.format("%0.2f", (work / 1000 ^ (i - 1)))) .. sizes[i] .. append
  22. end
  23.  
  24. local function formatEnergy(num)
  25.     return formatBigNumber(num, "fe", {"","K","M","G","T"})
  26. end
  27.  
  28. local function formatLiquid(num)
  29.     return formatBigNumber(num, "B", {"m","","K","M","G"})
  30. end
  31.  
  32. function printCenter(msg, line)
  33.     local x = (w + 1) / 2 - #msg / 2
  34.     m.setCursorPos(x, line)
  35.     m.write(msg)
  36. end
  37.  
  38. -- 0.1 burn = 3.3kFE
  39. local function calculateBurnRate()
  40.     local inductionEnergyOut = i.getLastOutput()
  41.     local outRate = inductionEnergyOut / 10000 / 3
  42.  if outRate > r.getMaxBurnRate() then outRate = r.getMaxBurnRate()  end
  43.     return outRate
  44. end
  45.  
  46. m.setTextScale(0.5)
  47.  
  48. local function getCurrentStats()
  49.     return {
  50.         inductionEnergyPct = i.getEnergyFilledPercentage(),
  51.         inductionEnergy = i.getEnergy(),
  52.         inductionEnergyMax = i.getMaxEnergy(),
  53.         inductionEnergyIn = i.getLastInput(),
  54.         inductionEnergyOut = i.getLastOutput(),
  55.         inductionEnergyRatio = i.getLastInput() / i.getLastOutput(),
  56.  
  57.         reactorSteamPct = r.getHeatedCoolantFilledPercentage(),
  58.         reactorSteam = r.getHeatedCoolant().amount,
  59.         reactorCoolantPct = r.getCoolantFilledPercentage(),
  60.         reactorCoolant = r.getCoolant().amount,
  61.         reactorTemp = r.getTemperature(),
  62.         reactorBurnRate = r.getBurnRate(),
  63.         reactorStatus = r.getStatus(),
  64.  
  65.         turbineSteamPct = t.getSteamFilledPercentage(),
  66.         turbineSteam = t.getSteam(),
  67.         turbineEnergyProd = t.getProductionRate(),
  68.     }
  69. end
  70.  
  71. local function checkReactor()
  72.   local stats = getCurrentStats()
  73.  
  74.     local desiredAction = r.activate -- r.activate, r.scram, nothing, makeMeASandwitch
  75.  
  76.     if (stats.inductionEnergyPct > 0.95 and not energyFull)
  77.     then
  78.         desiredAction = r.scram
  79.         energyFull = true
  80.     end
  81.  
  82.     if (stats.inductionEnergyPct < 0.1 and energyFull)
  83.     then
  84.         desiredAction = r.activate
  85.         energyFull = false
  86.     end
  87.  
  88.     if (stats.reactorSteamPct > 0.9 or stats.reactorCoolantPct < 0.1 and not onCooldown)
  89.     then
  90.         desiredAction = r.scram
  91.         onCooldown = true
  92.     end
  93.  
  94.     if (stats.reactorSteamPct < 0.9 and stats.reactorCoolantPct > 0.9 and onCooldown)
  95.     then
  96.         desiredAction = r.activate
  97.         onCooldown = false
  98.     end
  99.  
  100.     if buttons[1][2] then
  101.         r.setBurnRate(calculateBurnRate())
  102.     end
  103.  
  104.     desiredAction()
  105. end
  106.  
  107. local function updateMonitor()
  108.     local stats = getCurrentStats()
  109.  
  110.         m.clear()
  111.         m.setCursorPos(1, 1)
  112.         m.write("Energy stored:   " .. formatNumber(2, stats.inductionEnergyPct * 100) .. "%")
  113.         m.setCursorPos(1, 2)
  114.         m.write("                 " .. formatEnergy(stats.inductionEnergy) .. " / " .. formatEnergy(stats.inductionEnergyMax))
  115.         m.setCursorPos(1, 3)
  116.         m.write("                 " .. formatEnergy(stats.inductionEnergyIn) .. " in / " .. formatEnergy(stats.inductionEnergyOut) .. " out")
  117.     m.setCursorPos(1, 4)
  118.     m.write("Energy ratio:    " .. stats.inductionEnergyRatio)
  119.  
  120.         m.setCursorPos(1, 6)
  121.         m.write("Reactor burnrate:" .. stats.reactorBurnRate .. "mB/t")
  122.         m.setCursorPos(1, 7)
  123.         m.write("Reactor coolant: " .. formatNumber(2, stats.reactorCoolantPct * 100) .. "% (" .. formatLiquid(stats.reactorCoolant) .. ")")
  124.         m.setCursorPos(1, 8)
  125.         m.write("Reactor steam:   " .. formatNumber(2, stats.reactorSteamPct * 100) .. "% (" .. formatLiquid(stats.reactorSteam) .. ")")
  126.         m.setCursorPos(1, 9)
  127.         m.write("Reactor temp:    " .. formatNumber(2, stats.reactorTemp) .. "K")
  128.  
  129.         m.setCursorPos(1, 11)
  130.         m.write("Turbine prod:    " .. formatEnergy(stats.turbineEnergyProd) .. "")
  131.         m.setCursorPos(1, 12)
  132.         m.write("Turbine steam:   " .. formatNumber(2, stats.turbineSteamPct * 100) .. "% (" .. formatLiquid(stats.turbineSteam) .. ")")
  133.  
  134.         height = 15
  135.         for i = 1, #buttons do
  136.             if buttons[i][2] == true then
  137.                 m.setBackgroundColor(colors.green)
  138.             elseif buttons[i][2] == false then
  139.                 m.setBackgroundColor(colors.red)
  140.             else
  141.                 m.setBackgroundColor(colors.lightGray)
  142.             end
  143.  
  144.             printCenter("*                        *", i + height)
  145.             printCenter(buttons[i][1], i+height)
  146.             m.setBackgroundColor(colors.black)
  147.     end
  148. end
  149.  
  150.  -- event, peripheral_name, x, y
  151. local function processButtons(event)
  152.     height = 15
  153.  
  154.     for i = 1, #buttons do
  155.         if event[4] == height + i then
  156.             buttons[i][2] = not buttons[i][2]
  157.  
  158.             print(buttons[i][2])
  159.         end
  160.     end
  161. end
  162.  
  163. local checkTimer = os.startTimer(0.5) --queue first timer in 1 second
  164. while true do
  165.   local event = {os.pullEvent()}
  166.  
  167.   if event[1] == "monitor_touch" then
  168.     processButtons(event)
  169.   elseif event[1] == "timer" then
  170.     if event[2] == checkTimer then --our check timer
  171.       checkReactor()
  172.       updateMonitor()
  173.  
  174.       checkTimer = os.startTimer(0.5)  --start next timer for checks function in 1 second.
  175.     end
  176.   end
  177. end
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement