From b102c1cdab118e0b428f9842deae542a5b104ecc Mon Sep 17 00:00:00 2001 From: Csantucci Date: Fri, 31 Mar 2023 20:03:12 +0200 Subject: [PATCH] Bug fix for https://bugs.launchpad.net/or/+bug/2013969 Crash after uncoupling player loco of a train with EOT --- Source/Orts.Simulation/MultiPlayer/Message.cs | 2 +- Source/Orts.Simulation/Simulation/AIs/AITrain.cs | 10 ++++++---- .../Simulation/RollingStocks/TrainCar.cs | 2 +- Source/Orts.Simulation/Simulation/Simulator.cs | 6 ++++-- .../Orts.Simulation/Simulation/Timetables/TTTrain.cs | 8 +++++--- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Source/Orts.Simulation/MultiPlayer/Message.cs b/Source/Orts.Simulation/MultiPlayer/Message.cs index 96c4e6b8ff..36217015ec 100644 --- a/Source/Orts.Simulation/MultiPlayer/Message.cs +++ b/Source/Orts.Simulation/MultiPlayer/Message.cs @@ -1634,11 +1634,11 @@ static TrainCar findCar(Train t, string name) train.MUDirection = (Direction)mDirection; train.RearTDBTraveller = traveller; + train.ReinitializeEOT(); train.CalculatePositionOfCars(); train.travelled = Travelled; train.CheckFreight(); train.SetDPUnitIDs(); - train.ReinitializeEOT(); } // New train else diff --git a/Source/Orts.Simulation/Simulation/AIs/AITrain.cs b/Source/Orts.Simulation/Simulation/AIs/AITrain.cs index 193f73fd6e..c80ef65464 100644 --- a/Source/Orts.Simulation/Simulation/AIs/AITrain.cs +++ b/Source/Orts.Simulation/Simulation/AIs/AITrain.cs @@ -4419,6 +4419,8 @@ public void CoupleAI(Train attachTrain, bool thisTrainFront, bool attachTrainFro Cars.Clear(); attachTrain.Length += Length; + attachTrain.ReinitializeEOT(); + // recalculate position of formed train if (attachTrainFront) // coupled to front, so rear position is still valid { @@ -4447,7 +4449,6 @@ public void CoupleAI(Train attachTrain, bool thisTrainFront, bool attachTrainFro // set various items attachTrain.CheckFreight(); attachTrain.SetDPUnitIDs(); - attachTrain.ReinitializeEOT(); attachTrain.activityClearingDistanceM = attachTrain.Cars.Count < standardTrainMinCarNo ? shortClearingDistanceM : standardClearingDistanceM; attachCar.SignalEvent(Event.Couple); @@ -4533,6 +4534,8 @@ public void CoupleAIToStatic(Train attachTrain, bool thisTrainFront, bool attach Length += attachTrain.Length; attachTrain.Cars.Clear(); + ReinitializeEOT(); + // recalculate position of formed train if (thisTrainFront) // coupled to front, so rear position is still valid { @@ -4581,7 +4584,6 @@ public void CoupleAIToStatic(Train attachTrain, bool thisTrainFront, bool attach UpdateOccupancies(); AddTrackSections(); ResetActions(true); - ReinitializeEOT(); physicsUpdate(0); } @@ -4749,6 +4751,8 @@ public void TerminateCoupling(Train attachTrain, bool thisTrainFront, bool attac UncoupledFrom = attachTrain; attachTrain.UncoupledFrom = this; + ReinitializeEOT(); + attachTrain.ReinitializeEOT(); // recalculate position of coupling train if (thisTrainFront) // coupled to front, so rear position is still valid @@ -4809,11 +4813,9 @@ public void TerminateCoupling(Train attachTrain, bool thisTrainFront, bool attac // set various items CheckFreight(); SetDPUnitIDs(); - ReinitializeEOT(); activityClearingDistanceM = Cars.Count < standardTrainMinCarNo ? shortClearingDistanceM : standardClearingDistanceM; attachTrain.CheckFreight(); attachTrain.SetDPUnitIDs(); - attachTrain.ReinitializeEOT(); attachTrain.activityClearingDistanceM = attachTrain.Cars.Count < standardTrainMinCarNo ? shortClearingDistanceM : standardClearingDistanceM; // anticipate reversal point and remove active action TCRoute.ReversalInfo[TCRoute.activeSubpath].ReverseReversalOffset = Math.Max(PresentPosition[0].TCOffset - 10f, 0.3f); diff --git a/Source/Orts.Simulation/Simulation/RollingStocks/TrainCar.cs b/Source/Orts.Simulation/Simulation/RollingStocks/TrainCar.cs index 89297072d4..ec545107ea 100644 --- a/Source/Orts.Simulation/Simulation/RollingStocks/TrainCar.cs +++ b/Source/Orts.Simulation/Simulation/RollingStocks/TrainCar.cs @@ -2983,7 +2983,7 @@ internal void UpdateVibrationAndTilting(Traveller traveler, float elapsedTimeS, // Don't add vibrations to train cars less than 2.5 meter in length; they're unsuitable for these calculations. // Don't let vibrate car before EOT to avoid EOT not moving together with that car - if (CarLengthM < 2.5f || Train.EOT != null && Train.Cars[Train.Cars.Count - 2] == this) return; + if (CarLengthM < 2.5f || Train.EOT != null && Train.Cars.Count > 1 && Train.Cars[Train.Cars.Count - 2] == this) return; if (Simulator.Settings.CarVibratingLevel != 0) { diff --git a/Source/Orts.Simulation/Simulation/Simulator.cs b/Source/Orts.Simulation/Simulation/Simulator.cs index 342d9e4b43..ba92d45fd2 100644 --- a/Source/Orts.Simulation/Simulation/Simulator.cs +++ b/Source/Orts.Simulation/Simulation/Simulator.cs @@ -1749,6 +1749,10 @@ public void UncoupleBehind(TrainCar car, bool keepFront) } + // update EOT state + train.ReinitializeEOT(); + train2.ReinitializeEOT(); + // and fix up the travellers if (train.IsActualPlayerTrain && j >= i || !keepFront) { @@ -1846,10 +1850,8 @@ public void UncoupleBehind(TrainCar car, bool keepFront) train.CheckFreight(); train.SetDPUnitIDs(); - train.ReinitializeEOT(); train2.CheckFreight(); train2.SetDPUnitIDs(); - train2.ReinitializeEOT(); train.Update(0); // stop the wheels from moving etc train2.Update(0); // stop the wheels from moving etc diff --git a/Source/Orts.Simulation/Simulation/Timetables/TTTrain.cs b/Source/Orts.Simulation/Simulation/Timetables/TTTrain.cs index 85da68e8ad..3e249242dd 100644 --- a/Source/Orts.Simulation/Simulation/Timetables/TTTrain.cs +++ b/Source/Orts.Simulation/Simulation/Timetables/TTTrain.cs @@ -11705,6 +11705,8 @@ public void TTCouple(TTTrain attachTrain, bool thisTrainFront, bool attachTrainF attachTrain.Length += Length; float distanceTravelledCorrection = 0; + attachTrain.ReinitializeEOT(); + // recalculate position of formed train if (attachTrainFront) // coupled to front, so rear position is still valid { @@ -11769,7 +11771,6 @@ public void TTCouple(TTTrain attachTrain, bool thisTrainFront, bool attachTrainF // set various items attachTrain.CheckFreight(); attachTrain.SetDPUnitIDs(); - attachTrain.ReinitializeEOT(); attachCar.SignalEvent(Event.Couple); attachTrain.ProcessSpeedSettings(); @@ -12105,6 +12106,9 @@ public int TTUncoupleBehind(TTTrain newTrain, bool reverseTrain, int leadLocomot } + ReinitializeEOT(); + newTrain.ReinitializeEOT(); + // and fix up the travellers if (DetachPosition) { @@ -12153,10 +12157,8 @@ public int TTUncoupleBehind(TTTrain newTrain, bool reverseTrain, int leadLocomot // check freight for both trains CheckFreight(); SetDPUnitIDs(); - ReinitializeEOT(); newTrain.CheckFreight(); newTrain.SetDPUnitIDs(); - newTrain.ReinitializeEOT(); // check speed values for both trains ProcessSpeedSettings();