MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / FollowTrainReservation

Function FollowTrainReservation

src/pbs.cpp:289–322  ·  view source on GitHub ↗

* Follow a train reservation to the last tile. * * @param v the vehicle * @param train_on_res Is set to a train we might encounter * @returns The last tile of the reservation or the current train tile if no reservation present. */

Source from the content-addressed store, hash-verified

287 * @returns The last tile of the reservation or the current train tile if no reservation present.
288 */
289PBSTileInfo FollowTrainReservation(const Train *v, Vehicle **train_on_res)
290{
291 assert(v->type == VEH_TRAIN);
292
293 TileIndex tile = v->tile;
294 Trackdir trackdir = v->GetVehicleTrackdir();
295
296 if (IsRailDepotTile(tile) && !GetDepotReservationTrackBits(tile)) return PBSTileInfo(tile, trackdir, false);
297
298 FindTrainOnTrackInfo ftoti;
299 ftoti.res = FollowReservation(v->owner, GetAllCompatibleRailTypes(v->railtypes), tile, trackdir);
300 ftoti.res.okay = IsSafeWaitingPosition(v, ftoti.res.tile, ftoti.res.trackdir, true, _settings_game.pf.forbid_90_deg);
301 if (train_on_res != nullptr) {
302 CheckTrainsOnTrack(ftoti, ftoti.res.tile);
303 if (ftoti.best != nullptr) *train_on_res = ftoti.best->First();
304 if (*train_on_res == nullptr && IsRailStationTile(ftoti.res.tile)) {
305 /* The target tile is a rail station. The track follower
306 * has stopped on the last platform tile where we haven't
307 * found a train. Also check all previous platform tiles
308 * for a possible train. */
309 TileIndexDiff diff = TileOffsByDiagDir(TrackdirToExitdir(ReverseTrackdir(ftoti.res.trackdir)));
310 for (TileIndex st_tile = ftoti.res.tile + diff; *train_on_res == nullptr && IsCompatibleTrainStationTile(st_tile, ftoti.res.tile); st_tile += diff) {
311 CheckTrainsOnTrack(ftoti, st_tile);
312 if (ftoti.best != nullptr) *train_on_res = ftoti.best->First();
313 }
314 }
315 if (*train_on_res == nullptr && IsTileType(ftoti.res.tile, MP_TUNNELBRIDGE)) {
316 /* The target tile is a bridge/tunnel, also check the other end tile. */
317 CheckTrainsOnTrack(ftoti, GetOtherTunnelBridgeEnd(ftoti.res.tile));
318 if (ftoti.best != nullptr) *train_on_res = ftoti.best->First();
319 }
320 }
321 return ftoti.res;
322}
323
324/**
325 * Find the train which has reserved a specific path.

Callers 5

ExtendTrainReservationFunction · 0.85
ChooseTrainTrackFunction · 0.85
TryPathReserveFunction · 0.85
ChooseRailTrackMethod · 0.85

Calls 15

IsRailDepotTileFunction · 0.85
FollowReservationFunction · 0.85
IsSafeWaitingPositionFunction · 0.85
CheckTrainsOnTrackFunction · 0.85
IsRailStationTileFunction · 0.85
TileOffsByDiagDirFunction · 0.85
TrackdirToExitdirFunction · 0.85
ReverseTrackdirFunction · 0.85
IsTileTypeFunction · 0.85

Tested by

no test coverage detected