OpenTTD Source 20250604-master-g31fbb17c5b
water_regions.cpp File Reference

Handles dividing the water in the map into square regions to assist pathfinding. More...

#include "../stdafx.h"
#include "../map_func.h"
#include "water_regions.h"
#include "../tilearea_type.h"
#include "../track_func.h"
#include "../transport_type.h"
#include "../landscape.h"
#include "../tunnelbridge_map.h"
#include "follow_track.hpp"
#include "../ship.h"
#include "../debug.h"
#include "../3rdparty/fmt/ranges.h"
#include "../core/convertible_through_base.hpp"
#include "../safeguards.h"

Go to the source code of this file.

Data Structures

class  WaterRegionData
 The data stored for each water region. More...
 
class  WaterRegion
 Represents a square section of the map of a fixed size. More...
 

Typedefs

using WaterRegionTraversabilityBits = uint16_t
 
using WaterRegionPatchLabelArray = std::array< WaterRegionPatchLabel, WATER_REGION_NUMBER_OF_TILES >
 

Functions

static TrackBits GetWaterTracks (TileIndex tile)
 
static bool IsAqueductTile (TileIndex tile)
 
static int GetWaterRegionX (TileIndex tile)
 
static int GetWaterRegionY (TileIndex tile)
 
static int GetWaterRegionMapSizeX ()
 
static int GetWaterRegionMapSizeY ()
 
static WaterRegionIndex GetWaterRegionIndex (int region_x, int region_y)
 
static WaterRegionIndex GetWaterRegionIndex (TileIndex tile)
 
static TileIndex GetTileIndexFromLocalCoordinate (int region_x, int region_y, int local_x, int local_y)
 
static TileIndex GetEdgeTileCoordinate (int region_x, int region_y, DiagDirection side, int x_or_y)
 
static WaterRegion GetUpdatedWaterRegion (uint16_t region_x, uint16_t region_y)
 
static WaterRegion GetUpdatedWaterRegion (TileIndex tile)
 
static WaterRegionIndex GetWaterRegionIndex (const WaterRegionDesc &water_region)
 Returns the index of the water region.
 
int CalculateWaterRegionPatchHash (const WaterRegionPatchDesc &water_region_patch)
 Calculates a number that uniquely identifies the provided water region patch.
 
TileIndex GetWaterRegionCenterTile (const WaterRegionDesc &water_region)
 Returns the center tile of a particular water region.
 
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.
 
void InvalidateWaterRegion (TileIndex tile)
 Marks the water region that tile is part of as invalid.
 
static void VisitAdjacentWaterRegionPatchNeighbours (const WaterRegionPatchDesc &water_region_patch, DiagDirection side, VisitWaterRegionPatchCallback &func)
 Calls the provided callback function for all water region patches accessible from one particular side of the starting patch.
 
void VisitWaterRegionPatchNeighbours (const WaterRegionPatchDesc &water_region_patch, VisitWaterRegionPatchCallback &callback)
 Calls the provided callback function on all accessible water region patches in each cardinal direction, plus any others that are reachable via aqueducts.
 
void AllocateWaterRegions ()
 Allocates the appropriate amount of water regions for the current map size.
 
void PrintWaterRegionDebugInfo (TileIndex tile)
 

Variables

constexpr WaterRegionPatchLabel FIRST_REGION_LABEL {1}
 
ReferenceThroughBaseContainer< std::vector< WaterRegionData > > _water_region_data
 
ReferenceThroughBaseContainer< std::vector< bool > > _is_water_region_valid
 

Detailed Description

Handles dividing the water in the map into square regions to assist pathfinding.

Definition in file water_regions.cpp.

Typedef Documentation

◆ WaterRegionPatchLabelArray

using WaterRegionPatchLabelArray = std::array<WaterRegionPatchLabel, WATER_REGION_NUMBER_OF_TILES>

