Skip to content

Commit b78bcce

Browse files
committed
Fixes to user input
1 parent 71afdd6 commit b78bcce

File tree

2 files changed

+49
-75
lines changed

2 files changed

+49
-75
lines changed

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/CruiseControl.cs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ public enum SpeedSelectorMode { Parking, Neutral, On, Start }
145145
public float ParkingBrakeEngageSpeedMpS = 0;
146146
public float ParkingBrakePercent = 0;
147147
public bool DisableZeroForceStep = false;
148+
public bool DisableZeroSelectedSpeedStep = false;
148149
public bool DynamicBrakeIsSelectedForceDependant = false;
149150
public bool UseThrottleAsSpeedSelector = false;
150151
public bool UseThrottleAsForceSelector = false;
@@ -212,10 +213,8 @@ public enum SpeedSelectorMode { Parking, Neutral, On, Start }
212213
public MSTSNotchController MaxForceSelectorController;
213214
public MSTSNotchController SpeedSelectorController;
214215

215-
public bool SelectedSpeedPressed = false;
216216
public bool EngineBrakePriority = false;
217217
public int AccelerationBits = 0;
218-
public bool Speed0Pressed, SpeedDeltaPressed;
219218

220219
public CruiseControl(MSTSLocomotive locomotive)
221220
{
@@ -269,6 +268,7 @@ public CruiseControl(CruiseControl other, MSTSLocomotive locomotive)
269268
ParkingBrakeEngageSpeedMpS = other.ParkingBrakeEngageSpeedMpS;
270269
ParkingBrakePercent = other.ParkingBrakePercent;
271270
DisableZeroForceStep = other.DisableZeroForceStep;
271+
DisableZeroSelectedSpeedStep = other.DisableZeroSelectedSpeedStep;
272272
DynamicBrakeIsSelectedForceDependant = other.DynamicBrakeIsSelectedForceDependant;
273273
UseThrottleAsSpeedSelector = other.UseThrottleAsSpeedSelector;
274274
UseThrottleAsForceSelector = other.UseThrottleAsForceSelector;
@@ -362,6 +362,7 @@ public void Parse(STFReader stf)
362362
case "powerreductiondelaycargotrain": PowerReductionDelayCargoTrain = stf.ReadFloatBlock(STFReader.UNITS.Any, 0.0f); break;
363363
case "powerreductionvalue": PowerReductionValue = stf.ReadFloatBlock(STFReader.UNITS.Any, 100.0f); break;
364364
case "disablezeroforcestep": DisableZeroForceStep = stf.ReadBoolBlock(false); break;
365+
case "disablezeroselectedspeedstep": DisableZeroSelectedSpeedStep = stf.ReadBoolBlock(false); break;
365366
case "dynamicbrakeisselectedforcedependant": DynamicBrakeIsSelectedForceDependant = stf.ReadBoolBlock(false); break;
366367
case "defaultforcestep": defaultMaxAccelerationStep = stf.ReadFloatBlock(STFReader.UNITS.Any, 1.0f); break;
367368
case "startreducingspeeddelta": StartReducingSpeedDelta = (stf.ReadFloatBlock(STFReader.UNITS.Any, 1.0f) / 10); break;
@@ -525,8 +526,10 @@ public void Initialize()
525526
var notches = new List<MSTSNotch>();
526527
if (SpeedSelectorIsDiscrete)
527528
{
528-
float numNotches = (float)Math.Round(Locomotive.MaxSpeedMpS / SpeedRegulatorNominalSpeedStepMpS);
529-
for (int i=0; i<=numNotches; i++)
529+
if (!DisableZeroSelectedSpeedStep) notches.Add(new MSTSNotch(0, false, 0));
530+
if (MinimumSpeedForCCEffectMpS > 0) notches.Add(new MSTSNotch(float.Epsilon, false, 0));
531+
float numNotches = (float)Math.Round((Locomotive.MaxSpeedMpS - MinimumSpeedForCCEffectMpS) / SpeedRegulatorNominalSpeedStepMpS);
532+
for (int i = 1; i <= numNotches; i++)
530533
{
531534
notches.Add(new MSTSNotch(i / numNotches, false, 0));
532535
}
@@ -804,6 +807,8 @@ public void UpdateSpeedRegulatorModeChanges()
804807
var prevMode = SpeedRegMode;
805808
float throttle = Locomotive.ThrottleController.CurrentValue;
806809
float dynamic = (Locomotive.DynamicBrakeController?.CurrentValue ?? 0);
810+
bool zeroForce = MaxForceSelectorController.CurrentValue == 0 && MaxForceSelectorController.SavedValue == 0;
811+
bool zeroSelectedSpeed = SpeedSelectorController.CurrentValue == 0 && SpeedSelectorController.SavedValue == 0;
807812
if (DisableCruiseControlOnThrottleAndZeroSpeed)
808813
{
809814
if (throttle > 0 && Locomotive.AbsSpeedMpS == 0)
@@ -813,33 +818,33 @@ public void UpdateSpeedRegulatorModeChanges()
813818
}
814819
if (DisableCruiseControlOnThrottleAndZeroForce)
815820
{
816-
if ((throttle > 0 || UseThrottleAsForceSelector) && SelectedMaxAccelerationPercent == 0)
821+
if ((throttle > 0 || UseThrottleAsForceSelector) && zeroForce)
817822
{
818823
SpeedRegMode = SpeedRegulatorMode.Manual;
819824
}
820825
}
821826
if (DisableCruiseControlOnThrottleAndZeroForceAndZeroSpeed)
822827
{
823-
if ((throttle > 0 || UseThrottleAsForceSelector) && SelectedMaxAccelerationPercent == 0 && SelectedSpeedMpS == 0)
828+
if ((throttle > 0 || UseThrottleAsForceSelector) && zeroForce && zeroSelectedSpeed)
824829
{
825830
SpeedRegMode = SpeedRegulatorMode.Manual;
826831
}
827832
}
828833
if (ForceRegulatorAutoWhenNonZeroSpeedSelected)
829834
{
830-
if (SelectedSpeedMpS == 0 && (!ASCSpeedTakesPriorityOverSpeedSelector || !ASCSetSpeedMpS.HasValue))
835+
if (zeroSelectedSpeed && (!ASCSpeedTakesPriorityOverSpeedSelector || !ASCSetSpeedMpS.HasValue))
831836
{
832837
SpeedRegMode = SpeedRegulatorMode.Manual;
833838
}
834-
else
839+
else if (SelectedSpeedMpS > 0)
835840
{
836841
SpeedRegMode = SpeedRegulatorMode.Auto;
837842
}
838843
}
839844
if (ForceRegulatorAutoWhenNonZeroSpeedSelectedAndThrottleAtZero)
840845
{
841846
if (SelectedSpeedMpS > 0 && throttle == 0 && dynamic == 0 &&
842-
SelectedMaxAccelerationPercent == 0 && DisableCruiseControlOnThrottleAndZeroForceAndZeroSpeed)
847+
zeroForce && DisableCruiseControlOnThrottleAndZeroForceAndZeroSpeed)
843848
{
844849
SpeedRegMode = SpeedRegulatorMode.Auto;
845850
}
@@ -1173,8 +1178,8 @@ public void SpeedSelectorIncreaseStep()
11731178

11741179
if (SpeedSelectorController.CurrentValue > 0 || MinimumSpeedForCCEffectMpS == 0)
11751180
{
1176-
float speed = (SpeedSelectorController.CurrentValue * (Locomotive.MaxSpeedMpS - MinimumSpeedForCCEffectMpS) + MinimumSpeedForCCEffectMpS) + SpeedRegulatorNominalSpeedStepMpS;
1177-
SelectedSpeedMpS = Math.Min((float)Math.Round(speed / SpeedRegulatorNominalSpeedStepMpS) * SpeedRegulatorNominalSpeedStepMpS, Locomotive.MaxSpeedMpS);
1181+
float speed = ControllerValueToSelectedSpeedMpS(SpeedSelectorController.CurrentValue) + SpeedRegulatorNominalSpeedStepMpS;
1182+
SelectedSpeedMpS = Math.Min(speed, Locomotive.MaxSpeedMpS);
11781183
}
11791184
else
11801185
{
@@ -1189,26 +1194,20 @@ public void SpeedSelectorDecreaseStep()
11891194
if (time >= selectedSpeedLeverHoldTime && time < selectedSpeedLeverHoldTime + SpeedSelectorStepTimeSeconds) return;
11901195
selectedSpeedLeverHoldTime = time;
11911196

1192-
float speed = SpeedSelectorController.CurrentValue * (Locomotive.MaxSpeedMpS - MinimumSpeedForCCEffectMpS) + MinimumSpeedForCCEffectMpS - SpeedRegulatorNominalSpeedStepMpS;
1197+
float speed = ControllerValueToSelectedSpeedMpS(SpeedSelectorController.CurrentValue) - SpeedRegulatorNominalSpeedStepMpS;
11931198
if (speed < MinimumSpeedForCCEffectMpS)
11941199
{
11951200
SelectedSpeedMpS = 0;
11961201
}
11971202
else
11981203
{
1199-
SelectedSpeedMpS = Math.Max((float)Math.Round(speed / SpeedRegulatorNominalSpeedStepMpS) * SpeedRegulatorNominalSpeedStepMpS, MinimumSpeedForCCEffectMpS);
1204+
SelectedSpeedMpS = speed;
12001205
}
12011206
ConfirmSelectedSpeed();
12021207
}
12031208
public void ConfirmSelectedSpeed()
12041209
{
1205-
float val = SpeedSelectorController.CurrentValue;
1206-
if (val > 0)
1207-
{
1208-
float min = MinimumSpeedForCCEffectMpS;
1209-
float max = Locomotive.MaxSpeedMpS;
1210-
val = val * (max - min) + min;
1211-
}
1210+
float val = ControllerValueToSelectedSpeedMpS(SpeedSelectorController.CurrentValue);
12121211
if (SpeedIsMph)
12131212
Simulator.Confirmer.Message(ConfirmLevel.Information, Simulator.Catalog.GetStringFmt("Selected speed changed to {0} mph", Math.Round(MpS.FromMpS(val, false), 0, MidpointRounding.AwayFromZero).ToString()));
12141213
else
@@ -1501,19 +1500,6 @@ public float GetDataOf(CabViewControl cvc)
15011500
data = AccelerationBits;
15021501
break;
15031502
}
1504-
case CABViewControlTypes.ORTS_CC_SELECTED_SPEED:
1505-
data = SelectedSpeedPressed ? 1 : 0;
1506-
break;
1507-
case CABViewControlTypes.ORTS_CC_SPEED_0:
1508-
{
1509-
data = Speed0Pressed ? 1 : 0;
1510-
break;
1511-
}
1512-
case CABViewControlTypes.ORTS_CC_SPEED_DELTA:
1513-
{
1514-
data = SpeedDeltaPressed ? 1 : 0;
1515-
break;
1516-
}
15171503
default:
15181504
data = 0;
15191505
break;

Source/RunActivity/Viewer3D/RollingStock/MSTSLocomotiveViewer.cs

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,13 +2303,15 @@ public virtual int GetDrawIndex()
23032303
case CABViewControlTypes.ORTS_TRAIN_TYPE_PAX_OR_CARGO:
23042304
case CABViewControlTypes.ORTS_CONTROLLER_VOLTAGE:
23052305
case CABViewControlTypes.ORTS_AMPERS_BY_CONTROLLER_VOLTAGE:
2306-
case CABViewControlTypes.ORTS_CC_SELECTED_SPEED:
23072306
case CABViewControlTypes.ORTS_MULTI_POSITION_CONTROLLER:
23082307
case CABViewControlTypes.ORTS_ACCELERATION_IN_TIME:
2309-
case CABViewControlTypes.ORTS_CC_SPEED_DELTA:
2310-
case CABViewControlTypes.ORTS_CC_SPEED_0:
23112308
index = (int)data;
23122309
break;
2310+
case CABViewControlTypes.ORTS_CC_SPEED_DELTA:
2311+
case CABViewControlTypes.ORTS_CC_SPEED_0:
2312+
case CABViewControlTypes.ORTS_CC_SELECTED_SPEED:
2313+
index = ButtonState ? 1 : 0;
2314+
break;
23132315
case CABViewControlTypes.ORTS_SELECTED_SPEED_SELECTOR:
23142316
var fraction = data / (float)(ControlDiscrete.MaxValue);
23152317
index = (int)MathHelper.Clamp(0, (int)(fraction * ((ControlDiscrete as CVCDiscrete).FramesCount - 1)), (ControlDiscrete as CVCDiscrete).FramesCount - 1);
@@ -2663,16 +2665,13 @@ public void HandleUserInput()
26632665
case CABViewControlTypes.ORTS_CC_SELECTED_SPEED:
26642666
if (Locomotive.CruiseControl == null)
26652667
break;
2666-
var p = ChangedValue(0);
2667-
if (p == 1)
2668-
{
2668+
buttonState = ChangedValue(ButtonState ? 1 : 0) > 0;
2669+
if (!ButtonState && buttonState)
26692670
Locomotive.CruiseControl.SetSpeed(Control.Parameter1);
2670-
Locomotive.CruiseControl.SelectedSpeedPressed = true;
2671-
}
2672-
else if (p == 0) Locomotive.CruiseControl.SelectedSpeedPressed = false;
2671+
ButtonState = buttonState;
26732672
break;
26742673
case CABViewControlTypes.ORTS_SELECTED_SPEED_REGULATOR_MODE:
2675-
p = ChangedValue(0);
2674+
var p = ChangedValue(0);
26762675
if (Control.ControlStyle == CABViewControlStyles.ONOFF)
26772676
{
26782677
if (ChangedValue(0) == 1)
@@ -2773,85 +2772,74 @@ public void HandleUserInput()
27732772
break;
27742773
}
27752774
case CABViewControlTypes.ORTS_CC_SPEED_0:
2776-
{
2777-
p = ChangedValue(0);
2778-
if (p == 1)
2779-
{
2780-
Locomotive.CruiseControl.SetSpeed(0);
2781-
Locomotive.CruiseControl.Speed0Pressed = true;
2782-
}
2783-
else if (p == 0) Locomotive.CruiseControl.Speed0Pressed = false;
2784-
2785-
break;
2786-
}
2787-
case CABViewControlTypes.ORTS_CC_SPEED_DELTA:
2788-
{
2789-
p = ChangedValue(0);
2790-
if (p == 1)
2791-
{
2792-
Locomotive.CruiseControl.SetSpeed(Locomotive.CruiseControl.SetSpeedKpHOrMpH + Control.Parameter1);
2793-
Locomotive.CruiseControl.SpeedDeltaPressed = true;
2794-
}
2795-
else if (p == 0) Locomotive.CruiseControl.SpeedDeltaPressed = false;
2796-
break;
2797-
}
2775+
buttonState = ChangedValue(ButtonState ? 1 : 0) > 0;
2776+
if (!ButtonState && buttonState)
2777+
Locomotive.CruiseControl.SetSpeed(0);
2778+
ButtonState = buttonState;
2779+
break;
2780+
case CABViewControlTypes.ORTS_CC_SPEED_DELTA:
2781+
buttonState = ChangedValue(ButtonState ? 1 : 0) > 0;
2782+
if (!ButtonState && buttonState)
2783+
Locomotive.CruiseControl.SetSpeed(Locomotive.CruiseControl.SetSpeedKpHOrMpH + Control.Parameter1);
2784+
ButtonState = buttonState;
2785+
break;
27982786
case CABViewControlTypes.ORTS_DP_MOVE_TO_FRONT:
27992787
buttonState = ChangedValue(ButtonState ? 1 : 0) > 0;
2800-
if (!ButtonState && (ButtonState ? 1 : 0) != ChangedValue(ButtonState ? 1 : 0))
2788+
if (!ButtonState && buttonState)
28012789
new DPMoveToFrontCommand(Viewer.Log);
28022790
ButtonState = buttonState;
28032791
break;
28042792
case CABViewControlTypes.ORTS_DP_MOVE_TO_BACK:
28052793
buttonState = ChangedValue(ButtonState ? 1 : 0) > 0;
2806-
if (!ButtonState && (ButtonState ? 1 : 0) != ChangedValue(ButtonState ? 1 : 0))
2794+
if (!ButtonState && buttonState)
28072795
new DPMoveToBackCommand(Viewer.Log);
28082796
ButtonState = buttonState;
28092797
break;
28102798
case CABViewControlTypes.ORTS_DP_IDLE:
28112799
buttonState = ChangedValue(ButtonState ? 1 : 0) > 0;
2812-
if (!ButtonState && (ButtonState ? 1 : 0) != ChangedValue(ButtonState ? 1 : 0))
2800+
if (!ButtonState && buttonState)
28132801
new DPIdleCommand(Viewer.Log);
28142802
ButtonState = buttonState;
28152803
break;
28162804
case CABViewControlTypes.ORTS_DP_TRACTION:
28172805
buttonState = ChangedValue(ButtonState ? 1 : 0) > 0;
2818-
if (!ButtonState && (ButtonState ? 1 : 0) != ChangedValue(ButtonState ? 1 : 0))
2806+
if (!ButtonState && buttonState)
28192807
new DPTractionCommand(Viewer.Log);
28202808
ButtonState = buttonState;
28212809
break;
28222810
case CABViewControlTypes.ORTS_DP_BRAKE:
28232811
buttonState = ChangedValue(ButtonState ? 1 : 0) > 0;
2824-
if (!ButtonState && (ButtonState ? 1 : 0) != ChangedValue(ButtonState ? 1 : 0))
2812+
if (!ButtonState && buttonState)
28252813
new DPDynamicBrakeCommand(Viewer.Log);
28262814
ButtonState = buttonState;
28272815
break;
28282816
case CABViewControlTypes.ORTS_DP_MORE:
28292817
buttonState = ChangedValue(ButtonState ? 1 : 0) > 0;
2830-
if (!ButtonState && (ButtonState ? 1 : 0) != ChangedValue(ButtonState ? 1 : 0))
2818+
if (!ButtonState && buttonState)
28312819
new DPMoreCommand(Viewer.Log);
28322820
ButtonState = buttonState;
28332821
break;
28342822
case CABViewControlTypes.ORTS_DP_LESS:
28352823
buttonState = ChangedValue(ButtonState ? 1 : 0) > 0;
2836-
if (!ButtonState && (ButtonState ? 1 : 0) != ChangedValue(ButtonState ? 1 : 0))
2824+
if (!ButtonState && buttonState)
28372825
new DPLessCommand(Viewer.Log);
28382826
ButtonState = buttonState;
28392827
break;
28402828
case CABViewControlTypes.ORTS_EOT_COMM_TEST:
28412829
buttonState = ChangedValue(ButtonState ? 1 : 0) > 0;
2842-
if (!ButtonState && (ButtonState ? 1 : 0) != ChangedValue(ButtonState ? 1 : 0))
2830+
if (!ButtonState && buttonState)
28432831
new EOTCommTestCommand(Viewer.Log);
28442832
ButtonState = buttonState;
28452833
break;
28462834
case CABViewControlTypes.ORTS_EOT_DISARM:
28472835
buttonState = ChangedValue(ButtonState ? 1 : 0) > 0;
2848-
if (!ButtonState && (ButtonState ? 1 : 0) != ChangedValue(ButtonState ? 1 : 0))
2836+
if (!ButtonState && buttonState)
28492837
new EOTDisarmCommand(Viewer.Log);
28502838
ButtonState = buttonState;
28512839
break;
28522840
case CABViewControlTypes.ORTS_EOT_ARM_TWO_WAY:
28532841
buttonState = ChangedValue(ButtonState ? 1 : 0) > 0;
2854-
if (!ButtonState && (ButtonState ? 1 : 0) != ChangedValue(ButtonState ? 1 : 0))
2842+
if (!ButtonState && buttonState)
28552843
new EOTArmTwoWayCommand(Viewer.Log);
28562844
ButtonState = buttonState;
28572845
break;

0 commit comments

Comments
 (0)