Functions | Variables

# 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"`

Go to the source code of this file.

## Functions
void | AllocateMap (uint size_x, uint size_y)
| (Re)allocates a map with the given dimension
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
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 |
| ) |  |  |

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

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

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:

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

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

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

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:

  | tile | to 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).
  | 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 295 of file map.cpp.

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

uint DistanceFromEdge | ( | TileIndex | tile | ) |

Param the minimum distance to an edge.

shortest distance from any edge of the map

Parameters:

  | tile | the tile to get the distance from

Returns:
  the distance from the edge in tiles

Definition at line 216 of file map.cpp.

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

Referenced by CheckNewIndustry_OilRefinery(), CheckNewIndustry_OilRig(), GrayscaleToMapHeights(), GrowTownWithExtraHouse(), IsRoadAllowedHere(), and TownCanBePlacedHere().

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:

  | tile | the tile to get the distance from
  | dir | the direction of interest

Returns:
  the distance from the edge in tiles

Definition at line 233 of file map.cpp.

References _settings_game, GameSettings::construction, DIAGDIR_NE, DIAGDIR_NW, DIAGDIR_SE, DIAGDIR_SW, ConstructionSettings::freeform_edges, MapMaxX(), MapMaxY(), TileX(), and TileY().

Referenced by GetOtherAqueductEnd().

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

Gets the Manhattan distance between the two given tiles.

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

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

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

Referenced by AirportGetNearestTown(), CalcClosestTownFromTile(), CalcRaildirsDrawstyle(), CheckSubsidyDistance(), ClosestTownFromTile(), DeleteStaleLinks(), DeliverGoods(), FindDeletedWaypointCloseTo(), FindSubsidyPassengerRoute(), FlowRiver(), FlowsDown(), GetClosestDeletedStation(), GetClosestObject(), GetCountAndDistanceOfClosestInstance(), GetDistanceFromNearbyHouse(), GetMinimalAirportDistanceToTile(), GetOrderDistance(), ObjectScopeResolver::GetVariable(), IndustriesScopeResolver::GetVariable(), IsCloseToTown(), CargoPayment::PayTransfer(), Station::RecomputeIndustriesNear(), VpSelectTilesWithMethod(), VpSetPresizeRange(), and YapfTrainCheckReverse().

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:

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

Returns:
  the distance

Definition at line 188 of file map.cpp.

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

Referenced by CheckIfFarEnoughFromConflictingIndustry(), CheckIfIndustryIsAllowed(), and FindStationsNearby().

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:

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

Returns:
  the distance

Definition at line 204 of file map.cpp.

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

Referenced by LinkGraph::AddNode(), and LinkGraph::UpdateDistances().

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:

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

Returns:
  the distance

Definition at line 173 of file map.cpp.

References TileX(), and TileY().

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

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

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)

Definition at line 339 of file map.cpp.

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

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

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:

  | 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 _settings_game, GameSettings::construction, ConstructionSettings::freeform_edges, INVALID_TILE, MapMaxX(), MapMaxY(), TileX(), TileXY(), and TileY().

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

## Variable Documentation

Tile* _m = NULL

Tiles of the map.

Pointer to the tile-array.

Definition at line 29 of file map.cpp.

