# map.cpp File Reference

`#include "stdafx.h"`
`#include "debug.h"`
`#include "direction_func.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, uint32 data)
| Searches for some cirumstances of a tile around a given tile 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

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 34 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 | size,
|  | TestTileOnSearchProc | proc,
|  | uint32 | 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
  | size,: | number of tiles per side of the desired search area
  | proc,: | callback testing function pointer.
  | data | to be passed to the callback function. Depends on the implementation

Returns:
  result of the search

Precondition:
  proc != NULL

  size > 0

< Is the tile within the map?

< Is the callback successful?

< then stop the search

Definition at line 269 of file map.cpp.

References DIAGDIR_END, DIAGDIR_NE, DIR_W, MapMaxX(), MapMaxY(), TileX(), TileXY(), TileY(), TileIndexDiffC::x, and TileIndexDiffC::y.

Referenced by ChopLumberMillTrees(), 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 245 of file map.cpp.

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

Referenced by CmdBuildTown(), and GrayscaleToMapHeights().

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

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

Referenced by CalcRaildirsDrawstyle(), CmdInsertOrder(), DeliverGoodsToIndustry(), FindDeletedWaypointCloseTo(), GetClosestWaterDistance(), GetCountAndDistanceOfClosestInstance(), IndustryGetVariable(), IsCloseToTown(), VehiclePayment(), 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 217 of file map.cpp.

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

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

References TileX(), and TileY().

Referenced by 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 106 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 121 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 144 of file map.cpp.

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

## Variable Documentation

Tile* _m = NULL

Tiles of the map.

Pointer to the tile-array.

Definition at line 25 of file map.cpp.

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

TileExtended* _me = NULL

Extended Tiles of the map.

Pointer to the extended tile-array.

Definition at line 26 of file map.cpp.

Referenced by DecHouseProcessingTime(), GetHouseProcessingTime(), GetIndustryRandomBits(), GetLiftDestination(), HaltLift(), LiftHasDestination(), MakeHouseTile(), MakeIndustry(), MakeVoid(), SetHouseProcessingTime(), SetIndustryRandomBits(), and SetLiftDestination().

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