# npf.cpp File Reference

`#include "stdafx.h"`
`#include "openttd.h"`
`#include "npf.h"`
`#include "bridge_map.h"`
`#include "debug.h"`
`#include "tile_cmd.h"`
`#include "bridge.h"`
`#include "landscape.h"`
`#include "aystar.h"`
`#include "pathfind.h"`
`#include "station.h"`
`#include "station_map.h"`
`#include "depot.h"`
`#include "tunnel_map.h"`
`#include "network/network.h"`
`#include "water_map.h"`
`#include "tunnelbridge_map.h"`
`#include "functions.h"`
`#include "vehicle_base.h"`
`#include "settings_type.h"`
`#include "tunnelbridge.h"`

Go to the source code of this file.

## Defines
#define | NPF_STRAIGHT_LENGTH (uint)(NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH)
## Functions
static uint | NPFDistanceTrack (TileIndex t0, TileIndex t1)
| Calculates the minimum distance traveled to get from t0 to t1 when only using tracks (ie, only making 45 degree turns).
static uint | NPFHash (uint key1, uint key2)
| Calculates a hash value for use in the NPF.
static int32 | NPFCalcZero (AyStar *as, AyStarNode *current, OpenListNode *parent)
static TileIndex | CalcClosestStationTile (StationID station, TileIndex tile)
static int32 | NPFCalcStationOrTileHeuristic (AyStar *as, AyStarNode *current, OpenListNode *parent)
static void | NPFFillTrackdirChoice (AyStarNode *current, OpenListNode *parent)
static uint | NPFTunnelCost (AyStarNode *current)
static uint | NPFBridgeCost (AyStarNode *current)
static uint | NPFSlopeCost (AyStarNode *current)
static void | NPFMarkTile (TileIndex tile)
| Mark tiles by mowing the grass when npf debug level >= 1.
static int32 | NPFWaterPathCost (AyStar *as, AyStarNode *current, OpenListNode *parent)
static int32 | NPFRoadPathCost (AyStar *as, AyStarNode *current, OpenListNode *parent)
static int32 | NPFRailPathCost (AyStar *as, AyStarNode *current, OpenListNode *parent)
static int32 | NPFFindDepot (AyStar *as, OpenListNode *current)
static int32 | NPFFindStationOrTile (AyStar *as, OpenListNode *current)
static void | NPFSaveTargetData (AyStar *as, OpenListNode *current)
static bool | CanEnterTileOwnerCheck (Owner owner, TileIndex tile, DiagDirection enterdir)
| Finds out if a given player's vehicles are allowed to enter a given tile.
static DiagDirection | GetDepotDirection (TileIndex tile, TransportType type)
| Returns the direction the exit of the depot on the given tile is facing.
static DiagDirection | GetSingleTramBit (TileIndex tile)
| Tests if a tile is a road tile with a single tramtrack (tram can reverse).
static DiagDirection | GetTileSingleEntry (TileIndex tile, TransportType type, uint subtype)
| Tests if a tile can be entered or left only from one side.
static bool | ForceReverse (TileIndex tile, DiagDirection dir, TransportType type, uint subtype)
| Tests if a vehicle must reverse on a tile.
static bool | CanEnterTile (TileIndex tile, DiagDirection dir, TransportType type, uint subtype, RailTypes railtypes, Owner owner)
| Tests if a vehicle can enter a tile.
static TrackdirBits | GetDriveableTrackdirBits (TileIndex dst_tile, Trackdir src_trackdir, TransportType type, uint subtype)
| Returns the driveable Trackdirs on a tile.
static void | NPFFollowTrack (AyStar *aystar, OpenListNode *current)
static NPFFoundTargetData | NPFRouteInternal (AyStarNode *start1, bool ignore_start_tile1, AyStarNode *start2, bool ignore_start_tile2, NPFFindStationOrTileData *target, AyStar_EndNodeCheck target_proc, AyStar_CalculateH heuristic_proc, TransportType type, uint sub_type, Owner owner, RailTypes railtypes, uint reverse_penalty)
NPFFoundTargetData | NPFRouteToStationOrTileTwoWay (TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, NPFFindStationOrTileData *target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
NPFFoundTargetData | NPFRouteToStationOrTile (TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData *target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
NPFFoundTargetData | NPFRouteToDepotBreadthFirstTwoWay (TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, TransportType type, uint sub_type, Owner owner, RailTypes railtypes, uint reverse_penalty)
NPFFoundTargetData | NPFRouteToDepotBreadthFirst (TileIndex tile, Trackdir trackdir, bool ignore_start_tile, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
NPFFoundTargetData | NPFRouteToDepotTrialError (TileIndex tile, Trackdir trackdir, bool ignore_start_tile, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
void | InitializeNPF ()
void | NPFFillWithOrderData (NPFFindStationOrTileData *fstd, Vehicle *v)
## Variables
static AyStar | _npf_aystar
static const uint | _trackdir_length [TRACKDIR_END]

## Detailed Description

Definition in file npf.cpp.

## Function Documentation

static bool CanEnterTile | ( | TileIndex | tile,
|  | DiagDirection | dir,
|  | TransportType | type,
|  | uint | subtype,
|  | RailTypes | railtypes,
|  | Owner | owner |
| ) |  |  | `[static]`

Tests if a vehicle can enter a tile.

Parameters:

  | tile | The tile of interest.
  | dir | The direction in which the vehicle drives onto a tile.
  | type | The transporttype of the vehicle.
  | subtype | For TRANSPORT_ROAD the compatible RoadTypes of the vehicle.
  | railtypes | For TRANSPORT_RAIL the compatible RailTypes of the vehicle.
  | owner | The owner of the vehicle.

Returns:
  true iff the vehicle can enter the tile.

Definition at line 576 of file npf.cpp.

References CanEnterTileOwnerCheck(), GetTileRailType(), GetTileSingleEntry(), GetTunnelBridgeDirection(), HasBit(), INVALID_DIAGDIR, IsTileType(), MP_TUNNELBRIDGE, and ReverseDiagDir().

static bool CanEnterTileOwnerCheck | ( | Owner | owner,
|  | TileIndex | tile,
|  | DiagDirection | enterdir |
| ) |  |  | `[static]`

Finds out if a given player's vehicles are allowed to enter a given tile.

Parameters:

  | owner | The owner of the vehicle.
  | tile | The tile that is about to be entered.
  | enterdir | The direction in which the vehicle wants to enter the tile.

Returns:
  true if the vehicle can enter the tile.

Todo:
  This function should be used in other places than just NPF, maybe moved to another file too.

Definition at line 465 of file npf.cpp.

References DiagDirToAxis(), GetTileType(), GetTunnelBridgeTransportType(), IsTileDepotType(), IsTileOwner(), IsTileType(), MP_RAILWAY, MP_ROAD, and MP_TUNNELBRIDGE.

Referenced by CanEnterTile().

static bool ForceReverse | ( | TileIndex | tile,
|  | DiagDirection | dir,
|  | TransportType | type,
|  | uint | subtype |
| ) |  |  | `[inline, static]`

Tests if a vehicle must reverse on a tile.

Parameters:

  | tile | The tile of interest.
  | dir | The direction in which the vehicle drives on a tile.
  | type | The transporttype of the vehicle.
  | subtype | For TRANSPORT_ROAD the compatible RoadTypes of the vehicle.

Returns:
  true iff the vehicle must reverse on the tile.

Definition at line 559 of file npf.cpp.

References GetTileSingleEntry(), and INVALID_DIAGDIR.

static TrackdirBits GetDriveableTrackdirBits | ( | TileIndex | dst_tile,
|  | Trackdir | src_trackdir,
|  | TransportType | type,
|  | uint | subtype |
| ) |  |  | `[static]`

Returns the driveable Trackdirs on a tile.

One-way-roads are taken into account. Signals are not tested.

Parameters:

  | dst_tile | The tile of interest.
  | src_trackdir | The direction the vehicle is currently moving.
  | type | The transporttype of the vehicle.
  | subtype | For TRANSPORT_ROAD the compatible RoadTypes of the vehicle.

Returns:
  The Trackdirs the vehicle can continue moving on.

Definition at line 608 of file npf.cpp.

References DIAGDIR_NE, DIAGDIR_NW, DIAGDIR_SE, DIAGDIR_SW, GetSingleTramBit(), HasBit(), ROADTYPE_TRAM, TileX(), TileY(), TRACKDIR_BIT_X_NE, TRACKDIR_BIT_X_SW, TRACKDIR_BIT_Y_NW, TRACKDIR_BIT_Y_SE, TrackdirCrossesTrackdirs(), TrackdirReachesTrackdirs(), and TrackStatusToTrackdirBits().

static DiagDirection GetTileSingleEntry | ( | TileIndex | tile,
|  | TransportType | type,
|  | uint | subtype |
| ) |  |  | `[static]`

Tests if a tile can be entered or left only from one side.

Depots, non-drive-through roadstops, and tiles with single trambits are tested.

Parameters:

  | tile | The tile of interest.
  | type | The transporttype of the vehicle.
  | subtype | For TRANSPORT_ROAD the compatible RoadTypes of the vehicle.

Returns:
  The single entry/exit-direction of the tile, or INVALID_DIAGDIR if there are more or less directions

Definition at line 538 of file npf.cpp.

References GetDepotDirection(), GetRoadStopDir(), GetSingleTramBit(), HasBit(), INVALID_DIAGDIR, IsTileDepotType(), and ROADTYPE_TRAM.

Referenced by CanEnterTile(), and ForceReverse().

static uint NPFDistanceTrack | ( | TileIndex | t0,
|  | TileIndex | t1 |
| ) |  |  | `[static]`

Calculates the minimum distance traveled to get from t0 to t1 when only using tracks (ie, only making 45 degree turns).

Returns the distance in the NPF scale, ie the number of full tiles multiplied by NPF_TILE_LENGTH to prevent rounding.

Definition at line 45 of file npf.cpp.

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

static uint NPFHash | ( | uint | key1,
|  | uint | key2 |
| ) |  |  | `[static]`

Calculates a hash value for use in the NPF.

Parameters:

  | key1 | The TileIndex of the tile to hash
  | key2 | The Trackdir of the track on the tile.

Todo:
  Think of a better hash.

Definition at line 78 of file npf.cpp.

References IsValidTile(), IsValidTrackdir(), TileX(), TileY(), and TRACKDIR_END.

static void NPFMarkTile | ( | TileIndex | tile | ) | `[static]`

Mark tiles by mowing the grass when npf debug level >= 1.

Will not work for multiplayer games, since it can (will) cause desyncs.

Definition at line 227 of file npf.cpp.

References _networking, GetTileType(), IsTileDepotType(), MarkTileDirtyByTile(), MP_RAILWAY, MP_ROAD, and RAIL_GROUND_BARREN.

## Variable Documentation

const uint _trackdir_length[TRACKDIR_END]`[static]`

Initial value:

     {
      NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH,
      0, 0,
      NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH
    }

Definition at line 33 of file npf.cpp.