23 using TWaterRegionTraversabilityBits = uint16_t;
24 constexpr TWaterRegionPatchLabel FIRST_REGION_LABEL = 1;
25 constexpr TWaterRegionPatchLabel INVALID_WATER_REGION_PATCH = 0;
27 static_assert(
sizeof(TWaterRegionTraversabilityBits) * 8 == WATER_REGION_EDGE_LENGTH);
28 static_assert(
sizeof(TWaterRegionPatchLabel) ==
sizeof(
byte));
33 static inline int GetWaterRegionX(
TileIndex tile) {
return TileX(tile) / WATER_REGION_EDGE_LENGTH; }
34 static inline int GetWaterRegionY(
TileIndex tile) {
return TileY(tile) / WATER_REGION_EDGE_LENGTH; }
36 static inline int GetWaterRegionMapSizeX() {
return Map::SizeX() / WATER_REGION_EDGE_LENGTH; }
37 static inline int GetWaterRegionMapSizeY() {
return Map::SizeY() / WATER_REGION_EDGE_LENGTH; }
39 static inline TWaterRegionIndex
GetWaterRegionIndex(
int region_x,
int region_y) {
return GetWaterRegionMapSizeX() * region_y + region_x; }
51 std::array<TWaterRegionTraversabilityBits, DIAGDIR_END> edge_traversability_bits{};
52 bool has_cross_region_aqueducts =
false;
53 TWaterRegionPatchLabel number_of_patches = 0;
55 std::array<TWaterRegionPatchLabel, WATER_REGION_NUMBER_OF_TILES> tile_patch_labels{};
56 bool initialized =
false;
66 assert(this->tile_area.
Contains(tile));
72 : tile_area(
TileXY(region_x * WATER_REGION_EDGE_LENGTH, region_y * WATER_REGION_EDGE_LENGTH), WATER_REGION_EDGE_LENGTH, WATER_REGION_EDGE_LENGTH)
78 bool IsInitialized()
const {
return this->initialized; }
82 if (!IsInitialized())
Debug(map, 3,
"Invalidated water region ({},{})", GetWaterRegionX(this->tile_area.
tile), GetWaterRegionY(this->tile_area.
tile));
83 this->initialized =
false;
113 assert(this->tile_area.
Contains(tile));
123 Debug(map, 3,
"Updating water region ({},{})", GetWaterRegionX(this->tile_area.
tile), GetWaterRegionY(this->tile_area.
tile));
124 this->has_cross_region_aqueducts =
false;
126 this->tile_patch_labels.fill(INVALID_WATER_REGION_PATCH);
127 this->edge_traversability_bits.fill(0);
129 TWaterRegionPatchLabel current_label = 1;
130 TWaterRegionPatchLabel highest_assigned_label = 0;
134 for (
const TileIndex start_tile : tile_area) {
135 static std::vector<TileIndex> tiles_to_check;
136 tiles_to_check.clear();
137 tiles_to_check.push_back(start_tile);
139 bool increase_label =
false;
140 while (!tiles_to_check.empty()) {
141 const TileIndex tile = tiles_to_check.back();
142 tiles_to_check.pop_back();
147 if (this->tile_patch_labels[
GetLocalIndex(tile)] != INVALID_WATER_REGION_PATCH)
continue;
149 this->tile_patch_labels[
GetLocalIndex(tile)] = current_label;
150 highest_assigned_label = current_label;
151 increase_label =
true;
156 if (ft.
Follow(tile, dir)) {
157 if (this->tile_area.Contains(ft.
m_new_tile)) {
163 SetBit(this->edge_traversability_bits[side], local_x_or_y);
165 this->has_cross_region_aqueducts =
true;
171 if (increase_label) current_label++;
174 this->number_of_patches = highest_assigned_label;
175 this->initialized =
true;
186 void PrintDebugInfo()
188 Debug(map, 9,
"Water region {},{} labels and edge traversability = ...", GetWaterRegionX(tile_area.
tile), GetWaterRegionY(tile_area.
tile));
190 const size_t max_element_width = std::to_string(this->number_of_patches).size();
192 std::array<int, 16> traversability_NW{0};
193 for (
auto bitIndex :
SetBitIterator(edge_traversability_bits[
DIAGDIR_NW])) *(traversability_NW.rbegin() + bitIndex) = 1;
194 Debug(map, 9,
" {:{}}", fmt::join(traversability_NW,
" "), max_element_width);
195 Debug(map, 9,
" +{:->{}}+",
"", WATER_REGION_EDGE_LENGTH * (max_element_width + 1) + 1);
197 for (
int y = 0; y < WATER_REGION_EDGE_LENGTH; ++y) {
199 for (
int x = 0; x < WATER_REGION_EDGE_LENGTH; ++x) {
200 const auto label = this->tile_patch_labels[x + y * WATER_REGION_EDGE_LENGTH];
201 const std::string label_str = label == INVALID_WATER_REGION_PATCH ?
"." : std::to_string(label);
202 line = fmt::format(
"{:{}}", label_str, max_element_width) +
" " + line;
204 Debug(map, 9,
"{} | {}| {}",
GB(this->edge_traversability_bits[
DIAGDIR_SW], y, 1), line,
GB(this->edge_traversability_bits[
DIAGDIR_NE], y, 1));
207 Debug(map, 9,
" +{:->{}}+",
"", WATER_REGION_EDGE_LENGTH * (max_element_width + 1) + 1);
208 std::array<int, 16> traversability_SE{0};
209 for (
auto bitIndex :
SetBitIterator(edge_traversability_bits[
DIAGDIR_SE])) *(traversability_SE.rbegin() + bitIndex) = 1;
210 Debug(map, 9,
" {:{}}", fmt::join(traversability_SE,
" "), max_element_width);
214 std::vector<WaterRegion> _water_regions;
216 TileIndex GetTileIndexFromLocalCoordinate(
int region_x,
int region_y,
int local_x,
int local_y)
218 assert(local_x >= 0 && local_x < WATER_REGION_EDGE_LENGTH);
219 assert(local_y >= 0 && local_y < WATER_REGION_EDGE_LENGTH);
220 return TileXY(WATER_REGION_EDGE_LENGTH * region_x + local_x, WATER_REGION_EDGE_LENGTH * region_y + local_y);
225 assert(x_or_y >= 0 && x_or_y < WATER_REGION_EDGE_LENGTH);
227 case DIAGDIR_NE:
return GetTileIndexFromLocalCoordinate(region_x, region_y, 0, x_or_y);
228 case DIAGDIR_SW:
return GetTileIndexFromLocalCoordinate(region_x, region_y, WATER_REGION_EDGE_LENGTH - 1, x_or_y);
229 case DIAGDIR_NW:
return GetTileIndexFromLocalCoordinate(region_x, region_y, x_or_y, 0);
230 case DIAGDIR_SE:
return GetTileIndexFromLocalCoordinate(region_x, region_y, x_or_y, WATER_REGION_EDGE_LENGTH - 1);
231 default: NOT_REACHED();
235 WaterRegion &GetUpdatedWaterRegion(uint16_t region_x, uint16_t region_y)
238 result.UpdateIfNotInitialized();
245 result.UpdateIfNotInitialized();
274 return TileXY(water_region.
x * WATER_REGION_EDGE_LENGTH + (WATER_REGION_EDGE_LENGTH / 2), water_region.
y * WATER_REGION_EDGE_LENGTH + (WATER_REGION_EDGE_LENGTH / 2));
283 return WaterRegionDesc{ GetWaterRegionX(tile), GetWaterRegionY(tile) };
304 _water_regions[water_region_index].Invalidate();
311 if (adjacent_region_index != water_region_index) _water_regions[adjacent_region_index].Invalidate();
324 const WaterRegion ¤t_region = GetUpdatedWaterRegion(water_region_patch.
x, water_region_patch.
y);
327 const int nx = water_region_patch.
x + offset.
x;
328 const int ny = water_region_patch.
y + offset.
y;
330 if (nx < 0 || ny < 0 || nx >= GetWaterRegionMapSizeX() || ny >= GetWaterRegionMapSizeY())
return;
332 const WaterRegion &neighboring_region = GetUpdatedWaterRegion(nx, ny);
338 if (traversability_bits == 0)
return;
346 static std::vector<TWaterRegionPatchLabel> unique_labels;
347 unique_labels.clear();
348 for (
int x_or_y = 0; x_or_y < WATER_REGION_EDGE_LENGTH; ++x_or_y) {
349 if (!
HasBit(traversability_bits, x_or_y))
continue;
351 const TileIndex current_edge_tile = GetEdgeTileCoordinate(water_region_patch.
x, water_region_patch.
y, side, x_or_y);
352 const TWaterRegionPatchLabel current_label = current_region.
GetLabel(current_edge_tile);
353 if (current_label != water_region_patch.
label)
continue;
355 const TileIndex neighbor_edge_tile = GetEdgeTileCoordinate(nx, ny, opposite_side, x_or_y);
356 const TWaterRegionPatchLabel neighbor_label = neighboring_region.
GetLabel(neighbor_edge_tile);
357 if (std::find(unique_labels.begin(), unique_labels.end(), neighbor_label) == unique_labels.end()) unique_labels.push_back(neighbor_label);
359 for (TWaterRegionPatchLabel unique_label : unique_labels) func(
WaterRegionPatchDesc{ nx, ny, unique_label });
370 const WaterRegion ¤t_region = GetUpdatedWaterRegion(water_region_patch.
x, water_region_patch.
y);
377 for (
const TileIndex tile : current_region) {
391 _water_regions.clear();
392 _water_regions.reserve(
static_cast<size_t>(GetWaterRegionMapSizeX()) * GetWaterRegionMapSizeY());
394 Debug(map, 2,
"Allocating {} x {} water regions", GetWaterRegionMapSizeX(), GetWaterRegionMapSizeY());
396 for (
int region_y = 0; region_y < GetWaterRegionMapSizeY(); region_y++) {
397 for (
int region_x = 0; region_x < GetWaterRegionMapSizeX(); region_x++) {
398 _water_regions.emplace_back(region_x, region_y);
403 void PrintWaterRegionDebugInfo(
TileIndex tile)
405 GetUpdatedWaterRegion(tile).PrintDebugInfo();