Date: Sat, 4 Jun 2016 20:45:09 +0200 Subject: YAPF can leave earlier when trying to find a road depot for automatic servicing. diff --git a/src/pathfinder/yapf/yapf_road.cpp b/src/pathfinder/yapf/yapf_road.cpp index 380b641..d34d3e3 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; } @@ -442,15 +458,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();