12 #ifndef YAPF_COMMON_HPP 13 #define YAPF_COMMON_HPP 16 template <
class Types>
20 typedef typename Types::Tpf
Tpf;
21 typedef typename Types::NodeList::Titem
Node;
22 typedef typename Node::Key
Key;
31 return *
static_cast<Tpf *
>(
this);
39 m_orgTrackdirs = trackdirs;
48 Node &n1 =
Yapf().CreateNewNode();
49 n1.Set(NULL, m_orgTile, td, is_choice);
50 Yapf().AddStartupNode(n1);
56 template <
class Types>
60 typedef typename Types::Tpf
Tpf;
61 typedef typename Types::NodeList::Titem
Node;
62 typedef typename Node::Key
Key;
75 return *
static_cast<Tpf *
>(
this);
86 m_reverse_penalty = reverse_penalty;
87 m_treat_first_red_two_way_signal_as_eol = treat_first_red_two_way_signal_as_eol;
94 Node &n1 =
Yapf().CreateNewNode();
95 n1.Set(NULL, m_orgTile, m_orgTd,
false);
96 Yapf().AddStartupNode(n1);
99 Node &n2 =
Yapf().CreateNewNode();
100 n2.Set(NULL, m_revTile, m_revTd,
false);
101 n2.m_cost = m_reverse_penalty;
102 Yapf().AddStartupNode(n2);
109 return Yapf().PfGetSettings().rail_firstred_twoway_eol && m_treat_first_red_two_way_signal_as_eol;
114 template <
class Types>
118 typedef typename Types::Tpf
Tpf;
119 typedef typename Types::NodeList::Titem
Node;
120 typedef typename Node::Key
Key;
131 m_destTrackdirs = trackdirs;
138 return *
static_cast<Tpf *
>(
this);
155 static const int dg_dir_to_x_offs[] = {-1, 0, 1, 0};
156 static const int dg_dir_to_y_offs[] = {0, 1, 0, -1};
157 if (PfDetectDestination(n)) {
158 n.m_estimate = n.m_cost;
164 int x1 = 2 *
TileX(tile) + dg_dir_to_x_offs[(int)exitdir];
165 int y1 = 2 *
TileY(tile) + dg_dir_to_y_offs[(int)exitdir];
166 int x2 = 2 *
TileX(m_destTile);
167 int y2 = 2 *
TileY(m_destTile);
168 int dx =
abs(x1 - x2);
169 int dy =
abs(y1 - y2);
170 int dmin =
min(dx, dy);
171 int dxy =
abs(dx - dy);
173 n.m_estimate = n.m_cost + d;
174 assert(n.m_estimate >= n.m_parent->m_estimate);
185 template <
class Ttypes>
187 :
public Ttypes::PfBase
188 ,
public Ttypes::PfCost
189 ,
public Ttypes::PfCache
190 ,
public Ttypes::PfOrigin
191 ,
public Ttypes::PfDestination
192 ,
public Ttypes::PfFollow
void PfSetStartupNodes()
Called when YAPF needs to place origin nodes into open list.
YAPF origin provider base class - used when origin is one tile / multiple trackdirs.
Node::Key Key
key to hash tables
Tpf & Yapf()
to access inherited path finder
int m_reverse_penalty
penalty to be added for using the reversed origin
static uint TileX(TileIndex tile)
Get the X component of a tile.
void SetOrigin(TileIndex tile, TrackdirBits trackdirs)
Set origin tile / trackdir mask.
bool PfDetectDestination(Node &n)
Called by YAPF to detect if node ends in the desired destination.
YAPF destination provider base class - used when destination is single tile / multiple trackdirs...
Node::Key Key
key to hash tables
static DiagDirection TrackdirToExitdir(Trackdir trackdir)
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
TrackdirBits m_destTrackdirs
destination trackdir mask
void SetOrigin(TileIndex tile, Trackdir td, TileIndex tiler=INVALID_TILE, Trackdir tdr=INVALID_TRACKDIR, int reverse_penalty=0, bool treat_first_red_two_way_signal_as_eol=true)
set origin (tiles, trackdirs, etc.)
Types::NodeList::Titem Node
this will be our node type
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
static TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir)
Maps a Trackdir to the corresponding TrackdirBits value.
static const int YAPF_TILE_LENGTH
Length (penalty) of one tile with YAPF.
Trackdir
Enumeration for tracks and directions.
YAPF template that uses Ttypes template argument to determine all YAPF components (base classes) from...
TileIndex m_orgTile
origin tile
TileIndex m_revTile
second (reversed) origin tile
Types::NodeList::Titem Node
this will be our node type
DiagDirection
Enumeration for diagonal directions.
static T min(const T a, const T b)
Returns the minimum of two values.
Types::NodeList::Titem Node
this will be our node type
Trackdir m_revTd
second (reversed) origin trackdir
bool PfCalcEstimate(Node &n)
Called by YAPF to calculate cost estimate.
static T KillFirstBit(T value)
Clear the first bit in an integer.
Node::Key Key
key to hash tables
Tpf & Yapf()
to access inherited path finder
uint32 TileIndex
The index/ID of a Tile.
static uint TileY(TileIndex tile)
Get the Y component of a tile.
static T abs(const T a)
Returns the absolute value of (scalar) variable.
bool TreatFirstRedTwoWaySignalAsEOL()
return true if first two-way signal should be treated as dead end
Trackdir m_orgTd
first origin trackdir
static uint8 FindFirstBit2x64(const int value)
Finds the position of the first non-zero bit in an integer.
TrackdirBits
Enumeration of bitmasks for the TrackDirs.
void PfSetStartupNodes()
Called when YAPF needs to place origin nodes into open list.
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
TileIndex m_orgTile
first origin tile
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
bool m_treat_first_red_two_way_signal_as_eol
in some cases (leaving station) we need to handle first two-way signal differently ...
YAPF origin provider base class - used when there are two tile/trackdir origins.
Flag for an invalid trackdir.
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
static const int YAPF_TILE_CORNER_LENGTH
Length (penalty) of a corner with YAPF.
Tpf & Yapf()
to access inherited path finder
TrackdirBits m_orgTrackdirs
origin trackdir mask
void SetDestination(TileIndex tile, TrackdirBits trackdirs)
set the destination tile / more trackdirs
TileIndex m_destTile
destination tile