Skip to content

Commit 9dc7237

Browse files
authored
Merge pull request #740 from Sharpe49/feature/traction-cut-off-refactoring
Refactored the circuit breaker and the traction cut-off relay in order to use the same design pattern as the C# signal scripts (compatible with current scripts)
2 parents 5f2ded5 + b3e66ca commit 9dc7237

File tree

10 files changed

+204
-147
lines changed

10 files changed

+204
-147
lines changed

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
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 Orts.Simulation.RollingStocks.SubSystems.PowerSupplies;
1819
using ORTS.Common;
19-
using System;
2020

2121
namespace ORTS.Scripting.Api
2222
{
@@ -25,23 +25,33 @@ namespace ORTS.Scripting.Api
2525
/// </summary>
2626
public abstract class CircuitBreaker : TractionCutOffSubsystem
2727
{
28+
internal ScriptedCircuitBreaker CbHost => Host as ScriptedCircuitBreaker;
29+
2830
/// <summary>
2931
/// Current state of the circuit breaker
3032
/// </summary>
31-
public Func<CircuitBreakerState> CurrentState;
33+
protected CircuitBreakerState CurrentState() => CbHost.State;
34+
3235
/// <summary>
3336
/// TCS' circuit breaker closing order
3437
/// </summary>
35-
public Func<bool> TCSClosingOrder;
38+
protected bool TCSClosingOrder() => CbHost.TCSClosingOrder;
39+
3640
/// <summary>
3741
/// TCS' circuit breaker opening order
3842
/// </summary>
39-
public Func<bool> TCSOpeningOrder;
43+
protected bool TCSOpeningOrder() => CbHost.TCSOpeningOrder;
4044

4145
/// <summary>
4246
/// Sets the current state of the circuit breaker
4347
/// </summary>
44-
public Action<CircuitBreakerState> SetCurrentState;
48+
protected void SetCurrentState(CircuitBreakerState state)
49+
{
50+
CbHost.State = state;
51+
52+
TCSEvent tcsEvent = state == CircuitBreakerState.Closed ? TCSEvent.CircuitBreakerClosed : TCSEvent.CircuitBreakerOpen;
53+
Locomotive.TrainControlSystem.HandleEvent(tcsEvent);
54+
}
4555
}
4656

4757
public enum CircuitBreakerState

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
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 Orts.Simulation.RollingStocks.SubSystems.PowerSupplies;
1819
using ORTS.Common;
19-
using System;
2020

2121
namespace ORTS.Scripting.Api
2222
{
@@ -25,21 +25,32 @@ namespace ORTS.Scripting.Api
2525
/// </summary>
2626
public abstract class TractionCutOffRelay : TractionCutOffSubsystem
2727
{
28+
internal ScriptedTractionCutOffRelay TcorHost => Host as ScriptedTractionCutOffRelay;
29+
2830
/// <summary>
2931
/// Current state of the circuit breaker
3032
/// </summary>
31-
public Func<TractionCutOffRelayState> CurrentState;
33+
protected TractionCutOffRelayState CurrentState() => TcorHost.State;
3234

3335
/// <summary>
3436
/// Sets the current state of the circuit breaker
3537
/// </summary>
36-
public Action<TractionCutOffRelayState> SetCurrentState;
38+
protected void SetCurrentState(TractionCutOffRelayState state)
39+
{
40+
TcorHost.State = state;
41+
42+
TCSEvent tcsEvent = state == TractionCutOffRelayState.Closed ? TCSEvent.TractionCutOffRelayClosed : TCSEvent.TractionCutOffRelayOpen;
43+
Locomotive.TrainControlSystem.HandleEvent(tcsEvent);
44+
}
3745

3846
/// <summary>
3947
/// Current state of the circuit breaker
4048
/// Only available on dual mode locomotives
4149
/// </summary>
42-
public Func<CircuitBreakerState> CurrentCircuitBreakerState;
50+
protected CircuitBreakerState CurrentCircuitBreakerState()
51+
{
52+
return PowerSupply.Type == PowerSupplyType.DualMode ? (PowerSupply as ScriptedDualModePowerSupply).CircuitBreaker.State : CircuitBreakerState.Unavailable;
53+
}
4354
}
4455

4556
public enum TractionCutOffRelayState

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

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
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 ORTS.Common;
19-
using System;
18+
using Orts.Simulation.RollingStocks;
19+
using Orts.Simulation.RollingStocks.SubSystems.PowerSupplies;
2020

2121
namespace ORTS.Scripting.Api
2222
{
@@ -25,85 +25,128 @@ namespace ORTS.Scripting.Api
2525
/// </summary>
2626
public abstract class TractionCutOffSubsystem : AbstractTrainScriptClass
2727
{
28+
internal ITractionCutOffSubsystem Host;
29+
internal ILocomotivePowerSupply PowerSupply => Host.PowerSupply;
30+
internal MSTSLocomotive Locomotive => PowerSupply.Car as MSTSLocomotive;
31+
internal MSTSDieselLocomotive DieselLocomotive => Locomotive as MSTSDieselLocomotive;
32+
33+
/// <summary>
34+
/// Attaches the script to its host
35+
/// </summary>
36+
/// <param name="host">The hosting ITractionCutOffSubsystem object</param>
37+
internal void AttachToHost(ITractionCutOffSubsystem host)
38+
{
39+
Host = host;
40+
}
41+
2842
/// <summary>
2943
/// Locomotive's power supply type
3044
/// </summary>
31-
public Func<PowerSupplyType> SupplyType;
45+
protected PowerSupplyType SupplyType() => PowerSupply.Type;
46+
3247
/// <summary>
3348
/// Current state of the pantograph
3449
/// </summary>
35-
public Func<PantographState> CurrentPantographState;
50+
protected PantographState CurrentPantographState() => Locomotive?.Pantographs.State ?? PantographState.Unavailable;
51+
3652
/// <summary>
3753
/// Current state of the diesel engine
3854
/// </summary>
39-
public Func<DieselEngineState> CurrentDieselEngineState;
55+
protected DieselEngineState CurrentDieselEngineState() => DieselLocomotive?.DieselEngines.State ?? DieselEngineState.Unavailable;
56+
4057
/// <summary>
4158
/// Current state of the power supply
4259
/// </summary>
43-
public Func<PowerSupplyState> CurrentPowerSupplyState;
60+
protected PowerSupplyState CurrentPowerSupplyState() => PowerSupply.MainPowerSupplyState;
61+
4462
/// <summary>
4563
/// Driver's circuit breaker closing order
4664
/// </summary>
47-
public Func<bool> DriverClosingOrder;
65+
protected bool DriverClosingOrder() => Host.DriverClosingOrder;
66+
4867
/// <summary>
4968
/// Driver's circuit breaker closing authorization
5069
/// </summary>
51-
public Func<bool> DriverClosingAuthorization;
70+
protected bool DriverClosingAuthorization() => Host.DriverClosingAuthorization;
71+
5272
/// <summary>
5373
/// Driver's circuit breaker opening order
5474
/// </summary>
55-
public Func<bool> DriverOpeningOrder;
75+
protected bool DriverOpeningOrder() => Host.DriverOpeningOrder;
76+
5677
/// <summary>
5778
/// TCS' circuit breaker closing authorization
5879
/// </summary>
59-
public Func<bool> TCSClosingAuthorization;
80+
protected bool TCSClosingAuthorization() => Host.TCSClosingAuthorization;
81+
6082
/// <summary>
6183
/// Circuit breaker closing authorization
6284
/// </summary>
63-
public Func<bool> ClosingAuthorization;
85+
protected bool ClosingAuthorization() => Host.ClosingAuthorization;
86+
6487
/// <summary>
6588
/// True if low voltage power supply is switched on.
6689
/// </summary>
67-
public Func<bool> IsLowVoltagePowerSupplyOn;
90+
protected bool IsLowVoltagePowerSupplyOn() => PowerSupply.LowVoltagePowerSupplyOn;
91+
6892
/// <summary>
6993
/// True if cab power supply is switched on.
7094
/// </summary>
71-
public Func<bool> IsCabPowerSupplyOn;
95+
protected bool IsCabPowerSupplyOn() => PowerSupply.CabPowerSupplyOn;
96+
7297
/// <summary>
7398
/// Delay before circuit breaker closing
7499
/// </summary>
75-
public Func<float> ClosingDelayS;
100+
protected float ClosingDelayS() => Host.DelayS;
76101

77102
/// <summary>
78103
/// Sets the driver's circuit breaker closing order
79104
/// </summary>
80-
public Action<bool> SetDriverClosingOrder;
105+
protected void SetDriverClosingOrder(bool value)
106+
{
107+
Host.DriverClosingOrder = value;
108+
}
109+
81110
/// <summary>
82111
/// Sets the driver's circuit breaker closing authorization
83112
/// </summary>
84-
public Action<bool> SetDriverClosingAuthorization;
113+
protected void SetDriverClosingAuthorization(bool value)
114+
{
115+
Host.DriverClosingAuthorization = value;
116+
}
117+
85118
/// <summary>
86119
/// Sets the driver's circuit breaker opening order
87120
/// </summary>
88-
public Action<bool> SetDriverOpeningOrder;
121+
protected void SetDriverOpeningOrder(bool value)
122+
{
123+
Host.DriverOpeningOrder = value;
124+
}
125+
89126
/// <summary>
90127
/// Sets the circuit breaker closing authorization
91128
/// </summary>
92-
public Action<bool> SetClosingAuthorization;
129+
protected void SetClosingAuthorization(bool value)
130+
{
131+
Host.ClosingAuthorization = value;
132+
}
93133

94134
/// <summary>
95135
/// Called once at initialization time.
96136
/// </summary>
97137
public abstract void Initialize();
138+
98139
/// <summary>
99140
/// Called once at initialization time if the train speed is greater than 0.
100141
/// Set as virtual to keep compatibility with scripts not providing this method.
101142
/// </summary>
102143
public virtual void InitializeMoving() { }
144+
103145
/// <summary>
104146
/// Called regularly at every simulator update cycle.
105147
/// </summary>
106148
public abstract void Update(float elapsedClockSeconds);
149+
107150
/// <summary>
108151
/// Called when an event happens (a closing order from the driver for example)
109152
/// </summary>

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

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,33 @@
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;
19+
using System.IO;
1820
using Orts.Common;
1921
using Orts.Parsers.Msts;
20-
using Orts.Simulation.AIs;
2122
using Orts.Simulation.Physics;
2223
using ORTS.Scripting.Api;
23-
using System;
24-
using System.IO;
2524

2625
namespace Orts.Simulation.RollingStocks.SubSystems.PowerSupplies
2726
{
2827

29-
public class ScriptedCircuitBreaker : ISubSystem<ScriptedCircuitBreaker>
28+
public class ScriptedCircuitBreaker : ITractionCutOffSubsystem
3029
{
31-
public ScriptedLocomotivePowerSupply PowerSupply { get; protected set; }
32-
public MSTSLocomotive Locomotive => PowerSupply.Locomotive;
33-
public Simulator Simulator => PowerSupply.Locomotive.Simulator;
30+
public ILocomotivePowerSupply PowerSupply { get; protected set; }
31+
public ScriptedLocomotivePowerSupply LocomotivePowerSupply => PowerSupply as ScriptedLocomotivePowerSupply;
32+
public MSTSLocomotive Locomotive => LocomotivePowerSupply.Locomotive;
33+
public Simulator Simulator => Locomotive.Simulator;
3434

3535
public bool Activated = false;
3636
string ScriptName = "Automatic";
3737
CircuitBreaker Script;
3838

39-
private float DelayS = 0f;
39+
public float DelayS { get; protected set; } = 0f;
4040

41-
public CircuitBreakerState State { get; private set; } = CircuitBreakerState.Open;
42-
public bool DriverClosingOrder { get; private set; }
43-
public bool DriverOpeningOrder { get; private set; }
44-
public bool DriverClosingAuthorization { get; private set; }
41+
public CircuitBreakerState State { get; set; } = CircuitBreakerState.Open;
42+
public bool DriverClosingOrder { get; set; }
43+
public bool DriverOpeningOrder { get; set; }
44+
public bool DriverClosingAuthorization { get; set; }
4545
public bool TCSClosingOrder
4646
{
4747
get
@@ -72,18 +72,21 @@ public bool TCSClosingAuthorization
7272
return false;
7373
}
7474
}
75-
public bool ClosingAuthorization { get; private set; }
75+
public bool ClosingAuthorization { get; set; }
7676

7777
public ScriptedCircuitBreaker(ScriptedLocomotivePowerSupply powerSupply)
7878
{
7979
PowerSupply = powerSupply;
8080
}
8181

82-
public void Copy(ScriptedCircuitBreaker other)
82+
public void Copy(ITractionCutOffSubsystem other)
8383
{
84-
ScriptName = other.ScriptName;
85-
State = other.State;
86-
DelayS = other.DelayS;
84+
if (other is ScriptedCircuitBreaker cbOther)
85+
{
86+
ScriptName = cbOther.ScriptName;
87+
State = cbOther.State;
88+
DelayS = cbOther.DelayS;
89+
}
8790
}
8891

8992
public void Parse(string lowercasetoken, STFReader stf)
@@ -128,6 +131,8 @@ public void Initialize()
128131
Script = new AutomaticCircuitBreaker() as CircuitBreaker;
129132
}
130133

134+
Script.AttachToHost(this);
135+
131136
// AbstractScriptClass
132137
Script.ClockTime = () => (float)Simulator.ClockTime;
133138
Script.GameTime = () => (float)Simulator.GameTime;
@@ -145,40 +150,6 @@ public void Initialize()
145150
}
146151
};
147152

148-
// TractionCutOffSubsystem getters
149-
Script.SupplyType = () => PowerSupply.Type;
150-
Script.CurrentState = () => State;
151-
Script.CurrentPantographState = () => Locomotive?.Pantographs.State ?? PantographState.Unavailable;
152-
Script.CurrentDieselEngineState = () => (Locomotive as MSTSDieselLocomotive)?.DieselEngines.State ?? DieselEngineState.Unavailable;
153-
Script.CurrentPowerSupplyState = () => PowerSupply.MainPowerSupplyState;
154-
Script.DriverClosingOrder = () => DriverClosingOrder;
155-
Script.DriverOpeningOrder = () => DriverOpeningOrder;
156-
Script.DriverClosingAuthorization = () => DriverClosingAuthorization;
157-
Script.TCSClosingAuthorization = () => TCSClosingAuthorization;
158-
Script.ClosingAuthorization = () => ClosingAuthorization;
159-
Script.IsLowVoltagePowerSupplyOn = () => PowerSupply.LowVoltagePowerSupplyOn;
160-
Script.IsCabPowerSupplyOn = () => PowerSupply.CabPowerSupplyOn;
161-
Script.ClosingDelayS = () => DelayS;
162-
163-
// TractionCutOffSubsystem setters
164-
Script.SetDriverClosingOrder = (value) => DriverClosingOrder = value;
165-
Script.SetDriverOpeningOrder = (value) => DriverOpeningOrder = value;
166-
Script.SetDriverClosingAuthorization = (value) => DriverClosingAuthorization = value;
167-
Script.SetClosingAuthorization = (value) => ClosingAuthorization = value;
168-
169-
// CircuitBreaker getters
170-
Script.CurrentState = () => State;
171-
Script.TCSClosingOrder = () => TCSClosingOrder;
172-
Script.TCSOpeningOrder = () => TCSOpeningOrder;
173-
174-
// CircuitBreaker setters
175-
Script.SetCurrentState = (value) =>
176-
{
177-
State = value;
178-
TCSEvent CircuitBreakerEvent = State == CircuitBreakerState.Closed ? TCSEvent.CircuitBreakerClosed : TCSEvent.CircuitBreakerOpen;
179-
Locomotive.TrainControlSystem.HandleEvent(CircuitBreakerEvent);
180-
};
181-
182153
Script.Initialize();
183154
Activated = true;
184155
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace Orts.Simulation.RollingStocks.SubSystems.PowerSupplies
2424
/// </summary>
2525
public interface IPowerSupply : ISubSystem<IPowerSupply>, IParsable
2626
{
27+
TrainCar Car { get; }
2728
BatterySwitch BatterySwitch { get; }
2829

2930
PowerSupplyState ElectricTrainSupplyState { get; }

0 commit comments

Comments
 (0)