Skip to content

Commit

Permalink
Bug fix for https://bugs.launchpad.net/or/+bug/2013969 Crash after un…
Browse files Browse the repository at this point in the history
…coupling player loco of a train with EOT
  • Loading branch information
Csantucci committed Mar 31, 2023
1 parent c9abcd2 commit b102c1c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Source/Orts.Simulation/MultiPlayer/Message.cs
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions Source/Orts.Simulation/Simulation/AIs/AITrain.cs
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -4581,7 +4584,6 @@ public void CoupleAIToStatic(Train attachTrain, bool thisTrainFront, bool attach
UpdateOccupancies();
AddTrackSections();
ResetActions(true);
ReinitializeEOT();
physicsUpdate(0);

}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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)
{

Expand Down
6 changes: 4 additions & 2 deletions Source/Orts.Simulation/Simulation/Simulator.cs
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions Source/Orts.Simulation/Simulation/Timetables/TTTrain.cs
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -12105,6 +12106,9 @@ public int TTUncoupleBehind(TTTrain newTrain, bool reverseTrain, int leadLocomot

}

ReinitializeEOT();
newTrain.ReinitializeEOT();

// and fix up the travellers
if (DetachPosition)
{
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit b102c1c

Please sign in to comment.