Definition at line 43 of file water_regions.cpp.

◆ WaterRegionTraversabilityBits

using WaterRegionTraversabilityBits = uint16_t

Definition at line 25 of file water_regions.cpp.

Function Documentation

◆ AllocateWaterRegions()

void AllocateWaterRegions ( )

Allocates the appropriate amount of water regions for the current map size.

Definition at line 415 of file water_regions.cpp.

References Debug.

Referenced by Map::Allocate().

◆ CalculateWaterRegionPatchHash()

int CalculateWaterRegionPatchHash ( const WaterRegionPatchDesc water_region_patch)

Calculates a number that uniquely identifies the provided water region patch.

Parameters
water_region_patchThe Water region to calculate the hash for.

Definition at line 276 of file water_regions.cpp.

References WaterRegionPatchDesc::label.

◆ GetEdgeTileCoordinate()

static TileIndex GetEdgeTileCoordinate ( int  region_x,
int  region_y,
DiagDirection  side,
int  x_or_y 
)
static

Definition at line 235 of file water_regions.cpp.

◆ GetTileIndexFromLocalCoordinate()

static TileIndex GetTileIndexFromLocalCoordinate ( int  region_x,
int  region_y,
int  local_x,
int  local_y 
)
static

Definition at line 228 of file water_regions.cpp.

◆ GetUpdatedWaterRegion() [1/2]

static WaterRegion GetUpdatedWaterRegion ( TileIndex  tile)
static

Definition at line 258 of file water_regions.cpp.

◆ GetUpdatedWaterRegion() [2/2]

static WaterRegion GetUpdatedWaterRegion ( uint16_t  region_x,
uint16_t  region_y 
)
static

Definition at line 247 of file water_regions.cpp.

◆ GetWaterRegionCenterTile()

TileIndex GetWaterRegionCenterTile ( const WaterRegionDesc water_region)

Returns the center tile of a particular water region.

Parameters
water_regionThe water region to find the center tile for.
Returns
The center tile of the water region.

Definition at line 286 of file water_regions.cpp.

References TileXY(), WaterRegionDesc::x, and WaterRegionDesc::y.

◆ GetWaterRegionIndex() [1/3]

static WaterRegionIndex GetWaterRegionIndex ( const WaterRegionDesc water_region)
static

Returns the index of the water region.

Parameters
water_regionThe water region to return the index for.

Definition at line 267 of file water_regions.cpp.

References WaterRegionDesc::x, and WaterRegionDesc::y.

◆ GetWaterRegionIndex() [2/3]

static WaterRegionIndex GetWaterRegionIndex ( int  region_x,
int  region_y 
)
inlinestatic

Definition at line 40 of file water_regions.cpp.

◆ GetWaterRegionIndex() [3/3]

static WaterRegionIndex GetWaterRegionIndex ( TileIndex  tile)
inlinestatic

Definition at line 41 of file water_regions.cpp.

◆ GetWaterRegionInfo()

WaterRegionDesc GetWaterRegionInfo ( TileIndex  tile)

Returns basic water region information for the provided tile.

Parameters
tileThe tile for which the information will be calculated.

Definition at line 295 of file water_regions.cpp.

Referenced by CYapfFollowShipT< Types >::PfFollowNode().

◆ GetWaterRegionMapSizeX()

static int GetWaterRegionMapSizeX ( )
inlinestatic

Definition at line 37 of file water_regions.cpp.

◆ GetWaterRegionMapSizeY()

static int GetWaterRegionMapSizeY ( )
inlinestatic

Definition at line 38 of file water_regions.cpp.

◆ GetWaterRegionPatchInfo()

WaterRegionPatchDesc GetWaterRegionPatchInfo ( TileIndex  tile)

Returns basic water region patch information for the provided tile.

Parameters
tileThe tile for which the information will be calculated.

Definition at line 304 of file water_regions.cpp.

References WaterRegion::GetLabel().

