Umaler

EnanReactorAutoConfig_ForEEPROM

Oct 24th, 2025
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.41 KB | Gaming | 0 0
  1. local function proxy(name)
  2.     return component.proxy(component.list("gpu")())
  3. end
  4.  
  5. local gpu = proxy("gpu")
  6. local w, h = gpu.getResolution()
  7.  
  8. local TARGET_INSTABILITY = 2
  9. local STABILIZER_ENERGY  = 10000
  10. local OUTPUT_RATE        = 60000
  11.  
  12. local SLEEP_TIME = 3.0
  13. local ERROR_SLEEP_TIME = 8.0
  14.  
  15. local CORE_TYPE = "warpdriveEnanReactorCore"
  16.  
  17. local function pairsByKeys(t, f)
  18.     local a = {}
  19.     for n in pairs(t) do table.insert(a, n) end
  20.     table.sort(a, f)
  21.     local i = 0
  22.     local iter = function ()
  23.         i = i + 1
  24.         if a[i] == nil then return nil
  25.             else return a[i], t[a[i]]
  26.         end
  27.     end
  28.     return iter
  29. end
  30.  
  31. local function configureRelease(core)
  32.     local energy, capacity = core.energy()
  33.     local pct = energy / capacity
  34.     if pct > 0.9 then
  35.         core.releaseRate(OUTPUT_RATE)
  36.     else
  37.         core.release(false)
  38.     end
  39. end
  40.  
  41. local function initialConfig(core)
  42.     core.stabilizerEnergy(STABILIZER_ENERGY)
  43.     core.instabilityTarget(100 - TARGET_INSTABILITY)
  44.     configureRelease(core)
  45.     core.enable(true)
  46. end
  47.  
  48. local cursorY = 1
  49. local function print(...)
  50.     local resStr = ""
  51.     for _, v in ipairs({...}) do
  52.         resStr = resStr .. tostring(v) .. "    "
  53.     end
  54.     if gpu.set(1, cursorY, resStr) then
  55.         cursorY = cursorY + 1
  56.     end
  57. end
  58.  
  59. local function clear()
  60.     gpu.fill(1, 1, w, h, ' ')
  61.     cursorY = 1
  62. end
  63.  
  64. local function drawState(cores)
  65.     clear()
  66.     print("Состояние ядер:")
  67.  
  68.     for addr, core in pairsByKeys(cores) do
  69.         pcall(function()
  70.             local instabil = {core.instability()}
  71.             instabil = 100.0 - (instabil[1] + instabil[2] + instabil[3] + instabil[4]) / 4.0
  72.             local rmode = core.releaseRate()
  73.             if rmode == "at_rate" then rmode = "rate" else rmode = "hold" end
  74.             print(addr, "Energy: " .. tostring(core.energy()), "Stability: " .. tostring(instabil), "Release mode: " .. rmode)
  75.         end)
  76.     end
  77. end
  78.  
  79. local function main()
  80.     local cores = {}
  81.     for addr, _ in pairs(component.list(CORE_TYPE)) do
  82.         cores[addr] = component.proxy(addr)
  83.         initialConfig(cores[addr])
  84.     end
  85.  
  86.     drawState(cores)
  87.  
  88.     local lastDrawedTime = computer.uptime()
  89.     while true do
  90.         -- Get event
  91.         e = {computer.pullSignal(SLEEP_TIME)}
  92.  
  93.         if e[1] == "component_added" then
  94.             local addr, ctype = e[2], e[3]
  95.             if ctype == CORE_TYPE then
  96.                 cores[addr] = component.proxy(addr)
  97.                 initialConfig(cores[addr])
  98.             end
  99.         elseif e[1] == "component_removed" then
  100.             local addr, ctype = e[2], e[3]
  101.             if ctype == CORE_TYPE then
  102.                 cores[addr] = nil
  103.             end
  104.         end
  105.  
  106.         local currentTime = computer.uptime()
  107.         if currentTime > (lastDrawedTime + SLEEP_TIME) then
  108.             for _, core in pairs(cores) do
  109.                 pcall(configureRelease, core)
  110.             end
  111.             drawState(cores)
  112.             lastDrawedTime = currentTime
  113.         end
  114.     end
  115. end
  116.  
  117. local s, e = xpcall(main(), function(err) return err .. "\n" .. debug.traceback(); end)
  118.  
  119. if not s then
  120.     clear()
  121.     gpu.setForeground(0xFF0000)
  122.     print(e)
  123.     gpu.setForeground(0xFFFFFF)
  124.  
  125.     local startTime = computer.uptime()
  126.     repeat
  127.         computer.pullSignal(ERROR_SLEEP_TIME)
  128.     until computer.uptime() > (startTime + ERROR_SLEEP_TIME)
  129. end
  130.  
Advertisement
Add Comment
Please, Sign In to add comment