# yapf_rail.cpp File Reference

The rail pathfinding. More...

`#include "../stdafx.h"`
`#include "yapf.hpp"`
`#include "yapf_node_rail.hpp"`
`#include "yapf_costrail.hpp"`
`#include "yapf_destrail.hpp"`
`#include "../vehicle_func.h"`
`#include "../functions.h"`

Go to the source code of this file.

## Data Structures
class | CYapfReserveTrack< Types >
class | CYapfFollowAnyDepotRailT< Types >
class | CYapfFollowAnySafeTileRailT< Types >
class | CYapfFollowRailT< Types >
struct | CYapfRail_TypesT< Tpf_, Ttrack_follower, Tnode_list, TdestinationT, TfollowT >
struct | CYapfRail1
struct | CYapfRail2
struct | CYapfAnyDepotRail1
struct | CYapfAnyDepotRail2
struct | CYapfAnySafeTileRail1
struct | CYapfAnySafeTileRail2
## Defines
#define | DEBUG_YAPF_CACHE 0
## Functions
Trackdir | YapfChooseRailTrack (const Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found, bool reserve_track, PBSTileInfo *target)
| Finds the best path for given train.
bool | YapfCheckReverseTrain (const Vehicle *v)
| Returns true if it is better to reverse the train before leaving station.
bool | YapfFindNearestRailDepotTwoWay (const Vehicle *v, int max_distance, int reverse_penalty, TileIndex *depot_tile, bool *reversed)
| Used when user sends train to the nearest depot or if train needs servicing.
bool | YapfRailFindNearestSafeTile (const Vehicle *v, TileIndex tile, Trackdir td, bool override_railtype)
| Try to extend the reserved path of a train to the nearest safe tile.
void | YapfNotifyTrackLayoutChange (TileIndex tile, Track track)
| Use this function to notify YAPF that track layout (or signal configuration) has change.
## Variables
int | _total_pf_time_us = 0

## Detailed Description

The rail pathfinding.

Definition in file yapf_rail.cpp.

## Function Documentation

Trackdir YapfChooseRailTrack | ( | const Vehicle * | v,
|  | TileIndex | tile,
|  | DiagDirection | enterdir,
|  | TrackBits | tracks,
|  | bool * | path_not_found,
|  | bool | reserve_track,
|  | PBSTileInfo * | target |
| ) |  |  |

Finds the best path for given train.

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 trackdir for next turn or INVALID_TRACKDIR if the path could not be found

Definition at line 520 of file yapf_rail.cpp.

References _settings_game, PathfinderSettings::forbid_90_deg, and GameSettings::pf.

Referenced by DoTrainPathfind().

bool YapfFindNearestRailDepotTwoWay | ( | const Vehicle * | v,
|  | int | max_distance,
|  | int | reverse_penalty,
|  | TileIndex * | depot_tile,
|  | bool * | reversed |
| ) |  |  |

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

Parameters:

  | v | train that needs to go to some depot
  | max_distance | max distance (number of track tiles) from the current train position (used also as optimization - the pathfinder can stop path finding if max_distance was reached and no depot was seen)
  | reverse_penalty | penalty that should be added for the path that requires reversing the train first
  | depot_tile | receives the depot tile if depot was found
  | reversed | receives true if train needs to reversed first

Returns:
  the true if depot was found.

Definition at line 596 of file yapf_rail.cpp.

References _settings_game, FollowTrainReservation(), PathfinderSettings::forbid_90_deg, GetVehicleTrackdir(), INVALID_TILE, GameSettings::pf, ReverseTrackdir(), PBSTileInfo::tile, and PBSTileInfo::trackdir.

Referenced by FindClosestTrainDepot().

bool YapfRailFindNearestSafeTile | ( | const Vehicle * | v,
|  | TileIndex | tile,
|  | Trackdir | td,
|  | bool | override_railtype |
| ) |  |  |

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

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 compabtible railtypes be searched, even if the vehicle can't on them on it own?

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

Definition at line 619 of file yapf_rail.cpp.

References _settings_game, PathfinderSettings::forbid_90_deg, and GameSettings::pf.

Referenced by TryReserveSafeTrack().