Skip to content

Commit a9f8174

Browse files
authored
Merge pull request #678 from twpol/feature/traincar-refactor-1
Refactor TrainCar part 1
2 parents 678a674 + c19e718 commit a9f8174

File tree

7 files changed

+196
-249
lines changed

7 files changed

+196
-249
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/Simulation/Physics/Train.cs

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

5643-
// Check to see if coupler is opened or closed - only closed or opened couplers have been specified
5644-
// 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
5645-
if (i == 0) // first car
5646-
{
5647-
if (car.FrontCouplerOpenFitted)
5648-
{
5649-
5650-
car.FrontCouplerOpen = true;
5651-
}
5652-
else
5653-
{
5654-
car.FrontCouplerOpen = false;
5655-
}
5656-
}
5657-
else
5658-
{
5659-
car.FrontCouplerOpen = false;
5660-
}
5661-
5662-
// Set up coupler information for last car
5663-
if (i == Cars.Count - 2) // 2nd last car in count, but set up last car, ie i+1
5664-
{
5665-
5666-
if (Cars[i + 1].RearCouplerOpenFitted)
5667-
{
5668-
Cars[i + 1].RearCouplerOpen = true;
5669-
}
5670-
else
5671-
{
5672-
Cars[i + 1].RearCouplerOpen = false;
5673-
}
5674-
5675-
}
5676-
else
5677-
{
5678-
car.RearCouplerOpen = false;
5679-
}
5680-
5681-
5643+
// Update coupler open/closed status; it is assumed that:
5644+
// - the front coupler on first car will always be open
5645+
// - the rear coupler on last car will always be open
5646+
// - all others will be closed
5647+
car.FrontCoupler.IsOpen = i == 0;
5648+
car.RearCoupler.IsOpen = i == Cars.Count - 1;
56825649

56835650
TotalCouplerSlackM += car.CouplerSlackM; // Total coupler slack displayed in HUD only
56845651

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 AnimatedAirHose
23+
{
24+
public Vector3 Size;
25+
public AnimatedAirHoseState Connected;
26+
public AnimatedAirHoseState Disconnected;
27+
public float HeightAdjustmentM;
28+
public float YAngleAdjustmentRad;
29+
public float ZAngleAdjustmentRad;
30+
}
31+
32+
public struct AnimatedAirHoseState
33+
{
34+
public string ShapeFileName;
35+
}
36+
}
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: 32 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -422,28 +422,28 @@ 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

437-
if (FrontAirHoseShapeFileName != null && !File.Exists(wagonFolderSlash + FrontAirHoseShapeFileName))
437+
if (FrontAirHose.Connected.ShapeFileName != null && !File.Exists(wagonFolderSlash + FrontAirHose.Connected.ShapeFileName))
438438
{
439-
Trace.TraceWarning("{0} references non-existent shape {1}", WagFilePath, wagonFolderSlash + FrontAirHoseShapeFileName);
440-
FrontAirHoseShapeFileName = null;
439+
Trace.TraceWarning("{0} references non-existent shape {1}", WagFilePath, wagonFolderSlash + FrontAirHose.Connected.ShapeFileName);
440+
FrontAirHose.Connected.ShapeFileName = null;
441441
}
442442

443-
if (RearAirHoseShapeFileName != null && !File.Exists(wagonFolderSlash + RearAirHoseShapeFileName))
443+
if (RearAirHose.Connected.ShapeFileName != null && !File.Exists(wagonFolderSlash + RearAirHose.Connected.ShapeFileName))
444444
{
445-
Trace.TraceWarning("{0} references non-existent shape {1}", WagFilePath, wagonFolderSlash + RearAirHoseShapeFileName);
446-
RearAirHoseShapeFileName = null;
445+
Trace.TraceWarning("{0} references non-existent shape {1}", WagFilePath, wagonFolderSlash + RearAirHose.Connected.ShapeFileName);
446+
RearAirHose.Connected.ShapeFileName = null;
447447
}
448448

449449
// If trailing loco resistance constant has not been defined in WAG/ENG file then assign default value based upon orig Davis values
@@ -1224,37 +1224,29 @@ public virtual void Parse(string lowercasetoken, STFReader stf)
12241224

12251225
case "wagon(coupling(frontcoupleranim":
12261226
stf.MustMatch("(");
1227-
FrontCouplerShapeFileName = stf.ReadString();
1228-
FrontCouplerAnimWidthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1229-
FrontCouplerAnimHeightM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1230-
FrontCouplerAnimLengthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1227+
FrontCoupler.Closed.ShapeFileName = stf.ReadString();
1228+
FrontCoupler.Size = stf.ReadVector3(STFReader.UNITS.Distance, Vector3.Zero);
12311229
stf.SkipRestOfBlock();
12321230
break;
12331231

