# yapf_rail.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "../../stdafx.h"
    00013
    00014 #include "yapf.hpp"
    00015 #include "yapf_cache.h"
    00016 #include "yapf_node_rail.hpp"
    00017 #include "yapf_costrail.hpp"
    00018 #include "yapf_destrail.hpp"
    00019 #include "../../functions.h"
    00020
    00021 #define DEBUG_YAPF_CACHE 0
    00022
    00023 #if DEBUG_YAPF_CACHE
    00024 template <typename Tpf> void DumpState(Tpf &pf1, Tpf &pf2)
    00025 {
    00026   DumpTarget dmp1, dmp2;
    00027   pf1.DumpBase(dmp1);
    00028   pf2.DumpBase(dmp2);
    00029   FILE *f1 = fopen("yapf1.txt", "wt");
    00030   FILE *f2 = fopen("yapf2.txt", "wt");
    00031   fwrite(dmp1.m_out.Data(), 1, dmp1.m_out.Size(), f1);
    00032   fwrite(dmp2.m_out.Data(), 1, dmp2.m_out.Size(), f2);
    00033   fclose(f1);
    00034   fclose(f2);
    00035 }
    00036 #endif
    00037
    00038 int _total_pf_time_us = 0;
    00039
    00040 template <class Types>
    00041 class CYapfReserveTrack
    00042 {
    00043 public:
    00044   typedef typename Types::Tpf Tpf;
    00045   typedef typename Types::TrackFollower TrackFollower;
    00046   typedef typename Types::NodeList::Titem Node;
    00047
    00048 protected:
    00050   FORCEINLINE Tpf& Yapf()
    00051   {
    00052     return *static_cast<Tpf*>(this);
    00053   }
    00054
    00055 private:
    00056   TileIndex m_res_dest;
    00057   Trackdir  m_res_dest_td;
    00058   Node      *m_res_node;
    00059   TileIndex m_res_fail_tile;
    00060   Trackdir  m_res_fail_td;
    00061
    00062   bool FindSafePositionProc(TileIndex tile, Trackdir td)
    00063   {
    00064     if (IsSafeWaitingPosition(Yapf().GetVehicle(), tile, td, true, !TrackFollower::Allow90degTurns())) {
    00065       m_res_dest = tile;
    00066       m_res_dest_td = td;
    00067       return false;   // Stop iterating segment
    00068     }
    00069     return true;
    00070   }
    00071
    00073   bool ReserveRailStationPlatform(TileIndex &tile, DiagDirection dir)
    00074   {
    00075     TileIndex     start = tile;
    00076     TileIndexDiff diff = TileOffsByDiagDir(dir);
    00077
    00078     do {
    00079       if (HasStationReservation(tile)) return false;
    00080       SetRailStationReservation(tile, true);
    00081       MarkTileDirtyByTile(tile);
    00082       tile = TILE_ADD(tile, diff);
    00083     } while (IsCompatibleTrainStationTile(tile, start));
    00084
    00085     return true;
    00086   }
    00087
    00089   bool ReserveSingleTrack(TileIndex tile, Trackdir td)
    00090   {
    00091     if (IsRailStationTile(tile)) {
    00092       if (!ReserveRailStationPlatform(tile, TrackdirToExitdir(ReverseTrackdir(td)))) {
    00093         /* Platform could not be reserved, undo. */
    00094         m_res_fail_tile = tile;
    00095         m_res_fail_td = td;
    00096       }
    00097     } else {
    00098       if (!TryReserveRailTrack(tile, TrackdirToTrack(td))) {
    00099         /* Tile couldn't be reserved, undo. */
    00100         m_res_fail_tile = tile;
    00101         m_res_fail_td = td;
    00102         return false;
    00103       }
    00104     }
    00105
    00106     return tile != m_res_dest || td != m_res_dest_td;
    00107   }
    00108
    00110   bool UnreserveSingleTrack(TileIndex tile, Trackdir td)
    00111   {
    00112     if (IsRailStationTile(tile)) {
    00113       TileIndex     start = tile;
    00114       TileIndexDiff diff = TileOffsByDiagDir(TrackdirToExitdir(ReverseTrackdir(td)));
    00115       while ((tile != m_res_fail_tile || td != m_res_fail_td) && IsCompatibleTrainStationTile(tile, start)) {
    00116         SetRailStationReservation(tile, false);
    00117         tile = TILE_ADD(tile, diff);
    00118       }
    00119     } else if (tile != m_res_fail_tile || td != m_res_fail_td) {
    00120       UnreserveRailTrack(tile, TrackdirToTrack(td));
    00121     }
    00122     return (tile != m_res_dest || td != m_res_dest_td) && (tile != m_res_fail_tile || td != m_res_fail_td);
    00123   }
    00124
    00125 public:
    00127   inline void SetReservationTarget(Node *node, TileIndex tile, Trackdir td)
    00128   {
    00129     m_res_node = node;
    00130     m_res_dest = tile;
    00131     m_res_dest_td = td;
    00132   }
    00133
    00135   inline void FindSafePositionOnNode(Node *node)
    00136   {
    00137     assert(node->m_parent != NULL);
    00138
    00139     /* We will never pass more than two signals, no need to check for a safe tile. */
    00140     if (node->m_parent->m_num_signals_passed >= 2) return;
    00141
    00142     if (!node->IterateTiles(Yapf().GetVehicle(), Yapf(), *this, &CYapfReserveTrack<Types>::FindSafePositionProc)) {
    00143       m_res_node = node;
    00144     }
    00145   }
    00146
    00148   bool TryReservePath(PBSTileInfo *target)
    00149   {
    00150     m_res_fail_tile = INVALID_TILE;
    00151
    00152     if (target != NULL) {
    00153       target->tile = m_res_dest;
    00154       target->trackdir = m_res_dest_td;
    00155       target->okay = false;
    00156     }
    00157
    00158     /* Don't bother if the target is reserved. */
    00159     if (!IsWaitingPositionFree(Yapf().GetVehicle(), m_res_dest, m_res_dest_td)) return false;
    00160
    00161     for (Node *node = m_res_node; node->m_parent != NULL; node = node->m_parent) {
    00162       node->IterateTiles(Yapf().GetVehicle(), Yapf(), *this, &CYapfReserveTrack<Types>::ReserveSingleTrack);
    00163       if (m_res_fail_tile != INVALID_TILE) {
    00164         /* Reservation failed, undo. */
    00165         Node *fail_node = m_res_node;
    00166         TileIndex stop_tile = m_res_fail_tile;
    00167         do {
    00168           /* If this is the node that failed, stop at the failed tile. */
    00169           m_res_fail_tile = fail_node == node ? stop_tile : INVALID_TILE;
    00170           fail_node->IterateTiles(Yapf().GetVehicle(), Yapf(), *this, &CYapfReserveTrack<Types>::UnreserveSingleTrack);
    00171         } while (fail_node != node && (fail_node = fail_node->m_parent) != NULL);
    00172
    00173         return false;
    00174       }
    00175     }
    00176
    00177     if (target != NULL) target->okay = true;
    00178
    00179     if (Yapf().CanUseGlobalCache(*m_res_node))
    00180       YapfNotifyTrackLayoutChange(INVALID_TILE, INVALID_TRACK);
    00181
    00182     return true;
    00183   }
    00184 };
    00185
    00186 template <class Types>
    00187 class CYapfFollowAnyDepotRailT
    00188 {
    00189 public:
    00190   typedef typename Types::Tpf Tpf;
    00191   typedef typename Types::TrackFollower TrackFollower;
    00192   typedef typename Types::NodeList::Titem Node;
    00193   typedef typename Node::Key Key;
    00194
    00195 protected:
    00197   FORCEINLINE Tpf& Yapf()
    00198   {
    00199     return *static_cast<Tpf*>(this);
    00200   }
    00201
    00202 public:
    00206   inline void PfFollowNode(Node& old_node)
    00207   {
    00208     TrackFollower F(Yapf().GetVehicle());
    00209     if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir())) {
    00210       Yapf().AddMultipleNodes(&old_node, F);
    00211     }
    00212   }
    00213
    00215   FORCEINLINE char TransportTypeChar() const
    00216   {
    00217     return 't';
    00218   }
    00219
    00220   static bool stFindNearestDepotTwoWay(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_penalty, int reverse_penalty, TileIndex *depot_tile, bool *reversed)
    00221   {
    00222     Tpf pf1;
    00223     /*
    00224      * With caching enabled it simply cannot get a reliable result when you
    00225      * have limited the distance a train may travel. This means that the
    00226      * cached result does not match uncached result in all cases and that
    00227      * causes desyncs. So disable caching when finding for a depot that is
    00228      * nearby. This only happens with automatic servicing of vehicles,
    00229      * so it will only impact performance when you do not manually set
    00230      * depot orders and you do not disable automatic servicing.
    00231      */
    00232     if (max_penalty != 0) pf1.DisableCache(true);
    00233     bool result1 = pf1.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_penalty, reverse_penalty, depot_tile, reversed);
    00234
    00235 #if DEBUG_YAPF_CACHE
    00236     Tpf pf2;
    00237     TileIndex depot_tile2 = INVALID_TILE;
    00238     bool reversed2 = false;
    00239     pf2.DisableCache(true);
    00240     bool result2 = pf2.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_penalty, reverse_penalty, &depot_tile2, &reversed2);
    00241     if (result1 != result2 || (result1 && (*depot_tile != depot_tile2 || *reversed != reversed2))) {
    00242       DEBUG(yapf, 0, "CACHE ERROR: FindNearestDepotTwoWay() = [%s, %s]", result1 ? "T" : "F", result2 ? "T" : "F");
    00243       DumpState(pf1, pf2);
    00244     }
    00245 #endif
    00246
    00247     return result1;
    00248   }
    00249
    00250   FORCEINLINE bool FindNearestDepotTwoWay(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_penalty, int reverse_penalty, TileIndex *depot_tile, bool *reversed)
    00251   {
    00252     /* set origin and destination nodes */
    00253     Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty, true);
    00254     Yapf().SetDestination(v);
    00255     Yapf().SetMaxCost(max_penalty);
    00256
    00257     /* find the best path */
    00258     bool bFound = Yapf().FindPath(v);
    00259     if (!bFound) return false;
    00260
    00261     /* some path found
    00262      * get found depot tile */
    00263     Node *n = Yapf().GetBestNode();
    00264     *depot_tile = n->GetLastTile();
    00265
    00266     /* walk through the path back to the origin */
    00267     Node *pNode = n;
    00268     while (pNode->m_parent != NULL) {
    00269       pNode = pNode->m_parent;
    00270     }
    00271
    00272     /* if the origin node is our front vehicle tile/Trackdir then we didn't reverse
    00273      * but we can also look at the cost (== 0 -> not reversed, == reverse_penalty -> reversed) */
    00274     *reversed = (pNode->m_cost != 0);
    00275
    00276     return true;
    00277   }
    00278 };
    00279
    00280 template <class Types>
    00281 class CYapfFollowAnySafeTileRailT : public CYapfReserveTrack<Types>
    00282 {
    00283 public:
    00284   typedef typename Types::Tpf Tpf;
    00285   typedef typename Types::TrackFollower TrackFollower;
    00286   typedef typename Types::NodeList::Titem Node;
    00287   typedef typename Node::Key Key;
    00288
    00289 protected:
    00291   FORCEINLINE Tpf& Yapf()
    00292   {
    00293     return *static_cast<Tpf*>(this);
    00294   }
    00295
    00296 public:
    00300   inline void PfFollowNode(Node& old_node)
    00301   {
    00302     TrackFollower F(Yapf().GetVehicle(), Yapf().GetCompatibleRailTypes());
    00303     if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir()) && F.MaskReservedTracks()) {
    00304       Yapf().AddMultipleNodes(&old_node, F);
    00305     }
    00306   }
    00307
    00309   FORCEINLINE char TransportTypeChar() const
    00310   {
    00311     return 't';
    00312   }
    00313
    00314   static bool stFindNearestSafeTile(const Train *v, TileIndex t1, Trackdir td, bool override_railtype)
    00315   {
    00316     /* Create pathfinder instance */
    00317     Tpf pf1;
    00318 #if !DEBUG_YAPF_CACHE
    00319     bool result1 = pf1.FindNearestSafeTile(v, t1, td, override_railtype, false);
    00320
    00321 #else
    00322     bool result2 = pf1.FindNearestSafeTile(v, t1, td, override_railtype, true);
    00323     Tpf pf2;
    00324     pf2.DisableCache(true);
    00325     bool result1 = pf2.FindNearestSafeTile(v, t1, td, override_railtype, false);
    00326     if (result1 != result2) {
    00327       DEBUG(yapf, 0, "CACHE ERROR: FindSafeTile() = [%s, %s]", result2 ? "T" : "F", result1 ? "T" : "F");
    00328       DumpState(pf1, pf2);
    00329     }
    00330 #endif
    00331
    00332     return result1;
    00333   }
    00334
    00335   bool FindNearestSafeTile(const Train *v, TileIndex t1, Trackdir td, bool override_railtype, bool dont_reserve)
    00336   {
    00337     /* Set origin and destination. */
    00338     Yapf().SetOrigin(t1, td);
    00339     Yapf().SetDestination(v, override_railtype);
    00340
    00341     bool bFound = Yapf().FindPath(v);
    00342     if (!bFound) return false;
    00343
    00344     /* Found a destination, set as reservation target. */
    00345     Node *pNode = Yapf().GetBestNode();
    00346     this->SetReservationTarget(pNode, pNode->GetLastTile(), pNode->GetLastTrackdir());
    00347
    00348     /* Walk through the path back to the origin. */
    00349     Node *pPrev = NULL;
    00350     while (pNode->m_parent != NULL) {
    00351       pPrev = pNode;
    00352       pNode = pNode->m_parent;
    00353
    00354       this->FindSafePositionOnNode(pPrev);
    00355     }
    00356
    00357     return dont_reserve || this->TryReservePath(NULL);
    00358   }
    00359 };
    00360
    00361 template <class Types>
    00362 class CYapfFollowRailT : public CYapfReserveTrack<Types>
    00363 {
    00364 public:
    00365   typedef typename Types::Tpf Tpf;
    00366   typedef typename Types::TrackFollower TrackFollower;
    00367   typedef typename Types::NodeList::Titem Node;
    00368   typedef typename Node::Key Key;
    00369
    00370 protected:
    00372   FORCEINLINE Tpf& Yapf()
    00373   {
    00374     return *static_cast<Tpf*>(this);
    00375   }
    00376
    00377 public:
    00381   inline void PfFollowNode(Node& old_node)
    00382   {
    00383     TrackFollower F(Yapf().GetVehicle());
    00384     if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir())) {
    00385       Yapf().AddMultipleNodes(&old_node, F);
    00386     }
    00387   }
    00388
    00390   FORCEINLINE char TransportTypeChar() const
    00391   {
    00392     return 't';
    00393   }
    00394
    00395   static Trackdir stChooseRailTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found, bool reserve_track, PBSTileInfo *target)
    00396   {
    00397     /* create pathfinder instance */
    00398     Tpf pf1;
    00399 #if !DEBUG_YAPF_CACHE
    00400     Trackdir result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_not_found, reserve_track, target);
    00401
    00402 #else
    00403     Trackdir result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_not_found, false, NULL);
    00404     Tpf pf2;
    00405     pf2.DisableCache(true);
    00406     Trackdir result2 = pf2.ChooseRailTrack(v, tile, enterdir, tracks, path_not_found, reserve_track, target);
    00407     if (result1 != result2) {
    00408       DEBUG(yapf, 0, "CACHE ERROR: ChooseRailTrack() = [%d, %d]", result1, result2);
    00409       DumpState(pf1, pf2);
    00410     }
    00411 #endif
    00412
    00413     return result1;
    00414   }
    00415
    00416   FORCEINLINE Trackdir ChooseRailTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found, bool reserve_track, PBSTileInfo *target)
    00417   {
    00418     if (target != NULL) target->tile = INVALID_TILE;
    00419
    00420     /* set origin and destination nodes */
    00421     PBSTileInfo origin = FollowTrainReservation(v);
    00422     Yapf().SetOrigin(origin.tile, origin.trackdir, INVALID_TILE, INVALID_TRACKDIR, 1, true);
    00423     Yapf().SetDestination(v);
    00424
    00425     /* find the best path */
    00426     bool path_found = Yapf().FindPath(v);
    00427     if (path_not_found != NULL) {
    00428       /* tell controller that the path was only 'guessed'
    00429        * treat the path as found if stopped on the first two way signal(s) */
    00430       *path_not_found = !(path_found || Yapf().m_stopped_on_first_two_way_signal);
    00431     }
    00432
    00433     /* if path not found - return INVALID_TRACKDIR */
    00434     Trackdir next_trackdir = INVALID_TRACKDIR;
    00435     Node *pNode = Yapf().GetBestNode();
    00436     if (pNode != NULL) {
    00437       /* reserve till end of path */
    00438       this->SetReservationTarget(pNode, pNode->GetLastTile(), pNode->GetLastTrackdir());
    00439
    00440       /* path was found or at least suggested
    00441        * walk through the path back to the origin */
    00442       Node *pPrev = NULL;
    00443       while (pNode->m_parent != NULL) {
    00444         pPrev = pNode;
    00445         pNode = pNode->m_parent;
    00446
    00447         this->FindSafePositionOnNode(pPrev);
    00448       }
    00449       /* return trackdir from the best origin node (one of start nodes) */
    00450       Node& best_next_node = *pPrev;
    00451       next_trackdir = best_next_node.GetTrackdir();
    00452
    00453       if (reserve_track && path_found) this->TryReservePath(target);
    00454     }
    00455     return next_trackdir;
    00456   }
    00457
    00458   static bool stCheckReverseTrain(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int reverse_penalty)
    00459   {
    00460     Tpf pf1;
    00461     bool result1 = pf1.CheckReverseTrain(v, t1, td1, t2, td2, reverse_penalty);
    00462
    00463 #if DEBUG_YAPF_CACHE
    00464     Tpf pf2;
    00465     pf2.DisableCache(true);
    00466     bool result2 = pf2.CheckReverseTrain(v, t1, td1, t2, td2, reverse_penalty);
    00467     if (result1 != result2) {
    00468       DEBUG(yapf, 0, "CACHE ERROR: CheckReverseTrain() = [%s, %s]", result1 ? "T" : "F", result2 ? "T" : "F");
    00469       DumpState(pf1, pf2);
    00470     }
    00471 #endif
    00472
    00473     return result1;
    00474   }
    00475
    00476   FORCEINLINE bool CheckReverseTrain(const Train *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int reverse_penalty)
    00477   {
    00478     /* create pathfinder instance
    00479      * set origin and destination nodes */
    00480     Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty, false);
    00481     Yapf().SetDestination(v);
    00482
    00483     /* find the best path */
    00484     bool bFound = Yapf().FindPath(v);
    00485
    00486     if (!bFound) return false;
    00487
    00488     /* path was found
    00489      * walk through the path back to the origin */
    00490     Node *pNode = Yapf().GetBestNode();
    00491     while (pNode->m_parent != NULL) {
    00492       pNode = pNode->m_parent;
    00493     }
    00494
    00495     /* check if it was reversed origin */
    00496     Node& best_org_node = *pNode;
    00497     bool reversed = (best_org_node.m_cost != 0);
    00498     return reversed;
    00499   }
    00500 };
    00501
    00502 template <class Tpf_, class Ttrack_follower, class Tnode_list, template <class Types> class TdestinationT, template <class Types> class TfollowT>
    00503 struct CYapfRail_TypesT
    00504 {
    00505   typedef CYapfRail_TypesT<Tpf_, Ttrack_follower, Tnode_list, TdestinationT, TfollowT>  Types;
    00506
    00507   typedef Tpf_                                Tpf;
    00508   typedef Ttrack_follower                     TrackFollower;
    00509   typedef Tnode_list                          NodeList;
    00510   typedef Train                               VehicleType;
    00511   typedef CYapfBaseT<Types>                   PfBase;
    00512   typedef TfollowT<Types>                     PfFollow;
    00513   typedef CYapfOriginTileTwoWayT<Types>       PfOrigin;
    00514   typedef TdestinationT<Types>                PfDestination;
    00515   typedef CYapfSegmentCostCacheGlobalT<Types> PfCache;
    00516   typedef CYapfCostRailT<Types>               PfCost;
    00517 };
    00518
    00519 struct CYapfRail1         : CYapfT<CYapfRail_TypesT<CYapfRail1        , CFollowTrackRail    , CRailNodeListTrackDir, CYapfDestinationTileOrStationRailT, CYapfFollowRailT> > {};
    00520 struct CYapfRail2         : CYapfT<CYapfRail_TypesT<CYapfRail2        , CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationTileOrStationRailT, CYapfFollowRailT> > {};
    00521
    00522 struct CYapfAnyDepotRail1 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail1, CFollowTrackRail    , CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
    00523 struct CYapfAnyDepotRail2 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail2, CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
    00524
    00525 struct CYapfAnySafeTileRail1 : CYapfT<CYapfRail_TypesT<CYapfAnySafeTileRail1, CFollowTrackFreeRail    , CRailNodeListTrackDir, CYapfDestinationAnySafeTileRailT , CYapfFollowAnySafeTileRailT> > {};
    00526 struct CYapfAnySafeTileRail2 : CYapfT<CYapfRail_TypesT<CYapfAnySafeTileRail2, CFollowTrackFreeRailNo90, CRailNodeListTrackDir, CYapfDestinationAnySafeTileRailT , CYapfFollowAnySafeTileRailT> > {};
    00527
    00528
    00529 Track YapfTrainChooseTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found, bool reserve_track, PBSTileInfo *target)
    00530 {
    00531   /* default is YAPF type 2 */
    00532   typedef Trackdir (*PfnChooseRailTrack)(const Train*, TileIndex, DiagDirection, TrackBits, bool*, bool, PBSTileInfo*);
    00533   PfnChooseRailTrack pfnChooseRailTrack = &CYapfRail1::stChooseRailTrack;
    00534
    00535   /* check if non-default YAPF type needed */
    00536   if (_settings_game.pf.forbid_90_deg) {
    00537     pfnChooseRailTrack = &CYapfRail2::stChooseRailTrack; // Trackdir, forbid 90-deg
    00538   }
    00539
    00540   Trackdir td_ret = pfnChooseRailTrack(v, tile, enterdir, tracks, path_not_found, reserve_track, target);
    00541   return (td_ret != INVALID_TRACKDIR) ? TrackdirToTrack(td_ret) : FindFirstTrack(tracks);
    00542 }
    00543
    00544 bool YapfTrainCheckReverse(const Train *v)
    00545 {
    00546   const Train *last_veh = v->Last();
    00547
    00548   /* get trackdirs of both ends */
    00549   Trackdir td = v->GetVehicleTrackdir();
    00550   Trackdir td_rev = ReverseTrackdir(last_veh->GetVehicleTrackdir());
    00551
    00552   /* tiles where front and back are */
    00553   TileIndex tile = v->tile;
    00554   TileIndex tile_rev = last_veh->tile;
    00555
    00556   int reverse_penalty = 0;
    00557
    00558   if (v->track == TRACK_BIT_WORMHOLE) {
    00559     /* front in tunnel / on bridge */
    00560     DiagDirection dir_into_wormhole = GetTunnelBridgeDirection(tile);
    00561
    00562     if (TrackdirToExitdir(td) == dir_into_wormhole) tile = GetOtherTunnelBridgeEnd(tile);
    00563     /* Now 'tile' is the tunnel entry/bridge ramp the train will reach when driving forward */
    00564
    00565     /* Current position of the train in the wormhole */
    00566     TileIndex cur_tile = TileVirtXY(v->x_pos, v->y_pos);
    00567
    00568     /* Add distance to drive in the wormhole as penalty for the forward path, i.e. bonus for the reverse path
    00569      * Note: Negative penalties are ok for the start tile. */
    00570     reverse_penalty -= DistanceManhattan(cur_tile, tile) * YAPF_TILE_LENGTH;
    00571   }
    00572
    00573   if (last_veh->track == TRACK_BIT_WORMHOLE) {
    00574     /* back in tunnel / on bridge */
    00575     DiagDirection dir_into_wormhole = GetTunnelBridgeDirection(tile_rev);
    00576
    00577     if (TrackdirToExitdir(td_rev) == dir_into_wormhole) tile_rev = GetOtherTunnelBridgeEnd(tile_rev);
    00578     /* Now 'tile_rev' is the tunnel entry/bridge ramp the train will reach when reversing */
    00579
    00580     /* Current position of the last wagon in the wormhole */
    00581     TileIndex cur_tile = TileVirtXY(last_veh->x_pos, last_veh->y_pos);
    00582
    00583     /* Add distance to drive in the wormhole as penalty for the revere path. */
    00584     reverse_penalty += DistanceManhattan(cur_tile, tile_rev) * YAPF_TILE_LENGTH;
    00585   }
    00586
    00587   typedef bool (*PfnCheckReverseTrain)(const Train*, TileIndex, Trackdir, TileIndex, Trackdir, int);
    00588   PfnCheckReverseTrain pfnCheckReverseTrain = CYapfRail1::stCheckReverseTrain;
    00589
    00590   /* check if non-default YAPF type needed */
    00591   if (_settings_game.pf.forbid_90_deg) {
    00592     pfnCheckReverseTrain = &CYapfRail2::stCheckReverseTrain; // Trackdir, forbid 90-deg
    00593   }
    00594
    00595   /* slightly hackish: If the pathfinders finds a path, the cost of the first node is tested to distinguish between forward- and reverse-path. */
    00596   if (reverse_penalty == 0) reverse_penalty = 1;
    00597
    00598   bool reverse = pfnCheckReverseTrain(v, tile, td, tile_rev, td_rev, reverse_penalty);
    00599
    00600   return reverse;
    00601 }
    00602
    00603 FindDepotData YapfTrainFindNearestDepot(const Train *v, int max_penalty)
    00604 {
    00605   FindDepotData fdd;
    00606
    00607   const Train *last_veh = v->Last();
    00608
    00609   PBSTileInfo origin = FollowTrainReservation(v);
    00610   TileIndex last_tile = last_veh->tile;
    00611   Trackdir td_rev = ReverseTrackdir(last_veh->GetVehicleTrackdir());
    00612
    00613   typedef bool (*PfnFindNearestDepotTwoWay)(const Train*, TileIndex, Trackdir, TileIndex, Trackdir, int, int, TileIndex*, bool*);
    00614   PfnFindNearestDepotTwoWay pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail1::stFindNearestDepotTwoWay;
    00615
    00616   /* check if non-default YAPF type needed */
    00617   if (_settings_game.pf.forbid_90_deg) {
    00618     pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail2::stFindNearestDepotTwoWay; // Trackdir, forbid 90-deg
    00619   }
    00620
    00621   bool ret = pfnFindNearestDepotTwoWay(v, origin.tile, origin.trackdir, last_tile, td_rev, max_penalty, YAPF_INFINITE_PENALTY, &fdd.tile, &fdd.reverse);
    00622   fdd.best_length = ret ? max_penalty / 2 : UINT_MAX; // some fake distance or NOT_FOUND
    00623   return fdd;
    00624 }
    00625
    00626 bool YapfTrainFindNearestSafeTile(const Train *v, TileIndex tile, Trackdir td, bool override_railtype)
    00627 {
    00628   typedef bool (*PfnFindNearestSafeTile)(const Train*, TileIndex, Trackdir, bool);
    00629   PfnFindNearestSafeTile pfnFindNearestSafeTile = CYapfAnySafeTileRail1::stFindNearestSafeTile;
    00630
    00631   /* check if non-default YAPF type needed */
    00632   if (_settings_game.pf.forbid_90_deg) {
    00633     pfnFindNearestSafeTile = &CYapfAnySafeTileRail2::stFindNearestSafeTile;
    00634   }
    00635
    00636   return pfnFindNearestSafeTile(v, tile, td, override_railtype);
    00637 }
    00638
    00640 int CSegmentCostCacheBase::s_rail_change_counter = 0;
    00641
    00642 void YapfNotifyTrackLayoutChange(TileIndex tile, Track track)
    00643 {
    00644   CSegmentCostCacheBase::NotifyTrackLayoutChange(tile, track);
    00645 }