OpenTTD Source
12.0-beta2
|
Go to the documentation of this file.
13 #include "../../debug.h"
14 #include "../../settings_type.h"
46 template <
class Types>
49 typedef typename Types::Tpf
Tpf;
50 typedef typename Types::TrackFollower TrackFollower;
53 typedef typename NodeList::Titem
Node;
54 typedef typename Node::Key
Key;
92 return *
static_cast<Tpf *
>(
this);
115 Yapf().PfSetStartupNodes();
116 bool bDestFound =
true;
130 Yapf().PfFollowNode(*n);
132 m_nodes.PopOpenNode(n->GetKey());
142 if (_debug_yapf_level >= 3) {
144 char ttc =
Yapf().TransportTypeChar();
149 Debug(yapf, 3,
"[YAPF{}]{}{:4d} - {} rounds - {} open - {} closed - CHR {:4.1f}% - C {} D {}",
150 ttc, bDestFound ?
'-' :
'!', veh_idx,
m_num_steps,
m_nodes.OpenCount(),
m_nodes.ClosedCount(), cache_hit_ratio, cost, dist
179 Yapf().PfNodeCacheFetch(n);
181 if (
m_nodes.FindOpenNode(n.m_key) ==
nullptr) {
197 n.Set(parent, tf.m_new_tile, td, is_choice);
198 Yapf().AddNewNode(n, tf);
212 bool intermediate_on_branch =
false;
213 while (n !=
nullptr && (n->m_segment->m_end_segment_reason & ESRB_CHOICE_FOLLOWS) == 0) {
217 if (intermediate_on_branch)
Yapf().m_pBestIntermediateNode = n;
227 bool bCached =
Yapf().PfNodeCacheFetch(n);
234 bool bValid =
Yapf().PfCalcCost(n, &tf);
237 Yapf().PfNodeCacheFlush(n);
240 if (bValid) bValid =
Yapf().PfCalcEstimate(n);
246 bool bDestination =
Yapf().PfDetectDestination(n);
261 if (openNode !=
nullptr) {
264 if (n.GetCostEstimate() < openNode->GetCostEstimate()) {
266 m_nodes.PopOpenNode(n.GetKey());
269 m_nodes.InsertOpenNode(*openNode);
276 Node *closedNode =
m_nodes.FindClosedNode(n.GetKey());
277 if (closedNode !=
nullptr) {
280 int node_est = n.GetCostEstimate();
281 int closed_est = closedNode->GetCostEstimate();
282 if (node_est < closed_est) {
314 inline void PfSetStartupNodes()
317 Node &n1 = *base::m_nodes.CreateNewNode();
321 base::m_nodes.InsertOpenNode(n1);
325 inline void PfFollowNode(
Node &org)
327 for (each follower of node org) {
328 Node &n = *base::m_nodes.CreateNewNode();
338 inline bool PfCalcCost(
Node &n)
343 n.m_cost = n.m_parent->m_cost + cost;
348 inline bool PfCalcEstimate(
Node &n)
353 n.m_estimate = n.m_cost + distance;
358 inline bool PfDetectDestination(
Node &n)
360 bool bDest = (n.m_key.m_x == m_x2) && (n.m_key.m_y == m_y2);
const YAPFSettings & PfGetSettings() const
return current settings (can be custom - company based - but later)
TrackdirBits
Enumeration of bitmasks for the TrackDirs.
uint16 UnitID
Type for the company global vehicle unit number.
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
static T KillFirstBit(T value)
Clear the first bit in an integer.
void PruneIntermediateNodeBranch(Node *n)
In some cases an intermediate node branch should be pruned.
Settings related to the yet another pathfinder.
int m_stats_cache_hits
stats - how many node's costs were reused from cache
void AddMultipleNodes(Node *parent, const TrackFollower &tf)
add multiple nodes - direct children of the given node
const YAPFSettings * m_settings
current settings (_settings_game.yapf)
CYapfBaseT()
default constructor
static uint8 FindFirstBit2x64(const int value)
Finds the position of the first non-zero bit in an integer.
Tpf & Yapf()
to access inherited path finder
Class that represents the dump-into-string target.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
NodeList m_nodes
node list multi-container
int m_num_steps
this is there for debugging purposes (hope it doesn't hurt)
Types::VehicleType VehicleType
the type of vehicle
VehicleType
Available vehicle types.
Node * GetBestNode()
If path was found return the best node that has reached the destination.
Node::Key Key
key to hash tables
void AddNewNode(Node &n, const TrackFollower &tf)
AddNewNode() - called by Tderived::PfFollowNode() for each child node.
void WriteStructT(const char *name, const S *s)
Dump nested object (or only its name if this instance is already known).
int m_max_search_nodes
maximum number of nodes we are allowed to visit before we give up
@ TRACKDIR_BIT_NONE
No track build.
Node * m_pBestDestNode
pointer to the destination node found at last round
void AddStartupNode(Node &n)
Add new node (created by CreateNewNode and filled with data) into open list.
int m_stats_cost_calcs
stats - how many node's costs were calculated
void WriteValue(const char *name, int value)
Write 'name = value' with indent and new-line.
bool FindPath(const VehicleType *v)
Main pathfinder routine:
Node & CreateNewNode()
Calls NodeList::CreateNewNode() - allocates new node that can be filled and used as argument for AddS...
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Types::NodeList NodeList
our node list
~CYapfBaseT()
default destructor
Node * m_pBestIntermediateNode
here should be node closest to the destination if path not found
const VehicleType * m_veh
vehicle that we are trying to drive
Trackdir
Enumeration for tracks and directions.
CYapfBaseT - A-star type path finder base class.
NodeList::Titem Node
this will be our node type