Referenced by AddClearCounter(), AddClearDensity(), AddTreeCount(), AddTreeCounter(), AddTreeGrowth(), AfterLoadGame(), ClearSingleBridgeMiddle(), ClearSnow(), DecHouseProcessingTime(), FixOldVehicles(), GetBridgeAxis(), GetBridgeType(), GetCleanHouseType(), GetCleanIndustryGfx(), GetClearCounter(), GetClearDensity(), GetCrossingRoadAxis(), GetCustomStationSpecIndex(), GetDepotIndex(), GetDisallowedRoadDirections(), GetFence(), GetFieldType(), GetHouseAge(), GetHouseBuildingStage(), GetHouseConstructionTick(), GetHouseProcessingTime(), GetHouseRandomBits(), GetHouseTriggers(), GetIndustryAnimationLoop(), GetIndustryConstructionCounter(), GetIndustryConstructionStage(), GetIndustryIndex(), GetIndustryIndexOfField(), GetIndustryRandomBits(), GetIndustryTriggers(), GetLiftPosition(), GetLockDirection(), GetLockPart(), GetObjectIndex(), GetObjectRandomBits(), GetPresentSignals(), GetRailDepotDirection(), GetRailReservationTrackBits(), GetRailTileType(), GetRailType(), GetRawClearGround(), GetRoadBits(), GetRoadDepotDirection(), GetRoadOwner(), GetRoadside(), GetRoadTileType(), GetShipDepotAxis(), GetShipDepotPart(), GetSignalStates(), GetStationGfx(), GetStationIndex(), GetStationTileRandomBits(), GetStationType(), GetTileOwner(), GetTileType(), GetTownIndex(), GetTrackBits(), GetTreeCount(), GetTreeCounter(), GetTreeDensity(), GetTreeGround(), GetTreeGrowth(), GetTreeType(), GetTropicZone(), GetTunnelBridgeDirection(), GetTunnelBridgeTransportType(), GetWaterClass(), GetWaterTileRandomBits(), GetWaterTileType(), HasCrossingReservation(), HasDepotReservation(), HasStationReservation(), HasTunnelBridgeReservation(), IncHouseConstructionTick(), IncrementHouseAge(), IsBridge(), IsBridgeAbove(), IsCrossingBarred(), IsCustomStationSpecIndex(), IsHouseCompleted(), IsIndustryCompleted(), IsSnowTile(), IsTunnel(), MakeBridgeRamp(), MakeClear(), MakeField(), MakeHouseTile(), MakeIndustry(), MakeLockTile(), MakeObject(), MakeRailTunnel(), MakeRoadCrossing(), MakeRoadDepot(), MakeRoadNormal(), MakeRoadTunnel(), MakeShipDepot(), MakeShore(), MakeSnow(), MakeStation(), MakeTree(), MakeVoid(), MakeWater(), MoveBuoysToWaypoints(), MoveWaypointsToBaseStations(), ResetHouseAge(), ResetIndustryConstructionStage(), SetBridgeMiddle(), SetClearCounter(), SetClearDensity(), SetClearGroundDensity(), SetCrossingBarred(), SetCrossingReservation(), SetCustomStationSpecIndex(), SetDepotReservation(), SetDisallowedRoadDirections(), SetFence(), SetFieldType(), SetHasSignals(), SetHouseCompleted(), SetHouseProcessingTime(), SetHouseRandomBits(), SetHouseTriggers(), SetHouseType(), SetIndustryAnimationLoop(), SetIndustryCompleted(), SetIndustryConstructionCounter(), SetIndustryConstructionStage(), SetIndustryGfx(), SetIndustryIndexOfField(), SetIndustryRandomBits(), SetIndustryTriggers(), SetLiftPosition(), SetPresentSignals(), SetRailStationReservation(), SetRailType(), SetRoadBits(), SetRoadOwner(), SetRoadside(), SetSignalStates(), SetStationGfx(), SetStationTileRandomBits(), SetTileHeight(), SetTileOwner(), SetTileType(), SetTownIndex(), SetTrackBits(), SetTrackReservation(), SetTreeCounter(), SetTreeGroundDensity(), SetTreeGrowth(), SetTropicZone(), SetTunnelBridgeReservation(), SetWaterClass(), SetWaterClassDependingOnSurroundings(), TileHeight(), and CrashLog::WriteSavegame().

TileExtended* _me = NULL

Extended Tiles of the map.

Pointer to the extended tile-array.

Definition at line 30 of file map.cpp.

Referenced by AfterLoadGame(), GetAnimationFrame(), GetLiftDestination(), GetRoadOwner(), GetRoadTypes(), HaltLift(), HasTunnelBridgeSnowOrDesert(), IncreaseRoadWorksCounter(), IsOnSnow(), LiftHasDestination(), MakeBridgeRamp(), MakeClear(), MakeField(), MakeIndustry(), MakeLockTile(), MakeObject(), MakeRailTunnel(), MakeRoadCrossing(), MakeRoadDepot(), MakeRoadNormal(), MakeRoadTunnel(), MakeShipDepot(), MakeShore(), MakeStation(), MakeTree(), MakeVoid(), MakeWater(), SetAnimationFrame(), SetLiftDestination(), SetRoadOwner(), SetRoadTypes(), SetTunnelBridgeSnowOrDesert(), TerminateRoadWorks(), and ToggleSnow().

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