2019-06-04 21:07:43 +02:00
|
|
|
local Event = require 'utils.event'
|
|
|
|
|
|
|
|
local biter_utils_conf = global.config.biter_corpse_util
|
|
|
|
|
|
|
|
local random = math.random
|
|
|
|
|
|
|
|
-- currently no on_corpse_spawned event, using on_entity_died instead
|
|
|
|
|
|
|
|
local function biter_died(event)
|
|
|
|
local entity = event.entity
|
2019-06-04 22:25:34 +02:00
|
|
|
if not entity.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-06-04 22:40:33 +02:00
|
|
|
-- ignore non-enemy entities
|
|
|
|
if entity.force.name ~= 'enemy' then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Only trigger on units
|
|
|
|
if entity.type ~= 'unit' then
|
2019-06-04 22:25:34 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local surface = entity.surface
|
|
|
|
|
2019-06-04 22:50:09 +02:00
|
|
|
local filter = {
|
|
|
|
position = entity.position,
|
|
|
|
radius = biter_utils_conf.radius,
|
|
|
|
type = 'corpse',
|
|
|
|
force = 'neutral'
|
|
|
|
}
|
|
|
|
|
2019-06-04 22:25:34 +02:00
|
|
|
-- More than the desired number of corpses?
|
2019-06-04 22:50:09 +02:00
|
|
|
if not (surface.count_entities_filtered(filter) > biter_utils_conf.corpse_threshold) then
|
2019-06-04 22:25:34 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Get the actual entities
|
2019-06-04 22:50:09 +02:00
|
|
|
local corpse_list = surface.find_entities_filtered (filter)
|
2019-06-04 22:25:34 +02:00
|
|
|
|
2019-06-04 22:41:19 +02:00
|
|
|
local corpse_list_len = #corpse_list
|
|
|
|
local num_to_remove = corpse_list_len - biter_utils_conf.corpse_threshold
|
|
|
|
local random_offset = random(corpse_list_len)
|
2019-06-04 22:25:34 +02:00
|
|
|
|
|
|
|
-- Starting at a random number, remove enough entities to be under the threshold
|
|
|
|
for i = random_offset, num_to_remove + random_offset do
|
|
|
|
--modulus + 1 to ensure we are not past the end of the table
|
2019-06-04 22:41:19 +02:00
|
|
|
corpse_list[(i % corpse_list_len) + 1].destroy()
|
2019-06-04 21:07:43 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Event.add(defines.events.on_entity_died, biter_died)
|