50 typedef typename Types::Tpf
Tpf;
51 typedef typename Types::TrackFollower TrackFollower;
54 typedef typename NodeList::Item
Node;
55 typedef typename Node::Key
Key;
133 return *
static_cast<Tpf *
>(
this);
143 return *this->settings;
163 if (best_open_node ==
nullptr)
break;
165 if (
Yapf().PfDetectDestination(*best_open_node)) {
166 this->best_dest_node = best_open_node;
170 Yapf().PfFollowNode(*best_open_node);
171 if (this->max_search_nodes != 0 && this->nodes.
ClosedCount() >= this->max_search_nodes)
break;
177 const bool destination_found = (this->best_dest_node !=
nullptr);
179 if (_debug_yapf_level >= 3) {
180 const UnitID veh_idx = (this->vehicle !=
nullptr) ? this->vehicle->unitnumber : 0;
181 const char ttc =
Yapf().TransportTypeChar();
182 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);
183 const int cost = destination_found ? this->best_dest_node->cost : -1;
184 const int dist = destination_found ? this->best_dest_node->estimate - this->best_dest_node->cost : -1;
186 Debug(yapf, 3,
"[YAPF{}]{}{:4d} - {} rounds - {} open - {} closed - CHR {:4.1f}% - C {} D {}",
187 ttc, destination_found ?
'-' :
'!', veh_idx, this->num_steps, this->nodes.
OpenCount(), this->nodes.ClosedCount(), cache_hit_ratio, cost, dist
191 return destination_found;
201 return (this->best_dest_node !=
nullptr) ? this->best_dest_node : this->best_intermediate_node;
221 assert(n.parent ==
nullptr);
222 assert(this->num_steps == 0);
224 Yapf().PfNodeCacheFetch(n);
242 n.Set(parent, tf.new_tile, td, is_choice);
243 Yapf().AddNewNode(n, tf);
255 assert(n.parent !=
nullptr);
258 bool cached =
Yapf().PfNodeCacheFetch(n);
260 this->stats_cost_calcs++;
262 this->stats_cache_hits++;
265 bool valid =
Yapf().PfCalcCost(n, &follower);
267 if (valid) valid =
Yapf().PfCalcEstimate(n);
274 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()));
278 if (open_node !=
nullptr) {
281 if (n.GetCostEstimate() < open_node->GetCostEstimate()) {
287 if (set_intermediate) this->best_intermediate_node = open_node;
294 if (closed_node !=
nullptr) {
297 int node_est = n.GetCostEstimate();
298 int closed_est = closed_node->GetCostEstimate();
299 if (node_est < closed_est) {
313 if (set_intermediate) this->best_intermediate_node = &n;
318 return this->vehicle;
321 void DumpBase(DumpTarget &dmp)
const
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.
int num_steps
this is there for debugging purposes (hope it doesn't hurt)
void(Node &old_node) PfFollowNodeFunc
Called by YAPF to move from the given node to the next tile.
Node::Key Key
key to hash tables
void AddNewNode(Node &n, const TrackFollower &follower)
AddNewNode() - called by Tderived::PfFollowNode() for each child node.
CYapfBaseT()
default constructor
const VehicleType * vehicle
vehicle that we are trying to drive
Node * best_intermediate_node
here should be node closest to the destination if path not found
char() TransportTypeCharFunc
Return debug report character to identify the transportation type.
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
bool(TileIndex tile, Trackdir td) PfDetectDestinationTileFunc
Called by YAPF to detect if node ends in the desired destination.
bool(Node &n) PfDetectDestinationFunc
Called by YAPF to detect if node ends in the desired destination.
bool(Node &n, const TrackFollower *follower) PfCalcCostFunc
Called by YAPF to calculate the cost from the origin to the given node.
Node * GetBestNode()
If path was found return the best node that has reached the destination.
~CYapfBaseT()
default destructor
bool FindPath(const VehicleType *v)
Main pathfinder routine:
bool(Node &n) PfCalcEstimateFunc
Called by YAPF to calculate cost estimate.
Node & CreateNewNode()
Calls NodeList::CreateNewNode() - allocates new node that can be filled and used as argument for AddS...
NodeList nodes
node list multi-container
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
const YAPFSettings & PfGetSettings() const
Return current settings (can be custom - company based - but later).
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
Tpf & Yapf()
Access the inherited path finder.
void InsertClosedNode(Titem &item)
Insert the given item into the closed nodes set.
Titem * FindClosedNode(const Key &key)
Find a closed node by its key.
void InsertOpenNode(Titem &item)
Insert given item as open node (into open_nodes and open_queue).
int OpenCount()
Get open node count.
int ClosedCount()
Get closed node count.
Titem & CreateNewNode()
Allocate new data item from items.
Titem * GetBestOpenNode()
Get the open node at the begin of the open queue.
Titem & PopOpenNode(const Key &key)
Find and remove an open node by key.
Titem * FindOpenNode(const Key &key)
Find an open node by key.
Functions to be used for debug printings.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Types related to global configuration settings.
void WriteStructT(std::string_view name, const S *s)
Dump nested object (or only its name if this instance is already known).
void WriteValue(std::string_view name, const auto &value)
Write 'name = value' with indent and new-line.
Settings related to the yet another pathfinder.
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
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.