Skip to content

Commit 902506c

Browse files
committed
Initial support for power supplies on control cars
1 parent f88b8b9 commit 902506c

File tree

5 files changed

+179
-161
lines changed

5 files changed

+179
-161
lines changed

Source/Orts.Simulation/Common/Scripting/PowerSupply/ControlCarPowerSupply.cs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
1717

1818
using System;
19+
using Orts.Simulation.RollingStocks;
1920

2021
namespace ORTS.Scripting.Api
2122
{
@@ -24,12 +25,37 @@ namespace ORTS.Scripting.Api
2425
/// </summary>
2526
public abstract class ControlCarPowerSupply : LocomotivePowerSupply
2627
{
27-
28-
29-
30-
31-
32-
28+
MSTSLocomotive ControlActiveLocomotive => Locomotive.ControlActiveLocomotive;
29+
/// <summary>
30+
/// Index of the control active locomotive in the train (taking into account only locomotives)
31+
/// </summary>
32+
public int IndexOfControlActiveLocomotive
33+
{
34+
get
35+
{
36+
int count=0;
37+
for (int i=0; i<Train.Cars.Count; i++)
38+
{
39+
if (Train.Cars[i] is MSTSLocomotive)
40+
{
41+
if (Train.Cars[i] == ControlActiveLocomotive) return count;
42+
count++;
43+
}
44+
}
45+
return -1;
46+
}
47+
}
48+
protected override void SetCurrentMainPowerSupplyState(PowerSupplyState state) {}
49+
protected override void SetCurrentAuxiliaryPowerSupplyState(PowerSupplyState state) {}
50+
protected override void SetCurrentElectricTrainSupplyState(PowerSupplyState state) {}
51+
protected override void SetCurrentDynamicBrakeAvailability(bool avail) {}
52+
public void SignalEventToControlActiveLocomotive(PowerSupplyEvent evt)
53+
{
54+
ControlActiveLocomotive.LocomotivePowerSupply.HandleEvent(evt);
55+
}
56+
public void SignalEventToControlActiveLocomotive(PowerSupplyEvent evt, int id)
57+
{
58+
ControlActiveLocomotive.LocomotivePowerSupply.HandleEvent(evt, id);
59+
}
3360
}
34-
3561
}

Source/Orts.Simulation/Common/Scripting/PowerSupply/LocomotivePowerSupply.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,13 @@ protected void SetCustomizedCabviewControlName(int index, string name)
270270
/// Sets the current state of the main power supply (power from the pantograph or the generator)
271271
/// Main power comes from the pantograph or the diesel generator
272272
/// </summary>
273-
protected void SetCurrentMainPowerSupplyState(PowerSupplyState state) => LpsHost.MainPowerSupplyState = state;
273+
protected virtual void SetCurrentMainPowerSupplyState(PowerSupplyState state) => LpsHost.MainPowerSupplyState = state;
274274

275275
/// <summary>
276276
/// Sets the current state of the auxiliary power supply
277277
/// Auxiliary power is used by auxiliary systems of a locomotive (such as ventilation or air compressor) and by systems of the cars (such as air conditionning)
278278
/// </summary>
279-
protected void SetCurrentAuxiliaryPowerSupplyState(PowerSupplyState state) => LpsHost.AuxiliaryPowerSupplyState = state;
279+
protected virtual void SetCurrentAuxiliaryPowerSupplyState(PowerSupplyState state) => LpsHost.AuxiliaryPowerSupplyState = state;
280280

281281
/// <summary>
282282
/// Sets the current state of the cab power supply
@@ -287,11 +287,11 @@ protected void SetCustomizedCabviewControlName(int index, string name)
287287
/// Sets the current state of the electric train supply
288288
/// ETS is used by the systems of the cars (such as air conditionning)
289289
/// </summary>
290-
protected void SetCurrentElectricTrainSupplyState(PowerSupplyState state) => LpsHost.ElectricTrainSupplyState = state;
290+
protected virtual void SetCurrentElectricTrainSupplyState(PowerSupplyState state) => LpsHost.ElectricTrainSupplyState = state;
291291
/// <summary>
292292
/// Sets the current availability of the dynamic brake
293293
/// </summary>
294-
protected void SetCurrentDynamicBrakeAvailability(bool avail) => LpsHost.DynamicBrakeAvailable = avail;
294+
protected virtual void SetCurrentDynamicBrakeAvailability(bool avail) => LpsHost.DynamicBrakeAvailable = avail;
295295

