From 229cd326c02376088f394beffa4fdfcb668a6289 Mon Sep 17 00:00:00 2001 From: Oarcinae Date: Sun, 17 Nov 2024 14:14:28 -0500 Subject: [PATCH] Fix regrowth checkboxes not respecting previous setting if surface wasn't created yet. Surface init now defaults to the global setting. Adding a migration and change to surface init to ensure the center chunks never get deleted so that cargo-pods don't go missing. --- lib/config_parser.lua | 7 +++++ lib/gui_tabs/settings_controls.lua | 5 ++-- lib/regrowth_map.lua | 41 ++++++++++++++++++++++-------- migrations/oarc-mod-v2.1.13.lua | 17 +++++++++++++ 4 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 migrations/oarc-mod-v2.1.13.lua diff --git a/lib/config_parser.lua b/lib/config_parser.lua index 75fa894..c8f9b40 100644 --- a/lib/config_parser.lua +++ b/lib/config_parser.lua @@ -336,6 +336,13 @@ function RuntimeModSettingChanged(event) local new_value = storage.ocfg.gameplay.enable_coin_shop AddRemoveOarcGuiTabForAllPlayers(OARC_ITEM_SHOP_TAB_NAME, settings.global[event.setting].value --[[@as boolean]], true) end + + --Exception for regrowth, refresh the content tab so the checkboxes get updated + if (event.setting == "oarc-mod-enable-regrowth") then + for _,player in pairs(game.players) do + OarcGuiRefreshContent(player) + end + end end ---A probably quit stupid function to let me lookup and set the storage.ocfg entries using a key table. diff --git a/lib/gui_tabs/settings_controls.lua b/lib/gui_tabs/settings_controls.lua index a73a09c..3520f07 100644 --- a/lib/gui_tabs/settings_controls.lua +++ b/lib/gui_tabs/settings_controls.lua @@ -104,8 +104,9 @@ function CreateSurfaceSettingsSection(container, player) AddSurfaceCheckboxSetting(surface_table, name, "secondary_enabled", allowed.secondary, player.admin, { "oarc-settings-tab-surface-secondary-checkbox-tooltip" }) - local regrowth_enabled = TableContains(storage.rg.active_surfaces, name) - AddSurfaceCheckboxSetting(surface_table, name, "regrowth_enabled", regrowth_enabled, player.admin, + local surface_regrowth = IsRegrowthEnabledOnSurface(name) + local global_regrowth = storage.ocfg.regrowth.enable_regrowth + AddSurfaceCheckboxSetting(surface_table, name, "regrowth_enabled", surface_regrowth, player.admin and global_regrowth, {"oarc-settings-tab-surface-regrowth-checkbox-tooltip"}) end diff --git a/lib/regrowth_map.lua b/lib/regrowth_map.lua index d51cee9..32a3b4a 100644 --- a/lib/regrowth_map.lua +++ b/lib/regrowth_map.lua @@ -56,8 +56,13 @@ function RegrowthInit() ---@type RemovalListEntry[] storage.rg.removal_list = {} + for _,planet in pairs(game.planets) do + local surface_name = planet.name + RegrowthInitSurface(surface_name, storage.ocfg.regrowth.enable_regrowth) + end + for surface_name,_ in pairs(game.surfaces) do - InitSurface(surface_name --[[@as string]]) + RegrowthInitSurface(surface_name --[[@as string]], storage.ocfg.regrowth.enable_regrowth) end end @@ -65,7 +70,7 @@ end ---@param event EventData.on_surface_created ---@return nil function RegrowthSurfaceCreated(event) - InitSurface(game.surfaces[event.surface_index].name) + RegrowthInitSurface(game.surfaces[event.surface_index].name, nil) -- Leave activated as default or existing setting. end ---Called when a surface is deleted. This is used to remove surfaces from the regrowth map. @@ -79,10 +84,13 @@ end ---Initialize the new surface for regrowth ---@param surface_name string - The surface name to act on +---@param activate boolean? - If set, the surface will be enabled/disabled for regrowth ---@return nil -function InitSurface(surface_name) +function RegrowthInitSurface(surface_name, activate) + if IsSurfaceBlacklisted(surface_name) then return end - if (not IsSurfaceBlacklisted(surface_name) and not TableContains(storage.rg.active_surfaces, surface_name)) then + -- First time setup only? TODO: This code is not very logical (if one time only, why check for nil...) + if (not TableContains(storage.rg.active_surfaces, surface_name)) then log("Adding surface to regrowth: " .. surface_name) -- Add a new surface to the regrowth map (Don't overwrite if it already exists) @@ -101,8 +109,21 @@ function InitSurface(surface_name) storage.rg.we_current_surface = surface_name end - storage.rg[surface_name].active = true table.insert(storage.rg.active_surfaces, surface_name) + + -- TODO: Hopefully a temporary measure to make sure map center never gets deleted. + -- If we can detect and redirect cargo-pods, then this can be removed. + for i = -2, 2 do + for j = -2, 2 do + MarkChunkSafe(surface_name, { x = i, y = j }, true) + end + end + end + + if (activate ~= nil) then + storage.rg[surface_name].active = activate + elseif (storage.rg[surface_name].active == nil) then + storage.rg[surface_name].active = storage.ocfg.regrowth.enable_regrowth end end @@ -143,7 +164,7 @@ end ---@param surface_name string - The surface name to act on ---@return nil function RegrowthEnableSurface(surface_name) - InitSurface(surface_name) + RegrowthInitSurface(surface_name, true) end ---Trigger an immediate cleanup of any chunks that are marked for removal. @@ -412,8 +433,8 @@ function GetNextChunkAndUpdateIter() storage.rg.current_surface = next_surface_info.surface storage.rg.current_surface_index = next_surface_info.index - -- Surface may not exist - if game.surfaces[storage.rg.current_surface] ~= nil then + -- Surface may not exist or may not be active + if game.surfaces[storage.rg.current_surface] ~= nil and storage.rg[next_surface_info.surface].active then storage.rg.chunk_iter = game.surfaces[storage.rg.current_surface].get_chunks() next_chunk = storage.rg.chunk_iter() end @@ -442,8 +463,8 @@ function GetNextChunkAndUpdateWorldEaterIter() storage.rg.we_current_surface = next_surface_info.surface storage.rg.we_current_surface_index = next_surface_info.index - -- Surface may not exist - if game.surfaces[storage.rg.we_current_surface] ~= nil then + -- Surface may not exist or may not be active + if game.surfaces[storage.rg.we_current_surface] ~= nil and storage.rg[next_surface_info.surface].active then storage.rg.we_chunk_iter = game.surfaces[storage.rg.we_current_surface].get_chunks() next_chunk = storage.rg.we_chunk_iter() end diff --git a/migrations/oarc-mod-v2.1.13.lua b/migrations/oarc-mod-v2.1.13.lua new file mode 100644 index 0000000..eccdf3e --- /dev/null +++ b/migrations/oarc-mod-v2.1.13.lua @@ -0,0 +1,17 @@ +---For each planet's surface, mark the center of the map permanently safe from regrowth. +-- If we can detect and redirect cargo-pods, then this can be removed. + +-- TODO: Hopefully a temporary measure to make sure map center never gets deleted. +-- If we can detect and redirect cargo-pods, then this can be removed. + +-- Loop through each surface +for _,surface in pairs(game.surfaces) do + if (storage.rg[surface.name] ~= nil) then + for i = -2, 2 do + for j = -2, 2 do + MarkChunkSafe(surface.name, { x = i, y = j }, true) + end + end + log("Applying migration for V2.1.13: Marked center of "..surface.name.." safe from regrowth.") + end +end