1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-30 04:30:58 +02:00

Implement as guards to reduce nesting

This commit is contained in:
Russel Delainey 2019-06-04 14:25:34 -06:00
parent 8b79bc8024
commit 77d3a581f3

View File

@ -8,39 +8,43 @@ local random = math.random
local function biter_died(event)
local entity = event.entity
if entity.valid then
local dying_force = entity.force
-- ignore player owned entities
if dying_force == 'player' then
return
end
if not entity.valid then
return
end
local surface = entity.surface
local dying_force = entity.force
-- ignore player owned entities
if dying_force == 'player' then
return
end
-- More than the desired number of corpses?
if surface.count_entities_filtered {
position = entity.position,
radius = biter_utils_conf.radius,
type = 'corpse',
force = 'neutral'
} > biter_utils_conf.corpse_threshold then
-- Get the actual entities
local corpse_list = surface.find_entities_filtered {
position = entity.position,
radius = biter_utils_conf.radius,
type = 'corpse',
force = 'neutral'
}
local surface = entity.surface
local num_to_remove = #corpse_list - biter_utils_conf.corpse_threshold
local random_offset = random(#corpse_list)
-- More than the desired number of corpses?
if not (surface.count_entities_filtered {
position = entity.position,
radius = biter_utils_conf.radius,
type = 'corpse',
force = 'neutral'
} > biter_utils_conf.corpse_threshold) then
return
end
-- 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
corpse_list[(i % #corpse_list) + 1].destroy()
end
end
-- Get the actual entities
local corpse_list = surface.find_entities_filtered {
position = entity.position,
radius = biter_utils_conf.radius,
type = 'corpse',
force = 'neutral'
}
local num_to_remove = #corpse_list - biter_utils_conf.corpse_threshold
local random_offset = random(#corpse_list)
-- 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
corpse_list[(i % #corpse_list) + 1].destroy()
end
end