# yapf_rail.cpp

Go to the documentation of this file.

    00001 /* $Id: yapf_rail.cpp 15619 2009-03-05 16:14:56Z michi_cc $ */
    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     bool result1 = pf1.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_distance, reverse_penalty, depot_tile, reversed);
    00202
    00203 #if DEBUG_YAPF_CACHE
    00204     Tpf pf2;
    00205     TileIndex depot_tile2 = INVALID_TILE;
    00206     bool reversed2 = false;
    00207     pf2.DisableCache(true);
    00208     bool result2 = pf2.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_distance, reverse_penalty, &depot_tile2, &reversed2);
    00209     if (result1 != result2 || (result1 && (*depot_tile != depot_tile2 || *reversed != reversed2))) {
    00210       DEBUG(yapf, 0, "CACHE ERROR: FindNearestDepotTwoWay() = [%s, %s]", result1 ? "T" : "F", result2 ? "T" : "F");
    00211     }
    00212 #endif
    00213
    00214     return result1;
    00215   }
    00216
    00217   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)
    00218   {
    00219     // set origin and destination nodes
    00220     Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty, true);
    00221     Yapf().SetDestination(v);
    00222     Yapf().SetMaxCost(YAPF_TILE_LENGTH * max_distance);
    00223
    00224     // find the best path
    00225     bool bFound = Yapf().FindPath(v);
    00226     if (!bFound) return false;
    00227
    00228     // some path found
    00229     // get found depot tile
    00230     Node *n = Yapf().GetBestNode();
    00231     *depot_tile = n->GetLastTile();
    00232
    00233     // walk through the path back to the origin
    00234     Node *pNode = n;
    00235     while (pNode->m_parent != NULL) {
    00236       pNode = pNode->m_parent;
    00237     }
    00238
    00239     // if the origin node is our front vehicle tile/Trackdir then we didn't reverse
    00240     // but we can also look at the cost (== 0 -> not reversed, == reverse_penalty -> reversed)
    00241     *reversed = (pNode->m_cost != 0);
    00242
    00243     return true;
    00244   }
    00245 };
    00246
    00247 template <class Types>
    00248 class CYapfFollowAnySafeTileRailT : public CYapfReserveTrack<Types>
    00249 {
    00250 public:
    00251   typedef typename Types::Tpf Tpf;
    00252   typedef typename Types::TrackFollower TrackFollower;
    00253   typedef typename Types::NodeList::Titem Node;
    00254   typedef typename Node::Key Key;
    00255
    00256 protected:
    00258   FORCEINLINE Tpf& Yapf()
    00259   {
    00260     return *static_cast<Tpf*>(this);
    00261   }
    00262
    00263 public:
    00267   inline void PfFollowNode(Node& old_node)
    00268   {
    00269     TrackFollower F(Yapf().GetVehicle(), Yapf().GetCompatibleRailTypes());
    00270     if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir()) && F.MaskReservedTracks()) {
    00271       Yapf().AddMultipleNodes(&old_node, F);
    00272     }
    00273   }
    00274
    00276   FORCEINLINE char TransportTypeChar() const
    00277   {
    00278     return 't';
    00279   }
    00280
    00281   static bool stFindNearestSafeTile(const Vehicle *v, TileIndex t1, Trackdir td, bool override_railtype)
    00282   {
    00283     /* Create pathfinder instance */
    00284     Tpf pf1;
    00285 #if !DEBUG_YAPF_CACHE
    00286     bool result1 = pf1.FindNearestSafeTile(v, t1, td, override_railtype, false);
    00287
    00288 #else
    00289     bool result2 = pf1.FindNearestSafeTile(v, t1, td, override_railtype, true);
    00290     Tpf pf2;
    00291     pf2.DisableCache(true);
    00292     bool result1 = pf2.FindNearestSafeTile(v, t1, td, override_railtype, false);
    00293     if (result1 != result2) {
    00294       DEBUG(yapf, 0, "CACHE ERROR: FindSafeTile() = [%s, %s]", result2 ? "T" : "F", result1 ? "T" : "F");
    00295       DumpTarget dmp1, dmp2;
    00296       pf1.DumpBase(dmp1);
    00297       pf2.DumpBase(dmp2);
    00298       FILE *f1 = fopen("C:\\yapf1.txt", "wt");
    00299       FILE *f2 = fopen("C:\\yapf2.txt", "wt");
    00300       fwrite(dmp1.m_out.Data(), 1, dmp1.m_out.Size(), f1);
    00301       fwrite(dmp2.m_out.Data(), 1, dmp2.m_out.Size(), f2);
    00302       fclose(f1);
    00303       fclose(f2);
    00304     }
    00305 #endif
    00306
    00307     return result1;
    00308   }
    00309
    00310   bool FindNearestSafeTile(const Vehicle *v, TileIndex t1, Trackdir td, bool override_railtype, bool dont_reserve)
    00311   {
    00312     /* Set origin and destination. */
    00313     Yapf().SetOrigin(t1, td);
    00314     Yapf().SetDestination(v, override_railtype);
    00315
    00316     bool bFound = Yapf().FindPath(v);
    00317     if (!bFound) return false;
    00318
    00319     /* Found a destination, set as reservation target. */
    00320     Node *pNode = Yapf().GetBestNode();
    00321     this->SetReservationTarget(pNode, pNode->GetLastTile(), pNode->GetLastTrackdir());
    00322
    00323     /* Walk through the path back to the origin. */
    00324     Node *pPrev = NULL;
    00325     while (pNode->m_parent != NULL) {
    00326       pPrev = pNode;
    00327       pNode = pNode->m_parent;
    00328
    00329       this->FindSafePositionOnNode(pPrev);
    00330     }
    00331
    00332     return dont_reserve || this->TryReservePath(NULL);
    00333   }
    00334 };
    00335
    00336 template <class Types>
    00337 class CYapfFollowRailT : public CYapfReserveTrack<Types>
    00338 {
    00339 public:
    00340   typedef typename Types::Tpf Tpf;
    00341   typedef typename Types::TrackFollower TrackFollower;
    00342   typedef typename Types::NodeList::Titem Node;
    00343   typedef typename Node::Key Key;
    00344
    00345 protected:
    00347   FORCEINLINE Tpf& Yapf()
    00348   {
    00349     return *static_cast<Tpf*>(this);
    00350   }
    00351
    00352 public:
    00356   inline void PfFollowNode(Node& old_node)
    00357   {
    00358     TrackFollower F(Yapf().GetVehicle());
    00359     if (F.Follow(old_node.GetLastTile(), old_node.GetLastTrackdir())) {
    00360       Yapf().AddMultipleNodes(&old_node, F);
    00361     }
    00362   }
    00363
    00365   FORCEINLINE char TransportTypeChar() const
    00366   {
    00367     return 't';
    00368   }
    00369
    00370   static Trackdir stChooseRailTrack(const Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found, bool reserve_track, PBSTileInfo *target)
    00371   {
    00372     // create pathfinder instance
    00373     Tpf pf1;
    00374 #if !DEBUG_YAPF_CACHE
    00375     Trackdir result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_not_found, reserve_track, target);
    00376
    00377 #else
    00378     Trackdir result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_not_found, false, NULL);
    00379     Tpf pf2;
    00380     pf2.DisableCache(true);
    00381     Trackdir result2 = pf2.ChooseRailTrack(v, tile, enterdir, tracks, path_not_found, reserve_track, target);
    00382     if (result1 != result2) {
    00383       DEBUG(yapf, 0, "CACHE ERROR: ChooseRailTrack() = [%d, %d]", result1, result2);
    00384       DumpTarget dmp1, dmp2;
    00385       pf1.DumpBase(dmp1);
    00386       pf2.DumpBase(dmp2);
    00387       FILE *f1 = fopen("C:\\yapf1.txt", "wt");
    00388       FILE *f2 = fopen("C:\\yapf2.txt", "wt");
    00389       fwrite(dmp1.m_out.Data(), 1, dmp1.m_out.Size(), f1);
    00390       fwrite(dmp2.m_out.Data(), 1, dmp2.m_out.Size(), f2);
    00391       fclose(f1);
    00392       fclose(f2);
    00393     }
    00394 #endif
    00395
    00396     return result1;
    00397   }
    00398
    00399   FORCEINLINE Trackdir ChooseRailTrack(const Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found, bool reserve_track, PBSTileInfo *target)
    00400   {
    00401     if (target != NULL) target->tile = INVALID_TILE;
    00402
    00403     // set origin and destination nodes
    00404     PBSTileInfo origin = FollowTrainReservation(v);
    00405     Yapf().SetOrigin(origin.tile, origin.trackdir, INVALID_TILE, INVALID_TRACKDIR, 1, true);
    00406     Yapf().SetDestination(v);
    00407
    00408     // find the best path
    00409     bool path_found = Yapf().FindPath(v);
    00410     if (path_not_found != NULL) {
    00411       // tell controller that the path was only 'guessed'
    00412       // treat the path as found if stopped on the first two way signal(s)
    00413       *path_not_found = !(path_found || Yapf().m_stopped_on_first_two_way_signal);
    00414     }
    00415
    00416     // if path not found - return INVALID_TRACKDIR
    00417     Trackdir next_trackdir = INVALID_TRACKDIR;
    00418     Node *pNode = Yapf().GetBestNode();
    00419     if (pNode != NULL) {
    00420       // reserve till end of path
    00421       this->SetReservationTarget(pNode, pNode->GetLastTile(), pNode->GetLastTrackdir());
    00422
    00423       // path was found or at least suggested
    00424       // walk through the path back to the origin
    00425       Node *pPrev = NULL;
    00426       while (pNode->m_parent != NULL) {
    00427         pPrev = pNode;
    00428         pNode = pNode->m_parent;
    00429
    00430         this->FindSafePositionOnNode(pPrev);
    00431       }
    00432       // return trackdir from the best origin node (one of start nodes)
    00433       Node& best_next_node = *pPrev;
    00434       next_trackdir = best_next_node.GetTrackdir();
    00435
    00436       if (reserve_track && path_found) this->TryReservePath(target);
    00437     }
    00438     return next_trackdir;
    00439   }
    00440
    00441   static bool stCheckReverseTrain(const Vehicle *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int reverse_penalty)
    00442   {
    00443     Tpf pf1;
    00444     bool result1 = pf1.CheckReverseTrain(v, t1, td1, t2, td2, reverse_penalty);
    00445
    00446 #if DEBUG_YAPF_CACHE
    00447     Tpf pf2;
    00448     pf2.DisableCache(true);
    00449     bool result2 = pf2.CheckReverseTrain(v, t1, td1, t2, td2, reverse_penalty);
    00450     if (result1 != result2) {
    00451       DEBUG(yapf, 0, "CACHE ERROR: CheckReverseTrain() = [%s, %s]", result1 ? "T" : "F", result2 ? "T" : "F");
    00452     }
    00453 #endif
    00454
    00455     return result1;
    00456   }
    00457
    00458   FORCEINLINE bool CheckReverseTrain(const Vehicle *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int reverse_penalty)
    00459   {
    00460     // create pathfinder instance
    00461     // set origin and destination nodes
    00462     Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty, false);
    00463     Yapf().SetDestination(v);
    00464
    00465     // find the best path
    00466     bool bFound = Yapf().FindPath(v);
    00467
    00468     if (!bFound) return false;
    00469
    00470     // path was found
    00471     // walk through the path back to the origin
    00472     Node *pNode = Yapf().GetBestNode();
    00473     while (pNode->m_parent != NULL) {
    00474       pNode = pNode->m_parent;
    00475     }
    00476
    00477     // check if it was reversed origin
    00478     Node& best_org_node = *pNode;
    00479     bool reversed = (best_org_node.m_cost != 0);
    00480     return reversed;
    00481   }
    00482 };
    00483
    00484 template <class Tpf_, class Ttrack_follower, class Tnode_list, template <class Types> class TdestinationT, template <class Types> class TfollowT>
    00485 struct CYapfRail_TypesT
    00486 {
    00487   typedef CYapfRail_TypesT<Tpf_, Ttrack_follower, Tnode_list, TdestinationT, TfollowT>  Types;
    00488
    00489   typedef Tpf_                                Tpf;
    00490   typedef Ttrack_follower                     TrackFollower;
    00491   typedef Tnode_list                          NodeList;
    00492   typedef CYapfBaseT<Types>                   PfBase;
    00493   typedef TfollowT<Types>                     PfFollow;
    00494   typedef CYapfOriginTileTwoWayT<Types>       PfOrigin;
    00495   typedef TdestinationT<Types>                PfDestination;
    00496   typedef CYapfSegmentCostCacheGlobalT<Types> PfCache;
    00497   typedef CYapfCostRailT<Types>               PfCost;
    00498 };
    00499
    00500 struct CYapfRail1         : CYapfT<CYapfRail_TypesT<CYapfRail1        , CFollowTrackRail    , CRailNodeListTrackDir, CYapfDestinationTileOrStationRailT, CYapfFollowRailT> > {};
    00501 struct CYapfRail2         : CYapfT<CYapfRail_TypesT<CYapfRail2        , CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationTileOrStationRailT, CYapfFollowRailT> > {};
    00502
    00503 struct CYapfAnyDepotRail1 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail1, CFollowTrackRail    , CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
    00504 struct CYapfAnyDepotRail2 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail2, CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
    00505
    00506 struct CYapfAnySafeTileRail1 : CYapfT<CYapfRail_TypesT<CYapfAnySafeTileRail1, CFollowTrackFreeRail    , CRailNodeListTrackDir, CYapfDestinationAnySafeTileRailT , CYapfFollowAnySafeTileRailT> > {};
    00507 struct CYapfAnySafeTileRail2 : CYapfT<CYapfRail_TypesT<CYapfAnySafeTileRail2, CFollowTrackFreeRailNo90, CRailNodeListTrackDir, CYapfDestinationAnySafeTileRailT , CYapfFollowAnySafeTileRailT> > {};
    00508
    00509
    00510 Trackdir YapfChooseRailTrack(const Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found, bool reserve_track, PBSTileInfo *target)
    00511 {
    00512   // default is YAPF type 2
    00513   typedef Trackdir (*PfnChooseRailTrack)(const Vehicle*, TileIndex, DiagDirection, TrackBits, bool*, bool, PBSTileInfo*);
    00514   PfnChooseRailTrack pfnChooseRailTrack = &CYapfRail1::stChooseRailTrack;
    00515
    00516   // check if non-default YAPF type needed
    00517   if (_settings_game.pf.forbid_90_deg) {
    00518     pfnChooseRailTrack = &CYapfRail2::stChooseRailTrack; // Trackdir, forbid 90-deg
    00519   }
    00520
    00521   Trackdir td_ret = pfnChooseRailTrack(v, tile, enterdir, tracks, path_not_found, reserve_track, target);
    00522
    00523   return td_ret;
    00524 }
    00525
    00526 bool YapfCheckReverseTrain(const Vehicle *v)
    00527 {
    00528   /* last wagon */
    00529   const Vehicle *last_veh = GetLastVehicleInChain(v);
    00530
    00531   // get trackdirs of both ends
    00532   Trackdir td = GetVehicleTrackdir(v);
    00533   Trackdir td_rev = ReverseTrackdir(GetVehicleTrackdir(last_veh));
    00534
    00535   /* tiles where front and back are */
    00536   TileIndex tile = v->tile;
    00537   TileIndex tile_rev = last_veh->tile;
    00538
    00539   int reverse_penalty = 0;
    00540
    00541   if (v->u.rail.track == TRACK_BIT_WORMHOLE) {
    00542     /* front in tunnel / on bridge */
    00543     DiagDirection dir_into_wormhole = GetTunnelBridgeDirection(tile);
    00544
    00545     if (TrackdirToExitdir(td) == dir_into_wormhole) tile = GetOtherTunnelBridgeEnd(tile);
    00546     /* Now 'tile' is the tunnel entry/bridge ramp the train will reach when driving forward */
    00547
    00548     /* Current position of the train in the wormhole */
    00549     TileIndex cur_tile = TileVirtXY(v->x_pos, v->y_pos);
    00550
    00551     /* Add distance to drive in the wormhole as penalty for the forward path, i.e. bonus for the reverse path
    00552      * Note: Negative penalties are ok for the start tile. */
    00553     reverse_penalty -= DistanceManhattan(cur_tile, tile) * YAPF_TILE_LENGTH;
    00554   }
    00555
    00556   if (last_veh->u.rail.track == TRACK_BIT_WORMHOLE) {
    00557     /* back in tunnel / on bridge */
    00558     DiagDirection dir_into_wormhole = GetTunnelBridgeDirection(tile_rev);
    00559
    00560     if (TrackdirToExitdir(td_rev) == dir_into_wormhole) tile_rev = GetOtherTunnelBridgeEnd(tile_rev);
    00561     /* Now 'tile_rev' is the tunnel entry/bridge ramp the train will reach when reversing */
    00562
    00563     /* Current position of the last wagon in the wormhole */
    00564     TileIndex cur_tile = TileVirtXY(last_veh->x_pos, last_veh->y_pos);
    00565
    00566     /* Add distance to drive in the wormhole as penalty for the revere path. */
    00567     reverse_penalty += DistanceManhattan(cur_tile, tile_rev) * YAPF_TILE_LENGTH;
    00568   }
    00569
    00570   typedef bool (*PfnCheckReverseTrain)(const Vehicle*, TileIndex, Trackdir, TileIndex, Trackdir, int);
    00571   PfnCheckReverseTrain pfnCheckReverseTrain = CYapfRail1::stCheckReverseTrain;
    00572
    00573   // check if non-default YAPF type needed
    00574   if (_settings_game.pf.forbid_90_deg) {
    00575     pfnCheckReverseTrain = &CYapfRail2::stCheckReverseTrain; // Trackdir, forbid 90-deg
    00576   }
    00577
    00578   /* slightly hackish: If the pathfinders finds a path, the cost of the first node is tested to distinguish between forward- and reverse-path. */
    00579   if (reverse_penalty == 0) reverse_penalty = 1;
    00580
    00581   bool reverse = pfnCheckReverseTrain(v, tile, td, tile_rev, td_rev, reverse_penalty);
    00582
    00583   return reverse;
    00584 }
    00585
    00586 bool YapfFindNearestRailDepotTwoWay(const Vehicle *v, int max_distance, int reverse_penalty, TileIndex *depot_tile, bool *reversed)
    00587 {
    00588   *depot_tile = INVALID_TILE;
    00589   *reversed = false;
    00590
    00591   const Vehicle *last_veh = GetLastVehicleInChain(v);
    00592
    00593   PBSTileInfo origin = FollowTrainReservation(v);
    00594   TileIndex last_tile = last_veh->tile;
    00595   Trackdir td_rev = ReverseTrackdir(GetVehicleTrackdir(last_veh));
    00596
    00597   typedef bool (*PfnFindNearestDepotTwoWay)(const Vehicle*, TileIndex, Trackdir, TileIndex, Trackdir, int, int, TileIndex*, bool*);
    00598   PfnFindNearestDepotTwoWay pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail1::stFindNearestDepotTwoWay;
    00599
    00600   // check if non-default YAPF type needed
    00601   if (_settings_game.pf.forbid_90_deg) {
    00602     pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail2::stFindNearestDepotTwoWay; // Trackdir, forbid 90-deg
    00603   }
    00604
    00605   bool ret = pfnFindNearestDepotTwoWay(v, origin.tile, origin.trackdir, last_tile, td_rev, max_distance, reverse_penalty, depot_tile, reversed);
    00606   return ret;
    00607 }
    00608
    00609 bool YapfRailFindNearestSafeTile(const Vehicle *v, TileIndex tile, Trackdir td, bool override_railtype)
    00610 {
    00611   typedef bool (*PfnFindNearestSafeTile)(const Vehicle*, TileIndex, Trackdir, bool);
    00612   PfnFindNearestSafeTile pfnFindNearestSafeTile = CYapfAnySafeTileRail1::stFindNearestSafeTile;
    00613
    00614   /* check if non-default YAPF type needed */
    00615   if (_settings_game.pf.forbid_90_deg) {
    00616     pfnFindNearestSafeTile = &CYapfAnySafeTileRail2::stFindNearestSafeTile;
    00617   }
    00618
    00619   return pfnFindNearestSafeTile(v, tile, td, override_railtype);
    00620 }
    00621
    00623 int CSegmentCostCacheBase::s_rail_change_counter = 0;
    00624
    00625 void YapfNotifyTrackLayoutChange(TileIndex tile, Track track)
    00626 {
    00627   CSegmentCostCacheBase::NotifyTrackLayoutChange(tile, track);
    00628 }