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