KUDr@3900: /* $Id$ */ KUDr@3900: KUDr@3900: #ifndef FOLLOW_TRACK_HPP KUDr@3900: #define FOLLOW_TRACK_HPP KUDr@3900: KUDr@3900: #include "yapf.hpp" KUDr@3900: KUDr@3900: /** Track follower helper template class (can serve pathfinders and vehicle KUDr@3900: controllers). See 6 different typedefs below for 3 different transport KUDr@3900: types w/ of w/o 90-deg turns allowed */ KUDr@3900: template KUDr@3900: struct CFollowTrackT : public FollowTrack_t KUDr@3900: { KUDr@3900: CPerformanceTimer* m_pPerf; KUDr@3900: KUDr@3900: FORCEINLINE CFollowTrackT(Vehicle* v = NULL, CPerformanceTimer* pPerf = NULL) KUDr@3900: { KUDr@3900: Init(v, pPerf); KUDr@3900: } KUDr@3900: KUDr@3900: FORCEINLINE void Init(Vehicle* v, CPerformanceTimer* pPerf) KUDr@3900: { KUDr@3900: 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; KUDr@3900: m_is_tunnel = false; KUDr@3900: m_tunnel_tiles_skipped = 0; 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;} KUDr@3900: FORCEINLINE static bool IsRoadTT() {return TT() == TRANSPORT_ROAD;} KUDr@3900: FORCEINLINE static bool Allow90degTurns() {return T90deg_turns_allowed_;} KUDr@3900: KUDr@3900: /** main follower routine. Fills all members and return true on success. KUDr@3900: Otherwise returns false if track can't be followed. */ KUDr@3900: FORCEINLINE 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@3900: assert((GetTileTrackStatus(m_old_tile, TT()) & TrackdirToTrackdirBits(m_old_td)) != 0); KUDr@3900: m_exitdir = TrackdirToExitdir(m_old_td); KUDr@3900: if (EnteredRailDepot()) return true; KUDr@3900: if (!CanExitOldTile()) return false; KUDr@3900: FollowTileExit(); KUDr@3900: if (!QueryNewTileTrackStatus()) return false; KUDr@3900: if (!CanEnterNewTile()) return false; KUDr@3900: m_new_td_bits &= DiagdirReachesTrackdirs(m_exitdir); KUDr@3900: if (!Allow90degTurns()) KUDr@3900: m_new_td_bits &= (TrackdirBits)~(int)TrackdirCrossesTrackdirs(m_old_td); KUDr@3900: return (m_new_td_bits != TRACKDIR_BIT_NONE); KUDr@3900: } KUDr@3900: KUDr@3900: protected: KUDr@3900: /** Follow the m_exitdir from m_old_tile and fill m_new_tile and m_tunnel_tiles_skipped */ KUDr@3900: FORCEINLINE void FollowTileExit() KUDr@3900: { KUDr@3900: // extra handling for tunnels in our direction KUDr@3900: if (IsTunnelTile(m_old_tile)) { KUDr@3900: DiagDirection tunnel_enterdir = GetTunnelDirection(m_old_tile); KUDr@3900: if (tunnel_enterdir == m_exitdir) { KUDr@3900: // we are entering the tunnel KUDr@3900: FindLengthOfTunnelResult flotr = FindLengthOfTunnel(m_old_tile, m_exitdir); KUDr@3900: m_new_tile = flotr.tile; KUDr@3900: m_is_tunnel = true; KUDr@3900: m_tunnel_tiles_skipped = flotr.length - 1; KUDr@3900: return; KUDr@3900: } KUDr@3900: assert(ReverseDiagDir(tunnel_enterdir) == m_exitdir); KUDr@3900: } KUDr@3900: // not a tunnel KUDr@3900: m_is_tunnel = false; KUDr@3900: m_tunnel_tiles_skipped = 0; KUDr@3900: // normal tile KUDr@3900: TileIndexDiff diff = TileOffsByDir(m_exitdir); KUDr@3900: m_new_tile = TILE_ADD(m_old_tile, diff); 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 { KUDr@3900: uint32 ts = GetTileTrackStatus(m_new_tile, TT()); KUDr@3900: m_new_td_bits = (TrackdirBits)(ts & TRACKDIR_BIT_MASK); 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: { KUDr@3900: // road stop can be left at one direction only KUDr@3900: if (IsRoadTT() && IsRoadStopTile(m_old_tile)) { KUDr@3900: DiagDirection exitdir = GetRoadStopDir(m_old_tile); KUDr@3900: if (exitdir != m_exitdir) KUDr@3900: return false; KUDr@3900: } KUDr@3900: KUDr@3900: // road depots can be also left in one direction only KUDr@3900: if (IsRoadTT() && IsTileDepotType(m_old_tile, TT())) { KUDr@3900: DiagDirection exitdir = GetRoadDepotDirection(m_old_tile); KUDr@3900: if (exitdir != m_exitdir) KUDr@3900: return false; 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: { KUDr@3900: if (IsRoadTT() && IsRoadStopTile(m_new_tile)) { KUDr@3900: // road stop can be entered from one direction only KUDr@3900: DiagDirection exitdir = GetRoadStopDir(m_new_tile); KUDr@3900: if (ReverseDiagDir(exitdir) != m_exitdir) KUDr@3900: return false; KUDr@3900: } KUDr@3900: KUDr@3900: // road and rail depots can also be entered from one direction only KUDr@3900: if (IsRoadTT() && IsTileDepotType(m_new_tile, TT())) { KUDr@3900: DiagDirection exitdir = GetRoadDepotDirection(m_new_tile); KUDr@3900: if (ReverseDiagDir(exitdir) != m_exitdir) KUDr@3900: return false; KUDr@3900: } KUDr@3900: if (IsRailTT() && IsTileDepotType(m_new_tile, TT())) { KUDr@3900: DiagDirection exitdir = GetRailDepotDirection(m_new_tile); KUDr@3900: if (ReverseDiagDir(exitdir) != m_exitdir) KUDr@3900: return false; 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@3900: if (IsBridgeTile(m_new_tile)) { KUDr@3900: if (IsBridgeMiddle(m_new_tile)) { KUDr@3900: // bridge middle has no owner - tile is owned by the owner of the under-bridge track KUDr@3900: if (GetBridgeAxis(m_new_tile) != DiagDirToAxis(m_exitdir)) { KUDr@3900: // so it must be under bridge track (and wrong owner) KUDr@3900: return false; KUDr@3900: } KUDr@3900: // in the middle of the bridge - when we came here, it should be ok KUDr@3900: } else { KUDr@3900: // different owner, on the bridge ramp KUDr@3900: return false; KUDr@3900: } KUDr@3900: } else { KUDr@3900: // different owner, not a bridge KUDr@3900: return false; KUDr@3900: } KUDr@3900: } KUDr@3900: KUDr@3900: // rail transport is possible only on compatible rail types KUDr@3900: if (IsRailTT()) { KUDr@3900: RailType rail_type = GetTileRailType(m_new_tile, DiagdirToDiagTrackdir(m_exitdir)); KUDr@3900: if (((1 << rail_type) & m_veh->u.rail.compatible_railtypes) == 0) { KUDr@3900: // incompatible rail type KUDr@3900: return false; KUDr@3900: } KUDr@3900: } KUDr@3900: KUDr@3900: // tunnel tiles can be entered only from proper direction KUDr@3900: if (!IsWaterTT() && !m_is_tunnel && IsTunnelTile(m_new_tile)) { KUDr@3900: DiagDirection tunnel_enterdir = GetTunnelDirection(m_new_tile); KUDr@3900: if (tunnel_enterdir != m_exitdir) KUDr@3900: return false; KUDr@3900: } KUDr@3900: return true; KUDr@3900: } KUDr@3900: KUDr@3900: FORCEINLINE bool EnteredRailDepot() KUDr@3900: { KUDr@3900: // rail depots cause reversing KUDr@3900: if (IsRailTT() && IsTileDepotType(m_old_tile, TT())) { KUDr@3900: DiagDirection exitdir = GetRailDepotDirection(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@3900: m_tunnel_tiles_skipped = 0; KUDr@3900: m_is_tunnel = false; KUDr@3900: return true; KUDr@3900: } KUDr@3900: } KUDr@3900: return false; KUDr@3900: } KUDr@3900: public: KUDr@3900: /** Helper for pathfinders - get min/max speed on the m_old_tile/m_old_td */ KUDr@3900: int GetSpeedLimit(int *pmin_speed = NULL) 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 KUDr@3900: if (IsBridgeTile(m_old_tile) && !IsWaterTT() && IsDiagonalTrackdir(m_old_td)) { KUDr@3900: bool is_on_bridge = true; KUDr@3900: if (IsBridgeMiddle(m_old_tile)) { KUDr@3900: // get track axis KUDr@3900: Axis track_axis = DiagDirToAxis(TrackdirToExitdir(m_old_td)); KUDr@3900: // get under-bridge axis KUDr@3900: Axis bridge_axis = GetBridgeAxis(m_old_tile); KUDr@3900: if (track_axis != bridge_axis) is_on_bridge = false; KUDr@3900: } KUDr@3900: if (is_on_bridge) { KUDr@3900: int spd = _bridge[GetBridgeType(m_old_tile)].speed; KUDr@3900: if (IsRoadTT()) spd *= 2; KUDr@3900: if (max_speed > spd) max_speed = spd; KUDr@3900: } 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 */