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 ;
1819using System . IO ;
1920using Orts . Parsers . Msts ;
21+ using ORTS . Common ;
2022using ORTS . Scripting . Api ;
2123
2224namespace 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