OpenTTD Source  20241120-master-g6d3adc6169
CYapfBaseT< Types > Class Template Reference

CYapfBaseT - A-star type path finder base class. More...

#include <yapf_base.hpp>

Public Types

typedef Types::Tpf Tpf
 the pathfinder class (derived from THIS class)
 
typedef Types::TrackFollower TrackFollower
 
typedef Types::NodeList NodeList
 our node list
 
typedef Types::VehicleType VehicleType
 the type of vehicle
 
typedef NodeList::Item Node
 this will be our node type
 
typedef Node::Key Key
 key to hash tables
 

Public Member Functions

 CYapfBaseT ()
 default constructor
 
 ~CYapfBaseT ()
 default destructor
 
const YAPFSettingsPfGetSettings () const
 return current settings (can be custom - company based - but later)
 
bool FindPath (const VehicleType *v)
 Main pathfinder routine: More...
 
NodeGetBestNode ()
 If path was found return the best node that has reached the destination. More...
 
NodeCreateNewNode ()
 Calls NodeList::CreateNewNode() - allocates new node that can be filled and used as argument for AddStartupNode() or AddNewNode()
 
void AddStartupNode (Node &n)
 Add new node (created by CreateNewNode and filled with data) into open list.
 
void AddMultipleNodes (Node *parent, const TrackFollower &tf)
 add multiple nodes - direct children of the given node
 
void PruneIntermediateNodeBranch (Node *n)
 In some cases an intermediate node branch should be pruned. More...
 
void AddNewNode (Node &n, const TrackFollower &tf)
 AddNewNode() - called by Tderived::PfFollowNode() for each child node. More...
 
const VehicleTypeGetVehicle () const
 
void DumpBase (DumpTarget &dmp) const
 

Data Fields

NodeList nodes
 node list multi-container
 
int num_steps = 0
 this is there for debugging purposes (hope it doesn't hurt)
 

Protected Member Functions

TpfYapf ()
 to access inherited path finder
 

Protected Attributes

Nodebest_dest_node = nullptr
 pointer to the destination node found at last round
 
Nodebest_intermediate_node = nullptr
 here should be node closest to the destination if path not found
 
const YAPFSettingssettings
 current settings (_settings_game.yapf)
 
int max_search_nodes
 maximum number of nodes we are allowed to visit before we give up
 
const VehicleTypevehicle = nullptr
 vehicle that we are trying to drive
 
int stats_cost_calcs = 0
 stats - how many node's costs were calculated
 
int stats_cache_hits = 0
 stats - how many node's costs were reused from cache
 

Detailed Description

template<class Types>
class CYapfBaseT< Types >

CYapfBaseT - A-star type path finder base class.

Derive your own pathfinder from it. You must provide the following template argument: Types - used as collection of local types used in pathfinder

Requirements for the Types struct:

The following types must be defined in the 'Types' argument:

  • Types::Tpf - your pathfinder derived from CYapfBaseT
  • Types::NodeList - open/closed node list (look at CNodeList_HashTableT) NodeList needs to have defined local type Titem - defines the pathfinder node type. Node needs to define local type Key - the node key in the collection ()

For node list you can use template class NodeList, for which you need to declare only your node type. Look at test_yapf.h for an example.

Requirements to your pathfinder class derived from CYapfBaseT:

Your pathfinder derived class needs to implement following methods: inline void PfSetStartupNodes() inline void PfFollowNode(Node &org) inline bool PfCalcCost(Node &n) inline bool PfCalcEstimate(Node &n) inline bool PfDetectDestination(Node &n)

For more details about those methods, look at the end of CYapfBaseT declaration. There are some examples. For another example look at test_yapf.h (part or unittest project).

Definition at line 49 of file yapf_base.hpp.

Member Function Documentation

◆ AddNewNode()

template<class Types >
void CYapfBaseT< Types >::AddNewNode ( Node n,
const TrackFollower &  tf 
)
inline

◆ FindPath()

template<class Types >
bool CYapfBaseT< Types >::FindPath ( const VehicleType v)
inline

Main pathfinder routine:

  • set startup node(s)
  • main loop that stops if:
    • the destination was found
    • or the open list is empty (no route to destination).
    • or the maximum amount of loops reached - max_search_nodes (default = 10000)
      Returns
      true if the path was found

Definition at line 103 of file yapf_base.hpp.

References NodeList< Titem, Thash_bits_open, Thash_bits_closed >::ClosedCount(), NodeList< Titem, Thash_bits_open, Thash_bits_closed >::GetBestOpenNode(), NodeList< Titem, Thash_bits_open, Thash_bits_closed >::InsertClosedNode(), NodeList< Titem, Thash_bits_open, Thash_bits_closed >::PopOpenNode(), and CYapfBaseT< Types >::Yapf().

◆ GetBestNode()

template<class Types >
Node* CYapfBaseT< Types >::GetBestNode ( )
inline

If path was found return the best node that has reached the destination.

Otherwise return the best visited node (which was nearest to the destination).

Definition at line 147 of file yapf_base.hpp.

References CYapfBaseT< Types >::best_dest_node, and CYapfBaseT< Types >::best_intermediate_node.

◆ PruneIntermediateNodeBranch()

template<class Types >
void CYapfBaseT< Types >::PruneIntermediateNodeBranch ( Node n)
inline

In some cases an intermediate node branch should be pruned.

The most prominent case is when a red EOL signal is encountered, but there was a segment change (e.g. a rail type change) before that. If the branch would not be pruned, the rail type change location would remain the best intermediate node, and thus the vehicle would still go towards the red EOL signal.

Definition at line 196 of file yapf_base.hpp.


The documentation for this class was generated from the following file: