OpenTTD Source 20241224-master-gf74b0cf984
map.cpp File Reference

Base functions related to the map and distances on them. More...

#include "stdafx.h"
#include "debug.h"
#include "core/alloc_func.hpp"
#include "water_map.h"
#include "error_func.h"
#include "string_func.h"
#include "pathfinder/water_regions.h"
#include "safeguards.h"

Go to the source code of this file.

Functions

TileIndex TileAddWrap (TileIndex tile, int addx, int addy)
 This function checks if we add addx/addy to tile, if we do wrap around the edges.
 
uint DistanceManhattan (TileIndex t0, TileIndex t1)
 Gets the Manhattan distance between the two given tiles.
 
uint DistanceSquare (TileIndex t0, TileIndex t1)
 Gets the 'Square' distance between the two given tiles.
 
uint DistanceMax (TileIndex t0, TileIndex t1)
 Gets the biggest distance component (x or y) between the two given tiles.
 
uint DistanceMaxPlusManhattan (TileIndex t0, TileIndex t1)
 Gets the biggest distance component (x or y) between the two given tiles plus the Manhattan distance, i.e.
 
uint DistanceFromEdge (TileIndex tile)
 Param the minimum distance to an edge.
 
uint DistanceFromEdgeDir (TileIndex tile, DiagDirection dir)
 Gets the distance to the edge of the map in given direction.
 
