# map.cpp File Reference

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

`#include "stdafx.h"`
`#include "debug.h"`
`#include "core/bitmath_func.hpp"`
`#include "core/alloc_func.hpp"`
`#include "core/math_func.hpp"`
`#include "map_func.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.
uint | ScaleByMapSize (uint n)
| Scales relative to the number of tiles.
uint | ScaleByMapSize1D (uint n)
| Scale relative to the circumference of the map.
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.

## 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 33 of file map.cpp.

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

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.
  | 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

< Is the tile within the map?

< Is the callback successful?

< then stop the search

Definition at line 298 of file map.cpp.

References DIAGDIR_END, DIAGDIR_NE, DIR_W, INVALID_TILE, MapMaxX(), MapMaxY(), 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 263 of file map.cpp.

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

Referenced by ChopLumberMillTrees(), CircularTileSearch(), DeliverGoodsToIndustry(), FindStationsNearby(), GetDistanceFromNearbyHouse(), 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 239 of file map.cpp.

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

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

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 179 of file map.cpp.

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

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

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 211 of file map.cpp.

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

Referenced by DeliverGoodsToIndustry(), 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 227 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 196 of file map.cpp.

References TileX(), and TileY().

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

uint ScaleByMapSize | ( | uint | n | ) |

Scales relative to the number of tiles.

Scales the given value by the map size, where the given value is for a 256 by 256 map.

Parameters:

  | n | the value to scale

Returns:
  the scaled size

Definition at line 100 of file map.cpp.

References MapSize().

Referenced by GenerateIndustries(), GenerateTrees(), PlaceInitialIndustry(), PlaceMoreTrees(), and PlaceTreesRandomly().

uint ScaleByMapSize1D | ( | uint | n | ) |

Scale relative to the circumference of the map.

Scales the given value by the maps circumference, where the given value is for a 256 by 256 map

Parameters:

  | n | the value to scale

Returns:
  the scaled size

Definition at line 115 of file map.cpp.

References MapSizeX(), and MapSizeY().

Referenced by GenerateIndustries(), and PlaceInitialIndustry().

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 138 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 24 of file map.cpp.

Referenced by AddClearCounter(), AddClearDensity(), AddTreeCount(), AddTreeCounter(), AddTreeGrowth(), ClearSingleBridgeMiddle(), GetBridgeAxis(), GetBridgeType(), GetClearCounter(), GetClearDensity(), GetClearGround(), GetCompanyHQSection(), GetCompanyHQSize(), GetCrossingReservation(), GetDepotWaypointReservation(), GetDisallowedRoadDirections(), GetFenceSE(), GetFenceSW(), GetFieldType(), GetHouseAge(), GetHouseAnimationFrame(), GetHouseBuildingStage(), GetHouseConstructionTick(), GetHouseRandomBits(), GetHouseTriggers(), GetHouseType(), GetIndustryAnimationLoop(), GetIndustryAnimationState(), GetIndustryConstructionCounter(), GetIndustryConstructionStage(), GetIndustryIndex(), GetIndustryIndexOfField(), GetIndustryTriggers(), GetLiftPosition(), GetPresentSignals(), GetRailDepotDirection(), GetRailTileType(), GetRailType(), GetRailwayStationReservation(), GetSignalStates(), GetStationIndex(), GetStatueTownID(), GetTileOwner(), GetTileType(), GetTownIndex(), GetTrackBits(), GetTrackReservation(), GetTreeCount(), GetTreeCounter(), GetTreeDensity(), GetTreeGround(), GetTreeGrowth(), GetTreeType(), GetTropicZone(), GetTunnelBridgeDirection(), GetTunnelBridgeReservation(), GetTunnelBridgeTransportType(), GetUnmovableType(), GetWaypointAxis(), GetWaypointIndex(), IncHouseConstructionTick(), IncrementHouseAge(), IsBridge(), IsBridgeAbove(), IsCompanyHQ(), IsHouseCompleted(), IsIndustryCompleted(), IsTunnel(), MakeBridgeRamp(), MakeClear(), MakeField(), MakeHouseTile(), MakeIndustry(), MakeRailTunnel(), MakeRoadTunnel(), MakeStatue(), MakeTree(), MakeUnmovable(), MakeVoid(), ResetHouseAge(), ResetIndustryConstructionStage(), SetBridgeMiddle(), SetClearCounter(), SetClearGroundDensity(), SetCompanyHQSection(), SetCompanyHQSize(), SetCrossingReservation(), SetDepotWaypointReservation(), SetDisallowedRoadDirections(), SetFenceSE(), SetFenceSW(), SetFieldType(), SetHasSignals(), SetHouseAnimationFrame(), SetHouseCompleted(), SetHouseRandomBits(), SetHouseTriggers(), SetHouseType(), SetIndustryAnimationLoop(), SetIndustryAnimationState(), SetIndustryCompleted(), SetIndustryConstructionCounter(), SetIndustryConstructionStage(), SetIndustryGfx(), SetIndustryIndexOfField(), SetIndustryTriggers(), SetLiftPosition(), SetPresentSignals(), SetRailType(), SetRailwayStationReservation(), SetSignalStates(), SetTileHeight(), SetTileOwner(), SetTileType(), SetTownIndex(), SetTrackBits(), SetTrackReservation(), SetTreeCounter(), SetTreeGroundDensity(), SetTreeGrowth(), SetTropicZone(), SetTunnelBridgeReservation(), and TileHeight().

TileExtended* _me = NULL

Extended Tiles of the map.

Pointer to the extended tile-array.

Definition at line 25 of file map.cpp.

Referenced by DecHouseProcessingTime(), GetHouseProcessingTime(), GetIndustryRandomBits(), GetLiftDestination(), HaltLift(), HasTunnelBridgeSnowOrDesert(), LiftHasDestination(), MakeBridgeRamp(), MakeClear(), MakeField(), MakeHouseTile(), MakeRailTunnel(), MakeRoadTunnel(), MakeTree(), MakeUnmovable(), MakeVoid(), SetHouseProcessingTime(), SetIndustryRandomBits(), SetLiftDestination(), 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().