# follow_track.hpp

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 #ifndef  FOLLOW_TRACK_HPP
    00013 #define  FOLLOW_TRACK_HPP
    00014
    00015 #include "../pbs.h"
    00016 #include "../roadveh.h"
    00017 #include "../station_base.h"
    00018 #include "../train.h"
    00019 #include "../tunnelbridge.h"
    00020 #include "../tunnelbridge_map.h"
    00021 #include "../depot_map.h"
    00022 #include "pf_performance_timer.hpp"
    00023
    00029 template <TransportType Ttr_type_, typename VehicleType, bool T90deg_turns_allowed_ = true, bool Tmask_reserved_tracks = false>
    00030 struct CFollowTrackT
    00031 {
    00032   enum ErrorCode {
    00033     EC_NONE,
    00034     EC_OWNER,
    00035     EC_RAIL_TYPE,
    00036     EC_90DEG,
    00037     EC_NO_WAY,
    00038     EC_RESERVED,
    00039   };
    00040
    00041   const VehicleType  *m_veh;
    00042   Owner               m_veh_owner;
    00043   TileIndex           m_old_tile;
    00044   Trackdir            m_old_td;
    00045   TileIndex           m_new_tile;
    00046   TrackdirBits        m_new_td_bits;
    00047   DiagDirection       m_exitdir;
    00048   bool                m_is_tunnel;
    00049   bool                m_is_bridge;
    00050   bool                m_is_station;
    00051   int                 m_tiles_skipped;
    00052   ErrorCode           m_err;
    00053   CPerformanceTimer  *m_pPerf;
    00054   RailTypes           m_railtypes;
    00055
    00056   FORCEINLINE CFollowTrackT(const VehicleType *v = NULL, RailTypes railtype_override = INVALID_RAILTYPES, CPerformanceTimer *pPerf = NULL)
    00057   {
    00058     Init(v, railtype_override, pPerf);
    00059   }
    00060
    00061   FORCEINLINE CFollowTrackT(Owner o, RailTypes railtype_override = INVALID_RAILTYPES, CPerformanceTimer *pPerf = NULL)
    00062   {
    00063     m_veh = NULL;
    00064     Init(o, railtype_override, pPerf);
    00065   }
    00066
    00067   FORCEINLINE void Init(const VehicleType *v, RailTypes railtype_override, CPerformanceTimer *pPerf)
    00068   {
    00069     assert(!IsRailTT() || (v != NULL && v->type == VEH_TRAIN));
    00070     m_veh = v;
    00071     Init(v != NULL ? v->owner : INVALID_OWNER, IsRailTT() && railtype_override == INVALID_RAILTYPES ? Train::From(v)->compatible_railtypes : railtype_override, pPerf);
    00072   }
    00073
    00074   FORCEINLINE void Init(Owner o, RailTypes railtype_override, CPerformanceTimer *pPerf)
    00075   {
    00076     assert((!IsRoadTT() || m_veh != NULL) && (!IsRailTT() || railtype_override != INVALID_RAILTYPES));
    00077     m_veh_owner = o;
    00078     m_pPerf = pPerf;
    00079     /* don't worry, all is inlined so compiler should remove unnecessary initializations */
    00080     m_new_tile = INVALID_TILE;
    00081     m_new_td_bits = TRACKDIR_BIT_NONE;
    00082     m_exitdir = INVALID_DIAGDIR;
    00083     m_is_station = m_is_bridge = m_is_tunnel = false;
    00084     m_tiles_skipped = 0;
    00085     m_err = EC_NONE;
    00086     m_railtypes = railtype_override;
    00087   }
    00088
    00089   FORCEINLINE static TransportType TT() {return Ttr_type_;}
    00090   FORCEINLINE static bool IsWaterTT() {return TT() == TRANSPORT_WATER;}
    00091   FORCEINLINE static bool IsRailTT() {return TT() == TRANSPORT_RAIL;}
    00092   FORCEINLINE bool IsTram() {return IsRoadTT() && HasBit(RoadVehicle::From(m_veh)->compatible_roadtypes, ROADTYPE_TRAM);}
    00093   FORCEINLINE static bool IsRoadTT() {return TT() == TRANSPORT_ROAD;}
    00094   FORCEINLINE static bool Allow90degTurns() {return T90deg_turns_allowed_;}
    00095   FORCEINLINE static bool DoTrackMasking() {return IsRailTT() && Tmask_reserved_tracks;}
    00096
    00098   FORCEINLINE DiagDirection GetSingleTramBit(TileIndex tile)
    00099   {
    00100     assert(IsTram()); // this function shouldn't be called in other cases
    00101
    00102     if (IsNormalRoadTile(tile)) {
    00103       RoadBits rb = GetRoadBits(tile, ROADTYPE_TRAM);
    00104       switch (rb) {
    00105         case ROAD_NW: return DIAGDIR_NW;
    00106         case ROAD_SW: return DIAGDIR_SW;
    00107         case ROAD_SE: return DIAGDIR_SE;
    00108         case ROAD_NE: return DIAGDIR_NE;
    00109         default: break;
    00110       }
    00111     }
    00112     return INVALID_DIAGDIR;
    00113   }
    00114
    00119   inline bool Follow(TileIndex old_tile, Trackdir old_td)
    00120   {
    00121     m_old_tile = old_tile;
    00122     m_old_td = old_td;
    00123     m_err = EC_NONE;
    00124     assert(((TrackStatusToTrackdirBits(GetTileTrackStatus(m_old_tile, TT(), IsRoadTT() && m_veh != NULL ? RoadVehicle::From(m_veh)->compatible_roadtypes : 0)) & TrackdirToTrackdirBits(m_old_td)) != 0) ||
    00125            (IsTram() && GetSingleTramBit(m_old_tile) != INVALID_DIAGDIR)); // Disable the assertion for single tram bits
    00126     m_exitdir = TrackdirToExitdir(m_old_td);
    00127     if (ForcedReverse()) return true;
    00128     if (!CanExitOldTile()) return false;
    00129     FollowTileExit();
    00130     if (!QueryNewTileTrackStatus()) return TryReverse();
    00131     if (!CanEnterNewTile()) return false;
    00132     m_new_td_bits &= DiagdirReachesTrackdirs(m_exitdir);
    00133     if (m_new_td_bits == TRACKDIR_BIT_NONE) {
    00134       /* In case we can't enter the next tile, but are
    00135        * a normal road vehicle, then we can actually
    00136        * try to reverse as this is the end of the road.
    00137        * Trams can only turn on the appropriate bits in
    00138        * which case reaching this would mean a dead end
    00139        * near a building and in that case there would
    00140        * a "false" QueryNewTileTrackStatus result and
    00141        * as such reversing is already tried. The fact
    00142        * that function failed can have to do with a
    00143        * missing road bit, or inability to connect the
    00144        * different bits due to slopes. */
    00145       if (IsRoadTT() && !IsTram() && TryReverse()) return true;
    00146       m_err = EC_NO_WAY;
    00147       return false;
    00148     }
    00149     if (!Allow90degTurns()) {
    00150       m_new_td_bits &= (TrackdirBits)~(int)TrackdirCrossesTrackdirs(m_old_td);
    00151       if (m_new_td_bits == TRACKDIR_BIT_NONE) {
    00152         m_err = EC_90DEG;
    00153         return false;
    00154       }
    00155     }
    00156     return true;
    00157   }
    00158
    00159   inline bool MaskReservedTracks()
    00160   {
    00161     if (!DoTrackMasking()) return true;
    00162
    00163     if (m_is_station) {
    00164       /* Check skipped station tiles as well. */
    00165       TileIndexDiff diff = TileOffsByDiagDir(m_exitdir);
    00166       for (TileIndex tile = m_new_tile - diff * m_tiles_skipped; tile != m_new_tile; tile += diff) {
    00167         if (HasStationReservation(tile)) {
    00168           m_new_td_bits = TRACKDIR_BIT_NONE;
    00169           m_err = EC_RESERVED;
    00170           return false;
    00171         }
    00172       }
    00173     }
    00174
    00175     TrackBits reserved = GetReservedTrackbits(m_new_tile);
    00176     /* Mask already reserved trackdirs. */
    00177     m_new_td_bits &= ~TrackBitsToTrackdirBits(reserved);
    00178     /* Mask out all trackdirs that conflict with the reservation. */
    00179     Track t;
    00180     FOR_EACH_SET_TRACK(t, TrackdirBitsToTrackBits(m_new_td_bits)) {
    00181       if (TracksOverlap(reserved | TrackToTrackBits(t))) m_new_td_bits &= ~TrackToTrackdirBits(t);
    00182     }
    00183     if (m_new_td_bits == TRACKDIR_BIT_NONE) {
    00184       m_err = EC_RESERVED;
    00185       return false;
    00186     }
    00187     return true;
    00188   }
    00189
    00190 protected:
    00192   FORCEINLINE void FollowTileExit()
    00193   {
    00194     m_is_station = m_is_bridge = m_is_tunnel = false;
    00195     m_tiles_skipped = 0;
    00196
    00197     /* extra handling for tunnels and bridges in our direction */
    00198     if (IsTileType(m_old_tile, MP_TUNNELBRIDGE)) {
    00199       DiagDirection enterdir = GetTunnelBridgeDirection(m_old_tile);
    00200       if (enterdir == m_exitdir) {
    00201         /* we are entering the tunnel / bridge */
    00202         if (IsTunnel(m_old_tile)) {
    00203           m_is_tunnel = true;
    00204           m_new_tile = GetOtherTunnelEnd(m_old_tile);
    00205         } else { // IsBridge(m_old_tile)
    00206           m_is_bridge = true;
    00207           m_new_tile = GetOtherBridgeEnd(m_old_tile);
    00208         }
    00209         m_tiles_skipped = GetTunnelBridgeLength(m_new_tile, m_old_tile);
    00210         return;
    00211       }
    00212       assert(ReverseDiagDir(enterdir) == m_exitdir);
    00213     }
    00214
    00215     /* normal or station tile, do one step */
    00216     TileIndexDiff diff = TileOffsByDiagDir(m_exitdir);
    00217     m_new_tile = TILE_ADD(m_old_tile, diff);
    00218
    00219     /* special handling for stations */
    00220     if (IsRailTT() && HasStationTileRail(m_new_tile)) {
    00221       m_is_station = true;
    00222     } else if (IsRoadTT() && IsRoadStopTile(m_new_tile)) {
    00223       m_is_station = true;
    00224     } else {
    00225       m_is_station = false;
    00226     }
    00227   }
    00228
    00230   FORCEINLINE bool QueryNewTileTrackStatus()
    00231   {
    00232     CPerfStart perf(*m_pPerf);
    00233     if (IsRailTT() && IsPlainRailTile(m_new_tile)) {
    00234       m_new_td_bits = (TrackdirBits)(GetTrackBits(m_new_tile) * 0x101);
    00235     } else {
    00236       m_new_td_bits = TrackStatusToTrackdirBits(GetTileTrackStatus(m_new_tile, TT(), IsRoadTT() && m_veh != NULL ? RoadVehicle::From(m_veh)->compatible_roadtypes : 0));
    00237
    00238       if (IsTram() && m_new_td_bits == 0) {
    00239         /* GetTileTrackStatus() returns 0 for single tram bits.
    00240          * As we cannot change it there (easily) without breaking something, change it here */
    00241         switch (GetSingleTramBit(m_new_tile)) {
    00242           case DIAGDIR_NE:
    00243           case DIAGDIR_SW:
    00244             m_new_td_bits = TRACKDIR_BIT_X_NE | TRACKDIR_BIT_X_SW;
    00245             break;
    00246
    00247           case DIAGDIR_NW:
    00248           case DIAGDIR_SE:
    00249             m_new_td_bits = TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_Y_SE;
    00250             break;
    00251
    00252           default: break;
    00253         }
    00254       }
    00255     }
    00256     return (m_new_td_bits != TRACKDIR_BIT_NONE);
    00257   }
    00258
    00260   FORCEINLINE bool CanExitOldTile()
    00261   {
    00262     /* road stop can be left at one direction only unless it's a drive-through stop */
    00263     if (IsRoadTT() && IsStandardRoadStopTile(m_old_tile)) {
    00264       DiagDirection exitdir = GetRoadStopDir(m_old_tile);
    00265       if (exitdir != m_exitdir) {
    00266         m_err = EC_NO_WAY;
    00267         return false;
    00268       }
    00269     }
    00270
    00271     /* single tram bits can only be left in one direction */
    00272     if (IsTram()) {
    00273       DiagDirection single_tram = GetSingleTramBit(m_old_tile);
    00274       if (single_tram != INVALID_DIAGDIR && single_tram != m_exitdir) {
    00275         m_err = EC_NO_WAY;
    00276         return false;
    00277       }
    00278     }
    00279
    00280     /* road depots can be also left in one direction only */
    00281     if (IsRoadTT() && IsDepotTypeTile(m_old_tile, TT())) {
    00282       DiagDirection exitdir = GetRoadDepotDirection(m_old_tile);
    00283       if (exitdir != m_exitdir) {
    00284         m_err = EC_NO_WAY;
    00285         return false;
    00286       }
    00287     }
    00288     return true;
    00289   }
    00290
    00292   FORCEINLINE bool CanEnterNewTile()
    00293   {
    00294     if (IsRoadTT() && IsStandardRoadStopTile(m_new_tile)) {
    00295       /* road stop can be entered from one direction only unless it's a drive-through stop */
    00296       DiagDirection exitdir = GetRoadStopDir(m_new_tile);
    00297       if (ReverseDiagDir(exitdir) != m_exitdir) {
    00298         m_err = EC_NO_WAY;
    00299         return false;
    00300       }
    00301     }
    00302
    00303     /* single tram bits can only be entered from one direction */
    00304     if (IsTram()) {
    00305       DiagDirection single_tram = GetSingleTramBit(m_new_tile);
    00306       if (single_tram != INVALID_DIAGDIR && single_tram != ReverseDiagDir(m_exitdir)) {
    00307         m_err = EC_NO_WAY;
    00308         return false;
    00309       }
    00310     }
    00311
    00312     /* road and rail depots can also be entered from one direction only */
    00313     if (IsRoadTT() && IsDepotTypeTile(m_new_tile, TT())) {
    00314       DiagDirection exitdir = GetRoadDepotDirection(m_new_tile);
    00315       if (ReverseDiagDir(exitdir) != m_exitdir) {
    00316         m_err = EC_NO_WAY;
    00317         return false;
    00318       }
    00319       /* don't try to enter other company's depots */
    00320       if (GetTileOwner(m_new_tile) != m_veh_owner) {
    00321         m_err = EC_OWNER;
    00322         return false;
    00323       }
    00324     }
    00325     if (IsRailTT() && IsDepotTypeTile(m_new_tile, TT())) {
    00326       DiagDirection exitdir = GetRailDepotDirection(m_new_tile);
    00327       if (ReverseDiagDir(exitdir) != m_exitdir) {
    00328         m_err = EC_NO_WAY;
    00329         return false;
    00330       }
    00331     }
    00332
    00333     /* rail transport is possible only on tiles with the same owner as vehicle */
    00334     if (IsRailTT() && GetTileOwner(m_new_tile) != m_veh_owner) {
    00335       /* different owner */
    00336       m_err = EC_NO_WAY;
    00337       return false;
    00338     }
    00339
    00340     /* rail transport is possible only on compatible rail types */
    00341     if (IsRailTT()) {
    00342       RailType rail_type = GetTileRailType(m_new_tile);
    00343       if (!HasBit(m_railtypes, rail_type)) {
    00344         /* incompatible rail type */
    00345         m_err = EC_RAIL_TYPE;
    00346         return false;
    00347       }
    00348     }
    00349
    00350     /* tunnel holes and bridge ramps can be entered only from proper direction */
    00351     if (IsTileType(m_new_tile, MP_TUNNELBRIDGE)) {
    00352       if (IsTunnel(m_new_tile)) {
    00353         if (!m_is_tunnel) {
    00354           DiagDirection tunnel_enterdir = GetTunnelBridgeDirection(m_new_tile);
    00355           if (tunnel_enterdir != m_exitdir) {
    00356             m_err = EC_NO_WAY;
    00357             return false;
    00358           }
    00359         }
    00360       } else { // IsBridge(m_new_tile)
    00361         if (!m_is_bridge) {
    00362           DiagDirection ramp_enderdir = GetTunnelBridgeDirection(m_new_tile);
    00363           if (ramp_enderdir != m_exitdir) {
    00364             m_err = EC_NO_WAY;
    00365             return false;
    00366           }
    00367         }
    00368       }
    00369     }
    00370
    00371     /* special handling for rail stations - get to the end of platform */
    00372     if (IsRailTT() && m_is_station) {
    00373       /* entered railway station
    00374        * get platform length */
    00375       uint length = BaseStation::GetByTile(m_new_tile)->GetPlatformLength(m_new_tile, TrackdirToExitdir(m_old_td));
    00376       /* how big step we must do to get to the last platform tile; */
    00377       m_tiles_skipped = length - 1;
    00378       /* move to the platform end */
    00379       TileIndexDiff diff = TileOffsByDiagDir(m_exitdir);
    00380       diff *= m_tiles_skipped;
    00381       m_new_tile = TILE_ADD(m_new_tile, diff);
    00382       return true;
    00383     }
    00384
    00385     return true;
    00386   }
    00387
    00389   FORCEINLINE bool ForcedReverse()
    00390   {
    00391     /* rail and road depots cause reversing */
    00392     if (!IsWaterTT() && IsDepotTypeTile(m_old_tile, TT())) {
    00393       DiagDirection exitdir = IsRailTT() ? GetRailDepotDirection(m_old_tile) : GetRoadDepotDirection(m_old_tile);
    00394       if (exitdir != m_exitdir) {
    00395         /* reverse */
    00396         m_new_tile = m_old_tile;
    00397         m_new_td_bits = TrackdirToTrackdirBits(ReverseTrackdir(m_old_td));
    00398         m_exitdir = exitdir;
    00399         m_tiles_skipped = 0;
    00400         m_is_tunnel = m_is_bridge = m_is_station = false;
    00401         return true;
    00402       }
    00403     }
    00404
    00405     /* single tram bits cause reversing */
    00406     if (IsTram() && GetSingleTramBit(m_old_tile) == ReverseDiagDir(m_exitdir)) {
    00407       /* reverse */
    00408       m_new_tile = m_old_tile;
    00409       m_new_td_bits = TrackdirToTrackdirBits(ReverseTrackdir(m_old_td));
    00410       m_exitdir = ReverseDiagDir(m_exitdir);
    00411       m_tiles_skipped = 0;
    00412       m_is_tunnel = m_is_bridge = m_is_station = false;
    00413       return true;
    00414     }
    00415
    00416     return false;
    00417   }
    00418
    00420   FORCEINLINE bool TryReverse()
    00421   {
    00422     if (IsRoadTT() && !IsTram()) {
    00423       /* if we reached the end of road, we can reverse the RV and continue moving */
    00424       m_exitdir = ReverseDiagDir(m_exitdir);
    00425       /* new tile will be the same as old one */
    00426       m_new_tile = m_old_tile;
    00427       /* set new trackdir bits to all reachable trackdirs */
    00428       QueryNewTileTrackStatus();
    00429       m_new_td_bits &= DiagdirReachesTrackdirs(m_exitdir);
    00430       if (m_new_td_bits != TRACKDIR_BIT_NONE) {
    00431         /* we have some trackdirs reachable after reversal */
    00432         return true;
    00433       }
    00434     }
    00435     m_err = EC_NO_WAY;
    00436     return false;
    00437   }
    00438
    00439 public:
    00441   int GetSpeedLimit(int *pmin_speed = NULL) const
    00442   {
    00443     int min_speed = 0;
    00444     int max_speed = INT_MAX; // no limit
    00445
    00446     /* Check for on-bridge speed limit */
    00447     if (!IsWaterTT() && IsBridgeTile(m_old_tile)) {
    00448       int spd = GetBridgeSpec(GetBridgeType(m_old_tile))->speed;
    00449       if (IsRoadTT()) spd *= 2;
    00450       if (max_speed > spd) max_speed = spd;
    00451     }
    00452     /* Check for speed limit imposed by railtype */
    00453     if (IsRailTT()) {
    00454       uint16 rail_speed = GetRailTypeInfo(GetRailType(m_old_tile))->max_speed;
    00455       if (rail_speed > 0) max_speed = min(max_speed, rail_speed);
    00456     }
    00457
    00458     /* if min speed was requested, return it */
    00459     if (pmin_speed) *pmin_speed = min_speed;
    00460     return max_speed;
    00461   }
    00462 };
    00463
    00464 typedef CFollowTrackT<TRANSPORT_WATER, Ship,        true > CFollowTrackWater;
    00465 typedef CFollowTrackT<TRANSPORT_ROAD,  RoadVehicle, true > CFollowTrackRoad;
    00466 typedef CFollowTrackT<TRANSPORT_RAIL,  Train,       true > CFollowTrackRail;
    00467
    00468 typedef CFollowTrackT<TRANSPORT_WATER, Ship,        false> CFollowTrackWaterNo90;
    00469 typedef CFollowTrackT<TRANSPORT_ROAD,  RoadVehicle, false> CFollowTrackRoadNo90;
    00470 typedef CFollowTrackT<TRANSPORT_RAIL,  Train,       false> CFollowTrackRailNo90;
    00471
    00472 typedef CFollowTrackT<TRANSPORT_RAIL, Train, true,  true > CFollowTrackFreeRail;
    00473 typedef CFollowTrackT<TRANSPORT_RAIL, Train, false, true > CFollowTrackFreeRailNo90;
    00474
    00475 #endif /* FOLLOW_TRACK_HPP */