2019-02-07 07:37:34 +02:00
|
|
|
local Task = require 'utils.task'
|
|
|
|
local Token = require 'utils.token'
|
2019-02-14 19:16:05 +02:00
|
|
|
local Game = require 'utils.game'
|
2019-02-15 02:19:32 +02:00
|
|
|
local Global = require 'utils.global'
|
2019-02-17 04:24:28 +02:00
|
|
|
local Command = require 'utils.command'
|
2019-02-07 07:37:34 +02:00
|
|
|
local Toast = require 'features.gui.toast'
|
|
|
|
local RS = require 'map_gen.shared.redmew_surface'
|
2019-02-15 02:19:32 +02:00
|
|
|
local Color = require 'resources.color_presets'
|
2019-02-17 04:24:28 +02:00
|
|
|
local Ranks = require 'resources.ranks'
|
2019-06-26 14:19:08 +02:00
|
|
|
local Event = require 'utils.event'
|
|
|
|
local random = math.random
|
|
|
|
local Config = require 'config'
|
2019-02-07 07:37:34 +02:00
|
|
|
|
2019-06-26 14:19:08 +02:00
|
|
|
local duplicate_chance = Config.apocalypse.duplicate_chance
|
2019-02-07 07:37:34 +02:00
|
|
|
|
2019-02-15 02:19:32 +02:00
|
|
|
-- Local var
|
|
|
|
local Public = {}
|
|
|
|
|
|
|
|
-- Global vars
|
|
|
|
local second_run = {}
|
|
|
|
local primitives = {
|
|
|
|
apocalypse_now = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
Global.register(
|
|
|
|
{
|
|
|
|
primitives = primitives,
|
|
|
|
second_run = second_run
|
|
|
|
},
|
|
|
|
function(tbl)
|
|
|
|
primitives = tbl.primitives
|
|
|
|
second_run = tbl.second_run
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2019-06-26 14:19:08 +02:00
|
|
|
local name_map = {['behemoth-biter'] = true, ['behemoth-spitter'] = true}
|
|
|
|
|
|
|
|
local biter_died_token =
|
|
|
|
Token.register(
|
|
|
|
function(event)
|
|
|
|
local entity = event.entity
|
|
|
|
if not entity.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local name = entity.name
|
|
|
|
if not name_map[name] then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local force_name = entity.force.name
|
|
|
|
if force_name ~= 'enemy' then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local surface = entity.surface
|
|
|
|
local create_entity = surface.create_entity
|
|
|
|
local position = entity.position
|
|
|
|
local spawn = {name = entity.name, force = 'enemy', position = position}
|
|
|
|
|
|
|
|
create_entity(spawn)
|
|
|
|
|
|
|
|
if random() > duplicate_chance then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local spawn_position = surface.find_non_colliding_position(name, position, 8, 1)
|
|
|
|
if not spawn_position then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
spawn.position = spawn_position
|
|
|
|
create_entity(spawn)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
local aliens = {
|
|
|
|
'behemoth-biter',
|
|
|
|
'behemoth-biter',
|
|
|
|
'behemoth-spitter',
|
|
|
|
'behemoth-spitter'
|
|
|
|
}
|
|
|
|
|
2019-02-07 07:37:34 +02:00
|
|
|
local biter_spawn_token =
|
|
|
|
Token.register(
|
2019-02-15 02:02:39 +02:00
|
|
|
function()
|
|
|
|
local surface
|
|
|
|
local player_force
|
|
|
|
local enemy_force = game.forces.enemy
|
|
|
|
|
|
|
|
surface = RS.get_surface()
|
|
|
|
player_force = game.forces.player
|
|
|
|
|
|
|
|
enemy_force.evolution_factor = 1
|
|
|
|
|
|
|
|
local p_spawn = player_force.get_spawn_position(surface)
|
|
|
|
local group = surface.create_unit_group {position = p_spawn}
|
2019-02-07 07:37:34 +02:00
|
|
|
|
|
|
|
local create_entity = surface.create_entity
|
|
|
|
|
2019-02-08 21:19:50 +02:00
|
|
|
for i = 1, #aliens do
|
2019-02-07 07:37:34 +02:00
|
|
|
local spawn_pos = surface.find_non_colliding_position('behemoth-biter', p_spawn, 300, 1)
|
|
|
|
if spawn_pos then
|
2019-02-15 02:02:39 +02:00
|
|
|
local biter = create_entity({name = aliens[i], position = spawn_pos})
|
2019-02-07 07:37:34 +02:00
|
|
|
group.add_member(biter)
|
|
|
|
end
|
|
|
|
end
|
2019-02-08 21:19:50 +02:00
|
|
|
|
2019-02-07 07:37:34 +02:00
|
|
|
group.set_command({type = defines.command.attack_area, destination = {0, 0}, radius = 500})
|
2019-02-15 02:19:32 +02:00
|
|
|
Toast.toast_all_players(500, {'apocalypse.toast_message'})
|
2019-06-26 14:19:08 +02:00
|
|
|
|
|
|
|
Event.add_removable(defines.events.on_entity_died, biter_died_token)
|
2019-02-07 07:37:34 +02:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2019-02-15 02:19:32 +02:00
|
|
|
--- Begins the apocalypse
|
|
|
|
function Public.begin_apocalypse(_, player)
|
|
|
|
local index
|
|
|
|
if player and player.valid then
|
|
|
|
index = player.index
|
|
|
|
elseif not player then
|
|
|
|
index = 0
|
2019-02-14 19:16:05 +02:00
|
|
|
end
|
|
|
|
|
2019-02-16 23:44:25 +02:00
|
|
|
-- Check if the apocalypse is happening or if it's the first run of the command.
|
|
|
|
if primitives.apocalypse_now then
|
|
|
|
Game.player_print({'apocalypse.apocalypse_already_running'}, Color.yellow)
|
|
|
|
return
|
|
|
|
elseif not second_run[index] then
|
2019-02-15 02:19:32 +02:00
|
|
|
second_run[index] = true
|
|
|
|
game.server_save('pre-apocalypse-' .. index)
|
|
|
|
Game.player_print({'apocalypse.run_twice'})
|
2019-02-07 07:37:34 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-02-15 02:19:32 +02:00
|
|
|
primitives.apocalypse_now = true
|
|
|
|
game.print({'apocalypse.apocalypse_begins'}, Color.pink)
|
2019-06-26 14:19:08 +02:00
|
|
|
Task.set_timeout(1, biter_spawn_token, {})
|
2019-02-07 07:37:34 +02:00
|
|
|
end
|
|
|
|
|
2019-02-17 04:24:28 +02:00
|
|
|
Command.add(
|
|
|
|
'apocalypse',
|
|
|
|
{
|
2019-03-04 00:13:56 +02:00
|
|
|
description = {'command_description.apocalypse'},
|
|
|
|
required_rank = Ranks.admin
|
2019-02-17 04:24:28 +02:00
|
|
|
},
|
|
|
|
Public.begin_apocalypse
|
|
|
|
)
|
|
|
|
|
2019-02-08 22:19:30 +02:00
|
|
|
return Public
|