diff options
author | peter1138 <peter1138@openttd.org> | 2017-03-20 17:44:37 +0000 |
---|---|---|
committer | peter1138 <peter1138@openttd.org> | 2017-03-20 17:44:37 +0000 |
commit | 890a44f130d2257a98b09a224791d26fee274554 (patch) | |
tree | a7310c5d4867fc6e07d654ce5c45efb9b38768fc /src | |
parent | 67ab0450dd9ab652ee4ec515e06f4ea8f04b7f6b (diff) | |
download | openttd-890a44f130d2257a98b09a224791d26fee274554.tar.xz |
(svn r27810) -Change: Factor out reservation-handling code in station_cmd.cpp
Code dealing with freeing and restoring track reservations is
duplicated in CmdBuildRailStation and RemoveFromRailBaseStation;
make separate functions for it. (cirdan)
Diffstat (limited to 'src')
-rw-r--r-- | src/station_cmd.cpp | 52 |
1 files changed, 28 insertions, 24 deletions
diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 610cf7216..ea890688e 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -1143,6 +1143,30 @@ CommandCost FindJoiningWaypoint(StationID existing_waypoint, StationID waypoint_ } /** + * Clear platform reservation during station building/removing. + * @param v vehicle which holds reservation + */ +static void FreeTrainReservation(Train *v) +{ + FreeTrainTrackReservation(v); + if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(v->GetVehicleTrackdir()), false); + v = v->Last(); + if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(ReverseTrackdir(v->GetVehicleTrackdir())), false); +} + +/** + * Restore platform reservation during station building/removing. + * @param v vehicle which held reservation + */ +static void RestoreTrainReservation(Train *v) +{ + if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(v->GetVehicleTrackdir()), true); + TryPathReserve(v, true, true); + v = v->Last(); + if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(ReverseTrackdir(v->GetVehicleTrackdir())), true); +} + +/** * Build rail station * @param tile_org northern most position of station dragging/placement * @param flags operation to perform @@ -1281,11 +1305,8 @@ CommandCost CmdBuildRailStation(TileIndex tile_org, DoCommandFlag flags, uint32 /* Check for trains having a reservation for this tile. */ Train *v = GetTrainForReservation(tile, AxisToTrack(GetRailStationAxis(tile))); if (v != NULL) { - FreeTrainTrackReservation(v); *affected_vehicles.Append() = v; - if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(v->GetVehicleTrackdir()), false); - for (; v->Next() != NULL; v = v->Next()) { } - if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(ReverseTrackdir(v->GetVehicleTrackdir())), false); + FreeTrainReservation(v); } } @@ -1336,11 +1357,7 @@ CommandCost CmdBuildRailStation(TileIndex tile_org, DoCommandFlag flags, uint32 for (uint i = 0; i < affected_vehicles.Length(); ++i) { /* Restore reservations of trains. */ - Train *v = affected_vehicles[i]; - if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(v->GetVehicleTrackdir()), true); - TryPathReserve(v, true, true); - for (; v->Next() != NULL; v = v->Next()) { } - if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(ReverseTrackdir(v->GetVehicleTrackdir())), true); + RestoreTrainReservation(affected_vehicles[i]); } /* Check whether we need to expand the reservation of trains already on the station. */ @@ -1504,14 +1521,7 @@ CommandCost RemoveFromRailBaseStation(TileArea ta, SmallVector<T *, 4> &affected if (HasStationReservation(tile)) { v = GetTrainForReservation(tile, track); - if (v != NULL) { - /* Free train reservation. */ - FreeTrainTrackReservation(v); - if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(v->GetVehicleTrackdir()), false); - Vehicle *temp = v; - for (; temp->Next() != NULL; temp = temp->Next()) { } - if (IsRailStationTile(temp->tile)) SetRailStationPlatformReservation(temp->tile, TrackdirToExitdir(ReverseTrackdir(temp->GetVehicleTrackdir())), false); - } + if (v != NULL) FreeTrainReservation(v); } bool build_rail = keep_rail && !IsStationTileBlocked(tile); @@ -1531,13 +1541,7 @@ CommandCost RemoveFromRailBaseStation(TileArea ta, SmallVector<T *, 4> &affected affected_stations.Include(st); - if (v != NULL) { - /* Restore station reservation. */ - if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(v->GetVehicleTrackdir()), true); - TryPathReserve(v, true, true); - for (; v->Next() != NULL; v = v->Next()) { } - if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(ReverseTrackdir(v->GetVehicleTrackdir())), true); - } + if (v != NULL) RestoreTrainReservation(v); } } |