12341232
case "wagon(coupling(frontairhoseanim":
12351233
stf.MustMatch("(");
1236-
FrontAirHoseShapeFileName = stf.ReadString();
1237-
FrontAirHoseAnimWidthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1238-
FrontAirHoseAnimHeightM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1239-
FrontAirHoseAnimLengthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1234+
FrontAirHose.Connected.ShapeFileName = stf.ReadString();
1235+
FrontAirHose.Size = stf.ReadVector3(STFReader.UNITS.Distance, Vector3.Zero);
12401236
stf.SkipRestOfBlock();
12411237
break;
12421238

12431239
case "wagon(coupling(rearcoupleranim":
12441240
stf.MustMatch("(");
1245-
RearCouplerShapeFileName = stf.ReadString();
1246-
RearCouplerAnimWidthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1247-
RearCouplerAnimHeightM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1248-
RearCouplerAnimLengthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1241+
RearCoupler.Closed.ShapeFileName = stf.ReadString();
1242+
RearCoupler.Size = stf.ReadVector3(STFReader.UNITS.Distance, Vector3.Zero);
12491243
stf.SkipRestOfBlock();
12501244
break;
12511245

12521246
case "wagon(coupling(rearairhoseanim":
12531247
stf.MustMatch("(");
1254-
RearAirHoseShapeFileName = stf.ReadString();
1255-
RearAirHoseAnimWidthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1256-
RearAirHoseAnimHeightM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1257-
RearAirHoseAnimLengthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1248+
RearAirHose.Connected.ShapeFileName = stf.ReadString();
1249+
RearAirHose.Size = stf.ReadVector3(STFReader.UNITS.Distance, Vector3.Zero);
12581250
stf.SkipRestOfBlock();
12591251
break;
12601252

@@ -1266,39 +1258,29 @@ public virtual void Parse(string lowercasetoken, STFReader stf)
12661258

12671259
case "wagon(coupling(frontcoupleropenanim":
12681260
stf.MustMatch("(");
1269-
FrontCouplerOpenFitted = true;
1270-
FrontCouplerOpenShapeFileName = stf.ReadString();
1271-
FrontCouplerOpenAnimWidthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1272-
FrontCouplerOpenAnimHeightM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1273-
FrontCouplerOpenAnimLengthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1261+
FrontCoupler.Open.ShapeFileName = stf.ReadString();
1262+
// NOTE: Skip reading the size as it is unused: stf.ReadVector3(STFReader.UNITS.Distance, Vector3.Zero);
12741263
stf.SkipRestOfBlock();
12751264
break;
12761265

12771266
case "wagon(coupling(rearcoupleropenanim":
12781267
stf.MustMatch("(");
1279-
RearCouplerOpenFitted = true;
1280-
RearCouplerOpenShapeFileName = stf.ReadString();
1281-
RearCouplerOpenAnimWidthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1282-
RearCouplerOpenAnimHeightM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1283-
RearCouplerOpenAnimLengthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1268+
RearCoupler.Open.ShapeFileName = stf.ReadString();
1269+
// NOTE: Skip reading the size as it is unused: stf.ReadVector3(STFReader.UNITS.Distance, Vector3.Zero);
12841270
stf.SkipRestOfBlock();
12851271
break;
12861272

12871273
case "wagon(coupling(frontairhosediconnectedanim":
12881274
stf.MustMatch("(");
1289-
FrontAirHoseDisconnectedShapeFileName = stf.ReadString();
1290-
FrontAirHoseDisconnectedAnimWidthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1291-
FrontAirHoseDisconnectedAnimHeightM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1292-
FrontAirHoseDisconnectedAnimLengthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1275+
FrontAirHose.Disconnected.ShapeFileName = stf.ReadString();
1276+
// NOTE: Skip reading the size as it is unused: stf.ReadVector3(STFReader.UNITS.Distance, Vector3.Zero);
12931277
stf.SkipRestOfBlock();
12941278
break;
12951279

12961280
case "wagon(coupling(rearairhosediconnectedanim":
12971281
stf.MustMatch("(");
1298-
RearAirHoseDisconnectedShapeFileName = stf.ReadString();
1299-
RearAirHoseDisconnectedAnimWidthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1300-
RearAirHoseDisconnectedAnimHeightM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1301-
RearAirHoseDisconnectedAnimLengthM = stf.ReadFloat(STFReader.UNITS.Distance, null);
1282+
RearAirHose.Disconnected.ShapeFileName = stf.ReadString();
1283+
// NOTE: Skip reading the size as it is unused: stf.ReadVector3(STFReader.UNITS.Distance, Vector3.Zero);
13021284
stf.SkipRestOfBlock();
13031285
break;
13041286

