Advertisement
cc-editor

reactor.lua

Oct 2nd, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.89 KB | None | 0 0
  1. settings.load()
  2.  
  3. -- #region Setup
  4. local fuel_type = settings.get("reactor.fuelType")
  5. local me_interface = peripheral.wrap(settings.get("reactor.meInterface"))
  6. local battery_threshold = settings.get("reactor.batteryThreshold")
  7.  
  8. local rods = {}
  9. for i=1, 4 do
  10.     rods[i] = {
  11.         timer = nil,
  12.         refuel_count = 0,
  13.         control = peripheral.wrap(settings.get("reactor.rod"..i..".rodControl")),
  14.         redRouter = peripheral.wrap(settings.get("reactor.rod"..i..".redRouter")),
  15.         clutch_side = settings.get("reactor.rod"..i..".clutchSide"),
  16.         boiler = peripheral.wrap(settings.get("reactor.rod"..i..".boiler")),
  17.         battery = peripheral.wrap(settings.get("reactor.rod"..i..".battery")),
  18.     }
  19. end
  20. -- #endregion
  21.  
  22. local function is_powered(rod_number)
  23.     if string.find(rods[rod_number].boiler.getLine(1), "Max") then
  24.         return true
  25.     end
  26.     return false
  27. end
  28.  
  29. local function is_battery_level_ok(rod_number)
  30.     if rods[rod_number].battery.getEnergy() > battery_threshold then
  31.         return true
  32.     end
  33.     return false
  34. end
  35.  
  36. local function power_rod(rod_number, fuel_count)
  37.     print("Activiating rod "..rod_number)
  38.  
  39.     local me_slot = me_interface.getItemDetail(1)
  40.     if me_slot == nil or me_slot.count < fuel_count then
  41.         print("Not enough fuel to power rod "..rod_number)
  42.         -- TODO: Send alert
  43.         return false
  44.     end
  45.     if me_slot.name ~= fuel_type then
  46.         print("Wrong fuel type in ME interface")
  47.         -- TODO: Send alert
  48.         return false
  49.     end
  50.  
  51.     print("Disable output for boiler "..rod_number)
  52.     rods[rod_number].redRouter.setOutput(rods[rod_number].clutch_side, true)
  53.  
  54.     print("Powering rod "..rod_number.." with fuel")
  55.     rods[rod_number].control.pullItems(peripheral.getName(me_interface), 1, fuel_count)
  56.  
  57.     local consecutive_max_power = 0
  58.     for i = 1, 30 do
  59.         if is_powered(rod_number) then
  60.             consecutive_max_power = consecutive_max_power + 1
  61.         else
  62.             consecutive_max_power = 0
  63.         end
  64.  
  65.         if consecutive_max_power >= 3 then
  66.             print("Boiler for rod "..rod_number.." started")
  67.             rods[rod_number].redRouter.setOutput(rods[rod_number].clutch_side, false)
  68.             return true
  69.         end
  70.         sleep(1)
  71.     end
  72.  
  73.     print("Boiler for rod "..rod_number.." cannot be started")
  74.     -- TODO: Send alert
  75.     return false
  76. end
  77.  
  78. local function refuel_rod(rod_number, fuel_count)
  79.     rods[rod_number].refuel_count = rods[rod_number].refuel_count + 1
  80.     print("Refueling rod "..rod_number.." for "..rods[rod_number].refuel_count..". time")
  81.  
  82.     local me_slot = me_interface.getItemDetail(1)
  83.     if me_slot == nil or me_slot.count < fuel_count then
  84.         print("Not enough fuel to power rod "..rod_number)
  85.         -- TODO: Send alert
  86.         return false
  87.     end
  88.     if me_slot.name ~= fuel_type then
  89.         print("Wrong fuel type in ME interface")
  90.         -- TODO: Send alert
  91.         return false
  92.     end
  93.  
  94.     if not is_powered(rod_number) then
  95.         print("Cannot refuel rod "..rod_number.." because boiler is not active")
  96.         -- TODO: Send alert
  97.         return false
  98.     end
  99.  
  100.     print("Powering rod "..rod_number.." with fuel")
  101.     rods[rod_number].control.pullItems(peripheral.getName(me_interface), 1, fuel_count)
  102.     print("Refueled rod "..rod_number)
  103. end
  104.  
  105. local function check_rods()
  106.     while true do
  107.         print("Checking rods")
  108.         for i = 1, 4 do
  109.             if is_powered(i) then
  110.                 print("Rod "..i.." is active")
  111.             elseif not is_battery_level_ok(i) then
  112.                 print("Rod "..i.." is inactive and battery level is below threshold")
  113.                 if rods[i].timer ~= nil then
  114.                     os.cancelTimer(rods[i].timer)
  115.                     rods[i].timer = nil
  116.                 end
  117.                 rods[i].refuel_count = 0
  118.                 local started = power_rod(i, 2)
  119.                 if started then
  120.                     rods[i].timer = os.startTimer(570)
  121.                 end
  122.             else
  123.                 print("Rod "..i.." is inactive and battery level is ok")
  124.             end
  125.         end
  126.         print("Checks completed")
  127.         sleep(10)
  128.     end
  129. end
  130.  
  131. local function check_timer()
  132.     while true do
  133.         local _, timer_id = os.pullEvent("timer")
  134.         for i = 1, 4 do
  135.             if timer_id == rods[i].timer then
  136.                 print("Refuel-Timer for rod "..i.." expired")
  137.                 if is_powered(i)
  138.                 and rods[i].battery.getEnergy() < rods[i].battery.getEnergyCapacity() - 8000
  139.                 and rods[i].refuel_count < 6 then
  140.                     refuel_rod(i, 1)
  141.                 else
  142.                     print("but refuel conditions are not met")
  143.                 end
  144.                 break
  145.             end
  146.         end
  147.     end
  148. end
  149.  
  150. print("Starting reactor control")
  151. parallel.waitForAll(check_rods, check_timer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement