Skip to content

Commit 09af7ab

Browse files
committed
spin off vertical transfer table into an inheriting class
1 parent 6167a5d commit 09af7ab

File tree

2 files changed

+108
-43
lines changed

2 files changed

+108
-43
lines changed

Source/Orts.Simulation/Simulation/Transfertables.cs

Lines changed: 107 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ public class Transfertable : MovingTable
4242
{
4343
public float Span; // horizontal or vertical
4444
public List<float> Offsets = new List<float>();
45-
private bool VerticalTransfer = false;
46-
public float CenterOffsetComponent
47-
{
48-
get => VerticalTransfer ? CenterOffset.Y : CenterOffset.X;
49-
}
5045
// Dynamic data
5146
public bool Forward; // forward motion on
5247
public bool Reverse; // reverse motion on
@@ -58,32 +53,49 @@ public float CenterOffsetComponent
5853

5954
public Signals signalRef { get; protected set; }
6055

61-
public Transfertable(STFReader stf, Simulator simulator): base(stf, simulator)
56+
public virtual float CenterOffsetComponent { get => CenterOffset.X; }
57+
58+
protected Transfertable(STFReader stf, Simulator simulator) : base(stf, simulator) { }
59+
60+
public static Transfertable CreateFrom(STFReader stf, Simulator simulator)
6261
{
63-
signalRef = Simulator.Signals;
64-
string animation;
65-
WorldPosition.XNAMatrix.M44 = 100000000; //WorlPosition not yet defined, will be loaded when loading related tile
62+
var worldPosition = new WorldPosition();
63+
worldPosition.XNAMatrix.M44 = 100000000; //WorlPosition not yet defined, will be loaded when loading related tile
64+
var wFile = "";
65+
var uid = -1;
66+
var animations = new List<string>();
67+
var isVertical = false;
68+
var length = 0f;
69+
var centerOffset = new Vector3();
70+
var trackShapeIndex = -1;
71+
6672
stf.MustMatch("(");
6773
stf.ParseBlock(new[] {
6874
new STFReader.TokenProcessor("wfile", ()=>{
69-
WFile = stf.ReadStringBlock(null);
70-
WorldPosition.TileX = int.Parse(WFile.Substring(1, 7));
71-
WorldPosition.TileZ = int.Parse(WFile.Substring(8, 7));
72-
}),
73-
new STFReader.TokenProcessor("uid", ()=>{ UID = stf.ReadIntBlock(-1); }),
74-
new STFReader.TokenProcessor("animation", ()=>{ animation = stf.ReadStringBlock(null);
75-
Animations.Add(animation.ToLower());}),
76-
new STFReader.TokenProcessor("verticaltransfer", ()=>{ VerticalTransfer = stf.ReadBoolBlock(false);}),
77-
new STFReader.TokenProcessor("length", ()=>{ Length = stf.ReadFloatBlock(STFReader.UNITS.None , null);}),
78-
new STFReader.TokenProcessor("xoffset", ()=>{ CenterOffset.X = stf.ReadFloatBlock(STFReader.UNITS.None , null);}),
79-
new STFReader.TokenProcessor("zoffset", ()=>{ CenterOffset.Z = -stf.ReadFloatBlock(STFReader.UNITS.None , null);}),
80-
new STFReader.TokenProcessor("yoffset", ()=>{ CenterOffset.Y = stf.ReadFloatBlock(STFReader.UNITS.None , null);}),
81-
new STFReader.TokenProcessor("trackshapeindex", ()=>
82-
{
83-
TrackShapeIndex = stf.ReadIntBlock(-1);
84-
InitializeOffsetsAndTrackNodes();
75+
wFile = stf.ReadStringBlock(null);
76+
worldPosition.TileX = int.Parse(wFile.Substring(1, 7));
77+
worldPosition.TileZ = int.Parse(wFile.Substring(8, 7));
8578
}),
79+
new STFReader.TokenProcessor("uid", ()=>{ uid = stf.ReadIntBlock(null); }),
80+
new STFReader.TokenProcessor("animation", ()=>{ var animation = stf.ReadStringBlock(null);
81+
animations.Add(animation.ToLower());}),
82+
new STFReader.TokenProcessor("verticaltransfer", ()=>{ isVertical = stf.ReadBoolBlock(isVertical);}),
83+
new STFReader.TokenProcessor("length", ()=>{ length = stf.ReadFloatBlock(STFReader.UNITS.None , null);}),
84+
new STFReader.TokenProcessor("xoffset", ()=>{ centerOffset.X = stf.ReadFloatBlock(STFReader.UNITS.None , null);}),
85+
new STFReader.TokenProcessor("zoffset", ()=>{ centerOffset.Z = -stf.ReadFloatBlock(STFReader.UNITS.None , null);}),
86+
new STFReader.TokenProcessor("yoffset", ()=>{ centerOffset.Y = stf.ReadFloatBlock(STFReader.UNITS.None , null);}),
87+
new STFReader.TokenProcessor("trackshapeindex", ()=> { trackShapeIndex = stf.ReadIntBlock(null); }),
8688
});
89+
90+
var table = isVertical ? new VerticalTransfertable(null, simulator) : new Transfertable(null, simulator);
91+
table.signalRef = table.Simulator.Signals;
92+
table.WorldPosition = worldPosition;
93+
table.UID = uid;
94+
table.Animations.AddRange(animations);
95+
table.CenterOffset = centerOffset;
96+
table.TrackShapeIndex = trackShapeIndex;
97+
table.InitializeOffsetsAndTrackNodes();
98+
return table;
8799
}
88100

89101
/// <summary>
@@ -119,7 +131,7 @@ public override void Restore(BinaryReader inf, Simulator simulator)
119131
TargetOffset = inf.ReadSingle();
120132
}
121133

