1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-10 00:43:27 +02:00
ComfyFactorio/maps/fish_defender_v1/trapped_capsules.lua

55 lines
1.5 KiB
Lua
Raw Normal View History

2021-03-24 20:14:12 +02:00
local Event = require 'utils.event'
2019-09-26 20:07:19 +02:00
local radius = 20
local whitelist = {
2021-03-24 17:46:00 +02:00
['defender'] = 'explosive-cannon-projectile',
['distractor'] = 'explosive-uranium-cannon-projectile',
['destroyer'] = 'explosive-uranium-cannon-projectile'
2019-09-26 20:07:19 +02:00
}
local function on_entity_died(event)
2021-03-24 17:46:00 +02:00
if not global.trapped_capsules_unlocked then
return
end
2019-09-26 20:07:19 +02:00
2021-03-24 17:46:00 +02:00
if not event.entity.valid then
return
end
if not whitelist[event.entity.name] then
return
end
local valid_targets = {}
local position = event.entity.position
for _, e in pairs(
event.entity.surface.find_entities_filtered({area = {{position.x - radius, position.y - radius}, {position.x + radius, position.y + radius}}, force = 'enemy'})
) do
if e.health then
local distance_from_center = math.sqrt((e.position.x - position.x) ^ 2 + (e.position.y - position.y) ^ 2)
if distance_from_center <= radius then
valid_targets[#valid_targets + 1] = e
end
end
end
2019-09-26 20:07:19 +02:00
2021-03-24 17:46:00 +02:00
if not valid_targets[1] then
return
end
2019-09-26 20:07:19 +02:00
2021-03-24 17:46:00 +02:00
event.entity.surface.create_entity(
{
name = whitelist[event.entity.name],
position = position,
force = 'player',
source = position,
target = valid_targets[math.random(1, #valid_targets)].position,
max_range = 20,
speed = 0.1
}
)
end
2021-03-24 20:14:12 +02:00
Event.add(defines.events.on_entity_died, on_entity_died)