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 pbs.cpp PBS support routines */
13 #include "viewport_func.h"
14 #include "vehicle_func.h"
15 #include "newgrf_station.h"
16 #include "pathfinder/follow_track.hpp"
19 * Get the reserved trackbits for any tile, regardless of type.
21 * @return the reserved trackbits. TRACK_BIT_NONE on nothing reserved or
22 * a tile without rail.
24 TrackBits
GetReservedTrackbits(TileIndex t
)
26 switch (GetTileType(t
)) {
28 if (IsRailDepot(t
)) return GetDepotReservationTrackBits(t
);
29 if (IsPlainRail(t
)) return GetRailReservationTrackBits(t
);
33 if (IsLevelCrossing(t
)) return GetCrossingReservationTrackBits(t
);
37 if (HasStationRail(t
)) return GetStationReservationTrackBits(t
);
41 if (GetTunnelBridgeTransportType(t
) == TRANSPORT_RAIL
) return GetTunnelBridgeReservationTrackBits(t
);
47 return TRACK_BIT_NONE
;
51 * Set the reservation for a complete station platform.
52 * @pre IsRailStationTile(start)
53 * @param start starting tile of the platform
54 * @param dir the direction in which to follow the platform
55 * @param b the state the reservation should be set to
57 void SetRailStationPlatformReservation(TileIndex start
, DiagDirection dir
, bool b
)
59 TileIndex tile
= start
;
60 TileIndexDiff diff
= TileOffsByDiagDir(dir
);
62 assert(IsRailStationTile(start
));
63 assert(GetRailStationAxis(start
) == DiagDirToAxis(dir
));
66 SetRailStationReservation(tile
, b
);
67 MarkTileDirtyByTile(tile
);
68 tile
= TILE_ADD(tile
, diff
);
69 } while (IsCompatibleTrainStationTile(tile
, start
));
73 * Try to reserve a specific track on a tile
74 * @param tile the tile
76 * @param trigger_stations whether to call station randomisation trigger
77 * @return \c true if reservation was successful, i.e. the track was
78 * free and didn't cross any other reserved tracks.
80 bool TryReserveRailTrack(TileIndex tile
, Track t
, bool trigger_stations
)
82 assert((GetTileTrackStatus(tile
, TRANSPORT_RAIL
, 0) & TrackToTrackBits(t
)) != 0);
84 if (_settings_client
.gui
.show_track_reservation
) {
85 /* show the reserved rail if needed */
86 MarkTileDirtyByTile(tile
);
89 switch (GetTileType(tile
)) {
91 if (IsPlainRail(tile
)) return TryReserveTrack(tile
, t
);
92 if (IsRailDepot(tile
)) {
93 if (!HasDepotReservation(tile
)) {
94 SetDepotReservation(tile
, true);
95 MarkTileDirtyByTile(tile
); // some GRFs change their appearance when tile is reserved
102 if (IsLevelCrossing(tile
) && !HasCrossingReservation(tile
)) {
103 SetCrossingReservation(tile
, true);
105 MarkTileDirtyByTile(tile
); // crossing barred, make tile dirty
111 if (HasStationRail(tile
) && !HasStationReservation(tile
)) {
112 SetRailStationReservation(tile
, true);
113 if (trigger_stations
&& IsRailStation(tile
)) TriggerStationRandomisation(NULL
, tile
, SRT_PATH_RESERVATION
);
114 MarkTileDirtyByTile(tile
); // some GRFs need redraw after reserving track
119 case MP_TUNNELBRIDGE
:
120 if (GetTunnelBridgeTransportType(tile
) == TRANSPORT_RAIL
&& !GetTunnelBridgeReservationTrackBits(tile
)) {
121 SetTunnelBridgeReservation(tile
, true);
133 * Lift the reservation of a specific track on a tile
134 * @param tile the tile
137 void UnreserveRailTrack(TileIndex tile
, Track t
)
139 assert((GetTileTrackStatus(tile
, TRANSPORT_RAIL
, 0) & TrackToTrackBits(t
)) != 0);
141 if (_settings_client
.gui
.show_track_reservation
) {
142 MarkTileDirtyByTile(tile
);
145 switch (GetTileType(tile
)) {
147 if (IsRailDepot(tile
)) {
148 SetDepotReservation(tile
, false);
149 MarkTileDirtyByTile(tile
);
152 if (IsPlainRail(tile
)) UnreserveTrack(tile
, t
);
156 if (IsLevelCrossing(tile
)) {
157 SetCrossingReservation(tile
, false);
158 UpdateLevelCrossing(tile
);
163 if (HasStationRail(tile
)) {
164 SetRailStationReservation(tile
, false);
165 MarkTileDirtyByTile(tile
);
169 case MP_TUNNELBRIDGE
:
170 if (GetTunnelBridgeTransportType(tile
) == TRANSPORT_RAIL
) SetTunnelBridgeReservation(tile
, false);
179 /** Follow a reservation starting from a specific tile to the end. */
180 static PBSTileInfo
FollowReservation(Owner o
, RailTypes rts
, TileIndex tile
, Trackdir trackdir
, bool ignore_oneway
= false)
182 TileIndex start_tile
= tile
;
183 Trackdir start_trackdir
= trackdir
;
184 bool first_loop
= true;
186 /* Start track not reserved? This can happen if two trains
187 * are on the same tile. The reservation on the next tile
188 * is not ours in this case, so exit. */
189 if (!HasReservedTracks(tile
, TrackToTrackBits(TrackdirToTrack(trackdir
)))) return PBSTileInfo(tile
, trackdir
, false);
191 /* Do not disallow 90 deg turns as the setting might have changed between reserving and now. */
192 CFollowTrackRail
ft(o
, rts
);
193 while (ft
.Follow(tile
, trackdir
)) {
194 TrackdirBits reserved
= ft
.m_new_td_bits
& TrackBitsToTrackdirBits(GetReservedTrackbits(ft
.m_new_tile
));
196 /* No reservation --> path end found */
197 if (reserved
== TRACKDIR_BIT_NONE
) {
198 if (ft
.m_is_station
) {
199 /* Check skipped station tiles as well, maybe our reservation ends inside the station. */
200 TileIndexDiff diff
= TileOffsByDiagDir(ft
.m_exitdir
);
201 while (ft
.m_tiles_skipped
-- > 0) {
202 ft
.m_new_tile
-= diff
;
203 if (HasStationReservation(ft
.m_new_tile
)) {
204 tile
= ft
.m_new_tile
;
205 trackdir
= DiagDirToDiagTrackdir(ft
.m_exitdir
);
213 /* Can't have more than one reserved trackdir */
214 Trackdir new_trackdir
= FindFirstTrackdir(reserved
);
216 /* One-way signal against us. The reservation can't be ours as it is not
217 * a safe position from our direction and we can never pass the signal. */
218 if (!ignore_oneway
&& HasOnewaySignalBlockingTrackdir(ft
.m_new_tile
, new_trackdir
)) break;
220 tile
= ft
.m_new_tile
;
221 trackdir
= new_trackdir
;
224 /* Update the start tile after we followed the track the first
225 * time. This is necessary because the track follower can skip
226 * tiles (in stations for example) which means that we might
227 * never visit our original starting tile again. */
229 start_trackdir
= trackdir
;
232 /* Loop encountered? */
233 if (tile
== start_tile
&& trackdir
== start_trackdir
) break;
235 /* Depot tile? Can't continue. */
236 if (IsRailDepotTile(tile
)) break;
237 /* Non-pbs signal? Reservation can't continue. */
238 if (IsTileType(tile
, MP_RAILWAY
) && HasSignalOnTrackdir(tile
, trackdir
) && !IsPbsSignal(GetSignalType(tile
, TrackdirToTrack(trackdir
)))) break;
241 return PBSTileInfo(tile
, trackdir
, false);
245 * Helper struct for finding the best matching vehicle on a specific track.
247 struct FindTrainOnTrackInfo
{
248 PBSTileInfo res
; ///< Information about the track.
249 Train
*best
; ///< The currently "best" vehicle we have found.
251 /** Init the best location to NULL always! */
252 FindTrainOnTrackInfo() : best(NULL
) {}
255 /** Callback for Has/FindVehicleOnPos to find a train on a specific track. */
256 static Vehicle
*FindTrainOnTrackEnum(Vehicle
*v
, void *data
)
258 FindTrainOnTrackInfo
*info
= (FindTrainOnTrackInfo
*)data
;
260 if (v
->type
!= VEH_TRAIN
|| (v
->vehstatus
& VS_CRASHED
)) return NULL
;
262 Train
*t
= Train::From(v
);
263 if (t
->track
== TRACK_BIT_WORMHOLE
|| HasBit((TrackBits
)t
->track
, TrackdirToTrack(info
->res
.trackdir
))) {
266 /* ALWAYS return the lowest ID (anti-desync!) */
267 if (info
->best
== NULL
|| t
->index
< info
->best
->index
) info
->best
= t
;
275 * Follow a train reservation to the last tile.
277 * @param v the vehicle
278 * @param train_on_res Is set to a train we might encounter
279 * @returns The last tile of the reservation or the current train tile if no reservation present.
281 PBSTileInfo
FollowTrainReservation(const Train
*v
, Vehicle
**train_on_res
)
283 assert(v
->type
== VEH_TRAIN
);
285 TileIndex tile
= v
->tile
;
286 Trackdir trackdir
= v
->GetVehicleTrackdir();
288 if (IsRailDepotTile(tile
) && !GetDepotReservationTrackBits(tile
)) return PBSTileInfo(tile
, trackdir
, false);
290 FindTrainOnTrackInfo ftoti
;
291 ftoti
.res
= FollowReservation(v
->owner
, GetRailTypeInfo(v
->railtype
)->compatible_railtypes
, tile
, trackdir
);
292 ftoti
.res
.okay
= IsSafeWaitingPosition(v
, ftoti
.res
.tile
, ftoti
.res
.trackdir
, true, _settings_game
.pf
.forbid_90_deg
);
293 if (train_on_res
!= NULL
) {
294 FindVehicleOnPos(ftoti
.res
.tile
, &ftoti
, FindTrainOnTrackEnum
);
295 if (ftoti
.best
!= NULL
) *train_on_res
= ftoti
.best
->First();
296 if (*train_on_res
== NULL
&& IsRailStationTile(ftoti
.res
.tile
)) {
297 /* The target tile is a rail station. The track follower
298 * has stopped on the last platform tile where we haven't
299 * found a train. Also check all previous platform tiles
300 * for a possible train. */
301 TileIndexDiff diff
= TileOffsByDiagDir(TrackdirToExitdir(ReverseTrackdir(ftoti
.res
.trackdir
)));
302 for (TileIndex st_tile
= ftoti
.res
.tile
+ diff
; *train_on_res
== NULL
&& IsCompatibleTrainStationTile(st_tile
, ftoti
.res
.tile
); st_tile
+= diff
) {
303 FindVehicleOnPos(st_tile
, &ftoti
, FindTrainOnTrackEnum
);
304 if (ftoti
.best
!= NULL
) *train_on_res
= ftoti
.best
->First();
307 if (*train_on_res
== NULL
&& IsTileType(ftoti
.res
.tile
, MP_TUNNELBRIDGE
)) {
308 /* The target tile is a bridge/tunnel, also check the other end tile. */
309 FindVehicleOnPos(GetOtherTunnelBridgeEnd(ftoti
.res
.tile
), &ftoti
, FindTrainOnTrackEnum
);
310 if (ftoti
.best
!= NULL
) *train_on_res
= ftoti
.best
->First();
317 * Find the train which has reserved a specific path.
319 * @param tile A tile on the path.
320 * @param track A reserved track on the tile.
321 * @return The vehicle holding the reservation or NULL if the path is stray.
323 Train
*GetTrainForReservation(TileIndex tile
, Track track
)
325 assert(HasReservedTracks(tile
, TrackToTrackBits(track
)));
326 Trackdir trackdir
= TrackToTrackdir(track
);
328 RailTypes rts
= GetRailTypeInfo(GetTileRailType(tile
))->compatible_railtypes
;
330 /* Follow the path from tile to both ends, one of the end tiles should
331 * have a train on it. We need FollowReservation to ignore one-way signals
332 * here, as one of the two search directions will be the "wrong" way. */
333 for (int i
= 0; i
< 2; ++i
, trackdir
= ReverseTrackdir(trackdir
)) {
334 /* If the tile has a one-way block signal in the current trackdir, skip the
335 * search in this direction as the reservation can't come from this side.*/
336 if (HasOnewaySignalBlockingTrackdir(tile
, ReverseTrackdir(trackdir
)) && !HasPbsSignalOnTrackdir(tile
, trackdir
)) continue;
338 FindTrainOnTrackInfo ftoti
;
339 ftoti
.res
= FollowReservation(GetTileOwner(tile
), rts
, tile
, trackdir
, true);
341 FindVehicleOnPos(ftoti
.res
.tile
, &ftoti
, FindTrainOnTrackEnum
);
342 if (ftoti
.best
!= NULL
) return ftoti
.best
;
344 /* Special case for stations: check the whole platform for a vehicle. */
345 if (IsRailStationTile(ftoti
.res
.tile
)) {
346 TileIndexDiff diff
= TileOffsByDiagDir(TrackdirToExitdir(ReverseTrackdir(ftoti
.res
.trackdir
)));
347 for (TileIndex st_tile
= ftoti
.res
.tile
+ diff
; IsCompatibleTrainStationTile(st_tile
, ftoti
.res
.tile
); st_tile
+= diff
) {
348 FindVehicleOnPos(st_tile
, &ftoti
, FindTrainOnTrackEnum
);
349 if (ftoti
.best
!= NULL
) return ftoti
.best
;
353 /* Special case for bridges/tunnels: check the other end as well. */
354 if (IsTileType(ftoti
.res
.tile
, MP_TUNNELBRIDGE
)) {
355 FindVehicleOnPos(GetOtherTunnelBridgeEnd(ftoti
.res
.tile
), &ftoti
, FindTrainOnTrackEnum
);
356 if (ftoti
.best
!= NULL
) return ftoti
.best
;
364 * Determine whether a certain track on a tile is a safe position to end a path.
366 * @param v the vehicle to test for
367 * @param tile The tile
368 * @param trackdir The trackdir to test
369 * @param include_line_end Should end-of-line tiles be considered safe?
370 * @param forbid_90deg Don't allow trains to make 90 degree turns
371 * @return True if it is a safe position
373 bool IsSafeWaitingPosition(const Train
*v
, TileIndex tile
, Trackdir trackdir
, bool include_line_end
, bool forbid_90deg
)
375 if (IsRailDepotTile(tile
)) return true;
377 if (IsTileType(tile
, MP_RAILWAY
)) {
378 /* For non-pbs signals, stop on the signal tile. */
379 if (HasSignalOnTrackdir(tile
, trackdir
) && !IsPbsSignal(GetSignalType(tile
, TrackdirToTrack(trackdir
)))) return true;
382 /* Check next tile. For performance reasons, we check for 90 degree turns ourself. */
383 CFollowTrackRail
ft(v
, GetRailTypeInfo(v
->railtype
)->compatible_railtypes
);
386 if (!ft
.Follow(tile
, trackdir
)) {
387 /* Last tile of a terminus station is a safe position. */
388 if (include_line_end
) return true;
391 /* Check for reachable tracks. */
392 ft
.m_new_td_bits
&= DiagdirReachesTrackdirs(ft
.m_exitdir
);
393 if (forbid_90deg
) ft
.m_new_td_bits
&= ~TrackdirCrossesTrackdirs(trackdir
);
394 if (ft
.m_new_td_bits
== TRACKDIR_BIT_NONE
) return include_line_end
;
396 if (ft
.m_new_td_bits
!= TRACKDIR_BIT_NONE
&& KillFirstBit(ft
.m_new_td_bits
) == TRACKDIR_BIT_NONE
) {
397 Trackdir td
= FindFirstTrackdir(ft
.m_new_td_bits
);
398 /* PBS signal on next trackdir? Safe position. */
399 if (HasPbsSignalOnTrackdir(ft
.m_new_tile
, td
)) return true;
400 /* One-way PBS signal against us? Safe if end-of-line is allowed. */
401 if (IsTileType(ft
.m_new_tile
, MP_RAILWAY
) && HasSignalOnTrackdir(ft
.m_new_tile
, ReverseTrackdir(td
)) &&
402 GetSignalType(ft
.m_new_tile
, TrackdirToTrack(td
)) == SIGTYPE_PBS_ONEWAY
) {
403 return include_line_end
;
411 * Check if a safe position is free.
413 * @param v the vehicle to test for
414 * @param tile The tile
415 * @param trackdir The trackdir to test
416 * @param forbid_90deg Don't allow trains to make 90 degree turns
417 * @return True if the position is free
419 bool IsWaitingPositionFree(const Train
*v
, TileIndex tile
, Trackdir trackdir
, bool forbid_90deg
)
421 Track track
= TrackdirToTrack(trackdir
);
422 TrackBits reserved
= GetReservedTrackbits(tile
);
424 /* Tile reserved? Can never be a free waiting position. */
425 if (TrackOverlapsTracks(reserved
, track
)) return false;
427 /* Not reserved and depot or not a pbs signal -> free. */
428 if (IsRailDepotTile(tile
)) return true;
429 if (IsTileType(tile
, MP_RAILWAY
) && HasSignalOnTrackdir(tile
, trackdir
) && !IsPbsSignal(GetSignalType(tile
, track
))) return true;
431 /* Check the next tile, if it's a PBS signal, it has to be free as well. */
432 CFollowTrackRail
ft(v
, GetRailTypeInfo(v
->railtype
)->compatible_railtypes
);
434 if (!ft
.Follow(tile
, trackdir
)) return true;
436 /* Check for reachable tracks. */
437 ft
.m_new_td_bits
&= DiagdirReachesTrackdirs(ft
.m_exitdir
);
438 if (forbid_90deg
) ft
.m_new_td_bits
&= ~TrackdirCrossesTrackdirs(trackdir
);
440 return !HasReservedTracks(ft
.m_new_tile
, TrackdirBitsToTrackBits(ft
.m_new_td_bits
));