Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- settings.load()
- -- #region Setup
- local fuel_type = settings.get("reactor.fuelType")
- local me_interface = peripheral.wrap(settings.get("reactor.meInterface"))
- local battery_threshold = settings.get("reactor.batteryThreshold")
- local rods = {}
- for i=1, 4 do
- rods[i] = {
- timer = nil,
- refuel_count = 0,
- control = peripheral.wrap(settings.get("reactor.rod"..i..".rodControl")),
- redRouter = peripheral.wrap(settings.get("reactor.rod"..i..".redRouter")),
- clutch_side = settings.get("reactor.rod"..i..".clutchSide"),
- boiler = peripheral.wrap(settings.get("reactor.rod"..i..".boiler")),
- battery = peripheral.wrap(settings.get("reactor.rod"..i..".battery")),
- }
- end
- -- #endregion
- local function is_powered(rod_number)
- if string.find(rods[rod_number].boiler.getLine(1), "Max") then
- return true
- end
- return false
- end
- local function is_battery_level_ok(rod_number)
- if rods[rod_number].battery.getEnergy() > battery_threshold then
- return true
- end
- return false
- end
- local function power_rod(rod_number, fuel_count)
- print("Activiating rod "..rod_number)
- local me_slot = me_interface.getItemDetail(1)
- if me_slot == nil or me_slot.count < fuel_count then
- print("Not enough fuel to power rod "..rod_number)
- -- TODO: Send alert
- return false
- end
- if me_slot.name ~= fuel_type then
- print("Wrong fuel type in ME interface")
- -- TODO: Send alert
- return false
- end
- print("Disable output for boiler "..rod_number)
- rods[rod_number].redRouter.setOutput(rods[rod_number].clutch_side, true)
- print("Powering rod "..rod_number.." with fuel")
- rods[rod_number].control.pullItems(peripheral.getName(me_interface), 1, fuel_count)
- local consecutive_max_power = 0
- for i = 1, 30 do
- if is_powered(rod_number) then
- consecutive_max_power = consecutive_max_power + 1
- else
- consecutive_max_power = 0
- end
- if consecutive_max_power >= 3 then
- print("Boiler for rod "..rod_number.." started")
- rods[rod_number].redRouter.setOutput(rods[rod_number].clutch_side, false)
- return true
- end
- sleep(1)
- end
- print("Boiler for rod "..rod_number.." cannot be started")
- -- TODO: Send alert
- return false
- end
- local function refuel_rod(rod_number, fuel_count)
- rods[rod_number].refuel_count = rods[rod_number].refuel_count + 1
- print("Refueling rod "..rod_number.." for "..rods[rod_number].refuel_count..". time")
- local me_slot = me_interface.getItemDetail(1)
- if me_slot == nil or me_slot.count < fuel_count then
- print("Not enough fuel to power rod "..rod_number)
- -- TODO: Send alert
- return false
- end
- if me_slot.name ~= fuel_type then
- print("Wrong fuel type in ME interface")
- -- TODO: Send alert
- return false
- end
- if not is_powered(rod_number) then
- print("Cannot refuel rod "..rod_number.." because boiler is not active")
- -- TODO: Send alert
- return false
- end
- print("Powering rod "..rod_number.." with fuel")
- rods[rod_number].control.pullItems(peripheral.getName(me_interface), 1, fuel_count)
- print("Refueled rod "..rod_number)
- end
- local function check_rods()
- while true do
- print("Checking rods")
- for i = 1, 4 do
- if is_powered(i) then
- print("Rod "..i.." is active")
- elseif not is_battery_level_ok(i) then
- print("Rod "..i.." is inactive and battery level is below threshold")
- if rods[i].timer ~= nil then
- os.cancelTimer(rods[i].timer)
- rods[i].timer = nil
- end
- rods[i].refuel_count = 0
- local started = power_rod(i, 2)
- if started then
- rods[i].timer = os.startTimer(570)
- end
- else
- print("Rod "..i.." is inactive and battery level is ok")
- end
- end
- print("Checks completed")
- sleep(10)
- end
- end
- local function check_timer()
- while true do
- local _, timer_id = os.pullEvent("timer")
- for i = 1, 4 do
- if timer_id == rods[i].timer then
- print("Refuel-Timer for rod "..i.." expired")
- if is_powered(i)
- and rods[i].battery.getEnergy() < rods[i].battery.getEnergyCapacity() - 8000
- and rods[i].refuel_count < 6 then
- refuel_rod(i, 1)
- else
- print("but refuel conditions are not met")
- end
- break
- end
- end
- end
- end
- print("Starting reactor control")
- parallel.waitForAll(check_rods, check_timer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement