Teardown/teardown/on_tick.lua

48 lines
1.4 KiB
Lua

local next = next
function on_active_tick(event)
local active_mining_drills = global.active.mining_drills
local passive_mining_drills = global.passive.mining_drills
local miners = table.remove_key(active_mining_drills, event.tick) or {}
for _, miner in pairs(miners) do
if miner.valid then
local status = miner.status
if status == defines.entity_status.no_minable_resources then
miner.order_deconstruction(miner.force, nil)
elseif status ~= defines.entity_status.working then
passive_mining_drills[miner.unit_number] = miner
script.on_nth_tick(passive_tick, on_passive_tick)
end
end
end
if next(active_mining_drills) == nil then
script.on_nth_tick(active_tick, nil)
return
end
end
function on_passive_tick(event)
local passive_mining_drills = global.passive.mining_drills
for id, miner in pairs(passive_mining_drills) do
if miner.valid then
local status = miner.status
if status == defines.entity_status.no_minable_resources then
miner.order_deconstruction(miner.force, nil)
passive_mining_drills[id] = nil
elseif status == defines.entity_status.working then
passive_mining_drills[id] = nil
end
else
passive_mining_drills[id] = nil
end
end
if next(passive_mining_drills) == nil then
script.on_nth_tick(passive_tick, nil)
return
end
end