changeset 3914:aa90027eabb7 draft

(svn r5018) [YAPF] Added some comments to YAPF implementation.
author KUDr <KUDr@openttd.org>
date Mon, 29 May 2006 18:39:42 +0000
parents e07beca4cf6f
children 1fac5f0ab016
files yapf/unittest/test_yapf.h yapf/yapf_base.hpp yapf/yapf_common.hpp yapf/yapf_costcache.hpp yapf/yapf_costrail.hpp yapf/yapf_destrail.hpp yapf/yapf_rail.cpp yapf/yapf_road.cpp yapf/yapf_ship.cpp
diffstat 9 files changed, 171 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/yapf/unittest/test_yapf.h
+++ b/yapf/unittest/test_yapf.h
@@ -132,9 +132,9 @@
 template <class Types>
 struct CYapfTestBaseT
 {
-	typedef typename Types::Tpf Tpf;
+	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
-	typedef typename Node::Key Key;    ///< key to hash tables
+	typedef typename Node::Key Key;               ///< key to hash tables
 	typedef typename Types::Map Map;
 
 	int m_x1, m_y1;
@@ -155,9 +155,13 @@
 		m_td1 = td1;
 	}
 
+	/// to access inherited path finder
 	Tpf& Yapf() {return *static_cast<Tpf*>(this);}
 	FORCEINLINE char TransportTypeChar() const {return 'T';}
 
+	/** Called by YAPF to move from the given node to the next tile. For each
+	*   reachable trackdir on the new tile creates new node, initializes it
+	*   and adds it to the open list by calling Yapf().AddNewNode(n) */
 	FORCEINLINE void PfFollowNode(Node& org)
 	{
 		int x_org = org.m_key.m_x;
@@ -191,6 +195,7 @@
 		}
 	}
 
+	/// Called when YAPF needs to place origin nodes into open list
 	FORCEINLINE void PfSetStartupNodes()
 	{
 		Node& n1 = Yapf().CreateNewNode();
@@ -201,6 +206,9 @@
 		Yapf().AddStartupNode(n1);
 	}
 
+	/** Called by YAPF to calculate the cost from the origin to the given node.
+	*   Calculates only the cost of given node, adds it to the parent node cost
+	*   and stores the result into Node::m_cost member */
 	FORCEINLINE bool PfCalcCost(Node& n)
 	{
 		// base tile cost depending on distance
@@ -216,6 +224,8 @@
 		return true;
 	}
 
+	/** Called by YAPF to calculate cost estimate. Calculates distance to the destination
+	*   adds it to the actual cost from origin and stores the sum to the Node::m_estimate */
 	FORCEINLINE bool PfCalcEstimate(Node& n)
 	{
 		int dx = abs(n.m_key.m_x - m_x2);
@@ -227,6 +237,7 @@
 		return true;
 	}
 
