1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-04 09:42:30 +02:00

Fix restart bottlenecks (#1428)

This commit is contained in:
RedRafe 2024-08-18 21:11:42 +02:00 committed by GitHub
parent 23dd8f5bf7
commit eefc8da5f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 174 additions and 108 deletions

View File

@ -2,13 +2,12 @@ local Color = require 'resources.color_presets'
local EnemyTurret = require 'features.enemy_turret' local EnemyTurret = require 'features.enemy_turret'
local math = require 'utils.math' local math = require 'utils.math'
local PriceRaffle = require 'features.price_raffle' local PriceRaffle = require 'features.price_raffle'
local RS = require 'map_gen.shared.redmew_surface'
local Table = require 'utils.table' local Table = require 'utils.table'
local Toast = require 'features.gui.toast' local Toast = require 'features.gui.toast'
local Token = require 'utils.token' local Token = require 'utils.token'
local Debug = require 'map_gen.maps.frontier.shared.debug' local Debug = require 'map_gen.maps.frontier.shared.debug'
local Public = require 'map_gen.maps.frontier.shared.core' local Public = require 'map_gen.maps.frontier.shared.core'
local this = Public.get()
local register_on_entity_destroyed = script.register_on_entity_destroyed local register_on_entity_destroyed = script.register_on_entity_destroyed
local math_ceil = math.ceil local math_ceil = math.ceil
local math_clamp = math.clamp local math_clamp = math.clamp
@ -53,7 +52,7 @@ Enemy.commands = {
distraction = defines.distraction.by_enemy distraction = defines.distraction.by_enemy
} }
unit_group.start_moving() 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) 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
end, end,
@ -72,7 +71,7 @@ Enemy.commands = {
distraction = defines.distraction.by_enemy distraction = defines.distraction.by_enemy
} }
unit_group.start_moving() 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) 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
end, end,
@ -89,7 +88,7 @@ Enemy.commands = {
target = target, target = target,
distraction = defines.distraction.by_damage 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) 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
end end
@ -104,6 +103,7 @@ Enemy.stages = {
} }
function Enemy.ai_take_control(unit_group) function Enemy.ai_take_control(unit_group)
local this = Public.get()
if not this.unit_groups[unit_group.group_number] then if not this.unit_groups[unit_group.group_number] then
this.unit_groups[unit_group.group_number] = { this.unit_groups[unit_group.group_number] = {
unit_group = unit_group unit_group = unit_group
@ -130,6 +130,7 @@ function Enemy.ai_processor(unit_group, result)
return return
end end
local this = Public.get()
local data = this.unit_groups[unit_group.group_number] local data = this.unit_groups[unit_group.group_number]
if not data then if not data then
return return
@ -186,10 +187,11 @@ function Enemy.ai_processor(unit_group, result)
end end
function Enemy.spawn_enemy_wave(position) function Enemy.spawn_enemy_wave(position)
local surface = RS.get_surface() local surface = Public.surface()
local find_position = surface.find_non_colliding_position local find_position = surface.find_non_colliding_position
local spawn = surface.create_entity local spawn = surface.create_entity
local current_tick = game.tick local current_tick = game.tick
local this = Public.get()
local unit_group = surface.create_unit_group { position = position, force = 'enemy' } local unit_group = surface.create_unit_group { position = position, force = 'enemy' }
@ -239,6 +241,7 @@ Enemy.spawn_enemy_wave_token = Token.register(Enemy.spawn_enemy_wave)
function Enemy.on_enemy_died(entity) function Enemy.on_enemy_died(entity)
local uid = entity.unit_number local uid = entity.unit_number
local this = Public.get()
local data = this.invincible[uid] local data = this.invincible[uid]
if not data then if not data then
return return
@ -270,7 +273,7 @@ end
function Enemy.on_spawner_died(event) function Enemy.on_spawner_died(event)
local entity = event.entity local entity = event.entity
local this = Public.get()
local budget = this.loot_budget + entity.position.x * 2.75 local budget = this.loot_budget + entity.position.x * 2.75
budget = budget * math_random(25, 175) * 0.01 budget = budget * math_random(25, 175) * 0.01
@ -300,6 +303,7 @@ function Enemy.on_spawner_died(event)
end end
function Enemy.spawn_turret_outpost(position) function Enemy.spawn_turret_outpost(position)
local this = Public.get()
if position.x < this.right_boundary * 32 + this.wall_width then if position.x < this.right_boundary * 32 + this.wall_width then
return return
end end
@ -309,7 +313,7 @@ function Enemy.spawn_turret_outpost(position)
return return
end end
local surface = RS.get_surface() local surface = Public.surface()
if Public.ESCAPE_PLAYER then if Public.ESCAPE_PLAYER then
for _, player in pairs(surface.find_entities_filtered{type = 'character'}) do for _, player in pairs(surface.find_entities_filtered{type = 'character'}) do
@ -361,19 +365,19 @@ function Enemy.start_tracking(entity)
end end
register_on_entity_destroyed(entity) register_on_entity_destroyed(entity)
this.target_entities[entity.unit_number] = entity Public.get().target_entities[entity.unit_number] = entity
end end
function Enemy.stop_tracking(entity) function Enemy.stop_tracking(entity)
this.target_entities[entity.unit_number] = nil Public.get().target_entities[entity.unit_number] = nil
end end
function Enemy.get_target() 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 end
function Enemy.nuclear_explosion(position) function Enemy.nuclear_explosion(position)
RS.get_surface().create_entity { Public.surface().create_entity {
name = 'atomic-rocket', name = 'atomic-rocket',
position = position, position = position,
force = 'enemy', force = 'enemy',

View File

@ -1,6 +1,5 @@
local Config = global.config local Config = global.config
local Public = require 'map_gen.maps.frontier.shared.core' local Public = require 'map_gen.maps.frontier.shared.core'
local this = Public.get()
local Lobby = {} local Lobby = {}
@ -40,7 +39,7 @@ function Lobby.teleport_from(player, destination)
player.insert(stack) player.insert(stack)
end end
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) local position = surface.find_non_colliding_position('character', destination or {0, 0}, 0, 0.2)
player.teleport(position, surface, true) player.teleport(position, surface, true)
end end
@ -61,18 +60,34 @@ function Lobby.on_chunk_generated(event)
local area = event.area local area = event.area
local surface = event.surface local surface = event.surface
if surface.name ~= Lobby.name then
return
end
surface.build_checkerboard(area) surface.build_checkerboard(area)
for _, e in pairs(surface.find_entities_filtered{ area = area }) do for _, e in pairs(surface.find_entities_filtered{ area = area }) do
if e.type ~= 'character' then if e.type ~= 'character' then
e.destroy() e.destroy()
end end
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 end
function Lobby.on_init() function Lobby.on_init()
local surface = Lobby.get_surface() local surface = Lobby.get_surface()
surface.map_gen_settings = Lobby.mgs 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 end
return Lobby return Lobby

View File

@ -1,9 +1,7 @@
local math = require 'utils.math' local math = require 'utils.math'
local PriceRaffle = require 'features.price_raffle' local PriceRaffle = require 'features.price_raffle'
local RS = require 'map_gen.shared.redmew_surface'
local Table = require 'utils.table' local Table = require 'utils.table'
local Public = require 'map_gen.maps.frontier.shared.core' local Public = require 'map_gen.maps.frontier.shared.core'
local this = Public.get()
local math_ceil = math.ceil local math_ceil = math.ceil
local math_clamp = math.clamp local math_clamp = math.clamp
local math_min = math.min local math_min = math.min
@ -17,6 +15,7 @@ Market.cheap_items = {}
Market.expensive_items = {} Market.expensive_items = {}
do do
local this = Public.get()
local market_items = PriceRaffle.get_items_worth() local market_items = PriceRaffle.get_items_worth()
for k, _ in pairs(this.banned_items) do for k, _ in pairs(this.banned_items) do
market_items[k] = nil market_items[k] = nil
@ -38,6 +37,7 @@ do
end end
function Market.spawn_exchange_market(position) function Market.spawn_exchange_market(position)
local this = Public.get()
if position.x < this.left_boundary * 32 + this.wall_width then if position.x < this.left_boundary * 32 + this.wall_width then
return return
end end
@ -46,7 +46,7 @@ function Market.spawn_exchange_market(position)
return return
end end
local surface = RS.get_surface() local surface = Public.surface()
local market = surface.create_entity { local market = surface.create_entity {
name = 'market', name = 'market',
position = position, position = position,

View File

@ -1,6 +1,5 @@
local Color = require 'resources.color_presets' local Color = require 'resources.color_presets'
local math = require 'utils.math' local math = require 'utils.math'
local RS = require 'map_gen.shared.redmew_surface'
local ScoreTracker = require 'utils.score_tracker' local ScoreTracker = require 'utils.score_tracker'
local Sounds = require 'utils.sounds' local Sounds = require 'utils.sounds'
local Token = require 'utils.token' local Token = require 'utils.token'
@ -8,7 +7,6 @@ local Task = require 'utils.task'
local Public = require 'map_gen.maps.frontier.shared.core' local Public = require 'map_gen.maps.frontier.shared.core'
local Enemy = require 'map_gen.maps.frontier.modules.enemy' local Enemy = require 'map_gen.maps.frontier.modules.enemy'
local Terrain = require 'map_gen.maps.frontier.modules.terrain' local Terrain = require 'map_gen.maps.frontier.modules.terrain'
local this = Public.get()
local math_abs = math.abs local math_abs = math.abs
local math_ceil = math.ceil local math_ceil = math.ceil
local math_clamp = math.clamp local math_clamp = math.clamp
@ -110,7 +108,8 @@ end
RocketSilo.bard_message_token = Token.register(RocketSilo.bard_message) RocketSilo.bard_message_token = Token.register(RocketSilo.bard_message)
function RocketSilo.move_silo(position) 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_silo = this.rocket_silo
local old_position = old_silo and old_silo.position or { x = 0, y = 0 } local old_position = old_silo and old_silo.position or { x = 0, y = 0 }
local new_silo local new_silo
@ -198,6 +197,7 @@ end
RocketSilo.move_silo_token = Token.register(RocketSilo.move_silo) RocketSilo.move_silo_token = Token.register(RocketSilo.move_silo)
function RocketSilo.compute_silo_coordinates(step) function RocketSilo.compute_silo_coordinates(step)
local this = Public.get()
this.move_buffer = this.move_buffer + (step or 0) this.move_buffer = this.move_buffer + (step or 0)
if this.x + this.move_buffer > this.max_distance then if this.x + this.move_buffer > this.max_distance then
@ -237,20 +237,16 @@ function RocketSilo.compute_silo_coordinates(step)
end end
function RocketSilo.reveal_spawn_area() function RocketSilo.reveal_spawn_area()
local surface = RS.get_surface() local surface = Public.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 }, 1)
surface.request_to_generate_chunks({ x = 0, y = 0 }, math.ceil(math_max(far_left, far_right, this.height * 32) / 32))
surface.force_generate_chunk_requests() 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() 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 end
function RocketSilo.set_game_state(player_won) function RocketSilo.set_game_state(player_won)
this.scenario_finished = true Public.get().scenario_finished = true
game.set_game_state { game.set_game_state {
game_finished = true, game_finished = true,
player_won = player_won or false, player_won = player_won or false,
@ -280,6 +276,7 @@ function RocketSilo.on_game_started()
ms.pollution.diffusion_ratio = 0.04 -- 0.02 ms.pollution.diffusion_ratio = 0.04 -- 0.02
ms.pollution.enemy_attack_pollution_consumption_modifier = 0.5 --1 ms.pollution.enemy_attack_pollution_consumption_modifier = 0.5 --1
local this = Public.get()
this.rounds = this.rounds + 1 this.rounds = this.rounds + 1
this.kraken_contributors = {} this.kraken_contributors = {}
this.death_contributions = {} this.death_contributions = {}
@ -321,10 +318,10 @@ RocketSilo.restart_game_token = Token.register(function()
end) end)
function RocketSilo.on_game_finished() function RocketSilo.on_game_finished()
this.lobby_enabled = true Public.get().lobby_enabled = true
game.print({'frontier.map_setup'}) game.print({'frontier.map_setup'})
local surface = RS.get_surface() local surface = Public.surface()
surface.clear(true) surface.clear(true)
local mgs = table.deepcopy(surface.map_gen_settings) local mgs = table.deepcopy(surface.map_gen_settings)
mgs.seed = mgs.seed + 1e4 mgs.seed = mgs.seed + 1e4
@ -341,6 +338,7 @@ function RocketSilo.on_rocket_launched(event)
return return
end end
local this = Public.get()
if this.scenario_finished then if this.scenario_finished then
return return
end end

View File

@ -2,14 +2,12 @@ local Color = require 'resources.color_presets'
local Gui = require 'utils.gui' local Gui = require 'utils.gui'
local math = require 'utils.math' local math = require 'utils.math'
local PriceRaffle = require 'features.price_raffle' local PriceRaffle = require 'features.price_raffle'
local RS = require 'map_gen.shared.redmew_surface'
local ScoreTracker = require 'utils.score_tracker' local ScoreTracker = require 'utils.score_tracker'
local Table = require 'utils.table' local Table = require 'utils.table'
local Toast = require 'features.gui.toast' local Toast = require 'features.gui.toast'
local Token = require 'utils.token' local Token = require 'utils.token'
local Task = require 'utils.task' local Task = require 'utils.task'
local Public = require 'map_gen.maps.frontier.shared.core' local Public = require 'map_gen.maps.frontier.shared.core'
local this = Public.get()
local math_floor = math.floor local math_floor = math.floor
local math_random = math.random local math_random = math.random
@ -57,9 +55,9 @@ SpawnShop.upgrades = {
} }
function SpawnShop.add_render() function SpawnShop.add_render()
local e = this.spawn_shop local e = Public.get().spawn_shop
rendering.draw_sprite { rendering.draw_sprite {
sprite = 'achievement/lazy-bastard',--'file/graphics/neon_lightning.png', --TODO: add compatibility sprite = script.active_mods['redmew-data'] and 'neon-lightning' or 'achievement/lazy-bastard',
x_scale = 0.8, x_scale = 0.8,
y_scale = 0.8, y_scale = 0.8,
target = e, target = e,
@ -75,7 +73,7 @@ end
SpawnShop.add_render_token = Token.register(SpawnShop.add_render) SpawnShop.add_render_token = Token.register(SpawnShop.add_render)
function SpawnShop.on_game_started() function SpawnShop.on_game_started()
local surface = RS.get_surface() local surface = Public.surface()
local position = surface.find_non_colliding_position('market', {0, 0}, 32, 0.5, true) local position = surface.find_non_colliding_position('market', {0, 0}, 32, 0.5, true)
local shop = surface.create_entity { local shop = surface.create_entity {
name = 'market', name = 'market',
@ -86,7 +84,7 @@ function SpawnShop.on_game_started()
raise_built = false, raise_built = false,
} }
shop.minable = false shop.minable = false
this.spawn_shop = shop Public.get().spawn_shop = shop
Task.set_timeout(1, SpawnShop.add_render_token) Task.set_timeout(1, SpawnShop.add_render_token)
SpawnShop.refresh_all_prices(false) SpawnShop.refresh_all_prices(false)
end end
@ -98,6 +96,8 @@ function SpawnShop.draw_gui(player)
return return
end end
local this = Public.get()
frame = player.gui.screen.add { type = 'frame', name = SpawnShop.main_frame_name, direction = 'vertical' } frame = player.gui.screen.add { type = 'frame', name = SpawnShop.main_frame_name, direction = 'vertical' }
Gui.set_style(frame, { Gui.set_style(frame, {
horizontally_stretchable = true, horizontally_stretchable = true,
@ -223,7 +223,7 @@ function SpawnShop.destroy_gui(player)
local frame = player.gui.screen[SpawnShop.main_frame_name] local frame = player.gui.screen[SpawnShop.main_frame_name]
if frame then if frame then
frame.destroy() frame.destroy()
this.spawn_shop_players_in_gui_view[player.index] = nil Public.get().spawn_shop_players_in_gui_view[player.index] = nil
end end
end end
@ -247,6 +247,7 @@ function SpawnShop.refresh_price(id)
return return
end end
local this = Public.get()
local nominal_cost = math_floor(Public.VALUE_7_PACKS * Public.PROD_PENALTY * upgrade.packs) local nominal_cost = math_floor(Public.VALUE_7_PACKS * Public.PROD_PENALTY * upgrade.packs)
local item_stacks = PriceRaffle.roll(nominal_cost, 5, this.banned_items) local item_stacks = PriceRaffle.roll(nominal_cost, 5, this.banned_items)
if this._DEBUG_SHOP then if this._DEBUG_SHOP then
@ -278,6 +279,7 @@ function SpawnShop.refresh_all_prices(by_request)
end end
function SpawnShop.earn_coin() function SpawnShop.earn_coin()
local this = Public.get()
this.spawn_shop_funds = this.spawn_shop_funds + 1 this.spawn_shop_funds = this.spawn_shop_funds + 1
ScoreTracker.change_for_global(Public.scores.shop_funds.name, 1) ScoreTracker.change_for_global(Public.scores.shop_funds.name, 1)
Toast.toast_all_players(20, {'frontier.earn_coin'}) Toast.toast_all_players(20, {'frontier.earn_coin'})
@ -288,7 +290,7 @@ function SpawnShop.can_purchase(player, id)
return false return false
end end
local data = this.spawn_shop_upgrades[id] local data = Public.get().spawn_shop_upgrades[id]
if not (data and data.price) then if not (data and data.price) then
return false return false
end end
@ -329,7 +331,7 @@ function SpawnShop.on_player_purchase(player, id)
return return
end end
local data = this.spawn_shop_upgrades[id] local data = Public.get().spawn_shop_upgrades[id]
if not (data and data.price) then if not (data and data.price) then
return return
end end
@ -352,6 +354,7 @@ function SpawnShop.on_player_purchase(player, id)
end end
function SpawnShop.on_player_refresh(player) function SpawnShop.on_player_refresh(player)
local this = Public.get()
this.spawn_shop_funds = this.spawn_shop_funds - 1 this.spawn_shop_funds = this.spawn_shop_funds - 1
ScoreTracker.change_for_global(Public.scores.shop_funds.name, -1) ScoreTracker.change_for_global(Public.scores.shop_funds.name, -1)
player.print('[color=orange][Bard][/color] ' .. bard_refresh_messages[math_random(#bard_refresh_messages)], { sound_path = 'utility/scenario_message', color = Color.dark_grey }) player.print('[color=orange][Bard][/color] ' .. bard_refresh_messages[math_random(#bard_refresh_messages)], { sound_path = 'utility/scenario_message', color = Color.dark_grey })

View File

@ -2,15 +2,18 @@ local b = require 'map_gen.shared.builders'
local math = require 'utils.math' local math = require 'utils.math'
local MGSP = require 'resources.map_gen_settings' local MGSP = require 'resources.map_gen_settings'
local Noise = require 'map_gen.shared.simplex_noise' local Noise = require 'map_gen.shared.simplex_noise'
local Queue = require 'utils.queue'
local RS = require 'map_gen.shared.redmew_surface' local RS = require 'map_gen.shared.redmew_surface'
local Public = require 'map_gen.maps.frontier.shared.core' local Public = require 'map_gen.maps.frontier.shared.core'
local this = Public.get()
local math_abs = math.abs local math_abs = math.abs
local math_clamp = math.clamp local math_clamp = math.clamp
local math_floor = math.floor local math_floor = math.floor
local math_max = math.max local math_max = math.max
local math_min = math.min local math_min = math.min
local math_random = math.random local math_random = math.random
local q_size = Queue.size
local q_push = Queue.push
local q_pop = Queue.pop
local simplex = Noise.d2 local simplex = Noise.d2
local autoplace_controls = { local autoplace_controls = {
@ -45,7 +48,7 @@ RS.set_map_gen_settings({
{ {
autoplace_controls = autoplace_controls, autoplace_controls = autoplace_controls,
cliff_settings = { name = 'cliff', cliff_elevation_0 = 20, cliff_elevation_interval = 40, richness = 0.8 }, cliff_settings = { name = 'cliff', cliff_elevation_0 = 20, cliff_elevation_interval = 40, richness = 0.8 },
height = this.height * 32, height = Public.get().height * 32,
property_expression_names = { property_expression_names = {
['control-setting:aux:frequency:multiplier'] = '1.333333', ['control-setting:aux:frequency:multiplier'] = '1.333333',
['control-setting:moisture:bias'] = '-0.250000', ['control-setting:moisture:bias'] = '-0.250000',
@ -60,18 +63,51 @@ RS.set_map_gen_settings({
local Terrain = {} local Terrain = {}
function Terrain.get_map() function Terrain.get_map()
local map, water, green_water local map
local this = Public.get()
local bounds = function(x, y) local bounds = function(x, y)
return x > (-this.left_boundary * 32 - 320) and not ((y < -this.height * 16) or (y > this.height * 16)) return x > (-this.left_boundary * 32 - 320) and not ((y < -this.height * 16) or (y > this.height * 16))
end end
local water = function(x, y)
if x < -this.left_boundary * 32 then
return bounds(x, y) and 'water'
end
end
local green_water = function(x, y)
if math_floor(x) == -(this.kraken_distance + this.left_boundary * 32 + 1) then
return bounds(x, y) and 'deepwater-green'
end
end
local spawn = function(x, _)
return x >= -this.left_boundary * 32 and x < this.right_boundary * 32 + 96
end
spawn = b.remove_map_gen_entities_by_filter(spawn, { force = 'enemy' })
local wall_tile = function(x, _)
if x >= this.right_boundary * 32 - 1 and x <= this.right_boundary * 32 + this.wall_width + 1 then
return 'concrete'
elseif x >= this.right_boundary * 32 - 4 and x <= this.right_boundary * 32 + this.wall_width + 4 then
return 'hazard-concrete-left'
end
end
local wall = function(x, y)
if x >= this.right_boundary * 32 and x < this.right_boundary * 32 + this.wall_width then
return {
name = 'stone-wall',
position = { x = x , y = y },
force = 'player',
move_stuck_players = true,
}
end
end
wall = b.remove_map_gen_entities_by_filter(wall, { type = {'tree', 'simple-entity', 'cliff' } })
water = b.change_tile(bounds, true, 'water') map = b.add(water, bounds)
water = b.fish(water, 0.075) map = b.add(green_water, map)
map = b.fish(map, 0.075)
green_water = b.change_tile(bounds, true, 'deepwater-green') map = b.add(spawn, map)
map = b.apply_entity(map, wall)
map = b.choose(function(x) return x < -this.left_boundary * 32 end, water, bounds) map = b.overlay_tile_land(map, wall_tile)
map = b.choose(function(x) return math_floor(x) == -(this.kraken_distance + this.left_boundary * 32 + 1) end, green_water, map)
return map return map
end end
@ -88,6 +124,7 @@ function Terrain.noise_pattern(position, seed)
end end
function Terrain.mixed_resources(surface, area) function Terrain.mixed_resources(surface, area)
local this = Public.get()
local left_top = { x = math_max(area.left_top.x, this.right_boundary * 32), y = area.left_top.y } local left_top = { x = math_max(area.left_top.x, this.right_boundary * 32), y = area.left_top.y }
local right_bottom = area.right_bottom local right_bottom = area.right_bottom
if left_top.x >= right_bottom.x then if left_top.x >= right_bottom.x then
@ -119,9 +156,9 @@ function Terrain.mixed_resources(surface, area)
local position = { x = left_top.x + x, y = left_top.y + y } local position = { x = left_top.x + x, y = left_top.y + y }
if can_place_entity({ name = 'iron-ore', position = position }) then if can_place_entity({ name = 'iron-ore', position = position }) then
local noise = Terrain.noise_pattern(position, seed) local noise = Terrain.noise_pattern(position, seed)
if math_abs(noise) > 0.67 then if math_abs(noise) > 0.77 then
local idx = math_floor(noise * 25 + math_abs(position.x) * 0.05) % #mixed_ores + 1 local idx = math_floor(noise * 25 + math_abs(position.x) * 0.05) % #mixed_ores + 1
local amount = this.ore_base_quantity * chunks * 5 local amount = this.ore_base_quantity * chunks * 35 + math_random(100)
if clear_ore(position) then if clear_ore(position) then
create_entity({ name = mixed_ores[idx], position = position, amount = amount }) create_entity({ name = mixed_ores[idx], position = position, amount = amount })
end end
@ -131,15 +168,8 @@ function Terrain.mixed_resources(surface, area)
end end
end end
function Terrain.clear_enemies_inside_wall(surface, area)
if area.right_bottom.x < (this.right_boundary * 32 + 96) then
for _, entity in pairs(surface.find_entities_filtered { area = area, force = 'enemy' }) do
entity.destroy()
end
end
end
function Terrain.scale_resource_richness(surface, area) function Terrain.scale_resource_richness(surface, area)
local this = Public.get()
for _, resource in pairs(surface.find_entities_filtered { area = area, type = 'resource' }) do for _, resource in pairs(surface.find_entities_filtered { area = area, type = 'resource' }) do
if resource.position.x > this.right_boundary * 32 then if resource.position.x > this.right_boundary * 32 then
local chunks = math.clamp(math_abs((resource.position.x - this.right_boundary * 32) / this.ore_chunk_scale), 1, 100) local chunks = math.clamp(math_abs((resource.position.x - this.right_boundary * 32) / this.ore_chunk_scale), 1, 100)
@ -161,6 +191,7 @@ function Terrain.scale_resource_richness(surface, area)
end end
function Terrain.rich_rocks(surface, area) function Terrain.rich_rocks(surface, area)
local this = Public.get()
local left_top = { x = math_max(area.left_top.x, this.right_boundary * 32), y = area.left_top.y } local left_top = { x = math_max(area.left_top.x, this.right_boundary * 32), y = area.left_top.y }
local right_bottom = area.right_bottom local right_bottom = area.right_bottom
if left_top.x >= right_bottom.x then if left_top.x >= right_bottom.x then
@ -212,37 +243,33 @@ function Terrain.set_silo_tiles(entity)
entity.surface.set_tiles(tiles, true) entity.surface.set_tiles(tiles, true)
end end
function Terrain.create_wall(x, w) function Terrain.queue_reveal_map()
local surface = RS.get_surface() local this = Public.get()
local area = { { x, -this.height * 16 }, { x + w, this.height * 16 } } local chart_queue = this.chart_queue
for _, entity in pairs(surface.find_entities_filtered { area = area, collision_mask = 'player-layer' }) do -- important to flush the queue upon resetting a map or chunk requests from previous maps could overlap
entity.destroy() Queue.clear(chart_queue)
end local size = math_max(this.left_boundary, this.right_boundary, this.height / 2) * 32 + 96
for x = 16, size, 32 do
for y = 16, size, 32 do
q_push(chart_queue, {{-x, -y}, {-x, -y}})
q_push(chart_queue, {{ x, -y}, { x, -y}})
q_push(chart_queue, {{-x, y}, {-x, y}})
q_push(chart_queue, {{ x, y}, { x, y}})
end
end
end
for y = -this.height * 16, this.height * 16 do function Terrain.pop_chunk_request(max_requests)
for j = 0, w - 1 do max_requests = max_requests or 1
local e = surface.create_entity { local this = Public.get()
name = 'stone-wall', local chart_queue = this.chart_queue
position = { x + j, y }, local surface = Public.surface()
force = 'player', local players = game.forces.player
move_stuck_players = true,
}
e.destructible = this.wall_vulnerability
end
end
local tiles = {} while max_requests > 0 and q_size(chart_queue) > 0 do
for j = -4, w - 1 + 4 do players.chart(surface, q_pop(chart_queue))
for y = -this.height * 16, this.height * 16 do max_requests = max_requests - 1
tiles[#tiles +1] = { name = 'hazard-concrete-left', position = { x = x + j, y = y }} end
end
end
for j = -1, w do
for y = -this.height * 16, this.height * 16 do
tiles[#tiles +1] = { name = 'concrete', position = { x = x + j, y = y }}
end
end
surface.set_tiles(tiles, true)
end end
return Terrain return Terrain

View File

@ -13,7 +13,6 @@ local Market = require 'map_gen.maps.frontier.modules.market'
local RocketSilo = require 'map_gen.maps.frontier.modules.rocket_silo' local RocketSilo = require 'map_gen.maps.frontier.modules.rocket_silo'
local SpawnShop = require 'map_gen.maps.frontier.modules.spawn_shop' local SpawnShop = require 'map_gen.maps.frontier.modules.spawn_shop'
local Terrain = require 'map_gen.maps.frontier.modules.terrain' local Terrain = require 'map_gen.maps.frontier.modules.terrain'
local this = Public.get()
local math_random = math.random local math_random = math.random
--[[ --[[
@ -92,19 +91,30 @@ local function on_init()
Lobby.on_init() Lobby.on_init()
RocketSilo.on_game_started() RocketSilo.on_game_started()
RocketSilo.reveal_spawn_area() RocketSilo.reveal_spawn_area()
Terrain.queue_reveal_map()
SpawnShop.on_game_started() SpawnShop.on_game_started()
this.lobby_enabled = false Public.get().lobby_enabled = false
Lobby.teleport_all_from() Lobby.teleport_all_from()
end end
Event.on_init(on_init) Event.on_init(on_init)
local function on_tick()
local tick = game.tick
if (tick - 1) % 90 == 0 then
Terrain.pop_chunk_request(24)
end
end
Event.add(defines.events.on_tick, on_tick)
local function on_game_started() local function on_game_started()
RocketSilo.on_game_started() RocketSilo.on_game_started()
RocketSilo.reveal_spawn_area() RocketSilo.reveal_spawn_area()
Terrain.queue_reveal_map()
SpawnShop.on_game_started() SpawnShop.on_game_started()
this.lobby_enabled = false Public.get().lobby_enabled = false
Lobby.teleport_all_from() Lobby.teleport_all_from()
end end
Event.add(Public.events.on_game_started, on_game_started) Event.add(Public.events.on_game_started, on_game_started)
@ -124,7 +134,7 @@ local function on_player_created(event)
return return
end end
if this.lobby_enabled then if Public.get().lobby_enabled then
Lobby.teleport_to(player) Lobby.teleport_to(player)
end end
end end
@ -138,13 +148,10 @@ local function on_chunk_generated(event)
Lobby.on_chunk_generated(event) Lobby.on_chunk_generated(event)
end end
if surface.name ~= this.surface.name then if surface.name ~= Public.surface().name then
return return
end end
-- kill off biters inside the wall
Terrain.clear_enemies_inside_wall(surface, area)
-- scale freshly generated ore by a scale factor -- scale freshly generated ore by a scale factor
Terrain.scale_resource_richness(surface, area) Terrain.scale_resource_richness(surface, area)
@ -208,6 +215,7 @@ local function on_player_died(event)
return return
end end
local this = Public.get()
if this.rockets_per_death <= 0 then if this.rockets_per_death <= 0 then
return return
end end
@ -231,6 +239,7 @@ local function on_player_changed_position(event)
return return
end end
local this = Public.get()
if player.position.x < (-this.left_boundary * 32 - this.kraken_distance) then if player.position.x < (-this.left_boundary * 32 - this.kraken_distance) then
local player_name = 'a player' local player_name = 'a player'
if player.character ~= nil then if player.character ~= nil then
@ -288,7 +297,7 @@ Event.add(defines.events.on_entity_destroyed, on_entity_destroyed)
local function on_ai_command_completed(event) local function on_ai_command_completed(event)
if not event.was_distracted then if not event.was_distracted then
local data = this.unit_groups[event.unit_number] local data = Public.get().unit_groups[event.unit_number]
if data and data.unit_group and data.unit_group.valid then if data and data.unit_group and data.unit_group.valid then
Enemy.ai_processor(data.unit_group, event.result) Enemy.ai_processor(data.unit_group, event.result)
end end
@ -311,7 +320,7 @@ local function on_gui_opened(event)
return return
end end
if entity.unit_number == this.spawn_shop.unit_number then if entity.unit_number == Public.get().spawn_shop.unit_number then
SpawnShop.draw_gui(player) SpawnShop.draw_gui(player)
end end
end end
@ -364,7 +373,7 @@ Command.add('ping-silo',
allowed_by_server = true allowed_by_server = true
}, },
function(_, player) function(_, player)
local surface = this.surface local surface = Public.surface()
local msg = '[color=blue][Mapkeeper][/color] Here you\'ll find a silo:' local msg = '[color=blue][Mapkeeper][/color] Here you\'ll find a silo:'
local silos = surface.find_entities_filtered { name = 'rocket-silo' } local silos = surface.find_entities_filtered { name = 'rocket-silo' }
for _, s in pairs(silos) do for _, s in pairs(silos) do
@ -385,6 +394,7 @@ Command.add('toggle-debug-ai',
required_rank = Ranks.admin, required_rank = Ranks.admin,
}, },
function() function()
local this = Public.get()
this._DEBUG_AI = not this._DEBUG_AI this._DEBUG_AI = not this._DEBUG_AI
end end
) )
@ -396,6 +406,7 @@ Command.add('toggle-debug-shop',
required_rank = Ranks.admin, required_rank = Ranks.admin,
}, },
function() function()
local this = Public.get()
this._DEBUG_SHOP = not this._DEBUG_SHOP this._DEBUG_SHOP = not this._DEBUG_SHOP
end end
) )
@ -407,7 +418,7 @@ Command.add('print-global',
required_rank = Ranks.admin, required_rank = Ranks.admin,
}, },
function(_, player) function(_, player)
player.print(serpent.line(this)) player.print(serpent.line(Public.get()))
end end
) )
@ -418,7 +429,7 @@ Command.add('log-global',
required_rank = Ranks.admin, required_rank = Ranks.admin,
}, },
function() function()
Debug.log(this) Debug.log(Public.get())
end end
) )

View File

@ -1,5 +1,6 @@
local Event = require 'utils.event' local Event = require 'utils.event'
local Global = require 'utils.global' local Global = require 'utils.global'
local Queue = require 'utils.queue'
local RS = require 'map_gen.shared.redmew_surface' local RS = require 'map_gen.shared.redmew_surface'
local Public = {} local Public = {}
@ -21,13 +22,13 @@ Public.PROD_PENALTY = 1.2 * 1.4^5
local this = { local this = {
rounds = 0, rounds = 0,
-- Map gen -- Map gen
chart_queue = Queue.new(),
silo_starting_x = 1700, silo_starting_x = 1700,
height = 36, -- in chunks, height of the ribbon world height = 36, -- in chunks, height of the ribbon world
left_boundary = 8, -- in chunks, distance to water body left_boundary = 8, -- in chunks, distance to water body
right_boundary = 11, -- in chunks, distance to wall/biter presence right_boundary = 11, -- in chunks, distance to wall/biter presence
wall_width = 5, -- in tiles wall_width = 5, -- in tiles
wall_vulnerability = true,
rock_richness = 1, -- how many rocks/chunk rock_richness = 1, -- how many rocks/chunk
ore_base_quantity = 11, -- base ore quantity, everything is scaled up from this ore_base_quantity = 11, -- base ore quantity, everything is scaled up from this
@ -86,14 +87,14 @@ local this = {
spawn_shop_upgrades = {}, spawn_shop_upgrades = {},
} }
Global.register_init( Global.register(this, function(tbl) this = tbl end)
this,
function() this.surface = RS.get_surface() end,
function(tbl) this = tbl end
)
function Public.get() function Public.get()
return this return this
end end
function Public.surface()
return RS.get_surface()
end
return Public return Public

View File

@ -74,4 +74,11 @@ function Queue.pairs(queue)
end end
end end
function Queue.clear(queue)
for k, _ in pairs(queue) do
queue[k] = nil
end
queue._head, queue._tail = 1, 1
end
return Queue return Queue