Advertisement
Revector

Gate control Draconic Core

Jun 9th, 2025
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.78 KB | None | 0 0
  1. local config = {
  2.     -- maxCoreEnergy - на этом уровне будет поддерживаться количество энергии в ядре
  3.     -- 1000 * 10^9 соответсвунт 1000 B или 1 T
  4.     maxCoreEnergy = 1000 * 10^9,
  5.     maxDiffEnergy = 10000, -- максимальная скорость накапливания энергии Rf/t, тестировалось на этом значении
  6.     step = 5000,  -- шаг изменния значения на гейте
  7.  
  8.     sleepTime = 1, -- шаг замеров в сек
  9. }
  10.  
  11. local com = require('component')
  12. local gpu = com.gpu
  13. gpu.setResolution(80, 25) -- lvl 2
  14.  
  15. local function coreEnergy()
  16.     return com.draconic_rf_storage.getEnergyStored()
  17. end
  18.  
  19. local function format(num)
  20.     if num > 10^12 then
  21.         return string.format("%0.3f T", num/10^12)
  22.     elseif num > 10^9 then
  23.         return string.format("%0.3f B", num/10^9)
  24.     elseif num > 10^6 then
  25.         return string.format("%0.3f M", num/10^6)
  26.     elseif num > 10^3 then
  27.         return string.format("%0.3f K", num/10^3)
  28.     else
  29.         return string.format("%d", num)
  30.     end
  31. end
  32.  
  33. local filVal = 0
  34. function expRunningAvg(newVal)
  35.     filVal = filVal + ((newVal-filVal) * 0.3)
  36.     return filVal
  37. end
  38.  
  39. if false == com.isAvailable("draconic_rf_storage") then
  40.     print("Rf storage not connected!")
  41.     os.exit()
  42. end
  43.  
  44. if false == com.isAvailable("flux_gate") then
  45.     print("Flux gate not connected!")
  46.     os.exit()
  47. end
  48.  
  49. local time = os.time()
  50. local energy = coreEnergy()
  51.  
  52. local w, h = gpu.getResolution()
  53. function asbMax(t)
  54.     local  max = t[1]
  55.     for _, val in ipairs(t) do
  56.         if math.abs(val) > max then
  57.             max = math.abs(val)
  58.         end
  59.     end
  60.  
  61.     return max
  62. end
  63.  
  64. local _MID = h/2 + 3
  65. local _NUM_READ = w
  66. local energyLog = {}
  67. local current = 0
  68.  
  69. function displayGraph(diff)
  70.     current = current + 1
  71.     if current > _NUM_READ then
  72.         current = 1
  73.     end
  74.  
  75.     energyLog[current] = diff
  76.  
  77.     local maxVal = asbMax(energyLog)
  78.  
  79.     gpu.fill(1, _MID, w, 1, '━')
  80.     local row = 1
  81.     for i = current, _NUM_READ + current - 1 do
  82.         local key = i % w + 1
  83.  
  84.         if energyLog[key] then
  85.             local d = (math.ceil(energyLog[key] / (maxVal * 0.1)))
  86.             if d > 0 then
  87.                 gpu.fill(row, _MID-d, 1, d, '█')
  88.             else
  89.                 gpu.fill(row, _MID+1, 1, math.abs(d), '█')
  90.             end
  91.         end
  92.  
  93.         row = row + 1
  94.     end
  95. end
  96.  
  97. function displayData(core, diff, action, fluxGateFlow)
  98.     gpu.fill(1, 1, w, h, " ")
  99.     gpu.set(2, 1, string.format(" Энергии в ядре: %s [%0.0f Rf]", format(core), core))
  100.     gpu.set(2, 2, string.format(" Выход на гейте: %s Rf [%0.0f Rf]  %s", format(fluxGateFlow), fluxGateFlow, action))
  101.     gpu.set(2, 3, string.format(' Накопление энергии: %0.1f Rf/t', diff))
  102.  
  103.     displayGraph(diff)
  104. end
  105.  
  106. local running = true
  107. while running do
  108.     os.sleep(config.sleepTime)
  109.  
  110.     local tmpEnergy = coreEnergy()
  111.     local tmpTime = os.time()
  112.  
  113.     local energyDiff = tmpEnergy - energy
  114.     local timeDiff = tmpTime - time
  115.  
  116.     local diff = expRunningAvg(energyDiff / timeDiff)
  117.  
  118.     local fluxGateFlow = com.flux_gate.getFlow();
  119.  
  120.     local action = ""
  121.  
  122.     if tmpEnergy > config.maxCoreEnergy then
  123.         if diff > (config.maxDiffEnergy * -1) then
  124.             action = 'Повышаю ▲'
  125.             com.flux_gate.setSignalLowFlow(fluxGateFlow + config.step)
  126.         end
  127.     else
  128.         if diff < config.maxDiffEnergy then
  129.             action = 'Понижаю ▼'
  130.             com.flux_gate.setSignalLowFlow(fluxGateFlow - config.step)
  131.         end
  132.     end
  133.  
  134.     displayData(tmpEnergy, diff, action, fluxGateFlow)
  135.  
  136.     time = tmpTime
  137.     energy = tmpEnergy
  138. end
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement