22using TWaterRegionTraversabilityBits = uint16_t;
23constexpr TWaterRegionPatchLabel FIRST_REGION_LABEL = 1;
25static_assert(
sizeof(TWaterRegionTraversabilityBits) * 8 == WATER_REGION_EDGE_LENGTH);
26static_assert(
sizeof(TWaterRegionPatchLabel) ==
sizeof(uint8_t));
31static inline int GetWaterRegionX(
TileIndex tile) {
return TileX(tile) / WATER_REGION_EDGE_LENGTH; }
32static inline int GetWaterRegionY(
TileIndex tile) {
return TileY(tile) / WATER_REGION_EDGE_LENGTH; }
34static inline int GetWaterRegionMapSizeX() {
return Map::SizeX() / WATER_REGION_EDGE_LENGTH; }
35static inline int GetWaterRegionMapSizeY() {
return Map::SizeY() / WATER_REGION_EDGE_LENGTH; }
37static inline TWaterRegionIndex GetWaterRegionIndex(
int region_x,
int region_y) {
return GetWaterRegionMapSizeX() * region_y + region_x; }
38static inline TWaterRegionIndex GetWaterRegionIndex(
TileIndex tile) {
return GetWaterRegionIndex(GetWaterRegionX(tile), GetWaterRegionY(tile)); }
40using TWaterRegionPatchLabelArray = std::array<TWaterRegionPatchLabel, WATER_REGION_NUMBER_OF_TILES>;
48 std::array<TWaterRegionTraversabilityBits, DIAGDIR_END> edge_traversability_bits{};
49 std::unique_ptr<TWaterRegionPatchLabelArray> tile_patch_labels;
50 bool has_cross_region_aqueducts =
false;
51 TWaterRegionPatchLabel number_of_patches = 0;
74 assert(this->tile_area.
Contains(tile));
80 : data(water_region_data)
81 , tile_area(
TileXY(region_x * WATER_REGION_EDGE_LENGTH, region_y * WATER_REGION_EDGE_LENGTH), WATER_REGION_EDGE_LENGTH, WATER_REGION_EDGE_LENGTH)
100 int NumberOfPatches()
const {
return static_cast<int>(this->data.number_of_patches); }
114 assert(this->tile_area.
Contains(tile));
115 if (this->data.tile_patch_labels ==
nullptr) {
116 return this->
NumberOfPatches() == 0 ? INVALID_WATER_REGION_PATCH : FIRST_REGION_LABEL;
118 return (*this->data.tile_patch_labels)[this->
GetLocalIndex(tile)];
127 Debug(map, 3,
"Updating water region ({},{})", GetWaterRegionX(this->tile_area.
tile), GetWaterRegionY(this->tile_area.
tile));
128 this->data.has_cross_region_aqueducts =
false;
131 if (this->data.tile_patch_labels ==
nullptr) {
132 this->data.tile_patch_labels = std::make_unique<TWaterRegionPatchLabelArray>();
135 this->data.tile_patch_labels->fill(INVALID_WATER_REGION_PATCH);
136 this->data.edge_traversability_bits.fill(0);
138 TWaterRegionPatchLabel current_label = FIRST_REGION_LABEL;
139 TWaterRegionPatchLabel highest_assigned_label = INVALID_WATER_REGION_PATCH;
143 for (
const TileIndex start_tile : this->tile_area) {
144 static std::vector<TileIndex> tiles_to_check;
145 tiles_to_check.clear();
146 tiles_to_check.push_back(start_tile);
148 bool increase_label =
false;
149 while (!tiles_to_check.empty()) {
150 const TileIndex tile = tiles_to_check.back();
151 tiles_to_check.pop_back();
156 TWaterRegionPatchLabel &tile_patch = (*this->data.tile_patch_labels)[this->
GetLocalIndex(tile)];
157 if (tile_patch != INVALID_WATER_REGION_PATCH)
continue;
159 tile_patch = current_label;
160 highest_assigned_label = current_label;
161 increase_label =
true;
166 if (ft.
Follow(tile, dir)) {
168 tiles_to_check.push_back(ft.
new_tile);
173 SetBit(this->data.edge_traversability_bits[side], local_x_or_y);
175 this->data.has_cross_region_aqueducts =
true;
181 if (increase_label) current_label++;
184 this->data.number_of_patches = highest_assigned_label;
187 std::all_of(this->data.tile_patch_labels->begin(), this->data.tile_patch_labels->end(), [](TWaterRegionPatchLabel label) { return label == FIRST_REGION_LABEL; }))) {
189 this->data.tile_patch_labels.reset();
193 void PrintDebugInfo()
195 Debug(map, 9,
"Water region {},{} labels and edge traversability = ...", GetWaterRegionX(this->tile_area.
tile), GetWaterRegionY(this->tile_area.
tile));
197 const size_t max_element_width = std::to_string(this->
NumberOfPatches()).size();
200 Debug(map, 9,
" {:{}}", fmt::join(traversability,
" "), max_element_width);
201 Debug(map, 9,
" +{:->{}}+",
"", WATER_REGION_EDGE_LENGTH * (max_element_width + 1) + 1);
203 for (
int y = 0; y < WATER_REGION_EDGE_LENGTH; ++y) {
205 for (
int x = 0; x < WATER_REGION_EDGE_LENGTH; ++x) {
207 const std::string label_str = label == INVALID_WATER_REGION_PATCH ?
"." : std::to_string(label);
208 line = fmt::format(
"{:{}}", label_str, max_element_width) +
" " + line;
213 Debug(map, 9,
" +{:->{}}+",
"", WATER_REGION_EDGE_LENGTH * (max_element_width + 1) + 1);
215 Debug(map, 9,
" {:{}}", fmt::join(traversability,
" "), max_element_width);
219std::vector<WaterRegionData> _water_region_data;
220std::vector<bool> _is_water_region_valid;
222static TileIndex GetTileIndexFromLocalCoordinate(
int region_x,
int region_y,
int local_x,
int local_y)
224 assert(local_x >= 0 && local_x < WATER_REGION_EDGE_LENGTH);
225 assert(local_y >= 0 && local_y < WATER_REGION_EDGE_LENGTH);
226 return TileXY(WATER_REGION_EDGE_LENGTH * region_x + local_x, WATER_REGION_EDGE_LENGTH * region_y + local_y);
231 assert(x_or_y >= 0 && x_or_y < WATER_REGION_EDGE_LENGTH);
233 case DIAGDIR_NE:
return GetTileIndexFromLocalCoordinate(region_x, region_y, 0, x_or_y);
234 case DIAGDIR_SW:
return GetTileIndexFromLocalCoordinate(region_x, region_y, WATER_REGION_EDGE_LENGTH - 1, x_or_y);
235 case DIAGDIR_NW:
return GetTileIndexFromLocalCoordinate(region_x, region_y, x_or_y, 0);
236 case DIAGDIR_SE:
return GetTileIndexFromLocalCoordinate(region_x, region_y, x_or_y, WATER_REGION_EDGE_LENGTH - 1);
237 default: NOT_REACHED();
241static WaterRegion GetUpdatedWaterRegion(uint16_t region_x, uint16_t region_y)
243 const TWaterRegionIndex index = GetWaterRegionIndex(region_x, region_y);
244 WaterRegion water_region(region_x, region_y, _water_region_data[index]);
245 if (!_is_water_region_valid[index]) {
246 water_region.ForceUpdate();
247 _is_water_region_valid[index] =
true;
254 return GetUpdatedWaterRegion(GetWaterRegionX(tile), GetWaterRegionY(tile));
263 return GetWaterRegionIndex(water_region.
x, water_region.
y);
272 return water_region_patch.
label | GetWaterRegionIndex(water_region_patch) << 8;
282 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));
291 return WaterRegionDesc{ GetWaterRegionX(tile), GetWaterRegionY(tile) };
300 const WaterRegion region = GetUpdatedWaterRegion(tile);
312 auto invalidate_region = [](
TileIndex tile) {
313 const TWaterRegionIndex water_region_index = GetWaterRegionIndex(tile);
314 if (!_is_water_region_valid[water_region_index])
Debug(map, 3,
"Invalidated water region ({},{})", GetWaterRegionX(tile), GetWaterRegionY(tile));
315 _is_water_region_valid[water_region_index] =
false;
318 invalidate_region(tile);
326 if (GetWaterRegionIndex(adjacent_tile) != GetWaterRegionIndex(tile)) invalidate_region(adjacent_tile);
339 if (water_region_patch.
label == INVALID_WATER_REGION_PATCH)
return;
341 const WaterRegion current_region = GetUpdatedWaterRegion(water_region_patch.
x, water_region_patch.
y);
344 const int nx = water_region_patch.
x + offset.
x;
345 const int ny = water_region_patch.
y + offset.
y;
347 if (nx < 0 || ny < 0 || nx >= GetWaterRegionMapSizeX() || ny >= GetWaterRegionMapSizeY())
return;
349 const WaterRegion neighboring_region = GetUpdatedWaterRegion(nx, ny);
355 if (traversability_bits == 0)
return;
363 static std::vector<TWaterRegionPatchLabel> unique_labels;
364 unique_labels.clear();
365 for (
int x_or_y = 0; x_or_y < WATER_REGION_EDGE_LENGTH; ++x_or_y) {
366 if (!
HasBit(traversability_bits, x_or_y))
continue;
368 const TileIndex current_edge_tile = GetEdgeTileCoordinate(water_region_patch.
x, water_region_patch.
y, side, x_or_y);
369 const TWaterRegionPatchLabel current_label = current_region.
GetLabel(current_edge_tile);
370 if (current_label != water_region_patch.
label)
continue;
372 const TileIndex neighbor_edge_tile = GetEdgeTileCoordinate(nx, ny, opposite_side, x_or_y);
373 const TWaterRegionPatchLabel neighbor_label = neighboring_region.
GetLabel(neighbor_edge_tile);
374 assert(neighbor_label != INVALID_WATER_REGION_PATCH);
375 if (std::ranges::find(unique_labels, neighbor_label) == unique_labels.end()) unique_labels.push_back(neighbor_label);
377 for (TWaterRegionPatchLabel unique_label : unique_labels) func(
WaterRegionPatchDesc{ nx, ny, unique_label });
388 if (water_region_patch.
label == INVALID_WATER_REGION_PATCH)
return;
390 const WaterRegion current_region = GetUpdatedWaterRegion(water_region_patch.
x, water_region_patch.
y);
397 for (
const TileIndex tile : current_region) {
400 if (GetWaterRegionIndex(tile) != GetWaterRegionIndex(other_end_tile)) callback(
GetWaterRegionPatchInfo(other_end_tile));
411 const int number_of_regions = GetWaterRegionMapSizeX() * GetWaterRegionMapSizeY();
413 _water_region_data.clear();
414 _water_region_data.resize(number_of_regions);
416 _is_water_region_valid.clear();
417 _is_water_region_valid.resize(number_of_regions,
false);
419 Debug(map, 2,
"Allocating {} x {} water regions", GetWaterRegionMapSizeX(), GetWaterRegionMapSizeY());
420 assert(_is_water_region_valid.size() == _water_region_data.size());
423void PrintWaterRegionDebugInfo(
TileIndex tile)
425 GetUpdatedWaterRegion(tile).PrintDebugInfo();
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
TileIndex GetOtherBridgeEnd(TileIndex tile)
Starting at one bridge end finds the other bridge end.
bool IsBridgeTile(Tile t)
checks if there is a bridge on this tile
Iterator to iterate over a tile area (rectangle) of the map.
The data stored for each water region.
Represents a square section of the map of a fixed size.
TWaterRegionPatchLabel GetLabel(TileIndex tile) const
Returns the patch label that was assigned to the tile.
int NumberOfPatches() const
bool HasCrossRegionAqueducts() const
int GetLocalIndex(TileIndex tile) const
Returns the local index of the tile within the region.
void ForceUpdate()
Performs the connected component labeling and other data gathering.
TWaterRegionTraversabilityBits GetEdgeTraversabilityBits(DiagDirection side) const
Returns a set of bits indicating whether an edge tile on a particular side is traversable or not.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
DiagDirection
Enumeration for diagonal directions.
@ DIAGDIR_NE
Northeast, upper right on your monitor.
@ DIAGDIR_END
Used for iterations.
@ DIAGDIR_BEGIN
Used for iterations.
Template function for track followers.
TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
Returns information about trackdirs and signal states.
Functions related to OTTD's landscape.
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Functions related to maps.
DiagDirection DiagdirBetweenTiles(TileIndex tile_from, TileIndex tile_to)
Determines the DiagDirection to get from one tile to another.
TileIndex TileAddXY(TileIndex tile, int x, int y)
Adds a given offset to a tile.
TileIndex AddTileIndexDiffCWrap(TileIndex tile, TileIndexDiffC diff)
Add a TileIndexDiffC to a TileIndex and returns the new one.
static debug_inline TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
TileIndexDiffC TileIndexDiffCByDiagDir(DiagDirection dir)
Returns the TileIndexDiffC offset from a DiagDirection.
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition of base types and functions in a cross-platform compatible way.
Track follower helper template class (can serve pathfinders and vehicle controllers).
bool Follow(TileIndex old_tile, Trackdir old_td)
main follower routine.
bool is_bridge
last turn passed bridge ramp
TileIndex new_tile
the new tile (the vehicle has entered)
static uint SizeY()
Get the size of the map along the Y.
static debug_inline uint SizeX()
Get the size of the map along the X.
Represents the covered area of e.g.
OrthogonalTileIterator end() const
Returns an iterator to the end of the tile area.
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
TileIndex tile
The base tile of the area.
OrthogonalTileIterator begin() const
Returns an iterator to the beginning of the tile area.
Iterable ensemble of each set bit in a value.
A pair-construct of a TileIndexDiff.
int16_t x
The x value of the coordinate.
int16_t y
The y value of the coordinate.
Describes a single square water region.
int x
The X coordinate of the water region, i.e. X=2 is the 3rd water region along the X-axis.
int y
The Y coordinate of the water region, i.e. Y=2 is the 3rd water region along the Y-axis.
Describes a single interconnected patch of water within a particular water region.
TWaterRegionPatchLabel label
Unique label identifying the patch within the region.
int y
The Y coordinate of the water region, i.e. Y=2 is the 3rd water region along the Y-axis.
int x
The X coordinate of the water region, i.e. X=2 is the 3rd water region along the X-axis.
bool IsValidTile(Tile tile)
Checks if a tile is valid.
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Type for storing the 'area' of something uses on the map.
Different conversion functions from one kind of track to another.
TrackdirBits TrackBitsToTrackdirBits(TrackBits bits)
Converts TrackBits to TrackdirBits while allowing both directions.
TrackBits TrackStatusToTrackBits(TrackStatus ts)
Returns the present-track-information of a TrackStatus.
TrackBits
Allow incrementing of Track variables.
Trackdir
Enumeration for tracks and directions.
TrackdirBits
Allow incrementing of Trackdir variables.
@ TRACKDIR_BIT_NONE
No track build.
Base types related to transport.
@ TRANSPORT_WATER
Transport over water.
Functions that have tunnels and bridges in common.
TransportType GetTunnelBridgeTransportType(Tile t)
Tunnel: Get the transport type of the tunnel (road or rail) Bridge: Get the transport type of the bri...
void AllocateWaterRegions()
Allocates the appropriate amount of water regions for the current map size.
static void VisitAdjacentWaterRegionPatchNeighbors(const WaterRegionPatchDesc &water_region_patch, DiagDirection side, TVisitWaterRegionPatchCallBack &func)
Calls the provided callback function for all water region patches accessible from one particular side...
WaterRegionDesc GetWaterRegionInfo(TileIndex tile)
Returns basic water region information for the provided tile.
WaterRegionPatchDesc GetWaterRegionPatchInfo(TileIndex tile)
Returns basic water region patch information for the provided tile.
TileIndex GetWaterRegionCenterTile(const WaterRegionDesc &water_region)
Returns the center tile of a particular water region.
void InvalidateWaterRegion(TileIndex tile)
Marks the water region that tile is part of as invalid.
void VisitWaterRegionPatchNeighbors(const WaterRegionPatchDesc &water_region_patch, TVisitWaterRegionPatchCallBack &callback)
Calls the provided callback function on all accessible water region patches in each cardinal directio...
int CalculateWaterRegionPatchHash(const WaterRegionPatchDesc &water_region_patch)
Calculates a number that uniquely identifies the provided water region patch.
Handles dividing the water in the map into regions to assist pathfinding.