2018-12-09 16:04:23 +02:00
|
|
|
-- Cut off a limb, and two more shall take its place!
|
|
|
|
local Event = require 'utils.event'
|
|
|
|
local CreateParticles = require 'features.create_particles'
|
2019-02-07 07:37:34 +02:00
|
|
|
local Token = require 'utils.token'
|
|
|
|
local Global = require 'utils.global'
|
2019-02-15 02:02:39 +02:00
|
|
|
local table = require 'utils.table'
|
2018-12-09 16:04:23 +02:00
|
|
|
local random = math.random
|
|
|
|
local floor = math.floor
|
|
|
|
local ceil = math.ceil
|
2018-12-10 20:28:51 +02:00
|
|
|
local pairs = pairs
|
2019-05-30 21:57:43 +02:00
|
|
|
local type = type
|
2019-02-15 02:02:39 +02:00
|
|
|
local clear_table = table.clear_table
|
2018-12-09 16:04:23 +02:00
|
|
|
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
|
2019-03-03 23:13:49 +02:00
|
|
|
local hail_hydra_conf = global.config.hail_hydra
|
2018-12-09 16:04:23 +02:00
|
|
|
|
2019-02-15 02:02:39 +02:00
|
|
|
local spawn_table = {}
|
|
|
|
for k, v in pairs(global.config.hail_hydra.hydras) do
|
|
|
|
spawn_table[k] = v
|
|
|
|
end
|
|
|
|
|
2019-03-03 23:13:49 +02:00
|
|
|
local function formula(evo)
|
|
|
|
evo = evo * 100
|
|
|
|
local value = (0.00003 * evo ^ 3 + 0.004 * evo ^ 2 + 0.3 * evo) * 0.01
|
|
|
|
return value <= 1 and value or 1
|
|
|
|
end
|
|
|
|
|
2019-02-15 02:02:39 +02:00
|
|
|
local primitives = {
|
2019-03-03 23:13:49 +02:00
|
|
|
evolution_scale = (hail_hydra_conf.evolution_scale ~= nil) and hail_hydra_conf.evolution_scale or 1,
|
|
|
|
evolution_scale_formula = formula,
|
|
|
|
online_player_scale_enabled = hail_hydra_conf.online_player_scale_enabled,
|
|
|
|
online_player_scale = hail_hydra_conf.online_player_scale,
|
2019-02-15 02:02:39 +02:00
|
|
|
enabled = nil
|
|
|
|
}
|
2019-02-07 07:37:34 +02:00
|
|
|
|
|
|
|
Global.register(
|
|
|
|
{
|
2019-02-15 02:02:39 +02:00
|
|
|
primitives = primitives,
|
|
|
|
spawn_table = spawn_table
|
2019-02-07 07:37:34 +02:00
|
|
|
},
|
|
|
|
function(tbl)
|
|
|
|
primitives = tbl.primitives
|
2019-02-15 02:02:39 +02:00
|
|
|
spawn_table = tbl.spawn_table
|
2019-02-07 07:37:34 +02:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
local Public = {}
|
|
|
|
|
2019-03-03 23:13:49 +02:00
|
|
|
local function backwards_compatibility(amount)
|
|
|
|
return {min = amount, max = amount + primitives.evolution_scale}
|
|
|
|
end
|
|
|
|
|
2018-12-09 16:04:23 +02:00
|
|
|
local function create_attack_command(position, target)
|
|
|
|
local command = {type = attack_area, destination = position, radius = 10}
|
|
|
|
if target then
|
2019-02-07 07:37:34 +02:00
|
|
|
command = {
|
|
|
|
type = compound,
|
|
|
|
structure_type = logical_or,
|
|
|
|
commands = {
|
|
|
|
{type = attack, target = target},
|
|
|
|
command
|
|
|
|
}
|
|
|
|
}
|
2018-12-09 16:04:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return command
|
|
|
|
end
|
|
|
|
|
2019-02-07 07:37:34 +02:00
|
|
|
local on_died =
|
|
|
|
Token.register(
|
|
|
|
function(event)
|
|
|
|
local entity = event.entity
|
|
|
|
local name = entity.name
|
2018-12-09 16:04:23 +02:00
|
|
|
|
2019-02-15 02:02:39 +02:00
|
|
|
local hydra = spawn_table[name]
|
2019-02-07 07:37:34 +02:00
|
|
|
if not hydra then
|
|
|
|
return
|
|
|
|
end
|
2018-12-09 16:04:23 +02:00
|
|
|
|
2019-02-07 07:37:34 +02:00
|
|
|
local position = entity.position
|
|
|
|
local force = entity.force
|
2018-12-09 16:04:23 +02:00
|
|
|
|
2019-03-03 23:13:49 +02:00
|
|
|
local num_online_players = #game.connected_players
|
|
|
|
local player_scale = 0
|
|
|
|
if primitives.online_player_scale_enabled then
|
|
|
|
player_scale = (num_online_players - primitives.online_player_scale) * 0.01
|
|
|
|
end
|
|
|
|
|
|
|
|
local evolution_scaled = primitives.evolution_scale_formula(force.evolution_factor)
|
|
|
|
local evolution_factor = evolution_scaled + evolution_scaled * player_scale
|
|
|
|
|
|
|
|
local cause = event.cause
|
2019-02-07 07:37:34 +02:00
|
|
|
local surface = entity.surface
|
|
|
|
local create_entity = surface.create_entity
|
|
|
|
local find_non_colliding_position = surface.find_non_colliding_position
|
2018-12-09 16:04:23 +02:00
|
|
|
|
2019-02-07 07:37:34 +02:00
|
|
|
local command = create_attack_command(position, cause)
|
2018-12-09 16:04:23 +02:00
|
|
|
|
2019-02-07 07:37:34 +02:00
|
|
|
for hydra_spawn, amount in pairs(hydra) do
|
2019-03-03 23:13:49 +02:00
|
|
|
if (type(amount) == 'number') then
|
|
|
|
amount = backwards_compatibility(amount)
|
|
|
|
end
|
|
|
|
local trigger = amount.trigger
|
|
|
|
if trigger == nil or trigger < force.evolution_factor then
|
|
|
|
if amount.locked then
|
|
|
|
evolution_factor = evolution_scaled
|
|
|
|
end
|
|
|
|
local min = amount.min
|
|
|
|
local max = amount.max
|
|
|
|
max = (max ~= nil and max >= min) and max or min
|
|
|
|
if max ~= min then
|
|
|
|
amount = (max - min) * (evolution_factor / primitives.evolution_scale_formula(1)) + min
|
|
|
|
else
|
|
|
|
amount = min
|
|
|
|
end
|
|
|
|
else
|
|
|
|
amount = 0
|
|
|
|
end
|
|
|
|
amount = (amount > 0) and amount or 0
|
2018-12-09 16:04:23 +02:00
|
|
|
|
2019-02-07 07:37:34 +02:00
|
|
|
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
|
2018-12-13 21:59:00 +02:00
|
|
|
|
2019-02-07 07:37:34 +02:00
|
|
|
if amount > 4 then
|
|
|
|
particle_count = 60
|
2018-12-09 16:04:23 +02:00
|
|
|
else
|
2019-02-07 07:37:34 +02:00
|
|
|
particle_count = amount * 15
|
2018-12-09 16:04:23 +02:00
|
|
|
end
|
|
|
|
|
2019-02-07 07:37:34 +02:00
|
|
|
CreateParticles.blood_explosion(create_entity, particle_count, position)
|
2018-12-09 16:04:23 +02:00
|
|
|
|
2019-02-07 07:37:34 +02:00
|
|
|
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
|
2018-12-09 16:04:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-02-07 07:37:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
local function register_event()
|
|
|
|
if not primitives.enabled then
|
|
|
|
Event.add_removable(defines.events.on_entity_died, on_died)
|
|
|
|
primitives.enabled = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-15 02:02:39 +02:00
|
|
|
--- Enables hail_hydra
|
2019-02-07 07:37:34 +02:00
|
|
|
function Public.enable_hail_hydra()
|
|
|
|
register_event()
|
|
|
|
end
|
|
|
|
|
2019-02-15 02:02:39 +02:00
|
|
|
--- Disables hail_hydra
|
2019-02-07 07:37:34 +02:00
|
|
|
function Public.disable_hail_hydra()
|
|
|
|
if primitives.enabled then
|
|
|
|
Event.remove_removable(defines.events.on_entity_died, on_died)
|
|
|
|
primitives.enabled = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-15 02:02:39 +02:00
|
|
|
--- Sets the evolution scale
|
|
|
|
-- @param scale <number>
|
|
|
|
function Public.set_evolution_scale(scale)
|
|
|
|
primitives.evolution_scale = scale
|
|
|
|
end
|
|
|
|
|
2019-03-03 23:13:49 +02:00
|
|
|
--- Sets the online player scale
|
|
|
|
-- @param scale <number>
|
|
|
|
function Public.set_evolution_scale(scale)
|
|
|
|
primitives.online_player_scale = scale
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Toggles the online player scale feature
|
|
|
|
-- @param enable <boolean>
|
|
|
|
function Public.set_evolution_scale(enabled)
|
|
|
|
primitives.online_player_scale_enabled = enabled
|
|
|
|
end
|
|
|
|
|
2019-02-15 02:02:39 +02:00
|
|
|
--- Sets the hydra spawning table
|
|
|
|
-- @param hydras <table> see config.lua's hail_hydra section for example
|
|
|
|
function Public.set_hydras(hydras)
|
|
|
|
clear_table(spawn_table)
|
|
|
|
for k, v in pairs(hydras) do
|
|
|
|
spawn_table[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Adds to/overwrites parts of the hydra spawning table
|
|
|
|
-- @param hydras <table> see config.lua's hail_hydra section for example
|
|
|
|
function Public.add_hydras(hydras)
|
|
|
|
for k, v in pairs(hydras) do
|
|
|
|
spawn_table[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if global.config.hail_hydra.enabled then
|
|
|
|
register_event()
|
|
|
|
end
|
|
|
|
|
2019-02-07 07:37:34 +02:00
|
|
|
return Public
|