122-
protected void InitializeOffsetsAndTrackNodes()
134+
protected virtual void InitializeOffsetsAndTrackNodes()
123135
{
124136
var trackShape = Simulator.TSectionDat.TrackShapes.Get((uint)TrackShapeIndex);
125137
var nSections = trackShape.SectionIdxs[0].NoSections;
@@ -129,7 +141,7 @@ protected void InitializeOffsetsAndTrackNodes()
129141
var iMyTrackNodes = 0;
130142
foreach (var sectionIdx in trackShape.SectionIdxs)
131143
{
132-
Offsets.Add(VerticalTransfer ? (float)sectionIdx.Y : (float)sectionIdx.X);
144+
Offsets.Add((float)sectionIdx.X);
133145
MyTrackNodesIndex[iMyTrackNodes] = -1;
134146
MyTrVectorSectionsIndex[iMyTrackNodes] = -1;
135147
iMyTrackNodes++;
@@ -155,16 +167,9 @@ protected void InitializeOffsetsAndTrackNodes()
155167
}
156168
}
157169
}
158-
if (VerticalTransfer)
159-
{
160-
OffsetPos = CenterOffset.Y;
161-
Span = (float)(trackShape.SectionIdxs[trackShape.SectionIdxs.Length - 1].Y - trackShape.SectionIdxs[0].Y);
162-
}
163-
else
164-
{
165-
OffsetPos = CenterOffset.X;
166-
Span = (float)(trackShape.SectionIdxs[trackShape.SectionIdxs.Length - 1].X - trackShape.SectionIdxs[0].X);
167-
}
170+
OffsetPos = CenterOffset.X;
171+
// Compute width of transfer table
172+
Span = (float)(trackShape.SectionIdxs[trackShape.SectionIdxs.Length - 1].X - trackShape.SectionIdxs[0].X);
168173
}
169174

170175
/// <summary>
@@ -303,13 +308,10 @@ public override void StartContinuous(bool isForward)
303308
Continuous = true;
304309
}
305310

306-
public void ComputeCenter(WorldPosition worldPosition)
311+
public virtual void ComputeCenter(WorldPosition worldPosition)
307312
{
308313
Vector3 movingCenterOffset = CenterOffset;
309-
if (VerticalTransfer)
310-
movingCenterOffset.Y = OffsetPos;
311-
else
312-
movingCenterOffset.X = OffsetPos;
314+
movingCenterOffset.X = OffsetPos;
313315
Vector3 originCoordinates;
314316
Vector3.Transform(ref movingCenterOffset, ref worldPosition.XNAMatrix, out originCoordinates);
315317
WorldPosition = new WorldPosition(worldPosition);
@@ -438,4 +440,67 @@ public void PerformUpdateActions ( Matrix absAnimationMatrix, WorldPosition worl
438440
}
439441
}
440442

