Skip to content

Commit 7edd52d

Browse files
committed
refactor: TrainCar: extract animated coupler data structures
1 parent ab4f4bd commit 7edd52d

File tree

7 files changed

+101
-139
lines changed

7 files changed

+101
-139
lines changed

Source/Orts.Parsers.Msts/STFReader.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,19 @@ public bool ReadBoolBlock(bool defaultValue)
13721372
return defaultValue;
13731373
}
13741374

1375+
/// <summary>Read a Vector3 object in the STF format '... {X} {Y} {Z} ...'
1376+
/// </summary>
1377+
/// <param name="validUnits">Any combination of the UNITS enumeration, to limit the available suffixes to reasonable values.</param>
1378+
/// <param name="defaultValue">The default vector if any of the values are not specified</param>
1379+
/// <returns>The STF block as a Vector3</returns>
1380+
public Vector3 ReadVector3(UNITS validUnits, Vector3 defaultValue)
1381+
{
1382+
defaultValue.X = ReadFloat(validUnits, defaultValue.X);
1383+
defaultValue.Y = ReadFloat(validUnits, defaultValue.Y);
1384+
defaultValue.Z = ReadFloat(validUnits, defaultValue.Z);
1385+
return defaultValue;
1386+
}
1387+
13751388
/// <summary>Read a Vector3 object in the STF format '( {X} {Y} {Z} ... )'
13761389
/// </summary>
13771390
/// <param name="validUnits">Any combination of the UNITS enumeration, to limit the available suffixes to reasonable values.</param>

Source/Orts.Simulation/Orts.Simulation.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
<Compile Include="Simulation\Hazzard.cs" />
128128
<Compile Include="Simulation\LevelCrossing.cs" />
129129
<Compile Include="Simulation\Physics\Train.cs" />
130+
<Compile Include="Simulation\RollingStocks\Coupling\AnimatedCoupler.cs" />
130131
<Compile Include="Simulation\RollingStocks\LocomotiveAttributes.cs" />
131132
<Compile Include="Simulation\RollingStocks\MSTSControlTrailerCar.cs" />
132133
<Compile Include="Simulation\RollingStocks\MSTSDieselLocomotive.cs" />

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

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5671,45 +5671,12 @@ public void UpdateCouplerSlack(float elapsedTime)
56715671
car.RearCouplerSlackM = car.CouplerSlackM / AdvancedCouplerDuplicationFactor;
56725672
Cars[i + 1].FrontCouplerSlackM = car.CouplerSlackM / AdvancedCouplerDuplicationFactor;
56735673

5674-
// Check to see if coupler is opened or closed - only closed or opened couplers have been specified
5675-
// It is assumed that the front coupler on first car will always be opened, and so will the coupler on last car. All others on the train will be coupled
5676-
if (i == 0) // first car
5677-
{
5678-
if (car.FrontCouplerOpenFitted)
5679-
{
5680-
5681-
car.FrontCouplerOpen = true;
5682-
}
5683-
else
5684-
{
5685-
car.FrontCouplerOpen = false;
5686-
}
5687-
}
5688-
else
5689-
{
5690-
car.FrontCouplerOpen = false;
5691-
}
5692-
5693-
// Set up coupler information for last car
5694-
if (i == Cars.Count - 2) // 2nd last car in count, but set up last car, ie i+1
5695-
{
5696-
5697-
if (Cars[i + 1].RearCouplerOpenFitted)
5698-
{
5699-
Cars[i + 1].RearCouplerOpen = true;
5700-
}
5701-
else
5702-
{
5703-
Cars[i + 1].RearCouplerOpen = false;
5704-
}
5705-
5706-
}
5707-
else
5708-
{
5709-
car.RearCouplerOpen = false;
5710-
}
5711-
5712-
5674+
// Update coupler open/closed status; it is assumed that:
5675+
// - the front coupler on first car will always be open
5676+
// - the rear coupler on last car will always be open
5677+
// - all others will be closed
5678+
car.FrontCoupler.IsOpen = i == 0;
5679+
car.RearCoupler.IsOpen = i == Cars.Count - 1;
57135680

57145681
TotalCouplerSlackM += car.CouplerSlackM; // Total coupler slack displayed in HUD only
57155682

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// COPYRIGHT 2022 by the Open Rails project.
2+
//
3+
// This file is part of Open Rails.
4+
//
5+
// Open Rails is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// Open Rails is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
17+
18+
using Microsoft.Xna.Framework;
19+
20+
namespace Orts.Simulation.RollingStocks.Coupling
21+
{
22+
public struct AnimatedCoupler
23+
{
24+
public Vector3 Size;
25+
public AnimatedCouplerState Closed;
26+
public AnimatedCouplerState Open;
27+
public bool IsOpen;
28+
}
29+
30+
public struct AnimatedCouplerState
31+
{
32+
public string ShapeFileName;
33+
}
34+
}

