Skip to content

Commit b499ec9

Browse files
committed
Add support for cruise control
1 parent f6d3e76 commit b499ec9

File tree

6 files changed

+126
-128
lines changed

6 files changed

+126
-128
lines changed

Source/Orts.Formats.Msts/CabViewFile.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,11 @@ public struct CabViewControlType
343343
{
344344
public CABViewControlTypes Type;
345345
public int Id;
346+
public CabViewControlType(CABViewControlTypes type)
347+
{
348+
Type = type;
349+
Id = 0;
350+
}
346351
public CabViewControlType(string name)
347352
{
348353
Type = CABViewControlTypes.NONE;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3820,6 +3820,8 @@ public void SetThrottlePercent(float percent)
38203820
if (!CruiseControl.UseThrottleInCombinedControl) ThrottleController.SetPercent(percent);
38213821
return;
38223822
}
3823+
else if (CruiseControl.UseThrottleAsSpeedSelector && CruiseControl.SpeedRegMode == CruiseControl.SpeedRegulatorMode.Auto)
3824+
CruiseControl.SetSpeed(MpS.FromMpS(percent * MaxSpeedMpS / 100, !CruiseControl.SpeedIsMph));
38233825
else
38243826
ThrottleController.SetPercent(percent);
38253827
}

Source/RunActivity/Viewer3D/RollingStock/MSTSLocomotiveViewer.cs

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -238,55 +238,67 @@ public override void HandleUserInput(ElapsedTime elapsedTime)
238238
foreach (var external in externalDevices)
239239
{
240240
if (external == null) continue;
241-
if (external.Throttle.Changed)
242-
Locomotive.SetThrottlePercentWithSound(external.Throttle.Value * 100);
243-
if (external.TrainBrake.Changed)
244-
Locomotive.SetTrainBrakePercent(external.TrainBrake.Value * 100);
245-
if (external.EngineBrake.Changed)
246-
Locomotive.SetEngineBrakePercent(external.EngineBrake.Value * 100);
247-
// Locomotive.SetBrakemanBrakePercent(external.BrakemanBrakePercent); // For Raildriver control not complete for this value?
248-
if (external.DynamicBrake.Changed && Locomotive.CombinedControlType != MSTSLocomotive.CombinedControl.ThrottleAir)
249-
Locomotive.SetDynamicBrakePercentWithSound(external.DynamicBrake.Value * 100);
250-
251-
if (external.Direction.Changed)
252-
{
253-
if (Locomotive is MSTSSteamLocomotive steam)
254-
{
255-
steam.SetCutoffPercent(UserInput.RDState.Direction.Value * 100);
256-
}
257-
else if (external.Direction.Value > 0.5f)
258-
Locomotive.SetDirection(Direction.Forward);
259-
else if (external.Direction.Value < -0.5f)
260-
Locomotive.SetDirection(Direction.Reverse);
261-
else
262-
Locomotive.SetDirection(Direction.N);
263-
}
264-
265-
if (external.Lights.Changed)
266-
{
267-
// changing Headlight more than one step at a time doesn't work for some reason
268-
if (Locomotive.Headlight < external.Lights.Value - 1)
269-
{
270-
Locomotive.Headlight++;
271-
Locomotive.SignalEvent(Event.LightSwitchToggle);
272-
}
273-
if (Locomotive.Headlight > external.Lights.Value - 1)
274-
{
275-
Locomotive.Headlight--;
276-
Locomotive.SignalEvent(Event.LightSwitchToggle);
277-
}
278-
}
279241
// Handle other cabcontrols
280242
foreach (var kvp in external.CabControls)
281243
{
282244
if (_CabRenderer == null) break;
283245
if (!kvp.Value.Changed) continue;
284-
if (_CabRenderer.ControlMap.TryGetValue(kvp.Key, out var renderer) && renderer is CabViewDiscreteRenderer discrete)
246+
float val = kvp.Value.Value;
247+
switch (kvp.Key.Item1.Type)
285248
{
286-
var oldChanged = discrete.ChangedValue;
287-
discrete.ChangedValue = (val) => kvp.Value.Value;
288-
discrete.HandleUserInput();
289-
discrete.ChangedValue = oldChanged;
249+
// Some cab controls need specific handling for better results
250+
case CABViewControlTypes.THROTTLE:
251+
Locomotive.SetThrottlePercentWithSound(val * 100);
252+
break;
253+
case CABViewControlTypes.DIRECTION:
254+
if (Locomotive is MSTSSteamLocomotive steam)
255+
{
256+
steam.SetCutoffPercent(val * 100);
257+
}
258+
else if (val > 0.5f)
259+
Locomotive.SetDirection(Direction.Forward);
260+
else if (val < -0.5f)
261+
Locomotive.SetDirection(Direction.Reverse);
262+
else
263+
Locomotive.SetDirection(Direction.N);
264+
break;
265+
case CABViewControlTypes.TRAIN_BRAKE:
266+
Locomotive.SetTrainBrakePercent(val * 100);
267+
break;
268+
case CABViewControlTypes.DYNAMIC_BRAKE:
269+
if (Locomotive.CombinedControlType != MSTSLocomotive.CombinedControl.ThrottleAir)
270+
Locomotive.SetDynamicBrakePercentWithSound(val * 100);
271+
break;
272+
case CABViewControlTypes.ENGINE_BRAKE:
273+
Locomotive.SetEngineBrakePercent(val * 100);
274+
break;
275+
case CABViewControlTypes.FRONT_HLIGHT:
276+
// changing Headlight more than one step at a time doesn't work for some reason
277+
if (Locomotive.Headlight < val - 1)
278+
{
279+
Locomotive.Headlight++;
280+
Locomotive.SignalEvent(Event.LightSwitchToggle);
281+
}
282+
if (Locomotive.Headlight > val - 1)
283+
{
284+
Locomotive.Headlight--;
285+
Locomotive.SignalEvent(Event.LightSwitchToggle);
286+
}
287+
break;
288+
case CABViewControlTypes.ORTS_SELECTED_SPEED_SELECTOR:
289+
Locomotive.CruiseControl.SelectedSpeedMpS = val;
290+
break;
291+
// Other controls can hopefully be controlled faking mouse input
292+
// TODO: refactor HandleUserInput()
293+
default:
294+
if (_CabRenderer.ControlMap.TryGetValue(kvp.Key, out var renderer) && renderer is CabViewDiscreteRenderer discrete)
295+
{
296+
var oldChanged = discrete.ChangedValue;
297+
discrete.ChangedValue = (oldval) => val;
298+
discrete.HandleUserInput();
299+
discrete.ChangedValue = oldChanged;
300+
}
301+
break;
290302
}
291303
}
292304
}

