Skip to content

Commit 5522fc5

Browse files
authoredMay 8, 2025
Merge pull request #1078 from cesarBLG/steam-locomotive-script
Default PowerSupply script for steam locomotives
2 parents 4fdacd5 + 9ab642e commit 5522fc5

File tree

3 files changed

+70
-190
lines changed

3 files changed

+70
-190
lines changed
 

‎Source/Orts.Simulation/Simulation/RollingStocks/MSTSSteamLocomotive.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ public MSTSSteamLocomotive(Simulator simulator, string wagFile)
850850
: base(simulator, wagFile)
851851
{
852852
SteamEngines = new SteamEngines(this);
853-
PowerSupply = new SteamPowerSupply(this);
853+
PowerSupply = new ScriptedSteamPowerSupply(this);
854854

855855
RefillTenderWithFuel();
856856
RefillTenderWithWater();

‎Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerSupplies/ElectricTrainSupplySwitch.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// COPYRIGHT 2020 by the Open Rails project.
1+
// COPYRIGHT 2020 by the Open Rails project.
22
//
33
// This file is part of Open Rails.
44
//
@@ -31,16 +31,17 @@ public enum ModeType
3131
Unfitted, // Locomotive without ETS
3232
Switch, // Locomotive with ETS activation via a switch
3333
}
34-
public ModeType Mode { get; protected set; } = ModeType.Automatic;
34+
public ModeType Mode { get; protected set; }
3535

3636
// Variables
3737
readonly MSTSLocomotive Locomotive;
3838
public bool CommandSwitch { get; protected set; } = false;
3939
public bool On { get; protected set; } = false;
4040

41-
public ElectricTrainSupplySwitch(MSTSLocomotive locomotive)
41+
public ElectricTrainSupplySwitch(MSTSLocomotive locomotive, ModeType defaultMode = ModeType.Automatic)
4242
{
4343
Locomotive = locomotive;
44+
Mode = defaultMode;
4445
}
4546

4647
public virtual void Parse(string lowercasetoken, STFReader stf)

‎Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerSupplies/SteamPowerSupply.cs

Lines changed: 65 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
// You should have received a copy of the GNU General Public License
1616
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
1717

18+
using System.Diagnostics;
1819
using System.IO;
1920
using Orts.Parsers.Msts;
21+
using ORTS.Common;
2022
using ORTS.Scripting.Api;
2123

