Skip to content

Commit be03e85

Browse files
authored
Merge pull request #775 from Roeterdink/UpdateTurntablePools
Update turntable pools
2 parents 1e52c2c + d84c8d8 commit be03e85

File tree

1 file changed

+63
-47
lines changed

1 file changed

+63
-47
lines changed

Source/Orts.Simulation/Simulation/Timetables/TTTurntable.cs

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,20 @@ public TimetableTurntablePool(TimetableReader fileContents, ref int lineindex, S
183183
if (pathValid)
184184
{
185185
Train.TCRoutePath fullRoute = new Train.TCRoutePath(newPath, -2, 1, Simulatorref.Signals, -1, Simulatorref.Settings);
186-
// if last element is end of track, remove it from path
186+
// if first element is end of track, remove it from path (path is defined outbound)
187187
Train.TCSubpathRoute usedRoute = fullRoute.TCRouteSubpaths[0];
188-
int lastIndex = usedRoute.Count - 1;
189-
int lastSectionIndex = usedRoute[lastIndex].TCSectionIndex;
190-
if (Simulatorref.Signals.TrackCircuitList[lastSectionIndex].CircuitType == TrackCircuitSection.TrackCircuitType.EndOfTrack)
188+
if (Simulatorref.Signals.TrackCircuitList[usedRoute.First().TCSectionIndex].CircuitType == TrackCircuitSection.TrackCircuitType.EndOfTrack)
191189
{
192-
lastIndex = usedRoute.Count - 2;
190+
usedRoute.RemoveAt(0);
193191
}
194192

193+
// if last element is send of track, remove it from path (if path has no junction it may be in reverse direction)
194+
if (Simulatorref.Signals.TrackCircuitList[usedRoute.Last().TCSectionIndex].CircuitType == TrackCircuitSection.TrackCircuitType.EndOfTrack)
195+
{
196+
usedRoute.RemoveAt(usedRoute.Count - 1);
197+
}
198+
199+
// create path list if required
195200
if (AdditionalTurntableDetails.AccessPaths == null)
196201
{
197202
AdditionalTurntableDetails.AccessPaths = new List<AccessPathDetails>();
@@ -615,14 +620,6 @@ private void CalculateAccessOffsets(int ipath, Turntable thisTurntable)
615620
{
616621
AccessPathDetails thisPath = AdditionalTurntableDetails.AccessPaths[ipath];
617622

618-
float baseLength = 0;
619-
620-
// calculate total length of path sections except first section
621-
for (int isection = 1; isection < thisPath.AccessPath.Count; isection++)
622-
{
623-
baseLength += Simulatorref.Signals.TrackCircuitList[thisPath.AccessPath[isection].TCSectionIndex].Length;
624-
}
625-
626623
// calculate total length of track sections in first section backward upto turntable section
627624
TrackCircuitSection thisSection = Simulatorref.Signals.TrackCircuitList[thisPath.AccessPath[0].TCSectionIndex];
628625
int trackNodeIndex = thisSection.OriginalIndex;
@@ -656,13 +653,11 @@ private void CalculateAccessOffsets(int ipath, Turntable thisTurntable)
656653
thisPath.AccessTraveller.Direction = Traveller.TravellerDirection.Backward;
657654
}
658655

659-
float totalLength = baseLength + entrySectionLength;
660-
661656
// deduct clearance for turntable
662657
// if no explicit clearance defined, use length of last vector before turntable
663658

664-
thisPath.TableApproachOffset = totalLength - AdditionalTurntableDetails.TurntableApproachClearanceM;
665-
thisPath.TableMiddleEntry = totalLength + (thisTurntable.Length / 2.0f);
659+
thisPath.TableApproachOffset = entrySectionLength - AdditionalTurntableDetails.TurntableApproachClearanceM;
660+
thisPath.TableMiddleEntry = entrySectionLength + (thisTurntable.Length / 2.0f);
666661
thisPath.TableMiddleExit = exitSectionLength + (thisTurntable.Length / 2.0f);
667662

668663
#if DEBUG_TURNTABLEINFO
@@ -847,7 +842,6 @@ public override bool TestPoolExit(TTTrain train)
847842
bool validPath = TestPoolAccess(train, out testAccess);
848843
return (validPath);
849844
}
850-
851845
public bool TestPoolAccess(TTTrain train, out int accessIndex)
852846
{
853847
bool validPool = false;
@@ -864,24 +858,27 @@ public bool TestPoolAccess(TTTrain train, out int accessIndex)
864858
for (int iSection = train.TCRoute.TCRouteSubpaths.Last().Count - 1; iSection >= 0 && reqPath == -1; iSection--)
865859
{
866860
int lastSectionIndex = train.TCRoute.TCRouteSubpaths.Last()[iSection].TCSectionIndex;
867-
int lastSectionDirection = train.TCRoute.TCRouteSubpaths.Last().Last().Direction;
861+
int lastSectionDirection = train.TCRoute.TCRouteSubpaths.Last()[iSection].Direction;
868862

869-
for (int iPath = 0; iPath < AdditionalTurntableDetails.AccessPaths.Count && reqPath < 0; iPath++)
863+
if (train.signalRef.TrackCircuitList[lastSectionIndex].CircuitType == TrackCircuitSection.TrackCircuitType.Normal)
870864
{
871-
Train.TCSubpathRoute accessPath = AdditionalTurntableDetails.AccessPaths[iPath].AccessPath;
872-
reqPathIndex = accessPath.GetRouteIndex(lastSectionIndex, 0);
873-
874-
// path is defined outbound, so directions must be opposite
875-
if (reqPathIndex >= 0 && accessPath[reqPathIndex].Direction != lastSectionDirection)
865+
for (int iPath = 0; iPath < AdditionalTurntableDetails.AccessPaths.Count && reqPath < 0; iPath++)
876866
{
877-
reqPath = iPath;
878-
lastValidSectionIndex = iSection;
867+
Train.TCSubpathRoute accessPath = AdditionalTurntableDetails.AccessPaths[iPath].AccessPath;
868+
reqPathIndex = accessPath.GetRouteIndex(lastSectionIndex, 0);
869+
870+
// path is defined outbound, so directions must be opposite
871+
if (reqPathIndex >= 0 && accessPath[reqPathIndex].Direction != lastSectionDirection)
872+
{
873+
reqPath = iPath;
874+
lastValidSectionIndex = iSection;
875+
}
879876
}
880877
}
881878
}
882879

883880
// remove sections from train route if required
884-
if (lastValidSectionIndex < train.TCRoute.TCRouteSubpaths.Last().Count - 1)
881+
if (reqPath >= 0 && lastValidSectionIndex < train.TCRoute.TCRouteSubpaths.Last().Count - 1)
885882
{
886883
for (int iSection = train.TCRoute.TCRouteSubpaths.Last().Count - 1; iSection > lastValidSectionIndex; iSection--)
887884
{
@@ -1007,7 +1004,7 @@ override public TrainFromPool ExtractTrain(ref TTTrain train, int presentTime)
10071004
#if DEBUG_POOLINFO
10081005
sob = new StringBuilder();
10091006
sob.AppendFormat("Pool {0} : cannot find train {1} for {2} ({3}) \n", PoolName, selectedTrainNumber, train.Number, train.Name);
1010-
sob.AppendFormat(" stored units : {0}", reqStorage.StoredUnits.Count);
1007+
sob.AppendFormat(" stored units : {0}", StoragePool[selectedStorage].StoredUnits.Count);
10111008
File.AppendAllText(@"C:\temp\PoolAnal.csv", sob.ToString() + "\n");
10121009
#endif
10131010
return (TrainFromPool.Delayed);
@@ -1023,7 +1020,7 @@ override public TrainFromPool ExtractTrain(ref TTTrain train, int presentTime)
10231020
#if DEBUG_POOLINFO
10241021
sob = new StringBuilder();
10251022
sob.AppendFormat("Pool {0} : train {1} ({2}) extracted as {3} ({4}) \n", PoolName, selectedTrain.Number, selectedTrain.Name, train.Number, train.Name);
1026-
sob.AppendFormat(" stored units : {0}", reqStorage.StoredUnits.Count);
1023+
sob.AppendFormat(" stored units : {0}", StoragePool[selectedStorage].StoredUnits.Count);
10271024
File.AppendAllText(@"C:\temp\PoolAnal.csv", sob.ToString() + "\n");
10281025
#endif
10291026

@@ -1299,8 +1296,9 @@ override public Train.TCSubpathRoute SetPoolExit(TTTrain train, out int poolStor
12991296
// no action if state is poolClaimed - state will resolve as train ahead is stabled in pool
13001297

13011298
// valid pool
1302-
else if (reqPool > 0)
1299+
else if (reqPool >= 0)
13031300
{
1301+
// train approaches from exit path - train is moving toward turntable and is stored after turntable movement
13041302
if (checkAccessPath)
13051303
{
13061304
bool validPath = TestPoolAccess(train, out reqPath);
@@ -1331,26 +1329,33 @@ override public Train.TCSubpathRoute SetPoolExit(TTTrain train, out int poolStor
13311329
}
13321330
}
13331331

1334-
poolStorageIndex = reqPool;
1332+
train.PoolStorageIndex = poolStorageIndex = reqPool;
1333+
1334+
// set approach to turntable
1335+
// also add unit to storage as claim
1336+
newRoute.Last().MovingTableApproachPath = reqPath;
1337+
AddUnit(train, true);
1338+
StoragePool[poolStorageIndex].ClaimUnits.Add(train.Number);
13351339
}
13361340
}
1341+
13371342
// create new route from access track only
13381343
// use first defined access track
1339-
// reverse path as path is outbound
1344+
// if only one unit allowed on storage, reverse path so train stands at allocated position
1345+
// if multiple units allowed on storage, do not reverse path so train moves to end of storage location
13401346
else
13411347
{
1342-
newRoute = new Train.TCSubpathRoute(AdditionalTurntableDetails.AccessPaths[0].AccessPath.ReversePath(train.signalRef));
1343-
poolStorageIndex = reqPool;
1344-
}
1345-
}
1348+
if (StoragePool[poolStorageIndex].maxStoredUnits == 1)
1349+
{
1350+
newRoute = new Train.TCSubpathRoute(AdditionalTurntableDetails.AccessPaths[0].AccessPath.ReversePath(train.signalRef));
1351+
}
1352+
else
1353+
{
1354+
newRoute = new Train.TCSubpathRoute(AdditionalTurntableDetails.AccessPaths[0].AccessPath);
1355+
}
13461356

1347-
// if route is valid, set state for last section to approach moving table
1348-
// also add unit to storage as claim
1349-
if (newRoute != null)
1350-
{
1351-
newRoute.Last().MovingTableApproachPath = reqPath;
1352-
AddUnit(train, true);
1353-
StoragePool[poolStorageIndex].ClaimUnits.Add(train.Number);
1357+
train.PoolStorageIndex = poolStorageIndex = reqPool;
1358+
}
13541359
}
13551360
return (newRoute);
13561361
}
@@ -1365,6 +1370,12 @@ override public float GetEndOfRouteDistance(Train.TCSubpathRoute thisRoute, Trai
13651370
// get distance to approach point from present position of train
13661371
int turntableSectionIndex = thisRoute.GetRouteIndex(AdditionalTurntableDetails.AccessPaths[pathIndex].AccessPath[0].TCSectionIndex, 0);
13671372
float startoffset = signalRef.TrackCircuitList[frontPosition.TCSectionIndex].Length - frontPosition.TCOffset;
1373+
if (frontPosition.RouteListIndex < 0 && turntableSectionIndex < 0)
1374+
{
1375+
Trace.TraceInformation("Invalid check on turntable approach : present position and turntable index both < 0; for pool : " + PoolName);
1376+
return (-1);
1377+
}
1378+
13681379
float distanceToTurntable = thisRoute.GetDistanceAlongRoute(frontPosition.RouteListIndex, startoffset,
13691380
turntableSectionIndex, AdditionalTurntableDetails.AccessPaths[pathIndex].TableApproachOffset, true, signalRef);
13701381

@@ -2493,6 +2504,8 @@ public void PrepareMoveOffTable()
24932504
parentTrain.ControlMode = Train.TRAIN_CONTROL.AUTO_NODE;
24942505
parentTrain.DistanceTravelledM = 0;
24952506
parentTrain.DelayedStartMoving(AITrain.AI_START_MOVEMENT.PATH_ACTION);
2507+
parentTrain.EndAuthorityType[0] = Train.END_AUTHORITY.NO_PATH_RESERVED;
2508+
parentTrain.EndAuthorityType[1] = Train.END_AUTHORITY.NO_PATH_RESERVED;
24962509

24972510
// actions for mode access (train going into storage)
24982511
if (MovingTableAction == MovingTableActionEnum.FromAccess)
@@ -2505,7 +2518,7 @@ public void PrepareMoveOffTable()
25052518
float endOffset = parentPool.StoragePool[StoragePathIndex].StoragePathTraveller.TrackNodeOffset + parentTrain.Length;
25062519
if (endOffset < parentTrain.DistanceToEndNodeAuthorityM[0])
25072520
{
2508-
parentTrain.DistanceToEndNodeAuthorityM[0] = endOffset;
2521+
parentTrain.DistanceToEndNodeAuthorityM[0] = parentTrain.NextStopDistanceM = endOffset;
25092522
}
25102523
}
25112524

@@ -2532,14 +2545,17 @@ public void PrepareMoveOffTable()
25322545
public void RemoveTrainFromTurntable()
25332546
{
25342547
// clear table
2548+
2549+
if (parentTurntable == null) parentTurntable = parentPool.Simulatorref.MovingTables[parentIndex] as Turntable;
2550+
25352551
parentTurntable.TrainsOnMovingTable.Clear();
25362552
parentTurntable.InUse = false;
25372553
parentTurntable.GoToAutoTarget = false;
25382554
trainOnTable = null;
25392555

25402556
// reset train speed
25412557
parentTrain.TrainMaxSpeedMpS = originalTrainMaxSpeedMpS;
2542-
Train.ActivateSpeedLimit activeSpeeds = new Train.ActivateSpeedLimit(0.0f, originalSpeedLimitMpS, originalSpeedSignalMpS);
2558+
Train.ActivateSpeedLimit activeSpeeds = new Train.ActivateSpeedLimit(0.0f, originalSpeedLimitMpS, originalSpeedSignalMpS, originalSpeedLimitMpS);
25432559

25442560
if (parentTrain.TrainType == Train.TRAINTYPE.PLAYER)
25452561
{
@@ -2586,4 +2602,4 @@ public bool TestTrainFormation (TTTrain parentTrain)
25862602
return (reqReverse);
25872603
}
25882604
}
2589-
}
2605+
}

0 commit comments

Comments
 (0)