10 #ifndef YAPF_COMMON_HPP
11 #define YAPF_COMMON_HPP
13 #include "../../core/bitmath_func.hpp"
14 #include "../../direction_type.h"
15 #include "../../map_func.h"
16 #include "../../tile_type.h"
17 #include "../../track_type.h"
18 #include "../pathfinder_type.h"
21 template <
class Types>
25 typedef typename Types::Tpf
Tpf;
26 typedef typename Types::NodeList::Titem
Node;
27 typedef typename Node::Key
Key;
36 return *
static_cast<Tpf *
>(
this);
43 this->origin_tile = tile;
44 this->origin_trackdirs = trackdirs;
54 n1.Set(
nullptr, this->origin_tile, td, is_choice);
55 Yapf().AddStartupNode(n1);
61 template <
class Types>
65 typedef typename Types::Tpf
Tpf;
66 typedef typename Types::NodeList::Titem
Node;
67 typedef typename Node::Key
Key;
80 return *
static_cast<Tpf *
>(
this);
87 this->origin_tile = tile;
89 this->reverse_tile = tiler;
90 this->reverse_td = tdr;
100 n1.Set(
nullptr, this->origin_tile, this->origin_td,
false);
101 Yapf().AddStartupNode(n1);
105 n2.Set(
nullptr, this->reverse_tile, this->reverse_td,
false);
107 Yapf().AddStartupNode(n2);
119 template <
class Types>
123 typedef typename Types::Tpf
Tpf;
124 typedef typename Types::NodeList::Titem
Node;
125 typedef typename Node::Key
Key;
135 this->dest_tile = tile;
136 this->dest_trackdirs = trackdirs;
143 return *
static_cast<Tpf *
>(
this);
150 return (n.key.tile == this->dest_tile) &&
HasTrackdir(this->dest_trackdirs, n.GetTrackdir());
159 static const int dg_dir_to_x_offs[] = {-1, 0, 1, 0};
160 static const int dg_dir_to_y_offs[] = {0, 1, 0, -1};
168 int x1 = 2 *
TileX(tile) + dg_dir_to_x_offs[(int)exitdir];
169 int y1 = 2 *
TileY(tile) + dg_dir_to_y_offs[(int)exitdir];
170 int x2 = 2 *
TileX(this->dest_tile);
171 int y2 = 2 *
TileY(this->dest_tile);
172 int dx =
abs(x1 - x2);
173 int dy =
abs(y1 - y2);
174 int dmin = std::min(dx, dy);
175 int dxy =
abs(dx - dy);
177 n.estimate = n.cost + d;
178 assert(n.estimate >= n.parent->estimate);
189 template <
class Ttypes>
191 :
public Ttypes::PfBase
192 ,
public Ttypes::PfCost
193 ,
public Ttypes::PfCache
194 ,
public Ttypes::PfOrigin
195 ,
public Ttypes::PfDestination
196 ,
public Ttypes::PfFollow
constexpr uint8_t FindFirstBit(T x)
Search the first set bit in a value.
constexpr T KillFirstBit(T value)
Clear the first bit in an integer.
YAPF destination provider base class - used when destination is single tile / multiple trackdirs.
Tpf & Yapf()
to access inherited path finder
TrackdirBits dest_trackdirs
destination trackdir mask
void SetDestination(TileIndex tile, TrackdirBits trackdirs)
set the destination tile / more trackdirs
Node::Key Key
key to hash tables
bool PfDetectDestination(Node &n)
Called by YAPF to detect if node ends in the desired destination.
bool PfCalcEstimate(Node &n)
Called by YAPF to calculate cost estimate.
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
TileIndex dest_tile
destination tile
Types::NodeList::Titem Node
this will be our node type
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
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Types::NodeList::Titem Node
this will be our node type
TrackdirBits origin_trackdirs
origin trackdir mask
void SetOrigin(TileIndex tile, TrackdirBits trackdirs)
Set origin tile / trackdir mask.
void PfSetStartupNodes()
Called when YAPF needs to place origin nodes into open list.
TileIndex origin_tile
origin tile
YAPF origin provider base class - used when there are two tile/trackdir origins.
Trackdir reverse_td
second (reverse) origin trackdir
Tpf & Yapf()
to access inherited path finder
void PfSetStartupNodes()
Called when YAPF needs to place origin nodes into open list.
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
bool TreatFirstRedTwoWaySignalAsEOL()
return true if first two-way signal should be treated as dead end
int reverse_penalty
penalty to be added for using the reverse origin
Node::Key Key
key to hash tables
TileIndex reverse_tile
second (reverse) origin tile
Trackdir origin_td
first origin trackdir
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
bool treat_first_red_two_way_signal_as_eol
in some cases (leaving station) we need to handle first two-way signal differently
TileIndex origin_tile
first origin tile
YAPF template that uses Ttypes template argument to determine all YAPF components (base classes) from...
DiagDirection
Enumeration for diagonal directions.
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
static const int YAPF_TILE_CORNER_LENGTH
Length (penalty) of a corner with YAPF.
static const int YAPF_TILE_LENGTH
Length (penalty) of one tile with YAPF.
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
bool HasTrackdir(TrackdirBits trackdirs, Trackdir trackdir)
Checks whether a TrackdirBits has a given Trackdir.
DiagDirection TrackdirToExitdir(Trackdir trackdir)
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
Trackdir
Enumeration for tracks and directions.
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
TrackdirBits
Allow incrementing of Trackdir variables.
@ TRACKDIR_BIT_NONE
No track build.