2018-10-03 22:11:24 +02:00
|
|
|
--[[-- info
|
|
|
|
Provides the ability to spawn aliens.
|
|
|
|
]]
|
|
|
|
|
|
|
|
-- dependencies
|
|
|
|
local Event = require 'utils.event'
|
|
|
|
local AlienEvolutionProgress = require 'map_gen.Diggy.AlienEvolutionProgress'
|
|
|
|
local Debug = require 'map_gen.Diggy.Debug'
|
|
|
|
local Template = require 'map_gen.Diggy.Template'
|
2018-10-11 19:43:04 +02:00
|
|
|
local insert = table.insert
|
|
|
|
local random = math.random
|
2018-10-03 22:11:24 +02:00
|
|
|
|
|
|
|
-- this
|
2018-10-06 00:00:00 +02:00
|
|
|
local AlienSpawner = {}
|
2018-10-03 22:11:24 +02:00
|
|
|
|
|
|
|
local function spawn_alien(surface, x, y)
|
|
|
|
local enemy_force = game.forces.enemy
|
|
|
|
local enemy_force_evolution = enemy_force.evolution_factor
|
|
|
|
local position = {x = x, y = y}
|
2018-10-11 19:43:04 +02:00
|
|
|
local biters = AlienEvolutionProgress.getBitersByEvolution(random(1, 2), enemy_force_evolution)
|
|
|
|
local spitters = AlienEvolutionProgress.getSpittersByEvolution(random(1, 2), enemy_force_evolution)
|
2018-10-03 22:11:24 +02:00
|
|
|
|
|
|
|
local units = {}
|
|
|
|
for name, amount in pairs(biters) do
|
2018-10-11 19:43:04 +02:00
|
|
|
insert(units, {name = name, position = position, force = enemy_force, amount = amount})
|
2018-10-03 22:11:24 +02:00
|
|
|
end
|
|
|
|
for name, amount in pairs(spitters) do
|
2018-10-11 19:43:04 +02:00
|
|
|
insert(units, {name = name, position = position, force = enemy_force, amount = amount})
|
2018-10-03 22:11:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
Template.units(surface, units, 3)
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
Registers all event handlers.
|
|
|
|
]]
|
2018-10-07 17:05:59 +02:00
|
|
|
function AlienSpawner.register(config)
|
2018-10-03 22:11:24 +02:00
|
|
|
local alien_minimum_distance_square = config.alien_minimum_distance ^ 2
|
|
|
|
|
|
|
|
Event.add(Template.events.on_void_removed, function(event)
|
|
|
|
local x = event.old_tile.position.x
|
|
|
|
local y = event.old_tile.position.y
|
|
|
|
|
2018-10-11 19:43:04 +02:00
|
|
|
if (x^2 + y^2 < alien_minimum_distance_square or config.alien_probability < random()) then
|
2018-10-03 22:11:24 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
spawn_alien(event.surface, x, y)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2018-10-07 17:05:59 +02:00
|
|
|
function AlienSpawner.get_extra_map_info(config)
|
|
|
|
return [[Alien Spawner, aliens might spawn when mining!
|
|
|
|
Spawn chance: ]] .. (config.alien_probability * 100) .. [[%
|
|
|
|
Minimum spawn distance: ]] .. config.alien_minimum_distance .. ' tiles'
|
|
|
|
end
|
|
|
|
|
2018-10-06 00:00:00 +02:00
|
|
|
return AlienSpawner
|