# yapf_rail.cpp

Go to the documentation of this file.

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