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