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