Teardown/teardown/on_resource_depleted.lua

49 lines
1.2 KiB
Lua
Raw Permalink Normal View History

2024-09-15 08:10:53 +00:00
function on_resource_depleted(event)
local entity = event.entity
if entity.initial_amount ~= nil then
-- Infinite Resource
return
end
local surface = entity.surface
local position = entity.position
local category = entity.prototype.resource_category
local radius = global.max_mining_drill_radius[category]
if radius == nil then
log("max_mining_drill_radius: " .. serpent.block(global.max_mining_drill_radius))
error("max_mining_drill_radius is not set for category: " .. category)
return
end
local miners = surface.find_entities_filtered({
area = fmsl.box.center_radius(position, radius),
type = "mining-drill",
to_be_deconstructed = false,
})
-- two tick delay for dropping product and finding a new resource node
local target_tick = event.tick + 2
local add_table = false
local active_miners = table.remove_key(global.active.mining_drills, target_tick) or {}
for _, miner in pairs(miners) do
if miner.has_flag("hidden") then
goto miner_continue
end
add_table = true
table.insert(active_miners, miner)
::miner_continue::
end
if add_table then
global.active.mining_drills[target_tick] = active_miners
script.on_nth_tick(active_tick, on_active_tick)
end
end