Skip to content

Commit 2f57132

Browse files
authored
Merge pull request #846 from cesarBLG/fix-resume-initialize
Initialize cars before restore
2 parents ded0036 + eae6246 commit 2f57132

File tree

13 files changed

+21
-82
lines changed

13 files changed

+21
-82
lines changed

Source/Orts.Simulation/Simulation/Physics/Train.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -952,9 +952,8 @@ private void RestoreCars(Simulator simulator, BinaryReader inf)
952952
{
953953
for (int i = 0; i < count; ++i)
954954
{
955-
TrainCar car = RollingStock.Load(simulator, this, inf.ReadString(), false);
955+
TrainCar car = RollingStock.Load(simulator, this, inf.ReadString());
956956
car.Restore(inf);
957-
car.Initialize();
958957
}
959958
}
960959
SetDPUnitIDs(true);

Source/Orts.Simulation/Simulation/RollingStocks/MSTSControlTrailerCar.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public override void Copy(MSTSWagon copy)
128128
/// </summary>
129129
public override void Save(BinaryWriter outf)
130130
{
131+
base.Save(outf);
131132
ControllerFactory.Save(GearBoxController, outf);
132133
outf.Write(ControlGearIndication);
133134
outf.Write(ControlGearIndex);

Source/Orts.Simulation/Simulation/RollingStocks/MSTSDieselLocomotive.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,6 @@ public float DieselLevelL
9191

9292
public float LocomotiveMaxRailOutputPowerW;
9393

94-
public int currentGearIndexRestore = -1;
95-
public int currentnextGearRestore = -1;
96-
public bool gearSaved;
97-
public int dieselEngineRestoreState;
98-
9994
public float EngineRPM;
10095
public SmoothedData ExhaustParticles = new SmoothedData(1);
10196
public SmoothedData ExhaustMagnitude = new SmoothedData(1);
@@ -476,17 +471,6 @@ public override void Initialize()
476471
}
477472
}
478473

