From 936d636cddd9399026b4fba2a4fa4fc43818dac8 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Wed, 10 Jan 2024 00:51:44 +0000 Subject: [PATCH] No longer save/load water region invalidation states Move InitializeWaterRegions to AllocateMap No longer ForceUpdate in InitializeWaterRegions --- src/genworld.cpp | 3 --- src/map.cpp | 3 +++ src/pathfinder/water_regions.cpp | 29 +++++++---------------------- src/pathfinder/water_regions.h | 3 --- src/saveload/afterload.cpp | 2 -- src/saveload/water_regions_sl.cpp | 10 ---------- src/sl/CMakeLists.txt | 1 - src/sl/extended_ver_sl.cpp | 1 - src/sl/extended_ver_sl.h | 1 - src/sl/saveload.cpp | 2 -- src/sl/water_regions_sl.cpp | 27 --------------------------- 11 files changed, 10 insertions(+), 72 deletions(-) delete mode 100644 src/sl/water_regions_sl.cpp diff --git a/src/genworld.cpp b/src/genworld.cpp index 425c7ccb67..d2ff24a4a0 100644 --- a/src/genworld.cpp +++ b/src/genworld.cpp @@ -34,7 +34,6 @@ #include "string_func.h" #include "thread.h" #include "tgp.h" -#include "pathfinder/water_regions.h" #include "signal_func.h" #include "newgrf_industrytiles.h" #include "station_func.h" @@ -192,8 +191,6 @@ static void _GenerateWorld() } } - InitializeWaterRegions(); - BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP); ResetObjectToPlace(); diff --git a/src/map.cpp b/src/map.cpp index a937b82337..5246317136 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -14,6 +14,7 @@ #include "string_func.h" #include "rail_map.h" #include "tunnelbridge_map.h" +#include "pathfinder/water_regions.h" #include "3rdparty/cpp-btree/btree_map.h" #include "core/ring_buffer.hpp" #include @@ -133,6 +134,8 @@ void AllocateMap(uint size_x, uint size_y) _m = reinterpret_cast(buf); _me = reinterpret_cast(buf + (_map_size * sizeof(Tile))); + + InitializeWaterRegions(); } diff --git a/src/pathfinder/water_regions.cpp b/src/pathfinder/water_regions.cpp index 415d785778..a4bdcdfbbc 100644 --- a/src/pathfinder/water_regions.cpp +++ b/src/pathfinder/water_regions.cpp @@ -19,6 +19,7 @@ #include "follow_track.hpp" #include "ship.h" +#include #include using TWaterRegionTraversabilityBits = uint16_t; @@ -221,6 +222,11 @@ public: { if (!this->initialized) ForceUpdate(); } + + inline uint32_t CountPatchLabelOccurence(TWaterRegionPatchLabel label) const + { + return std::count(this->tile_patch_labels.begin(), this->tile_patch_labels.end(), label); + } }; std::vector _water_regions; @@ -379,27 +385,6 @@ void VisitWaterRegionPatchNeighbors(const WaterRegionPatchDesc &water_region_pat } } -std::vector GetWaterRegionSaveLoadInfo() -{ - std::vector result; - for (WaterRegion ®ion : _water_regions) result.push_back({ region.IsInitialized() }); - return result; -} - -void LoadWaterRegions(const std::vector &save_load_info) -{ - _water_regions.clear(); - _water_regions.reserve(save_load_info.size()); - TWaterRegionIndex index = 0; - for (const auto &loaded_region_info : save_load_info) { - const uint32_t region_x = index & ((1 << GetWaterRegionYShift()) - 1); - const uint32_t region_y = index >> GetWaterRegionYShift(); - WaterRegion ®ion = _water_regions.emplace_back(region_x, region_y); - if (loaded_region_info.initialized) region.ForceUpdate(); - index++; - } -} - /** * Initializes all water regions. All water tiles will be scanned and interconnected water patches within regions will be identified. */ @@ -410,7 +395,7 @@ void InitializeWaterRegions() for (uint32_t region_y = 0; region_y < GetWaterRegionMapSizeY(); region_y++) { for (uint32_t region_x = 0; region_x < GetWaterRegionMapSizeX(); region_x++) { - _water_regions.emplace_back(region_x, region_y).ForceUpdate(); + _water_regions.emplace_back(region_x, region_y); } } } diff --git a/src/pathfinder/water_regions.h b/src/pathfinder/water_regions.h index 84504cc936..d7fdbfa6dc 100644 --- a/src/pathfinder/water_regions.h +++ b/src/pathfinder/water_regions.h @@ -76,7 +76,4 @@ struct WaterRegionSaveLoadInfo bool initialized; }; -std::vector GetWaterRegionSaveLoadInfo(); -void LoadWaterRegions(const std::vector &save_load_info); - #endif /* WATER_REGIONS_H */ diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 2f34a7fd6f..083ad8357e 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -4399,8 +4399,6 @@ bool AfterLoadGame() c->settings = _settings_client.company; } - if (IsSavegameVersionBefore(SLV_WATER_REGIONS) && SlXvIsFeatureMissing(XSLFI_WATER_REGIONS)) InitializeWaterRegions(); - return true; } diff --git a/src/saveload/water_regions_sl.cpp b/src/saveload/water_regions_sl.cpp index e13c2b017a..48ca75c870 100644 --- a/src/saveload/water_regions_sl.cpp +++ b/src/saveload/water_regions_sl.cpp @@ -26,12 +26,6 @@ struct WRGNChunkHandler : ChunkHandler { void Save() const override { SlTableHeader(_water_region_desc); - - int index = 0; - for (WaterRegionSaveLoadInfo ®ion : GetWaterRegionSaveLoadInfo()) { - SlSetArrayIndex(index++); - SlObject(®ion, _water_region_desc); - } } void Load() const override @@ -40,14 +34,10 @@ struct WRGNChunkHandler : ChunkHandler { int index; - std::vector loaded_info; while ((index = SlIterateArray()) != -1) { WaterRegionSaveLoadInfo region_info; SlObject(®ion_info, slt); - loaded_info.push_back(std::move(region_info)); } - - LoadWaterRegions(loaded_info); } }; diff --git a/src/sl/CMakeLists.txt b/src/sl/CMakeLists.txt index 06e086376d..88f233e715 100644 --- a/src/sl/CMakeLists.txt +++ b/src/sl/CMakeLists.txt @@ -54,6 +54,5 @@ add_files( train_speed_adaptation.cpp tunnel_sl.cpp vehicle_sl.cpp - water_regions_sl.cpp waypoint_sl.cpp ) diff --git a/src/sl/extended_ver_sl.cpp b/src/sl/extended_ver_sl.cpp index 8469e964d1..c608dec652 100644 --- a/src/sl/extended_ver_sl.cpp +++ b/src/sl/extended_ver_sl.cpp @@ -208,7 +208,6 @@ const SlxiSubChunkInfo _sl_xv_sub_chunk_infos[] = { { XSLFI_SAVEGAME_ID, XSCF_NULL, 1, 1, "slv_savegame_id", nullptr, nullptr, nullptr }, { XSLFI_NEWGRF_LAST_SERVICE, XSCF_NULL, 1, 1, "slv_newgrf_last_service", nullptr, nullptr, nullptr }, { XSLFI_CARGO_TRAVELLED, XSCF_NULL, 1, 1, "slv_cargo_travelled", nullptr, nullptr, nullptr }, - { XSLFI_WATER_REGIONS, XSCF_IGNORABLE_ALL, 1, 1, "slv_water_regions", nullptr, nullptr, "WRGN" }, { XSLFI_TABLE_PATS, XSCF_NULL, 1, 1, "table_pats", nullptr, nullptr, nullptr }, diff --git a/src/sl/extended_ver_sl.h b/src/sl/extended_ver_sl.h index 1200ee803a..82c828f71c 100644 --- a/src/sl/extended_ver_sl.h +++ b/src/sl/extended_ver_sl.h @@ -157,7 +157,6 @@ enum SlXvFeatureIndex { XSLFI_SAVEGAME_ID, ///< See: SLV_SAVEGAME_ID (PR #10719) XSLFI_NEWGRF_LAST_SERVICE, ///< See: SLV_NEWGRF_LAST_SERVICE (PR #11124) XSLFI_CARGO_TRAVELLED, ///< See: SLV_CARGO_TRAVELLED (PR #11283) - XSLFI_WATER_REGIONS, ///< See: SLV_WATER_REGIONS (PR #11435) XSLFI_TABLE_PATS, ///< Use upstream table format for PATS diff --git a/src/sl/saveload.cpp b/src/sl/saveload.cpp index 61ee92d462..2713dc985a 100644 --- a/src/sl/saveload.cpp +++ b/src/sl/saveload.cpp @@ -302,7 +302,6 @@ static const std::vector &ChunkHandlers() extern const ChunkHandlerTable _tunnel_chunk_handlers; extern const ChunkHandlerTable _train_speed_adaptation_chunk_handlers; extern const ChunkHandlerTable _new_signal_chunk_handlers; - extern const ChunkHandlerTable _water_region_chunk_handlers; extern const ChunkHandlerTable _debug_chunk_handlers; /** List of all chunks in a savegame. */ @@ -351,7 +350,6 @@ static const std::vector &ChunkHandlers() _tunnel_chunk_handlers, _train_speed_adaptation_chunk_handlers, _new_signal_chunk_handlers, - _water_region_chunk_handlers, _debug_chunk_handlers, }; diff --git a/src/sl/water_regions_sl.cpp b/src/sl/water_regions_sl.cpp deleted file mode 100644 index 1aa4422a88..0000000000 --- a/src/sl/water_regions_sl.cpp +++ /dev/null @@ -1,27 +0,0 @@ -/* - * This file is part of OpenTTD. - * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. - * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . - */ - -/** @file water_regions_sl.cpp Handles saving and loading of water region data */ -#include "../stdafx.h" - -#include "saveload.h" - -extern SaveLoadVersion _sl_xv_upstream_version; - -struct GetWaterRegionsLoadInfo -{ - static SaveLoadVersion GetLoadVersion() - { - return _sl_xv_upstream_version != SL_MIN_VERSION ? _sl_xv_upstream_version : SLV_WATER_REGIONS; - } -}; - -static const ChunkHandler water_region_chunk_handlers[] = { - MakeUpstreamChunkHandler<'WRGN', GetWaterRegionsLoadInfo>(), -}; - -extern const ChunkHandlerTable _water_region_chunk_handlers(water_region_chunk_handlers);