Referenced by VisitWaterRegionPatchNeighbours().

◆ GetWaterRegionX()

static int GetWaterRegionX ( TileIndex  tile)
inlinestatic

Definition at line 34 of file water_regions.cpp.

◆ GetWaterRegionY()

static int GetWaterRegionY ( TileIndex  tile)
inlinestatic

Definition at line 35 of file water_regions.cpp.

◆ GetWaterTracks()

static TrackBits GetWaterTracks ( TileIndex  tile)
inlinestatic

Definition at line 31 of file water_regions.cpp.

◆ InvalidateWaterRegion()

void InvalidateWaterRegion ( TileIndex  tile)

Marks the water region that tile is part of as invalid.

Parameters
tileTile within the water region that we wish to invalidate.

Definition at line 314 of file water_regions.cpp.

References AddTileIndexDiffCWrap(), Debug, DIAGDIR_BEGIN, DIAGDIR_END, INVALID_TILE, IsValidTile(), and TileIndexDiffCByDiagDir().

Referenced by BuildObject(), DoBuildLock(), and DoFloodTile().

◆ IsAqueductTile()

static bool IsAqueductTile ( TileIndex  tile)
inlinestatic

Definition at line 32 of file water_regions.cpp.

◆ PrintWaterRegionDebugInfo()

void PrintWaterRegionDebugInfo ( TileIndex  tile)

Definition at line 429 of file water_regions.cpp.

◆ VisitAdjacentWaterRegionPatchNeighbours()

static void VisitAdjacentWaterRegionPatchNeighbours ( const WaterRegionPatchDesc water_region_patch,
DiagDirection  side,
VisitWaterRegionPatchCallback &  func 
)
inlinestatic

Calls the provided callback function for all water region patches accessible from one particular side of the starting patch.

Parameters
water_region_patchWater patch within the water region to start searching from
sideSide of the water region to look for neighbouring patches of water
callbackThe function that will be called for each neighbour that is found

Definition at line 343 of file water_regions.cpp.

References WaterRegion::GetEdgeTraversabilityBits(), WaterRegion::GetLabel(), HasBit(), WaterRegionPatchDesc::label, WaterRegion::NumberOfPatches(), ReverseDiagDir(), TileIndexDiffCByDiagDir(), TileIndexDiffC::x, WaterRegionPatchDesc::x, TileIndexDiffC::y, and WaterRegionPatchDesc::y.

Referenced by VisitWaterRegionPatchNeighbours().

◆ VisitWaterRegionPatchNeighbours()

void VisitWaterRegionPatchNeighbours ( const WaterRegionPatchDesc water_region_patch,
VisitWaterRegionPatchCallback &  callback 
)

Calls the provided callback function on all accessible water region patches in each cardinal direction, plus any others that are reachable via aqueducts.

Parameters
water_region_patchWater patch within the water region to start searching from
callbackThe function that will be called for each accessible water patch that is found

Definition at line 392 of file water_regions.cpp.

References DIAGDIR_BEGIN, DIAGDIR_END, GetOtherBridgeEnd(), GetWaterRegionPatchInfo(), WaterRegion::HasCrossRegionAqueducts(), WaterRegionPatchDesc::label, VisitAdjacentWaterRegionPatchNeighbours(), WaterRegionPatchDesc::x, and WaterRegionPatchDesc::y.

Variable Documentation

◆ _is_water_region_valid

ReferenceThroughBaseContainer<std::vector<bool> > _is_water_region_valid

Definition at line 226 of file water_regions.cpp.

◆ _water_region_data

ReferenceThroughBaseContainer<std::vector<WaterRegionData> > _water_region_data

Definition at line 225 of file water_regions.cpp.

◆ FIRST_REGION_LABEL

constexpr WaterRegionPatchLabel FIRST_REGION_LABEL {1}
constexpr

Definition at line 26 of file water_regions.cpp.