479-
// TO BE LOOKED AT - fix restoration process for gearbox and gear controller
480-
// It appears that the gearbox is initialised in two different places to cater for Basic and Advanced ENG file configurations(?).
481-
// Hence the restore values recovered in gearbox class are being overwritten , and resume was not working correctly
482-
// Hence restore gear position values are read as part of the diesel and restored at this point.
483-
if (gearSaved)
484-
{
485-
DieselEngines[0].GearBox.nextGearIndex = currentnextGearRestore;
486-
DieselEngines[0].GearBox.currentGearIndex = currentGearIndexRestore;
487-
GearBoxController.SetValue((float)DieselEngines[0].GearBox.currentGearIndex);
488-
}
489-
490474
if (Simulator.Settings.VerboseConfigurationMessages)
491475
{
492476
if (DieselEngines.HasGearBox)

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/Brakes/MSTS/AirSinglePipe.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ public override void Save(BinaryWriter outf)
322322
outf.Write(ReleaseRatePSIpS);
323323
outf.Write(RetainerPressureThresholdPSI);
324324
outf.Write(AutoCylPressurePSI);
325+
outf.Write(CylPressurePSI);
325326
outf.Write(AuxResPressurePSI);
326327
outf.Write(EmergResPressurePSI);
327328
outf.Write(ControlResPressurePSI);
@@ -345,6 +346,7 @@ public override void Restore(BinaryReader inf)
345346
ReleaseRatePSIpS = inf.ReadSingle();
346347
RetainerPressureThresholdPSI = inf.ReadSingle();
347348
AutoCylPressurePSI = inf.ReadSingle();
349+
CylPressurePSI = inf.ReadSingle();
348350
AuxResPressurePSI = inf.ReadSingle();
349351
EmergResPressurePSI = inf.ReadSingle();
350352
ControlResPressurePSI = inf.ReadSingle();

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerSupplies/CircuitBreaker.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ public void HandleEvent(PowerSupplyEvent evt)
188188

189189
public void Save(BinaryWriter outf)
190190
{
191-
outf.Write(ScriptName);
192191
outf.Write(DelayS);
193192
outf.Write(State.ToString());
194193
outf.Write(DriverClosingOrder);
@@ -199,7 +198,6 @@ public void Save(BinaryWriter outf)
199198

200199
public void Restore(BinaryReader inf)
201200
{
202-
ScriptName = inf.ReadString();
203201
DelayS = inf.ReadSingle();
204202
State = (CircuitBreakerState)Enum.Parse(typeof(CircuitBreakerState), inf.ReadString());
205203
DriverClosingOrder = inf.ReadBoolean();

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerSupplies/DieselEngine.cs

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ public void Parse(string lowercasetoken, STFReader stf)
109109
DEList.Add(new DieselEngine(Locomotive));
110110

111111
DEList[i].Parse(stf);
112-
DEList[i].Initialize();
113112

114113
// sets flag to indicate that a diesel eng prime mover code block has been defined by user, otherwise OR will define one through the next code section using "MSTS" values
115114
DEList[i].DieselEngineConfigured = true;
@@ -119,7 +118,6 @@ public void Parse(string lowercasetoken, STFReader stf)
119118
{
120119
STFException.TraceWarning(stf, "Diesel engine model has some errors - loading MSTS format");
121120
DEList[i].InitFromMSTS();
122-
DEList[i].Initialize();
123121
}
124122
}
125123
break;
@@ -993,15 +991,11 @@ public void Copy(DieselEngine other)
993991

994992
public void Initialize()
995993
{
996-
if (!Simulator.Settings.NoDieselEngineStart && !Locomotive.gearSaved)
994+
if (!Simulator.Settings.NoDieselEngineStart)
997995
{
998996
RealRPM = IdleRPM;
999997
State = DieselEngineState.Running;
1000998
}
1001-
else if (Locomotive.gearSaved)
1002-
{
1003-
State = (DieselEngineState)Locomotive.dieselEngineRestoreState;
1004-
}
1005999

10061000
RPMRange = MaxRPM - IdleRPM;
10071001
MagnitudeRange = MaxMagnitude - InitialMagnitude;
@@ -1018,16 +1012,14 @@ public void Initialize()
10181012

10191013
public void InitializeMoving()
10201014
{
1021-
if (!Simulator.Settings.NoDieselEngineStart && !Locomotive.gearSaved)
1022-
{
1023-
RealRPM = IdleRPM;
1024-
State = DieselEngineState.Running;
1025-
}
1026-
else if (Locomotive.gearSaved)
1015+
RealRPM = IdleRPM;
1016+
State = DieselEngineState.Running;
1017+
if (ThrottleRPMTab != null)
10271018
{
1028-
State = (DieselEngineState)Locomotive.dieselEngineRestoreState;
1019+
DemandedRPM = ThrottleRPMTab[Locomotive.ThrottlePercent];
1020+
DemandedRPM = MathHelper.Clamp(DemandedRPM, IdleRPM, MaxRPM); // Clamp throttle setting within bounds
1021+
RealRPM = DemandedRPM;
10291022
}
1030-
10311023
GearBox?.InitializeMoving();
10321024
}
10331025

@@ -1594,20 +1586,12 @@ public void HandleEvent(PowerSupplyEvent evt)
15941586

15951587
public void Restore(BinaryReader inf)
15961588
{
1597-
Locomotive.dieselEngineRestoreState = inf.ReadInt32();
1598-
State = (DieselEngineState)Locomotive.dieselEngineRestoreState;
1589+
State = (DieselEngineState)inf.ReadInt32();
15991590
RealRPM = inf.ReadSingle();
16001591
OutputPowerW = inf.ReadSingle();
16011592
DieselTemperatureDeg = inf.ReadSingle();
16021593
GovernorEnabled = inf.ReadBoolean();
1603-
1604-
Locomotive.gearSaved = inf.ReadBoolean(); // read boolean which indicates gear data was saved
1605-
1606-
if (Locomotive.gearSaved)
1607-
{
1608-
GearBox = new GearBox(this);
1609-
GearBox.Restore(inf);
1610-
}
1594+
GearBox?.Restore(inf);
16111595
}
16121596

16131597
public void Save(BinaryWriter outf)
@@ -1617,16 +1601,7 @@ public void Save(BinaryWriter outf)
16171601
outf.Write(OutputPowerW);
16181602
outf.Write(DieselTemperatureDeg);
16191603
outf.Write(GovernorEnabled);
1620-
1621-
if (GearBox != null)
1622-
{
1623-
outf.Write(true);
1624-
GearBox.Save(outf);
1625-
}
1626-
else
1627-
{
1628-
outf.Write(false);
1629-
}
1604+
GearBox?.Save(outf);
16301605
}
16311606

16321607
/// <summary>

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerSupplies/DieselPowerSupply.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,12 @@ public override void InitializeMoving()
110110

111111
public override void Save(BinaryWriter outf)
112112
{
113-
outf.Write(ScriptName);
114-
115113
base.Save(outf);
116114
TractionCutOffRelay.Save(outf);
117115
}
118116

119117
public override void Restore(BinaryReader inf)
120118
{
121-
ScriptName = inf.ReadString();
122-
123119
base.Restore(inf);
124120
TractionCutOffRelay.Restore(inf);
125121
}

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerSupplies/DualModePowerSupply.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,13 @@ public override void InitializeMoving()
112112

113113
public override void Save(BinaryWriter outf)
114114
{
115-
outf.Write(ScriptName);
116-
117115
base.Save(outf);
118116
CircuitBreaker.Save(outf);
119117
TractionCutOffRelay.Save(outf);
120118
}
121119

122120
public override void Restore(BinaryReader inf)
123121
{
124-
ScriptName = inf.ReadString();
125-
126122
base.Restore(inf);
127123
CircuitBreaker.Restore(inf);
128124
TractionCutOffRelay.Restore(inf);

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerSupplies/ElectricPowerSupply.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,12 @@ public override void InitializeMoving()
108108

109109
public override void Save(BinaryWriter outf)
110110
{
111-
outf.Write(ScriptName);
112-
113111
base.Save(outf);
114112
CircuitBreaker.Save(outf);
115113
}
116114

117115
public override void Restore(BinaryReader inf)
118116
{
119-
ScriptName = inf.ReadString();
120-
121117
base.Restore(inf);
122118
CircuitBreaker.Restore(inf);
123119
}

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerSupplies/Pantograph.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,14 @@ public void Copy(Pantographs pantographs)
7979

8080
public void Restore(BinaryReader inf)
8181
{
82-
List.Clear();
83-
8482
int n = inf.ReadInt32();
8583
for (int i = 0; i < n; i++)
8684
{
87-
List.Add(new Pantograph(Wagon));
88-
List.Last().Restore(inf);
85+
if (i >= List.Count)
86+
{
87+
List.Add(new Pantograph(Wagon));
88+
}
89+
List[i].Restore(inf);
8990
}
9091
}
9192

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerSupplies/TractionCutOffRelay.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ public void HandleEvent(PowerSupplyEvent evt)
165165

166166
public void Save(BinaryWriter outf)
167167
{
168-
outf.Write(ScriptName);
169168
outf.Write(DelayS);
170169
outf.Write(State.ToString());
171170
outf.Write(DriverClosingOrder);
@@ -176,7 +175,6 @@ public void Save(BinaryWriter outf)
176175

177176
public void Restore(BinaryReader inf)
178177
{
179-
ScriptName = inf.ReadString();
180178
DelayS = inf.ReadSingle();
181179
State = (TractionCutOffRelayState)Enum.Parse(typeof(TractionCutOffRelayState), inf.ReadString());
182180
DriverClosingOrder = inf.ReadBoolean();

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerTransmissions/GearBox.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,9 +751,7 @@ public void Copy(GearBox copy)
751751
public void Restore(BinaryReader inf)
752752
{
753753
currentGearIndex = inf.ReadInt32();
754-
Locomotive.currentGearIndexRestore = currentGearIndex;
755754
nextGearIndex = inf.ReadInt32();
756-
Locomotive.currentnextGearRestore = nextGearIndex;
757755
gearedUp = inf.ReadBoolean();
758756
gearedDown = inf.ReadBoolean();
759757
clutchOn = inf.ReadBoolean();

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/TrainControlSystem.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -946,19 +946,14 @@ public string GetDisplayString(int commandIndex)
946946

947947
public void Save(BinaryWriter outf)
948948
{
949-
outf.Write(ScriptName ?? "");
950-
if (ScriptName != "")
949+
if (!string.IsNullOrEmpty(ScriptName))
951950
Script.Save(outf);
952951
}
953952

954953
public void Restore(BinaryReader inf)
955954
{
956-
ScriptName = inf.ReadString();
957-
if (ScriptName != "")
958-
{
959-
Initialize();
955+
if (!string.IsNullOrEmpty(ScriptName))
960956
Script.Restore(inf);
961-
}
962957
}
963958
}
964959

0 commit comments

Comments
 (0)