2224
namespace Orts.Simulation.RollingStocks.SubSystems.PowerSupplies
@@ -25,226 +27,103 @@ namespace Orts.Simulation.RollingStocks.SubSystems.PowerSupplies
2527
/// Basic power supply class for steam locomotives
2628
/// For electrical systems powered by battery
2729
/// </summary>
28-
public class SteamPowerSupply : ILocomotivePowerSupply
30+
public class ScriptedSteamPowerSupply : ScriptedLocomotivePowerSupply
2931
{
30-
public TrainCar Car { get; }
31-
public MSTSSteamLocomotive Locomotive => Car as MSTSSteamLocomotive;
32-
public PowerSupplyType Type => PowerSupplyType.Steam;
33-
34-
public Pantographs Pantographs => Locomotive.Pantographs;
35-
public Battery Battery { get; protected set; }
36-
public BatterySwitch BatterySwitch => Battery.BatterySwitch;
37-
public MasterKey MasterKey { get; protected set; }
38-
public ElectricTrainSupplySwitch ElectricTrainSupplySwitch => null;
39-
40-
public PowerSupplyState MainPowerSupplyState
32+
public MSTSSteamLocomotive SteamLocomotive => Locomotive as MSTSSteamLocomotive;
33+
public override PowerSupplyType Type => PowerSupplyType.Steam;
34+
public ScriptedSteamPowerSupply(MSTSSteamLocomotive locomotive) : base(locomotive)
4135
{
42-
get
43-
{
44-
return PowerSupplyState.PowerOn;
45-
}
46-
set{}
36+
ElectricTrainSupplySwitch = new ElectricTrainSupplySwitch(Locomotive, ElectricTrainSupplySwitch.ModeType.Unfitted);
4737
}
48-
49-
public bool MainPowerSupplyOn => true;
50-
public bool DynamicBrakeAvailable
38+
public override void Initialize()
5139
{
52-
get
53-
{
54-
return false;
55-
}
56-
set{}
57-
}
58-
public float PowerSupplyDynamicBrakePercent { get; set; } = -1;
59-
public float MaximumDynamicBrakePowerW { get; set; } = 0;
60-
public float MaxThrottlePercent { get; set; } = 100;
61-
public float ThrottleReductionPercent { get; set; } = 0;
40+
base.Initialize();
6241

63-
public PowerSupplyState AuxiliaryPowerSupplyState
64-
{
65-
get
42+
if (AbstractScript == null)
6643
{
67-
return PowerSupplyState.PowerOn;
68-
}
69-
set{}
70-
}
44+
if (ScriptName != null && ScriptName != "Default")
45+
{
46+
Trace.TraceWarning("Skipped custom power supply script, not available for steam locomotives.");
47+
}
7148

72-
public bool AuxiliaryPowerSupplyOn => true;
49+
if (ParametersFileName != null)
50+
{
51+
ParametersFileName = Path.Combine(Path.Combine(Path.GetDirectoryName(Locomotive.WagFilePath), "Script"), ParametersFileName);
52+
}
7353

74-
public PowerSupplyState LowVoltagePowerSupplyState
75-
{
76-
get
77-
{
78-
return Battery.State == PowerSupplyState.PowerOn ? PowerSupplyState.PowerOn : PowerSupplyState.PowerOff;
79-
}
80-
set{}
81-
}
54+
if (AbstractScript == null)
55+
{
56+
AbstractScript = new DefaultSteamPowerSupply();
57+
}
8258

83-
public bool LowVoltagePowerSupplyOn => LowVoltagePowerSupplyState == PowerSupplyState.PowerOn;
59+
AssignScriptFunctions();
8460

85-
public PowerSupplyState BatteryState
86-
{
87-
get
88-
{
89-
return Battery.State;
90-
}
91-
set
92-
{
93-
Battery.State = value;
61+
AbstractScript.AttachToHost(this);
62+
AbstractScript.Initialize();
9463
}
9564
}
9665

97-
public bool BatteryOn => BatteryState == PowerSupplyState.PowerOn;
98-
public float BatteryVoltageV => BatteryOn ? Battery.VoltageV : 0;
66+
public override void Update(float elapsedClockSeconds)
67+
{
68+
base.Update(elapsedClockSeconds);
9969

100-
public PowerSupplyState CabPowerSupplyState
70+
AbstractScript?.Update(elapsedClockSeconds);
71+
}
72+
}
73+
public class DefaultSteamPowerSupply : LocomotivePowerSupply
74+
{
75+
public override void Initialize()
10176
{
102-
get
103-
{
104-
return MasterKey.On ? PowerSupplyState.PowerOn : PowerSupplyState.PowerOff;
105-
}
106-
set{}
10777
}
10878

109-
public bool CabPowerSupplyOn => CabPowerSupplyState == PowerSupplyState.PowerOn;
110-
111-
public PowerSupplyState ElectricTrainSupplyState
79+
public override void Update(float elapsedClockSeconds)
11280
{
113-
get
114-
{
115-
return PowerSupplyState.Unavailable;
116-
}
117-
set{}
118-
}
81+
SetCurrentBatteryState(BatterySwitchOn() ? PowerSupplyState.PowerOn : PowerSupplyState.PowerOff);
82+
SetCurrentLowVoltagePowerSupplyState(BatterySwitchOn() ? PowerSupplyState.PowerOn : PowerSupplyState.PowerOff);
83+
SetCurrentCabPowerSupplyState(BatterySwitchOn() && MasterKeyOn() ? PowerSupplyState.PowerOn : PowerSupplyState.PowerOff);
11984

120-
public bool ElectricTrainSupplyOn => false;
121-
public bool FrontElectricTrainSupplyCableConnected { get => false; set { } }
122-
public float ElectricTrainSupplyPowerW => 0f;
85+
SetCurrentMainPowerSupplyState(PowerSupplyState.PowerOn);
86+
SetCurrentAuxiliaryPowerSupplyState(PowerSupplyState.PowerOn);
12387

124-
public bool ServiceRetentionButton
125-
{
126-
get
88+
if (ElectricTrainSupplyUnfitted())
12789
{
128-
return false;
90+
SetCurrentElectricTrainSupplyState(PowerSupplyState.Unavailable);
12991
}
130-
set{}
131-
}
132-
133-
public bool ServiceRetentionCancellationButton
134-
{
135-
get
92+
else if (CurrentAuxiliaryPowerSupplyState() == PowerSupplyState.PowerOn
93+
&& ElectricTrainSupplySwitchOn())
13694
{
137-
return false;
95+
SetCurrentElectricTrainSupplyState(PowerSupplyState.PowerOn);
13896
}
139-
set{}
140-
}
141-
142-
public bool ServiceRetentionActive
143-
{
144-
get
97+
else
14598
{
146-
return false;
99+
SetCurrentElectricTrainSupplyState(PowerSupplyState.PowerOff);
147100
}
148-
set{}
149101
}
150102

151-
public SteamPowerSupply(MSTSSteamLocomotive locomotive)
103+
public override void HandleEvent(PowerSupplyEvent evt)
152104
{
153-
Car = locomotive;
154-
155-
Battery = new Battery(Locomotive);
156-
MasterKey = new MasterKey(Locomotive);
157-
}
158-
159-
public virtual void Parse(string lowercasetoken, STFReader stf)
160-
{
161-
switch (lowercasetoken)
105+
switch (evt)
162106
{
163-
case "engine(ortsbattery":
164-
Battery.Parse(lowercasetoken, stf);
107+
case PowerSupplyEvent.QuickPowerOn:
108+
SignalEventToBatterySwitch(PowerSupplyEvent.QuickPowerOn);
109+
SignalEventToMasterKey(PowerSupplyEvent.TurnOnMasterKey);
110+
SignalEventToPantograph(PowerSupplyEvent.RaisePantograph, 1);
111+
SignalEventToOtherTrainVehiclesWithId(PowerSupplyEvent.RaisePantograph, 1);
112+
SignalEventToElectricTrainSupplySwitch(PowerSupplyEvent.SwitchOnElectricTrainSupply);
165113
break;
166-
case "engine(ortsmasterkey(mode":
167-
case "engine(ortsmasterkey(delayoff":
168-
case "engine(ortsmasterkey(headlightcontrol":
169-
MasterKey.Parse(lowercasetoken, stf);
114+
115+
case PowerSupplyEvent.QuickPowerOff:
116+
SignalEventToElectricTrainSupplySwitch(PowerSupplyEvent.SwitchOffElectricTrainSupply);
117+
SignalEventToPantographs(PowerSupplyEvent.LowerPantograph);
118+
SignalEventToOtherTrainVehicles(PowerSupplyEvent.LowerPantograph);
119+
SignalEventToMasterKey(PowerSupplyEvent.TurnOffMasterKey);
120+
SignalEventToBatterySwitch(PowerSupplyEvent.QuickPowerOff);
170121
break;
171-
}
172-
}
173122

174-
public void Copy(IPowerSupply other)
175-
{
176-
if (other is SteamPowerSupply steamOther)
177-
{
178-
Battery.Copy(steamOther.Battery);
179-
MasterKey.Copy(steamOther.MasterKey);
123+
default:
124+
base.HandleEvent(evt);
125+
break;
180126
}
181127
}
182-
183-
public void Initialize()
184-
{
185-
Battery.Initialize();
186-
MasterKey.Initialize();
187-
}
188-
189-
public virtual void InitializeMoving()
190-
{
191-
Battery.InitializeMoving();
192-
MasterKey.InitializeMoving();
193-
}
194-
195-
public void Save(BinaryWriter outf)
196-
{
197-
Battery.Save(outf);
198-
MasterKey.Save(outf);
199-
}
200-
201-
public void Restore(BinaryReader inf)
202-
{
203-
Battery.Restore(inf);
204-
MasterKey.Restore(inf);
205-
}
206-
207-
public void Update(float elapsedClockSeconds)
208-
{
209-
Battery.State = BatterySwitch.On ? PowerSupplyState.PowerOn : PowerSupplyState.PowerOff;
210-
}
211-
212-
public void HandleEvent(PowerSupplyEvent evt)
213-
{
214-
BatterySwitch.HandleEvent(evt);
215-
MasterKey.HandleEvent(evt);
216-
}
217-
218-
public void HandleEvent(PowerSupplyEvent evt, int id)
219-
{
220-
}
221-
222-
public void HandleEventFromTcs(PowerSupplyEvent evt)
223-
{
224-
}
225-
226-
public void HandleEventFromTcs(PowerSupplyEvent evt, int id)
227-
{
228-
}
229-
230-
public void HandleEventFromTcs(PowerSupplyEvent evt, string message)
231-
{
232-
}
233-
234-
public void HandleEventFromLeadLocomotive(PowerSupplyEvent evt)
235-
{
236-
}
237-
238-
public void HandleEventFromLeadLocomotive(PowerSupplyEvent evt, int id)
239-
{
240-
}
241-
242-
public void HandleEventFromOtherLocomotive(int locoIndex, PowerSupplyEvent evt)
243-
{
244-
}
245-
246-
public void HandleEventFromOtherLocomotive(int locoIndex, PowerSupplyEvent evt, int id)
247-
{
248-
}
249128
}
250129
}

0 commit comments

Comments
 (0)
Please sign in to comment.