Skip to content

Commit 2a36208

Browse files
committed
Refactoring and comments
1 parent 154f883 commit 2a36208

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

Source/Orts.Simulation/Simulation/RollingStocks/MSTSDieselLocomotive.cs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,9 @@ public override float GetAvailableTractionForceN(float t)
639639
if (t <= 0) return 0;
640640
if (DieselEngines.HasGearBox && (TractiveForceCurves == null || DieselTransmissionType == MSTSDieselLocomotive.DieselTransmissionTypes.Mechanic))
641641
{
642-
return base.GetAvailableTractionForceN(t); // TODO: provide more accurate values
642+
// TODO: provide more accurate values
643+
// Note: if this clause is fulfilled, this method is not used to calculate TractiveForceN, it just provides information for other subsystems about theoretical tractive force
644+
return base.GetAvailableTractionForceN(t);
643645
}
644646
float forceN = 0;
645647
float powerW = float.MaxValue;
@@ -658,6 +660,9 @@ public override float GetAvailableTractionForceN(float t)
658660
{
659661
powerW = DieselPowerSupply.MaximumPowerW * t;
660662
}
663+
// This section calculates the traction force of the locomotive as follows:
664+
// Basic configuration (no TF table) - uses P = F /speed relationship - requires power and force parameters to be set in the ENG file.
665+
// Advanced configuration (TF table) - use a user defined tractive force table
661666
if (TractiveForceCurves == null)
662667
{
663668
// This sets the maximum force of the locomotive, it will be adjusted down if it exceeds the max power of the locomotive.
@@ -692,32 +697,7 @@ protected override void UpdateTractionForce(float elapsedClockSeconds)
692697
TractiveForceN = TractionForceN = DieselEngines.TractiveForceN;
693698
return;
694699
}
695-
float t = ThrottlePercent / 100;
696-
697-
float maxthrottle = MaxThrottlePercent / 100;
698-
if (IsPlayerTrain) maxthrottle = Math.Min(maxthrottle, DieselEngines.ApparentThrottleSetting / 100.0f);
699-
if (t > maxthrottle) t = maxthrottle;
700-
t = MathHelper.Clamp(t, 0, 1);
701-
702-
// This section calculates the traction force of the locomotive as follows:
703-
// Basic configuration (no TF table) - uses P = F /speed relationship - requires power and force parameters to be set in the ENG file.
704-
// Advanced configuration (TF table) - use a user defined tractive force table
705-
if (maxthrottle > 0 && Direction != Direction.N && LocomotivePowerSupply.MainPowerSupplyOn)
706-
{
707-
float targetForceN = GetAvailableTractionForceN(t);
708-
float limitForceN = GetAvailableTractionForceN(maxthrottle);
709-
float maxForceN = float.MaxValue;
710-
if (limitForceN >= targetForceN)
711-
maxForceN = limitForceN;
712-
float maxPowerW = DieselPowerSupply.AvailableTractionPowerW;
713-
if (targetForceN * AbsTractionSpeedMpS > maxPowerW) maxForceN = maxPowerW / AbsTractionSpeedMpS;
714-
UpdateForceWithRamp(ref TractionForceN, elapsedClockSeconds, targetForceN, maxForceN, TractionForceRampUpNpS, TractionForceRampDownNpS, TractionForceRampDownToZeroNpS, TractionPowerRampUpWpS, TractionPowerRampDownWpS, TractionPowerRampDownToZeroWpS);
715-
}
716-
else
717-
{
718-
TractionForceN = 0f;
719-
}
720-
TractiveForceN = TractionForceN;
700+
base.UpdateTractionForce(elapsedClockSeconds);
721701
}
722702

