From 1d6e82b4f74e81bc4fdb867cda3845b25fd75557 Mon Sep 17 00:00:00 2001 From: Gerkiz Date: Tue, 20 Dec 2022 13:48:28 +0100 Subject: [PATCH] Towny - fixes --- functions/soft_reset.lua | 16 ++-- maps/scrap_towny_ffa/building.lua | 20 +++- maps/scrap_towny_ffa/limited_radar.lua | 12 ++- maps/scrap_towny_ffa/main.lua | 14 +-- maps/scrap_towny_ffa/nauvis.lua | 91 ++++++------------- maps/scrap_towny_ffa/player.lua | 6 +- maps/scrap_towny_ffa/reset.lua | 16 ++-- .../scrap_towny_ffa_layout.lua | 13 ++- maps/scrap_towny_ffa/table.lua | 1 + maps/scrap_towny_ffa/team.lua | 48 +++++++--- 10 files changed, 134 insertions(+), 103 deletions(-) diff --git a/functions/soft_reset.lua b/functions/soft_reset.lua index f1f6576d..7c597d58 100644 --- a/functions/soft_reset.lua +++ b/functions/soft_reset.lua @@ -178,15 +178,17 @@ local function teleport_players(surface, small_force_chunk) end local function equip_players(player_starting_items) - for k, player in pairs(game.connected_players) do + for _, player in pairs(game.connected_players) do if player.character then player.character.destroy() end player.character = nil player.set_controller({type = defines.controllers.god}) player.create_character() - for item, amount in pairs(player_starting_items) do - player.insert({name = item, count = amount}) + if player_starting_items then + for item, amount in pairs(player_starting_items) do + player.insert({name = item, count = amount}) + end end Modifiers.update_player_modifiers(player) end @@ -261,10 +263,10 @@ function Public.get_reset_counter() end --- Customizes the message with the mapkeeper param. ----@param boolean -function Public.enable_mapkeeper(boolean) - if boolean and type(boolean) == 'boolean' then - this.enable_mapkeeper = boolean or false +---@param state boolean +function Public.enable_mapkeeper(state) + if state and type(state) == 'boolean' then + this.enable_mapkeeper = state or false end end diff --git a/maps/scrap_towny_ffa/building.lua b/maps/scrap_towny_ffa/building.lua index a1b1cf55..7a3c3cfb 100644 --- a/maps/scrap_towny_ffa/building.lua +++ b/maps/scrap_towny_ffa/building.lua @@ -160,7 +160,13 @@ function Public.near_another_town(force_name, position, surface, radius) end function Public.in_restricted_zone(surface, position) - if surface.name ~= 'nauvis' then + local this = ScenarioTable.get_table() + local map_surface = game.get_surface(this.active_surface_index) + if not map_surface or not map_surface.valid then + return + end + + if surface.name ~= map_surface.name then return false end local chunk_position = {} @@ -205,7 +211,11 @@ local function prevent_landfill_in_restricted_zone(event) if tile == nil or not tile.valid then return end - local surface = game.surfaces[event.surface_index] + local this = ScenarioTable.get_table() + local surface = game.get_surface(this.active_surface_index) + if not surface or not surface.valid then + return + end local fail = false local position for _, t in pairs(event.tiles) do @@ -275,7 +285,11 @@ local function prevent_tiles_near_towns(event) if tile == nil or not tile.valid then return end - local surface = game.surfaces[event.surface_index] + local this = ScenarioTable.get_table() + local surface = game.get_surface(this.active_surface_index) + if not surface or not surface.valid then + return + end local force_name if player_index ~= nil then local player = game.get_player(player_index) diff --git a/maps/scrap_towny_ffa/limited_radar.lua b/maps/scrap_towny_ffa/limited_radar.lua index f2ae739f..9947006d 100644 --- a/maps/scrap_towny_ffa/limited_radar.lua +++ b/maps/scrap_towny_ffa/limited_radar.lua @@ -8,10 +8,14 @@ function Public.reset() if this.testing_mode then return end + local map_surface = game.get_surface(this.active_surface_index) + if not map_surface or not map_surface.valid then + return + end for index = 1, table.size(game.forces), 1 do local force = game.forces[index] if force ~= nil then - force.clear_chart('nauvis') + force.clear_chart(map_surface.name) end end end @@ -39,7 +43,11 @@ local function update_forces(id) end local function on_chunk_charted(event) - local surface = game.surfaces[event.surface_index] + local this = ScenarioTable.get_table() + local surface = game.get_surface(this.active_surface_index) + if not surface or not surface.valid then + return + end local force = event.force local area = event.area local markets = surface.find_entities_filtered({area = area, name = 'market'}) diff --git a/maps/scrap_towny_ffa/main.lua b/maps/scrap_towny_ffa/main.lua index 7e9416c0..5b948cdb 100644 --- a/maps/scrap_towny_ffa/main.lua +++ b/maps/scrap_towny_ffa/main.lua @@ -97,22 +97,22 @@ local function update_score() }, field2 = { text1 = 'Town researched:', - text2 = winner.name .. ' with a score of ' .. winner.research_counter .. ' techs!', + text2 = winner.research_counter .. ' techs!', inline = 'false' }, field3 = { text1 = 'Town upgrades:', - text2 = winner.name .. ' upgraded their town with:\nCrafting speed:' .. winner.upgrades.crafting_speed .. '\nMining speed:' .. winner.upgrades.mining_speed, + text2 = 'Crafting speed:' .. winner.upgrades.crafting_speed .. '\nMining speed:' .. winner.upgrades.mining_speed, inline = 'false' }, field4 = { text1 = 'Town health:', - text2 = winner.name .. ' had a health of ' .. winner.health .. ' left!', + text2 = winner.health .. 'hp left!', inline = 'false' }, field5 = { text1 = 'Town coins:', - text2 = winner.name .. ' had ' .. winner.coin_balance .. ' coins stashed!', + text2 = winner.coin_balance .. ' coins stashed!', inline = 'false' } } @@ -135,7 +135,9 @@ local function update_score() subheader.style.horizontally_stretchable = true subheader.style.vertical_align = 'center' - subheader.add {type = 'label', style = 'subheader_label', caption = {'', 'Survive 2 days (48h) to win!'}} + local days = this.required_time_to_win / 24 + + subheader.add {type = 'label', style = 'subheader_label', caption = {'', 'Survive for ' .. days .. ' days (' .. this.required_time_to_win .. 'h) to win!'}} if not next(subheader.children) then subheader.destroy() @@ -246,8 +248,6 @@ local function on_nth_tick(event) if not tick_actions[seconds] then return end - --game.surfaces['nauvis'].play_sound({path = 'utility/alert_destroyed', volume_modifier = 1}) - --log('seconds = ' .. seconds) tick_actions[seconds]() end diff --git a/maps/scrap_towny_ffa/nauvis.lua b/maps/scrap_towny_ffa/nauvis.lua index 0e7e146a..9bb01ff1 100644 --- a/maps/scrap_towny_ffa/nauvis.lua +++ b/maps/scrap_towny_ffa/nauvis.lua @@ -1,5 +1,6 @@ local Event = require 'utils.event' local ScenarioTable = require 'maps.scrap_towny_ffa.table' +local SoftReset = require 'functions.soft_reset' local math_random = math.random local table_shuffle = table.shuffle_table @@ -11,8 +12,12 @@ local map_width = 2560 local map_height = 2560 function Public.nuke(position) - local surface = game.surfaces['nauvis'] - surface.create_entity({name = 'atomic-rocket', position = position, target = position, speed = 0.5}) + local this = ScenarioTable.get_table() + local map_surface = game.get_surface(this.active_surface_index) + if not map_surface or not map_surface.valid then + return + end + map_surface.create_entity({name = 'atomic-rocket', position = position, target = position, speed = 0.5}) end function Public.armageddon() @@ -63,14 +68,10 @@ function Public.armageddon() end function Public.initialize() - if game.surfaces['nauvis'] then - -- clear the surface - game.surfaces['nauvis'].clear(false) - end - local surface = game.surfaces['nauvis'] - + local this = ScenarioTable.get_table() + local surface_seed = game.surfaces['nauvis'] -- this overrides what is in the map_gen_settings.json file - local mgs = surface.map_gen_settings + local mgs = surface_seed.map_gen_settings mgs.default_enable_all_autoplace_controls = true -- don't mess with this! mgs.autoplace_controls = { coal = {frequency = 2, size = 0.1, richness = 0.2}, @@ -106,10 +107,6 @@ function Public.initialize() cliff_elevation_interval = 10, richness = 0.4 } - -- water = 0 means no water allowed - -- water = 1 means elevation is not reduced when calculating water tiles (elevation < 0) - -- water = 2 means elevation is reduced by 10 when calculating water tiles (elevation < 0) - -- or rather, the water table is 10 above the normal elevation mgs.water = 0.5 mgs.peaceful_mode = false mgs.starting_area = 'none' @@ -120,56 +117,10 @@ function Public.initialize() --mgs.starting_points = { -- {x = 0, y = 0} --} - -- here we put the named noise expressions for the specific noise-layer if we want to override them mgs.property_expression_names = { - -- here we are overriding the moisture noise-layer with a fixed value of 0 to keep moisture consistently dry across the map - -- it allows to free up the moisture noise expression - -- low moisture - --moisture = 0, - - -- here we are overriding the aux noise-layer with a fixed value to keep aux consistent across the map - -- it allows to free up the aux noise expression - -- aux should be not sand, nor red sand - --aux = 0.5, - - -- here we are overriding the temperature noise-layer with a fixed value to keep temperature consistent across the map - -- it allows to free up the temperature noise expression - -- temperature should be 20C or 68F - --temperature = 20, - - -- here we are overriding the cliffiness noise-layer with a fixed value of 0 to disable cliffs - -- it allows to free up the cliffiness noise expression (which may or may not be useful) - -- disable cliffs - --cliffiness = 0, - - -- we can disable starting lakes two ways, one by setting starting-lake-noise-amplitude = 0 - -- or by making the elevation a very large number - -- make sure starting lake amplitude is 0 to disable starting lakes ['starting-lake-noise-amplitude'] = 0, -- allow enemies to get up close on spawn ['starting-area'] = 0, - -- this accepts a string representing a named noise expression - -- or number to determine the elevation based on x, y and distance from starting points - -- we can not add a named noise expression at this point, we can only reference existing ones - -- if we have any custom noise expressions defined from a mod, we will be able to use them here - -- setting it to a fixed number would mean a flat map - -- elevation < 0 means there is water unless the water table has been changed - --elevation = -1, - --elevation = 0, - --elevation-persistence = 0, - - -- testing - --["control-setting:moisture:bias"] = 0.5, - --["control-setting:moisture:frequency:multiplier"] = 0, - --["control-setting:aux:bias"] = 0.5, - --["control-setting:aux:frequency:multiplier"] = 1, - --["control-setting:temperature:bias"] = 0.01, - --["control-setting:temperature:frequency:multiplier"] = 100, - - --["tile:water:probability"] = -1000, - --["tile:deep-water:probability"] = -1000, - - -- a constant intensity means base distribution will be consistent with regard to distance ['enemy-base-intensity'] = 1, -- adjust this value to set how many nests spawn per tile ['enemy-base-frequency'] = 0.4, @@ -177,6 +128,23 @@ function Public.initialize() ['enemy-base-radius'] = 12 } mgs.seed = math_random(10000, 999999) + + if not this.active_surface_index then + this.active_surface_index = game.create_surface('towny', mgs).index + else + this.active_surface_index = SoftReset.soft_reset_map(game.surfaces[this.active_surface_index], mgs, nil).index + end + + local surface = game.get_surface(this.active_surface_index) + if not surface or not surface.valid then + return + end + + if math.random(1, 32) == 1 then + this.required_time_to_win = 168 + this.required_time_to_win_in_ticks = 36288000 + end + surface.map_gen_settings = mgs surface.peaceful_mode = false surface.always_day = false @@ -184,11 +152,6 @@ function Public.initialize() surface.clear(true) surface.regenerate_entity({'rock-huge', 'rock-big', 'sand-rock-big'}) surface.regenerate_decorative() - -- this will force generate the entire map - --Server.to_discord_embed('ScrapTownyFFA Map Regeneration in Progress') - --surface.request_to_generate_chunks({x=0,y=0},64) - --surface.force_generate_chunk_requests() - --Server.to_discord_embed('Regeneration Complete') end local function on_tick() diff --git a/maps/scrap_towny_ffa/player.lua b/maps/scrap_towny_ffa/player.lua index bf7e2764..5cfe7d34 100644 --- a/maps/scrap_towny_ffa/player.lua +++ b/maps/scrap_towny_ffa/player.lua @@ -44,7 +44,11 @@ end function Public.spawn(player) -- first time spawn point - local surface = game.surfaces['nauvis'] + local this = ScenarioTable.get_table() + local surface = game.get_surface(this.active_surface_index) + if not surface or not surface.valid then + return + end local spawn_point = Spawn.get_new_spawn_point(player, surface) local this = ScenarioTable.get() this.strikes[player.name] = 0 diff --git a/maps/scrap_towny_ffa/reset.lua b/maps/scrap_towny_ffa/reset.lua index 6d256b33..3657edab 100644 --- a/maps/scrap_towny_ffa/reset.lua +++ b/maps/scrap_towny_ffa/reset.lua @@ -11,7 +11,6 @@ local table_insert = table.insert -- game duration in ticks -- 7d * 24h * 60m * 60s * 60t -- local game_duration = 36288000 -local game_duration = 36288000 local armageddon_duration = 3600 local warning_duration = 600 local mapkeeper = '[color=blue]Mapkeeper:[/color]' @@ -58,7 +57,10 @@ local function do_soft_reset() this.game_reset_tick = nil this.game_won = false ScenarioTable.reset_table() - local surface = game.surfaces['nauvis'] + local surface = game.get_surface(this.active_surface_index) + if not surface or not surface.valid then + return + end if get_victorious_force() then surface.play_sound({path = 'utility/game_won', volume_modifier = 1}) else @@ -152,17 +154,19 @@ local function on_tick() end end - if (tick + armageddon_duration + warning_duration) % game_duration == 0 then + local required_time_to_win_in_ticks = ScenarioTable.get('required_time_to_win_in_ticks') + + if (tick + armageddon_duration + warning_duration) % required_time_to_win_in_ticks == 0 then warning() end - if (tick + armageddon_duration) % game_duration == 0 then + if (tick + armageddon_duration) % required_time_to_win_in_ticks == 0 then armageddon() end - if (tick + 1) % game_duration == 0 then + if (tick + 1) % required_time_to_win_in_ticks == 0 then Nauvis.clear_nuke_schedule() Team.reset_all_forces() end - if tick % game_duration == 0 then + if tick % required_time_to_win_in_ticks == 0 then has_the_game_ended() end end diff --git a/maps/scrap_towny_ffa/scrap_towny_ffa_layout.lua b/maps/scrap_towny_ffa/scrap_towny_ffa_layout.lua index f8364cb4..6aacd7c7 100644 --- a/maps/scrap_towny_ffa/scrap_towny_ffa_layout.lua +++ b/maps/scrap_towny_ffa/scrap_towny_ffa_layout.lua @@ -266,7 +266,12 @@ end local function on_chunk_generated(event) --log("scrap_towny_ffa::on_chunk_generated") local surface = event.surface - if (surface.name ~= 'nauvis') then + local this = ScenarioTable.get_table() + local map_surface = game.get_surface(this.active_surface_index) + if not map_surface or not map_surface.valid then + return + end + if (surface.name ~= map_surface.name) then return end local seed = surface.map_gen_settings.seed @@ -329,7 +334,11 @@ end local function on_chunk_charted(event) local force = event.force - local surface = game.surfaces[event.surface_index] + local this = ScenarioTable.get_table() + local surface = game.get_surface(this.active_surface_index) + if not surface or not surface.valid then + return + end if force.valid then if force == game.forces['player'] or force == game.forces['rogue'] then force.clear_chart(surface) diff --git a/maps/scrap_towny_ffa/table.lua b/maps/scrap_towny_ffa/table.lua index e6a46143..6c2919c2 100644 --- a/maps/scrap_towny_ffa/table.lua +++ b/maps/scrap_towny_ffa/table.lua @@ -38,6 +38,7 @@ function Public.reset_table() this.spaceships = {} this.suicides = {} this.required_time_to_win = 48 + this.required_time_to_win_in_ticks = 10368000 this.announced_message = nil this.soft_reset = true this.winner = nil diff --git a/maps/scrap_towny_ffa/team.lua b/maps/scrap_towny_ffa/team.lua index 353c7afa..7e4f6f20 100644 --- a/maps/scrap_towny_ffa/team.lua +++ b/maps/scrap_towny_ffa/team.lua @@ -532,11 +532,15 @@ function Public.update_town_chart_tags() end end end + local surface = game.get_surface(this.active_surface_index) + if not surface or not surface.valid then + return + end if game.forces['player'] ~= nil then - game.forces['player'].clear_chart(game.surfaces['nauvis']) + game.forces['player'].clear_chart(surface) end if game.forces['rogue'] ~= nil then - game.forces['rogue'].clear_chart(game.surfaces['nauvis']) + game.forces['rogue'].clear_chart(surface) end end @@ -680,16 +684,16 @@ local function disable_cluster_grenades(force) force.recipes['cluster-grenade'].enabled = false end -local function enable_radar(force) +local function enable_radar(surface, force) force.recipes['radar'].enabled = true force.share_chart = true - force.clear_chart('nauvis') + force.clear_chart(surface.name) end -local function disable_radar(force) +local function disable_radar(surface, force) force.recipes['radar'].enabled = false force.share_chart = false - force.clear_chart('nauvis') + force.clear_chart(surface.name) end local function disable_achievements(permission_group) @@ -705,6 +709,10 @@ function Public.add_new_force(force_name) local this = ScenarioTable.get_table() -- disable permissions local force = game.create_force(force_name) + local surface = game.get_surface(this.active_surface_index) + if not surface or not surface.valid then + return + end local permission_group = game.permissions.create_group(force_name) reset_permissions(permission_group) enable_blueprints(permission_group) @@ -714,7 +722,7 @@ function Public.add_new_force(force_name) disable_rockets(force) disable_nukes(force) disable_cluster_grenades(force) - enable_radar(force) + enable_radar(surface, force) disable_achievements(permission_group) disable_tips_and_tricks(permission_group) -- friendly fire @@ -858,8 +866,13 @@ local function kill_force(force_name, cause) end local function on_forces_merged() + local this = ScenarioTable.get_table() + local map_surface = game.get_surface(this.active_surface_index) + if not map_surface or not map_surface.valid then + return + end -- Remove any ghosts that have been moved into neutral after a town is destroyed. This caused desyncs before. - for _, e in pairs(game.surfaces.nauvis.find_entities_filtered({force = 'neutral', type = 'entity-ghost'})) do + for _, e in pairs(map_surface.find_entities_filtered({force = 'neutral', type = 'entity-ghost'})) do if e.valid then e.destroy() end @@ -888,6 +901,10 @@ local function setup_player_force() local force = game.forces.player local permission_group = game.permissions.create_group('outlander') -- disable permissions + local surface = game.get_surface(this.active_surface_index) + if not surface or not surface.valid then + return + end reset_permissions(permission_group) disable_blueprints(permission_group) disable_deconstruct(permission_group) @@ -896,7 +913,7 @@ local function setup_player_force() disable_rockets(force) disable_nukes(force) disable_cluster_grenades(force) - disable_radar(force) + disable_radar(surface, force) disable_achievements(permission_group) disable_tips_and_tricks(permission_group) -- disable research @@ -926,6 +943,10 @@ local function setup_rogue_force() end local permission_group = game.permissions.create_group('rogue') -- disable permissions + local surface = game.get_surface(this.active_surface_index) + if not surface or not surface.valid then + return + end reset_permissions(permission_group) disable_blueprints(permission_group) disable_deconstruct(permission_group) @@ -934,7 +955,7 @@ local function setup_rogue_force() disable_rockets(force) disable_nukes(force) disable_cluster_grenades(force) - disable_radar(force) + disable_radar(surface, force) disable_achievements(permission_group) disable_tips_and_tricks(permission_group) -- disable research @@ -1090,7 +1111,12 @@ local function on_post_entity_died(event) if prototype ~= 'character' then return end - local entities = game.surfaces[event.surface_index].find_entities_filtered({position = event.position, radius = 1}) + local this = ScenarioTable.get_table() + local surface = game.get_surface(this.active_surface_index) + if not surface or not surface.valid then + return + end + local entities = surface.find_entities_filtered({position = event.position, radius = 1}) for _, e in pairs(entities) do if e.type == 'character-corpse' then Public.remove_key(e.character_corpse_player_index)