# npf_func.h File Reference

Functions to access the new pathfinder. More...

`#include "../../track_type.h"`
`#include "../../direction_type.h"`
`#include "../../vehicle_type.h"`
`#include "../pathfinder_type.h"`

Go to the source code of this file.

## Functions

FindDepotData | NPFRoadVehicleFindNearestDepot (const RoadVehicle *v, int max_penalty)
| Used when user sends road vehicle to the nearest depot or if road vehicle needs servicing using NPF.
Trackdir | NPFRoadVehicleChooseTrack (const RoadVehicle *v, TileIndex tile, DiagDirection enterdir, TrackdirBits trackdirs)
| Finds the best path for given road vehicle using NPF.
Track | NPFShipChooseTrack (const Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
| Finds the best path for given ship using NPF.
FindDepotData | NPFTrainFindNearestDepot (const Train *v, int max_penalty)
| Used when user sends train to the nearest depot or if train needs servicing using NPF.
bool | NPFTrainFindNearestSafeTile (const Train *v, TileIndex tile, Trackdir td, bool override_railtype)
| Try to extend the reserved path of a train to the nearest safe tile using NPF.
bool | NPFTrainCheckReverse (const Train *v)
| Returns true if it is better to reverse the train before leaving station using NPF.
Track | NPFTrainChooseTrack (const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found, bool reserve_track, struct PBSTileInfo *target)
| Finds the best path for given train using NPF.

## Detailed Description

Functions to access the new pathfinder.

Definition in file npf_func.h.

## Function Documentation

Trackdir NPFRoadVehicleChooseTrack | ( | const RoadVehicle * | v,
|  | TileIndex | tile,
|  | DiagDirection | enterdir,
|  | TrackdirBits | trackdirs |
| ) |  |  |

Finds the best path for given road vehicle using NPF.

Parameters:

  | v | the RV that needs to find a path
  | tile | the tile to find the path from (should be next tile the RV is about to enter)
  | enterdir | diagonal direction which the RV will enter this new tile from
  | trackdirs | available trackdirs on the new tile (to choose from)

Returns:
  the best trackdir for next turn or INVALID_TRACKDIR if the path could not be found

Definition at line 1142 of file npf.cpp.

References NPFFoundTargetData::best_trackdir, DiagDirToDiagTrackdir(), FindFirstBit2x64(), INVALID_RAILTYPES, INVALID_TRACKDIR, TileOffsByDiagDir(), and TRANSPORT_ROAD.

Referenced by RoadFindPathToDest().

FindDepotData NPFRoadVehicleFindNearestDepot | ( | const RoadVehicle * | v,
|  | int | max_penalty |
| ) |  |  |

Used when user sends road vehicle to the nearest depot or if road vehicle needs servicing using NPF.

Parameters:

  | v | vehicle that needs to go to some depot
  | max_penalty | max distance (in pathfinder penalty) from the current vehicle position (used also as optimization - the pathfinder can stop path finding if max_penalty was reached and no depot was seen)

Returns:
  the data about the depot

Definition at line 1126 of file npf.cpp.

References NPFFoundTargetData::best_bird_dist, NPFFoundTargetData::best_path_dist, RoadVehicle::GetVehicleTrackdir(), INVALID_RAILTYPES, NPFFoundTargetData::node, ReverseTrackdir(), Vehicle::tile, and TRANSPORT_ROAD.

Track NPFShipChooseTrack | ( | const Ship * | v,
|  | TileIndex | tile,
|  | DiagDirection | enterdir,
|  | TrackBits | tracks |
| ) |  |  |

Finds the best path for given ship using NPF.

Parameters:

  | v | the ship that needs to find a path
  | tile | the tile to find the path from (should be next tile the ship is about to enter)
  | enterdir | diagonal direction which the ship will enter this new tile from
  | tracks | available tracks on the new tile (to choose from)

Returns:
  the best trackdir for next turn or INVALID_TRACK if the path could not be found

Definition at line 1166 of file npf.cpp.

References NPFFoundTargetData::best_trackdir, Ship::GetVehicleTrackdir(), INVALID_RAILTYPES, INVALID_TRACK, INVALID_TRACKDIR, TileOffsByDiagDir(), TrackdirToTrack(), and TRANSPORT_WATER.

Referenced by ChooseShipTrack().

bool NPFTrainCheckReverse | ( | const Train * | v | ) |

Returns true if it is better to reverse the train before leaving station using NPF.

Parameters:

  | v | the train leaving the station

Returns:
  true if reversing is better

Definition at line 1228 of file npf.cpp.

References NPFFoundTargetData::best_bird_dist, Train::GetVehicleTrackdir(), INVALID_TRACKDIR, SpecializedVehicle< T, Type >::Last(), NPFFoundTargetData::node, NPF_FLAG_REVERSE, NPFGetFlag(), ReverseTrackdir(), Vehicle::tile, and TRANSPORT_RAIL.

Track NPFTrainChooseTrack | ( | const Train * | v,
|  | TileIndex | tile,
|  | DiagDirection | enterdir,
|  | TrackBits | tracks,
|  | bool * | path_not_found,
|  | bool | reserve_track,
|  | struct PBSTileInfo * | target |
| ) |  |  |

Finds the best path for given train using NPF.

Parameters:

  | v | the train that needs to find a path
  | tile | the tile to find the path from (should be next tile the train is about to enter)
  | enterdir | diagonal direction which the RV will enter this new tile from
  | tracks | available trackdirs on the new tile (to choose from)
  | path_not_found | [out] true is returned if no path can be found (returned Trackdir is only a 'guess')
  | reserve_track | indicates whether YAPF should try to reserve the found path
  | target | [out] the target tile of the reservation, free is set to true if path was reserved

Returns:
  the best track for next turn

Definition at line 1246 of file npf.cpp.

References NPFFoundTargetData::best_bird_dist, NPFFoundTargetData::best_trackdir, FindFirstTrack(), FollowTrainReservation(), INVALID_TRACKDIR, IsValidTrackdir(), NPFFoundTargetData::node, PBSTileInfo::okay, NPFFoundTargetData::res_okay, PBSTileInfo::tile, PBSTileInfo::trackdir, TrackdirToTrack(), and TRANSPORT_RAIL.

Referenced by DoTrainPathfind().

FindDepotData NPFTrainFindNearestDepot | ( | const Train * | v,
|  | int | max_penalty |
| ) |  |  |

Used when user sends train to the nearest depot or if train needs servicing using NPF.

Parameters:

  | v | train that needs to go to some depot
  | max_penalty | max max_penalty (in pathfinder penalty) from the current train position (used also as optimization - the pathfinder can stop path finding if max_penalty was reached and no depot was seen)

Returns:
  the data about the depot

Definition at line 1186 of file npf.cpp.

References NPFFoundTargetData::best_bird_dist, NPFFoundTargetData::best_path_dist, Train::GetVehicleTrackdir(), INVALID_TRACKDIR, SpecializedVehicle< T, Type >::Last(), NPFFoundTargetData::node, NPF_FLAG_REVERSE, NPF_INFINITE_PENALTY, NPFGetFlag(), ReverseTrackdir(), Vehicle::tile, and TRANSPORT_RAIL.

Referenced by FindClosestTrainDepot().

bool NPFTrainFindNearestSafeTile | ( | const Train * | v,
|  | TileIndex | tile,
|  | Trackdir | td,
|  | bool | override_railtype |
| ) |  |  |

Try to extend the reserved path of a train to the nearest safe tile using NPF.

Parameters:

  | v | The train that needs to find a safe tile.
  | tile | Last tile of the current reserved path.
  | td | Last trackdir of the current reserved path.
  | override_railtype | Should all physically compatible railtypes be searched, even if the vehicle can't run on them on its own?

Returns:
  True if the path could be extended to a safe tile.

Definition at line 1204 of file npf.cpp.

References RailtypeInfo::compatible_railtypes, GetRailTypeInfo(), INVALID_TRACKDIR, NPF_FLAG_IGNORE_RESERVED, NPF_TRACKDIR_CHOICE, NPFFindSafeTile(), NPFSetFlag(), NPFFoundTargetData::res_okay, NPFFindStationOrTileData::reserve_path, TRANSPORT_RAIL, NPFFindStationOrTileData::v, and VEH_TRAIN.

Referenced by TryReserveSafeTrack().