Skip to content

Commit 7196655

Browse files
committed
Blueprint https://blueprints.launchpad.net/or/+spec/distributed-power : Fixes, refinements and improvements after first community tests
1 parent acf84b8 commit 7196655

File tree

7 files changed

+177
-25
lines changed

7 files changed

+177
-25
lines changed

Source/Orts.Simulation/Simulation/Physics/Train.cs

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -933,12 +933,11 @@ public Train(Simulator simulator, BinaryReader inf)
933933
LeadLocomotive = Cars[LeadLocomotiveIndex];
934934
if (TrainType != TRAINTYPE.STATIC)
935935
Simulator.PlayerLocomotive = LeadLocomotive;
936+
(LeadLocomotive as MSTSLocomotive).DPThrottleController.SetValue(DPThrottlePercent / 100f);
937+
if ((LeadLocomotive as MSTSLocomotive).DPDynamicBrakeController != null)
938+
(LeadLocomotive as MSTSLocomotive).DPDynamicBrakeController.SetValue(DPDynamicBrakePercent / 100f);
936939
}
937940

938-
(LeadLocomotive as MSTSLocomotive).DPThrottleController.SetValue(DPThrottlePercent / 100f);
939-
if ((LeadLocomotive as MSTSLocomotive).DPDynamicBrakeController != null)
940-
(LeadLocomotive as MSTSLocomotive).DPDynamicBrakeController.SetValue(DPDynamicBrakePercent / 100f);
941-
942941
// restore logfile
943942
if (DatalogTrainSpeed)
944943
{
@@ -955,7 +954,7 @@ private void RestoreCars(Simulator simulator, BinaryReader inf)
955954
for (int i = 0; i < count; ++i)
956955
Cars.Add(RollingStock.Restore(simulator, inf, this));
957956
}
958-
SetDPUnitIDs();
957+
SetDPUnitIDs(true);
959958
}
960959

