# yapf_rail.cpp

Go to the documentation of this file.

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