13 #include "../../debug.h" 14 #include "../../settings_type.h" 16 extern int _total_pf_time_us;
48 template <
class Types>
51 typedef typename Types::Tpf
Tpf;
52 typedef typename Types::TrackFollower TrackFollower;
55 typedef typename NodeList::Titem
Node;
56 typedef typename Node::Key
Key;
82 : m_pBestDestNode(nullptr)
83 , m_pBestIntermediateNode(nullptr)
87 , m_stats_cost_calcs(0)
88 , m_stats_cache_hits(0)
100 return *
static_cast<Tpf *
>(
this);
126 Yapf().PfSetStartupNodes();
127 bool bDestFound =
true;
131 Node *n = m_nodes.GetBestOpenNode();
137 if (m_pBestDestNode !=
nullptr && m_pBestDestNode->GetCost() < n->GetCostEstimate()) {
141 Yapf().PfFollowNode(*n);
143 m_nodes.PopOpenNode(n->GetKey());
144 m_nodes.InsertClosedNode(*n);
151 bDestFound &= (m_pBestDestNode !=
nullptr);
154 if (_debug_yapf_level >= 2) {
155 int t = perf.Get(1000000);
156 _total_pf_time_us += t;
158 if (_debug_yapf_level >= 3) {
159 UnitID veh_idx = (m_veh !=
nullptr) ? m_veh->unitnumber : 0;
160 char ttc =
Yapf().TransportTypeChar();
161 float cache_hit_ratio = (m_stats_cache_hits == 0) ? 0.0f : ((
float)m_stats_cache_hits / (float)(m_stats_cache_hits + m_stats_cost_calcs) * 100.0f);
162 int cost = bDestFound ? m_pBestDestNode->m_cost : -1;
163 int dist = bDestFound ? m_pBestDestNode->m_estimate - m_pBestDestNode->m_cost : -1;
165 DEBUG(yapf, 3,
"[YAPF%c]%c%4d- %d us - %d rounds - %d open - %d closed - CHR %4.1f%% - C %d D %d - c%d(sc%d, ts%d, o%d) -- ",
166 ttc, bDestFound ?
'-' :
'!', veh_idx, t, m_num_steps, m_nodes.OpenCount(), m_nodes.ClosedCount(),
167 cache_hit_ratio, cost, dist, m_perf_cost.Get(1000000), m_perf_slope_cost.Get(1000000),
168 m_perf_ts_cost.Get(1000000), m_perf_other_cost.Get(1000000)
190 Node &node = *m_nodes.CreateNewNode();
197 Yapf().PfNodeCacheFetch(n);
199 if (m_nodes.FindOpenNode(n.m_key) ==
nullptr) {
200 m_nodes.InsertOpenNode(n);
214 Node &n =
Yapf().CreateNewNode();
215 n.Set(parent, tf.m_new_tile, td, is_choice);
216 Yapf().AddNewNode(n, tf);
230 while (
Yapf().m_pBestIntermediateNode !=
nullptr && (
Yapf().m_pBestIntermediateNode->m_segment->m_end_segment_reason & ESRB_CHOICE_FOLLOWS) == 0) {
231 Yapf().m_pBestIntermediateNode =
Yapf().m_pBestIntermediateNode->m_parent;
242 bool bCached =
Yapf().PfNodeCacheFetch(n);
244 m_stats_cost_calcs++;
246 m_stats_cache_hits++;
249 bool bValid =
Yapf().PfCalcCost(n, &tf);
252 Yapf().PfNodeCacheFlush(n);
255 if (bValid) bValid =
Yapf().PfCalcEstimate(n);
261 bool bDestination =
Yapf().PfDetectDestination(n);
263 if (m_pBestDestNode ==
nullptr || n < *m_pBestDestNode) {
264 m_pBestDestNode = &n;
266 m_nodes.FoundBestNode(n);
270 if (m_max_search_nodes > 0 && (m_pBestIntermediateNode ==
nullptr || (m_pBestIntermediateNode->GetCostEstimate() - m_pBestIntermediateNode->GetCost()) > (n.GetCostEstimate() - n.GetCost()))) {
271 m_pBestIntermediateNode = &n;
275 Node *openNode = m_nodes.FindOpenNode(n.GetKey());
276 if (openNode !=
nullptr) {
279 if (n.GetCostEstimate() < openNode->GetCostEstimate()) {
281 m_nodes.PopOpenNode(n.GetKey());
284 m_nodes.InsertOpenNode(*openNode);
290 Node *closedNode = m_nodes.FindClosedNode(n.GetKey());
291 if (closedNode !=
nullptr) {
294 int node_est = n.GetCostEstimate();
295 int closed_est = closedNode->GetCostEstimate();
296 if (node_est < closed_est) {
309 m_nodes.InsertOpenNode(n);
312 const VehicleType * GetVehicle()
const 320 dmp.
WriteLine(
"m_num_steps = %d", m_num_steps);
327 inline void PfSetStartupNodes()
330 Node &n1 = *base::m_nodes.CreateNewNode();
334 base::m_nodes.InsertOpenNode(n1);
338 inline void PfFollowNode(Node &org)
340 for (each follower of node org) {
341 Node &n = *base::m_nodes.CreateNewNode();
351 inline bool PfCalcCost(Node &n)
356 n.m_cost = n.m_parent->m_cost + cost;
361 inline bool PfCalcEstimate(Node &n)
366 n.m_estimate = n.m_cost + distance;
371 inline bool PfDetectDestination(Node &n)
373 bool bDest = (n.m_key.m_x == m_x2) && (n.m_key.m_y == m_y2);
CPerformanceTimer m_perf_other_cost
stats - other CPU time
GameSettings _settings_game
Game settings of a running game or the scenario editor.
void AddNewNode(Node &n, const TrackFollower &tf)
AddNewNode() - called by Tderived::PfFollowNode() for each child node.
Types::VehicleType VehicleType
the type of vehicle
TrackdirBits
Enumeration of bitmasks for the TrackDirs.
Node::Key Key
key to hash tables
Node * GetBestNode()
If path was found return the best node that has reached the destination.
Trackdir
Enumeration for tracks and directions.
int m_max_search_nodes
maximum number of nodes we are allowed to visit before we give up
VehicleType
Available vehicle types.
int m_stats_cost_calcs
stats - how many node's costs were calculated
void CDECL WriteLine(const char *format,...) WARN_FORMAT(2
Write a line with indent at the beginning and <LF> at the end.
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.
Settings related to the yet another pathfinder.
Node & CreateNewNode()
Calls NodeList::CreateNewNode() - allocates new node that can be filled and used as argument for AddS...
Types::NodeList NodeList
our node list
Node * m_pBestIntermediateNode
here should be node closest to the destination if path not found
bool FindPath(const VehicleType *v)
Main pathfinder routine:
const VehicleType * m_veh
vehicle that we are trying to drive
~CYapfBaseT()
default destructor
CYapfBaseT - A-star type path finder base class.
NodeList::Titem Node
this will be our node type
CPerformanceTimer m_perf_ts_cost
stats - GetTrackStatus() CPU time
CPerformanceTimer m_perf_cost
stats - total CPU time of this run
const YAPFSettings & PfGetSettings() const
return current settings (can be custom - company based - but later)
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
#define DEBUG(name, level,...)
Output a line of debugging information.
void WriteStructT(const char *name, const S *s)
Dump nested object (or only its name if this instance is already known).
static T KillFirstBit(T value)
Clear the first bit in an integer.
static uint8 FindFirstBit2x64(const int value)
Finds the position of the first non-zero bit in an integer.
int m_stats_cache_hits
stats - how many node's costs were reused from cache
uint16 UnitID
Type for the company global vehicle unit number.
void AddMultipleNodes(Node *parent, const TrackFollower &tf)
add multiple nodes - direct children of the given node
CPerformanceTimer m_perf_slope_cost
stats - slope calculation CPU time
int m_num_steps
this is there for debugging purposes (hope it doesn't hurt)
NodeList m_nodes
node list multi-container
const YAPFSettings * m_settings
current settings (_settings_game.yapf)
Class that represents the dump-into-string target.
CYapfBaseT()
default constructor
void PruneIntermediateNodeBranch()
In some cases an intermediate node branch should be pruned.
Tpf & Yapf()
to access inherited path finder