Skip to content

Commit de1bccb

Browse files
committed
Clean up cabview component further, support notchless dynamic brakes, improve control consistency with previous versions, avoid some potential null references
1 parent a0ffa96 commit de1bccb

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

Source/Orts.Simulation/Simulation/RollingStocks/MSTSLocomotive.cs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,10 +2106,10 @@ public override void Update(float elapsedClockSeconds)
21062106
if (DynamicBrakeCommandStartTime + DynamicBrakeDelayS < Simulator.ClockTime)
21072107
{
21082108
DynamicBrake = true; // Engage
2109-
if (IsLeadLocomotive() && DynamicBrakeController.CurrentValue > 0)
2109+
if (IsLeadLocomotive() && DynamicBrakeController?.CurrentValue > 0)
21102110
Simulator.Confirmer.ConfirmWithPerCent(CabControl.DynamicBrake, DynamicBrakeController.CurrentValue * 100);
21112111
}
2112-
else if (IsLeadLocomotive() && DynamicBrakeController.CurrentValue > 0)
2112+
else if (IsLeadLocomotive() && DynamicBrakeController?.CurrentValue > 0)
21132113
Simulator.Confirmer.Confirm(CabControl.DynamicBrake, CabSetting.On); // Keeping status string on screen so user knows what's happening
21142114
}
21152115
}
@@ -2394,24 +2394,31 @@ protected virtual void UpdateControllers(float elapsedClockSeconds)
23942394
{
23952395
LocalDynamicBrakePercent = TrainBrakeController.TrainDynamicBrakeIntervention * 100;
23962396
}
2397-
if (DynamicBrakeController != null && DynamicBrakeController.CurrentValue > 0)
2397+
if (DynamicBrakeController != null && (DynamicBrakeController.CurrentValue > 0 || DynamicBrakeController.UpdateValue > 0))
23982398
{
23992399
float prevValue = DynamicBrakeController.CurrentValue;
2400+
24002401
if (DynamicBrake || !DynamicBrakeControllerSetupLock)
24012402
DynamicBrakeController.Update(elapsedClockSeconds);
2402-
// Dynamic brake is about to be disabled, pause motion of controller to prevent accidental shut-off
2403-
if (DynamicBrakeController.CurrentValue <= 0)
2403+
2404+
// Dynamic brake is about to be enabled or disabled, pause motion of controller to prevent accidental overshooting
2405+
if ((DynamicBrakeController.CurrentValue <= 0 && prevValue > 0) || (DynamicBrakeController.CurrentValue > 0 && prevValue <= 0))
24042406
{
24052407
if (!DynamicBrakePause)
24062408
{
24072409
DynamicBrakePause = true;
2408-
StopDynamicBrakeDecrease();
2409-
DynamicBrakeController.CurrentValue = prevValue;
2410+
if (DynamicBrakeController.UpdateValue < 0)
2411+
{
2412+
StopDynamicBrakeDecrease();
2413+
DynamicBrakeController.CurrentValue = prevValue;
2414+
}
2415+
else if (DynamicBrakeController.UpdateValue > 0)
2416+
StopDynamicBrakeIncrease();
24102417
}
24112418
else
24122419
DynamicBrakePause = false;
24132420
}
2414-
else if (DynamicBrakeController.CurrentValue > prevValue)
2421+
else if (DynamicBrakeController.UpdateValue != 0)
24152422
DynamicBrakePause = false;
24162423

24172424
LocalDynamicBrakePercent = Math.Max(DynamicBrakeController.CurrentValue * 100, LocalDynamicBrakePercent);
@@ -4098,7 +4105,7 @@ public void SetCombinedHandleValue(float value)
40984105
ThrottleController.IntermediateValue = 0;
40994106
}
41004107
// Return to throttling
4101-
else if ((DynamicBrakeController.CurrentValue <= 0 && value < CombinedControlSplitPosition) ||
4108+
else if ((DynamicBrakeController?.CurrentValue ?? 0) <= 0 ||
41024109
(CruiseControl != null && !CruiseControl.DynamicBrakePriority && CruiseControl.SpeedRegMode == CruiseControl.SpeedRegulatorMode.Auto))
41034110
{
41044111
SetThrottleValue(1 - MathHelper.Clamp(value, 0, CombinedControlSplitPosition) / CombinedControlSplitPosition);
@@ -4118,9 +4125,9 @@ public void SetCombinedHandleValue(float value)
41184125
/// <returns>Combined position into 0-1 range, where arrangement is [[1--throttle--0]split[0--dynamic|airbrake--1]]</returns>
41194126
public float GetCombinedHandleValue(bool intermediateValue)
41204127
{
4121-
float throttleValue = intermediateValue ? ThrottleController.IntermediateValue : ThrottleController.CurrentValue;
4122-
float dynamicsValue = intermediateValue ? DynamicBrakeController.IntermediateValue : DynamicBrakeController.CurrentValue;
4123-
float brakesValue = intermediateValue ? TrainBrakeController.IntermediateValue : TrainBrakeController.CurrentValue;
4128+
var throttleValue = intermediateValue ? ThrottleController?.IntermediateValue : ThrottleController?.CurrentValue;
4129+
var dynamicsValue = intermediateValue ? DynamicBrakeController?.IntermediateValue : DynamicBrakeController?.CurrentValue;
4130+
var brakesValue = intermediateValue ? TrainBrakeController?.IntermediateValue : TrainBrakeController?.CurrentValue;
41244131

41254132
if (CruiseControl?.SpeedRegMode == CruiseControl.SpeedRegulatorMode.Auto)
41264133
{
@@ -4142,14 +4149,14 @@ public float GetCombinedHandleValue(bool intermediateValue)
41424149
if (CruiseControl != null && CruiseControl.SkipThrottleDisplay && !CruiseControl.DynamicBrakeCommandHasPriorityOverCruiseControl)
41434150
return CombinedControlSplitPosition;
41444151
else
4145-
return CombinedControlSplitPosition + (1 - CombinedControlSplitPosition) * dynamicsValue;
4152+
return CombinedControlSplitPosition + (1 - CombinedControlSplitPosition) * (dynamicsValue ?? 0);
41464153
}
41474154
else if (CombinedControlType == CombinedControl.ThrottleAir && throttleValue <= 0 && brakesValue > 0)
4148-
return CombinedControlSplitPosition + (1 - CombinedControlSplitPosition) * brakesValue;
4155+
return CombinedControlSplitPosition + (1 - CombinedControlSplitPosition) * (brakesValue ?? 0);
41494156
else if (CruiseControl == null)
4150-
return CombinedControlSplitPosition * (1 - throttleValue);
4157+
return CombinedControlSplitPosition * (1 - (throttleValue ?? 0));
41514158
else if (CruiseControl.SpeedRegMode == CruiseControl.SpeedRegulatorMode.Manual)
4152-
return CombinedControlSplitPosition * (1 - throttleValue);
4159+
return CombinedControlSplitPosition * (1 - (throttleValue ?? 0));
41534160
else if (CruiseControl.UseThrottleAsSpeedSelector)
41544161
return CombinedControlSplitPosition * (1 - (CruiseControl.SelectedSpeedMpS / MaxSpeedMpS));
41554162
else if (CruiseControl.UseThrottleAsForceSelector && CruiseControl.UseThrottleInCombinedControl)
@@ -4726,6 +4733,8 @@ public void StartDynamicBrakeIncrease(float? target)
47264733
// Only allow increasing dynamic brake if dynamic braking is off, there's no setup lock, or dynamic braking is already active
47274734
if (DynamicBrake || !DynamicBrakeControllerSetupLock || DynamicBrakeController.CurrentValue <= 0)
47284735
{
4736+
float prevValue = DynamicBrakeController.CurrentValue;
4737+
47294738
DynamicBrakeController.StartIncrease(target);
47304739

47314740
AlerterReset(TCSEvent.DynamicBrakeChanged);
@@ -4735,6 +4744,12 @@ public void StartDynamicBrakeIncrease(float? target)
47354744
StopDynamicBrakeIncrease();
47364745
Simulator.Confirmer.ConfirmWithPerCent(CabControl.DynamicBrake, DynamicBrakeController.CurrentValue * 100);
47374746
}
4747+
// If this is the first application of dynamics, pause movement to prevent accidental over-application
4748+
else if (DynamicBrakeController.CurrentValue > 0 && prevValue <= 0)
4749+
{
4750+
DynamicBrakePause = true;
4751+
StopDynamicBrakeIncrease();
4752+
}
47384753
}
47394754
}
47404755

@@ -4862,7 +4877,8 @@ public void SetDynamicBrakePercentWithSound(float percent)
48624877

48634878
public bool CheckDisableDynamicBrake()
48644879
{
4865-
if (DynamicBrakeController.CurrentValue <= 0)
4880+
// Only disable the dynamic brake if the lever is in the off position and the controller isn't trying to increase
4881+
if (DynamicBrakeController.CurrentValue <= 0 && DynamicBrakeController.UpdateValue <= 0)
48664882
{
48674883
StopDynamicBrakeDecrease();
48684884
Simulator.Confirmer.Confirm(CabControl.DynamicBrake, CabSetting.Off);

Source/RunActivity/Viewer3D/RollingStock/MSTSLocomotiveViewer.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ protected virtual void ReverserControlForwards()
118118
{
119119
if (Locomotive.Direction != Direction.Forward
120120
&& (Locomotive.ThrottlePercent >= 1
121-
|| Math.Abs(Locomotive.SpeedMpS) > 1 || Locomotive.DynamicBrakeController.CurrentValue > 0))
121+
|| Math.Abs(Locomotive.SpeedMpS) > 1 || Locomotive.DynamicBrakeController?.CurrentValue > 0))
122122
{
123123
Viewer.Simulator.Confirmer.Warning(CabControl.Reverser, CabSetting.Warn1);
124124
return;
@@ -130,7 +130,7 @@ protected virtual void ReverserControlBackwards()
130130
{
131131
if (Locomotive.Direction != Direction.Reverse
132132
&& (Locomotive.ThrottlePercent >= 1
133-
|| Math.Abs(Locomotive.SpeedMpS) > 1 || Locomotive.DynamicBrakeController.CurrentValue > 0))
133+
|| Math.Abs(Locomotive.SpeedMpS) > 1 || Locomotive.DynamicBrakeController?.CurrentValue > 0))
134134
{
135135
Viewer.Simulator.Confirmer.Warning(CabControl.Reverser, CabSetting.Warn1);
136136
return;
@@ -2132,22 +2132,12 @@ public virtual int GetDrawIndex()
21322132
case CABViewControlTypes.DYNAMIC_BRAKE_DISPLAY:
21332133
var dynBrakePercent = Locomotive.Train.TrainType == Train.TRAINTYPE.AI_PLAYERHOSTING ?
21342134
Locomotive.DynamicBrakePercent : Locomotive.LocalDynamicBrakePercent;
2135-
if (Locomotive.DynamicBrakeController != null)
2136-
{
2137-
if (dynBrakePercent <= 0)
2138-
{
2139-
index = 0;
2140-
break;
2141-
}
2142-
if (!Locomotive.HasSmoothStruc)
2143-
index = Locomotive.DynamicBrakeController != null ? Locomotive.DynamicBrakeController.CurrentNotch : 0;
2144-
else
2145-
index = PercentToIndex(dynBrakePercent);
2146-
}
2135+
if (dynBrakePercent <= 0)
2136+
index = 0;
2137+
else if (!Locomotive.HasSmoothStruc && Locomotive.DynamicBrakeController != null)
2138+
index = Locomotive.DynamicBrakeController.CurrentNotch;
21472139
else
2148-
{
21492140
index = PercentToIndex(dynBrakePercent);
2150-
}
21512141
break;
21522142
case CABViewControlTypes.CPH_DISPLAY:
21532143
if (Locomotive.CombinedControlType == MSTSLocomotive.CombinedControl.ThrottleDynamic && Locomotive.DynamicBrakePercent >= 0)

0 commit comments

Comments
 (0)