# 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 "core/math_func.hpp"`
`#include "tile_map.h"`

Go to the source code of this file.

## Functions
void | AllocateMap (uint size_x, uint size_y)
| Allocate a new map with the given size.
TileIndex | TileAddWrap (TileIndex tile, int addx, int addy)
| Adds an offset to a tile and check if we are still on the map.
uint | DistanceManhattan (TileIndex t0, TileIndex t1)
| also known as L1-Norm. Is the shortest distance one could go over diagonal tracks (or roads)
uint | DistanceSquare (TileIndex t0, TileIndex t1)
| euclidian- or L2-Norm squared
uint | DistanceMax (TileIndex t0, TileIndex t1)
| also known as L-Infinity-Norm
uint | DistanceMaxPlusManhattan (TileIndex t0, TileIndex t1)
| Max + Manhattan.
uint | DistanceFromEdge (TileIndex tile)
| shortest distance from any edge of the map
bool | CircularTileSearch (TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
| Searches for some cirumstances of a tile around a given tile with a helper function.
bool | CircularTileSearch (TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data)
| Searches for some cirumstances of a tile around a given rectangle with a helper function.
uint | GetClosestWaterDistance (TileIndex tile, bool water)
| Finds the distance for the closest tile with water/land given a tile.
## Variables
uint | _map_log_x
| 2^_map_log_x == _map_size_x
uint | _map_log_y
| 2^_map_log_y == _map_size_y
uint | _map_size_x
| Size of the map along the X.
uint | _map_size_y
| Size of the map along the Y.
uint | _map_size
| The number of tiles on the map.
uint | _map_tile_mask
| _map_size - 1 (to mask the mapsize)
Tile * | _m = NULL
| Tiles of the map.
TileExtended * | _me = NULL
| Extended Tiles of the map.
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

void AllocateMap | ( | uint | size_x,
|  | uint | size_y |
| ) |  |  |

Allocate a new map with the given size.

(Re)allocates a map with the given dimension

Parameters:

  | size_x | the width of the map along the NE/SW edge
  | size_y | the 'height' of the map along the SE/NW edge

Definition at line 39 of file map.cpp.

References _map_log_x, _map_log_y, _map_size, _map_size_x, _map_size_y, _map_tile_mask, error(), FindFirstBit(), IsInsideMM(), MAX_MAP_SIZE, and MIN_MAP_SIZE.

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

Searches for some cirumstances of a tile around a given rectangle with a helper function.

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:

  | tile | to start the search from. Upon completion, it will return the tile matching the search
  | radius | How 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.
  | w | the width of the inner rectangle
  | h | the height of the inner rectangle
  | proc | callback testing function pointer.
  | user_data | to be passed to the callback function. Depends on the implementation

Returns:
  result of the search

Precondition:
  proc != NULL

  radius > 0

Definition at line 275 of file map.cpp.

References DIAGDIR_BEGIN, DIAGDIR_END, DIR_W, INVALID_TILE, MapSizeX(), MapSizeY(), TileX(), TileXY(), TileY(), TileIndexDiffC::x, and TileIndexDiffC::y.

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

Searches for some cirumstances of a tile around a given tile with a helper function.

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:

  | tile | to start the search from. Upon completion, it will return the tile matching the search
  | size,: | number of tiles per side of the desired search area
  | proc,: | callback testing function pointer.
  | user_data | to be passed to the callback function. Depends on the implementation

Returns:
  result of the search

Precondition:
  proc != NULL

  size > 0

Definition at line 238 of file map.cpp.

References CircularTileSearch(), DIR_W, TILE_ADD, and TileOffsByDir().

Referenced by ChopLumberMillTrees(), CircularTileSearch(), FindNearestGoodCoastalTownSpot(), FindStationsNearby(), GetDistanceFromNearbyHouse(), Station::RecomputeIndustriesNear(), and TownActionBuildStatue().

uint DistanceFromEdge | ( | TileIndex | tile | ) |

shortest distance from any edge of the map

Param the minimum distance to an edge

Parameters:

  | tile | the tile to get the distance from

Returns:
  the distance from the edge in tiles

Definition at line 214 of file map.cpp.

References MapSizeX(), MapSizeY(), min(), TileX(), and TileY().

Referenced by AIMap::DistanceFromEdge(), GrayscaleToMapHeights(), GrowTownWithExtraHouse(), IsRoadAllowedHere(), and TownCanBePlacedHere().

uint DistanceManhattan | ( | TileIndex | t0,
|  | TileIndex | t1 |
| ) |  |  |

also known as L1-Norm. Is the shortest distance one could go over diagonal tracks (or roads)

Gets the Manhattan distance between the two given tiles. The Manhattan distance is the sum of the delta of both the X and Y component. Also known as L1-Norm

Parameters:

  | t0 | the start tile
  | t1 | the end tile

Returns:
  the distance

Definition at line 154 of file map.cpp.

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

Referenced by AirportGetNearestTown(), AIMarine::AreWaterTilesConnected(), AIRail::BuildRail(), CalcRaildirsDrawstyle(), AIRoad::CanBuildConnectedRoadPartsHere(), DeliverGoods(), AIMap::DistanceManhattan(), FindDeletedWaypointCloseTo(), GetClosestDeletedStation(), GetCountAndDistanceOfClosestInstance(), GetDistanceFromNearbyHouse(), IndustryGetVariable(), IsCloseToTown(), CargoPayment::PayTransfer(), Station::RecomputeIndustriesNear(), AIRail::RemoveRail(), VpSelectTilesWithMethod(), VpSetPresizeRange(), and YapfTrainCheckReverse().

uint DistanceMax | ( | TileIndex | t0,
|  | TileIndex | t1 |
| ) |  |  |

also known as L-Infinity-Norm

Gets the biggest distance component (x or y) between the two given tiles. Also known as L-Infinity-Norm.

Parameters:

  | t0 | the start tile
  | t1 | the end tile

Returns:
  the distance

Definition at line 186 of file map.cpp.

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

Referenced by AIMap::DistanceMax(), and FindStationsNearby().

uint DistanceMaxPlusManhattan | ( | TileIndex | t0,
|  | TileIndex | t1 |
| ) |  |  |

Max + Manhattan.

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

Parameters:

  | t0 | the start tile
  | t1 | the end tile

Returns:
  the distance

Definition at line 202 of file map.cpp.

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

uint DistanceSquare | ( | TileIndex | t0,
|  | TileIndex | t1 |
| ) |  |  |

euclidian- or L2-Norm squared

Gets the 'Square' distance between the two given tiles. 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:

  | t0 | the start tile
  | t1 | the end tile

Returns:
  the distance

Definition at line 171 of file map.cpp.

References TileX(), and TileY().

Referenced by AIMap::DistanceSquare(), FindNearestHangar(), GetTownRadiusGroup(), IndustryGetVariable(), and UpdateTownGrowRate().

uint GetClosestWaterDistance | ( | TileIndex | tile,
|  | bool | water |
| ) |  |  |

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

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

Parameters:

  | tile | the tile to find the distance too
  | water | whether to find water or land

Returns:
  distance to nearest water (max 0x7F) / land (max 0x1FF; 0x200 if there is no land)

Note:
  FAILS when an industry should be seen as water

Definition at line 320 of file map.cpp.

References _settings_game, GameSettings::construction, DIAGDIR_BEGIN, DIAGDIR_END, ConstructionSettings::freeform_edges, IsInsideMM(), IsTileType(), MapMaxX(), MapMaxY(), MapSize(), MP_VOID, MP_WATER, TileX(), TileXY(), and TileY().

Referenced by FindFurthestFromWater(), and IndustryGetVariable().

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

Adds an offset to a tile and check if we are still on the map.

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:

  | tile | the 'starting' point of the adding
  | addx | the amount of tiles in the X direction to add
  | addy | the amount of tiles in the Y direction to add

Returns:
  translated tile, or INVALID_TILE when it would've wrapped.

Definition at line 113 of file map.cpp.

References INVALID_TILE, MapMaxX(), MapMaxY(), TileDiffXY(), TileX(), and TileY().

Referenced by CountMapSquareAround(), DisasterTick_Big_Ufo_Destroyer(), DoPlaceMoreTrees(), FindStationsAroundTiles(), and PlaceTreeAtSameHeight().

## Variable Documentation

Tile* _m = NULL

Tiles of the map.

Pointer to the tile-array.

Definition at line 30 of file map.cpp.

Referenced by AddClearCounter(), AddClearDensity(), AddTreeCount(), AddTreeCounter(), AddTreeGrowth(), ClearSingleBridgeMiddle(), ClearSnow(), GetBridgeAxis(), GetBridgeType(), GetCleanHouseType(), GetCleanIndustryGfx(), GetClearCounter(), GetClearDensity(), GetCompanyHQSection(), GetCompanyHQSize(), GetDepotIndex(), GetDisallowedRoadDirections(), GetFenceSE(), GetFenceSW(), GetFieldType(), GetHouseAge(), GetHouseAnimationFrame(), GetHouseBuildingStage(), GetHouseConstructionTick(), GetHouseRandomBits(), GetHouseTriggers(), GetIndustryAnimationLoop(), GetIndustryAnimationState(), GetIndustryConstructionCounter(), GetIndustryConstructionStage(), GetIndustryIndex(), GetIndustryIndexOfField(), GetIndustryTriggers(), GetLiftPosition(), GetPresentSignals(), GetRailDepotDirection(), GetRailReservationTrackBits(), GetRailTileType(), GetRailType(), GetRawClearGround(), GetSignalStates(), GetStationGfx(), GetStationIndex(), GetStationType(), GetStatueTownID(), GetTileOwner(), GetTileType(), GetTownIndex(), GetTrackBits(), GetTreeCount(), GetTreeCounter(), GetTreeDensity(), GetTreeGround(), GetTreeGrowth(), GetTreeType(), GetTropicZone(), GetTunnelBridgeDirection(), GetTunnelBridgeTransportType(), GetUnmovableType(), HasCrossingReservation(), HasDepotReservation(), HasStationReservation(), HasTunnelBridgeReservation(), IncHouseConstructionTick(), IncrementHouseAge(), IsBridge(), IsBridgeAbove(), IsCompanyHQ(), IsHouseCompleted(), IsIndustryCompleted(), IsSnowTile(), IsTunnel(), MakeBridgeRamp(), MakeClear(), MakeField(), MakeHouseTile(), MakeIndustry(), MakeRailTunnel(), MakeRoadTunnel(), MakeSnow(), MakeStatue(), MakeTree(), MakeUnmovable(), MakeVoid(), MakeWater(), MoveWaypointsToBaseStations(), ResetHouseAge(), ResetIndustryConstructionStage(), SetBridgeMiddle(), SetClearCounter(), SetClearDensity(), SetClearGroundDensity(), SetCompanyHQSection(), SetCompanyHQSize(), SetCrossingReservation(), SetDepotReservation(), SetDisallowedRoadDirections(), SetFenceSE(), SetFenceSW(), SetFieldType(), SetHasSignals(), SetHouseAnimationFrame(), SetHouseCompleted(), SetHouseRandomBits(), SetHouseTriggers(), SetHouseType(), SetIndustryAnimationLoop(), SetIndustryAnimationState(), SetIndustryCompleted(), SetIndustryConstructionCounter(), SetIndustryConstructionStage(), SetIndustryGfx(), SetIndustryIndexOfField(), SetIndustryTriggers(), SetLiftPosition(), SetPresentSignals(), SetRailStationReservation(), SetRailType(), SetSignalStates(), SetStationGfx(), SetTileHeight(), SetTileOwner(), SetTileType(), SetTownIndex(), SetTrackBits(), SetTrackReservation(), SetTreeCounter(), SetTreeGroundDensity(), SetTreeGrowth(), SetTropicZone(), SetTunnelBridgeReservation(), SetWaterClassDependingOnSurroundings(), TileHeight(), and CrashLog::WriteSavegame().

TileExtended* _me = NULL

Extended Tiles of the map.

Pointer to the extended tile-array.

Definition at line 31 of file map.cpp.

Referenced by DecHouseProcessingTime(), GetHouseProcessingTime(), GetIndustryRandomBits(), GetLiftDestination(), GetStationAnimationFrame(), HaltLift(), HasTunnelBridgeSnowOrDesert(), LiftHasDestination(), MakeBridgeRamp(), MakeClear(), MakeField(), MakeHouseTile(), MakeRailTunnel(), MakeRoadTunnel(), MakeTree(), MakeUnmovable(), MakeVoid(), MakeWater(), SetHouseProcessingTime(), SetIndustryRandomBits(), SetLiftDestination(), SetStationAnimationFrame(), and SetTunnelBridgeSnowOrDesert().

const TileIndexDiffC _tileoffs_by_diagdir[]

Initial value:

     {
      {-1,  0},
      { 0,  1},
      { 1,  0},
      { 0, -1}
    }

'Lookup table' for tile offsets given a DiagDirection

Referenced by TileIndexDiffCByDiagDir(), and TileOffsByDiagDir().

const TileIndexDiffC _tileoffs_by_dir[]

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 TileIndexDiffCByDir(), and TileOffsByDir().