# yapf_rail.cpp

Go to the documentation of this file.

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