2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file yapf_road.cpp The road pathfinding. */
10 #include "../../stdafx.h"
12 #include "yapf_node_road.hpp"
13 #include "../../roadstop_base.h"
15 #include "../../safeguards.h"
18 template <class Types
>
22 typedef typename
Types::Tpf Tpf
; ///< pathfinder (derived from THIS class)
23 typedef typename
Types::TrackFollower TrackFollower
; ///< track follower helper
24 typedef typename
Types::NodeList::Titem Node
; ///< this will be our node type
25 typedef typename
Node::Key Key
; ///< key to hash tables
30 CYapfCostRoadT() : m_max_cost(0) {};
32 /** to access inherited path finder */
35 return *static_cast<Tpf
*>(this);
38 int SlopeCost(TileIndex tile
, TileIndex next_tile
, Trackdir
)
40 /* height of the center of the current tile */
41 int x1
= TileX(tile
) * TILE_SIZE
;
42 int y1
= TileY(tile
) * TILE_SIZE
;
43 int z1
= GetSlopePixelZ(x1
+ TILE_SIZE
/ 2, y1
+ TILE_SIZE
/ 2, true);
45 /* height of the center of the next tile */
46 int x2
= TileX(next_tile
) * TILE_SIZE
;
47 int y2
= TileY(next_tile
) * TILE_SIZE
;
48 int z2
= GetSlopePixelZ(x2
+ TILE_SIZE
/ 2, y2
+ TILE_SIZE
/ 2, true);
52 return Yapf().PfGetSettings().road_slope_penalty
;
57 /** return one tile cost */
58 inline int OneTileCost(TileIndex tile
, Trackdir trackdir
)
62 if (IsDiagonalTrackdir(trackdir
)) {
63 cost
+= YAPF_TILE_LENGTH
;
64 switch (GetTileType(tile
)) {
66 /* Increase the cost for level crossings */
67 if (IsLevelCrossing(tile
)) {
68 cost
+= Yapf().PfGetSettings().road_crossing_penalty
;
73 if (IsRoadWaypoint(tile
)) break;
75 const RoadStop
*rs
= RoadStop::GetByTile(tile
, GetRoadStopType(tile
));
76 if (IsDriveThroughStopTile(tile
)) {
77 /* Increase the cost for drive-through road stops */
78 cost
+= Yapf().PfGetSettings().road_stop_penalty
;
79 DiagDirection dir
= TrackdirToExitdir(trackdir
);
80 if (!RoadStop::IsDriveThroughRoadStopContinuation(tile
, tile
- TileOffsByDiagDir(dir
))) {
81 /* When we're the first road stop in a 'queue' of them we increase
82 * cost based on the fill percentage of the whole queue. */
83 const RoadStop::Entry
*entry
= rs
->GetEntry(dir
);
84 cost
+= entry
->GetOccupied() * Yapf().PfGetSettings().road_stop_occupied_penalty
/ entry
->GetLength();
87 /* Increase cost for filled road stops */
88 cost
+= Yapf().PfGetSettings().road_stop_bay_occupied_penalty
* (!rs
->IsFreeBay(0) + !rs
->IsFreeBay(1)) / 2;
97 /* non-diagonal trackdir */
98 cost
= YAPF_TILE_CORNER_LENGTH
+ Yapf().PfGetSettings().road_curve_penalty
;
104 inline void SetMaxCost(int max_cost
)
106 m_max_cost
= max_cost
;
110 * Called by YAPF to calculate the cost from the origin to the given node.
111 * Calculates only the cost of given node, adds it to the parent node cost
112 * and stores the result into Node::m_cost member
114 inline bool PfCalcCost(Node
&n
, const TrackFollower
*)
116 int segment_cost
= 0;
118 /* start at n.m_key.m_tile / n.m_key.m_td and walk to the end of segment */
119 TileIndex tile
= n
.m_key
.m_tile
;
120 Trackdir trackdir
= n
.m_key
.m_td
;
121 int parent_cost
= (n
.m_parent
!= nullptr) ? n
.m_parent
->m_cost
: 0;
124 /* base tile cost depending on distance between edges */
125 segment_cost
+= Yapf().OneTileCost(tile
, trackdir
);
127 const RoadVehicle
*v
= Yapf().GetVehicle();
128 /* we have reached the vehicle's destination - segment should end here to avoid target skipping */
129 if (Yapf().PfDetectDestinationTile(tile
, trackdir
)) break;
131 /* Finish if we already exceeded the maximum path cost (i.e. when
132 * searching for the nearest depot). */
133 if (m_max_cost
> 0 && (parent_cost
+ segment_cost
) > m_max_cost
) {
137 /* stop if we have just entered the depot */
138 if (IsRoadDepotTile(tile
) && trackdir
== DiagDirToDiagTrackdir(ReverseDiagDir(GetRoadDepotDirection(tile
)))) {
139 /* next time we will reverse and leave the depot */
143 /* if there are no reachable trackdirs on new tile, we have end of road */
144 TrackFollower
F(Yapf().GetVehicle());
145 if (!F
.Follow(tile
, trackdir
)) break;
147 /* if there are more trackdirs available & reachable, we are at the end of segment */
148 if (KillFirstBit(F
.m_new_td_bits
) != TRACKDIR_BIT_NONE
) break;
150 Trackdir new_td
= (Trackdir
)FindFirstBit(F
.m_new_td_bits
);
152 /* stop if RV is on simple loop with no junctions */
153 if (F
.m_new_tile
== n
.m_key
.m_tile
&& new_td
== n
.m_key
.m_td
) return false;
155 /* if we skipped some tunnel tiles, add their cost */
156 segment_cost
+= F
.m_tiles_skipped
* YAPF_TILE_LENGTH
;
157 tiles
+= F
.m_tiles_skipped
+ 1;
159 /* add hilly terrain penalty */
160 segment_cost
+= Yapf().SlopeCost(tile
, F
.m_new_tile
, trackdir
);
162 /* add min/max speed penalties */
164 int max_veh_speed
= std::min
<int>(v
->GetDisplayMaxSpeed(), v
->current_order
.GetMaxSpeed() * 2);
165 int max_speed
= F
.GetSpeedLimit(&min_speed
);
166 if (max_speed
< max_veh_speed
) segment_cost
+= YAPF_TILE_LENGTH
* (max_veh_speed
- max_speed
) * (4 + F
.m_tiles_skipped
) / max_veh_speed
;
167 if (min_speed
> max_veh_speed
) segment_cost
+= YAPF_TILE_LENGTH
* (min_speed
- max_veh_speed
);
169 /* move to the next tile */
172 if (tiles
> MAX_MAP_SIZE
) break;
175 /* save end of segment back to the node */
176 n
.m_segment_last_tile
= tile
;
177 n
.m_segment_last_td
= trackdir
;
179 /* save also tile cost */
180 n
.m_cost
= parent_cost
+ segment_cost
;
186 template <class Types
>
187 class CYapfDestinationAnyDepotRoadT
190 typedef typename
Types::Tpf Tpf
; ///< the pathfinder class (derived from THIS class)
191 typedef typename
Types::TrackFollower TrackFollower
;
192 typedef typename
Types::NodeList::Titem Node
; ///< this will be our node type
193 typedef typename
Node::Key Key
; ///< key to hash tables
195 /** to access inherited path finder */
198 return *static_cast<Tpf
*>(this);
201 /** Called by YAPF to detect if node ends in the desired destination */
202 inline bool PfDetectDestination(Node
&n
)
204 return IsRoadDepotTile(n
.m_segment_last_tile
);
207 inline bool PfDetectDestinationTile(TileIndex tile
, Trackdir
)
209 return IsRoadDepotTile(tile
);
213 * Called by YAPF to calculate cost estimate. Calculates distance to the destination
214 * adds it to the actual cost from origin and stores the sum to the Node::m_estimate
216 inline bool PfCalcEstimate(Node
&n
)
218 n
.m_estimate
= n
.m_cost
;
224 template <class Types
>
225 class CYapfDestinationTileRoadT
228 typedef typename
Types::Tpf Tpf
; ///< the pathfinder class (derived from THIS class)
229 typedef typename
Types::TrackFollower TrackFollower
;
230 typedef typename
Types::NodeList::Titem Node
; ///< this will be our node type
231 typedef typename
Node::Key Key
; ///< key to hash tables
234 TileIndex m_destTile
;
235 TrackdirBits m_destTrackdirs
;
236 StationID m_dest_station
;
237 StationType m_station_type
;
241 void SetDestination(const RoadVehicle
*v
)
243 if (v
->current_order
.IsType(OT_GOTO_STATION
)) {
244 m_dest_station
= v
->current_order
.GetDestination();
245 m_station_type
= v
->IsBus() ? STATION_BUS
: STATION_TRUCK
;
246 m_destTile
= CalcClosestStationTile(m_dest_station
, v
->tile
, m_station_type
);
247 m_non_artic
= !v
->HasArticulatedPart();
248 m_destTrackdirs
= INVALID_TRACKDIR_BIT
;
249 } else if (v
->current_order
.IsType(OT_GOTO_WAYPOINT
)) {
250 m_dest_station
= v
->current_order
.GetDestination();
251 m_station_type
= STATION_ROADWAYPOINT
;
252 m_destTile
= CalcClosestStationTile(m_dest_station
, v
->tile
, m_station_type
);
253 m_non_artic
= !v
->HasArticulatedPart();
254 m_destTrackdirs
= INVALID_TRACKDIR_BIT
;
256 m_dest_station
= INVALID_STATION
;
257 m_destTile
= v
->dest_tile
;
258 m_destTrackdirs
= TrackStatusToTrackdirBits(GetTileTrackStatus(v
->dest_tile
, TRANSPORT_ROAD
, GetRoadTramType(v
->roadtype
)));
262 const Station
*GetDestinationStation() const
264 return m_dest_station
!= INVALID_STATION
? Station::GetIfValid(m_dest_station
) : nullptr;
268 /** to access inherited path finder */
271 return *static_cast<Tpf
*>(this);
275 /** Called by YAPF to detect if node ends in the desired destination */
276 inline bool PfDetectDestination(Node
&n
)
278 return PfDetectDestinationTile(n
.m_segment_last_tile
, n
.m_segment_last_td
);
281 inline bool PfDetectDestinationTile(TileIndex tile
, Trackdir trackdir
)
283 if (m_dest_station
!= INVALID_STATION
) {
284 return IsTileType(tile
, MP_STATION
) &&
285 GetStationIndex(tile
) == m_dest_station
&&
286 (m_station_type
== GetStationType(tile
)) &&
287 (m_non_artic
|| IsDriveThroughStopTile(tile
));
290 return tile
== m_destTile
&& HasTrackdir(m_destTrackdirs
, trackdir
);
294 * Called by YAPF to calculate cost estimate. Calculates distance to the destination
295 * adds it to the actual cost from origin and stores the sum to the Node::m_estimate
297 inline bool PfCalcEstimate(Node
&n
)
299 static const int dg_dir_to_x_offs
[] = {-1, 0, 1, 0};
300 static const int dg_dir_to_y_offs
[] = {0, 1, 0, -1};
301 if (PfDetectDestination(n
)) {
302 n
.m_estimate
= n
.m_cost
;
306 TileIndex tile
= n
.m_segment_last_tile
;
307 DiagDirection exitdir
= TrackdirToExitdir(n
.m_segment_last_td
);
308 int x1
= 2 * TileX(tile
) + dg_dir_to_x_offs
[(int)exitdir
];
309 int y1
= 2 * TileY(tile
) + dg_dir_to_y_offs
[(int)exitdir
];
310 int x2
= 2 * TileX(m_destTile
);
311 int y2
= 2 * TileY(m_destTile
);
312 int dx
= abs(x1
- x2
);
313 int dy
= abs(y1
- y2
);
314 int dmin
= std::min(dx
, dy
);
315 int dxy
= abs(dx
- dy
);
316 int d
= dmin
* YAPF_TILE_CORNER_LENGTH
+ (dxy
- 1) * (YAPF_TILE_LENGTH
/ 2);
317 n
.m_estimate
= n
.m_cost
+ d
;
318 assert(n
.m_estimate
>= n
.m_parent
->m_estimate
);
325 template <class Types
>
326 class CYapfFollowRoadT
329 typedef typename
Types::Tpf Tpf
; ///< the pathfinder class (derived from THIS class)
330 typedef typename
Types::TrackFollower TrackFollower
;
331 typedef typename
Types::NodeList::Titem Node
; ///< this will be our node type
332 typedef typename
Node::Key Key
; ///< key to hash tables
335 /** to access inherited path finder */
338 return *static_cast<Tpf
*>(this);
344 * Called by YAPF to move from the given node to the next tile. For each
345 * reachable trackdir on the new tile creates new node, initializes it
346 * and adds it to the open list by calling Yapf().AddNewNode(n)
348 inline void PfFollowNode(Node
&old_node
)
350 TrackFollower
F(Yapf().GetVehicle());
351 if (F
.Follow(old_node
.m_segment_last_tile
, old_node
.m_segment_last_td
)) {
352 Yapf().AddMultipleNodes(&old_node
, F
);
356 /** return debug report character to identify the transportation type */
357 inline char TransportTypeChar() const
362 static Trackdir
stChooseRoadTrack(const RoadVehicle
*v
, TileIndex tile
, DiagDirection enterdir
, bool &path_found
, RoadVehPathCache
&path_cache
)
365 return pf
.ChooseRoadTrack(v
, tile
, enterdir
, path_found
, path_cache
);
368 inline Trackdir
ChooseRoadTrack(const RoadVehicle
*v
, TileIndex tile
, DiagDirection enterdir
, bool &path_found
, RoadVehPathCache
&path_cache
)
370 /* Handle special case - when next tile is destination tile.
371 * However, when going to a station the (initial) destination
372 * tile might not be a station, but a junction, in which case
373 * this method forces the vehicle to jump in circles. */
374 if (tile
== v
->dest_tile
&& !v
->current_order
.IsType(OT_GOTO_STATION
)) {
375 /* choose diagonal trackdir reachable from enterdir */
376 return DiagDirToDiagTrackdir(enterdir
);
378 /* our source tile will be the next vehicle tile (should be the given one) */
379 TileIndex src_tile
= tile
;
380 /* get available trackdirs on the start tile */
381 TrackdirBits src_trackdirs
= GetTrackdirBitsForRoad(tile
, GetRoadTramType(v
->roadtype
));
382 /* select reachable trackdirs only */
383 src_trackdirs
&= DiagdirReachesTrackdirs(enterdir
);
385 /* set origin and destination nodes */
386 Yapf().SetOrigin(src_tile
, src_trackdirs
);
387 Yapf().SetDestination(v
);
389 /* find the best path */
390 path_found
= Yapf().FindPath(v
);
392 /* if path not found - return INVALID_TRACKDIR */
393 Trackdir next_trackdir
= INVALID_TRACKDIR
;
394 Node
*pNode
= Yapf().GetBestNode();
395 if (pNode
!= nullptr) {
397 for (Node
*n
= pNode
; n
->m_parent
!= nullptr; n
= n
->m_parent
) steps
++;
399 /* path was found or at least suggested
400 * walk through the path back to its origin */
401 while (pNode
->m_parent
!= nullptr) {
403 if (pNode
->GetIsChoice() && steps
< YAPF_ROADVEH_PATH_CACHE_SEGMENTS
) {
404 path_cache
.td
.push_front(pNode
->GetTrackdir());
405 path_cache
.tile
.push_front(pNode
->GetTile());
407 pNode
= pNode
->m_parent
;
409 /* return trackdir from the best origin node (one of start nodes) */
410 Node
&best_next_node
= *pNode
;
411 assert(best_next_node
.GetTile() == tile
);
412 next_trackdir
= best_next_node
.GetTrackdir();
413 /* remove last element for the special case when tile == dest_tile */
414 if (path_found
&& !path_cache
.empty() && tile
== v
->dest_tile
) {
415 path_cache
.td
.pop_back();
416 path_cache
.tile
.pop_back();
419 /* Check if target is a station, and cached path ends within 8 tiles of the dest tile */
420 const Station
*st
= Yapf().GetDestinationStation();
422 const RoadStop
*stop
= st
->GetPrimaryRoadStop(v
);
423 if (stop
!= nullptr && (IsDriveThroughStopTile(stop
->xy
) || stop
->GetNextRoadStop(v
) != nullptr)) {
424 /* Destination station has at least 2 usable road stops, or first is a drive-through stop,
425 * trim end of path cache within a number of tiles of road stop tile area */
426 TileArea non_cached_area
= v
->IsBus() ? st
->bus_station
: st
->truck_station
;
427 non_cached_area
.Expand(YAPF_ROADVEH_PATH_CACHE_DESTINATION_LIMIT
);
428 while (!path_cache
.empty() && non_cached_area
.Contains(path_cache
.tile
.back())) {
429 path_cache
.td
.pop_back();
430 path_cache
.tile
.pop_back();
435 return next_trackdir
;
438 inline uint
DistanceToTile(const RoadVehicle
*v
, TileIndex dst_tile
)
440 /* handle special case - when current tile is the destination tile */
441 if (dst_tile
== v
->tile
) {
442 /* distance is zero in this case */
446 if (!SetOriginFromVehiclePos(v
)) return UINT_MAX
;
448 /* get available trackdirs on the destination tile */
449 Yapf().SetDestination(v
);
451 /* if path not found - return distance = UINT_MAX */
452 uint dist
= UINT_MAX
;
454 /* find the best path */
455 if (!Yapf().FindPath(v
)) return dist
;
457 Node
*pNode
= Yapf().GetBestNode();
458 if (pNode
!= nullptr) {
460 * get the path cost estimate */
461 dist
= pNode
->GetCostEstimate();
467 /** Return true if the valid origin (tile/trackdir) was set from the current vehicle position. */
468 inline bool SetOriginFromVehiclePos(const RoadVehicle
*v
)
470 /* set origin (tile, trackdir) */
471 TileIndex src_tile
= v
->tile
;
472 Trackdir src_td
= v
->GetVehicleTrackdir();
473 if (!HasTrackdir(GetTrackdirBitsForRoad(src_tile
, Yapf().IsTram() ? RTT_TRAM
: RTT_ROAD
), src_td
)) {
474 /* sometimes the roadveh is not on the road (it resides on non-existing track)
475 * how should we handle that situation? */
478 Yapf().SetOrigin(src_tile
, TrackdirToTrackdirBits(src_td
));
482 static FindDepotData
stFindNearestDepot(const RoadVehicle
*v
, TileIndex tile
, Trackdir td
, int max_distance
)
485 return pf
.FindNearestDepot(v
, tile
, td
, max_distance
);
489 * Find the best depot for a road vehicle.
491 * @param tile Tile of the vehicle.
492 * @param td Trackdir of the vehicle.
493 * @param max_distance max length (penalty) for paths.
495 inline FindDepotData
FindNearestDepot(const RoadVehicle
*v
, TileIndex tile
, Trackdir td
, int max_distance
)
498 Yapf().SetOrigin(tile
, TrackdirToTrackdirBits(td
));
499 Yapf().SetMaxCost(max_distance
);
501 /* Find the best path and return if no depot is found. */
502 if (!Yapf().FindPath(v
)) return FindDepotData();
504 /* Return the cost of the best path and its depot. */
505 Node
*n
= Yapf().GetBestNode();
506 return FindDepotData(n
->m_segment_last_tile
, n
->m_cost
);
510 template <class Tpf_
, class Tnode_list
, template <class Types
> class Tdestination
>
511 struct CYapfRoad_TypesT
513 typedef CYapfRoad_TypesT
<Tpf_
, Tnode_list
, Tdestination
> Types
;
516 typedef CFollowTrackRoad TrackFollower
;
517 typedef Tnode_list NodeList
;
518 typedef RoadVehicle VehicleType
;
519 typedef CYapfBaseT
<Types
> PfBase
;
520 typedef CYapfFollowRoadT
<Types
> PfFollow
;
521 typedef CYapfOriginTileT
<Types
> PfOrigin
;
522 typedef Tdestination
<Types
> PfDestination
;
523 typedef CYapfSegmentCostCacheNoneT
<Types
> PfCache
;
524 typedef CYapfCostRoadT
<Types
> PfCost
;
527 struct CYapfRoad1
: CYapfT
<CYapfRoad_TypesT
<CYapfRoad1
, CRoadNodeListTrackDir
, CYapfDestinationTileRoadT
> > {};
528 struct CYapfRoad2
: CYapfT
<CYapfRoad_TypesT
<CYapfRoad2
, CRoadNodeListExitDir
, CYapfDestinationTileRoadT
> > {};
530 struct CYapfRoadAnyDepot1
: CYapfT
<CYapfRoad_TypesT
<CYapfRoadAnyDepot1
, CRoadNodeListTrackDir
, CYapfDestinationAnyDepotRoadT
> > {};
531 struct CYapfRoadAnyDepot2
: CYapfT
<CYapfRoad_TypesT
<CYapfRoadAnyDepot2
, CRoadNodeListExitDir
, CYapfDestinationAnyDepotRoadT
> > {};
534 Trackdir
YapfRoadVehicleChooseTrack(const RoadVehicle
*v
, TileIndex tile
, DiagDirection enterdir
, TrackdirBits trackdirs
, bool &path_found
, RoadVehPathCache
&path_cache
)
536 /* default is YAPF type 2 */
537 typedef Trackdir (*PfnChooseRoadTrack
)(const RoadVehicle
*, TileIndex
, DiagDirection
, bool &path_found
, RoadVehPathCache
&path_cache
);
538 PfnChooseRoadTrack pfnChooseRoadTrack
= &CYapfRoad2::stChooseRoadTrack
; // default: ExitDir, allow 90-deg
540 /* check if non-default YAPF type should be used */
541 if (_settings_game
.pf
.yapf
.disable_node_optimization
) {
542 pfnChooseRoadTrack
= &CYapfRoad1::stChooseRoadTrack
; // Trackdir
545 Trackdir td_ret
= pfnChooseRoadTrack(v
, tile
, enterdir
, path_found
, path_cache
);
546 return (td_ret
!= INVALID_TRACKDIR
) ? td_ret
: (Trackdir
)FindFirstBit(trackdirs
);
549 FindDepotData
YapfRoadVehicleFindNearestDepot(const RoadVehicle
*v
, int max_distance
)
551 TileIndex tile
= v
->tile
;
552 Trackdir trackdir
= v
->GetVehicleTrackdir();
554 if (!HasTrackdir(GetTrackdirBitsForRoad(tile
, GetRoadTramType(v
->roadtype
)), trackdir
)) {
555 return FindDepotData();
558 /* default is YAPF type 2 */
559 typedef FindDepotData (*PfnFindNearestDepot
)(const RoadVehicle
*, TileIndex
, Trackdir
, int);
560 PfnFindNearestDepot pfnFindNearestDepot
= &CYapfRoadAnyDepot2::stFindNearestDepot
;
562 /* check if non-default YAPF type should be used */
563 if (_settings_game
.pf
.yapf
.disable_node_optimization
) {
564 pfnFindNearestDepot
= &CYapfRoadAnyDepot1::stFindNearestDepot
; // Trackdir
567 return pfnFindNearestDepot(v
, tile
, trackdir
, max_distance
);