Skip to content

Commit 9ebd050

Browse files
authored
Merge pull request #785 from Roeterdink/IncorrectCalculationTrainAhead
Incorrect calculation of distance to train ahead
2 parents eb30a97 + fd121be commit 9ebd050

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

Source/Orts.Simulation/Simulation/Signalling/Signals.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3894,7 +3894,7 @@ public List<int> ScanRoute(Train thisTrain, int firstSectionIndex, float firstOf
38943894
if (considerSpeedReset)
38953895
{
38963896
var speed_infoR = thisSpeedpost.this_sig_speed(SignalFunction.SPEED);
3897-
speed_info.speed_reset = speed_infoR.speed_reset;
3897+
if (speed_infoR != null) speed_info.speed_reset = speed_infoR.speed_reset;
38983898
}
38993899
if ((isFreight && speed_info.speed_freight > 0) || (!isFreight && speed_info.speed_pass > 0) || speed_info.speed_reset == 1)
39003900
{

Source/Orts.Simulation/Simulation/Signalling/TrackCircuitSection.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,8 +1795,35 @@ public Dictionary<Train, float> TestTrainAhead(Train thisTrain, float offset, in
17951795
}
17961796
else
17971797
{
1798-
distanceTrainAheadM = offset; // train is off its route - assume full section occupied, offset is deducted later //
1799-
trainFound = nextTrain.Train;
1798+
distanceTrainAheadM = offset; // train is off its route - check if track occupied by train is ahead or behind us //
1799+
1800+
if (thisTrain != null)
1801+
{
1802+
int presentFront = thisTrain.PresentPosition[0].RouteListIndex;
1803+
1804+
foreach (TrackCircuitSection occSection in nextTrain.Train.OccupiedTrack)
1805+
{
1806+
int otherSectionIndex = thisTrain.TCRoute.TCRouteSubpaths[thisTrain.TCRoute.activeSubpath].GetRouteIndex(occSection.Index, 0);
1807+
1808+
// other index is lower - train is behind us
1809+
if (otherSectionIndex >= 0 && otherSectionIndex < presentFront)
1810+
{
1811+
trainFound = null;
1812+
continue;
1813+
}
1814+
// other index is higher - train is in front of us
1815+
else if (otherSectionIndex >= 0 && otherSectionIndex > presentFront)
1816+
{
1817+
trainFound = nextTrain.Train;
1818+
continue;
1819+
}
1820+
}
1821+
}
1822+
else
1823+
{
1824+
// else assume ahead of us - assume full section occupied, offset is deducted later //
1825+
trainFound = nextTrain.Train;
1826+
}
18001827
}
18011828
}
18021829

0 commit comments

Comments
 (0)