bool CircularTileSearch (TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
 Function performing a search around a center tile and going outward, thus in circle.
 
bool CircularTileSearch (TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data)
 Generalized circular search allowing for rectangles and a hole.
 
uint GetClosestWaterDistance (TileIndex tile, bool water)
 Finds the distance for the closest tile with water/land given a tile.
 

Variables

const TileIndexDiffC _tileoffs_by_axis []
 'Lookup table' for tile offsets given an Axis
 
const TileIndexDiffC _tileoffs_by_diagdir []
 'Lookup table' for tile offsets given a DiagDirection
 
const TileIndexDiffC _tileoffs_by_dir []
 'Lookup table' for tile offsets given a Direction
 

Detailed Description

Base functions related to the map and distances on them.

Definition in file map.cpp.

Function Documentation

◆ CircularTileSearch() [1/2]

bool CircularTileSearch ( TileIndex tile,
uint  radius,
uint  w,
uint  h,
TestTileOnSearchProc  proc,
void *  user_data 
)

Generalized circular search allowing for rectangles and a hole.

Function performing a search around a center rectangle and going outward. The center rectangle is left out from the search. To do a rectangular search without a hole, set either h or w to zero. Every tile will be tested by means of the callback function proc, which will determine if yes or no the given tile meets criteria of search.

Parameters
tileto start the search from. Upon completion, it will return the tile matching the search. This tile should be directly north of the hole (if any).
radiusHow many tiles to search outwards. Note: This is a radius and thus different from the size parameter of the other CircularTileSearch function, which is a diameter.
wthe width of the inner rectangle
hthe height of the inner rectangle
proccallback testing function pointer.
user_datato be passed to the callback function. Depends on the implementation
Returns
result of the search
Precondition
proc != nullptr
radius > 0

Definition at line 285 of file map.cpp.

References _tileoffs_by_diagdir, _tileoffs_by_dir, DIAGDIR_BEGIN, DIAGDIR_END, DIR_W, INVALID_TILE, Map::SizeX(), Map::SizeY(), TileX(), TileXY(), TileY(), TileIndexDiffC::x, and TileIndexDiffC::y.

◆ CircularTileSearch() [2/2]

bool CircularTileSearch ( TileIndex tile,
uint  size,
TestTileOnSearchProc  proc,
void *  user_data 
)

Function performing a search around a center tile and going outward, thus in circle.

Although it really is a square search... Every tile will be tested by means of the callback function proc, which will determine if yes or no the given tile meets criteria of search.

Parameters
tileto start the search from. Upon completion, it will return the tile matching the search
sizenumber of tiles per side of the desired search area
proccallback testing function pointer.
user_datato be passed to the callback function. Depends on the implementation
Returns
result of the search
Precondition
proc != nullptr
size > 0

Definition at line 247 of file map.cpp.

References CircularTileSearch(), DIR_N, and TileAddByDir().

Referenced by ChopLumberMillTrees(), CircularTileSearch(), CmdBuildCanal(), CreateRivers(), FindNearestGoodCoastalTownSpot(), FindStationsNearby(), FlowRiver(), GenerateStationName(), GetDistanceFromNearbyHouse(), GrowTownWithBridge(), LoadTownData(), MakeRiverAndModifyDesertZoneAround(), TownActionBuildStatue(), and TryBuildTransmitter().

◆ DistanceFromEdge()

uint DistanceFromEdge ( TileIndex  tile)

Param the minimum distance to an edge.

shortest distance from any edge of the map

Parameters
tilethe tile to get the distance from
Returns
the distance from the edge in tiles

Definition at line 206 of file map.cpp.

References Map::SizeX(), Map::SizeY(), TileX(), and TileY().

Referenced by GenerateStationName(), GrayscaleToMapHeights(), GrowTownWithExtraHouse(), IsRoadAllowedHere(), and TownCanBePlacedHere().

◆ DistanceFromEdgeDir()

uint DistanceFromEdgeDir ( TileIndex  tile,
DiagDirection  dir 
)

Gets the distance to the edge of the map in given direction.

distance from the map edge in given direction

Parameters
tilethe tile to get the distance from
dirthe direction of interest
Returns
the distance from the edge in tiles

Definition at line 223 of file map.cpp.

References _settings_game, GameSettings::construction, DIAGDIR_NE, DIAGDIR_NW, DIAGDIR_SE, DIAGDIR_SW, ConstructionSettings::freeform_edges, Map::MaxX(), Map::MaxY(), TileX(), and TileY().

Referenced by CheckScaledDistanceFromEdge(), and GetOtherAqueductEnd().

◆ DistanceManhattan()

uint DistanceManhattan ( TileIndex  t0,
TileIndex  t1 
)

◆ DistanceMax()

uint DistanceMax ( TileIndex  t0,
TileIndex  t1 
)

Gets the biggest distance component (x or y) between the two given tiles.

also known as L-Infinity-Norm

Also known as L-Infinity-Norm.

Parameters
t0the start tile
t1the end tile
Returns
the distance

Definition at line 178 of file map.cpp.

References Delta(), TileX(), and TileY().

Referenced by Station::AddIndustryToDeliver(), CheckIfFarEnoughFromConflictingIndustry(), CheckIfIndustryIsAllowed(), FindStationsNearby(), and GenerateStationName().

◆ DistanceMaxPlusManhattan()

uint DistanceMaxPlusManhattan ( TileIndex  t0,
TileIndex  t1 
)

Gets the biggest distance component (x or y) between the two given tiles plus the Manhattan distance, i.e.

Max + Manhattan.

two times the biggest distance component and once the smallest component.

Parameters
t0the start tile
t1the end tile
Returns
the distance

Definition at line 194 of file map.cpp.

References Delta(), TileX(), and TileY().

Referenced by DemandCalculator::CalcDemand(), and MultiCommodityFlow::Dijkstra().

◆ DistanceSquare()

uint DistanceSquare ( TileIndex  t0,
TileIndex  t1 
)

Gets the 'Square' distance between the two given tiles.

euclidian- or L2-Norm squared

The 'Square' distance is the square of the shortest (straight line) distance between the two tiles. Also known as euclidian- or L2-Norm squared.

Parameters
t0the start tile
t1the end tile
Returns
the distance

Definition at line 163 of file map.cpp.

References TileX(), and TileY().

Referenced by CmdBuildBridge(), CmdBuildRoad(), CmdBuildTunnel(), FindNearestHangar(), GetOrderDistance(), GetTownRadiusGroup(), IndustriesScopeResolver::GetVariable(), ObjectScopeResolver::GetVariable(), RoadStopScopeResolver::GetVariable(), and PlaceTreeGroupAroundTile().

◆ GetClosestWaterDistance()

uint GetClosestWaterDistance ( TileIndex  tile,
bool  water 
)

Finds the distance for the closest tile with water/land given a tile.

Parameters
tilethe tile to find the distance too
waterwhether to find water or land
Returns
distance to nearest water (max 0x7F) / land (max 0x1FF; 0x200 if there is no land)

Definition at line 329 of file map.cpp.

References _settings_game, GameSettings::construction, DIAGDIR_BEGIN, DIAGDIR_END, ConstructionSettings::freeform_edges, HasTileWaterGround(), IsInsideMM(), IsTileType(), Map::Iterate(), Map::MaxX(), Map::MaxY(), MP_VOID, MP_WATER, TileX(), TileXY(), and TileY().

Referenced by FindFurthestFromWater(), and IndustriesScopeResolver::GetVariable().

◆ TileAddWrap()

TileIndex TileAddWrap ( TileIndex  tile,
int  addx,
int  addy 
)

This function checks if we add addx/addy to tile, if we do wrap around the edges.

For example, tile = (10,2) and addx = +3 and addy = -4. This function will now return INVALID_TILE, because the y is wrapped. This is needed in for example, farmland. When the tile is not wrapped, the result will be tile + TileDiffXY(addx, addy)

Parameters
tilethe 'starting' point of the adding
addxthe amount of tiles in the X direction to add
addythe amount of tiles in the Y direction to add
Returns
translated tile, or INVALID_TILE when it would've wrapped.

Definition at line 97 of file map.cpp.

References _settings_game, GameSettings::construction, ConstructionSettings::freeform_edges, INVALID_TILE, Map::MaxX(), Map::MaxY(), TileX(), TileXY(), and TileY().

Referenced by CheckIfIndustryTilesAreFree(), CheckIfIndustryTileSlopes(), CmdBuildRoadStop(), CmdRemoveRoadStop(), CountMapSquareAround(), DisasterTick_Big_Ufo_Destroyer(), FindSpring(), PlaceTreeAtSameHeight(), PlaceTreeGroupAroundTile(), and PlaceTreeGroups().

Variable Documentation

◆ _tileoffs_by_axis

const TileIndexDiffC _tileoffs_by_axis[]
extern
Initial value:
= {
{ 1, 0},
{ 0, 1},
}

'Lookup table' for tile offsets given an Axis

Referenced by TileOffsByAxis().

◆ _tileoffs_by_diagdir

const TileIndexDiffC _tileoffs_by_diagdir[]
extern
Initial value:
= {
{-1, 0},
{ 0, 1},
{ 1, 0},
{ 0, -1}
}

'Lookup table' for tile offsets given a DiagDirection

Referenced by CircularTileSearch(), TileIndexDiffCByDiagDir(), and TileOffsByDiagDir().

◆ _tileoffs_by_dir

const TileIndexDiffC _tileoffs_by_dir[]
extern
Initial value:
= {
{-1, -1},
{-1, 0},
{-1, 1},
{ 0, 1},
{ 1, 1},
{ 1, 0},
{ 1, -1},
{ 0, -1}
}

'Lookup table' for tile offsets given a Direction

Referenced by CircularTileSearch(), TileIndexDiffCByDir(), and TileOffsByDir().