summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuanjo! <juanjo.ng.83@gmail.com>2016-06-04 20:45:09 +0200
committerNiels Martin Hansen <nielsm@indvikleren.dk>2019-01-06 16:47:45 +0100
commit1db66a285e9e887d86dad60e73b00c5a3efc0219 (patch)
tree1c98e257610813590793a810f44375a3234d520c
parentedb7adf183d11c452ab5f60ef7176cd34bab3f54 (diff)
downloadopenttd-1db66a285e9e887d86dad60e73b00c5a3efc0219.tar.xz
Codechange: [YAPF] Stop looking for an automatic servicing road depot when the cost of a path exceeds max. penalty.
-rw-r--r--src/pathfinder/yapf/yapf_road.cpp23
1 files changed, 18 insertions, 5 deletions
diff --git a/src/pathfinder/yapf/yapf_road.cpp b/src/pathfinder/yapf/yapf_road.cpp
index edc8d2a3a..0240eb936 100644
--- a/src/pathfinder/yapf/yapf_road.cpp
+++ b/src/pathfinder/yapf/yapf_road.cpp
@@ -27,6 +27,10 @@ public:
typedef typename Node::Key Key; ///< key to hash tables
protected:
+ int m_max_cost;
+
+ CYapfCostRoadT() : m_max_cost(0) {};
+
/** to access inherited path finder */
Tpf& Yapf()
{
@@ -97,6 +101,11 @@ protected:
}
public:
+ inline void SetMaxCost(int max_cost)
+ {
+ m_max_cost = max_cost;
+ }
+
/**
* Called by YAPF to calculate the cost from the origin to the given node.
* Calculates only the cost of given node, adds it to the parent node cost
@@ -109,6 +118,8 @@ public:
/* start at n.m_key.m_tile / n.m_key.m_td and walk to the end of segment */
TileIndex tile = n.m_key.m_tile;
Trackdir trackdir = n.m_key.m_td;
+ int parent_cost = (n.m_parent != NULL) ? n.m_parent->m_cost : 0;
+
for (;;) {
/* base tile cost depending on distance between edges */
segment_cost += Yapf().OneTileCost(tile, trackdir);
@@ -117,6 +128,12 @@ public:
/* we have reached the vehicle's destination - segment should end here to avoid target skipping */
if (Yapf().PfDetectDestinationTile(tile, trackdir)) break;
+ /* Finish if we already exceeded the maximum path cost (i.e. when
+ * searching for the nearest depot). */
+ if (m_max_cost > 0 && (parent_cost + segment_cost) > m_max_cost) {
+ return false;
+ }
+
/* stop if we have just entered the depot */
if (IsRoadDepotTile(tile) && trackdir == DiagDirToDiagTrackdir(ReverseDiagDir(GetRoadDepotDirection(tile)))) {
/* next time we will reverse and leave the depot */
@@ -160,7 +177,6 @@ public:
n.m_segment_last_td = trackdir;
/* save also tile cost */
- int parent_cost = (n.m_parent != NULL) ? n.m_parent->m_cost : 0;
n.m_cost = parent_cost + segment_cost;
return true;
}
@@ -441,15 +457,12 @@ public:
* @param tile Tile of the vehicle.
* @param td Trackdir of the vehicle.
* @param max_distance max length (penalty) for paths.
- * @todo max_distance not used by YAPF for road vehicles.
- * It can be removed or copy the SetMaxCost() strategy
- * applied in YAPF for rail. The best depot can be at
- * a distance greater than max_distance.
*/
inline FindDepotData FindNearestDepot(const RoadVehicle *v, TileIndex tile, Trackdir td, int max_distance)
{
/* Set origin. */
Yapf().SetOrigin(tile, TrackdirToTrackdirBits(td));
+ Yapf().SetMaxCost(max_distance);
/* Find the best path and return if no depot is found. */
if (!Yapf().FindPath(v)) return FindDepotData();