mirror of https://github.com/OpenTTD/OpenTTD.git
(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)
This commit is contained in:
parent
67ab0450dd
commit
890a44f130
|
@ -1142,6 +1142,30 @@ CommandCost FindJoiningWaypoint(StationID existing_waypoint, StationID waypoint_
|
||||||
return FindJoiningBaseStation<Waypoint, STR_ERROR_MUST_REMOVE_RAILWAYPOINT_FIRST>(existing_waypoint, waypoint_to_join, adjacent, ta, wp);
|
return FindJoiningBaseStation<Waypoint, STR_ERROR_MUST_REMOVE_RAILWAYPOINT_FIRST>(existing_waypoint, waypoint_to_join, adjacent, ta, wp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
* Build rail station
|
||||||
* @param tile_org northern most position of station dragging/placement
|
* @param tile_org northern most position of station dragging/placement
|
||||||
|
@ -1281,11 +1305,8 @@ CommandCost CmdBuildRailStation(TileIndex tile_org, DoCommandFlag flags, uint32
|
||||||
/* Check for trains having a reservation for this tile. */
|
/* Check for trains having a reservation for this tile. */
|
||||||
Train *v = GetTrainForReservation(tile, AxisToTrack(GetRailStationAxis(tile)));
|
Train *v = GetTrainForReservation(tile, AxisToTrack(GetRailStationAxis(tile)));
|
||||||
if (v != NULL) {
|
if (v != NULL) {
|
||||||
FreeTrainTrackReservation(v);
|
|
||||||
*affected_vehicles.Append() = v;
|
*affected_vehicles.Append() = v;
|
||||||
if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(v->GetVehicleTrackdir()), false);
|
FreeTrainReservation(v);
|
||||||
for (; v->Next() != NULL; v = v->Next()) { }
|
|
||||||
if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(ReverseTrackdir(v->GetVehicleTrackdir())), false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1336,11 +1357,7 @@ CommandCost CmdBuildRailStation(TileIndex tile_org, DoCommandFlag flags, uint32
|
||||||
|
|
||||||
for (uint i = 0; i < affected_vehicles.Length(); ++i) {
|
for (uint i = 0; i < affected_vehicles.Length(); ++i) {
|
||||||
/* Restore reservations of trains. */
|
/* Restore reservations of trains. */
|
||||||
Train *v = affected_vehicles[i];
|
RestoreTrainReservation(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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check whether we need to expand the reservation of trains already on the station. */
|
/* 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)) {
|
if (HasStationReservation(tile)) {
|
||||||
v = GetTrainForReservation(tile, track);
|
v = GetTrainForReservation(tile, track);
|
||||||
if (v != NULL) {
|
if (v != NULL) FreeTrainReservation(v);
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool build_rail = keep_rail && !IsStationTileBlocked(tile);
|
bool build_rail = keep_rail && !IsStationTileBlocked(tile);
|
||||||
|
@ -1531,13 +1541,7 @@ CommandCost RemoveFromRailBaseStation(TileArea ta, SmallVector<T *, 4> &affected
|
||||||
|
|
||||||
affected_stations.Include(st);
|
affected_stations.Include(st);
|
||||||
|
|
||||||
if (v != NULL) {
|
if (v != NULL) RestoreTrainReservation(v);
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue