KUDr@3900: /* $Id$ */ KUDr@3900: celestar@6121: /** @file follow_track.hpp Template function for track followers */ celestar@6121: KUDr@3900: #ifndef FOLLOW_TRACK_HPP KUDr@3900: #define FOLLOW_TRACK_HPP KUDr@3900: KUDr@3900: #include "yapf.hpp" rubidium@8962: #include "../depot_map.h" KUDr@8636: KUDr@3900: /** Track follower helper template class (can serve pathfinders and vehicle rubidium@4549: * controllers). See 6 different typedefs below for 3 different transport rubidium@4549: * types w/ of w/o 90-deg turns allowed */ KUDr@3900: template KUDr@8636: struct CFollowTrackT KUDr@3900: { KUDr@8636: enum ErrorCode { KUDr@8636: EC_NONE, KUDr@8636: EC_OWNER, KUDr@8636: EC_RAIL_TYPE, KUDr@8636: EC_90DEG, KUDr@8636: EC_NO_WAY, KUDr@8636: }; KUDr@8636: rubidium@9439: const Vehicle *m_veh; ///< moving vehicle KUDr@8636: TileIndex m_old_tile; ///< the origin (vehicle moved from) before move KUDr@8636: Trackdir m_old_td; ///< the trackdir (the vehicle was on) before move KUDr@8636: TileIndex m_new_tile; ///< the new tile (the vehicle has entered) KUDr@8636: TrackdirBits m_new_td_bits; ///< the new set of available trackdirs KUDr@8636: DiagDirection m_exitdir; ///< exit direction (leaving the old tile) KUDr@8636: bool m_is_tunnel; ///< last turn passed tunnel KUDr@8636: bool m_is_bridge; ///< last turn passed bridge ramp KUDr@8636: bool m_is_station; ///< last turn passed station KUDr@8636: int m_tiles_skipped; ///< number of skipped tunnel or station tiles KUDr@8636: ErrorCode m_err; rubidium@9439: CPerformanceTimer *m_pPerf; rubidium@9797: RailTypes m_railtypes; KUDr@3900: rubidium@9797: FORCEINLINE CFollowTrackT(const Vehicle *v = NULL, RailTypes railtype_override = INVALID_RAILTYPES, CPerformanceTimer *pPerf = NULL) KUDr@3900: { rubidium@9797: Init(v, railtype_override, pPerf); KUDr@3900: } KUDr@3900: rubidium@9797: FORCEINLINE void Init(const Vehicle *v, RailTypes railtype_override, CPerformanceTimer *pPerf) KUDr@3900: { rubidium@6259: assert(!IsRailTT() || (v != NULL && v->type == VEH_TRAIN)); KUDr@3900: m_veh = v; KUDr@3900: m_pPerf = pPerf; KUDr@3900: // don't worry, all is inlined so compiler should remove unnecessary initializations KUDr@3900: m_new_tile = INVALID_TILE; KUDr@3900: m_new_td_bits = TRACKDIR_BIT_NONE; KUDr@3900: m_exitdir = INVALID_DIAGDIR; celestar@5385: m_is_station = m_is_bridge = m_is_tunnel = false; KUDr@3931: m_tiles_skipped = 0; KUDr@7211: m_err = EC_NONE; rubidium@9797: if (IsRailTT()) m_railtypes = railtype_override == INVALID_RAILTYPES ? v->u.rail.compatible_railtypes : railtype_override; KUDr@3900: } KUDr@3900: KUDr@3900: FORCEINLINE static TransportType TT() {return Ttr_type_;} KUDr@3900: FORCEINLINE static bool IsWaterTT() {return TT() == TRANSPORT_WATER;} KUDr@3900: FORCEINLINE static bool IsRailTT() {return TT() == TRANSPORT_RAIL;} frosch@8465: FORCEINLINE bool IsTram() {return IsRoadTT() && HasBit(m_veh->u.road.compatible_roadtypes, ROADTYPE_TRAM);} KUDr@3900: FORCEINLINE static bool IsRoadTT() {return TT() == TRANSPORT_ROAD;} KUDr@3900: FORCEINLINE static bool Allow90degTurns() {return T90deg_turns_allowed_;} KUDr@3900: frosch@8465: /** Tests if a tile is a road tile with a single tramtrack (tram can reverse) */ frosch@8465: FORCEINLINE DiagDirection GetSingleTramBit(TileIndex tile) frosch@8465: { frosch@8563: if (IsTram() && IsNormalRoadTile(tile)) { frosch@8465: RoadBits rb = GetRoadBits(tile, ROADTYPE_TRAM); frosch@8465: switch (rb) { frosch@8465: case ROAD_NW: return DIAGDIR_NW; frosch@8465: case ROAD_SW: return DIAGDIR_SW; frosch@8465: case ROAD_SE: return DIAGDIR_SE; frosch@8465: case ROAD_NE: return DIAGDIR_NE; frosch@8465: default: break; frosch@8465: } frosch@8465: } frosch@8465: return INVALID_DIAGDIR; frosch@8465: } frosch@8465: KUDr@3900: /** main follower routine. Fills all members and return true on success. rubidium@4549: * Otherwise returns false if track can't be followed. */ rubidium@9797: inline bool Follow(TileIndex old_tile, Trackdir old_td) KUDr@3900: { KUDr@3900: m_old_tile = old_tile; KUDr@3900: m_old_td = old_td; KUDr@7211: m_err = EC_NONE; frosch@8616: assert(((TrackStatusToTrackdirBits(GetTileTrackStatus(m_old_tile, TT(), m_veh->u.road.compatible_roadtypes)) & TrackdirToTrackdirBits(m_old_td)) != 0) || frosch@8465: (GetSingleTramBit(m_old_tile) != INVALID_DIAGDIR)); // Disable the assertion for single tram bits KUDr@3900: m_exitdir = TrackdirToExitdir(m_old_td); frosch@8465: if (ForcedReverse()) return true; KUDr@3900: if (!CanExitOldTile()) return false; KUDr@3900: FollowTileExit(); KUDr@3976: if (!QueryNewTileTrackStatus()) return TryReverse(); KUDr@3900: if (!CanEnterNewTile()) return false; KUDr@3900: m_new_td_bits &= DiagdirReachesTrackdirs(m_exitdir); KUDr@7211: if (m_new_td_bits == TRACKDIR_BIT_NONE) { KUDr@7211: m_err = EC_NO_WAY; KUDr@7211: return false; KUDr@7211: } KUDr@7211: if (!Allow90degTurns()) { KUDr@3900: m_new_td_bits &= (TrackdirBits)~(int)TrackdirCrossesTrackdirs(m_old_td); KUDr@7211: if (m_new_td_bits == TRACKDIR_BIT_NONE) { KUDr@7211: m_err = EC_90DEG; KUDr@7211: return false; KUDr@7211: } KUDr@7211: } KUDr@7211: return true; KUDr@3900: } KUDr@3900: KUDr@3900: protected: KUDr@3931: /** Follow the m_exitdir from m_old_tile and fill m_new_tile and m_tiles_skipped */ KUDr@3900: FORCEINLINE void FollowTileExit() KUDr@3900: { celestar@5385: m_is_station = m_is_bridge = m_is_tunnel = false; celestar@5385: m_tiles_skipped = 0; celestar@5385: smatz@8398: // extra handling for tunnels and bridges in our direction smatz@8398: if (IsTileType(m_old_tile, MP_TUNNELBRIDGE)) { smatz@8398: DiagDirection enterdir = GetTunnelBridgeDirection(m_old_tile); smatz@8398: if (enterdir == m_exitdir) { smatz@8398: // we are entering the tunnel / bridge smatz@8398: if (IsTunnel(m_old_tile)) { smatz@8398: m_is_tunnel = true; smatz@8398: m_new_tile = GetOtherTunnelEnd(m_old_tile); smatz@8398: } else { // IsBridge(m_old_tile) smatz@8398: m_is_bridge = true; smatz@8398: m_new_tile = GetOtherBridgeEnd(m_old_tile); smatz@8398: } smatz@8398: m_tiles_skipped = GetTunnelBridgeLength(m_new_tile, m_old_tile); KUDr@3900: return; KUDr@3900: } smatz@8398: assert(ReverseDiagDir(enterdir) == m_exitdir); celestar@5385: } celestar@5385: celestar@5385: // normal or station tile, do one step Darkvater@4559: TileIndexDiff diff = TileOffsByDiagDir(m_exitdir); KUDr@3900: m_new_tile = TILE_ADD(m_old_tile, diff); KUDr@3931: KUDr@3931: // special handling for stations KUDr@3931: if (IsRailTT() && IsRailwayStationTile(m_new_tile)) { KUDr@3931: m_is_station = true; KUDr@3931: } else if (IsRoadTT() && IsRoadStopTile(m_new_tile)) { KUDr@3931: m_is_station = true; KUDr@3931: } else { KUDr@3931: m_is_station = false; KUDr@3931: } KUDr@3900: } KUDr@3900: KUDr@3900: /** stores track status (available trackdirs) for the new tile into m_new_td_bits */ KUDr@3900: FORCEINLINE bool QueryNewTileTrackStatus() KUDr@3900: { KUDr@3900: CPerfStart perf(*m_pPerf); KUDr@3912: if (IsRailTT() && GetTileType(m_new_tile) == MP_RAILWAY && IsPlainRailTile(m_new_tile)) { KUDr@3900: m_new_td_bits = (TrackdirBits)(GetTrackBits(m_new_tile) * 0x101); KUDr@3900: } else { frosch@8616: m_new_td_bits = TrackStatusToTrackdirBits(GetTileTrackStatus(m_new_tile, TT(), m_veh->u.road.compatible_roadtypes)); frosch@8465: frosch@8465: if (m_new_td_bits == 0) { frosch@8465: /* GetTileTrackStatus() returns 0 for single tram bits. frosch@8465: * As we cannot change it there (easily) without breaking something, change it here */ frosch@8465: switch (GetSingleTramBit(m_new_tile)) { frosch@8465: case DIAGDIR_NE: frosch@8465: case DIAGDIR_SW: frosch@8465: m_new_td_bits = TRACKDIR_BIT_X_NE | TRACKDIR_BIT_X_SW; frosch@8465: break; frosch@8465: frosch@8465: case DIAGDIR_NW: frosch@8465: case DIAGDIR_SE: frosch@8465: m_new_td_bits = TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_Y_SE; frosch@8465: break; frosch@8465: frosch@8465: default: break; frosch@8465: } frosch@8465: } KUDr@3900: } KUDr@3900: return (m_new_td_bits != TRACKDIR_BIT_NONE); KUDr@3900: } KUDr@3900: KUDr@3900: /** return true if we can leave m_old_tile in m_exitdir */ KUDr@3900: FORCEINLINE bool CanExitOldTile() KUDr@3900: { rubidium@6012: // road stop can be left at one direction only unless it's a drive-through stop rubidium@6012: if (IsRoadTT() && IsStandardRoadStopTile(m_old_tile)) { KUDr@3900: DiagDirection exitdir = GetRoadStopDir(m_old_tile); KUDr@7211: if (exitdir != m_exitdir) { KUDr@7211: m_err = EC_NO_WAY; KUDr@3900: return false; KUDr@7211: } KUDr@3900: } KUDr@3900: frosch@8465: /* single tram bits can only be left in one direction */ frosch@8465: DiagDirection single_tram = GetSingleTramBit(m_old_tile); frosch@8465: if (single_tram != INVALID_DIAGDIR && single_tram != m_exitdir) { frosch@8465: m_err = EC_NO_WAY; frosch@8465: return false; frosch@8465: } frosch@8465: KUDr@3900: // road depots can be also left in one direction only smatz@8954: if (IsRoadTT() && IsDepotTypeTile(m_old_tile, TT())) { KUDr@3900: DiagDirection exitdir = GetRoadDepotDirection(m_old_tile); KUDr@7211: if (exitdir != m_exitdir) { KUDr@7211: m_err = EC_NO_WAY; KUDr@3900: return false; KUDr@7211: } KUDr@3900: } KUDr@3900: return true; KUDr@3900: } KUDr@3900: KUDr@3900: /** return true if we can enter m_new_tile from m_exitdir */ KUDr@3900: FORCEINLINE bool CanEnterNewTile() KUDr@3900: { rubidium@6012: if (IsRoadTT() && IsStandardRoadStopTile(m_new_tile)) { rubidium@6012: // road stop can be entered from one direction only unless it's a drive-through stop KUDr@3900: DiagDirection exitdir = GetRoadStopDir(m_new_tile); KUDr@7211: if (ReverseDiagDir(exitdir) != m_exitdir) { KUDr@7211: m_err = EC_NO_WAY; KUDr@3900: return false; KUDr@7211: } KUDr@3900: } KUDr@3900: frosch@8465: /* single tram bits can only be entered from one direction */ frosch@8465: DiagDirection single_tram = GetSingleTramBit(m_new_tile); frosch@8465: if (single_tram != INVALID_DIAGDIR && single_tram != ReverseDiagDir(m_exitdir)) { frosch@8465: m_err = EC_NO_WAY; frosch@8465: return false; frosch@8465: } frosch@8465: KUDr@3900: // road and rail depots can also be entered from one direction only smatz@8954: if (IsRoadTT() && IsDepotTypeTile(m_new_tile, TT())) { KUDr@3900: DiagDirection exitdir = GetRoadDepotDirection(m_new_tile); KUDr@7211: if (ReverseDiagDir(exitdir) != m_exitdir) { KUDr@7211: m_err = EC_NO_WAY; KUDr@3900: return false; KUDr@7211: } KUDr@4407: // don't try to enter other player's depots KUDr@4407: if (GetTileOwner(m_new_tile) != m_veh->owner) { KUDr@7211: m_err = EC_OWNER; KUDr@4407: return false; KUDr@4407: } KUDr@3900: } smatz@8954: if (IsRailTT() && IsDepotTypeTile(m_new_tile, TT())) { KUDr@3900: DiagDirection exitdir = GetRailDepotDirection(m_new_tile); KUDr@7211: if (ReverseDiagDir(exitdir) != m_exitdir) { KUDr@7211: m_err = EC_NO_WAY; KUDr@3900: return false; KUDr@7211: } KUDr@3900: } KUDr@3900: KUDr@3900: // rail transport is possible only on tiles with the same owner as vehicle KUDr@3900: if (IsRailTT() && GetTileOwner(m_new_tile) != m_veh->owner) { KUDr@3900: // different owner KUDr@7211: m_err = EC_NO_WAY; celestar@5385: return false; KUDr@3900: } KUDr@3900: KUDr@3900: // rail transport is possible only on compatible rail types KUDr@3900: if (IsRailTT()) { tron@6154: RailType rail_type = GetTileRailType(m_new_tile); rubidium@9797: if (!HasBit(m_railtypes, rail_type)) { KUDr@3900: // incompatible rail type KUDr@7211: m_err = EC_RAIL_TYPE; KUDr@3900: return false; KUDr@3900: } KUDr@3900: } KUDr@3900: KUDr@5420: // tunnel holes and bridge ramps can be entered only from proper direction rubidium@9490: if (IsTileType(m_new_tile, MP_TUNNELBRIDGE)) { KUDr@5420: if (IsTunnel(m_new_tile)) { KUDr@5420: if (!m_is_tunnel) { smatz@8083: DiagDirection tunnel_enterdir = GetTunnelBridgeDirection(m_new_tile); KUDr@7211: if (tunnel_enterdir != m_exitdir) { KUDr@7214: m_err = EC_NO_WAY; KUDr@7211: return false; KUDr@7211: } KUDr@5420: } smatz@8390: } else { // IsBridge(m_new_tile) KUDr@5420: if (!m_is_bridge) { smatz@8083: DiagDirection ramp_enderdir = GetTunnelBridgeDirection(m_new_tile); KUDr@7211: if (ramp_enderdir != m_exitdir) { KUDr@7214: m_err = EC_NO_WAY; KUDr@7211: return false; KUDr@7211: } KUDr@5420: } KUDr@5420: } KUDr@3900: } KUDr@3931: KUDr@3931: // special handling for rail stations - get to the end of platform KUDr@3931: if (IsRailTT() && m_is_station) { KUDr@3931: // entered railway station KUDr@3931: // get platform length celestar@5998: uint length = GetStationByTile(m_new_tile)->GetPlatformLength(m_new_tile, TrackdirToExitdir(m_old_td)); KUDr@3931: // how big step we must do to get to the last platform tile; KUDr@3931: m_tiles_skipped = length - 1; KUDr@3931: // move to the platform end Darkvater@4559: TileIndexDiff diff = TileOffsByDiagDir(m_exitdir); KUDr@3931: diff *= m_tiles_skipped; KUDr@3931: m_new_tile = TILE_ADD(m_new_tile, diff); KUDr@3931: return true; KUDr@3931: } KUDr@3931: KUDr@3900: return true; KUDr@3900: } KUDr@3900: frosch@8465: /** return true if we must reverse (in depots and single tram bits) */ frosch@8465: FORCEINLINE bool ForcedReverse() KUDr@3900: { KUDr@3976: // rail and road depots cause reversing smatz@8954: if (!IsWaterTT() && IsDepotTypeTile(m_old_tile, TT())) { KUDr@3976: DiagDirection exitdir = IsRailTT() ? GetRailDepotDirection(m_old_tile) : GetRoadDepotDirection(m_old_tile); KUDr@3900: if (exitdir != m_exitdir) { KUDr@3900: // reverse KUDr@3900: m_new_tile = m_old_tile; KUDr@3900: m_new_td_bits = TrackdirToTrackdirBits(ReverseTrackdir(m_old_td)); KUDr@3900: m_exitdir = exitdir; KUDr@3931: m_tiles_skipped = 0; celestar@5385: m_is_tunnel = m_is_bridge = m_is_station = false; KUDr@3900: return true; KUDr@3900: } KUDr@3900: } frosch@8465: frosch@8465: // single tram bits cause reversing frosch@8465: if (GetSingleTramBit(m_old_tile) == ReverseDiagDir(m_exitdir)) { frosch@8465: // reverse frosch@8465: m_new_tile = m_old_tile; frosch@8465: m_new_td_bits = TrackdirToTrackdirBits(ReverseTrackdir(m_old_td)); frosch@8465: m_exitdir = ReverseDiagDir(m_exitdir); frosch@8465: m_tiles_skipped = 0; frosch@8465: m_is_tunnel = m_is_bridge = m_is_station = false; frosch@8465: return true; frosch@8465: } frosch@8465: KUDr@3900: return false; KUDr@3900: } KUDr@3976: KUDr@3976: /** return true if we successfully reversed at end of road/track */ KUDr@3976: FORCEINLINE bool TryReverse() KUDr@3976: { frosch@8465: if (IsRoadTT() && !IsTram()) { KUDr@3976: // if we reached the end of road, we can reverse the RV and continue moving KUDr@3976: m_exitdir = ReverseDiagDir(m_exitdir); KUDr@3976: // new tile will be the same as old one KUDr@3976: m_new_tile = m_old_tile; KUDr@3976: // set new trackdir bits to all reachable trackdirs KUDr@3976: QueryNewTileTrackStatus(); KUDr@3976: m_new_td_bits &= DiagdirReachesTrackdirs(m_exitdir); KUDr@3976: if (m_new_td_bits != TRACKDIR_BIT_NONE) { KUDr@3976: // we have some trackdirs reachable after reversal KUDr@3976: return true; KUDr@3976: } KUDr@3976: } KUDr@7211: m_err = EC_NO_WAY; KUDr@3976: return false; KUDr@3976: } KUDr@3976: KUDr@3900: public: KUDr@3900: /** Helper for pathfinders - get min/max speed on the m_old_tile/m_old_td */ KUDr@7037: int GetSpeedLimit(int *pmin_speed = NULL) const KUDr@3900: { KUDr@3900: int min_speed = 0; KUDr@3900: int max_speed = INT_MAX; // no limit KUDr@3900: KUDr@3900: // for now we handle only on-bridge speed limit celestar@5385: if (!IsWaterTT() && IsBridgeTile(m_old_tile)) { belugas@8491: int spd = GetBridgeSpec(GetBridgeType(m_old_tile))->speed; celestar@5385: if (IsRoadTT()) spd *= 2; celestar@5385: if (max_speed > spd) max_speed = spd; KUDr@3900: } KUDr@3900: KUDr@3900: // if min speed was requested, return it KUDr@3900: if (pmin_speed) *pmin_speed = min_speed; KUDr@3900: return max_speed; KUDr@3900: } KUDr@3900: }; KUDr@3900: KUDr@3900: typedef CFollowTrackT CFollowTrackWater; KUDr@3900: typedef CFollowTrackT CFollowTrackRoad; KUDr@3900: typedef CFollowTrackT CFollowTrackRail; KUDr@3900: KUDr@3900: typedef CFollowTrackT CFollowTrackWaterNo90; KUDr@3900: typedef CFollowTrackT CFollowTrackRoadNo90; KUDr@3900: typedef CFollowTrackT CFollowTrackRailNo90; KUDr@3900: KUDr@3900: #endif /* FOLLOW_TRACK_HPP */