OpenTTD: src/pathfinder/yapf/yapf_rail.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

yapf_rail.cpp
Go to the documentation of this file.
1 /* $Id: yapf_rail.cpp 27332 2015-07-12 17:06:02Z frosch $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
9
12 #include "../../stdafx.h"
13
14 #include "yapf.hpp"
15 #include "yapf_cache.h"
16 #include "yapf_node_rail.hpp"
17 #include "yapf_costrail.hpp"
18 #include "yapf_destrail.hpp"
19 #include "../../viewport_func.h"
20 #include "../../newgrf_station.h"
21
22 #include "../../safeguards.h"
23
24 template <typename Tpf> void DumpState(Tpf &pf1, Tpf &pf2)
25 {
26 DumpTarget dmp1, dmp2;
27 pf1.DumpBase(dmp1);
28 pf2.DumpBase(dmp2);
29 FILE *f1 = fopen("yapf1.txt", "wt");
30 FILE *f2 = fopen("yapf2.txt", "wt");
31 fwrite(dmp1.m_out.Data(), 1, dmp1.m_out.Size(), f1);
32 fwrite(dmp2.m_out.Data(), 1, dmp2.m_out.Size(), f2);
33 fclose(f1);
34 fclose(f2);
35 }
36
37 int _total_pf_time_us = 0;
38
39 template <class Types>
40 class CYapfReserveTrack
41 {
42 public:
43 typedef typename Types::Tpf Tpf;
44 typedef typename Types::TrackFollower TrackFollower;
45 typedef typename Types::NodeList::Titem Node;
46
47 protected:
49 inline Tpf& Yapf()
50 {
51 return *static_cast<Tpf*>(this);
52 }
53
54 private:
55 TileIndex m_res_dest;
56 Trackdir m_res_dest_td;
57 Node *m_res_node;
58 TileIndex m_res_fail_tile;
59 Trackdir m_res_fail_td;
60 TileIndex m_origin_tile;
61
62 bool FindSafePositionProc(TileIndex tile, Trackdir td)
63 {
64 if (IsSafeWaitingPosition(Yapf().GetVehicle(), tile, td, true, !TrackFollower::Allow90degTurns())) {
65 m_res_dest = tile;
66 m_res_dest_td = td;
67 return false; // Stop iterating segment
68 }
69 return true;
70 }
71
73 bool ReserveRailStationPlatform(TileIndex &tile, DiagDirection dir)
74 {
75 TileIndex start = tile;
76 TileIndexDiff diff = TileOffsByDiagDir(dir);
77
78 do {
79 if (HasStationReservation(tile)) return false;
80 SetRailStationReservation(tile, true);
81 MarkTileDirtyByTile(tile);
82 tile = TILE_ADD(tile, diff);
83 } while (IsCompatibleTrainStationTile(tile, start) && tile != m_origin_tile);
84
85 TriggerStationRandomisation(NULL, start, SRT_PATH_RESERVATION);
86
87 return true;
88 }
89
91 bool ReserveSingleTrack(TileIndex tile, Trackdir td)
92 {
93 if (IsRailStationTile(tile)) {
94 if (!ReserveRailStationPlatform(tile, TrackdirToExitdir(ReverseTrackdir(td)))) {
95 /* Platform could not be reserved, undo. */
96 m_res_fail_tile = tile;
97 m_res_fail_td = td;
98 }
99 } else {
100 if (!TryReserveRailTrack(tile, TrackdirToTrack(td))) {
101 /* Tile couldn't be reserved, undo. */
102 m_res_fail_tile = tile;
103 m_res_fail_td = td;
104 return false;
105 }
106 }
107
108 return tile != m_res_dest || td != m_res_dest_td;
109 }
110
112 bool UnreserveSingleTrack(TileIndex tile, Trackdir td)
113 {
114 if (IsRailStationTile(tile)) {
115 TileIndex start = tile;
116 TileIndexDiff diff = TileOffsByDiagDir(TrackdirToExitdir(ReverseTrackdir(td)));
117 while ((tile != m_res_fail_tile || td != m_res_fail_td) && IsCompatibleTrainStationTile(tile, start)) {
118 SetRailStationReservation(tile, false);
119 tile = TILE_ADD(tile, diff);
120 }
121 } else if (tile != m_res_fail_tile || td != m_res_fail_td) {
122 UnreserveRailTrack(tile, TrackdirToTrack(td));
123 }
124 return (tile != m_res_dest || td != m_res_dest_td) && (tile != m_res_fail_tile || td != m_res_fail_td);
125 }
126
127 public:
129 inline void SetReservationTarget(Node *node, TileIndex tile, Trackdir td)
130 {
131 m_res_node = node;
132 m_res_dest = tile;
133 m_res_dest_td = td;
134 }
135
137 inline void FindSafePositionOnNode(Node *node)
138 {
139 assert(node->m_parent != NULL);
140
141 /* We will never pass more than two signals, no need to check for a safe tile. */
142 if (node->m_parent->m_num_signals_passed >= 2) return;
143
144 if (!node->IterateTiles(Yapf().GetVehicle(), Yapf(), *this, &CYapfReserveTrack<Types>::FindSafePositionProc)) {
145 m_res_node = node;
146 }
147 }
148
150 bool TryReservePath(PBSTileInfo *target, TileIndex origin)
151 {
152 m_res_fail_tile = INVALID_TILE;
153 m_origin_tile = origin;
154
155 if (target != NULL) {
156 target->tile = m_res_dest;
157 target->trackdir = m_res_dest_td;
158 target->okay = false;
159 }
160
161 /* Don't bother if the target is reserved. */
162 if (!IsWaitingPositionFree(Yapf().GetVehicle(), m_res_dest, m_res_dest_td)) return false;
163
164 for (Node *node = m_res_node; node->m_parent != NULL; node = node->m_parent) {
165 node->IterateTiles(Yapf().GetVehicle(), Yapf(), *this, &CYapfReserveTrack<Types>::ReserveSingleTrack);
166 if (m_res_fail_tile != INVALID_TILE) {
167 /* Reservation failed, undo. */
168 Node *fail_node = m_res_node;
169 TileIndex stop_tile = m_res_fail_tile;
170 do {
171 /* If this is the node that failed, stop at the failed tile. */
172 m_res_fail_tile = fail_node == node ? stop_tile : INVALID_TILE;
173 fail_node->IterateTiles(Yapf().GetVehicle(), Yapf(), *this, &CYapfReserveTrack<Types>::UnreserveSingleTrack);
174 } while (fail_node != node && (fail_node = fail_node->m_parent) != NULL);
175
176 return false;
177 }
178 }
179
180 if (target != NULL) target->okay = true;
181
182 if (Yapf().CanUseGlobalCache(*m_res_node)) {
183 YapfNotifyTrackLayoutChange(INVALID_TILE, INVALID_TRACK);
184 }
185
186 return true;
187 }
188 };
189
190 template <class Types>
191 class CYapfFollowAnyDepotRailT
192 {
193 public:
194 typedef typename Types::Tpf Tpf;
195 typedef typename Types::TrackFollower TrackFollower;
196 typedef typename Types::NodeList::Titem Node;
197 typedef typename Node::Key Key;
198
199 protected:
201 inline Tpf& Yapf()
202 {
203 return *static_cast<Tpf*>(this);
204 }
205
206 public:
212 inline void PfFollowNode(Node& old_node)
213 {
214 TrackFollower F(Yapf().GetVehicle());
215 if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir())) {
216 Yapf().AddMultipleNodes(&old_node, F);
217 }
218 }
219
221 inline char TransportTypeChar() const
222 {
223 return 't';
224 }
225
226 static bool stFindNearestDepotTwoWay(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_penalty, int reverse_penalty, TileIndex *depot_tile, bool *reversed)
227 {
228 Tpf pf1;
229 /*
230 * With caching enabled it simply cannot get a reliable result when you
231 * have limited the distance a train may travel. This means that the
232 * cached result does not match uncached result in all cases and that
233 * causes desyncs. So disable caching when finding for a depot that is
234 * nearby. This only happens with automatic servicing of vehicles,
235 * so it will only impact performance when you do not manually set
236 * depot orders and you do not disable automatic servicing.
237 */
238 if (max_penalty != 0) pf1.DisableCache(true);
239 bool result1 = pf1.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_penalty, reverse_penalty, depot_tile, reversed);
240
241 if (_debug_desync_level >= 2) {
242 Tpf pf2;
243 TileIndex depot_tile2 = INVALID_TILE;
244 bool reversed2 = false;
245 pf2.DisableCache(true);
246 bool result2 = pf2.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_penalty, reverse_penalty, &depot_tile2, &reversed2);
247 if (result1 != result2 || (result1 && (*depot_tile != depot_tile2 || *reversed != reversed2))) {
248 DEBUG(desync, 2, "CACHE ERROR: FindNearestDepotTwoWay() = [%s, %s]", result1 ? "T" : "F", result2 ? "T" : "F");
249 DumpState(pf1, pf2);
250 }
251 }
252
253 return result1;
254 }
255
256 inline bool FindNearestDepotTwoWay(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_penalty, int reverse_penalty, TileIndex *depot_tile, bool *reversed)
257 {
258 /* set origin and destination nodes */
259 Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty, true);
260 Yapf().SetDestination(v);
261 Yapf().SetMaxCost(max_penalty);
262
263 /* find the best path */
264 bool bFound = Yapf().FindPath(v);
265 if (!bFound) return false;
266
267 /* some path found
268 * get found depot tile */
269 Node *n = Yapf().GetBestNode();
270 *depot_tile = n->GetLastTile();
271
272 /* walk through the path back to the origin */
273 Node *pNode = n;
274 while (pNode->m_parent != NULL) {
275 pNode = pNode->m_parent;
276 }
277
278 /* if the origin node is our front vehicle tile/Trackdir then we didn't reverse
279 * but we can also look at the cost (== 0 -> not reversed, == reverse_penalty -> reversed) */
280 *reversed = (pNode->m_cost != 0);
281
282 return true;
283 }
284 };
285
286 template <class Types>
287 class CYapfFollowAnySafeTileRailT : public CYapfReserveTrack<Types>
288 {
289 public:
290 typedef typename Types::Tpf Tpf;
291 typedef typename Types::TrackFollower TrackFollower;
292 typedef typename Types::NodeList::Titem Node;
293 typedef typename Node::Key Key;
294
295 protected:
297 inline Tpf& Yapf()
298 {
299 return *static_cast<Tpf*>(this);
300 }
301
302 public:
308 inline void PfFollowNode(Node& old_node)
309 {
310 TrackFollower F(Yapf().GetVehicle(), Yapf().GetCompatibleRailTypes());
311 if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir()) && F.MaskReservedTracks()) {
312 Yapf().AddMultipleNodes(&old_node, F);
313 }
314 }
315
317 inline char TransportTypeChar() const
318 {
319 return 't';
320 }
321
322 static bool stFindNearestSafeTile(const Train *v, TileIndex t1, Trackdir td, bool override_railtype)
323 {
324 /* Create pathfinder instance */
325 Tpf pf1;
326 bool result1;
327 if (_debug_desync_level < 2) {
328 result1 = pf1.FindNearestSafeTile(v, t1, td, override_railtype, false);
329 } else {
330 bool result2 = pf1.FindNearestSafeTile(v, t1, td, override_railtype, true);
331 Tpf pf2;
332 pf2.DisableCache(true);
333 result1 = pf2.FindNearestSafeTile(v, t1, td, override_railtype, false);
334 if (result1 != result2) {
335 DEBUG(desync, 2, "CACHE ERROR: FindSafeTile() = [%s, %s]", result2 ? "T" : "F", result1 ? "T" : "F");
336 DumpState(pf1, pf2);
337 }
338 }
339
340 return result1;
341 }
342
343 bool FindNearestSafeTile(const Train *v, TileIndex t1, Trackdir td, bool override_railtype, bool dont_reserve)
344 {
345 /* Set origin and destination. */
346 Yapf().SetOrigin(t1, td);
347 Yapf().SetDestination(v, override_railtype);
348
349 bool bFound = Yapf().FindPath(v);
350 if (!bFound) return false;
351
352 /* Found a destination, set as reservation target. */
353 Node *pNode = Yapf().GetBestNode();
354 this->SetReservationTarget(pNode, pNode->GetLastTile(), pNode->GetLastTrackdir());
355
356 /* Walk through the path back to the origin. */
357 Node *pPrev = NULL;
358 while (pNode->m_parent != NULL) {
359 pPrev = pNode;
360 pNode = pNode->m_parent;
361
362 this->FindSafePositionOnNode(pPrev);
363 }
364
365 return dont_reserve || this->TryReservePath(NULL, pNode->GetLastTile());
366 }
367 };
368
369 template <class Types>
370 class CYapfFollowRailT : public CYapfReserveTrack<Types>
371 {
372 public:
373 typedef typename Types::Tpf Tpf;
374 typedef typename Types::TrackFollower TrackFollower;
375 typedef typename Types::NodeList::Titem Node;
376 typedef typename Node::Key Key;
377
378 protected:
380 inline Tpf& Yapf()
381 {
382 return *static_cast<Tpf*>(this);
383 }
384
385 public:
391 inline void PfFollowNode(Node& old_node)
392 {
393 TrackFollower F(Yapf().GetVehicle());
394 if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir())) {
395 Yapf().AddMultipleNodes(&old_node, F);
396 }
397 }
398
400 inline char TransportTypeChar() const
401 {
402 return 't';
403 }
404
405 static Trackdir stChooseRailTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, PBSTileInfo *target)
406 {
407 /* create pathfinder instance */
408 Tpf pf1;
409 Trackdir result1;
410
411 if (_debug_desync_level < 2) {
412 result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_found, reserve_track, target);
413 } else {
414 result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_found, false, NULL);
415 Tpf pf2;
416 pf2.DisableCache(true);
417 Trackdir result2 = pf2.ChooseRailTrack(v, tile, enterdir, tracks, path_found, reserve_track, target);
418 if (result1 != result2) {
419 DEBUG(desync, 2, "CACHE ERROR: ChooseRailTrack() = [%d, %d]", result1, result2);
420 DumpState(pf1, pf2);
421 }
422 }
423
424 return result1;
425 }
426
427 inline Trackdir ChooseRailTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, PBSTileInfo *target)
428 {
429 if (target != NULL) target->tile = INVALID_TILE;
430
431 /* set origin and destination nodes */
432 PBSTileInfo origin = FollowTrainReservation(v);
433 Yapf().SetOrigin(origin.tile, origin.trackdir, INVALID_TILE, INVALID_TRACKDIR, 1, true);
434 Yapf().SetDestination(v);
435
436 /* find the best path */
437 path_found = Yapf().FindPath(v);
438
439 /* if path not found - return INVALID_TRACKDIR */
440 Trackdir next_trackdir = INVALID_TRACKDIR;
441 Node *pNode = Yapf().GetBestNode();
442 if (pNode != NULL) {
443 /* reserve till end of path */
444 this->SetReservationTarget(pNode, pNode->GetLastTile(), pNode->GetLastTrackdir());
445
446 /* path was found or at least suggested
447 * walk through the path back to the origin */
448 Node *pPrev = NULL;
449 while (pNode->m_parent != NULL) {
450 pPrev = pNode;
451 pNode = pNode->m_parent;
452
453 this->FindSafePositionOnNode(pPrev);
454 }
455 /* return trackdir from the best origin node (one of start nodes) */
456 Node& best_next_node = *pPrev;
457 next_trackdir = best_next_node.GetTrackdir();
458
459 if (reserve_track && path_found) this->TryReservePath(target, pNode->GetLastTile());
460 }
461
462 /* Treat the path as found if stopped on the first two way signal(s). */
463 path_found |= Yapf().m_stopped_on_first_two_way_signal;
464 return next_trackdir;
465 }
466
467 static bool stCheckReverseTrain(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int reverse_penalty)
468 {
469 Tpf pf1;
470 bool result1 = pf1.CheckReverseTrain(v, t1, td1, t2, td2, reverse_penalty);
471
472 if (_debug_desync_level >= 2) {
473 Tpf pf2;
474 pf2.DisableCache(true);
475 bool result2 = pf2.CheckReverseTrain(v, t1, td1, t2, td2, reverse_penalty);
476 if (result1 != result2) {
477 DEBUG(desync, 2, "CACHE ERROR: CheckReverseTrain() = [%s, %s]", result1 ? "T" : "F", result2 ? "T" : "F");
478 DumpState(pf1, pf2);
479 }
480 }
481
482 return result1;
483 }
484
485 inline bool CheckReverseTrain(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int reverse_penalty)
486 {
487 /* create pathfinder instance
488 * set origin and destination nodes */
489 Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty, false);
490 Yapf().SetDestination(v);
491
492 /* find the best path */
493 bool bFound = Yapf().FindPath(v);
494
495 if (!bFound) return false;
496
497 /* path was found
498 * walk through the path back to the origin */
499 Node *pNode = Yapf().GetBestNode();
500 while (pNode->m_parent != NULL) {
501 pNode = pNode->m_parent;
502 }
503
504 /* check if it was reversed origin */
505 Node& best_org_node = *pNode;
506 bool reversed = (best_org_node.m_cost != 0);
507 return reversed;
508 }
509 };
510
511 template <class Tpf_, class Ttrack_follower, class Tnode_list, template <class Types> class TdestinationT, template <class Types> class TfollowT>
512 struct CYapfRail_TypesT
513 {
514 typedef CYapfRail_TypesT<Tpf_, Ttrack_follower, Tnode_list, TdestinationT, TfollowT> Types;
515
516 typedef Tpf_ Tpf;
517 typedef Ttrack_follower TrackFollower;
518 typedef Tnode_list NodeList;
519 typedef Train VehicleType;
520 typedef CYapfBaseT<Types> PfBase;
521 typedef TfollowT<Types> PfFollow;
522 typedef CYapfOriginTileTwoWayT<Types> PfOrigin;
523 typedef TdestinationT<Types> PfDestination;
524 typedef CYapfSegmentCostCacheGlobalT<Types> PfCache;
525 typedef CYapfCostRailT<Types> PfCost;
526 };
527
528 struct CYapfRail1 : CYapfT<CYapfRail_TypesT<CYapfRail1 , CFollowTrackRail , CRailNodeListTrackDir, CYapfDestinationTileOrStationRailT, CYapfFollowRailT> > {};
529 struct CYapfRail2 : CYapfT<CYapfRail_TypesT<CYapfRail2 , CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationTileOrStationRailT, CYapfFollowRailT> > {};
530
531 struct CYapfAnyDepotRail1 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail1, CFollowTrackRail , CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT , CYapfFollowAnyDepotRailT> > {};
532 struct CYapfAnyDepotRail2 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail2, CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT , CYapfFollowAnyDepotRailT> > {};
533
534 struct CYapfAnySafeTileRail1 : CYapfT<CYapfRail_TypesT<CYapfAnySafeTileRail1, CFollowTrackFreeRail , CRailNodeListTrackDir, CYapfDestinationAnySafeTileRailT , CYapfFollowAnySafeTileRailT> > {};
535 struct CYapfAnySafeTileRail2 : CYapfT<CYapfRail_TypesT<CYapfAnySafeTileRail2, CFollowTrackFreeRailNo90, CRailNodeListTrackDir, CYapfDestinationAnySafeTileRailT , CYapfFollowAnySafeTileRailT> > {};
536
537
538 Track YapfTrainChooseTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, PBSTileInfo *target)
539 {
540 /* default is YAPF type 2 */
541 typedef Trackdir (*PfnChooseRailTrack)(const Train*, TileIndex, DiagDirection, TrackBits, bool&, bool, PBSTileInfo*);
542 PfnChooseRailTrack pfnChooseRailTrack = &CYapfRail1::stChooseRailTrack;
543
544 /* check if non-default YAPF type needed */
545 if (_settings_game.pf.forbid_90_deg) {
546 pfnChooseRailTrack = &CYapfRail2::stChooseRailTrack; // Trackdir, forbid 90-deg
547 }
548
549 Trackdir td_ret = pfnChooseRailTrack(v, tile, enterdir, tracks, path_found, reserve_track, target);
550 return (td_ret != INVALID_TRACKDIR) ? TrackdirToTrack(td_ret) : FindFirstTrack(tracks);
551 }
552
553 bool YapfTrainCheckReverse(const Train *v)
554 {
555 const Train *last_veh = v->Last();
556
557 /* get trackdirs of both ends */
558 Trackdir td = v->GetVehicleTrackdir();
559 Trackdir td_rev = ReverseTrackdir(last_veh->GetVehicleTrackdir());
560
561 /* tiles where front and back are */
562 TileIndex tile = v->tile;
563 TileIndex tile_rev = last_veh->tile;
564
565 int reverse_penalty = 0;
566
567 if (v->track == TRACK_BIT_WORMHOLE) {
568 /* front in tunnel / on bridge */
569 DiagDirection dir_into_wormhole = GetTunnelBridgeDirection(tile);
570
571 if (TrackdirToExitdir(td) == dir_into_wormhole) tile = GetOtherTunnelBridgeEnd(tile);
572 /* Now 'tile' is the tunnel entry/bridge ramp the train will reach when driving forward */
573
574 /* Current position of the train in the wormhole */
575 TileIndex cur_tile = TileVirtXY(v->x_pos, v->y_pos);
576
577 /* Add distance to drive in the wormhole as penalty for the forward path, i.e. bonus for the reverse path
578 * Note: Negative penalties are ok for the start tile. */
579 reverse_penalty -= DistanceManhattan(cur_tile, tile) * YAPF_TILE_LENGTH;
580 }
581
582 if (last_veh->track == TRACK_BIT_WORMHOLE) {
583 /* back in tunnel / on bridge */
584 DiagDirection dir_into_wormhole = GetTunnelBridgeDirection(tile_rev);
585
586 if (TrackdirToExitdir(td_rev) == dir_into_wormhole) tile_rev = GetOtherTunnelBridgeEnd(tile_rev);
587 /* Now 'tile_rev' is the tunnel entry/bridge ramp the train will reach when reversing */
588
589 /* Current position of the last wagon in the wormhole */
590 TileIndex cur_tile = TileVirtXY(last_veh->x_pos, last_veh->y_pos);
591
592 /* Add distance to drive in the wormhole as penalty for the revere path. */
593 reverse_penalty += DistanceManhattan(cur_tile, tile_rev) * YAPF_TILE_LENGTH;
594 }
595
596 typedef bool (*PfnCheckReverseTrain)(const Train*, TileIndex, Trackdir, TileIndex, Trackdir, int);
597 PfnCheckReverseTrain pfnCheckReverseTrain = CYapfRail1::stCheckReverseTrain;
598
599 /* check if non-default YAPF type needed */
600 if (_settings_game.pf.forbid_90_deg) {
601 pfnCheckReverseTrain = &CYapfRail2::stCheckReverseTrain; // Trackdir, forbid 90-deg
602 }
603
604 /* slightly hackish: If the pathfinders finds a path, the cost of the first node is tested to distinguish between forward- and reverse-path. */
605 if (reverse_penalty == 0) reverse_penalty = 1;
606
607 bool reverse = pfnCheckReverseTrain(v, tile, td, tile_rev, td_rev, reverse_penalty);
608
609 return reverse;
610 }
611
612 FindDepotData YapfTrainFindNearestDepot(const Train *v, int max_penalty)
613 {
614 FindDepotData fdd;
615
616 const Train *last_veh = v->Last();
617
618 PBSTileInfo origin = FollowTrainReservation(v);
619 TileIndex last_tile = last_veh->tile;
620 Trackdir td_rev = ReverseTrackdir(last_veh->GetVehicleTrackdir());
621
622 typedef bool (*PfnFindNearestDepotTwoWay)(const Train*, TileIndex, Trackdir, TileIndex, Trackdir, int, int, TileIndex*, bool*);
623 PfnFindNearestDepotTwoWay pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail1::stFindNearestDepotTwoWay;
624
625 /* check if non-default YAPF type needed */
626 if (_settings_game.pf.forbid_90_deg) {
627 pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail2::stFindNearestDepotTwoWay; // Trackdir, forbid 90-deg
628 }
629
630 bool ret = pfnFindNearestDepotTwoWay(v, origin.tile, origin.trackdir, last_tile, td_rev, max_penalty, YAPF_INFINITE_PENALTY, &fdd.tile, &fdd.reverse);
631 fdd.best_length = ret ? max_penalty / 2 : UINT_MAX; // some fake distance or NOT_FOUND
632 return fdd;
633 }
634
635 bool YapfTrainFindNearestSafeTile(const Train *v, TileIndex tile, Trackdir td, bool override_railtype)
636 {
637 typedef bool (*PfnFindNearestSafeTile)(const Train*, TileIndex, Trackdir, bool);
638 PfnFindNearestSafeTile pfnFindNearestSafeTile = CYapfAnySafeTileRail1::stFindNearestSafeTile;
639
640 /* check if non-default YAPF type needed */
641 if (_settings_game.pf.forbid_90_deg) {
642 pfnFindNearestSafeTile = &CYapfAnySafeTileRail2::stFindNearestSafeTile;
643 }
644
645 return pfnFindNearestSafeTile(v, tile, td, override_railtype);
646 }
647
649 int CSegmentCostCacheBase::s_rail_change_counter = 0;
650
651 void YapfNotifyTrackLayoutChange(TileIndex tile, Track track)
652 {
653 CSegmentCostCacheBase::NotifyTrackLayoutChange(tile, track);
654 }