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
15 #include "../map/road.h"
17 #include "../roadveh.h"
18 #include "../station_base.h"
20 #include "../tunnelbridge.h"
21 #include "../depot_func.h"
22 #include "../bridge.h"
23 #include "pathfinder_type.h"
24 #include "pf_performance_timer.hpp"
27 * Track follower helper template class (can serve pathfinders and vehicle
28 * controllers). See 6 different typedefs below for 3 different transport
29 * types w/ or w/o 90-deg turns allowed
31 template <TransportType Ttr_type_
, typename VehicleType
, bool Twormhole
= false>
56 const VehicleType
*m_veh
; ///< moving vehicle
57 Owner m_veh_owner
; ///< owner of the vehicle
58 PFPos m_old
; ///< the origin (vehicle moved from) before move
59 PFNewPos m_new
; ///< the new tile (the vehicle has entered)
60 DiagDirection m_exitdir
; ///< exit direction (leaving the old tile)
61 TileFlag m_flag
; ///< last turn passed station, tunnel or bridge
62 int m_tiles_skipped
; ///< number of skipped tunnel or station tiles
64 CPerformanceTimer
*m_pPerf
;
65 RailTypes m_railtypes
;
69 inline CFollowTrack(const VehicleType
*v
= NULL
, bool allow_90deg
= true, bool mask_reserved
= false, RailTypes railtype_override
= INVALID_RAILTYPES
, CPerformanceTimer
*pPerf
= NULL
)
71 assert(!IsRailTT() || (v
!= NULL
&& v
->type
== VEH_TRAIN
));
73 Init(v
!= NULL
? v
->owner
: INVALID_OWNER
, allow_90deg
, mask_reserved
, IsRailTT() && railtype_override
== INVALID_RAILTYPES
? Train::From(v
)->compatible_railtypes
: railtype_override
, pPerf
);
76 inline CFollowTrack(Owner o
, bool allow_90deg
= true, bool mask_reserved
= false, RailTypes railtype_override
= INVALID_RAILTYPES
, CPerformanceTimer
*pPerf
= NULL
)
79 Init(o
, allow_90deg
, mask_reserved
, railtype_override
, pPerf
);
82 inline void Init(Owner o
, bool allow_90deg
, bool mask_reserved
, RailTypes railtype_override
, CPerformanceTimer
*pPerf
)
84 assert((!IsRoadTT() || m_veh
!= NULL
) && (!IsRailTT() || railtype_override
!= INVALID_RAILTYPES
));
87 m_railtypes
= railtype_override
;
88 m_allow_90deg
= allow_90deg
;
89 m_mask_reserved
= mask_reserved
;
92 inline static TransportType
TT() { return Ttr_type_
; }
93 inline static bool IsWaterTT() { return TT() == TRANSPORT_WATER
; }
94 inline static bool IsRailTT() { return TT() == TRANSPORT_RAIL
; }
95 inline bool IsTram() { return IsRoadTT() && HasBit(RoadVehicle::From(m_veh
)->compatible_roadtypes
, ROADTYPE_TRAM
); }
96 inline static bool IsRoadTT() { return TT() == TRANSPORT_ROAD
; }
97 inline static bool StepWormhole() {return Twormhole
;}
100 inline TrackdirBits
GetTrackStatusTrackdirBits(TileIndex tile
) const
103 return TrackStatusToTrackdirBits(GetTileRailwayStatus(tile
));
104 } else if (IsRoadTT()) {
105 return TrackStatusToTrackdirBits(GetTileRoadStatus(tile
, m_veh
!= NULL
? RoadVehicle::From(m_veh
)->compatible_roadtypes
: 0));
107 return TrackStatusToTrackdirBits(GetTileWaterwayStatus(tile
));
112 /** Tests if a tile is a road tile with a single tramtrack (tram can reverse) */
113 inline DiagDirection
GetSingleTramBit(TileIndex tile
)
115 assert(IsTram()); // this function shouldn't be called in other cases
117 if (IsRoadTile(tile
)) {
118 RoadBits rb
= GetRoadBits(tile
, ROADTYPE_TRAM
);
120 case ROAD_NW
: return DIAGDIR_NW
;
121 case ROAD_SW
: return DIAGDIR_SW
;
122 case ROAD_SE
: return DIAGDIR_SE
;
123 case ROAD_NE
: return DIAGDIR_NE
;
127 return INVALID_DIAGDIR
;
131 * main follower routine. Fills all members and return true on success.
132 * Otherwise returns false if track can't be followed.
134 inline bool Follow(const PFPos
&pos
)
138 m_exitdir
= TrackdirToExitdir(m_old
.td
);
140 if (m_old
.InWormhole()) {
143 switch (CheckOldTile()) {
148 m_new
.tile
= m_old
.tile
;
149 m_new
.wormhole
= INVALID_TILE
;
150 m_new
.td
= ReverseTrackdir(m_old
.td
);
151 m_new
.trackdirs
= TrackdirToTrackdirBits(m_new
.td
);
152 m_exitdir
= ReverseDiagDir(m_exitdir
);
162 if (m_new
.InWormhole()) {
163 assert(StepWormhole());
164 m_new
.td
= DiagDirToDiagTrackdir(m_exitdir
);
165 m_new
.trackdirs
= TrackdirToTrackdirBits(m_new
.td
);
169 if (!CheckNewTile() || (m_new
.trackdirs
&= DiagdirReachesTrackdirs(m_exitdir
)) == TRACKDIR_BIT_NONE
) {
170 /* In case we can't enter the next tile, but are
171 * a normal road vehicle, then we can actually
172 * try to reverse as this is the end of the road.
173 * Trams can only turn on the appropriate bits in
174 * which case reaching this would mean a dead end
175 * near a building and in that case there would
176 * a "false" QueryNewTileTrackStatus result and
177 * as such reversing is already tried. The fact
178 * that function failed can have to do with a
179 * missing road bit, or inability to connect the
180 * different bits due to slopes. */
183 if (!m_allow_90deg
) {
184 m_new
.trackdirs
&= (TrackdirBits
)~(int)TrackdirCrossesTrackdirs(m_old
.td
);
185 if (m_new
.trackdirs
== TRACKDIR_BIT_NONE
) {
190 /* Check if the resulting trackdirs is a single trackdir */
195 inline bool FollowNext()
197 assert(m_new
.tile
!= INVALID_TILE
);
198 assert(m_new
.IsTrackdirSet());
199 return Follow(m_new
);
202 inline void SetPos(const PFPos
&pos
)
204 m_new
.PFPos::operator = (pos
);
205 m_new
.trackdirs
= TrackdirToTrackdirBits(pos
.td
);
208 inline bool MaskReservedTracks()
210 if (!m_mask_reserved
) return true;
212 if (m_flag
== TF_STATION
) {
213 /* Check skipped station tiles as well. */
214 TileIndexDiff diff
= TileOffsByDiagDir(m_exitdir
);
215 for (TileIndex tile
= m_new
.tile
- diff
* m_tiles_skipped
; tile
!= m_new
.tile
; tile
+= diff
) {
216 if (HasStationReservation(tile
)) {
217 m_new
.td
= INVALID_TRACKDIR
;
218 m_new
.trackdirs
= TRACKDIR_BIT_NONE
;
225 if (m_new
.InWormhole()) {
226 assert(m_new
.IsTrackdirSet());
227 if (HasReservedPos(m_new
)) {
228 m_new
.td
= INVALID_TRACKDIR
;
229 m_new
.trackdirs
= TRACKDIR_BIT_NONE
;
237 TrackBits reserved
= GetReservedTrackbits(m_new
.tile
);
238 /* Mask already reserved trackdirs. */
239 m_new
.trackdirs
&= ~TrackBitsToTrackdirBits(reserved
);
240 /* Mask out all trackdirs that conflict with the reservation. */
242 FOR_EACH_SET_TRACK(t
, TrackdirBitsToTrackBits(m_new
.trackdirs
)) {
243 if (TracksOverlap(reserved
| TrackToTrackBits(t
))) m_new
.trackdirs
&= ~TrackToTrackdirBits(t
);
245 if (m_new
.trackdirs
== TRACKDIR_BIT_NONE
) {
246 m_new
.td
= INVALID_TRACKDIR
;
250 /* Check if the resulting trackdirs is a single trackdir */
256 /** Follow m_exitdir from m_old and fill m_new.tile and m_tiles_skipped */
257 inline void FollowTileExit()
259 assert(!m_old
.InWormhole());
260 /* extra handling for bridges in our direction */
261 if (IsBridgeHeadTile(m_old
.tile
)) {
262 if (m_exitdir
== GetTunnelBridgeDirection(m_old
.tile
)) {
263 /* we are entering the bridge */
265 m_new
.tile
= GetOtherBridgeEnd(m_old
.tile
);
266 m_tiles_skipped
= GetTunnelBridgeLength(m_new
.tile
, m_old
.tile
);
267 if (StepWormhole() && m_tiles_skipped
> 0) {
269 m_new
.wormhole
= m_new
.tile
;
270 m_new
.tile
= TILE_ADD(m_new
.tile
, TileOffsByDiagDir(ReverseDiagDir(m_exitdir
)));
272 m_new
.wormhole
= INVALID_TILE
;
276 /* extra handling for tunnels in our direction */
277 } else if (IsTunnelTile(m_old
.tile
)) {
278 DiagDirection enterdir
= GetTunnelBridgeDirection(m_old
.tile
);
279 if (enterdir
== m_exitdir
) {
280 /* we are entering the tunnel */
282 m_new
.tile
= GetOtherTunnelEnd(m_old
.tile
);
283 m_tiles_skipped
= GetTunnelBridgeLength(m_new
.tile
, m_old
.tile
);
284 if (StepWormhole() && m_tiles_skipped
> 0) {
286 m_new
.wormhole
= m_new
.tile
;
287 m_new
.tile
= TILE_ADD(m_new
.tile
, TileOffsByDiagDir(ReverseDiagDir(m_exitdir
)));
289 m_new
.wormhole
= INVALID_TILE
;
293 assert(ReverseDiagDir(enterdir
) == m_exitdir
);
296 /* normal or station tile, do one step */
297 TileIndexDiff diff
= TileOffsByDiagDir(m_exitdir
);
298 m_new
.tile
= TILE_ADD(m_old
.tile
, diff
);
299 m_new
.wormhole
= INVALID_TILE
;
301 /* special handling for stations */
302 if (IsRailTT() && HasStationTileRail(m_new
.tile
)) {
304 } else if (IsRoadTT() && IsRoadStopTile(m_new
.tile
)) {
313 /** Follow m_old when in a wormhole */
314 inline void FollowWormhole()
316 assert(m_old
.InWormhole());
317 assert(IsBridgeHeadTile(m_old
.wormhole
) || IsTunnelTile(m_old
.wormhole
));
319 m_new
.tile
= m_old
.wormhole
;
320 m_new
.wormhole
= INVALID_TILE
;
321 m_flag
= IsTileSubtype(m_old
.wormhole
, TT_BRIDGE
) ? TF_BRIDGE
: TF_TUNNEL
;
322 m_tiles_skipped
= GetTunnelBridgeLength(m_new
.tile
, m_old
.tile
);
325 /** check old tile */
326 inline TileResult
CheckOldTile()
328 assert(((GetTrackStatusTrackdirBits(m_old
.tile
) & TrackdirToTrackdirBits(m_old
.td
)) != 0) ||
329 (IsTram() && GetSingleTramBit(m_old
.tile
) != INVALID_DIAGDIR
)); // Disable the assertion for single tram bits
331 /* rail and road depots cause reversing */
332 if (!IsWaterTT() && IsDepotTypeTile(m_old
.tile
, TT())) {
333 DiagDirection exitdir
= GetGroundDepotDirection(m_old
.tile
);
334 if (exitdir
!= m_exitdir
) {
335 assert(exitdir
== ReverseDiagDir(m_exitdir
));
340 /* single tram bits cause reversing */
341 if (IsTram() && GetSingleTramBit(m_old
.tile
) == ReverseDiagDir(m_exitdir
)) {
345 /* road stop can be left at one direction only unless it's a drive-through stop */
346 if (IsRoadTT() && IsStandardRoadStopTile(m_old
.tile
)) {
347 DiagDirection exitdir
= GetRoadStopDir(m_old
.tile
);
348 if (exitdir
!= m_exitdir
) {
353 /* single tram bits can only be left in one direction */
355 DiagDirection single_tram
= GetSingleTramBit(m_old
.tile
);
356 if (single_tram
!= INVALID_DIAGDIR
&& single_tram
!= m_exitdir
) {
361 /* road depots can be also left in one direction only */
362 if (IsRoadTT() && IsDepotTypeTile(m_old
.tile
, TT())) {
363 DiagDirection exitdir
= GetGroundDepotDirection(m_old
.tile
);
364 if (exitdir
!= m_exitdir
) {
372 /** stores track status (available trackdirs) for the new tile into m_new.trackdirs */
373 inline bool CheckNewTile()
376 CPerfStart
perf(*m_pPerf
);
377 if (IsRailTT() && IsNormalRailTile(m_new
.tile
)) {
378 m_new
.trackdirs
= TrackBitsToTrackdirBits(GetTrackBits(m_new
.tile
));
380 m_new
.trackdirs
= GetTrackStatusTrackdirBits(m_new
.tile
);
382 if (IsTram() && m_new
.trackdirs
== 0) {
383 /* GetTileRoadStatus() returns 0 for single tram bits.
384 * As we cannot change it there (easily) without breaking something, change it here */
385 switch (GetSingleTramBit(m_new
.tile
)) {
388 m_new
.trackdirs
= TRACKDIR_BIT_X_NE
| TRACKDIR_BIT_X_SW
;
393 m_new
.trackdirs
= TRACKDIR_BIT_Y_NW
| TRACKDIR_BIT_Y_SE
;
401 if (m_new
.trackdirs
== TRACKDIR_BIT_NONE
) return false;
404 if (IsRoadTT() && IsStandardRoadStopTile(m_new
.tile
)) {
405 /* road stop can be entered from one direction only unless it's a drive-through stop */
406 DiagDirection exitdir
= GetRoadStopDir(m_new
.tile
);
407 if (ReverseDiagDir(exitdir
) != m_exitdir
) {
413 /* single tram bits can only be entered from one direction */
415 DiagDirection single_tram
= GetSingleTramBit(m_new
.tile
);
416 if (single_tram
!= INVALID_DIAGDIR
&& single_tram
!= ReverseDiagDir(m_exitdir
)) {
422 /* road and rail depots can also be entered from one direction only */
423 if (IsRoadTT() && IsDepotTypeTile(m_new
.tile
, TT())) {
424 DiagDirection exitdir
= GetGroundDepotDirection(m_new
.tile
);
425 if (ReverseDiagDir(exitdir
) != m_exitdir
) {
429 /* don't try to enter other company's depots */
430 if (GetTileOwner(m_new
.tile
) != m_veh_owner
) {
435 if (IsRailTT() && IsDepotTypeTile(m_new
.tile
, TT())) {
436 DiagDirection exitdir
= GetGroundDepotDirection(m_new
.tile
);
437 if (ReverseDiagDir(exitdir
) != m_exitdir
) {
443 /* rail transport is possible only on tiles with the same owner as vehicle */
444 if (IsRailTT() && GetTileOwner(m_new
.tile
) != m_veh_owner
) {
445 /* different owner */
450 /* rail transport is possible only on compatible rail types */
453 if (IsRailwayTile(m_new
.tile
)) {
454 rail_type
= GetSideRailType(m_new
.tile
, ReverseDiagDir(m_exitdir
));
455 if (rail_type
== INVALID_RAILTYPE
) {
460 rail_type
= GetTileRailType(m_new
.tile
);
462 if (!HasBit(m_railtypes
, rail_type
)) {
463 /* incompatible rail type */
464 m_err
= EC_RAIL_TYPE
;
469 /* tunnel holes and bridge ramps can be entered only from proper direction */
470 if (IsTunnelTile(m_new
.tile
)) {
471 if (m_flag
!= TF_TUNNEL
) {
472 DiagDirection tunnel_enterdir
= GetTunnelBridgeDirection(m_new
.tile
);
473 if (tunnel_enterdir
!= m_exitdir
) {
478 } else if (IsBridgeHeadTile(m_new
.tile
)) {
479 if (m_flag
!= TF_BRIDGE
) {
480 DiagDirection ramp_enderdir
= GetTunnelBridgeDirection(m_new
.tile
);
481 if (ramp_enderdir
== ReverseDiagDir(m_exitdir
)) {
488 /* special handling for rail stations - get to the end of platform */
489 if (IsRailTT() && m_flag
== TF_STATION
) {
490 /* entered railway station
491 * get platform length */
492 uint length
= BaseStation::GetByTile(m_new
.tile
)->GetPlatformLength(m_new
.tile
, TrackdirToExitdir(m_old
.td
));
493 /* how big step we must do to get to the last platform tile; */
494 m_tiles_skipped
= length
- 1;
495 /* move to the platform end */
496 TileIndexDiff diff
= TileOffsByDiagDir(m_exitdir
);
497 diff
*= m_tiles_skipped
;
498 m_new
.tile
= TILE_ADD(m_new
.tile
, diff
);
505 /** return true if we successfully reversed at end of road/track */
506 inline bool TryReverse()
508 if (IsRoadTT() && !IsTram()) {
509 /* if we reached the end of road, we can reverse the RV and continue moving */
510 m_exitdir
= ReverseDiagDir(m_exitdir
);
511 /* new tile will be the same as old one */
512 m_new
.tile
= m_old
.tile
;
513 m_new
.wormhole
= INVALID_TILE
;
514 /* set new trackdir bits to all reachable trackdirs */
515 m_new
.trackdirs
= GetTrackStatusTrackdirBits(m_new
.tile
);
516 m_new
.trackdirs
&= DiagdirReachesTrackdirs(m_exitdir
);
517 /* we always have some trackdirs reachable after reversal */
518 assert(m_new
.trackdirs
!= TRACKDIR_BIT_NONE
);
519 /* check if the resulting trackdirs is a single trackdir */
523 m_new
.td
= INVALID_TRACKDIR
;
529 /** Helper for pathfinders - get min/max speed on m_old */
530 int GetSpeedLimit(int *pmin_speed
= NULL
) const
533 int max_speed
= INT_MAX
; // no limit
536 /* Check for on-bridge and railtype speed limit */
537 TileIndex bridge_tile
;
540 if (!m_old
.InWormhole()) {
541 bridge_tile
= IsRailBridgeTile(m_old
.tile
) ? m_old
.tile
: INVALID_TILE
;
542 rt
= GetRailType(m_old
.tile
, TrackdirToTrack(m_old
.td
));
543 } else if (IsTileSubtype(m_old
.wormhole
, TT_BRIDGE
)) {
544 bridge_tile
= m_old
.wormhole
;
545 rt
= GetBridgeRailType(bridge_tile
);
547 bridge_tile
= INVALID_TILE
;
548 rt
= GetRailType(m_old
.wormhole
);
551 /* Check for on-bridge speed limit */
552 if (bridge_tile
!= INVALID_TILE
) {
553 int spd
= GetBridgeSpec(GetRailBridgeType(bridge_tile
))->speed
;
554 if (max_speed
> spd
) max_speed
= spd
;
557 /* Check for speed limit imposed by railtype */
558 uint16 rail_speed
= GetRailTypeInfo(rt
)->max_speed
;
559 if (rail_speed
> 0) max_speed
= min(max_speed
, rail_speed
);
560 } else if (IsRoadTT()) {
561 /* Check for on-bridge speed limit */
562 if (IsRoadBridgeTile(m_old
.tile
)) {
563 int spd
= 2 * GetBridgeSpec(GetRoadBridgeType(m_old
.tile
))->speed
;
564 if (max_speed
> spd
) max_speed
= spd
;
568 /* if min speed was requested, return it */
569 if (pmin_speed
!= NULL
) *pmin_speed
= min_speed
;
574 template <TransportType Ttr_type_
, typename VehicleType
, bool T90deg_turns_allowed
, bool Twormhole
= false, bool Tmask_reserved_tracks
= false>
575 struct CFollowTrackT
: CFollowTrack
<Ttr_type_
, VehicleType
, Twormhole
>
577 inline CFollowTrackT(const VehicleType
*v
= NULL
, RailTypes railtype_override
= INVALID_RAILTYPES
, CPerformanceTimer
*pPerf
= NULL
)
578 : CFollowTrack
<Ttr_type_
, VehicleType
, Twormhole
>(v
, T90deg_turns_allowed
, Tmask_reserved_tracks
, railtype_override
, pPerf
)
582 inline static bool Allow90degTurns() { return T90deg_turns_allowed
; }
583 inline static bool DoTrackMasking() { return Tmask_reserved_tracks
; }
586 typedef CFollowTrackT
<TRANSPORT_WATER
, Ship
, true > CFollowTrackWater90
;
587 typedef CFollowTrackT
<TRANSPORT_WATER
, Ship
, false> CFollowTrackWaterNo90
;
589 typedef CFollowTrackT
<TRANSPORT_ROAD
, RoadVehicle
, true > CFollowTrackRoad
;
591 typedef CFollowTrackT
<TRANSPORT_RAIL
, Train
, true, true> CFollowTrackRail90
;
592 typedef CFollowTrackT
<TRANSPORT_RAIL
, Train
, false, true> CFollowTrackRailNo90
;
593 typedef CFollowTrackT
<TRANSPORT_RAIL
, Train
, true, true, true > CFollowTrackFreeRail90
;
594 typedef CFollowTrackT
<TRANSPORT_RAIL
, Train
, false, true, true > CFollowTrackFreeRailNo90
;
596 struct CFollowTrackRail
: CFollowTrack
<TRANSPORT_RAIL
, Train
, true>
598 inline CFollowTrackRail(const Train
*v
= NULL
, bool allow_90deg
= true, bool railtype_override
= false)
599 : CFollowTrack
<TRANSPORT_RAIL
, Train
, true>(v
, allow_90deg
, false, railtype_override
? GetRailTypeInfo(v
->railtype
)->compatible_railtypes
: INVALID_RAILTYPES
)
603 inline CFollowTrackRail(Owner o
, bool allow_90deg
= true, RailTypes railtype_override
= INVALID_RAILTYPES
)
604 : CFollowTrack
<TRANSPORT_RAIL
, Train
, true>(o
, allow_90deg
, false, railtype_override
)
609 #endif /* FOLLOW_TRACK_HPP */