mirror of
https://github.com/Refactorio/RedMew.git
synced 2024-12-14 10:13:13 +02:00
127 lines
3.4 KiB
Lua
127 lines
3.4 KiB
Lua
-- Cut off a limb, and two more shall take its place!
|
|
local Event = require 'utils.event'
|
|
local CreateParticles = require 'features.create_particles'
|
|
local Token = require 'utils.token'
|
|
local Global = require 'utils.global'
|
|
|
|
local random = math.random
|
|
local floor = math.floor
|
|
local ceil = math.ceil
|
|
local pairs = pairs
|
|
local compound = defines.command.compound
|
|
local logical_or = defines.compound_command.logical_or
|
|
local attack = defines.command.attack
|
|
local attack_area = defines.command.attack_area
|
|
local config = global.config.hail_hydra
|
|
local evolution_scale = config.evolution_scale
|
|
local hydras = config.hydras
|
|
|
|
local primitives = {enabled = nil}
|
|
|
|
Global.register(
|
|
{
|
|
primitives = primitives
|
|
},
|
|
function(tbl)
|
|
primitives = tbl.primitives
|
|
end
|
|
)
|
|
|
|
local Public = {}
|
|
|
|
local function create_attack_command(position, target)
|
|
local command = {type = attack_area, destination = position, radius = 10}
|
|
if target then
|
|
command = {
|
|
type = compound,
|
|
structure_type = logical_or,
|
|
commands = {
|
|
{type = attack, target = target},
|
|
command
|
|
}
|
|
}
|
|
end
|
|
|
|
return command
|
|
end
|
|
|
|
local on_died =
|
|
Token.register(
|
|
function(event)
|
|
local entity = event.entity
|
|
local name = entity.name
|
|
|
|
local hydra = hydras[name]
|
|
if not hydra then
|
|
return
|
|
end
|
|
|
|
local position = entity.position
|
|
local force = entity.force
|
|
local evolution_factor = force.evolution_factor * evolution_scale
|
|
local cause = event.cause
|
|
|
|
local surface = entity.surface
|
|
local create_entity = surface.create_entity
|
|
local find_non_colliding_position = surface.find_non_colliding_position
|
|
|
|
local command = create_attack_command(position, cause)
|
|
|
|
for hydra_spawn, amount in pairs(hydra) do
|
|
amount = amount + evolution_factor
|
|
|
|
local extra_chance = amount % 1
|
|
if extra_chance > 0 then
|
|
if random() <= extra_chance then
|
|
amount = ceil(amount)
|
|
else
|
|
amount = floor(amount)
|
|
end
|
|
end
|
|
local particle_count
|
|
|
|
if amount > 4 then
|
|
particle_count = 60
|
|
else
|
|
particle_count = amount * 15
|
|
end
|
|
|
|
CreateParticles.blood_explosion(create_entity, particle_count, position)
|
|
|
|
for _ = amount, 1, -1 do
|
|
position = find_non_colliding_position(hydra_spawn, position, 2, 0.4) or position
|
|
local spawned = create_entity({name = hydra_spawn, force = force, position = position})
|
|
if spawned and spawned.type == 'unit' then
|
|
spawned.set_command(command)
|
|
elseif spawned and cause and cause.valid and cause.force then
|
|
spawned.shooting_target = cause
|
|
end
|
|
end
|
|
end
|
|
end
|
|
)
|
|
|
|
local function register_event()
|
|
if not primitives.enabled then
|
|
Event.add_removable(defines.events.on_entity_died, on_died)
|
|
primitives.enabled = true
|
|
end
|
|
end
|
|
|
|
if config.enabled then
|
|
register_event()
|
|
end
|
|
|
|
function Public.enable_hail_hydra()
|
|
register_event()
|
|
end
|
|
|
|
function Public.disable_hail_hydra()
|
|
if primitives.enabled then
|
|
Event.remove_removable(defines.events.on_entity_died, on_died)
|
|
primitives.enabled = nil
|
|
end
|
|
end
|
|
|
|
return Public
|