OpenTTD
yapf_rail.cpp
Go to the documentation of this file.
1 /* $Id$ */
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  assert(f1 != NULL);
32  assert(f2 != NULL);
33  fwrite(dmp1.m_out.Data(), 1, dmp1.m_out.Size(), f1);
34  fwrite(dmp2.m_out.Data(), 1, dmp2.m_out.Size(), f2);
35  fclose(f1);
36  fclose(f2);
37 }
38 
39 int _total_pf_time_us = 0;
40 
41 template <class Types>
43 {
44 public:
45  typedef typename Types::Tpf Tpf;
46  typedef typename Types::TrackFollower TrackFollower;
47  typedef typename Types::NodeList::Titem Node;
48 
49 protected:
51  inline Tpf& Yapf()
52  {
53  return *static_cast<Tpf *>(this);
54  }
55 
56 private:
59  Node *m_res_node;
63 
64  bool FindSafePositionProc(TileIndex tile, Trackdir td)
65  {
66  if (IsSafeWaitingPosition(Yapf().GetVehicle(), tile, td, true, !TrackFollower::Allow90degTurns())) {
67  m_res_dest = tile;
68  m_res_dest_td = td;
69  return false; // Stop iterating segment
70  }
71  return true;
72  }
73 
76  {
77  TileIndex start = tile;
78  TileIndexDiff diff = TileOffsByDiagDir(dir);
79 
80  do {
81  if (HasStationReservation(tile)) return false;
82  SetRailStationReservation(tile, true);
83  MarkTileDirtyByTile(tile);
84  tile = TILE_ADD(tile, diff);
85  } while (IsCompatibleTrainStationTile(tile, start) && tile != m_origin_tile);
86 
88 
89  return true;
90  }
91 
94  {
95  if (IsRailStationTile(tile)) {
97  /* Platform could not be reserved, undo. */
98  m_res_fail_tile = tile;
99  m_res_fail_td = td;
100  }
101  } else {
102  if (!TryReserveRailTrack(tile, TrackdirToTrack(td))) {
103  /* Tile couldn't be reserved, undo. */
104  m_res_fail_tile = tile;
105  m_res_fail_td = td;
106  return false;
107  }
108  }
109 
110  return tile != m_res_dest || td != m_res_dest_td;
111  }
112 
115  {
116  if (IsRailStationTile(tile)) {
117  TileIndex start = tile;
119  while ((tile != m_res_fail_tile || td != m_res_fail_td) && IsCompatibleTrainStationTile(tile, start)) {
120  SetRailStationReservation(tile, false);
121  tile = TILE_ADD(tile, diff);
122  }
123  } else if (tile != m_res_fail_tile || td != m_res_fail_td) {
125  }
126  return (tile != m_res_dest || td != m_res_dest_td) && (tile != m_res_fail_tile || td != m_res_fail_td);
127  }
128 
129 public:
131  inline void SetReservationTarget(Node *node, TileIndex tile, Trackdir td)
132  {
133  m_res_node = node;
134  m_res_dest = tile;
135  m_res_dest_td = td;
136  }
137 
139  inline void FindSafePositionOnNode(Node *node)
140  {
141  assert(node->m_parent != NULL);
142 
143  /* We will never pass more than two signals, no need to check for a safe tile. */
144  if (node->m_parent->m_num_signals_passed >= 2) return;
145 
146  if (!node->IterateTiles(Yapf().GetVehicle(), Yapf(), *this, &CYapfReserveTrack<Types>::FindSafePositionProc)) {
147  m_res_node = node;
148  }
149  }
150 
152  bool TryReservePath(PBSTileInfo *target, TileIndex origin)
153  {
154  m_res_fail_tile = INVALID_TILE;
155  m_origin_tile = origin;
156 
157  if (target != NULL) {
158  target->tile = m_res_dest;
159  target->trackdir = m_res_dest_td;
160  target->okay = false;
161  }
162 
163  /* Don't bother if the target is reserved. */
164  if (!IsWaitingPositionFree(Yapf().GetVehicle(), m_res_dest, m_res_dest_td)) return false;
165 
166  for (Node *node = m_res_node; node->m_parent != NULL; node = node->m_parent) {
167  node->IterateTiles(Yapf().GetVehicle(), Yapf(), *this, &CYapfReserveTrack<Types>::ReserveSingleTrack);
168  if (m_res_fail_tile != INVALID_TILE) {
169  /* Reservation failed, undo. */
170  Node *fail_node = m_res_node;
171  TileIndex stop_tile = m_res_fail_tile;
172  do {
173  /* If this is the node that failed, stop at the failed tile. */
174  m_res_fail_tile = fail_node == node ? stop_tile : INVALID_TILE;
175  fail_node->IterateTiles(Yapf().GetVehicle(), Yapf(), *this, &CYapfReserveTrack<Types>::UnreserveSingleTrack);
176  } while (fail_node != node && (fail_node = fail_node->m_parent) != NULL);
177 
178  return false;
179  }
180  }
181 
182  if (target != NULL) target->okay = true;
183 
184  if (Yapf().CanUseGlobalCache(*m_res_node)) {
186  }
187 
188  return true;
189  }
190 };
191 
192 template <class Types>
194 {
195 public:
196  typedef typename Types::Tpf Tpf;
197  typedef typename Types::TrackFollower TrackFollower;
198  typedef typename Types::NodeList::Titem Node;
199  typedef typename Node::Key Key;
200 
201 protected:
203  inline Tpf& Yapf()
204  {
205  return *static_cast<Tpf *>(this);
206  }
207 
208 public:
214  inline void PfFollowNode(Node &old_node)
215  {
216  TrackFollower F(Yapf().GetVehicle());
217  if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir())) {
218  Yapf().AddMultipleNodes(&old_node, F);
219  }
220  }
221 
223  inline char TransportTypeChar() const
224  {
225  return 't';
226  }
227 
228  static FindDepotData stFindNearestDepotTwoWay(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_penalty, int reverse_penalty)
229  {
230  Tpf pf1;
231  /*
232  * With caching enabled it simply cannot get a reliable result when you
233  * have limited the distance a train may travel. This means that the
234  * cached result does not match uncached result in all cases and that
235  * causes desyncs. So disable caching when finding for a depot that is
236  * nearby. This only happens with automatic servicing of vehicles,
237  * so it will only impact performance when you do not manually set
238  * depot orders and you do not disable automatic servicing.
239  */
240  if (max_penalty != 0) pf1.DisableCache(true);
241  FindDepotData result1 = pf1.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_penalty, reverse_penalty);
242 
243  if (_debug_desync_level >= 2) {
244  Tpf pf2;
245  pf2.DisableCache(true);
246  FindDepotData result2 = pf2.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_penalty, reverse_penalty);
247  if (result1.tile != result2.tile || (result1.reverse != result2.reverse)) {
248  DEBUG(desync, 2, "CACHE ERROR: FindNearestDepotTwoWay() = [%s, %s]",
249  result1.tile != INVALID_TILE ? "T" : "F",
250  result2.tile != INVALID_TILE ? "T" : "F");
251  DumpState(pf1, pf2);
252  }
253  }
254 
255  return result1;
256  }
257 
258  inline FindDepotData FindNearestDepotTwoWay(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_penalty, int reverse_penalty)
259  {
260  /* set origin and destination nodes */
261  Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty, true);
262  Yapf().SetDestination(v);
263  Yapf().SetMaxCost(max_penalty);
264 
265  /* find the best path */
266  if (!Yapf().FindPath(v)) return FindDepotData();
267 
268  /* Some path found. */
269  Node *n = Yapf().GetBestNode();
270 
271  /* walk through the path back to the origin */
272  Node *pNode = n;
273  while (pNode->m_parent != NULL) {
274  pNode = pNode->m_parent;
275  }
276 
277  /* if the origin node is our front vehicle tile/Trackdir then we didn't reverse
278  * but we can also look at the cost (== 0 -> not reversed, == reverse_penalty -> reversed) */
279  return FindDepotData(n->GetLastTile(), n->m_cost, pNode->m_cost != 0);
280  }
281 };
282 
283 template <class Types>
285 {
286 public:
287  typedef typename Types::Tpf Tpf;
288  typedef typename Types::TrackFollower TrackFollower;
289  typedef typename Types::NodeList::Titem Node;
290  typedef typename Node::Key Key;
291 
292 protected:
294  inline Tpf& Yapf()
295  {
296  return *static_cast<Tpf *>(this);
297  }
298 
299 public:
305  inline void PfFollowNode(Node &old_node)
306  {
307  TrackFollower F(Yapf().GetVehicle(), Yapf().GetCompatibleRailTypes());
308  if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir()) && F.MaskReservedTracks()) {
309  Yapf().AddMultipleNodes(&old_node, F);
310  }
311  }
312 
314  inline char TransportTypeChar() const
315  {
316  return 't';
317  }
318 
319  static bool stFindNearestSafeTile(const Train *v, TileIndex t1, Trackdir td, bool override_railtype)
320  {
321  /* Create pathfinder instance */
322  Tpf pf1;
323  bool result1;
324  if (_debug_desync_level < 2) {
325  result1 = pf1.FindNearestSafeTile(v, t1, td, override_railtype, false);
326  } else {
327  bool result2 = pf1.FindNearestSafeTile(v, t1, td, override_railtype, true);
328  Tpf pf2;
329  pf2.DisableCache(true);
330  result1 = pf2.FindNearestSafeTile(v, t1, td, override_railtype, false);
331  if (result1 != result2) {
332  DEBUG(desync, 2, "CACHE ERROR: FindSafeTile() = [%s, %s]", result2 ? "T" : "F", result1 ? "T" : "F");
333  DumpState(pf1, pf2);
334  }
335  }
336 
337  return result1;
338  }
339 
340  bool FindNearestSafeTile(const Train *v, TileIndex t1, Trackdir td, bool override_railtype, bool dont_reserve)
341  {
342  /* Set origin and destination. */
343  Yapf().SetOrigin(t1, td);
344  Yapf().SetDestination(v, override_railtype);
345 
346  bool bFound = Yapf().FindPath(v);
347  if (!bFound) return false;
348 
349  /* Found a destination, set as reservation target. */
350  Node *pNode = Yapf().GetBestNode();
351  this->SetReservationTarget(pNode, pNode->GetLastTile(), pNode->GetLastTrackdir());
352 
353  /* Walk through the path back to the origin. */
354  Node *pPrev = NULL;
355  while (pNode->m_parent != NULL) {
356  pPrev = pNode;
357  pNode = pNode->m_parent;
358 
359  this->FindSafePositionOnNode(pPrev);
360  }
361 
362  return dont_reserve || this->TryReservePath(NULL, pNode->GetLastTile());
363  }
364 };
365 
366 template <class Types>
367 class CYapfFollowRailT : public CYapfReserveTrack<Types>
368 {
369 public:
370  typedef typename Types::Tpf Tpf;
371  typedef typename Types::TrackFollower TrackFollower;
372  typedef typename Types::NodeList::Titem Node;
373  typedef typename Node::Key Key;
374 
375 protected:
377  inline Tpf& Yapf()
378  {
379  return *static_cast<Tpf *>(this);
380  }
381 
382 public:
388  inline void PfFollowNode(Node &old_node)
389  {
390  TrackFollower F(Yapf().GetVehicle());
391  if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir())) {
392  Yapf().AddMultipleNodes(&old_node, F);
393  }
394  }
395 
397  inline char TransportTypeChar() const
398  {
399  return 't';
400  }
401 
402  static Trackdir stChooseRailTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, PBSTileInfo *target)
403  {
404  /* create pathfinder instance */
405  Tpf pf1;
406  Trackdir result1;
407 
408  if (_debug_desync_level < 2) {
409  result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_found, reserve_track, target);
410  } else {
411  result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_found, false, NULL);
412  Tpf pf2;
413  pf2.DisableCache(true);
414  Trackdir result2 = pf2.ChooseRailTrack(v, tile, enterdir, tracks, path_found, reserve_track, target);
415  if (result1 != result2) {
416  DEBUG(desync, 2, "CACHE ERROR: ChooseRailTrack() = [%d, %d]", result1, result2);
417  DumpState(pf1, pf2);
418  }
419  }
420 
421  return result1;
422  }
423 
424  inline Trackdir ChooseRailTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, PBSTileInfo *target)
425  {
426  if (target != NULL) target->tile = INVALID_TILE;
427 
428  /* set origin and destination nodes */
430  Yapf().SetOrigin(origin.tile, origin.trackdir, INVALID_TILE, INVALID_TRACKDIR, 1, true);
431  Yapf().SetDestination(v);
432 
433  /* find the best path */
434  path_found = Yapf().FindPath(v);
435 
436  /* if path not found - return INVALID_TRACKDIR */
437  Trackdir next_trackdir = INVALID_TRACKDIR;
438  Node *pNode = Yapf().GetBestNode();
439  if (pNode != NULL) {
440  /* reserve till end of path */
441  this->SetReservationTarget(pNode, pNode->GetLastTile(), pNode->GetLastTrackdir());
442 
443  /* path was found or at least suggested
444  * walk through the path back to the origin */
445  Node *pPrev = NULL;
446  while (pNode->m_parent != NULL) {
447  pPrev = pNode;
448  pNode = pNode->m_parent;
449 
450  this->FindSafePositionOnNode(pPrev);
451  }
452  /* return trackdir from the best origin node (one of start nodes) */
453  Node &best_next_node = *pPrev;
454  next_trackdir = best_next_node.GetTrackdir();
455 
456  if (reserve_track && path_found) this->TryReservePath(target, pNode->GetLastTile());
457  }
458 
459  /* Treat the path as found if stopped on the first two way signal(s). */
460  path_found |= Yapf().m_stopped_on_first_two_way_signal;
461  return next_trackdir;
462  }
463 
464  static bool stCheckReverseTrain(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int reverse_penalty)
465  {
466  Tpf pf1;
467  bool result1 = pf1.CheckReverseTrain(v, t1, td1, t2, td2, reverse_penalty);
468 
469  if (_debug_desync_level >= 2) {
470  Tpf pf2;
471  pf2.DisableCache(true);
472  bool result2 = pf2.CheckReverseTrain(v, t1, td1, t2, td2, reverse_penalty);
473  if (result1 != result2) {
474  DEBUG(desync, 2, "CACHE ERROR: CheckReverseTrain() = [%s, %s]", result1 ? "T" : "F", result2 ? "T" : "F");
475  DumpState(pf1, pf2);
476  }
477  }
478 
479  return result1;
480  }
481 
482  inline bool CheckReverseTrain(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int reverse_penalty)
483  {
484  /* create pathfinder instance
485  * set origin and destination nodes */
486  Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty, false);
487  Yapf().SetDestination(v);
488 
489  /* find the best path */
490  bool bFound = Yapf().FindPath(v);
491 
492  if (!bFound) return false;
493 
494  /* path was found
495  * walk through the path back to the origin */
496  Node *pNode = Yapf().GetBestNode();
497  while (pNode->m_parent != NULL) {
498  pNode = pNode->m_parent;
499  }
500 
501  /* check if it was reversed origin */
502  Node &best_org_node = *pNode;
503  bool reversed = (best_org_node.m_cost != 0);
504  return reversed;
505  }
506 };
507 
508 template <class Tpf_, class Ttrack_follower, class Tnode_list, template <class Types> class TdestinationT, template <class Types> class TfollowT>
510 {
512 
513  typedef Tpf_ Tpf;
514  typedef Ttrack_follower TrackFollower;
515  typedef Tnode_list NodeList;
516  typedef Train VehicleType;
517  typedef CYapfBaseT<Types> PfBase;
518  typedef TfollowT<Types> PfFollow;
520  typedef TdestinationT<Types> PfDestination;
523 };
524 
525 struct CYapfRail1 : CYapfT<CYapfRail_TypesT<CYapfRail1 , CFollowTrackRail , CRailNodeListTrackDir, CYapfDestinationTileOrStationRailT, CYapfFollowRailT> > {};
526 struct CYapfRail2 : CYapfT<CYapfRail_TypesT<CYapfRail2 , CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationTileOrStationRailT, CYapfFollowRailT> > {};
527 
528 struct CYapfAnyDepotRail1 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail1, CFollowTrackRail , CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT , CYapfFollowAnyDepotRailT> > {};
529 struct CYapfAnyDepotRail2 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail2, CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT , CYapfFollowAnyDepotRailT> > {};
530 
531 struct CYapfAnySafeTileRail1 : CYapfT<CYapfRail_TypesT<CYapfAnySafeTileRail1, CFollowTrackFreeRail , CRailNodeListTrackDir, CYapfDestinationAnySafeTileRailT , CYapfFollowAnySafeTileRailT> > {};
532 struct CYapfAnySafeTileRail2 : CYapfT<CYapfRail_TypesT<CYapfAnySafeTileRail2, CFollowTrackFreeRailNo90, CRailNodeListTrackDir, CYapfDestinationAnySafeTileRailT , CYapfFollowAnySafeTileRailT> > {};
533 
534 
535 Track YapfTrainChooseTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, PBSTileInfo *target)
536 {
537  /* default is YAPF type 2 */
538  typedef Trackdir (*PfnChooseRailTrack)(const Train*, TileIndex, DiagDirection, TrackBits, bool&, bool, PBSTileInfo*);
539  PfnChooseRailTrack pfnChooseRailTrack = &CYapfRail1::stChooseRailTrack;
540 
541  /* check if non-default YAPF type needed */
543  pfnChooseRailTrack = &CYapfRail2::stChooseRailTrack; // Trackdir, forbid 90-deg
544  }
545 
546  Trackdir td_ret = pfnChooseRailTrack(v, tile, enterdir, tracks, path_found, reserve_track, target);
547  return (td_ret != INVALID_TRACKDIR) ? TrackdirToTrack(td_ret) : FindFirstTrack(tracks);
548 }
549 
551 {
552  const Train *last_veh = v->Last();
553 
554  /* get trackdirs of both ends */
555  Trackdir td = v->GetVehicleTrackdir();
556  Trackdir td_rev = ReverseTrackdir(last_veh->GetVehicleTrackdir());
557 
558  /* tiles where front and back are */
559  TileIndex tile = v->tile;
560  TileIndex tile_rev = last_veh->tile;
561 
562  int reverse_penalty = 0;
563 
564  if (v->track == TRACK_BIT_WORMHOLE) {
565  /* front in tunnel / on bridge */
566  DiagDirection dir_into_wormhole = GetTunnelBridgeDirection(tile);
567 
568  if (TrackdirToExitdir(td) == dir_into_wormhole) tile = GetOtherTunnelBridgeEnd(tile);
569  /* Now 'tile' is the tunnel entry/bridge ramp the train will reach when driving forward */
570 
571  /* Current position of the train in the wormhole */
572  TileIndex cur_tile = TileVirtXY(v->x_pos, v->y_pos);
573 
574  /* Add distance to drive in the wormhole as penalty for the forward path, i.e. bonus for the reverse path
575  * Note: Negative penalties are ok for the start tile. */
576  reverse_penalty -= DistanceManhattan(cur_tile, tile) * YAPF_TILE_LENGTH;
577  }
578 
579  if (last_veh->track == TRACK_BIT_WORMHOLE) {
580  /* back in tunnel / on bridge */
581  DiagDirection dir_into_wormhole = GetTunnelBridgeDirection(tile_rev);
582 
583  if (TrackdirToExitdir(td_rev) == dir_into_wormhole) tile_rev = GetOtherTunnelBridgeEnd(tile_rev);
584  /* Now 'tile_rev' is the tunnel entry/bridge ramp the train will reach when reversing */
585 
586  /* Current position of the last wagon in the wormhole */
587  TileIndex cur_tile = TileVirtXY(last_veh->x_pos, last_veh->y_pos);
588 
589  /* Add distance to drive in the wormhole as penalty for the revere path. */
590  reverse_penalty += DistanceManhattan(cur_tile, tile_rev) * YAPF_TILE_LENGTH;
591  }
592 
593  typedef bool (*PfnCheckReverseTrain)(const Train*, TileIndex, Trackdir, TileIndex, Trackdir, int);
594  PfnCheckReverseTrain pfnCheckReverseTrain = CYapfRail1::stCheckReverseTrain;
595 
596  /* check if non-default YAPF type needed */
598  pfnCheckReverseTrain = &CYapfRail2::stCheckReverseTrain; // Trackdir, forbid 90-deg
599  }
600 
601  /* slightly hackish: If the pathfinders finds a path, the cost of the first node is tested to distinguish between forward- and reverse-path. */
602  if (reverse_penalty == 0) reverse_penalty = 1;
603 
604  bool reverse = pfnCheckReverseTrain(v, tile, td, tile_rev, td_rev, reverse_penalty);
605 
606  return reverse;
607 }
608 
610 {
611  const Train *last_veh = v->Last();
612 
614  TileIndex last_tile = last_veh->tile;
615  Trackdir td_rev = ReverseTrackdir(last_veh->GetVehicleTrackdir());
616 
617  typedef FindDepotData (*PfnFindNearestDepotTwoWay)(const Train*, TileIndex, Trackdir, TileIndex, Trackdir, int, int);
618  PfnFindNearestDepotTwoWay pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail1::stFindNearestDepotTwoWay;
619 
620  /* check if non-default YAPF type needed */
622  pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail2::stFindNearestDepotTwoWay; // Trackdir, forbid 90-deg
623  }
624 
625  return pfnFindNearestDepotTwoWay(v, origin.tile, origin.trackdir, last_tile, td_rev, max_penalty, YAPF_INFINITE_PENALTY);
626 }
627 
628 bool YapfTrainFindNearestSafeTile(const Train *v, TileIndex tile, Trackdir td, bool override_railtype)
629 {
630  typedef bool (*PfnFindNearestSafeTile)(const Train*, TileIndex, Trackdir, bool);
631  PfnFindNearestSafeTile pfnFindNearestSafeTile = CYapfAnySafeTileRail1::stFindNearestSafeTile;
632 
633  /* check if non-default YAPF type needed */
635  pfnFindNearestSafeTile = &CYapfAnySafeTileRail2::stFindNearestSafeTile;
636  }
637 
638  return pfnFindNearestSafeTile(v, tile, td, override_railtype);
639 }
640 
643 
645 {
646  CSegmentCostCacheBase::NotifyTrackLayoutChange(tile, track);
647 }
CStrA m_out
the output string
Definition: dbg_helpers.h:125
void TriggerStationRandomisation(Station *st, TileIndex tile, StationRandomTrigger trigger, CargoID cargo_type)
Trigger station randomisation.
Determining the destination for rail vehicles.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:77
Types::NodeList::Titem Node
this will be our node type
Definition: yapf_rail.cpp:289
void PfFollowNode(Node &old_node)
Called by YAPF to move from the given node to the next tile.
Definition: yapf_rail.cpp:214
Trackdir m_res_dest_td
The reservation target trackdir.
Definition: yapf_rail.cpp:58
int32 TileIndexDiff
An offset value between to tiles.
Definition: map_func.h:156
static Track TrackdirToTrack(Trackdir trackdir)
Returns the Track that a given Trackdir represents.
Definition: track_func.h:272
bool ReserveSingleTrack(TileIndex tile, Trackdir td)
Try to reserve a single track/platform.
Definition: yapf_rail.cpp:93
Tpf & Yapf()
to access inherited pathfinder
Definition: yapf_rail.cpp:51
bool reverse
True if reversing is necessary for the train to get to this depot.
Trackdir m_res_fail_td
The trackdir where the reservation failed.
Definition: yapf_rail.cpp:61
void YapfNotifyTrackLayoutChange(TileIndex tile, Track track)
Use this function to notify YAPF that track layout (or signal configuration) has change.
Definition: yapf_rail.cpp:644
Track
These are used to specify a single track.
Definition: track_type.h:21
Types::NodeList::Titem Node
this will be our node type
Definition: yapf_rail.cpp:372
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition: yapf_rail.cpp:45
char TransportTypeChar() const
Return debug report character to identify the transportation type.
Definition: yapf_rail.cpp:314
PathfinderSettings pf
settings for all pathfinders
void PfFollowNode(Node &old_node)
Called by YAPF to move from the given node to the next tile.
Definition: yapf_rail.cpp:388
FindDepotData YapfTrainFindNearestDepot(const Train *v, int max_penalty)
Used when user sends train to the nearest depot or if train needs servicing using YAPF...
Definition: yapf_rail.cpp:609
TileIndex m_origin_tile
Tile our reservation will originate from.
Definition: yapf_rail.cpp:62
bool forbid_90_deg
forbid trains to make 90 deg turns
static void SetRailStationReservation(TileIndex t, bool b)
Set the reservation state of the rail station.
Definition: station_map.h:406
void FindSafePositionOnNode(Node *node)
Check the node for a possible reservation target.
Definition: yapf_rail.cpp:139
Tpf & Yapf()
to access inherited path finder
Definition: yapf_rail.cpp:377
Node * m_res_node
The reservation target node.
Definition: yapf_rail.cpp:59
PBSTileInfo FollowTrainReservation(const Train *v, Vehicle **train_on_res)
Follow a train reservation to the last tile.
Definition: pbs.cpp:291
Node::Key Key
key to hash tables
Definition: yapf_rail.cpp:199
static DiagDirection TrackdirToExitdir(Trackdir trackdir)
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
Definition: track_func.h:449
bool YapfTrainFindNearestSafeTile(const Train *v, TileIndex tile, Trackdir td, bool override_railtype)
Try to extend the reserved path of a train to the nearest safe tile using YAPF.
Definition: yapf_rail.cpp:628
bool TryReserveRailTrack(TileIndex tile, Track t, bool trigger_stations)
Try to reserve a specific track on a tile.
Definition: pbs.cpp:82
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition: yapf_rail.cpp:287
static bool IsCompatibleTrainStationTile(TileIndex test_tile, TileIndex station_tile)
Check if a tile is a valid continuation to a railstation tile.
Definition: station_map.h:379
TrackBits
Bitfield corresponding to Track.
Definition: track_type.h:41
static int s_rail_change_counter
if any track changes, this counter is incremented - that will invalidate segment cost cache ...
bool TryReservePath(PBSTileInfo *target, TileIndex origin)
Try to reserve the path till the reservation target.
Definition: yapf_rail.cpp:152
static TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:343
CYapfBaseT - A-star type path finder base class.
Definition: yapf_base.hpp:51
static bool HasStationReservation(TileIndex t)
Get the reservation state of the rail station.
Definition: station_map.h:394
static bool IsRailStationTile(TileIndex t)
Is this tile a station tile and a rail station?
Definition: station_map.h:103
char TransportTypeChar() const
return debug report character to identify the transportation type
Definition: yapf_rail.cpp:223
VehicleType
Available vehicle types.
Definition: vehicle_type.h:23
Entry point for OpenTTD to YAPF&#39;s cache.
bool IsSafeWaitingPosition(const Train *v, TileIndex tile, Trackdir trackdir, bool include_line_end, bool forbid_90deg)
Determine whether a certain track on a tile is a safe position to end a path.
Definition: pbs.cpp:383
Trigger platform when train reserves path.
TileIndex m_res_dest
The reservation target tile.
Definition: yapf_rail.cpp:57
bool IsWaitingPositionFree(const Train *v, TileIndex tile, Trackdir trackdir, bool forbid_90deg)
Check if a safe position is free.
Definition: pbs.cpp:429
static const int YAPF_TILE_LENGTH
Length (penalty) of one tile with YAPF.
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:74
YAPF template that uses Ttypes template argument to determine all YAPF components (base classes) from...
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition: yapf_rail.cpp:370
TileIndex tile
Current tile index.
Definition: vehicle_base.h:230
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
Mark a tile given by its index dirty for repaint.
Definition: viewport.cpp:1785
bool UnreserveSingleTrack(TileIndex tile, Trackdir td)
Unreserve a single track/platform.
Definition: yapf_rail.cpp:114
bool okay
True if tile is a safe waiting position, false otherwise.
Definition: pbs.h:31
DiagDirection
Enumeration for diagonal directions.
Node::Key Key
key to hash tables
Definition: yapf_rail.cpp:373
static DiagDirection GetTunnelBridgeDirection(TileIndex t)
Get the direction pointing to the other end.
static Trackdir ReverseTrackdir(Trackdir trackdir)
Maps a trackdir to the reverse trackdir.
Definition: track_func.h:257
TileIndex tile
Tile the path ends, INVALID_TILE if no valid path was found.
Definition: pbs.h:29
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:36
&#39;Train&#39; is either a loco or a wagon.
Definition: train.h:88
TileIndex tile
The tile of the depot.
This struct contains information about the end of a reserved path.
Definition: pbs.h:28
bool YapfTrainCheckReverse(const Train *v)
Returns true if it is better to reverse the train before leaving station using YAPF.
Definition: yapf_rail.cpp:550
TileIndex m_res_fail_tile
The tile where the reservation failed.
Definition: yapf_rail.cpp:60
void SetReservationTarget(Node *node, TileIndex tile, Trackdir td)
Set the target to where the reservation should be extended.
Definition: yapf_rail.cpp:131
Bitflag for a wormhole (used for tunnels)
Definition: track_type.h:58
static TileIndex GetOtherTunnelBridgeEnd(TileIndex t)
Determines type of the wormhole and returns its other end.
CYapfSegmentCostCacheGlobalT - the yapf cost cache provider that adds the segment cost caching functi...
Tpf & Yapf()
to access inherited path finder
Definition: yapf_rail.cpp:203
static TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition: map_func.h:196
Flag for an invalid track.
Definition: track_type.h:30
Trackdir GetVehicleTrackdir() const
Get the tracks of the train vehicle.
Definition: train_cmd.cpp:4012
T * Last()
Get the last vehicle in the chain.
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition: map.cpp:159
Helper container to find a depot.
Base includes/functions for YAPF.
T * Data()
Return pointer to the first data item - non-const version.
Definition: blob.hpp:358
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition: yapf_rail.cpp:196
static Track FindFirstTrack(TrackBits tracks)
Returns first Track from TrackBits or INVALID_TRACK.
Definition: track_func.h:187
Types::NodeList::Titem Node
this will be our node type
Definition: yapf_rail.cpp:198
int32 x_pos
x coordinate.
Definition: vehicle_base.h:268
Trackdir trackdir
The reserved trackdir on the tile.
Definition: pbs.h:30
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:85
Node::Key Key
key to hash tables
Definition: yapf_rail.cpp:290
int32 y_pos
y coordinate.
Definition: vehicle_base.h:269
YAPF origin provider base class - used when there are two tile/trackdir origins.
Definition: yapf_common.hpp:57
Flag for an invalid trackdir.
Definition: track_type.h:93
size_t Size() const
Return number of items in the Blob.
Definition: blob.hpp:384
Cost determination for rails.
void PfFollowNode(Node &old_node)
Called by YAPF to move from the given node to the next tile.
Definition: yapf_rail.cpp:305
#define TILE_ADD(x, y)
Adds to tiles together.
Definition: map_func.h:246
bool ReserveRailStationPlatform(TileIndex &tile, DiagDirection dir)
Reserve a railway platform.
Definition: yapf_rail.cpp:75
Track YapfTrainChooseTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, PBSTileInfo *target)
Finds the best path for given train using YAPF.
Definition: yapf_rail.cpp:535
char TransportTypeChar() const
return debug report character to identify the transportation type
Definition: yapf_rail.cpp:397
Class that represents the dump-into-string target.
Definition: dbg_helpers.h:96
static const int YAPF_INFINITE_PENALTY
This penalty is the equivalent of "infinite", which means that paths that get this penalty will be ch...
Types::NodeList::Titem Node
this will be our node type
Definition: yapf_rail.cpp:47
void UnreserveRailTrack(TileIndex tile, Track t)
Lift the reservation of a specific track on a tile.
Definition: pbs.cpp:143
Tpf & Yapf()
to access inherited path finder
Definition: yapf_rail.cpp:294
Node tailored for rail pathfinding.