12 #include "../../stdafx.h" 13 #include "../../ship.h" 18 #include "../../safeguards.h" 21 template <
class Types>
25 typedef typename Types::Tpf
Tpf;
26 typedef typename Types::TrackFollower TrackFollower;
27 typedef typename Types::NodeList::Titem
Node;
28 typedef typename Node::Key
Key;
34 return *
static_cast<Tpf *
>(
this);
45 TrackFollower F(
Yapf().GetVehicle());
46 if (F.Follow(old_node.m_key.m_tile, old_node.m_key.m_td)) {
47 Yapf().AddMultipleNodes(&old_node, F);
84 pf.SetOrigin(src_tile, trackdirs);
85 pf.SetDestination(v->
dest_tile, dest_trackdirs);
87 path_found = pf.FindPath(v);
91 Node *pNode = pf.GetBestNode();
94 for (Node *n = pNode; n->m_parent != NULL; n = n->m_parent) steps++;
97 Node *pPrevNode = NULL;
98 while (pNode->m_parent != NULL) {
101 td = pNode->GetTrackdir();
102 path_cache.push_front(td);
105 pNode = pNode->m_parent;
108 Node &best_next_node = *pPrevNode;
109 assert(best_next_node.GetTile() == tile);
110 next_trackdir = best_next_node.GetTrackdir();
112 if (path_found && !path_cache.empty()) path_cache.pop_back();
114 return next_trackdir;
135 pf.SetDestination(v->
dest_tile, dest_trackdirs);
137 if (!pf.FindPath(v))
return false;
139 Node *pNode = pf.GetBestNode();
140 if (pNode == NULL)
return false;
144 while (pNode->m_parent != NULL) {
145 pNode = pNode->m_parent;
148 Trackdir best_trackdir = pNode->GetTrackdir();
149 assert(best_trackdir == td1 || best_trackdir == td2);
150 return best_trackdir == td2;
155 template <
class Types>
159 typedef typename Types::Tpf
Tpf;
160 typedef typename Types::TrackFollower TrackFollower;
161 typedef typename Types::NodeList::Titem
Node;
162 typedef typename Node::Key
Key;
168 return *
static_cast<Tpf *
>(
this);
182 if (n.GetTrackdir() !=
NextTrackdir(n.m_parent->GetTrackdir())) {
193 if (speed_frac > 0) c +=
YAPF_TILE_LENGTH * (1 + tf->m_tiles_skipped) * speed_frac / (256 - speed_frac);
196 n.m_cost = n.m_parent->m_cost + c;
205 template <
class Tpf_,
class Ttrack_follower,
class Tnode_list>
228 struct CYapfShip1 :
CYapfT<CYapfShip_TypesT<CYapfShip1, CFollowTrackWater , CShipNodeListTrackDir> > {};
230 struct CYapfShip2 :
CYapfT<CYapfShip_TypesT<CYapfShip2, CFollowTrackWater , CShipNodeListExitDir > > {};
232 struct CYapfShip3 :
CYapfT<CYapfShip_TypesT<CYapfShip3, CFollowTrackWaterNo90, CShipNodeListTrackDir> > {};
248 Trackdir td_ret = pfnChooseShipTrack(v, tile, enterdir, tracks, path_found, path_cache);
259 PfnCheckReverseShip pfnCheckReverseShip = CYapfShip2::CheckShipReverse;
263 pfnCheckReverseShip = &CYapfShip3::CheckShipReverse;
265 pfnCheckReverseShip = &CYapfShip1::CheckShipReverse;
268 bool reverse = pfnCheckReverseShip(v, tile, td, td_rev);
Information about a ship vehicle.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
YAPF origin provider base class - used when origin is one tile / multiple trackdirs.
CYapfShip_TypesT< Tpf_, Ttrack_follower, Tnode_list > Types
Types - shortcut for this struct type.
Ttrack_follower TrackFollower
track follower helper class
byte ocean_speed_frac
Fraction of maximum speed for ocean tiles.
static Track TrackdirToTrack(Trackdir trackdir)
Returns the Track that a given Trackdir represents.
char TransportTypeChar() const
return debug report character to identify the transportation type
Track
These are used to specify a single track.
TileIndex dest_tile
Heading for this tile.
CYapfSegmentCostCacheNoneT - the formal only yapf cost cache provider that implements PfNodeCacheFetc...
Tnode_list NodeList
node list type
PathfinderSettings pf
settings for all pathfinders
static TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
Adds a DiagDir to a tile.
Node::Key Key
key to hash tables
static Trackdir NextTrackdir(Trackdir trackdir)
Maps a trackdir to the trackdir that you will end up on if you go straight ahead. ...
bool forbid_90_deg
forbid trains to make 90 deg turns
static const int YAPF_SHIP_PATH_CACHE_LENGTH
Maximum length of ship path cache.
YAPF destination provider base class - used when destination is single tile / multiple trackdirs...
static TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir)
Returns all trackdirs that can be reached when entering a tile from a given (diagonal) direction...
Tpf & Yapf()
to access inherited path finder
CYapfBaseT< Types > PfBase
pathfinder components (modules)
Types::NodeList::Titem Node
this will be our node type
TrackBits
Bitfield corresponding to Track.
Cost Provider module of YAPF for ships.
static bool IsValidTrackdir(Trackdir trackdir)
Checks if a Trackdir is valid for non-road vehicles.
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
CYapfBaseT - A-star type path finder base class.
Tpf & Yapf()
to access inherited path finder
bool disable_node_optimization
whether to use exit-dir instead of trackdir in node key
Config struct of YAPF for ships.
YAPFSettings yapf
pathfinder settings for the yet another pathfinder
VehicleType
Available vehicle types.
static TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir)
Maps a Trackdir to the corresponding TrackdirBits value.
static DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
static bool HasTrackdir(TrackdirBits trackdirs, Trackdir trackdir)
Checks whether a TrackdirBits has a given Trackdir.
Node Follower module of YAPF for ships.
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 tile
Current tile index.
static bool CheckShipReverse(const Ship *v, TileIndex tile, Trackdir td1, Trackdir td2)
Check whether a ship should reverse to reach its destination.
DiagDirection
Enumeration for diagonal directions.
All ships have this type.
static Trackdir ReverseTrackdir(Trackdir trackdir)
Maps a trackdir to the reverse trackdir.
TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
Returns information about trackdirs and signal states.
Flag for an invalid track.
uint32 TileIndex
The index/ID of a Tile.
Base includes/functions for YAPF.
Types::NodeList::Titem Node
this will be our node type
static bool IsDiagonalTrackdir(Trackdir trackdir)
Checks if a given Trackdir is diagonal.
Trackdir GetVehicleTrackdir() const
Returns the Trackdir on which the vehicle is currently located.
static uint8 FindFirstBit2x64(const int value)
Finds the position of the first non-zero bit in an integer.
Track YapfShipChooseTrack(const Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, ShipPathCache &path_cache)
Ship controller helper - path finder invoker.
TrackdirBits
Enumeration of bitmasks for the TrackDirs.
static TrackdirBits TrackBitsToTrackdirBits(TrackBits bits)
Converts TrackBits to TrackdirBits while allowing both directions.
bool PfCalcCost(Node &n, const TrackFollower *tf)
Called by YAPF to calculate the cost from the origin to the given node.
Flag for an invalid trackdir.
Tpf_ Tpf
Tpf - pathfinder type.
static const int YAPF_TILE_CORNER_LENGTH
Length (penalty) of a corner with YAPF.
WaterClass GetEffectiveWaterClass(TileIndex tile)
Determine the effective WaterClass for a ship travelling on a tile.
Node::Key Key
key to hash tables
byte canal_speed_frac
Fraction of maximum speed for canal/river tiles.
static TrackdirBits TrackStatusToTrackdirBits(TrackStatus ts)
Returns the present-trackdir-information of a TrackStatus.
void PfFollowNode(Node &old_node)
Called by YAPF to move from the given node to the next tile.
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
bool YapfShipCheckReverse(const Ship *v)
Returns true if it is better to reverse the ship before leaving depot using YAPF. ...
Node tailored for ship pathfinding.
static Track ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
Runs the pathfinder to choose a track to continue along.