1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-02-07 13:31:40 +02:00

194 lines
6.1 KiB
Lua
Raw Normal View History

2020-05-17 12:23:55 +02:00
local Server = require 'utils.server'
local Public = require 'maps.mountain_fortress_v3.table'
2023-09-20 21:37:45 +02:00
local Event = require 'utils.event'
2020-05-17 12:23:55 +02:00
local mapkeeper = '[color=blue]Mapkeeper:[/color]'
local function reset_forces(new_surface, old_surface)
for _, f in pairs(game.forces) do
local spawn = {
x = game.forces.player.get_spawn_position(old_surface).x,
y = game.forces.player.get_spawn_position(old_surface).y
}
f.reset()
f.reset_evolution()
f.set_spawn_position(spawn, new_surface)
end
for _, tech in pairs(game.forces.player.technologies) do
tech.researched = false
2020-05-20 09:09:39 +02:00
game.forces.player.set_saved_technology_progress(tech, 0)
2020-05-17 12:23:55 +02:00
end
end
local function teleport_players(surface)
local adjusted_zones = Public.get('adjusted_zones')
local position
2021-06-06 20:14:26 +02:00
if adjusted_zones.reversed then
game.forces.player.set_spawn_position({-27, -25}, surface)
position = game.forces.player.get_spawn_position(surface)
if not position then
game.forces.player.set_spawn_position({-27, -25}, surface)
position = game.forces.player.get_spawn_position(surface)
end
else
2021-06-06 20:14:26 +02:00
game.forces.player.set_spawn_position({-27, 25}, surface)
position = game.forces.player.get_spawn_position(surface)
if not position then
game.forces.player.set_spawn_position({-27, 25}, surface)
position = game.forces.player.get_spawn_position(surface)
end
2021-06-06 20:14:26 +02:00
end
2020-05-17 12:23:55 +02:00
for _, player in pairs(game.connected_players) do
2021-06-06 20:14:26 +02:00
player.teleport(surface.find_non_colliding_position('character', position, 3, 0, 5), surface)
2020-05-17 12:23:55 +02:00
end
end
2023-09-20 23:33:47 +02:00
local function clear_scheduler(scheduler)
scheduler.operation = nil
scheduler.surface = nil
scheduler.remove_surface = false
scheduler.start_after = 0
2023-09-20 21:37:45 +02:00
end
local function scheduled_surface_clearing()
2023-09-20 23:33:47 +02:00
local scheduler = Public.get('scheduler')
if not scheduler.operation then
2023-09-20 21:37:45 +02:00
return
end
2023-09-20 23:33:47 +02:00
local tick = game.tick
if scheduler.start_after > tick then
2023-09-20 21:37:45 +02:00
return
end
2023-09-20 23:33:47 +02:00
local operation = scheduler.operation
if operation == 'warn' then
game.print(mapkeeper .. ' Preparing to remove old entites and clearing surface - this might lag the server a bit.')
scheduler.operation = 'player_clearing'
scheduler.start_after = tick + 100
elseif operation == 'player_clearing' then
local surface = scheduler.surface
if not surface or not surface.valid then
clear_scheduler(scheduler)
2023-09-20 21:37:45 +02:00
return
end
2023-09-20 23:33:47 +02:00
game.print(mapkeeper .. ' Removing old entities.')
local ent = surface.find_entities_filtered {force = 'player', limit = 1000}
for _, e in pairs(ent) do
if e.valid then
e.destroy()
2023-09-20 21:37:45 +02:00
end
end
2023-09-20 23:33:47 +02:00
scheduler.operation = 'clear'
scheduler.start_after = tick + 100
elseif operation == 'clear' then
local surface = scheduler.surface
if not surface or not surface.valid then
clear_scheduler(scheduler)
return
end
game.print(mapkeeper .. ' Clearing old surface.')
surface.clear()
if scheduler.remove_surface then
scheduler.operation = 'delete'
else
scheduler.operation = 'done'
end
scheduler.start_after = tick + 100
elseif operation == 'delete' then
local surface = scheduler.surface
if not surface or not surface.valid then
clear_scheduler(scheduler)
return
end
game.print(mapkeeper .. ' Deleting old surface.')
game.delete_surface(surface)
scheduler.operation = 'done'
scheduler.start_after = tick + 100
elseif operation == 'done' then
game.print(mapkeeper .. ' Done clearing old surface.')
clear_scheduler(scheduler)
2023-09-20 21:37:45 +02:00
end
end
2024-02-06 14:09:57 +01:00
function Public.soft_reset_map(old_surface, map_gen_settings)
local this = Public.get()
2020-05-17 12:23:55 +02:00
if not this.soft_reset_counter then
this.soft_reset_counter = 0
end
if not this.original_surface_name then
this.original_surface_name = old_surface.name
end
this.soft_reset_counter = this.soft_reset_counter + 1
2021-03-14 22:49:18 +01:00
local new_surface = game.create_surface(this.original_surface_name .. '_' .. tostring(this.soft_reset_counter), map_gen_settings)
2022-10-26 00:05:47 +02:00
new_surface.request_to_generate_chunks({0, 0}, 0.1)
2020-05-17 12:23:55 +02:00
new_surface.force_generate_chunk_requests()
reset_forces(new_surface, old_surface)
teleport_players(new_surface)
2024-02-07 20:02:46 +01:00
Public.equip_players(nil, true)
2020-05-17 12:23:55 +02:00
2023-09-20 21:37:45 +02:00
Public.add_schedule_to_delete_surface(true)
2020-05-17 12:23:55 +02:00
2020-06-03 20:09:00 +02:00
local radius = 512
local area = {{x = -radius, y = -radius}, {x = radius, y = radius}}
for _, entity in pairs(new_surface.find_entities_filtered {area = area, type = 'logistic-robot'}) do
entity.destroy()
end
for _, entity in pairs(new_surface.find_entities_filtered {area = area, type = 'construction-robot'}) do
entity.destroy()
end
2020-05-17 12:23:55 +02:00
local message = table.concat({mapkeeper .. ' Welcome to ', this.original_surface_name, '!'})
local message_to_discord = table.concat({'** Welcome to ', this.original_surface_name, '! **'})
if this.soft_reset_counter > 1 then
message =
table.concat(
{
mapkeeper,
' The world has been reshaped, welcome to attempt number ',
2020-05-17 12:23:55 +02:00
tostring(this.soft_reset_counter),
'!'
}
)
end
game.print(message, {r = 0.98, g = 0.66, b = 0.22})
Server.to_discord_embed(message_to_discord)
return new_surface
end
2023-09-20 21:37:45 +02:00
function Public.add_schedule_to_delete_surface(remove_surface)
2023-09-20 23:33:47 +02:00
local old_surface_index = Public.get('old_surface_index')
local surface = game.get_surface(old_surface_index)
2023-09-20 21:37:45 +02:00
if not surface or not surface.valid then
return
end
2023-09-20 23:33:47 +02:00
local tick = game.tick
2023-09-20 21:37:45 +02:00
2023-09-20 23:33:47 +02:00
local scheduler = Public.get('scheduler')
scheduler.operation = 'warn'
scheduler.surface = surface
scheduler.remove_surface = remove_surface or false
scheduler.start_after = tick + 500
2023-09-20 21:37:45 +02:00
end
Event.on_nth_tick(10, scheduled_surface_clearing)
2020-05-17 12:23:55 +02:00
return Public