No longer save/load water region invalidation states

Move InitializeWaterRegions to AllocateMap
No longer ForceUpdate in InitializeWaterRegions
This commit is contained in:
Jonathan G Rennison 2024-01-10 00:51:44 +00:00
parent 9206fa1fce
commit 936d636cdd
11 changed files with 10 additions and 72 deletions

View File

@ -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();

View File

@ -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 <array>
@ -133,6 +134,8 @@ void AllocateMap(uint size_x, uint size_y)
_m = reinterpret_cast<Tile *>(buf);
_me = reinterpret_cast<TileExtended *>(buf + (_map_size * sizeof(Tile)));
InitializeWaterRegions();
}

View File

@ -19,6 +19,7 @@
#include "follow_track.hpp"
#include "ship.h"
#include <algorithm>
#include <array>
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<WaterRegion> _water_regions;
@ -379,27 +385,6 @@ void VisitWaterRegionPatchNeighbors(const WaterRegionPatchDesc &water_region_pat
}
}
std::vector<WaterRegionSaveLoadInfo> GetWaterRegionSaveLoadInfo()
{
std::vector<WaterRegionSaveLoadInfo> result;
for (WaterRegion &region : _water_regions) result.push_back({ region.IsInitialized() });
return result;
}
void LoadWaterRegions(const std::vector<WaterRegionSaveLoadInfo> &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 &region = _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);
}
}
}

View File

@ -76,7 +76,4 @@ struct WaterRegionSaveLoadInfo
bool initialized;
};
std::vector<WaterRegionSaveLoadInfo> GetWaterRegionSaveLoadInfo();
void LoadWaterRegions(const std::vector<WaterRegionSaveLoadInfo> &save_load_info);
#endif /* WATER_REGIONS_H */

View File

@ -4399,8 +4399,6 @@ bool AfterLoadGame()
c->settings = _settings_client.company;
}
if (IsSavegameVersionBefore(SLV_WATER_REGIONS) && SlXvIsFeatureMissing(XSLFI_WATER_REGIONS)) InitializeWaterRegions();
return true;
}

View File

@ -26,12 +26,6 @@ struct WRGNChunkHandler : ChunkHandler {
void Save() const override
{
SlTableHeader(_water_region_desc);
int index = 0;
for (WaterRegionSaveLoadInfo &region : GetWaterRegionSaveLoadInfo()) {
SlSetArrayIndex(index++);
SlObject(&region, _water_region_desc);
}
}
void Load() const override
@ -40,14 +34,10 @@ struct WRGNChunkHandler : ChunkHandler {
int index;
std::vector<WaterRegionSaveLoadInfo> loaded_info;
while ((index = SlIterateArray()) != -1) {
WaterRegionSaveLoadInfo region_info;
SlObject(&region_info, slt);
loaded_info.push_back(std::move(region_info));
}
LoadWaterRegions(loaded_info);
}
};

View File

@ -54,6 +54,5 @@ add_files(
train_speed_adaptation.cpp
tunnel_sl.cpp
vehicle_sl.cpp
water_regions_sl.cpp
waypoint_sl.cpp
)

View File

@ -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 },

View File

@ -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

View File

@ -302,7 +302,6 @@ static const std::vector<ChunkHandler> &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<ChunkHandler> &ChunkHandlers()
_tunnel_chunk_handlers,
_train_speed_adaptation_chunk_handlers,
_new_signal_chunk_handlers,
_water_region_chunk_handlers,
_debug_chunk_handlers,
};

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
/** @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);