From b45b8e77f3a71eda16402778b98760235c977615 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Wed, 10 Jan 2024 17:40:51 +0000 Subject: [PATCH] Add cache checking of water regions --- src/debug_desync.h | 1 + src/openttd.cpp | 5 ++++ src/pathfinder/water_regions.cpp | 49 ++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/src/debug_desync.h b/src/debug_desync.h index 4e6fdf9ce8..745e9ca5e9 100644 --- a/src/debug_desync.h +++ b/src/debug_desync.h @@ -16,6 +16,7 @@ enum CheckCachesFlags : uint32_t { CHECK_CACHE_NONE = 0, CHECK_CACHE_GENERAL = 1 << 0, CHECK_CACHE_INFRA_TOTALS = 1 << 1, + CHECK_CACHE_WATER_REGIONS = 1 << 2, CHECK_CACHE_ALL = UINT16_MAX, CHECK_CACHE_EMIT_LOG = 1 << 16, }; diff --git a/src/openttd.cpp b/src/openttd.cpp index efa36b0716..70b79b3522 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -2052,6 +2052,11 @@ void CheckCaches(bool force_check, std::function log, CheckC } } + if (flags & CHECK_CACHE_WATER_REGIONS) { + extern void WaterRegionCheckCaches(std::function log); + WaterRegionCheckCaches(log); + } + if ((flags & CHECK_CACHE_EMIT_LOG) && !saved_messages.empty()) { InconsistencyExtraInfo info; info.check_caches_result = std::move(saved_messages); diff --git a/src/pathfinder/water_regions.cpp b/src/pathfinder/water_regions.cpp index 47856f6fe8..b38e6c7b7d 100644 --- a/src/pathfinder/water_regions.cpp +++ b/src/pathfinder/water_regions.cpp @@ -260,6 +260,17 @@ public: { return this->wr.tile_patch_labels != nullptr; } + + TWaterRegionPatchLabelArray CopyPatchLabelArray() const + { + TWaterRegionPatchLabelArray out; + if (this->HasPatchStorage()) { + out = *this->wr.tile_patch_labels; + } else { + out.fill(this->NumberOfPatches() == 0 ? INVALID_WATER_REGION_PATCH : 1); + } + return out; + } }; std::vector _water_regions; @@ -472,3 +483,41 @@ uint GetWaterRegionTileDebugColourIndex(TileIndex tile) return 0; } } + +void WaterRegionCheckCaches(std::function log) +{ + char cclog_buffer[1024]; +#define CCLOG(...) { \ + char *cc_log_pos = cclog_buffer + seprintf(cclog_buffer, lastof(cclog_buffer), "Region: %u x %u to %u x %u: ", \ + x * WATER_REGION_EDGE_LENGTH, y * WATER_REGION_EDGE_LENGTH, (x * WATER_REGION_EDGE_LENGTH) + WATER_REGION_EDGE_MASK, (y * WATER_REGION_EDGE_LENGTH) + WATER_REGION_EDGE_MASK); \ + seprintf(cc_log_pos, lastof(cclog_buffer), __VA_ARGS__); \ + DEBUG(desync, 0, "%s", cclog_buffer); \ + if (log) log(cclog_buffer); \ +} + + const uint32_t size_x = GetWaterRegionMapSizeX(); + const uint32_t size_y = GetWaterRegionMapSizeY(); + for (uint32_t y = 0; y < size_y; y++) { + for (uint32_t x = 0; x < size_x; x++) { + WaterRegionReference wr = GetWaterRegionRef(x, y); + if (!wr.IsInitialized()) continue; + + const bool old_has_cross_region_aqueducts = wr.HasCrossRegionAqueducts(); + const int old_number_of_patches = wr.NumberOfPatches(); + const TWaterRegionPatchLabelArray old_patch_labels = wr.CopyPatchLabelArray(); + + wr.ForceUpdate(); + + if (old_has_cross_region_aqueducts != wr.HasCrossRegionAqueducts()) { + CCLOG("Has cross region aqueducts mismatch: %u -> %u", old_has_cross_region_aqueducts, wr.HasCrossRegionAqueducts()); + } + if (old_number_of_patches != wr.NumberOfPatches()) { + CCLOG("Number of patches mismatch: %u -> %u", old_number_of_patches, wr.NumberOfPatches()); + } + if (old_patch_labels != wr.CopyPatchLabelArray()) { + CCLOG("Patch label mismatch"); + } + } + } +#undef CCLOG +}