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

OpenTTD Source 14.0-beta1

yapf_rail.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #include "../../stdafx.h"
11
12 #include "yapf.hpp"
13 #include "yapf_cache.h"
14 #include "yapf_node_rail.hpp"
15 #include "yapf_costrail.hpp"
16 #include "yapf_destrail.hpp"
17 #include "../../viewport_func.h"
18 #include "../../newgrf_station.h"
19
20 #include "../../safeguards.h"
21
22 template <typename Tpf> void DumpState(Tpf &pf1, Tpf &pf2)
23 {
24 DumpTarget dmp1, dmp2;
25 pf1.DumpBase(dmp1);
26 pf2.DumpBase(dmp2);
27 FILE *f1 = fopen("yapf1.txt", "wt");
28 FILE *f2 = fopen("yapf2.txt", "wt");
29 assert(f1 != nullptr);
30 assert(f2 != nullptr);
31 fwrite(dmp1.m_out.c_str(), 1, dmp1.m_out.size(), f1);
32 fwrite(dmp2.m_out.c_str(), 1, dmp2.m_out.size(), f2);
33 fclose(f1);
34 fclose(f2);
35 }
36
37 template <class Types>
38 class CYapfReserveTrack
39 {
40 public:
41 typedef typename Types::Tpf Tpf;
42 typedef typename Types::TrackFollower TrackFollower;
43 typedef typename Types::NodeList::Titem Node;
44
45 protected:
47 inline Tpf &Yapf()
48 {
49 return *static_cast<Tpf *>(this);
50 }
51
52 private:
53 TileIndex m_res_dest;
54 Trackdir m_res_dest_td;
55 Node *m_res_node;
56 TileIndex m_res_fail_tile;
57 Trackdir m_res_fail_td;
58 TileIndex m_origin_tile;
59
60 bool FindSafePositionProc(TileIndex tile, Trackdir td)
61 {
62 if (IsSafeWaitingPosition(Yapf().GetVehicle(), tile, td, true, !TrackFollower::Allow90degTurns())) {
63 m_res_dest = tile;
64 m_res_dest_td = td;
65 return false; // Stop iterating segment
66 }
67 return true;
68 }
69
71 bool ReserveRailStationPlatform(TileIndex &tile, DiagDirection dir)
72 {
73 TileIndex start = tile;
74 TileIndexDiff diff = TileOffsByDiagDir(dir);
75
76 do {
77 if (HasStationReservation(tile)) return false;
78 SetRailStationReservation(tile, true);
79 MarkTileDirtyByTile(tile);
80 tile = TILE_ADD(tile, diff);
81 } while (IsCompatibleTrainStationTile(tile, start) && tile != m_origin_tile);
82
83 TriggerStationRandomisation(nullptr, start, SRT_PATH_RESERVATION);
84
85 return true;
86 }
87
89 bool ReserveSingleTrack(TileIndex tile, Trackdir td)
90 {
91 if (IsRailStationTile(tile)) {
92 if (!ReserveRailStationPlatform(tile, TrackdirToExitdir(ReverseTrackdir(td)))) {
93 /* Platform could not be reserved, undo. */
94 m_res_fail_tile = tile;
95 m_res_fail_td = td;
96 }
97 } else {
98 if (!TryReserveRailTrack(tile, TrackdirToTrack(td))) {
99 /* Tile couldn't be reserved, undo. */
100 m_res_fail_tile = tile;
101 m_res_fail_td = td;
102 return false;
103 }
104 }
105
106 return tile != m_res_dest || td != m_res_dest_td;
107 }
108
110 bool UnreserveSingleTrack(TileIndex tile, Trackdir td)
111 {
112 if (IsRailStationTile(tile)) {
113 TileIndex start = tile;
114 TileIndexDiff diff = TileOffsByDiagDir(TrackdirToExitdir(ReverseTrackdir(td)));
115 while ((tile != m_res_fail_tile || td != m_res_fail_td) && IsCompatibleTrainStationTile(tile, start)) {
116 SetRailStationReservation(tile, false);
117 tile = TILE_ADD(tile, diff);
118 }
119 } else if (tile != m_res_fail_tile || td != m_res_fail_td) {
120 UnreserveRailTrack(tile, TrackdirToTrack(td));
121 }
122 return (tile != m_res_dest || td != m_res_dest_td) && (tile != m_res_fail_tile || td != m_res_fail_td);
123 }
124
125 public:
127 inline void SetReservationTarget(Node *node, TileIndex tile, Trackdir td)
128 {
129 m_res_node = node;
130 m_res_dest = tile;
131 m_res_dest_td = td;
132 }
133
135 inline void FindSafePositionOnNode(Node *node)
136 {
137 assert(node->m_parent != nullptr);
138
139 /* We will never pass more than two signals, no need to check for a safe tile. */
140 if (node->m_parent->m_num_signals_passed >= 2) return;
141
142 if (!node->IterateTiles(Yapf().GetVehicle(), Yapf(), *this, &CYapfReserveTrack<Types>::FindSafePositionProc)) {
143 m_res_node = node;
144 }
145 }
146
148 bool TryReservePath(PBSTileInfo *target, TileIndex origin)
149 {
150 m_res_fail_tile = INVALID_TILE;
151 m_origin_tile = origin;
152
153 if (target != nullptr) {
154 target->tile = m_res_dest;
155 target->trackdir = m_res_dest_td;
156 target->okay = false;
157 }
158
159 /* Don't bother if the target is reserved. */
160 if (!IsWaitingPositionFree(Yapf().GetVehicle(), m_res_dest, m_res_dest_td)) return false;
161
162 for (Node *node = m_res_node; node->m_parent != nullptr; node = node->m_parent) {
163 node->IterateTiles(Yapf().GetVehicle(), Yapf(), *this, &CYapfReserveTrack<Types>::ReserveSingleTrack);
164 if (m_res_fail_tile != INVALID_TILE) {
165 /* Reservation failed, undo. */
166 Node *fail_node = m_res_node;
167 TileIndex stop_tile = m_res_fail_tile;
168 do {
169 /* If this is the node that failed, stop at the failed tile. */
170 m_res_fail_tile = fail_node == node ? stop_tile : INVALID_TILE;
171 fail_node->IterateTiles(Yapf().GetVehicle(), Yapf(), *this, &CYapfReserveTrack<Types>::UnreserveSingleTrack);
172 } while (fail_node != node && (fail_node = fail_node->m_parent) != nullptr);
173
174 return false;
175 }
176 }
177
178 if (target != nullptr) target->okay = true;
179
180 if (Yapf().CanUseGlobalCache(*m_res_node)) {
181 YapfNotifyTrackLayoutChange(INVALID_TILE, INVALID_TRACK);
182 }
183
184 return true;
185 }
186 };
187
188 template <class Types>
189 class CYapfFollowAnyDepotRailT
190 {
191 public:
192 typedef typename Types::Tpf Tpf;
193 typedef typename Types::TrackFollower TrackFollower;
194 typedef typename Types::NodeList::Titem Node;
195 typedef typename Node::Key Key;
196
197 protected:
199 inline Tpf &Yapf()
200 {
201 return *static_cast<Tpf *>(this);
202 }
203
204 public:
210 inline void PfFollowNode(Node &old_node)
211 {
212 TrackFollower F(Yapf().GetVehicle());
213 if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir())) {
214 Yapf().AddMultipleNodes(&old_node, F);
215 }
216 }
217
219 inline char TransportTypeChar() const
220 {
221 return 't';
222 }
223
224 static FindDepotData stFindNearestDepotTwoWay(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_penalty, int reverse_penalty)
225 {
226 Tpf pf1;
227 /*
228 * With caching enabled it simply cannot get a reliable result when you
229 * have limited the distance a train may travel. This means that the
230 * cached result does not match uncached result in all cases and that
231 * causes desyncs. So disable caching when finding for a depot that is
232 * nearby. This only happens with automatic servicing of vehicles,
233 * so it will only impact performance when you do not manually set
234 * depot orders and you do not disable automatic servicing.
235 */
236 if (max_penalty != 0) pf1.DisableCache(true);
237 FindDepotData result1 = pf1.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_penalty, reverse_penalty);
238
239 if (_debug_desync_level >= 2) {
240 Tpf pf2;
241 pf2.DisableCache(true);
242 FindDepotData result2 = pf2.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_penalty, reverse_penalty);
243 if (result1.tile != result2.tile || (result1.reverse != result2.reverse)) {
244 Debug(desync, 2, "CACHE ERROR: FindNearestDepotTwoWay() = [{}, {}]",
245 result1.tile != INVALID_TILE ? "T" : "F",
246 result2.tile != INVALID_TILE ? "T" : "F");
247 DumpState(pf1, pf2);
248 }
249 }
250
251 return result1;
252 }
253
254 inline FindDepotData FindNearestDepotTwoWay(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_penalty, int reverse_penalty)
255 {
256 /* set origin and destination nodes */
257 Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty, true);
258 Yapf().SetDestination(v);
259 Yapf().SetMaxCost(max_penalty);
260
261 /* find the best path */
262 if (!Yapf().FindPath(v)) return FindDepotData();
263
264 /* Some path found. */
265 Node *n = Yapf().GetBestNode();
266
267 /* walk through the path back to the origin */
268 Node *pNode = n;
269 while (pNode->m_parent != nullptr) {
270 pNode = pNode->m_parent;
271 }
272
273 /* if the origin node is our front vehicle tile/Trackdir then we didn't reverse
274 * but we can also look at the cost (== 0 -> not reversed, == reverse_penalty -> reversed) */
275 return FindDepotData(n->GetLastTile(), n->m_cost, pNode->m_cost != 0);
276 }
277 };
278
279 template <class Types>
280 class CYapfFollowAnySafeTileRailT : public CYapfReserveTrack<Types>
281 {
282 public:
283 typedef typename Types::Tpf Tpf;
284 typedef typename Types::TrackFollower TrackFollower;
285 typedef typename Types::NodeList::Titem Node;
286 typedef typename Node::Key Key;
287
288 protected:
290 inline Tpf &Yapf()
291 {
292 return *static_cast<Tpf *>(this);
293 }
294
295 public:
301 inline void PfFollowNode(Node &old_node)
302 {
303 TrackFollower F(Yapf().GetVehicle(), Yapf().GetCompatibleRailTypes());
304 if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir()) && F.MaskReservedTracks()) {
305 Yapf().AddMultipleNodes(&old_node, F);
306 }
307 }
308
310 inline char TransportTypeChar() const
311 {
312 return 't';
313 }
314
315 static bool stFindNearestSafeTile(const Train *v, TileIndex t1, Trackdir td, bool override_railtype)
316 {
317 /* Create pathfinder instance */
318 Tpf pf1;
319 bool result1;
320 if (_debug_desync_level < 2) {
321 result1 = pf1.FindNearestSafeTile(v, t1, td, override_railtype, false);
322 } else {
323 bool result2 = pf1.FindNearestSafeTile(v, t1, td, override_railtype, true);
324 Tpf pf2;
325 pf2.DisableCache(true);
326 result1 = pf2.FindNearestSafeTile(v, t1, td, override_railtype, false);
327 if (result1 != result2) {
328 Debug(desync, 2, "CACHE ERROR: FindSafeTile() = [{}, {}]", result2 ? "T" : "F", result1 ? "T" : "F");
329 DumpState(pf1, pf2);
330 }
331 }
332
333 return result1;
334 }
335
336 bool FindNearestSafeTile(const Train *v, TileIndex t1, Trackdir td, bool override_railtype, bool dont_reserve)
337 {
338 /* Set origin and destination. */
339 Yapf().SetOrigin(t1, td);
340 Yapf().SetDestination(v, override_railtype);
341
342 bool bFound = Yapf().FindPath(v);
343 if (!bFound) return false;
344
345 /* Found a destination, set as reservation target. */
346 Node *pNode = Yapf().GetBestNode();
347 this->SetReservationTarget(pNode, pNode->GetLastTile(), pNode->GetLastTrackdir());
348
349 /* Walk through the path back to the origin. */
350 Node *pPrev = nullptr;
351 while (pNode->m_parent != nullptr) {
352 pPrev = pNode;
353 pNode = pNode->m_parent;
354
355 this->FindSafePositionOnNode(pPrev);
356 }
357
358 return dont_reserve || this->TryReservePath(nullptr, pNode->GetLastTile());
359 }
360 };
361
362 template <class Types>
363 class CYapfFollowRailT : public CYapfReserveTrack<Types>
364 {
365 public:
366 typedef typename Types::Tpf Tpf;
367 typedef typename Types::TrackFollower TrackFollower;
368 typedef typename Types::NodeList::Titem Node;
369 typedef typename Node::Key Key;
370
371 protected:
373 inline Tpf &Yapf()
374 {
375 return *static_cast<Tpf *>(this);
376 }
377
378 public:
384 inline void PfFollowNode(Node &old_node)
385 {
386 TrackFollower F(Yapf().GetVehicle());
387 if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir())) {
388 Yapf().AddMultipleNodes(&old_node, F);
389 }
390 }
391
393 inline char TransportTypeChar() const
394 {
395 return 't';
396 }
397
398 static Trackdir stChooseRailTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, PBSTileInfo *target, TileIndex *dest)
399 {
400 /* create pathfinder instance */
401 Tpf pf1;
402 Trackdir result1;
403
404 if (_debug_desync_level < 2) {
405 result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_found, reserve_track, target, dest);
406 } else {
407 result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_found, false, nullptr, nullptr);
408 Tpf pf2;
409 pf2.DisableCache(true);
410 Trackdir result2 = pf2.ChooseRailTrack(v, tile, enterdir, tracks, path_found, reserve_track, target, dest);
411 if (result1 != result2) {
412 Debug(desync, 2, "CACHE ERROR: ChooseRailTrack() = [{}, {}]", result1, result2);
413 DumpState(pf1, pf2);
414 }
415 }
416
417 return result1;
418 }
419
420 inline Trackdir ChooseRailTrack(const Train *v, TileIndex, DiagDirection, TrackBits, bool &path_found, bool reserve_track, PBSTileInfo *target, TileIndex *dest)
421 {
422 if (target != nullptr) target->tile = INVALID_TILE;
423 if (dest != nullptr) *dest = INVALID_TILE;
424
425 /* set origin and destination nodes */
426 PBSTileInfo origin = FollowTrainReservation(v);
427 Yapf().SetOrigin(origin.tile, origin.trackdir, INVALID_TILE, INVALID_TRACKDIR, 1, true);
428 Yapf().SetDestination(v);
429
430 /* find the best path */
431 path_found = Yapf().FindPath(v);
432
433 /* if path not found - return INVALID_TRACKDIR */
434 Trackdir next_trackdir = INVALID_TRACKDIR;
435 Node *pNode = Yapf().GetBestNode();
436 if (pNode != nullptr) {
437 /* reserve till end of path */
438 this->SetReservationTarget(pNode, pNode->GetLastTile(), pNode->GetLastTrackdir());
439
440 /* path was found or at least suggested
441 * walk through the path back to the origin */
442 Node *pPrev = nullptr;
443 while (pNode->m_parent != nullptr) {
444 pPrev = pNode;
445 pNode = pNode->m_parent;
446
447 this->FindSafePositionOnNode(pPrev);
448 }
449 /* return trackdir from the best origin node (one of start nodes) */
450 Node &best_next_node = *pPrev;
451 next_trackdir = best_next_node.GetTrackdir();
452
453 if (reserve_track && path_found) {
454 if (dest != nullptr) *dest = Yapf().GetBestNode()->GetLastTile();
455 this->TryReservePath(target, pNode->GetLastTile());
456 }
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() = [{}, {}]", 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 != nullptr) {
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>
509 struct CYapfRail_TypesT
510 {
511 typedef CYapfRail_TypesT<Tpf_, Ttrack_follower, Tnode_list, TdestinationT, TfollowT> Types;
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;
519 typedef CYapfOriginTileTwoWayT<Types> PfOrigin;
520 typedef TdestinationT<Types> PfDestination;
521 typedef CYapfSegmentCostCacheGlobalT<Types> PfCache;
522 typedef CYapfCostRailT<Types> PfCost;
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, TileIndex *dest)
536 {
537 /* default is YAPF type 2 */
538 typedef Trackdir (*PfnChooseRailTrack)(const Train*, TileIndex, DiagDirection, TrackBits, bool&, bool, PBSTileInfo*, TileIndex*);
539 PfnChooseRailTrack pfnChooseRailTrack = &CYapfRail1::stChooseRailTrack;
540
541 /* check if non-default YAPF type needed */
542 if (_settings_game.pf.forbid_90_deg) {
543 pfnChooseRailTrack = &CYapfRail2::stChooseRailTrack; // Trackdir, forbid 90-deg
544 }
545
546 Trackdir td_ret = pfnChooseRailTrack(v, tile, enterdir, tracks, path_found, reserve_track, target, dest);
547 return (td_ret != INVALID_TRACKDIR) ? TrackdirToTrack(td_ret) : FindFirstTrack(tracks);
548 }
549
550 bool YapfTrainCheckReverse(const Train *v)
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 */
597 if (_settings_game.pf.forbid_90_deg) {
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
609 FindDepotData YapfTrainFindNearestDepot(const Train *v, int max_penalty)
610 {
611 const Train *last_veh = v->Last();
612
613 PBSTileInfo origin = FollowTrainReservation(v);
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 */
621 if (_settings_game.pf.forbid_90_deg) {
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 */
634 if (_settings_game.pf.forbid_90_deg) {
635 pfnFindNearestSafeTile = &CYapfAnySafeTileRail2::stFindNearestSafeTile;
636 }
637
638 return pfnFindNearestSafeTile(v, tile, td, override_railtype);
639 }
640
642 int CSegmentCostCacheBase::s_rail_change_counter = 0;
643
644 void YapfNotifyTrackLayoutChange(TileIndex tile, Track track)
645 {
646 CSegmentCostCacheBase::NotifyTrackLayoutChange(tile, track);
647 }
PBSTileInfo::okay
bool okay
True if tile is a safe waiting position, false otherwise.
Definition: pbs.h:29
TRACK_BIT_WORMHOLE
@ TRACK_BIT_WORMHOLE
Bitflag for a wormhole (used for tunnels)
Definition: track_type.h:52
CYapfReserveTrack::m_res_dest_td
Trackdir m_res_dest_td
The reservation target trackdir.
Definition: yapf_rail.cpp:54
TILE_ADD
#define TILE_ADD(x, y)
Adds two tiles together.
Definition: map_func.h:466
FindFirstTrack
Track FindFirstTrack(TrackBits tracks)
Returns first Track from TrackBits or INVALID_TRACK.
Definition: track_func.h:177
CYapfFollowAnyDepotRailT::Node
Types::NodeList::Titem Node
this will be our node type
Definition: yapf_rail.cpp:194
yapf_destrail.hpp
CYapfReserveTrack::Yapf
Tpf & Yapf()
to access inherited pathfinder
Definition: yapf_rail.cpp:47
CYapfFollowAnySafeTileRailT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition: yapf_rail.cpp:290
FindDepotData
Helper container to find a depot.
Definition: pathfinder_type.h:51
PathfinderSettings::forbid_90_deg
bool forbid_90_deg
forbid trains to make 90 deg turns
Definition: settings_type.h:490
CYapfReserveTrack::ReserveRailStationPlatform
bool ReserveRailStationPlatform(TileIndex &tile, DiagDirection dir)
Reserve a railway platform.
Definition: yapf_rail.cpp:71
CYapfReserveTrack::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition: yapf_rail.cpp:41
CYapfReserveTrack::m_res_node
Node * m_res_node
The reservation target node.
Definition: yapf_rail.cpp:55
YapfNotifyTrackLayoutChange
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
TrackdirToTrack
Track TrackdirToTrack(Trackdir trackdir)
Returns the Track that a given Trackdir represents.
Definition: track_func.h:262
TrackdirToExitdir
DiagDirection TrackdirToExitdir(Trackdir trackdir)
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
Definition: track_func.h:439
yapf.hpp
FindDepotData::reverse
bool reverse
True if reversing is necessary for the train to get to this depot.
Definition: pathfinder_type.h:54
CYapfFollowRailT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition: yapf_rail.cpp:366
FollowTrainReservation
PBSTileInfo FollowTrainReservation(const Train *v, Vehicle **train_on_res)
Follow a train reservation to the last tile.
Definition: pbs.cpp:288
CYapfReserveTrack::FindSafePositionOnNode
void FindSafePositionOnNode(Node *node)
Check the node for a possible reservation target.
Definition: yapf_rail.cpp:135
yapf_node_rail.hpp
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:95
IsCompatibleTrainStationTile
bool IsCompatibleTrainStationTile(Tile test_tile, Tile station_tile)
Check if a tile is a valid continuation to a railstation tile.
Definition: station_map.h:451
IsRailStationTile
bool IsRailStationTile(Tile t)
Is this tile a station tile and a rail station?
Definition: station_map.h:102
CYapfCostRailT
Definition: yapf_costrail.hpp:17
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
CYapfT
YAPF template that uses Ttypes template argument to determine all YAPF components (base classes) from...
Definition: yapf_common.hpp:183
CYapfFollowAnySafeTileRailT::PfFollowNode
void PfFollowNode(Node &old_node)
Called by YAPF to move from the given node to the next tile.
Definition: yapf_rail.cpp:301
CYapfFollowAnyDepotRailT
Definition: yapf_rail.cpp:189
CYapfFollowAnyDepotRailT::TransportTypeChar
char TransportTypeChar() const
return debug report character to identify the transportation type
Definition: yapf_rail.cpp:219
CYapfReserveTrack::m_res_fail_td
Trackdir m_res_fail_td
The trackdir where the reservation failed.
Definition: yapf_rail.cpp:57
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
TryReserveRailTrack
bool TryReserveRailTrack(TileIndex tile, Track t, bool trigger_stations)
Try to reserve a specific track on a tile.
Definition: pbs.cpp:80
Vehicle::x_pos
int32_t x_pos
x coordinate.
Definition: vehicle_base.h:299
CYapfFollowRailT::Key
Node::Key Key
key to hash tables
Definition: yapf_rail.cpp:369
CYapfFollowRailT::TransportTypeChar
char TransportTypeChar() const
return debug report character to identify the transportation type
Definition: yapf_rail.cpp:393
GameSettings::pf
PathfinderSettings pf
settings for all pathfinders
Definition: settings_type.h:625
DistanceManhattan
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition: map.cpp:159
UnreserveRailTrack
void UnreserveRailTrack(TileIndex tile, Track t)
Lift the reservation of a specific track on a tile.
Definition: pbs.cpp:140
IsWaitingPositionFree
bool IsWaitingPositionFree(const Train *v, TileIndex tile, Trackdir trackdir, bool forbid_90deg)
Check if a safe position is free.
Definition: pbs.cpp:426
CYapfOriginTileTwoWayT
YAPF origin provider base class - used when there are two tile/trackdir origins.
Definition: yapf_common.hpp:55
CYapfReserveTrack::m_origin_tile
TileIndex m_origin_tile
Tile our reservation will originate from.
Definition: yapf_rail.cpp:58
Train::GetVehicleTrackdir
Trackdir GetVehicleTrackdir() const override
Get the tracks of the train vehicle.
Definition: train_cmd.cpp:4220
CYapfAnySafeTileRail2
Definition: yapf_rail.cpp:532
YAPF_TILE_LENGTH
static const int YAPF_TILE_LENGTH
Length (penalty) of one tile with YAPF.
Definition: pathfinder_type.h:29
Vehicle::tile
TileIndex tile
Current tile index.
Definition: vehicle_base.h:260
CYapfRail_TypesT
Definition: yapf_rail.cpp:509
PBSTileInfo
This struct contains information about the end of a reserved path.
Definition: pbs.h:26
CYapfFollowAnySafeTileRailT::Key
Node::Key Key
key to hash tables
Definition: yapf_rail.cpp:286
PBSTileInfo::trackdir
Trackdir trackdir
The reserved trackdir on the tile.
Definition: pbs.h:28
CYapfSegmentCostCacheGlobalT
CYapfSegmentCostCacheGlobalT - the yapf cost cache provider that adds the segment cost caching functi...
Definition: yapf_costcache.hpp:161
ReverseTrackdir
Trackdir ReverseTrackdir(Trackdir trackdir)
Maps a trackdir to the reverse trackdir.
Definition: track_func.h:247
DumpTarget
Class that represents the dump-into-string target.
Definition: dbg_helpers.h:95
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:55
CYapfRail1
Definition: yapf_rail.cpp:525
CYapfAnyDepotRail2
Definition: yapf_rail.cpp:529
Train
'Train' is either a loco or a wagon.
Definition: train.h:89
CYapfReserveTrack::UnreserveSingleTrack
bool UnreserveSingleTrack(TileIndex tile, Trackdir td)
Unreserve a single track/platform.
Definition: yapf_rail.cpp:110
CYapfReserveTrack::SetReservationTarget
void SetReservationTarget(Node *node, TileIndex tile, Trackdir td)
Set the target to where the reservation should be extended.
Definition: yapf_rail.cpp:127
CYapfReserveTrack::TryReservePath
bool TryReservePath(PBSTileInfo *target, TileIndex origin)
Try to reserve the path till the reservation target.
Definition: yapf_rail.cpp:148
IsSafeWaitingPosition
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:380
CYapfFollowAnySafeTileRailT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition: yapf_rail.cpp:283
INVALID_TRACKDIR
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition: track_type.h:86
TileIndexDiff
int32_t TileIndexDiff
An offset value between two tiles.
Definition: map_func.h:376
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:73
SetRailStationReservation
void SetRailStationReservation(Tile t, bool b)
Set the reservation state of the rail station.
Definition: station_map.h:478
YapfTrainFindNearestDepot
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
CYapfFollowAnySafeTileRailT::TransportTypeChar
char TransportTypeChar() const
Return debug report character to identify the transportation type.
Definition: yapf_rail.cpp:310
CSegmentCostCacheBase::s_rail_change_counter
static int s_rail_change_counter
if any track changes, this counter is incremented - that will invalidate segment cost cache
Definition: yapf_costcache.hpp:102
yapf_cache.h
CYapfReserveTrack::m_res_fail_tile
TileIndex m_res_fail_tile
The tile where the reservation failed.
Definition: yapf_rail.cpp:56
TileOffsByDiagDir
TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:563
CYapfFollowAnyDepotRailT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition: yapf_rail.cpp:192
CYapfAnySafeTileRail1
Definition: yapf_rail.cpp:531
CYapfFollowRailT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition: yapf_rail.cpp:373
CYapfReserveTrack::m_res_dest
TileIndex m_res_dest
The reservation target tile.
Definition: yapf_rail.cpp:53
PBSTileInfo::tile
TileIndex tile
Tile the path ends, INVALID_TILE if no valid path was found.
Definition: pbs.h:27
YapfTrainChooseTrack
Track YapfTrainChooseTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, PBSTileInfo *target, TileIndex *dest)
Finds the best path for given train using YAPF.
Definition: yapf_rail.cpp:535
CYapfRail2
Definition: yapf_rail.cpp:526
HasStationReservation
bool HasStationReservation(Tile t)
Get the reservation state of the rail station.
Definition: station_map.h:466
CYapfAnyDepotRail1
Definition: yapf_rail.cpp:528
MarkTileDirtyByTile
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:2051
DumpTarget::m_out
std::string m_out
the output string
Definition: dbg_helpers.h:118
CYapfReserveTrack
Definition: yapf_rail.cpp:38
CYapfFollowAnyDepotRailT::PfFollowNode
void PfFollowNode(Node &old_node)
Called by YAPF to move from the given node to the next tile.
Definition: yapf_rail.cpp:210
FindDepotData::tile
TileIndex tile
The tile of the depot.
Definition: pathfinder_type.h:52
YAPF_INFINITE_PENALTY
static const int YAPF_INFINITE_PENALTY
This penalty is the equivalent of "infinite", which means that paths that get this penalty will be ch...
Definition: pathfinder_type.h:40
TileIndex
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition: tile_type.h:87
YapfTrainCheckReverse
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
GetOtherTunnelBridgeEnd
TileIndex GetOtherTunnelBridgeEnd(Tile t)
Determines type of the wormhole and returns its other end.
Definition: tunnelbridge_map.h:78
Vehicle::y_pos
int32_t y_pos
y coordinate.
Definition: vehicle_base.h:300
CYapfFollowRailT
Definition: yapf_rail.cpp:363
TriggerStationRandomisation
void TriggerStationRandomisation(Station *st, TileIndex trigger_tile, StationRandomTrigger trigger, CargoID cargo_type)
Trigger station randomisation.
Definition: newgrf_station.cpp:923
TrackBits
TrackBits
Allow incrementing of Track variables.
Definition: track_type.h:35
SRT_PATH_RESERVATION
@ SRT_PATH_RESERVATION
Trigger platform when train reserves path.
Definition: newgrf_station.h:108
CYapfFollowAnySafeTileRailT
Definition: yapf_rail.cpp:280
CYapfFollowRailT::PfFollowNode
void PfFollowNode(Node &old_node)
Called by YAPF to move from the given node to the next tile.
Definition: yapf_rail.cpp:384
TileVirtXY
static debug_inline TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition: map_func.h:416
CYapfFollowAnySafeTileRailT::Node
Types::NodeList::Titem Node
this will be our node type
Definition: yapf_rail.cpp:285
Trackdir
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:67
CYapfBaseT
CYapfBaseT - A-star type path finder base class.
Definition: yapf_base.hpp:47
CYapfFollowRailT::Node
Types::NodeList::Titem Node
this will be our node type
Definition: yapf_rail.cpp:368
LinkGraph::BaseNode
Node of the link graph.
Definition: linkgraph.h:90
Track
Track
These are used to specify a single track.
Definition: track_type.h:19
CYapfFollowAnyDepotRailT::Key
Node::Key Key
key to hash tables
Definition: yapf_rail.cpp:195
SpecializedVehicle::Last
T * Last()
Get the last vehicle in the chain.
Definition: vehicle_base.h:1104
CYapfFollowAnyDepotRailT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition: yapf_rail.cpp:199
CYapfReserveTrack::Node
Types::NodeList::Titem Node
this will be our node type
Definition: yapf_rail.cpp:43
YapfTrainFindNearestSafeTile
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
yapf_costrail.hpp
CYapfReserveTrack::ReserveSingleTrack
bool ReserveSingleTrack(TileIndex tile, Trackdir td)
Try to reserve a single track/platform.
Definition: yapf_rail.cpp:89
GetTunnelBridgeDirection
DiagDirection GetTunnelBridgeDirection(Tile t)
Get the direction pointing to the other end.
Definition: tunnelbridge_map.h:26
INVALID_TRACK
@ INVALID_TRACK
Flag for an invalid track.
Definition: track_type.h:28