13 #include "../../debug.h"
14 #include "../../settings_type.h"
15 #include "../../misc/dbg_helpers.h"
48 template <
class Types>
51 typedef typename Types::Tpf
Tpf;
52 typedef typename Types::TrackFollower TrackFollower;
55 typedef typename NodeList::Item
Node;
56 typedef typename Node::Key
Key;
84 return *
static_cast<Tpf *
>(
this);
107 Yapf().PfSetStartupNodes();
112 if (best_open_node ==
nullptr)
break;
114 if (
Yapf().PfDetectDestination(*best_open_node)) {
115 this->best_dest_node = best_open_node;
119 Yapf().PfFollowNode(*best_open_node);
120 if (this->max_search_nodes != 0 && this->nodes.
ClosedCount() >= this->max_search_nodes)
break;
126 const bool destination_found = (this->best_dest_node !=
nullptr);
128 if (_debug_yapf_level >= 3) {
129 const UnitID veh_idx = (this->vehicle !=
nullptr) ? this->vehicle->unitnumber : 0;
130 const char ttc =
Yapf().TransportTypeChar();
131 const float cache_hit_ratio = (this->stats_cache_hits == 0) ? 0.0f : ((
float)this->stats_cache_hits / (float)(this->stats_cache_hits + this->stats_cost_calcs) * 100.0f);
132 const int cost = destination_found ? this->best_dest_node->cost : -1;
133 const int dist = destination_found ? this->best_dest_node->estimate - this->best_dest_node->cost : -1;
135 Debug(yapf, 3,
"[YAPF{}]{}{:4d} - {} rounds - {} open - {} closed - CHR {:4.1f}% - C {} D {}",
136 ttc, destination_found ?
'-' :
'!', veh_idx, this->num_steps, this->nodes.
OpenCount(), this->nodes.ClosedCount(), cache_hit_ratio, cost, dist
140 return destination_found;
165 Yapf().PfNodeCacheFetch(n);
183 n.Set(parent, tf.new_tile, td, is_choice);
184 Yapf().AddNewNode(n, tf);
198 bool intermediate_on_branch =
false;
199 while (n !=
nullptr && (n->segment->end_segment_reason & ESRB_CHOICE_FOLLOWS) == 0) {
203 if (intermediate_on_branch)
Yapf().best_intermediate_node = n;
213 bool cached =
Yapf().PfNodeCacheFetch(n);
215 this->stats_cost_calcs++;
217 this->stats_cache_hits++;
229 bool set_intermediate = this->max_search_nodes > 0 && (this->best_intermediate_node ==
nullptr || (this->best_intermediate_node->GetCostEstimate() - this->best_intermediate_node->GetCost()) > (n.GetCostEstimate() - n.GetCost()));
233 if (open_node !=
nullptr) {
236 if (n.GetCostEstimate() < open_node->GetCostEstimate()) {
242 if (set_intermediate) this->best_intermediate_node = open_node;
249 if (closed_node !=
nullptr) {
252 int node_est = n.GetCostEstimate();
253 int closed_est = closed_node->GetCostEstimate();
254 if (node_est < closed_est) {
268 if (set_intermediate) this->best_intermediate_node = &n;
constexpr uint8_t FindFirstBit(T x)
Search the first set bit in a value.
constexpr T KillFirstBit(T value)
Clear the first bit in an integer.
CYapfBaseT - A-star type path finder base class.
int num_steps
this is there for debugging purposes (hope it doesn't hurt)
void PruneIntermediateNodeBranch(Node *n)
In some cases an intermediate node branch should be pruned.
Node::Key Key
key to hash tables
CYapfBaseT()
default constructor
Node * GetBestNode()
If path was found return the best node that has reached the destination.
void AddNewNode(Node &n, const TrackFollower &tf)
AddNewNode() - called by Tderived::PfFollowNode() for each child node.
const VehicleType * vehicle
vehicle that we are trying to drive
Node & CreateNewNode()
Calls NodeList::CreateNewNode() - allocates new node that can be filled and used as argument for AddS...
const YAPFSettings & PfGetSettings() const
return current settings (can be custom - company based - but later)
Node * best_intermediate_node
here should be node closest to the destination if path not found
Tpf & Yapf()
to access inherited path finder
const YAPFSettings * settings
current settings (_settings_game.yapf)
int 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
int stats_cost_calcs
stats - how many node's costs were calculated
int max_search_nodes
maximum number of nodes we are allowed to visit before we give up
~CYapfBaseT()
default destructor
bool FindPath(const VehicleType *v)
Main pathfinder routine:
NodeList nodes
node list multi-container
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Types::VehicleType VehicleType
the type of vehicle
void AddStartupNode(Node &n)
Add new node (created by CreateNewNode and filled with data) into open list.
Node * best_dest_node
pointer to the destination node found at last round
NodeList::Item Node
this will be our node type
Types::NodeList NodeList
our node list
Hash table based node list multi-container class.
void InsertClosedNode(Titem &item)
close node
Titem * FindClosedNode(const Key &key)
return the closed node specified by a key or nullptr if not found
Titem * FindOpenNode(const Key &key)
return the open node specified by a key or nullptr if not found
Titem & CreateNewNode()
allocate new data item from items
void InsertOpenNode(Titem &item)
insert given item as open node (into m_open and m_open_queue)
int OpenCount()
return number of open nodes
int ClosedCount()
return number of closed nodes
Titem * GetBestOpenNode()
return the best open node
Titem & PopOpenNode(const Key &key)
remove and return the open node specified by a key
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
uint8_t valid
Bits indicating what variable is valid (for each bit, 0 is invalid, 1 is valid).
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Class that represents the dump-into-string target.
void WriteValue(const std::string &name, int value)
Write 'name = value' with indent and new-line.
void WriteStructT(const std::string &name, const S *s)
Dump nested object (or only its name if this instance is already known).
Settings related to the yet another pathfinder.
Trackdir
Enumeration for tracks and directions.
TrackdirBits
Allow incrementing of Trackdir variables.
@ TRACKDIR_BIT_NONE
No track build.
uint16_t UnitID
Type for the company global vehicle unit number.
VehicleType
Available vehicle types.