Skip to content

Commit 2ce2855

Browse files
committed
Fix for disrupted train after restore
1 parent af9e9ae commit 2ce2855

File tree

2 files changed

+14
-31
lines changed

2 files changed

+14
-31
lines changed

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,13 @@
5050
// #define DEBUG_COUPLER_FORCES
5151

5252
using Microsoft.Xna.Framework;
53-
using Microsoft.Xna.Framework.Graphics;
5453
using Orts.Formats.Msts;
5554
using Orts.MultiPlayer;
5655
using Orts.Simulation.AIs;
5756
using Orts.Simulation.RollingStocks;
58-
using Orts.Simulation.RollingStocks.SubSystems;
5957
using Orts.Simulation.RollingStocks.SubSystems.Brakes;
6058
using Orts.Simulation.RollingStocks.SubSystems.Brakes.MSTS;
61-
using Orts.Parsers.Msts;
59+
using Orts.Simulation.RollingStocks.SubSystems.PowerSupplies;
6260
using Orts.Simulation.Signalling;
6361
using Orts.Simulation.Timetables;
6462
using ORTS.Common;
@@ -71,7 +69,6 @@
7169
using System.Linq;
7270
using System.Text;
7371
using Event = Orts.Common.Event;
74-
using Orts.Simulation.RollingStocks.SubSystems.PowerSupplies;
7572

7673
namespace Orts.Simulation.Physics
7774
{
@@ -664,11 +661,9 @@ public Train(Simulator simulator, Train orgTrain)
664661

665662
}
666663

667-
//================================================================================================//
668664
/// <summary>
669665
/// Restore
670-
/// <\summary>
671-
666+
/// </summary>
672667
public Train(Simulator simulator, BinaryReader inf)
673668
{
674669
Init(simulator);
@@ -953,7 +948,11 @@ private void RestoreCars(Simulator simulator, BinaryReader inf)
953948
if (count > 0)
954949
{
955950
for (int i = 0; i < count; ++i)
956-
Cars.Add(RollingStock.Restore(simulator, inf, this));
951+
{
952+
TrainCar car = RollingStock.Load(simulator, this, inf.ReadString(), false);
953+
car.Restore(inf);
954+
car.Initialize();
955+
}
957956
}
958957
SetDPUnitIDs(true);
959958
}
@@ -1014,12 +1013,9 @@ private void RestoreDeadlockInfo(BinaryReader inf)
10141013
}
10151014
}
10161015

1017-
1018-
//================================================================================================//
10191016
/// <summary>
10201017
/// save game state
1021-
/// <\summary>
1022-
1018+
/// </summary>
10231019
public virtual void Save(BinaryWriter outf)
10241020
{
10251021
SaveCars(outf);
@@ -1209,8 +1205,11 @@ public virtual void Save(BinaryWriter outf)
12091205
private void SaveCars(BinaryWriter outf)
12101206
{
12111207
outf.Write(Cars.Count);
1212-
foreach (TrainCar car in Cars)
1213-
RollingStock.Save(outf, car);
1208+
foreach (MSTSWagon wagon in Cars.OfType<MSTSWagon>())
1209+
{
1210+
outf.Write(wagon.WagFilePath);
1211+
wagon.Save(outf);
1212+
}
12141213
}
12151214

12161215
static void SaveTrafficSDefinition(BinaryWriter outf, Traffic_Service_Definition thisTSD)
@@ -4258,9 +4257,8 @@ public TrainCar FindLeadLocomotive()
42584257

42594258
public void PropagateBrakePressure(float elapsedClockSeconds)
42604259
{
4261-
if (LeadLocomotiveIndex >= 0)
4260+
if (LeadLocomotive is MSTSLocomotive lead)
42624261
{
4263-
MSTSLocomotive lead = (MSTSLocomotive)Cars[LeadLocomotiveIndex];
42644262
if (lead.TrainBrakeController != null)
42654263
lead.TrainBrakeController.UpdatePressure(ref EqualReservoirPressurePSIorInHg, elapsedClockSeconds, ref BrakeLine4);
42664264
if (lead.EngineBrakeController != null)

Source/Orts.Simulation/Simulation/RollingStocks/RollingStock.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,6 @@ public static TrainCar Load(Simulator simulator, Train train, string wagFilePath
9696
return car;
9797
}
9898

99-
public static void Save(BinaryWriter outf, TrainCar car)
100-
{
101-
MSTSWagon wagon = (MSTSWagon)car; // extend this when we introduce other types of rolling stock
102-
outf.Write(wagon.WagFilePath);
103-
wagon.Save(outf);
104-
}
105-
106-
public static TrainCar Restore(Simulator simulator, BinaryReader inf, Train train)
107-
{
108-
TrainCar car = Load(simulator, train, inf.ReadString(), false);
109-
car.Restore(inf);
110-
car.Initialize();
111-
return car;
112-
}
113-
11499
/// <summary>
115100
/// Utility class to avoid loading multiple copies of the same file.
116101
/// </summary>

0 commit comments

Comments
 (0)