Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix restart bottlenecks #1428

Merged
merged 1 commit into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions map_gen/maps/frontier/modules/enemy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ local Color = require 'resources.color_presets'
local EnemyTurret = require 'features.enemy_turret'
local math = require 'utils.math'
local PriceRaffle = require 'features.price_raffle'
local RS = require 'map_gen.shared.redmew_surface'
local Table = require 'utils.table'
local Toast = require 'features.gui.toast'
local Token = require 'utils.token'
local Debug = require 'map_gen.maps.frontier.shared.debug'
local Public = require 'map_gen.maps.frontier.shared.core'
local this = Public.get()

local register_on_entity_destroyed = script.register_on_entity_destroyed
local math_ceil = math.ceil
local math_clamp = math.clamp
Expand Down Expand Up @@ -53,7 +52,7 @@ Enemy.commands = {
distraction = defines.distraction.by_enemy
}
unit_group.start_moving()
if this._DEBUG_AI then
if Public.get()._DEBUG_AI then
Debug.print_admins(string.format('AI [id=%d] | cmd: MOVE [gps=%.2f,%.2f,%s]', unit_group.group_number, position.x, position.y, unit_group.surface.name), Color.dark_gray)
end
end,
Expand All @@ -72,7 +71,7 @@ Enemy.commands = {
distraction = defines.distraction.by_enemy
}
unit_group.start_moving()
if this._DEBUG_AI then
if Public.get()._DEBUG_AI then
Debug.print_admins(string.format('AI [id=%d] | cmd: SCOUT [gps=%.2f,%.2f,%s]', unit_group.group_number, position.x, position.y, unit_group.surface.name), Color.dark_gray)
end
end,
Expand All @@ -89,7 +88,7 @@ Enemy.commands = {
target = target,
distraction = defines.distraction.by_damage
}
if this._DEBUG_AI then
if Public.get()._DEBUG_AI then
Debug.print_admins(string.format('AI [id=%d] | cmd: ATTACK [gps=%.2f,%.2f,%s] (type = %s)', unit_group.group_number, target.position.x, target.position.y, unit_group.surface.name, target.type), Color.dark_gray)
end
end
Expand All @@ -104,6 +103,7 @@ Enemy.stages = {
}