@@ -1450,44 +1432,10 @@ public virtual void Copy(MSTSWagon copy)
14501432
FreightAnimMaxLevelM = copy.FreightAnimMaxLevelM;
14511433
FreightAnimMinLevelM = copy.FreightAnimMinLevelM;
14521434
FreightAnimFlag = copy.FreightAnimFlag;
1453-
FrontCouplerShapeFileName = copy.FrontCouplerShapeFileName;
1454-
FrontCouplerAnimWidthM = copy.FrontCouplerAnimWidthM;
1455-
FrontCouplerAnimHeightM = copy.FrontCouplerAnimHeightM;
1456-
FrontCouplerAnimLengthM = copy.FrontCouplerAnimLengthM;
1457-
FrontCouplerOpenShapeFileName = copy.FrontCouplerOpenShapeFileName;
1458-
FrontCouplerOpenAnimWidthM = copy.FrontCouplerOpenAnimWidthM;
1459-
FrontCouplerOpenAnimHeightM = copy.FrontCouplerOpenAnimHeightM;
1460-
FrontCouplerOpenAnimLengthM = copy.FrontCouplerOpenAnimLengthM;
1461-
FrontCouplerOpenFitted = copy.FrontCouplerOpenFitted;
1462-
RearCouplerShapeFileName = copy.RearCouplerShapeFileName;
1463-
RearCouplerAnimWidthM = copy.RearCouplerAnimWidthM;
1464-
RearCouplerAnimHeightM = copy.RearCouplerAnimHeightM;
1465-
RearCouplerAnimLengthM = copy.RearCouplerAnimLengthM;
1466-
RearCouplerOpenShapeFileName = copy.RearCouplerOpenShapeFileName;
1467-
RearCouplerOpenAnimWidthM = copy.RearCouplerOpenAnimWidthM;
1468-
RearCouplerOpenAnimHeightM = copy.RearCouplerOpenAnimHeightM;
1469-
RearCouplerOpenAnimLengthM = copy.RearCouplerOpenAnimLengthM;
1470-
RearCouplerOpenFitted = copy.RearCouplerOpenFitted;
1471-
1472-
FrontAirHoseShapeFileName = copy.FrontAirHoseShapeFileName;
1473-
FrontAirHoseAnimWidthM = copy.FrontAirHoseAnimWidthM;
1474-
FrontAirHoseAnimHeightM = copy.FrontAirHoseAnimHeightM;
1475-
FrontAirHoseAnimLengthM = copy.FrontAirHoseAnimLengthM;
1476-
1477-
FrontAirHoseDisconnectedShapeFileName = copy.FrontAirHoseDisconnectedShapeFileName;
1478-
FrontAirHoseDisconnectedAnimWidthM = copy.FrontAirHoseDisconnectedAnimWidthM;
1479-
FrontAirHoseDisconnectedAnimHeightM = copy.FrontAirHoseDisconnectedAnimHeightM;
1480-
FrontAirHoseDisconnectedAnimLengthM = copy.FrontAirHoseDisconnectedAnimLengthM;
1481-
1482-
RearAirHoseShapeFileName = copy.RearAirHoseShapeFileName;
1483-
RearAirHoseAnimWidthM = copy.RearAirHoseAnimWidthM;
1484-
RearAirHoseAnimHeightM = copy.RearAirHoseAnimHeightM;
1485-
RearAirHoseAnimLengthM = copy.RearAirHoseAnimLengthM;
1486-
1487-
RearAirHoseDisconnectedShapeFileName = copy.RearAirHoseDisconnectedShapeFileName;
1488-
RearAirHoseDisconnectedAnimWidthM = copy.RearAirHoseDisconnectedAnimWidthM;
1489-
RearAirHoseDisconnectedAnimHeightM = copy.RearAirHoseDisconnectedAnimHeightM;
1490-
RearAirHoseDisconnectedAnimLengthM = copy.RearAirHoseDisconnectedAnimLengthM;
1435+
FrontCoupler = copy.FrontCoupler;
1436+
RearCoupler = copy.RearCoupler;
1437+
FrontAirHose = copy.FrontAirHose;
1438+
RearAirHose = copy.RearAirHose;
14911439

14921440
CarWidthM = copy.CarWidthM;
14931441
CarHeightM = copy.CarHeightM;

0 commit comments

Comments
 (0)