23 using TWaterRegionTraversabilityBits = uint16_t;
24 constexpr TWaterRegionPatchLabel FIRST_REGION_LABEL = 1;
26 static_assert(
sizeof(TWaterRegionTraversabilityBits) * 8 == WATER_REGION_EDGE_LENGTH);
27 static_assert(
sizeof(TWaterRegionPatchLabel) ==
sizeof(
byte));
32 static inline int GetWaterRegionX(
TileIndex tile) {
return TileX(tile) / WATER_REGION_EDGE_LENGTH; }
33 static inline int GetWaterRegionY(
TileIndex tile) {
return TileY(tile) / WATER_REGION_EDGE_LENGTH; }
35 static inline int GetWaterRegionMapSizeX() {
return Map::SizeX() / WATER_REGION_EDGE_LENGTH; }
36 static inline int GetWaterRegionMapSizeY() {
return Map::SizeY() / WATER_REGION_EDGE_LENGTH; }
38 static inline TWaterRegionIndex
GetWaterRegionIndex(
int region_x,
int region_y) {
return GetWaterRegionMapSizeX() * region_y + region_x; }
50 std::array<TWaterRegionTraversabilityBits, DIAGDIR_END> edge_traversability_bits{};
51 bool has_cross_region_aqueducts =
false;
52 TWaterRegionPatchLabel number_of_patches = 0;
54 std::array<TWaterRegionPatchLabel, WATER_REGION_NUMBER_OF_TILES> tile_patch_labels{};
55 bool initialized =
false;
65 assert(this->tile_area.
Contains(tile));
71 : tile_area(
TileXY(region_x * WATER_REGION_EDGE_LENGTH, region_y * WATER_REGION_EDGE_LENGTH), WATER_REGION_EDGE_LENGTH, WATER_REGION_EDGE_LENGTH)
77 bool IsInitialized()
const {
return this->initialized; }
81 if (!IsInitialized())
Debug(map, 3,
"Invalidated water region ({},{})", GetWaterRegionX(this->tile_area.
tile), GetWaterRegionY(this->tile_area.
tile));
82 this->initialized =
false;
112 assert(this->tile_area.
Contains(tile));
122 Debug(map, 3,
"Updating water region ({},{})", GetWaterRegionX(this->tile_area.
tile), GetWaterRegionY(this->tile_area.
tile));
123 this->has_cross_region_aqueducts =
false;
125 this->tile_patch_labels.fill(INVALID_WATER_REGION_PATCH);
126 this->edge_traversability_bits.fill(0);
128 TWaterRegionPatchLabel current_label = 1;
129 TWaterRegionPatchLabel highest_assigned_label = 0;
133 for (
const TileIndex start_tile : tile_area) {
134 static std::vector<TileIndex> tiles_to_check;
135 tiles_to_check.clear();
136 tiles_to_check.push_back(start_tile);
138 bool increase_label =
false;
139 while (!tiles_to_check.empty()) {
140 const TileIndex tile = tiles_to_check.back();
141 tiles_to_check.pop_back();
146 if (this->tile_patch_labels[
GetLocalIndex(tile)] != INVALID_WATER_REGION_PATCH)
continue;
148 this->tile_patch_labels[
GetLocalIndex(tile)] = current_label;
149 highest_assigned_label = current_label;
150 increase_label =
true;
155 if (ft.
Follow(tile, dir)) {
156 if (this->tile_area.Contains(ft.
m_new_tile)) {
162 SetBit(this->edge_traversability_bits[side], local_x_or_y);
164 this->has_cross_region_aqueducts =
true;
170 if (increase_label) current_label++;
173 this->number_of_patches = highest_assigned_label;
174 this->initialized =
true;
185 void PrintDebugInfo()
187 Debug(map, 9,
"Water region {},{} labels and edge traversability = ...", GetWaterRegionX(tile_area.
tile), GetWaterRegionY(tile_area.
tile));
189 const size_t max_element_width = std::to_string(this->number_of_patches).size();
191 std::array<int, 16> traversability_NW{0};
192 for (
auto bitIndex :
SetBitIterator(edge_traversability_bits[
DIAGDIR_NW])) *(traversability_NW.rbegin() + bitIndex) = 1;
193 Debug(map, 9,
" {:{}}", fmt::join(traversability_NW,
" "), max_element_width);
194 Debug(map, 9,
" +{:->{}}+",
"", WATER_REGION_EDGE_LENGTH * (max_element_width + 1) + 1);
196 for (
int y = 0; y < WATER_REGION_EDGE_LENGTH; ++y) {
198 for (
int x = 0; x < WATER_REGION_EDGE_LENGTH; ++x) {
199 const auto label = this->tile_patch_labels[x + y * WATER_REGION_EDGE_LENGTH];
200 const std::string label_str = label == INVALID_WATER_REGION_PATCH ?
"." : std::to_string(label);
201 line = fmt::format(
"{:{}}", label_str, max_element_width) +
" " + line;
203 Debug(map, 9,
"{} | {}| {}",
GB(this->edge_traversability_bits[
DIAGDIR_SW], y, 1), line,
GB(this->edge_traversability_bits[
DIAGDIR_NE], y, 1));
206 Debug(map, 9,
" +{:->{}}+",
"", WATER_REGION_EDGE_LENGTH * (max_element_width + 1) + 1);
207 std::array<int, 16> traversability_SE{0};
208 for (
auto bitIndex :
SetBitIterator(edge_traversability_bits[
DIAGDIR_SE])) *(traversability_SE.rbegin() + bitIndex) = 1;
209 Debug(map, 9,
" {:{}}", fmt::join(traversability_SE,
" "), max_element_width);
213 std::vector<WaterRegion> _water_regions;
215 TileIndex GetTileIndexFromLocalCoordinate(
int region_x,
int region_y,
int local_x,
int local_y)
217 assert(local_x >= 0 && local_x < WATER_REGION_EDGE_LENGTH);
218 assert(local_y >= 0 && local_y < WATER_REGION_EDGE_LENGTH);
219 return TileXY(WATER_REGION_EDGE_LENGTH * region_x + local_x, WATER_REGION_EDGE_LENGTH * region_y + local_y);
224 assert(x_or_y >= 0 && x_or_y < WATER_REGION_EDGE_LENGTH);
226 case DIAGDIR_NE:
return GetTileIndexFromLocalCoordinate(region_x, region_y, 0, x_or_y);
227 case DIAGDIR_SW:
return GetTileIndexFromLocalCoordinate(region_x, region_y, WATER_REGION_EDGE_LENGTH - 1, x_or_y);
228 case DIAGDIR_NW:
return GetTileIndexFromLocalCoordinate(region_x, region_y, x_or_y, 0);
229 case DIAGDIR_SE:
return GetTileIndexFromLocalCoordinate(region_x, region_y, x_or_y, WATER_REGION_EDGE_LENGTH - 1);
230 default: NOT_REACHED();
234 WaterRegion &GetUpdatedWaterRegion(uint16_t region_x, uint16_t region_y)
237 result.UpdateIfNotInitialized();
244 result.UpdateIfNotInitialized();
273 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));
282 return WaterRegionDesc{ GetWaterRegionX(tile), GetWaterRegionY(tile) };
303 _water_regions[water_region_index].Invalidate();
310 if (adjacent_region_index != water_region_index) _water_regions[adjacent_region_index].Invalidate();
323 if (water_region_patch.
label == INVALID_WATER_REGION_PATCH)
return;
325 const WaterRegion ¤t_region = GetUpdatedWaterRegion(water_region_patch.
x, water_region_patch.
y);
328 const int nx = water_region_patch.
x + offset.
x;
329 const int ny = water_region_patch.
y + offset.
y;
331 if (nx < 0 || ny < 0 || nx >= GetWaterRegionMapSizeX() || ny >= GetWaterRegionMapSizeY())
return;
333 const WaterRegion &neighboring_region = GetUpdatedWaterRegion(nx, ny);
339 if (traversability_bits == 0)
return;
347 static std::vector<TWaterRegionPatchLabel> unique_labels;
348 unique_labels.clear();
349 for (
int x_or_y = 0; x_or_y < WATER_REGION_EDGE_LENGTH; ++x_or_y) {
350 if (!
HasBit(traversability_bits, x_or_y))
continue;
352 const TileIndex current_edge_tile = GetEdgeTileCoordinate(water_region_patch.
x, water_region_patch.
y, side, x_or_y);
353 const TWaterRegionPatchLabel current_label = current_region.
GetLabel(current_edge_tile);
354 if (current_label != water_region_patch.
label)
continue;
356 const TileIndex neighbor_edge_tile = GetEdgeTileCoordinate(nx, ny, opposite_side, x_or_y);
357 const TWaterRegionPatchLabel neighbor_label = neighboring_region.
GetLabel(neighbor_edge_tile);
358 assert(neighbor_label != INVALID_WATER_REGION_PATCH);
359 if (std::find(unique_labels.begin(), unique_labels.end(), neighbor_label) == unique_labels.end()) unique_labels.push_back(neighbor_label);
361 for (TWaterRegionPatchLabel unique_label : unique_labels) func(
WaterRegionPatchDesc{ nx, ny, unique_label });
372 if (water_region_patch.
label == INVALID_WATER_REGION_PATCH)
return;
374 const WaterRegion ¤t_region = GetUpdatedWaterRegion(water_region_patch.
x, water_region_patch.
y);
381 for (
const TileIndex tile : current_region) {
395 _water_regions.clear();
396 _water_regions.reserve(
static_cast<size_t>(GetWaterRegionMapSizeX()) * GetWaterRegionMapSizeY());
398 Debug(map, 2,
"Allocating {} x {} water regions", GetWaterRegionMapSizeX(), GetWaterRegionMapSizeY());
400 for (
int region_y = 0; region_y < GetWaterRegionMapSizeY(); region_y++) {
401 for (
int region_x = 0; region_x < GetWaterRegionMapSizeX(); region_x++) {
402 _water_regions.emplace_back(region_x, region_y);
407 void PrintWaterRegionDebugInfo(
TileIndex tile)
409 GetUpdatedWaterRegion(tile).PrintDebugInfo();