From 8218476e8960a22e841b3d1fd70c3dd4639f5421 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 1 May 2023 13:27:41 +0100 Subject: [PATCH] Fix rail platforms being left partially reserved after train crash --- src/train_cmd.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 96a74c6ea6..5342545da6 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -6122,6 +6122,20 @@ static void SetSignalledBridgeTunnelGreenIfClear(TileIndex tile, TileIndex end) } } +static bool IsRailStationPlatformOccupied(TileIndex tile) +{ + TileIndexDiff delta = (GetRailStationAxis(tile) == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1)); + + for (TileIndex t = tile; IsCompatibleTrainStationTile(t, tile); t -= delta) { + if (HasVehicleOnPos(t, VEH_TRAIN, nullptr, &TrainOnTileEnum)) return true; + } + for (TileIndex t = tile + delta; IsCompatibleTrainStationTile(t, tile); t += delta) { + if (HasVehicleOnPos(t, VEH_TRAIN, nullptr, &TrainOnTileEnum)) return true; + } + + return false; +} + /** * Deletes/Clears the last wagon of a crashed train. It takes the engine of the * train, then goes to the last wagon and deletes that. Each call to this function @@ -6176,6 +6190,13 @@ static void DeleteLastWagon(Train *v) /* check if the wagon was on a road/rail-crossing */ if (IsLevelCrossingTile(tile)) UpdateLevelCrossing(tile); + if (IsRailStationTile(tile)) { + bool occupied = IsRailStationPlatformOccupied(tile); + DiagDirection dir = AxisToDiagDir(GetRailStationAxis(tile)); + SetRailStationPlatformReservation(tile, dir, occupied); + SetRailStationPlatformReservation(tile, ReverseDiagDir(dir), occupied); + } + /* Update signals */ if (IsTunnelBridgeWithSignalSimulation(tile)) { TileIndex end = GetOtherTunnelBridgeEnd(tile);