function Enemy.ai_take_control(unit_group)
local this = Public.get()
if not this.unit_groups[unit_group.group_number] then
this.unit_groups[unit_group.group_number] = {
unit_group = unit_group
Expand All @@ -130,6 +130,7 @@ function Enemy.ai_processor(unit_group, result)
return
end

local this = Public.get()
local data = this.unit_groups[unit_group.group_number]
if not data then
return
Expand Down Expand Up @@ -186,10 +187,11 @@ function Enemy.ai_processor(unit_group, result)
end

function Enemy.spawn_enemy_wave(position)
local surface = RS.get_surface()
local surface = Public.surface()
local find_position = surface.find_non_colliding_position
local spawn = surface.create_entity
local current_tick = game.tick
local this = Public.get()

local unit_group = surface.create_unit_group { position = position, force = 'enemy' }

Expand Down Expand Up @@ -239,6 +241,7 @@ Enemy.spawn_enemy_wave_token = Token.register(Enemy.spawn_enemy_wave)

function Enemy.on_enemy_died(entity)
local uid = entity.unit_number
local this = Public.get()
local data = this.invincible[uid]
if not data then
return
Expand Down Expand Up @@ -270,7 +273,7 @@ end

function Enemy.on_spawner_died(event)
local entity = event.entity

local this = Public.get()
local budget = this.loot_budget + entity.position.x * 2.75
budget = budget * math_random(25, 175) * 0.01

Expand Down Expand Up @@ -300,6 +303,7 @@ function Enemy.on_spawner_died(event)
end

function Enemy.spawn_turret_outpost(position)
local this = Public.get()
if position.x < this.right_boundary * 32 + this.wall_width then
return
end
Expand All @@ -309,7 +313,7 @@ function Enemy.spawn_turret_outpost(position)
return
end

local surface = RS.get_surface()
local surface = Public.surface()

if Public.ESCAPE_PLAYER then
for _, player in pairs(surface.find_entities_filtered{type = 'character'}) do
Expand Down Expand Up @@ -361,19 +365,19 @@ function Enemy.start_tracking(entity)
end

register_on_entity_destroyed(entity)
this.target_entities[entity.unit_number] = entity
Public.get().target_entities[entity.unit_number] = entity
end

function Enemy.stop_tracking(entity)
this.target_entities[entity.unit_number] = nil
Public.get().target_entities[entity.unit_number] = nil
end

function Enemy.get_target()
return Table.get_random_dictionary_entry(this.target_entities, false)
return Table.get_random_dictionary_entry(Public.get().target_entities, false)
end

function Enemy.nuclear_explosion(position)
RS.get_surface().create_entity {
Public.surface().create_entity {
name = 'atomic-rocket',
position = position,
force = 'enemy',
Expand Down
21 changes: 18 additions & 3 deletions map_gen/maps/frontier/modules/lobby.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local Config = global.config
local Public = require 'map_gen.maps.frontier.shared.core'
local this = Public.get()

local Lobby = {}

Expand Down Expand Up @@ -40,7 +39,7 @@ function Lobby.teleport_from(player, destination)
player.insert(stack)
end
end
local surface = this.surface
local surface = Public.surface()
local position = surface.find_non_colliding_position('character', destination or {0, 0}, 0, 0.2)
player.teleport(position, surface, true)
end
Expand All @@ -61,18 +60,34 @@ function Lobby.on_chunk_generated(event)
local area = event.area
local surface = event.surface

if surface.name ~= Lobby.name then
return
end

surface.build_checkerboard(area)
for _, e in pairs(surface.find_entities_filtered{ area = area }) do
if e.type ~= 'character' then
e.destroy()
end
end

local lt = area.left_top
local w, h = Lobby.mgs.width / 2, Lobby.mgs.height / 2
local tiles = {}
for x = lt.x, lt.x + 31 do
for y = lt.y, lt.y + 31 do
if x > w or x < -w or y > h or y < -h then
tiles[#tiles +1] = {name = 'out-of-map', position = { x = x, y = y }}
end
end
end
surface.set_tiles(tiles, false)
end

function Lobby.on_init()
local surface = Lobby.get_surface()
surface.map_gen_settings = Lobby.mgs
Lobby.on_chunk_generated({ area = {left_top = {-64, -64}, right_bottom = {64, 64}}, surface = surface })
Lobby.on_chunk_generated({ area = {left_top = { x = -Lobby.mgs.width, y = -Lobby.mgs.height }, right_bottom = { x = Lobby.mgs.width, y = Lobby.mgs.height }}, surface = surface })
end

return Lobby
6 changes: 3 additions & 3 deletions map_gen/maps/frontier/modules/market.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
local math = require 'utils.math'
local PriceRaffle = require 'features.price_raffle'
local RS = require 'map_gen.shared.redmew_surface'
local Table = require 'utils.table'
local Public = require 'map_gen.maps.frontier.shared.core'
local this = Public.get()
local math_ceil = math.ceil
local math_clamp = math.clamp
local math_min = math.min
Expand All @@ -17,6 +15,7 @@ Market.cheap_items = {}
Market.expensive_items = {}

do
local this = Public.get()
local market_items = PriceRaffle.get_items_worth()
for k, _ in pairs(this.banned_items) do
market_items[k] = nil
Expand All @@ -38,6 +37,7 @@ do
end

function Market.spawn_exchange_market(position)
local this = Public.get()
if position.x < this.left_boundary * 32 + this.wall_width then
return
end
Expand All @@ -46,7 +46,7 @@ function Market.spawn_exchange_market(position)
return
end

local surface = RS.get_surface()
local surface = Public.surface()
local market = surface.create_entity {
name = 'market',
position = position,
Expand Down
24 changes: 11 additions & 13 deletions map_gen/maps/frontier/modules/rocket_silo.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
local Color = require 'resources.color_presets'
local math = require 'utils.math'
local RS = require 'map_gen.shared.redmew_surface'
local ScoreTracker = require 'utils.score_tracker'
local Sounds = require 'utils.sounds'
local Token = require 'utils.token'
local Task = require 'utils.task'
local Public = require 'map_gen.maps.frontier.shared.core'
local Enemy = require 'map_gen.maps.frontier.modules.enemy'
local Terrain = require 'map_gen.maps.frontier.modules.terrain'
local this = Public.get()
local math_abs = math.abs
local math_ceil = math.ceil
local math_clamp = math.clamp
Expand Down Expand Up @@ -110,7 +108,8 @@ end
RocketSilo.bard_message_token = Token.register(RocketSilo.bard_message)

function RocketSilo.move_silo(position)
local surface = RS.get_surface()
local this = Public.get()
local surface = Public.surface()
local old_silo = this.rocket_silo
local old_position = old_silo and old_silo.position or { x = 0, y = 0 }
local new_silo
Expand Down Expand Up @@ -198,6 +197,7 @@ end
RocketSilo.move_silo_token = Token.register(RocketSilo.move_silo)

function RocketSilo.compute_silo_coordinates(step)
local this = Public.get()
this.move_buffer = this.move_buffer + (step or 0)

if this.x + this.move_buffer > this.max_distance then
Expand Down Expand Up @@ -237,20 +237,16 @@ function RocketSilo.compute_silo_coordinates(step)
end

function RocketSilo.reveal_spawn_area()
local surface = RS.get_surface()
local far_left, far_right = this.kraken_distance + this.left_boundary * 32 + 1, this.right_boundary * 32 + this.wall_width
surface.request_to_generate_chunks({ x = 0, y = 0 }, math.ceil(math_max(far_left, far_right, this.height * 32) / 32))
local surface = Public.surface()
surface.request_to_generate_chunks({ x = 0, y = 0 }, 1)
surface.force_generate_chunk_requests()

RocketSilo.compute_silo_coordinates(this.silo_starting_x + math_random(100))
RocketSilo.compute_silo_coordinates(Public.get().silo_starting_x + math_random(100))
RocketSilo.move_silo()
Terrain.create_wall(this.right_boundary * 32, this.wall_width)

game.forces.player.chart(surface, { { -far_left - 32, -this.height * 16 }, { far_right + 32, this.height * 16 } })
end

function RocketSilo.set_game_state(player_won)
this.scenario_finished = true
Public.get().scenario_finished = true
game.set_game_state {
game_finished = true,
player_won = player_won or false,
Expand Down Expand Up @@ -280,6 +276,7 @@ function RocketSilo.on_game_started()
ms.pollution.diffusion_ratio = 0.04 -- 0.02
ms.pollution.enemy_attack_pollution_consumption_modifier = 0.5 --1

local this = Public.get()
this.rounds = this.rounds + 1
this.kraken_contributors = {}
this.death_contributions = {}
Expand Down Expand Up @@ -321,10 +318,10 @@ RocketSilo.restart_game_token = Token.register(function()
end)

function RocketSilo.on_game_finished()
this.lobby_enabled = true
Public.get().lobby_enabled = true
game.print({'frontier.map_setup'})

local surface = RS.get_surface()
local surface = Public.surface()
surface.clear(true)
local mgs = table.deepcopy(surface.map_gen_settings)
mgs.seed = mgs.seed + 1e4
Expand All @@ -341,6 +338,7 @@ function RocketSilo.on_rocket_launched(event)
return
end

local this = Public.get()
if this.scenario_finished then
return
end
Expand Down
Loading
Loading