Skip to content

Commit 614b222

Browse files
committed
Add MaxThrottlePercent to TCS scripts (with some refactoring in TrainCar)
1 parent a81711c commit 614b222

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
lines changed

Source/Orts.Simulation/Common/Scripting/TrainControlSystem.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ public abstract class TrainControlSystem : AbstractTrainScriptClass
225225
/// </summary>
226226
public Func<float> ThrottlePercent;
227227
/// <summary>
228+
/// Returns maximum throttle percent
229+
/// </summary>
230+
public Func<float> MaxThrottlePercent;
231+
/// <summary>
228232
/// Returns dynamic brake percent
229233
/// </summary>
230234
public Func<float> DynamicBrakePercent;
@@ -371,6 +375,11 @@ public abstract class TrainControlSystem : AbstractTrainScriptClass
371375
/// </summary>
372376
public Action<bool> SetTractionAuthorization;
373377
/// <summary>
378+
/// Set the maximum throttle percent
379+
/// Range: 0 to 100
380+
/// </summary>
381+
public Action<float> SetMaxThrottlePercent;
382+
/// <summary>
374383
/// Switch vigilance alarm sound on (true) or off (false).
375384
/// </summary>
376385
public Action<bool> SetVigilanceAlarm;

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ protected set
152152
public bool CircuitBreakerClosingOrder { get; private set; }
153153
public bool CircuitBreakerOpeningOrder { get; private set; }
154154
public bool TractionAuthorization { get; private set; }
155+
public float MaxThrottlePercent { get; private set; } = 100f;
155156
public bool FullDynamicBrakingOrder { get; private set; }
156157

157158
public float[] CabDisplayControls = new float[TCSCabviewControlCount];
@@ -341,7 +342,7 @@ public void Initialize()
341342
Script.PantographCount = () => Locomotive.Pantographs.Count;
342343
Script.GetPantographState = (pantoID) =>
343344
{
344-
if (pantoID >= Pantographs.MinPantoID && pantoID <= Pantographs.MaxPantoID)
345+
if (pantoID >= Pantographs.MinPantoID && pantoID <= Pantographs.MaxPantoID)
345346
{
346347
return Locomotive.Pantographs[pantoID].State;
347348
}
@@ -353,6 +354,7 @@ public void Initialize()
353354
};
354355
Script.ArePantographsDown = () => Locomotive.Pantographs.State == PantographState.Down;
355356
Script.ThrottlePercent = () => Locomotive.ThrottleController.CurrentValue * 100;
357+
Script.MaxThrottlePercent = () => MaxThrottlePercent;
356358
Script.DynamicBrakePercent = () => Locomotive.DynamicBrakeController == null ? 0 : Locomotive.DynamicBrakeController.CurrentValue * 100;
357359
Script.TractionAuthorization = () => TractionAuthorization;
358360
Script.BrakePipePressureBar = () => Locomotive.BrakeSystem != null ? Bar.FromPSI(Locomotive.BrakeSystem.BrakeLine1PressurePSI) : float.MaxValue;
@@ -450,6 +452,13 @@ public void Initialize()
450452
Script.SetCircuitBreakerClosingOrder = (value) => CircuitBreakerClosingOrder = value;
451453
Script.SetCircuitBreakerOpeningOrder = (value) => CircuitBreakerOpeningOrder = value;
452454
Script.SetTractionAuthorization = (value) => TractionAuthorization = value;
455+
Script.SetMaxThrottlePercent = (value) =>
456+
{
457+
if (value >= 0 && value <= 100f)
458+
{
459+
MaxThrottlePercent = value;
460+
}
461+
};
453462
Script.SetVigilanceAlarm = (value) => Locomotive.SignalEvent(value ? Event.VigilanceAlarmOn : Event.VigilanceAlarmOff);
454463
Script.SetHorn = (value) => Locomotive.TCSHorn = value;
455464
Script.TriggerSoundAlert1 = () => this.SignalEvent(Event.TrainControlSystemAlert1, Script);

Source/Orts.Simulation/Simulation/RollingStocks/TrainCar.cs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -467,19 +467,29 @@ public float ThrottlePercent
467467
{
468468
if (RemoteControlGroup == 0 && Train != null)
469469
{
470-
if (Train.LeadLocomotive != null && !((MSTSLocomotive)Train.LeadLocomotive).TrainControlSystem.TractionAuthorization && Train.MUThrottlePercent > 0)
470+
if (Train.LeadLocomotive is MSTSLocomotive locomotive)
471471
{
472-
return 0;
473-
}
474-
else
475-
{
476-
return Train.MUThrottlePercent;
472+
if (!locomotive.TrainControlSystem.TractionAuthorization
473+
|| Train.MUThrottlePercent <= 0)
474+
{
475+
return 0;
476+
}
477+
else if (Train.MUThrottlePercent > locomotive.TrainControlSystem.MaxThrottlePercent)
478+
{
479+
return Math.Max(locomotive.TrainControlSystem.MaxThrottlePercent, 0);
480+
}
477481
}
482+
483+
return Train.MUThrottlePercent;
478484
}
479485
else if (RemoteControlGroup == 1 && Train != null)
486+
{
480487
return Train.DPThrottlePercent;
488+
}
481489
else
490+
{
482491
return LocalThrottlePercent;
492+
}
483493
}
484494
set
485495
{
@@ -514,21 +524,26 @@ public float DynamicBrakePercent
514524
{
515525
get
516526
{
517-
if (RemoteControlGroup >= 0 && Train != null)
527+
if (RemoteControlGroup == 0 && Train != null)
518528
{
519-
if (Train.LeadLocomotive != null && ((MSTSLocomotive) Train.LeadLocomotive).TrainControlSystem.FullDynamicBrakingOrder)
529+
if (Train.LeadLocomotive is MSTSLocomotive locomotive)
520530
{
521-
return 100;
522-
}
523-
else if (RemoteControlGroup == 1 && Train != null)
524-
return Train.DPDynamicBrakePercent;
525-
else
526-
{
527-
return Train.MUDynamicBrakePercent;
531+
if (locomotive.TrainControlSystem.FullDynamicBrakingOrder)
532+
{
533+
return 100;
534+
}
528535
}
529-
}
536+
537+
return Train.MUDynamicBrakePercent;
538+
}
539+
else if (RemoteControlGroup == 1 && Train != null)
540+
{
541+
return Train.DPDynamicBrakePercent;
542+
}
530543
else
544+
{
531545
return LocalDynamicBrakePercent;
546+
}
532547
}
533548
set
534549
{

0 commit comments

Comments
 (0)