Skip to content

Commit 8e62073

Browse files
committed
[train] fix cash on setSpeed() when no table is configured
1 parent cb78b4d commit 8e62073

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

server/src/train/train.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,21 +1217,29 @@ std::error_code Train::setSpeed(Throttle& throttle, double value)
12171217

12181218
value = std::clamp(value, Attributes::getMin(speed), Attributes::getMax(speed));
12191219

1220-
// Get speed point
1221-
auto match = m_speedTable->getClosestMatch(value);
1220+
if(m_speedTable)
1221+
{
1222+
// Get speed point
1223+
auto match = m_speedTable->getClosestMatch(value);
12221224

1223-
SpeedPoint speedPoint;
1224-
speedPoint.speedMetersPerSecond = match.tableEntry.avgSpeed;
1225-
speedPoint.tableIdx = match.tableIdx;
1225+
SpeedPoint speedPoint;
1226+
speedPoint.speedMetersPerSecond = match.tableEntry.avgSpeed;
1227+
speedPoint.tableIdx = match.tableIdx;
12261228

1227-
// Force set speed bypassing user acceleration
1228-
setThrottleSpeed(speedPoint, true);
1229+
// Force set speed bypassing user acceleration
1230+
setThrottleSpeed(speedPoint, true);
12291231

1230-
// Update real speed property
1231-
const double realSpeedMS = throttleSpeedPoint.speedMetersPerSecond * m_world.scaleRatio.value();
1232-
throttleSpeed.setValueInternal(convertUnit(realSpeedMS,
1233-
SpeedUnit::MeterPerSecond,
1234-
throttleSpeed.unit()));
1232+
// Update real speed property
1233+
const double realSpeedMS = throttleSpeedPoint.speedMetersPerSecond * m_world.scaleRatio.value();
1234+
throttleSpeed.setValueInternal(convertUnit(realSpeedMS,
1235+
SpeedUnit::MeterPerSecond,
1236+
throttleSpeed.unit()));
1237+
}
1238+
else
1239+
{
1240+
// Use legacy logic
1241+
setThrottleSpeed(SpeedPoint(), true);
1242+
}
12351243

12361244
const bool currentValue = isStopped;
12371245
isStopped.setValueInternal(m_speedState == SpeedState::Idle && almostZero(speed.value()) && almostZero(throttleSpeed.value()));

0 commit comments

Comments
 (0)