723703
protected override void UpdateAxleDriveForce()

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2558,13 +2558,25 @@ public void UpdateForceWithRamp(ref float forceN, float elapsedClockSeconds, flo
25582558
forceN = Math.Max(targetForceN, forceN - (targetForceN == 0 ? rampZeroNpS : rampDownNpS) * elapsedClockSeconds);
25592559
}
25602560
}
2561+
/// <summary>
2562+
/// Provides the theoretical tractive force that can be achieved at current speed
2563+
/// Takes into account number of available engines and power limitation effects from the power supply.
2564+
/// This method does not take into account transient effects that temporary limit tractive force
2565+
/// (e.g. diesel engine still revving up)
2566+
/// </summary>
2567+
/// <param name="t">Throttle position (0-1)</param>
2568+
/// <returns></returns>
25612569
public virtual float GetAvailableTractionForceN(float t)
25622570
{
25632571
if (t <= 0) return 0;
25642572
float powerW = float.MaxValue;
25652573
float forceN;
2566-
if (this is MSTSElectricLocomotive electric && electric.ElectricPowerSupply.MaximumPowerW > 0)
2567-
powerW = electric.ElectricPowerSupply.MaximumPowerW * t * (1-PowerReduction);
2574+
// Take into account power limitation
2575+
if (LocomotivePowerSupply is ScriptedLocomotivePowerSupply supply && supply.MaximumPowerW > 0)
2576+
powerW = supply.MaximumPowerW * t * (1-PowerReduction);
2577+
// This section calculates the traction force of the locomotive as follows:
2578+
// Basic configuration (no TF table) - uses P = F /speed relationship - requires power and force parameters to be set in the ENG file.
2579+
// Advanced configuration (TF table) - use a user defined tractive force table
25682580
if (TractiveForceCurves == null)
25692581
{
25702582
powerW = Math.Min(powerW, MaxPowerW * t * t * (1 - PowerReduction));
@@ -2596,20 +2608,28 @@ protected virtual void UpdateTractionForce(float elapsedClockSeconds)
25962608
{
25972609
float t = ThrottlePercent / 100;
25982610

2611+
// Ensure that throttle never exceeds the limits imposed by other subsystems
25992612
float maxthrottle = MaxThrottlePercent / 100;
2613+
// For diesel locomotives, also take into account the throttle setting associated to the current engine RPM
2614+
if (IsPlayerTrain && this is MSTSDieselLocomotive diesel) maxthrottle = Math.Min(maxthrottle, diesel.DieselEngines.ApparentThrottleSetting / 100.0f);
26002615
if (t > maxthrottle) t = maxthrottle;
26012616
t = MathHelper.Clamp(t, 0, 1);
26022617

26032618
if (maxthrottle > 0 && Direction != Direction.N && LocomotivePowerSupply.MainPowerSupplyOn)
26042619
{
2620+
// Set the target force to the theoretically available tractive force for current throttle setting
2621+
// If there is a limit on the amount of throttle, ensure that tractive force never exceeds the limit
2622+
// imposed by that throttle setting
26052623
float targetForceN = GetAvailableTractionForceN(t);
26062624
float limitForceN = GetAvailableTractionForceN(maxthrottle);
26072625
float maxForceN = float.MaxValue;
2626+
// Ignore force limit for exotic curves where higher throttle settings provide less tractive force
26082627
if (limitForceN >= targetForceN)
26092628
maxForceN = limitForceN;
2610-
if (this is MSTSElectricLocomotive electric)
2629+
// Ensure that power consumption never exceeds the power that is available for traction at the moment
2630+
if (LocomotivePowerSupply is ScriptedLocomotivePowerSupply supply)
26112631
{
2612-
float maxPowerW = electric.ElectricPowerSupply.AvailableTractionPowerW;
2632+
float maxPowerW = supply.AvailableTractionPowerW;
26132633
if (targetForceN * AbsTractionSpeedMpS > maxPowerW) maxForceN = maxPowerW / AbsTractionSpeedMpS;
26142634
}
26152635
UpdateForceWithRamp(ref TractionForceN, elapsedClockSeconds, targetForceN, maxForceN, TractionForceRampUpNpS, TractionForceRampDownNpS, TractionForceRampDownToZeroNpS, TractionPowerRampUpWpS, TractionPowerRampDownWpS, TractionPowerRampDownToZeroWpS);

0 commit comments

Comments
 (0)