443+
/// <summary>
444+
/// A transfer table that moves vertically.
445+
/// </summary>
446+
public class VerticalTransfertable : Transfertable
447+
{
448+
public override float CenterOffsetComponent { get => CenterOffset.Y; }
449+
450+
internal VerticalTransfertable(STFReader stf, Simulator simulator) : base(stf, simulator) { }
451+
452+
protected override void InitializeOffsetsAndTrackNodes()
453+
{
454+
var trackShape = Simulator.TSectionDat.TrackShapes.Get((uint)TrackShapeIndex);
455+
var nSections = trackShape.SectionIdxs[0].NoSections;
456+
MyTrackNodesIndex = new int[trackShape.SectionIdxs.Length];
457+
MyTrackNodesOrientation = new bool[MyTrackNodesIndex.Length];
458+
MyTrVectorSectionsIndex = new int[MyTrackNodesIndex.Length];
459+
var iMyTrackNodes = 0;
460+
foreach (var sectionIdx in trackShape.SectionIdxs)
461+
{
462+
Offsets.Add((float)sectionIdx.Y);
463+
MyTrackNodesIndex[iMyTrackNodes] = -1;
464+
MyTrVectorSectionsIndex[iMyTrackNodes] = -1;
465+
iMyTrackNodes++;
466+
}
467+
var trackNodes = Simulator.TDB.TrackDB.TrackNodes;
468+
int iTrackNode = 0;
469+
for (iTrackNode = 1; iTrackNode < trackNodes.Length; iTrackNode++)
470+
{
471+
if (trackNodes[iTrackNode].TrVectorNode != null && trackNodes[iTrackNode].TrVectorNode.TrVectorSections != null)
472+
{
473+
var iTrVectorSection = Array.FindIndex(trackNodes[iTrackNode].TrVectorNode.TrVectorSections, trVectorSection =>
474+
(trVectorSection.WFNameX == WorldPosition.TileX && trVectorSection.WFNameZ == WorldPosition.TileZ && trVectorSection.WorldFileUiD == UID));
475+
if (iTrVectorSection >= 0)
476+
{
477+
if (trackNodes[iTrackNode].TrVectorNode.TrVectorSections.Length > (int)nSections)
478+
{
479+
iMyTrackNodes = trackNodes[iTrackNode].TrVectorNode.TrVectorSections[iTrVectorSection].Flag1 / 2;
480+
MyTrackNodesIndex[iMyTrackNodes] = iTrackNode;
481+
MyTrVectorSectionsIndex[iMyTrackNodes] = iTrVectorSection;
482+
MyTrackNodesOrientation[iMyTrackNodes] = trackNodes[iTrackNode].TrVectorNode.TrVectorSections[iTrVectorSection].Flag1 % 2 == 0 ? true : false;
483+
484+
}
485+
}
486+
}
487+
}
488+
OffsetPos = CenterOffset.Y;
489+
// Compute height of transfer table
490+
Span = (float)(trackShape.SectionIdxs[trackShape.SectionIdxs.Length - 1].Y - trackShape.SectionIdxs[0].Y);
491+
}
492+
493+
public override void ComputeCenter(WorldPosition worldPosition)
494+
{
495+
Vector3 movingCenterOffset = CenterOffset;
496+
movingCenterOffset.Y = OffsetPos;
497+
Vector3 originCoordinates;
498+
Vector3.Transform(ref movingCenterOffset, ref worldPosition.XNAMatrix, out originCoordinates);
499+
WorldPosition = new WorldPosition(worldPosition);
500+
WorldPosition.XNAMatrix.M41 = originCoordinates.X;
501+
WorldPosition.XNAMatrix.M42 = originCoordinates.Y;
502+
WorldPosition.XNAMatrix.M43 = originCoordinates.Z;
503+
}
504+
}
505+
441506
}

Source/Orts.Simulation/Simulation/Turntables.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public TurntableFile(string filePath, string shapePath, List<MovingTable> moving
6262
if (--count < 0)
6363
STFException.TraceWarning(stf, "Skipped extra Transfertable");
6464
else
65-
movingTables.Add(new Transfertable(stf, simulator));
65+
movingTables.Add(Transfertable.CreateFrom(stf, simulator));
6666
}),
6767
});
6868
if (count > 0)

0 commit comments

Comments
 (0)