Source/Orts.Simulation/Simulation/RollingStocks/MSTSWagon.cs

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -422,16 +422,16 @@ public virtual void LoadFromWagFile(string wagFilePath)
422422
InteriorShapeFileName = null;
423423
}
424424

425-
if (FrontCouplerShapeFileName != null && !File.Exists(wagonFolderSlash + FrontCouplerShapeFileName))
425+
if (FrontCoupler.Closed.ShapeFileName != null && !File.Exists(wagonFolderSlash + FrontCoupler.Closed.ShapeFileName))
426426
{
427-
Trace.TraceWarning("{0} references non-existent shape {1}", WagFilePath, wagonFolderSlash + FrontCouplerShapeFileName);
428-
FrontCouplerShapeFileName = null;
427+
Trace.TraceWarning("{0} references non-existent shape {1}", WagFilePath, wagonFolderSlash + FrontCoupler.Closed.ShapeFileName);
428+
FrontCoupler.Closed.ShapeFileName = null;
429429
}
430430

431-
if (RearCouplerShapeFileName != null && !File.Exists(wagonFolderSlash + RearCouplerShapeFileName))
431+
if (RearCoupler.Closed.ShapeFileName != null && !File.Exists(wagonFolderSlash + RearCoupler.Closed.ShapeFileName))
432432
{
433-
Trace.TraceWarning("{0} references non-existent shape {1}", WagFilePath, wagonFolderSlash + RearCouplerShapeFileName);
434-
RearCouplerShapeFileName = null;
433+
Trace.TraceWarning("{0} references non-existent shape {1}", WagFilePath, wagonFolderSlash + RearCoupler.Closed.ShapeFileName);
434+
RearCoupler.Closed.ShapeFileName = null;
435435
}
436436

437437
if (FrontAirHoseShapeFileName != null && !File.Exists(wagonFolderSlash + FrontAirHoseShapeFileName))
@@ -1222,10 +1222,8 @@ public virtual void Parse(string lowercasetoken, STFReader stf)
12221222

12231223
case "wagon(coupling(frontcoupleranim":
12241224
stf.MustMatch("(");
1225-
FrontCouplerShapeFileName = stf.ReadString();
1226-
FrontCouplerAnimWidthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1227-
FrontCouplerAnimHeightM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1228-
FrontCouplerAnimLengthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1225+
FrontCoupler.Closed.ShapeFileName = stf.ReadString();
1226+
FrontCoupler.Size = stf.ReadVector3(STFReader.UNITS.Distance, Vector3.Zero);
12291227
stf.SkipRestOfBlock();
12301228
break;
12311229

@@ -1240,10 +1238,8 @@ public virtual void Parse(string lowercasetoken, STFReader stf)
12401238

12411239
case "wagon(coupling(rearcoupleranim":
12421240
stf.MustMatch("(");
1243-
RearCouplerShapeFileName = stf.ReadString();
1244-
RearCouplerAnimWidthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1245-
RearCouplerAnimHeightM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1246-
RearCouplerAnimLengthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1241+
RearCoupler.Closed.ShapeFileName = stf.ReadString();
1242+
RearCoupler.Size = stf.ReadVector3(STFReader.UNITS.Distance, Vector3.Zero);
12471243
stf.SkipRestOfBlock();
12481244
break;
12491245

@@ -1264,21 +1260,15 @@ public virtual void Parse(string lowercasetoken, STFReader stf)
12641260

12651261
case "wagon(coupling(frontcoupleropenanim":
12661262
stf.MustMatch("(");
1267-
FrontCouplerOpenFitted = true;
1268-
FrontCouplerOpenShapeFileName = stf.ReadString();
1269-
FrontCouplerOpenAnimWidthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1270-
FrontCouplerOpenAnimHeightM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1271-
FrontCouplerOpenAnimLengthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1263+
FrontCoupler.Open.ShapeFileName = stf.ReadString();
1264+
// NOTE: Skip reading the size as it is unused: stf.ReadVector3(STFReader.UNITS.Distance, Vector3.Zero);
12721265
stf.SkipRestOfBlock();
12731266
break;
12741267

12751268
case "wagon(coupling(rearcoupleropenanim":
12761269
stf.MustMatch("(");
1277-
RearCouplerOpenFitted = true;
1278-
RearCouplerOpenShapeFileName = stf.ReadString();
1279-
RearCouplerOpenAnimWidthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1280-
RearCouplerOpenAnimHeightM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1281-
RearCouplerOpenAnimLengthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1270+
RearCoupler.Open.ShapeFileName = stf.ReadString();
1271+
// NOTE: Skip reading the size as it is unused: stf.ReadVector3(STFReader.UNITS.Distance, Vector3.Zero);
12821272
stf.SkipRestOfBlock();
12831273
break;
12841274