296296
/// <summary>
297297
/// Sends an event to the master switch

Source/Orts.Simulation/Simulation/RollingStocks/MSTSControlTrailerCar.cs

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
using System.Diagnostics;
3232
using System.IO;
3333
using System.Text;
34+
using Orts.Formats.Msts;
3435
using Orts.Parsers.Msts;
3536
using Orts.Simulation.Physics;
3637
using Orts.Simulation.RollingStocks.SubSystems.Controllers;
@@ -49,17 +50,14 @@ public class MSTSControlTrailerCar : MSTSLocomotive
4950
int ControlGearIndication;
5051
TypesGearBox ControlGearBoxType;
5152

53+
private bool controlTrailerBrakeSystemSet = false;
54+
5255
public MSTSControlTrailerCar(Simulator simulator, string wagFile)
5356
: base(simulator, wagFile)
5457
{
5558
PowerSupply = new ScriptedControlCarPowerSupply(this);
5659
}
5760

58-
public override void LoadFromWagFile(string wagFilePath)
59-
{
60-
base.LoadFromWagFile(wagFilePath);
61-
}
62-
6361
public override void Initialize()
6462
{
6563
// Initialise gearbox controller
@@ -83,18 +81,12 @@ public override void Parse(string lowercasetoken, STFReader stf)
8381
{
8482
switch (lowercasetoken)
8583
{
86-
case "engine(ortspowerondelay":
87-
case "engine(ortsauxpowerondelay":
8884
case "engine(ortspowersupply":
8985
case "engine(ortspowersupplyparameters":
90-
case "engine(ortstractioncutoffrelay":
91-
case "engine(ortstractioncutoffrelayclosingdelay":
9286
case "engine(ortsbattery":
9387
case "engine(ortsmasterkey(mode":
9488
case "engine(ortsmasterkey(delayoff":
9589
case "engine(ortsmasterkey(headlightcontrol":
96-
case "engine(ortselectrictrainsupply(mode":
97-
case "engine(ortselectrictrainsupply(dieselengineminrpm":
9890
LocomotivePowerSupply.Parse(lowercasetoken, stf);
9991
break;
10092

@@ -161,6 +153,31 @@ public override void InitializeMoving()
161153
/// </summary>
162154
public override void Update(float elapsedClockSeconds)
163155
{
156+
FindControlActiveLocomotive();
157+
// A control car typically doesn't have its own compressor and relies on the attached power car. However OR uses the lead locomotive as the reference car for compressor calculations.
158+
// Hence whilst users are encouraged to leave these parameters out of the ENG file, they need to be setup for OR to work correctly.
159+
// Some parameters need to be split across the unpowered and powered car for correct timing and volume calculations.
160+
// This setup loop is only processed the first time that update is run.
161+
if (!controlTrailerBrakeSystemSet)
162+
{
163+
if (ControlActiveLocomotive != null)
164+
{
165+
// Split reservoir volume across the power car and the active locomotive
166+
MainResVolumeM3 = ControlActiveLocomotive.MainResVolumeM3 / 2;
167+
ControlActiveLocomotive.MainResVolumeM3 = MainResVolumeM3;
168+
169+
MaxMainResPressurePSI = ControlActiveLocomotive.MaxMainResPressurePSI;
170+
MainResPressurePSI = MaxMainResPressurePSI;
171+
ControlActiveLocomotive.MainResPressurePSI = MainResPressurePSI;
172+
controlTrailerBrakeSystemSet = true; // Ensure this loop is only processes the first time update routine run
173+
MaximumMainReservoirPipePressurePSI = ControlActiveLocomotive.MaximumMainReservoirPipePressurePSI;
174+
CompressorRestartPressurePSI = ControlActiveLocomotive.CompressorRestartPressurePSI;
175+
MainResChargingRatePSIpS = ControlActiveLocomotive.MainResChargingRatePSIpS;
176+
BrakePipeChargingRatePSIorInHgpS = ControlActiveLocomotive.BrakePipeChargingRatePSIorInHgpS;
177+
TrainBrakePipeLeakPSIorInHgpS = ControlActiveLocomotive.TrainBrakePipeLeakPSIorInHgpS;
178+
}
179+
}
180+
164181
base.Update(elapsedClockSeconds);
165182
WheelSpeedMpS = SpeedMpS; // Set wheel speed for control car, required to make wheels go around.
166183

@@ -296,5 +313,38 @@ public override void ChangeGearDown()
296313
ControlGearUp = false;
297314
ControlGearDown = true;
298315
}
316+
public override float GetDataOf(CabViewControl cvc)
317+
{
318+
float data;
319+
switch (cvc.ControlType.Type)
320+
{
321+
case CABViewControlTypes.AMMETER:
322+
case CABViewControlTypes.AMMETER_ABS:
323+
case CABViewControlTypes.DYNAMIC_BRAKE_FORCE:
324+
case CABViewControlTypes.LOAD_METER:
325+
case CABViewControlTypes.ORTS_DIESEL_TEMPERATURE:
326+
case CABViewControlTypes.ORTS_OIL_PRESSURE:
327+
case CABViewControlTypes.ORTS_SIGNED_TRACTION_BRAKING:
328+
case CABViewControlTypes.ORTS_SIGNED_TRACTION_TOTAL_BRAKING:
329+
case CABViewControlTypes.ORTS_TRACTION_CUT_OFF_RELAY_AUTHORIZED:
330+
case CABViewControlTypes.ORTS_TRACTION_CUT_OFF_RELAY_CLOSED:
331+
case CABViewControlTypes.ORTS_TRACTION_CUT_OFF_RELAY_DRIVER_CLOSING_AUTHORIZATION:
332+
case CABViewControlTypes.ORTS_TRACTION_CUT_OFF_RELAY_DRIVER_CLOSING_ORDER:
333+
case CABViewControlTypes.ORTS_TRACTION_CUT_OFF_RELAY_DRIVER_OPENING_ORDER:
334+
case CABViewControlTypes.ORTS_TRACTION_CUT_OFF_RELAY_OPEN:
335+
case CABViewControlTypes.ORTS_TRACTION_CUT_OFF_RELAY_OPEN_AND_AUTHORIZED:
336+
case CABViewControlTypes.ORTS_TRACTION_CUT_OFF_RELAY_STATE:
337+
case CABViewControlTypes.RPM:
338+
case CABViewControlTypes.RPM_2:
339+
case CABViewControlTypes.TRACTION_BRAKING:
340+
case CABViewControlTypes.WHEELSLIP:
341+
data = ControlActiveLocomotive?.GetDataOf(cvc) ?? 0;
342+
break;
343+
default:
344+
data = base.GetDataOf(cvc);
345+
break;
346+
}
347+
return data;
348+
}
299349
}
300350
}

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

Lines changed: 17 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,6 @@ public float OdometerM
427427
public float CombinedControlSplitPosition;
428428
public bool HasSmoothStruc;
429429

430-
bool controlTrailerBrakeSystemSet = false;
431-
432430
public float MaxContinuousForceN;
433431
public float SpeedOfMaxContinuousForceMpS; // Speed where maximum tractive effort occurs
434432
public float MSTSSpeedOfMaxContinuousForceMpS; // Speed where maximum tractive effort occurs - MSTS parameter if used
@@ -2018,32 +2016,6 @@ public void DynamicBrakeBlending(float elapsedClockSeconds)
20182016
/// </summary>
20192017
public override void Update(float elapsedClockSeconds)
20202018
{
2021-
// A control car typically doesn't have its own compressor and relies on the attached power car. However OR uses the lead locomotive as the reference car for compressor calculations.
2022-
// Hence whilst users are encouraged to leave these parameters out of the ENG file, they need to be setup for OR to work correctly.
2023-
// Some parameters need to be split across the unpowered and powered car for correct timing and volume calculations.
2024-
// This setup loop is only processed the first time that update is run.
2025-
if (EngineType == EngineTypes.Control && !controlTrailerBrakeSystemSet)
2026-
{
2027-
FindControlActiveLocomotive();
2028-
2029-
if (ControlActiveLocomotive != null)
2030-
{
2031-
// Split reservoir volume across the power car and the active locomotive
2032-
MainResVolumeM3 = ControlActiveLocomotive.MainResVolumeM3 / 2;
2033-
ControlActiveLocomotive.MainResVolumeM3 = MainResVolumeM3;
2034-
2035-
MaxMainResPressurePSI = ControlActiveLocomotive.MaxMainResPressurePSI;
2036-
MainResPressurePSI = MaxMainResPressurePSI;
2037-
ControlActiveLocomotive.MainResPressurePSI = MainResPressurePSI;
2038-
controlTrailerBrakeSystemSet = true; // Ensure this loop is only processes the first time update routine run
2039-
MaximumMainReservoirPipePressurePSI = ControlActiveLocomotive.MaximumMainReservoirPipePressurePSI;
2040-
CompressorRestartPressurePSI = ControlActiveLocomotive.CompressorRestartPressurePSI;
2041-
MainResChargingRatePSIpS = ControlActiveLocomotive.MainResChargingRatePSIpS;
2042-
BrakePipeChargingRatePSIorInHgpS = ControlActiveLocomotive.BrakePipeChargingRatePSIorInHgpS;
2043-
TrainBrakePipeLeakPSIorInHgpS = ControlActiveLocomotive.TrainBrakePipeLeakPSIorInHgpS;
2044-
}
2045-
}
2046-
20472019
var gearloco = this as MSTSDieselLocomotive;
20482020

20492021
// Pass Gearbox commands
@@ -5559,97 +5531,35 @@ public virtual float GetDataOf(CabViewControl cvc)
55595531

55605532
case CABViewControlTypes.RPM:
55615533
{
5562-
5563-
if (EngineType == EngineTypes.Control)
5564-
{
5565-
FindControlActiveLocomotive();
5566-
5567-
if (ControlActiveLocomotive != null)
5568-
{
5569-
var activeloco = ControlActiveLocomotive as MSTSDieselLocomotive;
5570-
if (activeloco.DieselEngines[0] != null)
5571-
data = activeloco.DieselEngines[0].RealRPM;
5572-
}
5573-
5574-
}
5575-
else
5576-
{
5577-
var mstsDieselLocomotive = this as MSTSDieselLocomotive;
5578-
if (mstsDieselLocomotive.DieselEngines[0] != null)
5579-
data = mstsDieselLocomotive.DieselEngines[0].RealRPM;
5580-
}
5534+
var mstsDieselLocomotive = this as MSTSDieselLocomotive;
5535+
if (mstsDieselLocomotive.DieselEngines[0] != null)
5536+
data = mstsDieselLocomotive.DieselEngines[0].RealRPM;
55815537
break;
55825538
}
55835539

55845540
case CABViewControlTypes.RPM_2:
55855541
{
5586-
5587-
FindControlActiveLocomotive();
5588-
5589-
if (ControlActiveLocomotive != null)
5590-
{
5591-
var activeloco = ControlActiveLocomotive as MSTSDieselLocomotive;
5592-
var mstsDieselLocomotive = this as MSTSDieselLocomotive;
5593-
5594-
if (EngineType == EngineTypes.Control && activeloco.DieselEngines.NumOfActiveEngines > 1)
5595-
{
5596-
5597-
if (activeloco.DieselEngines[1] != null)
5598-
data = activeloco.DieselEngines[1].RealRPM;
5599-
}
5600-
else if (EngineType == EngineTypes.Diesel && mstsDieselLocomotive.DieselEngines.NumOfActiveEngines > 1)
5601-
{
5602-
if (mstsDieselLocomotive.DieselEngines[1] != null)
5603-
data = mstsDieselLocomotive.DieselEngines[1].RealRPM;
5604-
}
5542+
if (this is MSTSDieselLocomotive mstsDieselLocomotive && mstsDieselLocomotive.DieselEngines.NumOfActiveEngines > 1)
5543+
{
5544+
if (mstsDieselLocomotive.DieselEngines[1] != null)
5545+
data = mstsDieselLocomotive.DieselEngines[1].RealRPM;
56055546
}
5606-
break;
56075547
}
5548+
break;
56085549

56095550
case CABViewControlTypes.ORTS_DIESEL_TEMPERATURE:
56105551
{
5611-
5612-
if (EngineType == EngineTypes.Control)
5613-
{
5614-
FindControlActiveLocomotive();
5615-
5616-
if (ControlActiveLocomotive != null)
5617-
{
5618-
var activeloco = ControlActiveLocomotive as MSTSDieselLocomotive;
5619-
if (activeloco.DieselEngines[0] != null)
5620-
data = activeloco.DieselEngines[0].DieselTemperatureDeg;
5621-
}
5622-
5623-
}
5624-
else
5625-
{
5626-
var mstsDieselLocomotive = this as MSTSDieselLocomotive;
5627-
if (mstsDieselLocomotive.DieselEngines[0] != null)
5628-
data = mstsDieselLocomotive.DieselEngines[0].DieselTemperatureDeg;
5629-
}
5552+
var mstsDieselLocomotive = this as MSTSDieselLocomotive;
5553+
if (mstsDieselLocomotive.DieselEngines[0] != null)
5554+
data = mstsDieselLocomotive.DieselEngines[0].DieselTemperatureDeg;
56305555
break;
56315556
}
56325557

56335558
case CABViewControlTypes.ORTS_OIL_PRESSURE:
56345559
{
5635-
if (EngineType == EngineTypes.Control)
5636-
{
5637-
FindControlActiveLocomotive();
5638-
5639-
if (ControlActiveLocomotive != null)
5640-
{
5641-
var activeloco = ControlActiveLocomotive as MSTSDieselLocomotive;
5642-
if (activeloco.DieselEngines[0] != null)
5643-
data = activeloco.DieselEngines[0].DieselOilPressurePSI;
5644-
}
5645-
5646-
}
5647-
else
5648-
{
5649-
var mstsDieselLocomotive = this as MSTSDieselLocomotive;
5650-
if (mstsDieselLocomotive.DieselEngines[0] != null)
5651-
data = mstsDieselLocomotive.DieselEngines[0].DieselOilPressurePSI;
5652-
}
5560+
var mstsDieselLocomotive = this as MSTSDieselLocomotive;
5561+
if (mstsDieselLocomotive.DieselEngines[0] != null)
5562+
data = mstsDieselLocomotive.DieselEngines[0].DieselOilPressurePSI;
56535563
break;
56545564
}
56555565

@@ -5852,30 +5762,10 @@ public virtual float GetDataOf(CabViewControl cvc)
58525762
}
58535763
case CABViewControlTypes.WHEELSLIP:
58545764
{
5855-
if (EngineType == EngineTypes.Control)
5856-
{
5857-
FindControlActiveLocomotive();
5858-
5859-
if (ControlActiveLocomotive != null)
5860-
{
5861-
var activeloco = ControlActiveLocomotive as MSTSDieselLocomotive;
5862-
if (activeloco.DieselEngines[0] != null)
5863-
{
5864-
if (activeloco.AdvancedAdhesionModel && Train.TrainType != Train.TRAINTYPE.AI_PLAYERHOSTING && !Train.Autopilot)
5865-
data = activeloco.HuDIsWheelSlipWarninq ? 1 : 0;
5866-
else
5867-
data = activeloco.HuDIsWheelSlip ? 1 : 0;
5868-
5869-
}
5870-
}
5871-
}
5765+
if (AdvancedAdhesionModel && Train.TrainType != Train.TRAINTYPE.AI_PLAYERHOSTING && !Train.Autopilot)
5766+
data = HuDIsWheelSlipWarninq ? 1 : 0;
58725767
else
5873-
{
5874-
if (AdvancedAdhesionModel && Train.TrainType != Train.TRAINTYPE.AI_PLAYERHOSTING && !Train.Autopilot)
5875-
data = HuDIsWheelSlipWarninq ? 1 : 0;
5876-
else
5877-
data = HuDIsWheelSlip ? 1 : 0;
5878-
}
5768+
data = HuDIsWheelSlip ? 1 : 0;
58795769
break;
58805770
}
58815771

0 commit comments

Comments
 (0)