Changeset - r22599:ccaa563db613
[Not reviewed]
master
0 1 0
peter1138 - 8 years ago 2017-03-20 17:44:37
peter1138@openttd.org
(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)
1 file changed with 28 insertions and 24 deletions:
0 comments (0 inline, 0 general)
src/station_cmd.cpp
Show inline comments
 
@@ -1143,6 +1143,30 @@ CommandCost FindJoiningWaypoint(StationI
 
}
 

	
 
/**
 
 * 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(TileInde
 
					/* 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(TileInde
 

	
 
		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(Ti
 

	
 
			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(Ti
 

	
 
			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);
 
		}
 
	}
 

	
0 comments (0 inline, 0 general)