961960
static Traffic_Service_Definition RestoreTrafficSDefinition(BinaryReader inf)
@@ -1515,7 +1514,7 @@ public void ReverseCars()
15151514
/// <summary>
15161515
/// Set Distributed Power locomotive groups IDs, and reset async/back group assignments
15171516
/// </summary>
1518-
public void SetDPUnitIDs()
1517+
public void SetDPUnitIDs(bool keepRemoteGroups = false)
15191518
{
15201519
var id = 0;
15211520
foreach (var car in Cars)
@@ -1524,7 +1523,7 @@ public void SetDPUnitIDs()
15241523
if (car is MSTSLocomotive)
15251524
{
15261525
(car as MSTSLocomotive).DPUnitID = id;
1527-
if (car.RemoteControlGroup == 1)
1526+
if (car.RemoteControlGroup == 1 && !keepRemoteGroups)
15281527
car.RemoteControlGroup = 0;
15291528
}
15301529
else
@@ -1537,6 +1536,8 @@ public void SetDPUnitIDs()
15371536
/// </summary>
15381537
public void DPMoveToFront()
15391538
{
1539+
if (LeadLocomotive == null || (LeadLocomotive as MSTSLocomotive).DPDynamicBrakeController == null)
1540+
return;
15401541
int idToMove = -1;
15411542
for (var i = 0; i < Cars.Count; i++)
15421543
{
@@ -1559,22 +1560,40 @@ public void DPMoveToFront()
15591560
/// </summary>
15601561
public void DPMoveToBack()
15611562
{
1563+
if (LeadLocomotive == null || (LeadLocomotive as MSTSLocomotive).DPDynamicBrakeController == null)
1564+
return;
1565+
var dpDynamicBrakePercent = LeadLocomotive.DynamicBrakePercent;
1566+
var dpThrottlePercent = LeadLocomotive.ThrottlePercent;
1567+
var dpDynamicBrakeCurrentNotch = MathHelper.Clamp((LeadLocomotive as MSTSLocomotive).DPDynamicBrakeController.GetNotch(dpDynamicBrakePercent/100), 0, 8);
1568+
var dpThrottleCurrentNotch = (LeadLocomotive as MSTSLocomotive).ThrottleController.CurrentNotch;
15621569
int idToMove = -1;
15631570
int idLead = LeadLocomotive != null ? (Cars[LeadLocomotiveIndex] as MSTSLocomotive).DPUnitID : -1;
15641571
for (var i = Cars.Count - 1; i >= 0; i--)
15651572
{
15661573
if (!(Cars[i] is MSTSLocomotive))
15671574
continue;
15681575
if (idToMove == -1 && Cars[i].RemoteControlGroup == 1)
1576+
{
1577+
dpDynamicBrakePercent = DPDynamicBrakePercent;
1578+
dpThrottlePercent = DPThrottlePercent;
1579+
dpDynamicBrakeCurrentNotch = (LeadLocomotive as MSTSLocomotive).DPDynamicBrakeController.CurrentNotch;
1580+
dpThrottleCurrentNotch = (LeadLocomotive as MSTSLocomotive).DPThrottleController.CurrentNotch;
15691581
continue;
1582+
}
15701583
if (idToMove == -1 && Cars[i].RemoteControlGroup == 0)
15711584
idToMove = (Cars[i] as MSTSLocomotive).DPUnitID;
15721585

15731586
if (idToMove == idLead)
15741587
idToMove = int.MaxValue;
15751588

15761589
if ((Cars[i] as MSTSLocomotive).DPUnitID == idToMove && Cars[i].RemoteControlGroup != -1)
1590+
{
15771591
Cars[i].RemoteControlGroup = 1;
1592+
DPDynamicBrakePercent = dpDynamicBrakePercent;
1593+
DPThrottlePercent = dpThrottlePercent;
1594+
(LeadLocomotive as MSTSLocomotive).DPDynamicBrakeController.CurrentNotch = dpDynamicBrakeCurrentNotch;
1595+
(LeadLocomotive as MSTSLocomotive).DPThrottleController.CurrentNotch = dpThrottleCurrentNotch;
1596+
}
15781597
else if (idToMove > -1 && Cars[i].RemoteControlGroup == 1)
15791598
Cars[i].RemoteControlGroup = 0;
15801599
}
@@ -1585,6 +1604,8 @@ public void DPMoveToBack()
15851604
/// </summary>
15861605
public void DPTraction()
15871606
{
1607+
if (LeadLocomotive == null || (LeadLocomotive as MSTSLocomotive).DPDynamicBrakeController == null)
1608+
return;
15881609
DPMode = 1;
15891610
DPDynamicBrakePercent = -1;
15901611
if (DPThrottlePercent == 0)
@@ -1597,6 +1618,8 @@ public void DPTraction()
15971618
/// </summary>
15981619
public void DPIdle()
15991620
{
1621+
if (LeadLocomotive == null || (LeadLocomotive as MSTSLocomotive).DPDynamicBrakeController == null)
1622+
return;
16001623
DPMode = 0;
16011624
if (DPDynamicBrakePercent >= 0)
16021625
DPDynamicBrakePercent = 0;
@@ -1611,12 +1634,16 @@ public void DPIdle()
16111634
/// </summary>
16121635
public void DPDynamicBrake()
16131636
{
1614-
DPMode = -1;
1637+
if (LeadLocomotive == null || (LeadLocomotive as MSTSLocomotive).DPDynamicBrakeController == null)
1638+
return;
16151639
DPThrottlePercent = 0;
16161640
if (DPDynamicBrakePercent == -1)
16171641
DPDynamicBrakePercent = 0;
1618-
if (DPDynamicBrakePercent == 0)
1642+
if (DPDynamicBrakePercent == 0 && DPMode != -1)
1643+
{
1644+
DPMode = -1;
16191645
DPMore();
1646+
}
16201647
DistributedPowerUpdate();
16211648
}
16221649

@@ -1630,6 +1657,8 @@ public void DPDynamicBrake()
16301657
/// </summary>
16311658
public void DPMore()
16321659
{
1660+
if (LeadLocomotive == null || (LeadLocomotive as MSTSLocomotive).DPDynamicBrakeController == null)
1661+
return;
16331662
if (DPMode == 1)
16341663
DPMore((LeadLocomotive as MSTSLocomotive).DPThrottleController, ref DPThrottlePercent);
16351664
else if (DPMode == -1)
@@ -1656,6 +1685,8 @@ protected void DPMore(RollingStocks.SubSystems.Controllers.MSTSNotchController c
16561685
/// </summary>
16571686
public void DPLess()
16581687
{
1688+
if (LeadLocomotive == null || (LeadLocomotive as MSTSLocomotive).DPDynamicBrakeController == null)
1689+
return;
16591690
if (DPMode == 1)
16601691
DPLess((LeadLocomotive as MSTSLocomotive).DPThrottleController, ref DPThrottlePercent);
16611692
else if (DPMode == -1)
@@ -1677,7 +1708,7 @@ protected void DPLess(RollingStocks.SubSystems.Controllers.MSTSNotchController c
16771708
if (percent <= 0)
16781709
{
16791710
percent = 0;
1680-
DPMore(controller, ref percent);
1711+
// DPMore(controller, ref percent);
16811712
}
16821713
}
16831714

@@ -1686,8 +1717,7 @@ protected void DPLess(RollingStocks.SubSystems.Controllers.MSTSNotchController c
16861717
/// </summary>
16871718
protected void DistributedPowerUpdate()
16881719
{
1689-
if (LeadLocomotive.Direction == Direction.N
1690-
|| LeadLocomotive.BrakeSystem.GetCylPressurePSI() > 20)
1720+
if (LeadLocomotive != null && LeadLocomotive.Direction == Direction.N)
16911721
DPIdle();
16921722
}
16931723

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -895,10 +895,18 @@ public string GetDPDebugStatus()
895895
}
896896
else if (DynamicBrakePercent > 0 && DynamicBrake)
897897
{
898-
if (DynamicBrakeController.NotchCount() > 3)
899-
throttle = Simulator.Catalog.GetParticularString("Notch", "B") + DynamicBrakeController.GetNearestNotch(DynamicBrakePercent / 100f);
898+
if (RemoteControlGroup == 1)
899+
{
900+
throttle = Simulator.Catalog.GetParticularString("Notch", "B") + MathHelper.Clamp((Train.LeadLocomotive as MSTSLocomotive).DPDynamicBrakeController.CurrentNotch, 1, 8);
901+
}
900902
else
901-
throttle = string.Format("{0:F0}%", DynamicBrakePercent);
903+
{
904+
// The clause here below leads to possible differences of one notch near the notch value, and therefore is commented
905+
// if (DynamicBrakeController.NotchCount() > 3)
906+
// throttle = Simulator.Catalog.GetParticularString("Notch", "B") + MathHelper.Clamp((DynamicBrakeController.GetNearestNotch(DynamicBrakePercent / 100f)), 1, 8);
907+
// else
908+
throttle = Simulator.Catalog.GetParticularString("Notch", "B") + MathHelper.Clamp((Train.LeadLocomotive as MSTSLocomotive).DPDynamicBrakeController.GetNotch(DynamicBrakePercent / 100f), 1, 8);
909+
}
902910
}
903911
else if (DynamicBrakePercent == 0 && !DynamicBrake)
904912
throttle = Simulator.Catalog.GetString("Setup");
@@ -915,7 +923,7 @@ public string GetDPDebugStatus()
915923
status.AppendFormat("{0}\t", throttle);
916924
status.AppendFormat("{0}\t", FormatStrings.FormatFuelVolume(DieselLevelL, IsMetric, IsUK));
917925
status.AppendFormat("{0}{1}", FormatStrings.FormatForce(MotiveForceN, IsMetric), CouplerOverloaded ? "???" : "");
918-
status.Append(DieselEngines.GetStatus());
926+
status.Append(DieselEngines.GetDPStatus());
919927

920928
return status.ToString();
921929
}

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

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,10 @@ protected void CheckCoherence()
557557
{
558558
HasSmoothStruc = true;
559559
}
560-
DPDynamicBrakeController = (MSTSNotchController)DynamicBrakeController.Clone();
560+
if (DynamicBrakeController.NotchCount() > 3)
561+
DPDynamicBrakeController = (MSTSNotchController)DynamicBrakeController.Clone();
562+
else
563+
DPDynamicBrakeController = BuildDPDynamicBrakeController();
561564
}
562565
else
563566
{
@@ -583,6 +586,69 @@ protected void CheckCoherence()
583586
DynamicBrakeForceCurves[1] = interp;
584587
}
585588
}
589+
590+
protected MSTSNotchController BuildDPDynamicBrakeController()
591+
{
592+
var dpDynController = new MSTSNotchController();
593+
CabView cabView = null;
594+
CVCMultiStateDisplay msDisplay = null;
595+
if (CabView3D != null)
596+
cabView = CabView3D;
597+
else if (CabViewList.Count > 0)
598+
{
599+
if (CabViewList[0].CabViewType == CabViewType.Front)
600+
cabView = CabViewList[0];
601+
else
602+
cabView = CabViewList[1];
603+
}
604+
if (cabView != null)
605+
{
606+
try
607+
{
608+
msDisplay = (CVCMultiStateDisplay) cabView.CVFFile.CabViewControls.Where(
609+
control => control is CVCMultiStateDisplay &&
610+
(((CVCMultiStateDisplay) control).ControlType == CABViewControlTypes.DYNAMIC_BRAKE_DISPLAY ||
611+
((CVCMultiStateDisplay) control).ControlType == CABViewControlTypes.CPH_DISPLAY)).First();
612+
}
613+
catch
614+
{
615+
616+
}
617+
if (msDisplay != null)
618+
{
619+
if (msDisplay.ControlType == CABViewControlTypes.DYNAMIC_BRAKE_DISPLAY)
620+
{
621+
foreach (var switchval in msDisplay.Values)
622+
dpDynController.AddNotch((float) switchval);
623+
}
624+
else
625+
{
626+
foreach (var switchval in msDisplay.Values)
627+
{
628+
if (switchval<CombinedControlSplitPosition)
629+
continue;
630+
dpDynController.AddNotch((float)(switchval - CombinedControlSplitPosition) / (1 - CombinedControlSplitPosition));
631+
}
632+
}
633+
}
634+
}
635+
if (cabView == null || msDisplay == null)
636+
// Use default Dash9 arrangement if no display is found
637+
{
638+
var switchval = 0f;
639+
while (switchval <= 1)
640+
{
641+
if (switchval == 0.99f)
642+
switchval = 1;
643+
dpDynController.AddNotch(switchval);
644+
switchval += 0.11f;
645+
}
646+
}
647+
648+
649+
return dpDynController;
650+
}
651+
586652

587653
protected void GetPressureUnit()
588654
{
@@ -1074,7 +1140,15 @@ public override void Copy(MSTSWagon copy)
10741140
BrakemanBrakeController = locoCopy.BrakemanBrakeController != null ? locoCopy.BrakemanBrakeController.Clone(this) : null;
10751141
DynamicBrakeController = locoCopy.DynamicBrakeController != null ? (MSTSNotchController)locoCopy.DynamicBrakeController.Clone() : null;
10761142
DPThrottleController = (MSTSNotchController)ThrottleController.Clone();
1077-
DPDynamicBrakeController = DynamicBrakeController != null ? (MSTSNotchController)DynamicBrakeController.Clone() : null;
1143+
if (DynamicBrakeController != null)
1144+
{
1145+
if (DynamicBrakeController.NotchCount() > 3)
1146+
DPDynamicBrakeController = (MSTSNotchController)DynamicBrakeController.Clone();
1147+
else
1148+
DPDynamicBrakeController = BuildDPDynamicBrakeController();
1149+
}
1150+
else
1151+
DPDynamicBrakeController = null;
10781152

10791153
LocomotivePowerSupply.Copy(locoCopy.LocomotivePowerSupply);
10801154
TrainControlSystem.Copy(locoCopy.TrainControlSystem);
@@ -1145,6 +1219,7 @@ public override void Save(BinaryWriter outf)
11451219
outf.Write(CurrentTrackSandBoxCapacityM3);
11461220
outf.Write(SaveAdhesionFilter);
11471221
outf.Write(RemoteControlGroup);
1222+
outf.Write(DPUnitID);
11481223

11491224
base.Save(outf);
11501225

@@ -1194,6 +1269,7 @@ public override void Restore(BinaryReader inf)
11941269

11951270
AdhesionFilter.Reset(SaveAdhesionFilter);
11961271
RemoteControlGroup = inf.ReadInt32();
1272+
DPUnitID = inf.ReadInt32();
11971273

11981274
base.Restore(inf);
11991275

@@ -2072,7 +2148,7 @@ protected virtual void UpdateControllers(float elapsedClockSeconds)
20722148
ConfirmWheelslip(elapsedClockSeconds);
20732149
LocalThrottlePercent = (ThrottleIntervention < 0 ? ThrottleController.CurrentValue : ThrottleIntervention) * 100.0f;
20742150
DPThrottleController.Update(elapsedClockSeconds);
2075-
DPDynamicBrakeController.Update(elapsedClockSeconds);
2151+
if (DPDynamicBrakeController != null) DPDynamicBrakeController.Update(elapsedClockSeconds);
20762152
}
20772153
else
20782154
{
@@ -3997,7 +4073,7 @@ public override string GetDPDynamicBrakeStatus()
39974073
{
39984074
if (DynamicBrakeController == null)
39994075
return null;
4000-
var dpStatus = Train.DPMode == -1 ? string.Format("({0:F0}%)", Train.DPDynamicBrakePercent) : string.Empty;
4076+
var dpStatus = this is MSTSDieselLocomotive && Train.DPMode == -1 ? string.Format("({0:F0}%)", Train.DPDynamicBrakePercent) : string.Empty;
40014077
if (DynamicBrakePercent < 0)
40024078
return dpStatus;
40034079
if (TrainControlSystem.FullDynamicBrakingOrder)

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/Controllers/MSTSNotchController.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ private float GetNotchBoost(float boost)
245245
IntermediateValue - CurrentValue > StepSize) ? FastBoost : boost);
246246
}
247247

248+
public void AddNotch(float value)
249+
{
250+
Notches.Add(new MSTSNotch(value, false, (int)ControllerState.Dummy));
251+
}
252+
248253
/// <summary>
249254
/// Sets the actual value of the controller, and adjusts the actual notch to match.
250255
/// </summary>
@@ -612,5 +617,22 @@ public int GetNearestNotch(float value)
612617
return notch;
613618
}
614619

620+
/// <summary>
621+
/// Get the discrete notch position for a normalized input value.
622+
/// This function is not dependent on notch controller actual (current) value, so can be queried for computer-intervened value as well.
623+
/// </summary>
624+
public int GetNotch(float value)
625+
{
626+
var notch = 0;
627+
for (notch = Notches.Count - 1; notch > 0; notch--)
628+
{
629+
if (Notches[notch].Value <= value)
630+
{
631+
break;
632+
}
633+
}
634+
return notch;
635+
}
636+
615637
}
616638
}

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerSupplies/DieselEngine.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,21 @@ public string GetStatus()
426426
return result.ToString();
427427
}
428428

429+
public string GetDPStatus()
430+
{
431+
var result = new StringBuilder();
432+
var eng = DEList[0];
433+
result.AppendFormat("\t{0}", Simulator.Catalog.GetParticularString("Engine", GetStringAttribute.GetPrettyName(eng.State)));
434+
result.AppendFormat("\t{0}", FormatStrings.FormatPower(eng.CurrentDieselOutputPowerW, Locomotive.IsMetric, false, false));
435+
result.AppendFormat("\t{0:F1}%", eng.LoadPercent);
436+
result.AppendFormat("\t{0:F0} {1}", eng.RealRPM, FormatStrings.rpm);
437+
result.AppendFormat("\t{0}/{1}", FormatStrings.FormatFuelVolume(pS.TopH(eng.DieselFlowLps), Locomotive.IsMetric, Locomotive.IsUK), FormatStrings.h);
438+
result.AppendFormat("\t{0}", FormatStrings.FormatTemperature(eng.DieselTemperatureDeg, Locomotive.IsMetric, false));
439+
result.AppendFormat("\t{0}", FormatStrings.FormatPressure(eng.DieselOilPressurePSI, PressureUnit.PSI, Locomotive.MainPressureUnit, true));
440+
441+
return result.ToString();
442+
}
443+
429444
public int NumOfActiveEngines
430445
{
431446
get

0 commit comments

Comments
 (0)