+	/// Called by YAPF to detect if node ends in the desired destination
 	FORCEINLINE bool PfDetectDestination(Node& n)
 	{
 		bool bDest = (n.m_key.m_x == m_x2) && (n.m_key.m_y == m_y2);
--- a/yapf/yapf_base.hpp
+++ b/yapf/yapf_base.hpp
@@ -45,34 +45,34 @@
 template <class Types>
 class CYapfBaseT {
 public:
-	typedef typename Types::Tpf Tpf;
+	typedef typename Types::Tpf Tpf;           ///< the pathfinder class (derived from THIS class)
 	typedef typename Types::NodeList NodeList; ///< our node list
 	typedef typename NodeList::Titem Node;     ///< this will be our node type
 	typedef typename Node::Key Key;            ///< key to hash tables
 
 
-	NodeList             m_nodes;         ///< node list multi-container
+	NodeList             m_nodes;              ///< node list multi-container
 protected:
-	Node*                m_pBestDestNode; ///< pointer to the destination node found at last round
-	Node*                m_pBestIntermediateNode;
-	const YapfSettings  *m_settings;
-	int                  m_max_search_nodes;
-	Vehicle*             m_veh;
+	Node*                m_pBestDestNode;      ///< pointer to the destination node found at last round
+	Node*                m_pBestIntermediateNode; ///< here should be node closest to the destination if path not found
+	const YapfSettings  *m_settings;           ///< current settings (_patches.yapf)
+	int                  m_max_search_nodes;   ///< maximum number of nodes we are allowed to visit before we give up
+	Vehicle*             m_veh;                ///< vehicle that we are trying to drive
 
-	int                  m_stats_cost_calcs;
-	int                  m_stats_cache_hits;
+	int                  m_stats_cost_calcs;   ///< stats - how many node's costs were calculated
+	int                  m_stats_cache_hits;   ///< stats - how many node's costs were reused from cache
 
 public:
-	CPerformanceTimer    m_perf_cost;
-	CPerformanceTimer    m_perf_slope_cost;
-	CPerformanceTimer    m_perf_ts_cost;
-	CPerformanceTimer    m_perf_other_cost;
+	CPerformanceTimer    m_perf_cost;          ///< stats - total CPU time of this run
+	CPerformanceTimer    m_perf_slope_cost;    ///< stats - slope calculation CPU time
+	CPerformanceTimer    m_perf_ts_cost;       ///< stats - GetTrackStatus() CPU time
+	CPerformanceTimer    m_perf_other_cost;    ///< stats - other CPU time
 
 public:
-	int                  m_num_steps;     ///< this is there for debugging purposes (hope it doesn't hurt)
+	int                  m_num_steps;          ///< this is there for debugging purposes (hope it doesn't hurt)
 
 public:
-	// default constructor
+	/// default constructor
 	FORCEINLINE CYapfBaseT()
 		: m_pBestDestNode(NULL)
 		, m_pBestIntermediateNode(NULL)
@@ -90,12 +90,15 @@
 	{
 	}
 
+	/// default destructor
 	~CYapfBaseT() {}
 
 protected:
+	/// to access inherited path finder
 	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
 
 public:
+	/// return current settings (can be custom - player based - but later)
 	FORCEINLINE const YapfSettings& PfGetSettings() const
 	{
 		return *m_settings;
--- a/yapf/yapf_common.hpp
+++ b/yapf/yapf_common.hpp
@@ -3,28 +3,31 @@
 #ifndef  YAPF_COMMON_HPP
 #define  YAPF_COMMON_HPP
 
-
+/** YAPF origin provider base class - used when origin is one tile / multiple trackdirs */
 template <class Types>
 class CYapfOriginTileT
 {
 public:
-	typedef typename Types::Tpf Tpf;
+	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
-	typedef typename Node::Key Key;    ///< key to hash tables
+	typedef typename Node::Key Key;               ///< key to hash tables
 
 protected:
-	TileIndex    m_orgTile;
-	TrackdirBits m_orgTrackdirs;
+	TileIndex    m_orgTile;                       ///< origin tile
+	TrackdirBits m_orgTrackdirs;                  ///< origin trackdir mask
 
+	/// to access inherited path finder
 	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
 
 public:
+	/// Set origin tile / trackdir mask
 	void SetOrigin(TileIndex tile, TrackdirBits trackdirs)
 	{
 		m_orgTile = tile;
 		m_orgTrackdirs = trackdirs;
 	}
 
+	/// Called when YAPF needs to place origin nodes into open list
 	void PfSetStartupNodes()
 	{
 		for (TrackdirBits tdb = m_orgTrackdirs; tdb != TRACKDIR_BIT_NONE; tdb = (TrackdirBits)KillFirstBit2x64(tdb)) {
@@ -36,25 +39,28 @@
 	}
 };
 
+/** YAPF origin provider base class - used when there are two tile/trackdir origins */
 template <class Types>
 class CYapfOriginTileTwoWayT
 {
 public:
-	typedef typename Types::Tpf Tpf;
+	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
-	typedef typename Node::Key Key;    ///< key to hash tables
+	typedef typename Node::Key Key;               ///< key to hash tables
 
 protected:
-	TileIndex   m_orgTile;
-	Trackdir    m_orgTd;
-	TileIndex   m_revTile;
-	Trackdir    m_revTd;
-	int         m_reverse_penalty;
-	bool        m_treat_first_red_two_way_signal_as_eol;
+	TileIndex   m_orgTile;                        ///< first origin tile
+	Trackdir    m_orgTd;                          ///< first origin trackdir
+	TileIndex   m_revTile;                        ///< second (reversed) origin tile
+	Trackdir    m_revTd;                          ///< second (reversed) origin trackdir
+	int         m_reverse_penalty;                ///< penalty to be added for using the reversed origin
+	bool        m_treat_first_red_two_way_signal_as_eol; ///< in some cases (leaving station) we need to handle first two-way signal differently
 
+	/// to access inherited path finder
 	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
 
 public:
+	/// set origin (tiles, trackdirs, etc.)
 	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)
 	{
 		m_orgTile = tile;
@@ -65,6 +71,7 @@
 		m_treat_first_red_two_way_signal_as_eol = treat_first_red_two_way_signal_as_eol;
 	}
 
+	/// Called when YAPF needs to place origin nodes into open list
 	void PfSetStartupNodes()
 	{
 		if (m_orgTile != INVALID_TILE && m_orgTd != INVALID_TRACKDIR) {
@@ -80,25 +87,28 @@
 		}
 	}
 
+	/// return true if first two-way signal should be treated as dead end
 	FORCEINLINE bool TreatFirstRedTwoWaySignalAsEOL()
 	{
 		return Yapf().PfGetSettings().rail_firstred_twoway_eol && m_treat_first_red_two_way_signal_as_eol;
 	}
 };
 
+/** YAPF destination provider base class - used when destination is single tile / multiple trackdirs */
 template <class Types>
 class CYapfDestinationTileT
 {
 public:
-	typedef typename Types::Tpf Tpf;
+	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
-	typedef typename Node::Key Key;    ///< key to hash tables
+	typedef typename Node::Key Key;               ///< key to hash tables
 
 protected:
-	TileIndex    m_destTile;
-	TrackdirBits m_destTrackdirs;
+	TileIndex    m_destTile;                      ///< destination tile
+	TrackdirBits m_destTrackdirs;                 ///< destination trackdir mask
 
 public:
+	/// set the destination tile / more trackdirs
 	void SetDestination(TileIndex tile, TrackdirBits trackdirs)
 	{
 		m_destTile = tile;
@@ -106,15 +116,19 @@
 	}
 
 protected:
+	/// to access inherited path finder
 	Tpf& Yapf() {return *static_cast<Tpf*>(this);}
 
 public:
+	/// Called by YAPF to detect if node ends in the desired destination
 	FORCEINLINE bool PfDetectDestination(Node& n)
 	{
 		bool bDest = (n.m_key.m_tile == m_destTile) && ((m_destTrackdirs & TrackdirToTrackdirBits(n.GetTrackdir())) != TRACKDIR_BIT_NONE);
 		return bDest;
 	}
 
+	/** Called by YAPF to calculate cost estimate. Calculates distance to the destination
+	*   adds it to the actual cost from origin and stores the sum to the Node::m_estimate */
 	inline bool PfCalcEstimate(Node& n)
 	{
 		int dx = abs(TileX(n.GetTile()) - TileX(m_destTile));
@@ -128,14 +142,18 @@
 	}
 };
 
+/** YAPF template that uses Ttypes template argument to determine all YAPF
+*   components (base classes) from which the actual YAPF is composed.
+*   For example classes consult: CYapfRail_TypesT template and its instantiations:
+*   CYapfRail1, CYapfRail2, CYapfRail3, CYapfAnyDepotRail1, CYapfAnyDepotRail2, CYapfAnyDepotRail3 */
 template <class Ttypes>
 class CYapfT
-	: public Ttypes::PfBase
-	, public Ttypes::PfCost
-	, public Ttypes::PfCache
-	, public Ttypes::PfOrigin
-	, public Ttypes::PfDestination
-	, public Ttypes::PfFollow
+	: public Ttypes::PfBase         ///< Instance of CYapfBaseT - main YAPF loop and support base class
+	, public Ttypes::PfCost         ///< Cost calculation provider base class
+	, public Ttypes::PfCache        ///< Segment cost cache provider
+	, public Ttypes::PfOrigin       ///< Origin (tile or two-tile origin)
+	, public Ttypes::PfDestination  ///< Destination detector and distance (estimate) calculation provider
+	, public Ttypes::PfFollow       ///< Node follower (stepping provider)
 {
 };
 
--- a/yapf/yapf_costcache.hpp
+++ b/yapf/yapf_costcache.hpp
@@ -11,14 +11,18 @@
 class CYapfSegmentCostCacheNoneT
 {
 public:
-	typedef typename Types::Tpf Tpf;
+	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
 
+	/** Called by YAPF to attach cached or local segment cost data to the given node.
+	*   @return true if globally cached data were used or false if local data was used */
 	FORCEINLINE bool PfNodeCacheFetch(Node& n)
 	{
 		return false;
 	};
 
+	/** Called by YAPF to flush the cached segment cost data back into cache storage.
+	*   Current cache implementation doesn't use that. */
 	FORCEINLINE void PfNodeCacheFlush(Node& n)
 	{
 	};
@@ -33,9 +37,9 @@
 class CYapfSegmentCostCacheLocalT
 {
 public:
-	typedef typename Types::Tpf Tpf;
+	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
-	typedef typename Node::Key Key;    ///< key to hash tables
+	typedef typename Node::Key Key;               ///< key to hash tables
 	typedef typename Node::CachedData CachedData;
 	typedef typename CachedData::Key CacheKey;
 	typedef CArrayT<CachedData> LocalCache;
@@ -43,9 +47,12 @@
 protected:
 	LocalCache      m_local_cache;
 
+	/// to access inherited path finder
 	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
 
 public:
+	/** Called by YAPF to attach cached or local segment cost data to the given node.
+	*   @return true if globally cached data were used or false if local data was used */
 	FORCEINLINE bool PfNodeCacheFetch(Node& n)
 	{
 		CacheKey key(n.GetKey());
@@ -53,13 +60,19 @@
 		return false;
 	};
 
+	/** Called by YAPF to flush the cached segment cost data back into cache storage.
+	*   Current cache implementation doesn't use that. */
 	FORCEINLINE void PfNodeCacheFlush(Node& n)
 	{
 	};
 };
 
 
-
+/** Base class for segment cost cache providers. Contains global counter
+*   of track layout changes and static notification function called whenever
+*   the track layout changes. It is implemented as base class because it needs
+*   to be shared between all rail YAPF types (one shared counter, one notification
+*   function. */
 struct CSegmentCostCacheBase
 {
 	static int   s_rail_change_counter;
@@ -76,7 +89,6 @@
     of the segment (origin tile and exit-dir from this tile).
     Different CYapfCachedCostT types can share the same type of CSegmentCostCacheT.
     Look at CYapfRailSegment (yapf_node_rail.hpp) for the segment example */
-
 template <class Tsegment>
 struct CSegmentCostCacheT
 	: public CSegmentCostCacheBase
@@ -116,7 +128,7 @@
 {
 public:
 	typedef CYapfSegmentCostCacheLocalT<Types> Tlocal;
-	typedef typename Types::Tpf Tpf;
+	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
 	typedef typename Node::Key Key;    ///< key to hash tables
 	typedef typename Node::CachedData CachedData;
@@ -128,6 +140,7 @@
 
 	FORCEINLINE CYapfSegmentCostCacheGlobalT() : m_global_cache(stGetGlobalCache()) {};
 
+	/// to access inherited path finder
 	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
 
 	FORCEINLINE static Cache*& stGlobalCachePtr() {static Cache* pC = NULL; return pC;}
@@ -159,6 +172,8 @@
 	}
 
 public:
+	/** Called by YAPF to attach cached or local segment cost data to the given node.
+	*   @return true if globally cached data were used or false if local data was used */
 	FORCEINLINE bool PfNodeCacheFetch(Node& n)
 	{
 		if (!Yapf().CanUseGlobalCache(n)) {
@@ -171,6 +186,8 @@
 		return found;
 	};
 
+	/** Called by YAPF to flush the cached segment cost data back into cache storage.
+	*   Current cache implementation doesn't use that. */
 	FORCEINLINE void PfNodeCacheFlush(Node& n)
 	{
 	};
--- a/yapf/yapf_costrail.hpp
+++ b/yapf/yapf_costrail.hpp
@@ -10,10 +10,10 @@
 	, public CostRailSettings
 {
 public:
-	typedef typename Types::Tpf Tpf;
+	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
 	typedef typename Types::TrackFollower TrackFollower;
 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
-	typedef typename Node::Key Key;    ///< key to hash tables
+	typedef typename Node::Key Key;               ///< key to hash tables
 	typedef typename Node::CachedData CachedData;
 
 protected:
@@ -33,6 +33,7 @@
 			pen[i] = p0 + i * (p1 + i * p2);
 	}
 
+	/// to access inherited path finder
 	Tpf& Yapf() {return *static_cast<Tpf*>(this);}
 
 public:
@@ -140,6 +141,9 @@
 public:
 	FORCEINLINE void SetMaxCost(int max_cost) {m_max_cost = max_cost;}
 
+	/** Called by YAPF to calculate the cost from the origin to the given node.
+	*   Calculates only the cost of given node, adds it to the parent node cost
+	*   and stores the result into Node::m_cost member */
 	FORCEINLINE bool PfCalcCost(Node& n)
 	{
 		assert(!n.flags_u.flags_s.m_targed_seen);
--- a/yapf/yapf_destrail.hpp
+++ b/yapf/yapf_destrail.hpp
@@ -25,18 +25,22 @@
 	: public CYapfDestinationRailBase
 {
 public:
-	typedef typename Types::Tpf Tpf;
+	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
-	typedef typename Node::Key Key;    ///< key to hash tables
+	typedef typename Node::Key Key;               ///< key to hash tables
 
+	/// to access inherited path finder
 	Tpf& Yapf() {return *static_cast<Tpf*>(this);}
 
+	/// Called by YAPF to detect if node ends in the desired destination
 	FORCEINLINE bool PfDetectDestination(Node& n)
 	{
 		bool bDest = IsTileDepotType(n.GetLastTile(), TRANSPORT_RAIL);
 		return bDest;
 	}
 
+	/** Called by YAPF to calculate cost estimate. Calculates distance to the destination
+	*   adds it to the actual cost from origin and stores the sum to the Node::m_estimate */
 	FORCEINLINE bool PfCalcEstimate(Node& n)
 	{
 		n.m_estimate = n.m_cost;
@@ -49,15 +53,16 @@
 	: public CYapfDestinationRailBase
 {
 public:
-	typedef typename Types::Tpf Tpf;
+	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
-	typedef typename Node::Key Key;    ///< key to hash tables
+	typedef typename Node::Key Key;               ///< key to hash tables
 
 protected:
 	TileIndex    m_destTile;
 	TrackdirBits m_destTrackdirs;
 	StationID    m_dest_station_id;
 
+	/// to access inherited path finder
 	Tpf& Yapf() {return *static_cast<Tpf*>(this);}
 
 	static TileIndex CalcStationCenterTile(StationID station)
@@ -85,6 +90,7 @@
 		CYapfDestinationRailBase::SetDestination(v);
 	}
 
+	/// Called by YAPF to detect if node ends in the desired destination
 	FORCEINLINE bool PfDetectDestination(Node& n)
 	{
 		bool bDest;
@@ -99,6 +105,8 @@
 		return bDest;
 	}
 
+	/** Called by YAPF to calculate cost estimate. Calculates distance to the destination
+	*   adds it to the actual cost from origin and stores the sum to the Node::m_estimate */
 	FORCEINLINE bool PfCalcEstimate(Node& n)
 	{
 		static int dg_dir_to_x_offs[] = {-1, 0, 1, 0};
--- a/yapf/yapf_rail.cpp
+++ b/yapf/yapf_rail.cpp
@@ -17,15 +17,19 @@
 class CYapfFollowAnyDepotRailT
 {
 public:
-	typedef typename Types::Tpf Tpf;
+	typedef typename Types::Tpf Tpf;                     ///< the pathfinder class (derived from THIS class)
 	typedef typename Types::TrackFollower TrackFollower;
-	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
-	typedef typename Node::Key Key;    ///< key to hash tables
+	typedef typename Types::NodeList::Titem Node;        ///< this will be our node type
+	typedef typename Node::Key Key;                      ///< key to hash tables
 
 protected:
+	/// to access inherited path finder
 	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
 
 public:
+	/** Called by YAPF to move from the given node to the next tile. For each
+	*   reachable trackdir on the new tile creates new node, initializes it
+	*   and adds it to the open list by calling Yapf().AddNewNode(n) */
 	inline void PfFollowNode(Node& old_node)
 	{
 		TrackFollower F(Yapf().GetVehicle());
@@ -33,6 +37,7 @@
 			Yapf().AddMultipleNodes(&old_node, F.m_new_tile, F.m_new_td_bits);
 	}
 
+	/// return debug report character to identify the transportation type
 	FORCEINLINE char TransportTypeChar() const {return 't';}
 
 	static bool stFindNearestDepotTwoWay(Vehicle *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_distance, int reverse_penalty, TileIndex* depot_tile, bool* reversed)
@@ -75,15 +80,19 @@
 class CYapfFollowRailT
 {
 public:
-	typedef typename Types::Tpf Tpf;
+	typedef typename Types::Tpf Tpf;                     ///< the pathfinder class (derived from THIS class)
 	typedef typename Types::TrackFollower TrackFollower;
-	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
-	typedef typename Node::Key Key;    ///< key to hash tables
+	typedef typename Types::NodeList::Titem Node;        ///< this will be our node type
+	typedef typename Node::Key Key;                      ///< key to hash tables
 
 protected:
+	/// to access inherited path finder
 	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
 
 public:
+	/** Called by YAPF to move from the given node to the next tile. For each
+	*   reachable trackdir on the new tile creates new node, initializes it
+	*   and adds it to the open list by calling Yapf().AddNewNode(n) */
 	inline void PfFollowNode(Node& old_node)
 	{
 		TrackFollower F(Yapf().GetVehicle());
@@ -91,6 +100,7 @@
 			Yapf().AddMultipleNodes(&old_node, F.m_new_tile, F.m_new_td_bits);
 	}
 
+	/// return debug report character to identify the transportation type
 	FORCEINLINE char TransportTypeChar() const {return 't';}
 
 	static Trackdir stChooseRailTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackdirBits trackdirs)
--- a/yapf/yapf_road.cpp
+++ b/yapf/yapf_road.cpp
@@ -10,12 +10,13 @@
 class CYapfCostRoadT
 {
 public:
-	typedef typename Types::Tpf Tpf;
-	typedef typename Types::TrackFollower TrackFollower;
+	typedef typename Types::Tpf Tpf; ///< pathfinder (derived from THIS class)
+	typedef typename Types::TrackFollower TrackFollower; ///< track follower helper
 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
 	typedef typename Node::Key Key;    ///< key to hash tables
 
 protected:
+	/// to access inherited path finder
 	Tpf& Yapf() {return *static_cast<Tpf*>(this);}
 
 	int SlopeCost(TileIndex tile, TileIndex next_tile, Trackdir trackdir)
@@ -62,6 +63,9 @@
 	}
 
 public:
+	/** Called by YAPF to calculate the cost from the origin to the given node.
+	*   Calculates only the cost of given node, adds it to the parent node cost
+	*   and stores the result into Node::m_cost member */
 	FORCEINLINE bool PfCalcCost(Node& n)
 	{
 		int segment_cost = 0;
@@ -118,19 +122,23 @@
 class CYapfDestinationAnyDepotRoadT
 {
 public:
-	typedef typename Types::Tpf Tpf;
+	typedef typename Types::Tpf Tpf;                     ///< the pathfinder class (derived from THIS class)
 	typedef typename Types::TrackFollower TrackFollower;
-	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
-	typedef typename Node::Key Key;    ///< key to hash tables
+	typedef typename Types::NodeList::Titem Node;        ///< this will be our node type
+	typedef typename Node::Key Key;                      ///< key to hash tables
 
+	/// to access inherited path finder
 	Tpf& Yapf() {return *static_cast<Tpf*>(this);}
 
+	/// Called by YAPF to detect if node ends in the desired destination
 	FORCEINLINE bool PfDetectDestination(Node& n)
 	{
 		bool bDest = IsTileDepotType(n.m_segment_last_tile, TRANSPORT_ROAD);
 		return bDest;
 	}
 
+	/** Called by YAPF to calculate cost estimate. Calculates distance to the destination
+	*   adds it to the actual cost from origin and stores the sum to the Node::m_estimate */
 	FORCEINLINE bool PfCalcEstimate(Node& n)
 	{
 		n.m_estimate = n.m_cost;
@@ -143,10 +151,10 @@
 class CYapfDestinationTileRoadT
 {
 public:
-	typedef typename Types::Tpf Tpf;
+	typedef typename Types::Tpf Tpf;                     ///< the pathfinder class (derived from THIS class)
 	typedef typename Types::TrackFollower TrackFollower;
-	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
-	typedef typename Node::Key Key;    ///< key to hash tables
+	typedef typename Types::NodeList::Titem Node;        ///< this will be our node type
+	typedef typename Node::Key Key;                      ///< key to hash tables
 
 protected:
 	TileIndex    m_destTile;
@@ -160,15 +168,19 @@
 	}
 
 protected:
+	/// to access inherited path finder
 	Tpf& Yapf() {return *static_cast<Tpf*>(this);}
 
 public:
+	/// Called by YAPF to detect if node ends in the desired destination
 	FORCEINLINE bool PfDetectDestination(Node& n)
 	{
 		bool bDest = (n.m_segment_last_tile == m_destTile) && ((m_destTrackdirs & TrackdirToTrackdirBits(n.m_segment_last_td)) != TRACKDIR_BIT_NONE);
 		return bDest;
 	}
 
+	/** Called by YAPF to calculate cost estimate. Calculates distance to the destination
+	*   adds it to the actual cost from origin and stores the sum to the Node::m_estimate */
 	inline bool PfCalcEstimate(Node& n)
 	{
 		static int dg_dir_to_x_offs[] = {-1, 0, 1, 0};
@@ -201,16 +213,20 @@
 class CYapfFollowRoadT
 {
 public:
-	typedef typename Types::Tpf Tpf;
+	typedef typename Types::Tpf Tpf;                     ///< the pathfinder class (derived from THIS class)
 	typedef typename Types::TrackFollower TrackFollower;
-	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
-	typedef typename Node::Key Key;    ///< key to hash tables
+	typedef typename Types::NodeList::Titem Node;        ///< this will be our node type
+	typedef typename Node::Key Key;                      ///< key to hash tables
 
 protected:
+	/// to access inherited path finder
 	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
 
 public:
 
+	/** Called by YAPF to move from the given node to the next tile. For each
+	*   reachable trackdir on the new tile creates new node, initializes it
+	*   and adds it to the open list by calling Yapf().AddNewNode(n) */
 	inline void PfFollowNode(Node& old_node)
 	{
 		TrackFollower F;
@@ -218,6 +234,7 @@
 			Yapf().AddMultipleNodes(&old_node, F.m_new_tile, F.m_new_td_bits);
 	}
 
+	/// return debug report character to identify the transportation type
 	FORCEINLINE char TransportTypeChar() const {return 'r';}
 
 	static Trackdir stChooseRoadTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir)
--- a/yapf/yapf_ship.cpp
+++ b/yapf/yapf_ship.cpp
@@ -9,15 +9,19 @@
 class CYapfFollowShipT
 {
 public:
-	typedef typename Types::Tpf Tpf;
+	typedef typename Types::Tpf Tpf;                     ///< the pathfinder class (derived from THIS class)
 	typedef typename Types::TrackFollower TrackFollower;
-	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
-	typedef typename Node::Key Key;    ///< key to hash tables
+	typedef typename Types::NodeList::Titem Node;        ///< this will be our node type
+	typedef typename Node::Key Key;                      ///< key to hash tables
 
 protected:
+	/// to access inherited path finder
 	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
 
 public:
+	/** Called by YAPF to move from the given node to the next tile. For each
+	*   reachable trackdir on the new tile creates new node, initializes it
+	*   and adds it to the open list by calling Yapf().AddNewNode(n) */
 	inline void PfFollowNode(Node& old_node)
 	{
 		TrackFollower F;
@@ -25,6 +29,7 @@
 			Yapf().AddMultipleNodes(&old_node, F.m_new_tile, F.m_new_td_bits);
 	}
 
+	/// return debug report character to identify the transportation type
 	FORCEINLINE char TransportTypeChar() const {return 'w';}
 
 	static Trackdir ChooseShipTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
@@ -80,14 +85,18 @@
 class CYapfCostShipT
 {
 public:
-	typedef typename Types::Tpf Tpf;
+	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
-	typedef typename Node::Key Key;    ///< key to hash tables
+	typedef typename Node::Key Key;               ///< key to hash tables
 
 protected:
+	/// to access inherited path finder
 	Tpf& Yapf() {return *static_cast<Tpf*>(this);}
 
 public:
+	/** Called by YAPF to calculate the cost from the origin to the given node.
+	*   Calculates only the cost of given node, adds it to the parent node cost
+	*   and stores the result into Node::m_cost member */
 	FORCEINLINE bool PfCalcCost(Node& n)
 	{
 		// base tile cost depending on distance