OpenTTD
yapf_rail.cpp
Go to the documentation of this file.
1 /* $Id: yapf_rail.cpp 27845 2017-04-03 22:02:28Z peter1138 $ */
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>
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:
57  Node *m_res_node;
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 
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 
86 
87  return true;
88  }
89 
92  {
93  if (IsRailStationTile(tile)) {
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 
113  {
114  if (IsRailStationTile(tile)) {
115  TileIndex start = tile;
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) {
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)) {
184  }
185 
186  return true;
187  }
188 };
189 
190 template <class Types>
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 FindDepotData stFindNearestDepotTwoWay(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_penalty, int reverse_penalty)
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  FindDepotData result1 = pf1.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_penalty, reverse_penalty);
240 
241  if (_debug_desync_level >= 2) {
242  Tpf pf2;
243  pf2.DisableCache(true);
244  FindDepotData result2 = pf2.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_penalty, reverse_penalty);
245  if (result1.tile != result2.tile || (result1.reverse != result2.reverse)) {
246  DEBUG(desync, 2, "CACHE ERROR: FindNearestDepotTwoWay() = [%s, %s]",
247  result1.tile != INVALID_TILE ? "T" : "F",
248  result2.tile != INVALID_TILE ? "T" : "F");
249  DumpState(pf1, pf2);
250  }
251  }
252 
253  return result1;
254  }
255 
256  inline FindDepotData FindNearestDepotTwoWay(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_penalty, int reverse_penalty)
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  if (!Yapf().FindPath(v)) return FindDepotData();
265 
266  /* Some path found. */
267  Node *n = Yapf().GetBestNode();
268 
269  /* walk through the path back to the origin */
270  Node *pNode = n;
271  while (pNode->m_parent != NULL) {
272  pNode = pNode->m_parent;
273  }
274 
275  /* if the origin node is our front vehicle tile/Trackdir then we didn't reverse
276  * but we can also look at the cost (== 0 -> not reversed, == reverse_penalty -> reversed) */
277  return FindDepotData(n->GetLastTile(), n->m_cost, pNode->m_cost != 0);
278  }
279 };
280 
281 template <class Types>
283 {
284 public:
285  typedef typename Types::Tpf Tpf;
286  typedef typename Types::TrackFollower TrackFollower;
287  typedef typename Types::NodeList::Titem Node;
288  typedef typename Node::Key Key;
289 
290 protected:
292  inline Tpf& Yapf()
293  {
294  return *static_cast<Tpf *>(this);
295  }
296 
297 public:
303  inline void PfFollowNode(Node &old_node)
304  {
305  TrackFollower F(Yapf().GetVehicle(), Yapf().GetCompatibleRailTypes());
306  if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir()) && F.MaskReservedTracks()) {
307  Yapf().AddMultipleNodes(&old_node, F);
308  }
309  }
310 
312  inline char TransportTypeChar() const
313  {
314  return 't';
315  }
316 
317  static bool stFindNearestSafeTile(const Train *v, TileIndex t1, Trackdir td, bool override_railtype)
318  {
319  /* Create pathfinder instance */
320  Tpf pf1;
321  bool result1;
322  if (_debug_desync_level < 2) {
323  result1 = pf1.FindNearestSafeTile(v, t1, td, override_railtype, false);
324  } else {
325  bool result2 = pf1.FindNearestSafeTile(v, t1, td, override_railtype, true);
326  Tpf pf2;
327  pf2.DisableCache(true);
328  result1 = pf2.FindNearestSafeTile(v, t1, td, override_railtype, false);
329  if (result1 != result2) {
330  DEBUG(desync, 2, "CACHE ERROR: FindSafeTile() = [%s, %s]", result2 ? "T" : "F", result1 ? "T" : "F");
331  DumpState(pf1, pf2);
332  }
333  }
334 
335  return result1;
336  }
337 
338  bool FindNearestSafeTile(const Train *v, TileIndex t1, Trackdir td, bool override_railtype, bool dont_reserve)
339  {
340  /* Set origin and destination. */
341  Yapf().SetOrigin(t1, td);
342  Yapf().SetDestination(v, override_railtype);
343 
344  bool bFound = Yapf().FindPath(v);
345  if (!bFound) return false;
346 
347  /* Found a destination, set as reservation target. */
348  Node *pNode = Yapf().GetBestNode();
349  this->SetReservationTarget(pNode, pNode->GetLastTile(), pNode->GetLastTrackdir());
350 
351  /* Walk through the path back to the origin. */
352  Node *pPrev = NULL;
353  while (pNode->m_parent != NULL) {
354  pPrev = pNode;
355  pNode = pNode->m_parent;
356 
357  this->FindSafePositionOnNode(pPrev);
358  }
359 
360  return dont_reserve || this->TryReservePath(NULL, pNode->GetLastTile());
361  }
362 };
363 
364 template <class Types>
365 class CYapfFollowRailT : public CYapfReserveTrack<Types>
366 {
367 public:
368  typedef typename Types::Tpf Tpf;
369  typedef typename Types::TrackFollower TrackFollower;
370  typedef typename Types::NodeList::Titem Node;
371  typedef typename Node::Key Key;
372 
373 protected:
375  inline Tpf& Yapf()
376  {
377  return *static_cast<Tpf *>(this);
378  }
379 
380 public:
386  inline void PfFollowNode(Node &old_node)
387  {
388  TrackFollower F(Yapf().GetVehicle());
389  if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir())) {
390  Yapf().AddMultipleNodes(&old_node, F);
391  }
392  }
393 
395  inline char TransportTypeChar() const
396  {
397  return 't';
398  }
399 
400  static Trackdir stChooseRailTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, PBSTileInfo *target)
401  {
402  /* create pathfinder instance */
403  Tpf pf1;
404  Trackdir result1;
405 
406  if (_debug_desync_level < 2) {
407  result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_found, reserve_track, target);
408  } else {
409  result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_found, false, NULL);
410  Tpf pf2;
411  pf2.DisableCache(true);
412  Trackdir result2 = pf2.ChooseRailTrack(v, tile, enterdir, tracks, path_found, reserve_track, target);
413  if (result1 != result2) {
414  DEBUG(desync, 2, "CACHE ERROR: ChooseRailTrack() = [%d, %d]", result1, result2);
415  DumpState(pf1, pf2);
416  }
417  }
418 
419  return result1;
420  }
421 
422  inline Trackdir ChooseRailTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, PBSTileInfo *target)
423  {
424  if (target != NULL) target->tile = INVALID_TILE;
425 
426  /* set origin and destination nodes */
428  Yapf().SetOrigin(origin.tile, origin.trackdir, INVALID_TILE, INVALID_TRACKDIR, 1, true);
429  Yapf().SetDestination(v);
430 
431  /* find the best path */
432  path_found = Yapf().FindPath(v);
433 
434  /* if path not found - return INVALID_TRACKDIR */
435  Trackdir next_trackdir = INVALID_TRACKDIR;
436  Node *pNode = Yapf().GetBestNode();
437  if (pNode != NULL) {
438  /* reserve till end of path */
439  this->SetReservationTarget(pNode, pNode->GetLastTile(), pNode->GetLastTrackdir());
440 
441  /* path was found or at least suggested
442  * walk through the path back to the origin */
443  Node *pPrev = NULL;
444  while (pNode->m_parent != NULL) {
445  pPrev = pNode;
446  pNode = pNode->m_parent;
447 
448  this->FindSafePositionOnNode(pPrev);
449  }
450  /* return trackdir from the best origin node (one of start nodes) */
451  Node &best_next_node = *pPrev;
452  next_trackdir = best_next_node.GetTrackdir();
453 
454  if (reserve_track && path_found) this->TryReservePath(target, pNode->GetLastTile());
455  }
456 
457  /* Treat the path as found if stopped on the first two way signal(s). */
458  path_found |= Yapf().m_stopped_on_first_two_way_signal;
459  return next_trackdir;
460  }
461 
462  static bool stCheckReverseTrain(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int reverse_penalty)
463  {
464  Tpf pf1;
465  bool result1 = pf1.CheckReverseTrain(v, t1, td1, t2, td2, reverse_penalty);
466 
467  if (_debug_desync_level >= 2) {
468  Tpf pf2;
469  pf2.DisableCache(true);
470  bool result2 = pf2.CheckReverseTrain(v, t1, td1, t2, td2, reverse_penalty);
471  if (result1 != result2) {
472  DEBUG(desync, 2, "CACHE ERROR: CheckReverseTrain() = [%s, %s]", result1 ? "T" : "F", result2 ? "T" : "F");
473  DumpState(pf1, pf2);
474  }
475  }
476 
477  return result1;
478  }
479 
480  inline bool CheckReverseTrain(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int reverse_penalty)
481  {
482  /* create pathfinder instance
483  * set origin and destination nodes */
484  Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty, false);
485  Yapf().SetDestination(v);
486 
487  /* find the best path */
488  bool bFound = Yapf().FindPath(v);
489 
490  if (!bFound) return false;
491 
492  /* path was found
493  * walk through the path back to the origin */
494  Node *pNode = Yapf().GetBestNode();
495  while (pNode->m_parent != NULL) {
496  pNode = pNode->m_parent;
497  }
498 
499  /* check if it was reversed origin */
500  Node &best_org_node = *pNode;
501  bool reversed = (best_org_node.m_cost != 0);
502  return reversed;
503  }
504 };
505 
506 template <class Tpf_, class Ttrack_follower, class Tnode_list, template <class Types> class TdestinationT, template <class Types> class TfollowT>
508 {
510 
511  typedef Tpf_ Tpf;
512  typedef Ttrack_follower TrackFollower;
513  typedef Tnode_list NodeList;
514  typedef Train VehicleType;
515  typedef CYapfBaseT<Types> PfBase;
516  typedef TfollowT<Types> PfFollow;
518  typedef TdestinationT<Types> PfDestination;
521 };
522 
523 struct CYapfRail1 : CYapfT<CYapfRail_TypesT<CYapfRail1 , CFollowTrackRail , CRailNodeListTrackDir, CYapfDestinationTileOrStationRailT, CYapfFollowRailT> > {};
524 struct CYapfRail2 : CYapfT<CYapfRail_TypesT<CYapfRail2 , CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationTileOrStationRailT, CYapfFollowRailT> > {};
525 
526 struct CYapfAnyDepotRail1 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail1, CFollowTrackRail , CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT , CYapfFollowAnyDepotRailT> > {};
527 struct CYapfAnyDepotRail2 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail2, CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT , CYapfFollowAnyDepotRailT> > {};
528 
529 struct CYapfAnySafeTileRail1 : CYapfT<CYapfRail_TypesT<CYapfAnySafeTileRail1, CFollowTrackFreeRail , CRailNodeListTrackDir, CYapfDestinationAnySafeTileRailT , CYapfFollowAnySafeTileRailT> > {};
530 struct CYapfAnySafeTileRail2 : CYapfT<CYapfRail_TypesT<CYapfAnySafeTileRail2, CFollowTrackFreeRailNo90, CRailNodeListTrackDir, CYapfDestinationAnySafeTileRailT , CYapfFollowAnySafeTileRailT> > {};
531 
532 
533 Track YapfTrainChooseTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, PBSTileInfo *target)
534 {
535  /* default is YAPF type 2 */
536  typedef Trackdir (*PfnChooseRailTrack)(const Train*, TileIndex, DiagDirection, TrackBits, bool&, bool, PBSTileInfo*);
537  PfnChooseRailTrack pfnChooseRailTrack = &CYapfRail1::stChooseRailTrack;
538 
539  /* check if non-default YAPF type needed */
541  pfnChooseRailTrack = &CYapfRail2::stChooseRailTrack; // Trackdir, forbid 90-deg
542  }
543 
544  Trackdir td_ret = pfnChooseRailTrack(v, tile, enterdir, tracks, path_found, reserve_track, target);
545  return (td_ret != INVALID_TRACKDIR) ? TrackdirToTrack(td_ret) : FindFirstTrack(tracks);
546 }
547 
549 {
550  const Train *last_veh = v->Last();
551 
552  /* get trackdirs of both ends */
553  Trackdir td = v->GetVehicleTrackdir();
554  Trackdir td_rev = ReverseTrackdir(last_veh->GetVehicleTrackdir());
555 
556  /* tiles where front and back are */
557  TileIndex tile = v->tile;
558  TileIndex tile_rev = last_veh->tile;
559 
560  int reverse_penalty = 0;
561 
562  if (v->track == TRACK_BIT_WORMHOLE) {
563  /* front in tunnel / on bridge */
564  DiagDirection dir_into_wormhole = GetTunnelBridgeDirection(tile);
565 
566  if (TrackdirToExitdir(td) == dir_into_wormhole) tile = GetOtherTunnelBridgeEnd(tile);
567  /* Now 'tile' is the tunnel entry/bridge ramp the train will reach when driving forward */
568 
569  /* Current position of the train in the wormhole */
570  TileIndex cur_tile = TileVirtXY(v->x_pos, v->y_pos);
571 
572  /* Add distance to drive in the wormhole as penalty for the forward path, i.e. bonus for the reverse path
573  * Note: Negative penalties are ok for the start tile. */
574  reverse_penalty -= DistanceManhattan(cur_tile, tile) * YAPF_TILE_LENGTH;
575  }
576 
577  if (last_veh->track == TRACK_BIT_WORMHOLE) {
578  /* back in tunnel / on bridge */
579  DiagDirection dir_into_wormhole = GetTunnelBridgeDirection(tile_rev);
580 
581  if (TrackdirToExitdir(td_rev) == dir_into_wormhole) tile_rev = GetOtherTunnelBridgeEnd(tile_rev);
582  /* Now 'tile_rev' is the tunnel entry/bridge ramp the train will reach when reversing */
583 
584  /* Current position of the last wagon in the wormhole */
585  TileIndex cur_tile = TileVirtXY(last_veh->x_pos, last_veh->y_pos);
586 
587  /* Add distance to drive in the wormhole as penalty for the revere path. */
588  reverse_penalty += DistanceManhattan(cur_tile, tile_rev) * YAPF_TILE_LENGTH;
589  }
590 
591  typedef bool (*PfnCheckReverseTrain)(const Train*, TileIndex, Trackdir, TileIndex, Trackdir, int);
592  PfnCheckReverseTrain pfnCheckReverseTrain = CYapfRail1::stCheckReverseTrain;
593 
594  /* check if non-default YAPF type needed */
596  pfnCheckReverseTrain = &CYapfRail2::stCheckReverseTrain; // Trackdir, forbid 90-deg
597  }
598 
599  /* slightly hackish: If the pathfinders finds a path, the cost of the first node is tested to distinguish between forward- and reverse-path. */
600  if (reverse_penalty == 0) reverse_penalty = 1;
601 
602  bool reverse = pfnCheckReverseTrain(v, tile, td, tile_rev, td_rev, reverse_penalty);
603 
604  return reverse;
605 }
606 
608 {
609  const Train *last_veh = v->Last();
610 
612  TileIndex last_tile = last_veh->tile;
613  Trackdir td_rev = ReverseTrackdir(last_veh->GetVehicleTrackdir());
614 
615  typedef FindDepotData (*PfnFindNearestDepotTwoWay)(const Train*, TileIndex, Trackdir, TileIndex, Trackdir, int, int);
616  PfnFindNearestDepotTwoWay pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail1::stFindNearestDepotTwoWay;
617 
618  /* check if non-default YAPF type needed */
620  pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail2::stFindNearestDepotTwoWay; // Trackdir, forbid 90-deg
621  }
622 
623  return pfnFindNearestDepotTwoWay(v, origin.tile, origin.trackdir, last_tile, td_rev, max_penalty, YAPF_INFINITE_PENALTY);
624 }
625 
626 bool YapfTrainFindNearestSafeTile(const Train *v, TileIndex tile, Trackdir td, bool override_railtype)
627 {
628  typedef bool (*PfnFindNearestSafeTile)(const Train*, TileIndex, Trackdir, bool);
629  PfnFindNearestSafeTile pfnFindNearestSafeTile = CYapfAnySafeTileRail1::stFindNearestSafeTile;
630 
631  /* check if non-default YAPF type needed */
633  pfnFindNearestSafeTile = &CYapfAnySafeTileRail2::stFindNearestSafeTile;
634  }
635 
636  return pfnFindNearestSafeTile(v, tile, td, override_railtype);
637 }
638 
641 
643 {
644  CSegmentCostCacheBase::NotifyTrackLayoutChange(tile, track);
645 }
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:287
void PfFollowNode(Node &old_node)
Called by YAPF to move from the given node to the next tile.
Definition: yapf_rail.cpp:212
Trackdir m_res_dest_td
The reservation target trackdir.
Definition: yapf_rail.cpp:56
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:91
Tpf & Yapf()
to access inherited pathfinder
Definition: yapf_rail.cpp:49
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:59
void YapfNotifyTrackLayoutChange(TileIndex tile, Track track)
Use this function to notify YAPF that track layout (or signal configuration) has change.
Definition: yapf_rail.cpp:642
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:370
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition: yapf_rail.cpp:43
char TransportTypeChar() const
Return debug report character to identify the transportation type.
Definition: yapf_rail.cpp:312
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:386
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:607
TileIndex m_origin_tile
Tile our reservation will originate from.
Definition: yapf_rail.cpp:60
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:137
Tpf & Yapf()
to access inherited path finder
Definition: yapf_rail.cpp:375
Node * m_res_node
The reservation target node.
Definition: yapf_rail.cpp:57
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:197
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:427
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:626
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:285
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:150
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:221
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
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:55
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:368
TileIndex tile
Current tile index.
Definition: vehicle_base.h:230
bool UnreserveSingleTrack(TileIndex tile, Trackdir td)
Unreserve a single track/platform.
Definition: yapf_rail.cpp:112
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:371
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:39
&#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:548
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset)
Mark a tile given by its index dirty for repaint.
Definition: viewport.cpp:1904
TileIndex m_res_fail_tile
The tile where the reservation failed.
Definition: yapf_rail.cpp:58
void SetReservationTarget(Node *node, TileIndex tile, Trackdir td)
Set the target to where the reservation should be extended.
Definition: yapf_rail.cpp:129
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:201
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:4032
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:194
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:196
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:288
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:303
#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:73
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:533
char TransportTypeChar() const
return debug report character to identify the transportation type
Definition: yapf_rail.cpp:395
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:45
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:292
Node tailored for rail pathfinding.