@@ -1453,24 +1443,8 @@ public virtual void Copy(MSTSWagon copy)
14531443
FreightAnimMaxLevelM = copy.FreightAnimMaxLevelM;
14541444
FreightAnimMinLevelM = copy.FreightAnimMinLevelM;
14551445
FreightAnimFlag = copy.FreightAnimFlag;
1456-
FrontCouplerShapeFileName = copy.FrontCouplerShapeFileName;
1457-
FrontCouplerAnimWidthM = copy.FrontCouplerAnimWidthM;
1458-
FrontCouplerAnimHeightM = copy.FrontCouplerAnimHeightM;
1459-
FrontCouplerAnimLengthM = copy.FrontCouplerAnimLengthM;
1460-
FrontCouplerOpenShapeFileName = copy.FrontCouplerOpenShapeFileName;
1461-
FrontCouplerOpenAnimWidthM = copy.FrontCouplerOpenAnimWidthM;
1462-
FrontCouplerOpenAnimHeightM = copy.FrontCouplerOpenAnimHeightM;
1463-
FrontCouplerOpenAnimLengthM = copy.FrontCouplerOpenAnimLengthM;
1464-
FrontCouplerOpenFitted = copy.FrontCouplerOpenFitted;
1465-
RearCouplerShapeFileName = copy.RearCouplerShapeFileName;
1466-
RearCouplerAnimWidthM = copy.RearCouplerAnimWidthM;
1467-
RearCouplerAnimHeightM = copy.RearCouplerAnimHeightM;
1468-
RearCouplerAnimLengthM = copy.RearCouplerAnimLengthM;
1469-
RearCouplerOpenShapeFileName = copy.RearCouplerOpenShapeFileName;
1470-
RearCouplerOpenAnimWidthM = copy.RearCouplerOpenAnimWidthM;
1471-
RearCouplerOpenAnimHeightM = copy.RearCouplerOpenAnimHeightM;
1472-
RearCouplerOpenAnimLengthM = copy.RearCouplerOpenAnimLengthM;
1473-
RearCouplerOpenFitted = copy.RearCouplerOpenFitted;
1446+
FrontCoupler = copy.FrontCoupler;
1447+
RearCoupler = copy.RearCoupler;
14741448

14751449
FrontAirHoseShapeFileName = copy.FrontAirHoseShapeFileName;
14761450
FrontAirHoseAnimWidthM = copy.FrontAirHoseAnimWidthM;

Source/Orts.Simulation/Simulation/RollingStocks/TrainCar.cs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// COPYRIGHT 2009, 2010, 2011, 2012, 2013, 2014, 2015 by the Open Rails project.
1+
// COPYRIGHT 2009 - 2022 by the Open Rails project.
22
//
33
// This file is part of Open Rails.
44
//
@@ -38,6 +38,7 @@
3838
using Orts.Parsers.Msts;
3939
using Orts.Simulation.AIs;
4040
using Orts.Simulation.Physics;
41+
using Orts.Simulation.RollingStocks.Coupling;
4142
using Orts.Simulation.RollingStocks.SubSystems;
4243
using Orts.Simulation.RollingStocks.SubSystems.Brakes;
4344
using Orts.Simulation.RollingStocks.SubSystems.PowerSupplies;
@@ -211,29 +212,8 @@ public static Interpolator SteamHeatBoilerFuelUsageGalukpH()
211212
public float InitialMaxBrakeForceN = 89e3f; // Initial force when agon initialised
212213

213214
// Coupler Animation
214-
public string FrontCouplerShapeFileName;
215-
public float FrontCouplerAnimLengthM;
216-
public float FrontCouplerAnimWidthM;
217-
public float FrontCouplerAnimHeightM;
218-
219-
public string FrontCouplerOpenShapeFileName;
220-
public float FrontCouplerOpenAnimLengthM;
221-
public float FrontCouplerOpenAnimWidthM;
222-
public float FrontCouplerOpenAnimHeightM;
223-
public bool FrontCouplerOpenFitted = false;
224-
public bool FrontCouplerOpen = false;
225-
226-
public string RearCouplerShapeFileName;
227-
public float RearCouplerAnimLengthM;
228-
public float RearCouplerAnimWidthM;
229-
public float RearCouplerAnimHeightM;
230-
231-
public string RearCouplerOpenShapeFileName;
232-
public float RearCouplerOpenAnimLengthM;
233-
public float RearCouplerOpenAnimWidthM;
234-
public float RearCouplerOpenAnimHeightM;
235-
public bool RearCouplerOpenFitted = false;
236-
public bool RearCouplerOpen = false;
215+
public AnimatedCoupler FrontCoupler = new AnimatedCoupler();
216+
public AnimatedCoupler RearCoupler = new AnimatedCoupler();
237217

238218
// Air hose animation
239219
public string FrontAirHoseShapeFileName;

0 commit comments

Comments
 (0)