# yapf_rail.cpp

Go to the documentation of this file.

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