Source/RunActivity/Viewer3D/UserInputExternal.cs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,17 @@ namespace Orts.Viewer3D
2929
/// </summary>
3030
public class ExternalDeviceState
3131
{
32-
public ExternalDeviceCabControl Direction = new ExternalDeviceCabControl(); // -100 (reverse) to 100 (forward)
33-
public ExternalDeviceCabControl Throttle = new ExternalDeviceCabControl(); // 0 to 100
34-
public ExternalDeviceCabControl DynamicBrake = new ExternalDeviceCabControl(); // 0 to 100 if active otherwise less than 0
35-
public ExternalDeviceCabControl TrainBrake = new ExternalDeviceCabControl(); // 0 (release) to 100 (CS), does not include emergency
36-
public ExternalDeviceCabControl EngineBrake = new ExternalDeviceCabControl(); // 0 to 100
37-
public ExternalDeviceCabControl Lights = new ExternalDeviceCabControl(); // lights rotary, 1 off, 2 dim, 3 full
3832
public Dictionary<(CabViewControlType,int), ExternalDeviceCabControl> CabControls;
39-
public Dictionary<UserCommand, ExternalDeviceButton> Buttons;
33+
public Dictionary<UserCommand, ExternalDeviceButton> Commands;
4034
public ExternalDeviceState()
4135
{
42-
Buttons = new Dictionary<UserCommand, ExternalDeviceButton>();
36+
Commands = new Dictionary<UserCommand, ExternalDeviceButton>();
4337
CabControls = new Dictionary<(CabViewControlType,int), ExternalDeviceCabControl>();
4438
}
4539

4640
public virtual void Handled()
4741
{
48-
Direction.Changed = false;
49-
Throttle.Changed = false;
50-
DynamicBrake.Changed = false;
51-
TrainBrake.Changed = false;
52-
EngineBrake.Changed = false;
53-
Lights.Changed = false;
54-
foreach (var button in Buttons.Values)
42+
foreach (var button in Commands.Values)
5543
{
5644
button.Changed = false;
5745
}
@@ -63,17 +51,17 @@ public virtual void Handled()
6351

6452
public bool IsPressed(UserCommand command)
6553
{
66-
return Buttons.TryGetValue(command, out var button) && button.IsPressed;
54+
return Commands.TryGetValue(command, out var button) && button.IsPressed;
6755
}
6856

6957
public bool IsReleased(UserCommand command)
7058
{
71-
return Buttons.TryGetValue(command, out var button) && button.IsReleased;
59+
return Commands.TryGetValue(command, out var button) && button.IsReleased;
7260
}
7361

7462
public bool IsDown(UserCommand command)
7563
{
76-
return Buttons.TryGetValue(command, out var button) && button.IsDown;
64+
return Commands.TryGetValue(command, out var button) && button.IsDown;
7765
}
7866
}
7967
public class ExternalDeviceButton

0 commit comments

Comments
 (0)