Changeset - r9815:96f4a9d86b27
[Not reviewed]
master
0 4 0
rubidium - 16 years ago 2008-08-02 22:55:08
rubidium@openttd.org
(svn r13957) -Codechange [YAPP]: Free the old path reservation on removing some tracks and reroute trains afterwards. (michi_cc)
4 files changed with 99 insertions and 3 deletions:
0 comments (0 inline, 0 general)
src/rail_cmd.cpp
Show inline comments
 
@@ -46,6 +46,8 @@
 
#include "functions.h"
 
#include "elrail_func.h"
 
#include "oldpool_func.h"
 
#include "pbs.h"
 
#include "core/smallvec_type.hpp"
 

	
 
#include "table/sprites.h"
 
#include "table/strings.h"
 
@@ -458,6 +460,8 @@ CommandCost CmdRemoveSingleRail(TileInde
 
	 * so do not call GetTileOwner(tile) in any case here */
 
	Owner owner = INVALID_OWNER;
 

	
 
	Vehicle *v = NULL;
 

	
 
	switch (GetTileType(tile)) {
 
		case MP_ROAD: {
 
			if (!IsLevelCrossing(tile) ||
 
@@ -468,6 +472,10 @@ CommandCost CmdRemoveSingleRail(TileInde
 
			}
 

	
 
			if (flags & DC_EXEC) {
 
				if (HasReservedTracks(tile, trackbit)) {
 
					v = GetTrainForReservation(tile, track);
 
					if (v != NULL) FreeTrainTrackReservation(v);
 
				}
 
				owner = GetTileOwner(tile);
 
				MakeRoadNormal(tile, GetCrossingRoadBits(tile), GetRoadTypes(tile), GetTownIndex(tile), GetRoadOwner(tile, ROADTYPE_ROAD), GetRoadOwner(tile, ROADTYPE_TRAM), GetRoadOwner(tile, ROADTYPE_HWAY));
 
			}
 
@@ -492,6 +500,10 @@ CommandCost CmdRemoveSingleRail(TileInde
 
				cost.AddCost(DoCommand(tile, track, 0, flags, CMD_REMOVE_SIGNALS));
 

	
 
			if (flags & DC_EXEC) {
 
				if (HasReservedTracks(tile, trackbit)) {
 
					v = GetTrainForReservation(tile, track);
 
					if (v != NULL) FreeTrainTrackReservation(v);
 
				}
 
				owner = GetTileOwner(tile);
 
				present ^= trackbit;
 
				if (present == 0) {
 
@@ -531,6 +543,8 @@ CommandCost CmdRemoveSingleRail(TileInde
 
			AddTrackToSignalBuffer(tile, track, owner);
 
			YapfNotifyTrackLayoutChange(tile, track);
 
		}
 

	
 
		if (v != NULL) TryPathReserve(v, true);
 
	}
 

	
 
	return cost;
 
@@ -1259,11 +1273,24 @@ CommandCost CmdConvertRail(TileIndex til
 
			/* Trying to convert other's rail */
 
			if (!CheckTileOwnership(tile)) continue;
 

	
 
			SmallVector<Vehicle*, 2> vehicles_affected;
 

	
 
			/* Vehicle on the tile when not converting Rail <-> ElRail
 
			 * Tunnels and bridges have special check later */
 
			if (tt != MP_TUNNELBRIDGE) {
 
				if (!IsCompatibleRail(type, totype) && !EnsureNoVehicleOnGround(tile)) continue;
 
				if (flags & DC_EXEC) { // we can safely convert, too
 
					TrackBits reserved = GetReservedTrackbits(tile);
 
					Track     track;
 
					while ((track = RemoveFirstTrack(&reserved)) != INVALID_TRACK) {
 
						Vehicle *v = GetTrainForReservation(tile, track);
 
						if (v != NULL && !HasPowerOnRail(v->u.rail.railtype, totype)) {
 
							/* No power on new rail type, reroute. */
 
							FreeTrainTrackReservation(v);
 
							*vehicles_affected.Append() = v;
 
						}
 
					}
 

	
 
					SetRailType(tile, totype);
 
					MarkTileDirtyByTile(tile);
 
					/* update power of train engines on this tile */
 
@@ -1320,14 +1347,20 @@ CommandCost CmdConvertRail(TileIndex til
 
							GetVehicleTunnelBridge(tile, endtile) != NULL) continue;
 

	
 
					if (flags & DC_EXEC) {
 
						Track track = DiagDirToDiagTrack(GetTunnelBridgeDirection(tile));
 
						if (GetTunnelBridgeReservation(tile)) {
 
							Vehicle *v = GetTrainForReservation(tile, track);
 
							if (v != NULL) {
 
								FreeTrainTrackReservation(v);
 
								*vehicles_affected.Append() = v;
 
							}
 
						}
 
						SetRailType(tile, totype);
 
						SetRailType(endtile, totype);
 

	
 
						VehicleFromPos(tile, NULL, &UpdateTrainPowerProc);
 
						VehicleFromPos(endtile, NULL, &UpdateTrainPowerProc);
 

	
 
						Track track = DiagDirToDiagTrack(GetTunnelBridgeDirection(tile));
 

	
 
						YapfNotifyTrackLayoutChange(tile, track);
 
						YapfNotifyTrackLayoutChange(endtile, track);
 

	
 
@@ -1353,6 +1386,10 @@ CommandCost CmdConvertRail(TileIndex til
 
					cost.AddCost(RailConvertCost(type, totype));
 
					break;
 
			}
 

	
 
			for (uint i = 0; i < vehicles_affected.Length(); ++i) {
 
				TryPathReserve(vehicles_affected[i], true);
 
			}
 
		}
 
	}
 

	
 
@@ -1371,11 +1408,18 @@ static CommandCost RemoveTrainDepot(Tile
 
		/* read variables before the depot is removed */
 
		DiagDirection dir = GetRailDepotDirection(tile);
 
		Owner owner = GetTileOwner(tile);
 
		Vehicle *v = NULL;
 

	
 
		if (GetDepotWaypointReservation(tile)) {
 
			v = GetTrainForReservation(tile, DiagDirToDiagTrack(dir));
 
			if (v != NULL) FreeTrainTrackReservation(v);
 
		}
 

	
 
		DoClearSquare(tile);
 
		delete GetDepotByTile(tile);
 
		AddSideToSignalBuffer(tile, dir, owner);
 
		YapfNotifyTrackLayoutChange(tile, DiagDirToDiagTrack(dir));
 
		if (v != NULL) TryPathReserve(v, true);
 
	}
 

	
 
	return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_train_depot);
src/station_cmd.cpp
Show inline comments
 
@@ -44,6 +44,7 @@
 
#include "animated_tile_func.h"
 
#include "elrail_func.h"
 
#include "newgrf.h"
 
#include "core/smallvec_type.hpp"
 

	
 
#include "table/sprites.h"
 
#include "table/strings.h"
 
@@ -1019,11 +1020,20 @@ CommandCost CmdBuildRailroadStation(Tile
 

	
 
		numtracks_orig = numtracks;
 

	
 
		SmallVector<Vehicle*, 4> affected_vehicles;
 
		do {
 
			TileIndex tile = tile_org;
 
			int w = plat_len;
 
			do {
 
				byte layout = *layout_ptr++;
 
				if (IsRailwayStationTile(tile) && GetRailwayStationReservation(tile)) {
 
					Vehicle *v = GetTrainForReservation(tile, AxisToTrack(GetRailStationAxis(tile)));
 
					if (v != NULL) {
 
						FreeTrainTrackReservation(v);
 
						*affected_vehicles.Append() = v;
 
					}
 
				}
 

	
 
				MakeRailStation(tile, st->owner, st->index, axis, layout & ~1, (RailType)GB(p2, 0, 4));
 
				SetCustomStationSpecIndex(tile, specindex);
 
				SetStationTileRandomBits(tile, GB(Random(), 0, 4));
 
@@ -1048,6 +1058,10 @@ CommandCost CmdBuildRailroadStation(Tile
 
			tile_org += tile_delta ^ TileDiffXY(1, 1); // perpendicular to tile_delta
 
		} while (--numtracks);
 

	
 
		for (uint i = 0; i < affected_vehicles.Length(); ++i) {
 
			TryPathReserve(affected_vehicles[i], true);
 
		}
 

	
 
		st->MarkTilesDirty(false);
 
		UpdateStationVirtCoordDirty(st);
 
		UpdateStationAcceptance(st, false);
 
@@ -1173,6 +1187,12 @@ CommandCost CmdRemoveFromRailroadStation
 
			uint specindex = GetCustomStationSpecIndex(tile2);
 
			Track track = GetRailStationTrack(tile2);
 
			Owner owner = GetTileOwner(tile2);
 
			Vehicle *v = NULL;
 

	
 
			if (GetRailwayStationReservation(tile2)) {
 
				v = GetTrainForReservation(tile2, track);
 
				if (v != NULL) FreeTrainTrackReservation(v);
 
			}
 

	
 
			DoClearSquare(tile2);
 
			st->rect.AfterRemoveTile(st, tile2);
 
@@ -1188,6 +1208,8 @@ CommandCost CmdRemoveFromRailroadStation
 
			st->MarkTilesDirty(false);
 
			UpdateStationSignCoord(st);
 

	
 
			if (v != NULL) TryPathReserve(v, true);
 

	
 
			/* if we deleted the whole station, delete the train facility. */
 
			if (st->train_tile == 0) {
 
				st->facilities &= ~FACIL_TRAIN;
 
@@ -1236,9 +1258,15 @@ static CommandCost RemoveRailroadStation
 
					/* read variables before the station tile is removed */
 
					Track track = GetRailStationTrack(tile);
 
					Owner owner = GetTileOwner(tile); // _current_player can be OWNER_WATER
 
					Vehicle *v = NULL;
 
					if (GetRailwayStationReservation(tile)) {
 
						v = GetTrainForReservation(tile, track);
 
						if (v != NULL) FreeTrainTrackReservation(v);
 
					}
 
					DoClearSquare(tile);
 
					AddTrackToSignalBuffer(tile, track, owner);
 
					YapfNotifyTrackLayoutChange(tile, track);
 
					if (v != NULL) TryPathReserve(v, true);
 
				}
 
			}
 
			tile += TileDiffXY(1, 0);
src/tunnelbridge_cmd.cpp
Show inline comments
 
@@ -630,8 +630,15 @@ static CommandCost DoClearTunnel(TileInd
 
		if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) {
 
			/* We first need to request values before calling DoClearSquare */
 
			DiagDirection dir = GetTunnelBridgeDirection(tile);
 
			Track track = DiagDirToDiagTrack(dir);
 
			Owner owner = GetTileOwner(tile);
 

	
 
			Vehicle *v = NULL;
 
			if (GetTunnelBridgeReservation(tile)) {
 
				v = GetTrainForReservation(tile, track);
 
				if (v != NULL) FreeTrainTrackReservation(v);
 
			}
 

	
 
			DoClearSquare(tile);
 
			DoClearSquare(endtile);
 

	
 
@@ -639,9 +646,10 @@ static CommandCost DoClearTunnel(TileInd
 
			AddSideToSignalBuffer(tile,    ReverseDiagDir(dir), owner);
 
			AddSideToSignalBuffer(endtile, dir,                 owner);
 

	
 
			Track track = DiagDirToDiagTrack(dir);
 
			YapfNotifyTrackLayoutChange(tile,    track);
 
			YapfNotifyTrackLayoutChange(endtile, track);
 

	
 
			if (v != NULL) TryPathReserve(v);
 
		} else {
 
			DoClearSquare(tile);
 
			DoClearSquare(endtile);
 
@@ -689,6 +697,12 @@ static CommandCost DoClearBridge(TileInd
 
		bool rail = GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL;
 
		Owner owner = GetTileOwner(tile);
 
		uint height = GetBridgeHeight(tile);
 
		Vehicle *v = NULL;
 

	
 
		if (rail && GetTunnelBridgeReservation(tile)) {
 
			v = GetTrainForReservation(tile, DiagDirToDiagTrack(direction));
 
			if (v != NULL) FreeTrainTrackReservation(v);
 
		}
 

	
 
		DoClearSquare(tile);
 
		DoClearSquare(endtile);
 
@@ -710,6 +724,8 @@ static CommandCost DoClearBridge(TileInd
 
			Track track = DiagDirToDiagTrack(direction);
 
			YapfNotifyTrackLayoutChange(tile,    track);
 
			YapfNotifyTrackLayoutChange(endtile, track);
 

	
 
			if (v != NULL) TryPathReserve(v, true);
 
		}
 
	}
 

	
src/waypoint.cpp
Show inline comments
 
@@ -33,6 +33,8 @@
 
#include "newgrf_station.h"
 
#include "oldpool_func.h"
 
#include "viewport_func.h"
 
#include "pbs.h"
 
#include "train.h"
 

	
 
#include "table/strings.h"
 

	
 
@@ -320,6 +322,7 @@ CommandCost RemoveTrainWaypoint(TileInde
 
		wp->deleted = 30; // let it live for this many days before we do the actual deletion.
 
		RedrawWaypointSign(wp);
 

	
 
		Vehicle *v = NULL;
 
		if (justremove) {
 
			TrackBits tracks = GetRailWaypointBits(tile);
 
			bool reserved = GetDepotWaypointReservation(tile);
 
@@ -327,10 +330,15 @@ CommandCost RemoveTrainWaypoint(TileInde
 
			if (reserved) SetTrackReservation(tile, tracks);
 
			MarkTileDirtyByTile(tile);
 
		} else {
 
			if (GetDepotWaypointReservation(tile)) {
 
				v = GetTrainForReservation(tile, track);
 
				if (v != NULL) FreeTrainTrackReservation(v);
 
			}
 
			DoClearSquare(tile);
 
			AddTrackToSignalBuffer(tile, track, owner);
 
		}
 
		YapfNotifyTrackLayoutChange(tile, track);
 
		if (v != NULL) TryPathReserve(v, true);
 
	}
 

	
 
	return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_train_depot);
0 comments (0 inline, 0 general)