4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file follow_track.hpp Template function for track followers */
12 #ifndef FOLLOW_TRACK_HPP
13 #define FOLLOW_TRACK_HPP
16 #include "../roadveh.h"
17 #include "../station_base.h"
19 #include "../tunnelbridge.h"
20 #include "../tunnelbridge_map.h"
21 #include "../depot_map.h"
22 #include "pf_performance_timer.hpp"
25 * Track follower helper template class (can serve pathfinders and vehicle
26 * controllers). See 6 different typedefs below for 3 different transport
27 * types w/ or w/o 90-deg turns allowed
29 template <TransportType Ttr_type_
, typename VehicleType
, bool T90deg_turns_allowed_
= true, bool Tmask_reserved_tracks
= false>
41 const VehicleType
*m_veh
; ///< moving vehicle
42 Owner m_veh_owner
; ///< owner of the vehicle
43 TileIndex m_old_tile
; ///< the origin (vehicle moved from) before move
44 Trackdir m_old_td
; ///< the trackdir (the vehicle was on) before move
45 TileIndex m_new_tile
; ///< the new tile (the vehicle has entered)
46 TrackdirBits m_new_td_bits
; ///< the new set of available trackdirs
47 DiagDirection m_exitdir
; ///< exit direction (leaving the old tile)
48 bool m_is_tunnel
; ///< last turn passed tunnel
49 bool m_is_bridge
; ///< last turn passed bridge ramp
50 bool m_is_station
; ///< last turn passed station
51 int m_tiles_skipped
; ///< number of skipped tunnel or station tiles
53 CPerformanceTimer
*m_pPerf
;
54 RailTypes m_railtypes
;
56 inline CFollowTrackT(const VehicleType
*v
= NULL
, RailTypes railtype_override
= INVALID_RAILTYPES
, CPerformanceTimer
*pPerf
= NULL
)
58 Init(v
, railtype_override
, pPerf
);
61 inline CFollowTrackT(Owner o
, RailTypes railtype_override
= INVALID_RAILTYPES
, CPerformanceTimer
*pPerf
= NULL
)
64 Init(o
, railtype_override
, pPerf
);
67 inline void Init(const VehicleType
*v
, RailTypes railtype_override
, CPerformanceTimer
*pPerf
)
69 assert(!IsRailTT() || (v
!= NULL
&& v
->type
== VEH_TRAIN
));
71 Init(v
!= NULL
? v
->owner
: INVALID_OWNER
, IsRailTT() && railtype_override
== INVALID_RAILTYPES
? Train::From(v
)->compatible_railtypes
: railtype_override
, pPerf
);
74 inline void Init(Owner o
, RailTypes railtype_override
, CPerformanceTimer
*pPerf
)
76 assert((!IsRoadTT() || m_veh
!= NULL
) && (!IsRailTT() || railtype_override
!= INVALID_RAILTYPES
));
79 /* don't worry, all is inlined so compiler should remove unnecessary initializations */
80 m_old_tile
= INVALID_TILE
;
81 m_old_td
= INVALID_TRACKDIR
;
82 m_new_tile
= INVALID_TILE
;
83 m_new_td_bits
= TRACKDIR_BIT_NONE
;
84 m_exitdir
= INVALID_DIAGDIR
;
85 m_is_station
= m_is_bridge
= m_is_tunnel
= false;
88 m_railtypes
= railtype_override
;
91 inline static TransportType
TT() { return Ttr_type_
; }
92 inline static bool IsWaterTT() { return TT() == TRANSPORT_WATER
; }
93 inline static bool IsRailTT() { return TT() == TRANSPORT_RAIL
; }
94 inline bool IsTram() { return IsRoadTT() && HasBit(RoadVehicle::From(m_veh
)->compatible_roadtypes
, ROADTYPE_TRAM
); }
95 inline static bool IsRoadTT() { return TT() == TRANSPORT_ROAD
; }
96 inline static bool Allow90degTurns() { return T90deg_turns_allowed_
; }
97 inline static bool DoTrackMasking() { return Tmask_reserved_tracks
; }
99 /** Tests if a tile is a road tile with a single tramtrack (tram can reverse) */
100 inline DiagDirection
GetSingleTramBit(TileIndex tile
)
102 assert(IsTram()); // this function shouldn't be called in other cases
104 const bool is_bridge
= IsRoadCustomBridgeHeadTile(tile
);
105 if (is_bridge
|| IsNormalRoadTile(tile
)) {
106 RoadBits rb
= is_bridge
? GetCustomBridgeHeadRoadBits(tile
, ROADTYPE_TRAM
) : GetRoadBits(tile
, ROADTYPE_TRAM
);
108 case ROAD_NW
: return DIAGDIR_NW
;
109 case ROAD_SW
: return DIAGDIR_SW
;
110 case ROAD_SE
: return DIAGDIR_SE
;
111 case ROAD_NE
: return DIAGDIR_NE
;
115 return INVALID_DIAGDIR
;
119 * main follower routine. Fills all members and return true on success.
120 * Otherwise returns false if track can't be followed.
122 inline bool Follow(TileIndex old_tile
, Trackdir old_td
)
124 m_old_tile
= old_tile
;
127 assert((IsTram() && GetSingleTramBit(m_old_tile
) != INVALID_DIAGDIR
) || // Disable the assertion for single tram bits
128 ((TrackStatusToTrackdirBits(GetTileTrackStatus(m_old_tile
, TT(), IsRoadTT() ? RoadVehicle::From(m_veh
)->compatible_roadtypes
: 0)) & TrackdirToTrackdirBits(m_old_td
)) != 0));
129 m_exitdir
= TrackdirToExitdir(m_old_td
);
130 if (ForcedReverse()) return true;
131 if (!CanExitOldTile()) return false;
133 if (!QueryNewTileTrackStatus()) return TryReverse();
134 m_new_td_bits
&= DiagdirReachesTrackdirs(m_exitdir
);
135 if (m_new_td_bits
== TRACKDIR_BIT_NONE
|| !CanEnterNewTile()) {
136 /* In case we can't enter the next tile, but are
137 * a normal road vehicle, then we can actually
138 * try to reverse as this is the end of the road.
139 * Trams can only turn on the appropriate bits in
140 * which case reaching this would mean a dead end
141 * near a building and in that case there would
142 * a "false" QueryNewTileTrackStatus result and
143 * as such reversing is already tried. The fact
144 * that function failed can have to do with a
145 * missing road bit, or inability to connect the
146 * different bits due to slopes. */
147 if (IsRoadTT() && !IsTram() && TryReverse()) return true;
149 /* CanEnterNewTile already set a reason.
150 * Do NOT overwrite it (important for example for EC_RAIL_TYPE).
151 * Only set a reason if CanEnterNewTile was not called */
152 if (m_new_td_bits
== TRACKDIR_BIT_NONE
) m_err
= EC_NO_WAY
;
156 if (!Allow90degTurns()) {
157 m_new_td_bits
&= (TrackdirBits
)~(int)TrackdirCrossesTrackdirs(m_old_td
);
158 if (m_new_td_bits
== TRACKDIR_BIT_NONE
) {
166 inline bool MaskReservedTracks()
168 if (!DoTrackMasking()) return true;
171 /* Check skipped station tiles as well. */
172 TileIndexDiff diff
= TileOffsByDiagDir(m_exitdir
);
173 for (TileIndex tile
= m_new_tile
- diff
* m_tiles_skipped
; tile
!= m_new_tile
; tile
+= diff
) {
174 if (HasStationReservation(tile
)) {
175 m_new_td_bits
= TRACKDIR_BIT_NONE
;
182 TrackBits reserved
= GetReservedTrackbits(m_new_tile
);
183 /* Mask already reserved trackdirs. */
184 m_new_td_bits
&= ~TrackBitsToTrackdirBits(reserved
);
185 /* Mask out all trackdirs that conflict with the reservation. */
187 FOR_EACH_SET_TRACK(t
, TrackdirBitsToTrackBits(m_new_td_bits
)) {
188 if (TracksOverlap(reserved
| TrackToTrackBits(t
))) m_new_td_bits
&= ~TrackToTrackdirBits(t
);
190 if (m_new_td_bits
== TRACKDIR_BIT_NONE
) {
198 /** Follow the m_exitdir from m_old_tile and fill m_new_tile and m_tiles_skipped */
199 inline void FollowTileExit()
201 m_is_station
= m_is_bridge
= m_is_tunnel
= false;
204 /* extra handling for tunnels and bridges in our direction */
205 if (IsTileType(m_old_tile
, MP_TUNNELBRIDGE
)) {
206 DiagDirection enterdir
= GetTunnelBridgeDirection(m_old_tile
);
207 if (enterdir
== m_exitdir
) {
208 /* we are entering the tunnel / bridge */
209 if (IsTunnel(m_old_tile
)) {
211 m_new_tile
= GetOtherTunnelEnd(m_old_tile
);
212 } else { // IsBridge(m_old_tile)
214 m_new_tile
= GetOtherBridgeEnd(m_old_tile
);
216 m_tiles_skipped
= GetTunnelBridgeLength(m_new_tile
, m_old_tile
);
219 if (!IsRoadCustomBridgeHeadTile(m_old_tile
)) assert(ReverseDiagDir(enterdir
) == m_exitdir
);
222 /* normal or station tile, do one step */
223 TileIndexDiff diff
= TileOffsByDiagDir(m_exitdir
);
224 m_new_tile
= TILE_ADD(m_old_tile
, diff
);
226 /* special handling for stations */
227 if (IsRailTT() && HasStationTileRail(m_new_tile
)) {
229 } else if (IsRoadTT() && IsRoadStopTile(m_new_tile
)) {
232 m_is_station
= false;
236 /** stores track status (available trackdirs) for the new tile into m_new_td_bits */
237 inline bool QueryNewTileTrackStatus()
239 CPerfStart
perf(m_pPerf
);
240 if (IsRailTT() && IsPlainRailTile(m_new_tile
)) {
241 m_new_td_bits
= (TrackdirBits
)(GetTrackBits(m_new_tile
) * 0x101);
243 m_new_td_bits
= TrackStatusToTrackdirBits(GetTileTrackStatus(m_new_tile
, TT(), IsRoadTT() ? RoadVehicle::From(m_veh
)->compatible_roadtypes
: 0));
245 if (IsTram() && m_new_td_bits
== 0) {
246 /* GetTileTrackStatus() returns 0 for single tram bits.
247 * As we cannot change it there (easily) without breaking something, change it here */
248 switch (GetSingleTramBit(m_new_tile
)) {
251 m_new_td_bits
= TRACKDIR_BIT_X_NE
| TRACKDIR_BIT_X_SW
;
256 m_new_td_bits
= TRACKDIR_BIT_Y_NW
| TRACKDIR_BIT_Y_SE
;
263 return (m_new_td_bits
!= TRACKDIR_BIT_NONE
);
266 /** return true if we can leave m_old_tile in m_exitdir */
267 inline bool CanExitOldTile()
269 /* road stop can be left at one direction only unless it's a drive-through stop */
270 if (IsRoadTT() && IsStandardRoadStopTile(m_old_tile
)) {
271 DiagDirection exitdir
= GetRoadStopDir(m_old_tile
);
272 if (exitdir
!= m_exitdir
) {
278 /* single tram bits can only be left in one direction */
280 DiagDirection single_tram
= GetSingleTramBit(m_old_tile
);
281 if (single_tram
!= INVALID_DIAGDIR
&& single_tram
!= m_exitdir
) {
287 /* road depots can be also left in one direction only */
288 if (IsRoadTT() && IsDepotTypeTile(m_old_tile
, TT())) {
289 DiagDirection exitdir
= GetRoadDepotDirection(m_old_tile
);
290 if (exitdir
!= m_exitdir
) {
298 /** return true if we can enter m_new_tile from m_exitdir */
299 inline bool CanEnterNewTile()
301 if (IsRoadTT() && IsStandardRoadStopTile(m_new_tile
)) {
302 /* road stop can be entered from one direction only unless it's a drive-through stop */
303 DiagDirection exitdir
= GetRoadStopDir(m_new_tile
);
304 if (ReverseDiagDir(exitdir
) != m_exitdir
) {
310 /* single tram bits can only be entered from one direction */
312 DiagDirection single_tram
= GetSingleTramBit(m_new_tile
);
313 if (single_tram
!= INVALID_DIAGDIR
&& single_tram
!= ReverseDiagDir(m_exitdir
)) {
319 /* road and rail depots can also be entered from one direction only */
320 if (IsRoadTT() && IsDepotTypeTile(m_new_tile
, TT())) {
321 DiagDirection exitdir
= GetRoadDepotDirection(m_new_tile
);
322 if (ReverseDiagDir(exitdir
) != m_exitdir
) {
326 /* don't try to enter other company's depots */
327 if (GetTileOwner(m_new_tile
) != m_veh_owner
) {
332 if (IsRailTT() && IsDepotTypeTile(m_new_tile
, TT())) {
333 DiagDirection exitdir
= GetRailDepotDirection(m_new_tile
);
334 if (ReverseDiagDir(exitdir
) != m_exitdir
) {
340 /* rail transport is possible only on tiles with the same owner as vehicle */
341 if (IsRailTT() && GetTileOwner(m_new_tile
) != m_veh_owner
) {
342 /* different owner */
347 /* rail transport is possible only on compatible rail types */
349 RailType rail_type
= GetTileRailType(m_new_tile
);
350 if (!HasBit(m_railtypes
, rail_type
)) {
351 /* incompatible rail type */
352 m_err
= EC_RAIL_TYPE
;
357 /* tunnel holes and bridge ramps can be entered only from proper direction */
358 if (IsTileType(m_new_tile
, MP_TUNNELBRIDGE
)) {
359 if (IsTunnel(m_new_tile
)) {
361 DiagDirection tunnel_enterdir
= GetTunnelBridgeDirection(m_new_tile
);
362 if (tunnel_enterdir
!= m_exitdir
) {
367 } else { // IsBridge(m_new_tile)
368 if (!m_is_bridge
&& IsRoadTT() && IsRoadCustomBridgeHeadTile(m_new_tile
)) {
369 if (!(DiagDirToRoadBits(ReverseDiagDir(m_exitdir
)) & GetCustomBridgeHeadRoadBits(m_new_tile
, IsTram() ? ROADTYPE_TRAM
: ROADTYPE_ROAD
))) {
373 } else if (!m_is_bridge
) {
374 DiagDirection ramp_enderdir
= GetTunnelBridgeDirection(m_new_tile
);
375 if (ramp_enderdir
!= m_exitdir
) {
383 /* special handling for rail stations - get to the end of platform */
384 if (IsRailTT() && m_is_station
) {
385 /* entered railway station
386 * get platform length */
387 uint length
= BaseStation::GetByTile(m_new_tile
)->GetPlatformLength(m_new_tile
, TrackdirToExitdir(m_old_td
));
388 /* how big step we must do to get to the last platform tile; */
389 m_tiles_skipped
= length
- 1;
390 /* move to the platform end */
391 TileIndexDiff diff
= TileOffsByDiagDir(m_exitdir
);
392 diff
*= m_tiles_skipped
;
393 m_new_tile
= TILE_ADD(m_new_tile
, diff
);
400 /** return true if we must reverse (in depots and single tram bits) */
401 inline bool ForcedReverse()
403 /* rail and road depots cause reversing */
404 if (!IsWaterTT() && IsDepotTypeTile(m_old_tile
, TT())) {
405 DiagDirection exitdir
= IsRailTT() ? GetRailDepotDirection(m_old_tile
) : GetRoadDepotDirection(m_old_tile
);
406 if (exitdir
!= m_exitdir
) {
408 m_new_tile
= m_old_tile
;
409 m_new_td_bits
= TrackdirToTrackdirBits(ReverseTrackdir(m_old_td
));
412 m_is_tunnel
= m_is_bridge
= m_is_station
= false;
417 /* single tram bits cause reversing */
418 if (IsTram() && GetSingleTramBit(m_old_tile
) == ReverseDiagDir(m_exitdir
)) {
420 m_new_tile
= m_old_tile
;
421 m_new_td_bits
= TrackdirToTrackdirBits(ReverseTrackdir(m_old_td
));
422 m_exitdir
= ReverseDiagDir(m_exitdir
);
424 m_is_tunnel
= m_is_bridge
= m_is_station
= false;
431 /** return true if we successfully reversed at end of road/track */
432 inline bool TryReverse()
434 if (IsRoadTT() && !IsTram()) {
435 /* if we reached the end of road, we can reverse the RV and continue moving */
436 m_exitdir
= ReverseDiagDir(m_exitdir
);
437 /* new tile will be the same as old one */
438 m_new_tile
= m_old_tile
;
439 /* set new trackdir bits to all reachable trackdirs */
440 QueryNewTileTrackStatus();
441 m_new_td_bits
&= DiagdirReachesTrackdirs(m_exitdir
);
442 if (m_new_td_bits
!= TRACKDIR_BIT_NONE
) {
443 /* we have some trackdirs reachable after reversal */
452 /** Helper for pathfinders - get min/max speed on the m_old_tile/m_old_td */
453 int GetSpeedLimit(int *pmin_speed
= NULL
) const
456 int max_speed
= INT_MAX
; // no limit
458 /* Check for on-bridge speed limit */
459 if (!IsWaterTT() && IsBridgeTile(m_old_tile
)) {
460 int spd
= GetBridgeSpec(GetBridgeType(m_old_tile
))->speed
;
461 if (IsRoadTT()) spd
*= 2;
462 if (max_speed
> spd
) max_speed
= spd
;
464 /* Check for speed limit imposed by railtype */
466 uint16 rail_speed
= GetRailTypeInfo(GetRailType(m_old_tile
))->max_speed
;
467 if (rail_speed
> 0) max_speed
= min(max_speed
, rail_speed
);
470 /* if min speed was requested, return it */
471 if (pmin_speed
!= NULL
) *pmin_speed
= min_speed
;
476 typedef CFollowTrackT
<TRANSPORT_WATER
, Ship
, true > CFollowTrackWater
;
477 typedef CFollowTrackT
<TRANSPORT_ROAD
, RoadVehicle
, true > CFollowTrackRoad
;
478 typedef CFollowTrackT
<TRANSPORT_RAIL
, Train
, true > CFollowTrackRail
;
480 typedef CFollowTrackT
<TRANSPORT_WATER
, Ship
, false> CFollowTrackWaterNo90
;
481 typedef CFollowTrackT
<TRANSPORT_ROAD
, RoadVehicle
, false> CFollowTrackRoadNo90
;
482 typedef CFollowTrackT
<TRANSPORT_RAIL
, Train
, false> CFollowTrackRailNo90
;
484 typedef CFollowTrackT
<TRANSPORT_RAIL
, Train
, true, true > CFollowTrackFreeRail
;
485 typedef CFollowTrackT
<TRANSPORT_RAIL
, Train
, false, true > CFollowTrackFreeRailNo90
;
487 #endif /* FOLLOW_TRACK_HPP */