Umaler

EnanReactorAutoConfig

Oct 19th, 2025
2,374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.92 KB | Gaming | 0 0
  1. local event = require("event")
  2. local component = require("component")
  3. local term = require("term")
  4.  
  5. local TARGET_INSTABILITY = 2
  6. local STABILIZER_ENERGY  = 10000
  7. local OUTPUT_RATE    = 60000
  8.  
  9. local SLEEP_TIME = 3.0
  10.  
  11. local CORE_TYPE = "warpdriveEnanReactorCore"
  12. local PROCESSED_EVENTS = {
  13.   "interrupted",
  14.   "component_added",
  15.   "component_removed"
  16. }
  17.  
  18. local function pairsByKeys(t, f)
  19.   -- Возвращает элементы массива в отсортированном порядке
  20.   local a = {}
  21.   for n in pairs(t) do table.insert(a, n) end
  22.   table.sort(a, f)
  23.   local i = 0
  24.   local iter = function ()
  25.     i = i + 1
  26.     if a[i] == nil then return nil
  27.       else return a[i], t[a[i]]
  28.     end
  29.   end
  30.   return iter
  31. end
  32.  
  33. local function configureRelease(core)
  34.   local energy, capacity = core.energy()
  35.   local pct = energy / capacity
  36.   if pct > 0.9 then
  37.     core.releaseRate(OUTPUT_RATE)
  38.   else
  39.     core.release(false)
  40.   end
  41. end
  42.  
  43. local function initialConfig(core)
  44.   core.stabilizerEnergy(STABILIZER_ENERGY)
  45.   core.instabilityTarget(100 - TARGET_INSTABILITY)
  46.   configureRelease(core)
  47.   core.enable(true)
  48. end
  49.  
  50. local function drawState(cores)
  51.   term.clear()
  52.   print("Состояние ядер:")
  53.  
  54.   for addr, core in pairsByKeys(cores) do
  55.     pcall(function()
  56.       local instabil = {core.instability()}
  57.       instabil = 100.0 - (instabil[1] + instabil[2] + instabil[3] + instabil[4]) / 4.0
  58.       local rmode = core.releaseRate()
  59.       if rmode == "at_rate" then rmode = "rate" else rmode = "hold" end
  60.       local strToPrint = addr ..
  61.               "\tEnergy: " .. tostring(core.energy()) ..
  62.               "\tStability: " .. tostring(instabil) ..
  63.               "\tRelease mode: " .. rmode
  64.       print(strToPrint)
  65.     end)
  66.   end
  67. end
  68.  
  69. local function main()
  70.   local cores = {}
  71.   local rawList = component.list(CORE_TYPE)
  72.   for addr, _ in pairs(rawList) do
  73.     cores[addr] = component.proxy(addr)
  74.     initialConfig(cores[addr])
  75.   end
  76.   rawList = nil
  77.  
  78.   drawState(cores)
  79.  
  80.   while true do
  81.     local e = {event.pullFiltered(SLEEP_TIME, function(...)
  82.       local passedName = select(1, ...)
  83.       for _, ename in ipairs(PROCESSED_EVENTS) do
  84.         if ename == passedName then
  85.           return true
  86.         end
  87.       end
  88.       return false
  89.     end)}
  90.     if e[1] == "interrupted" then
  91.       return
  92.     elseif e[1] == "component_added" then
  93.       local addr, ctype = e[2], e[3]
  94.       if ctype == CORE_TYPE then
  95.         cores[addr] = component.proxy(addr)
  96.         initialConfig(cores[addr])
  97.       end
  98.     elseif e[1] == "component_removed" then
  99.       local addr, ctype = e[2], e[3]
  100.       if ctype == CORE_TYPE then
  101.         cores[addr] = nil
  102.       end
  103.     end
  104.  
  105.     for _, core in pairs(cores) do
  106.       pcall(configureRelease, core)
  107.     end
  108.     drawState(cores)
  109.   end
  110. end
  111.  
  112. local s, e = xpcall(main(), function(err) return err .. "\n" .. debug.traceback(); end)
  113. if not s then error(e) end
  114.  
Advertisement
Add Comment
Please, Sign In to add comment