Skip to content

Commit 02e149c

Browse files
authored
Merge pull request #738 from Sharpe49/feature/brake-controller-improvements
Brake controller scripts refactoring and improvements for mouse control
2 parents 896573d + 88cadc9 commit 02e149c

File tree

3 files changed

+132
-83
lines changed

3 files changed

+132
-83
lines changed

Source/Orts.Simulation/Common/Scripting/BrakeController.cs

Lines changed: 121 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,128 +15,219 @@
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 GNU.Gettext;
19-
using Orts.Simulation.RollingStocks.SubSystems.Controllers;
2018
using System;
2119
using System.Collections.Generic;
20+
using GNU.Gettext;
21+
using Orts.Simulation.RollingStocks;
22+
using Orts.Simulation.RollingStocks.SubSystems.Controllers;
23+
using Orts.Simulation.RollingStocks.SubSystems.PowerSupplies;
24+
using ORTS.Common;
2225

2326
namespace ORTS.Scripting.Api
2427
{
2528
public abstract class BrakeController : AbstractTrainScriptClass
2629
{
30+
internal ScriptedBrakeController Host;
31+
internal MSTSLocomotive Locomotive => Host.Locomotive;
32+
internal ILocomotivePowerSupply LocomotivePowerSupply => Locomotive.PowerSupply as ILocomotivePowerSupply;
33+
34+
/// <summary>
35+
/// Attaches the script to its host
36+
/// </summary>
37+
/// <param name="host">The hosting ScriptedBrakeController object</param>
38+
internal void AttachToHost(ScriptedBrakeController host)
39+
{
40+
Host = host;
41+
}
42+
2743
/// <summary>
2844
/// True if the driver has asked for an emergency braking (push button)
2945
/// </summary>
30-
public Func<bool> EmergencyBrakingPushButton;
46+
public bool EmergencyBrakingPushButton() => Host.EmergencyBrakingPushButton;
47+
3148
/// <summary>
3249
/// True if the TCS has asked for an emergency braking
3350
/// </summary>
34-
public Func<bool> TCSEmergencyBraking;
51+
public bool TCSEmergencyBraking() => Host.TCSEmergencyBraking;
52+
3553
/// <summary>
3654
/// True if the TCS has asked for a full service braking
3755
/// </summary>
38-
public Func<bool> TCSFullServiceBraking;
56+
public bool TCSFullServiceBraking() => Host.TCSFullServiceBraking;
57+
3958
/// <summary>
4059
/// True if the driver has pressed the Quick Release button
4160
/// </summary>
42-
public Func<bool> QuickReleaseButtonPressed;
61+
public bool QuickReleaseButtonPressed() => Host.QuickReleaseButtonPressed;
62+
4363
/// <summary>
4464
/// True if the driver has pressed the Overcharge button
4565
/// </summary>
46-
public Func<bool> OverchargeButtonPressed;
66+
public bool OverchargeButtonPressed() => Host.OverchargeButtonPressed;
67+
4768
/// <summary>
4869
/// True if low voltage power supply is switched on.
4970
/// </summary>
50-
public Func<bool> IsLowVoltagePowerSupplyOn;
71+
public bool IsLowVoltagePowerSupplyOn() => LocomotivePowerSupply.LowVoltagePowerSupplyOn;
72+
5173
/// <summary>
5274
/// True if cab power supply is switched on.
5375
/// </summary>
54-
public Func<bool> IsCabPowerSupplyOn;
76+
public bool IsCabPowerSupplyOn() => LocomotivePowerSupply.CabPowerSupplyOn;
77+
5578
/// <summary>
5679
/// Main reservoir pressure
5780
/// </summary>
58-
public Func<float> MainReservoirPressureBar;
81+
public float MainReservoirPressureBar()
82+
{
83+
if (Locomotive.Train != null)
84+
return Bar.FromPSI(Locomotive.MainResPressurePSI);
85+
else
86+
return float.MaxValue;
87+
}
88+
5989
/// <summary>
6090
/// Maximum pressure in the brake pipes and the equalizing reservoir
6191
/// </summary>
62-
public Func<float> MaxPressureBar;
92+
public float MaxPressureBar() => Bar.FromPSI(Host.MaxPressurePSI);
93+
6394
/// <summary>
6495
/// Maximum pressure in the brake pipes when they are overcharged
6596
/// </summary>
66-
public Func<float> MaxOverchargePressureBar;
97+
public float MaxOverchargePressureBar() => Bar.FromPSI(Host.MaxOverchargePressurePSI);
98+
6799
/// <summary>
68100
/// Release rate of the equalizing reservoir
69101
/// </summary>
70-
public Func<float> ReleaseRateBarpS;
102+
public float ReleaseRateBarpS() => BarpS.FromPSIpS(Host.ReleaseRatePSIpS);
103+
71104
/// <summary>
72105
/// Quick release rate of the equalizing reservoir
73106
/// </summary>
74-
public Func<float> QuickReleaseRateBarpS;
107+
public float QuickReleaseRateBarpS() => BarpS.FromPSIpS(Host.QuickReleaseRatePSIpS);
108+
75109
/// <summary>
76110
/// Pressure decrease rate of equalizing reservoir when eliminating overcharge
77111
/// </summary>
78-
public Func<float> OverchargeEliminationRateBarpS;
112+
public float OverchargeEliminationRateBarpS() => BarpS.FromPSIpS(Host.OverchargeEliminationRatePSIpS);
113+
79114
/// <summary>
80115
/// Slow application rate of the equalizing reservoir
81116
/// </summary>
82-
public Func<float> SlowApplicationRateBarpS;
117+
public float SlowApplicationRateBarpS() => BarpS.FromPSIpS(Host.SlowApplicationRatePSIpS);
118+
83119
/// <summary>
84120
/// Apply rate of the equalizing reservoir
85121
/// </summary>
86-
public Func<float> ApplyRateBarpS;
122+
public float ApplyRateBarpS() => BarpS.FromPSIpS(Host.ApplyRatePSIpS);
123+
87124
/// <summary>
88125
/// Emergency rate of the equalizing reservoir
89126
/// </summary>
90-
public Func<float> EmergencyRateBarpS;
127+
public float EmergencyRateBarpS() => BarpS.FromPSIpS(Host.EmergencyRatePSIpS);
128+
91129
/// <summary>
92130
/// Depressure needed in order to obtain the full service braking
93131
/// </summary>
94-
public Func<float> FullServReductionBar;
132+
public float FullServReductionBar() => Bar.FromPSI(Host.FullServReductionPSI);
133+
95134
/// <summary>
96135
/// Release rate of the equalizing reservoir
97136
/// </summary>
98-
public Func<float> MinReductionBar;
137+
public float MinReductionBar() => Bar.FromPSI(Host.MinReductionPSI);
138+
99139
/// <summary>
100140
/// Current value of the brake controller
101141
/// </summary>
102-
public Func<float> CurrentValue;
142+
public float CurrentValue() => Host.CurrentValue;
143+
144+
/// <summary>
145+
/// Intermediate value of the brake controller
146+
/// </summary>
147+
public float IntermediateValue
148+
{
149+
get
150+
{
151+
return Host.IntermediateValue;
152+
}
153+
set
154+
{
155+
Host.IntermediateValue = value;
156+
}
157+
}
158+
103159
/// <summary>
104160
/// Minimum value of the brake controller
105161
/// </summary>
106-
public Func<float> MinimumValue;
162+
public float MinimumValue() => Host.MinimumValue;
163+
107164
/// <summary>
108165
/// Maximum value of the brake controller
109166
/// </summary>
110-
public Func<float> MaximumValue;
167+
public float MaximumValue() => Host.MaximumValue;
168+
111169
/// <summary>
112170
/// Step size of the brake controller
113171
/// </summary>
114-
public Func<float> StepSize;
172+
public float StepSize() => Host.StepSize;
173+
115174
/// <summary>
116175
/// State of the brake pressure (1 = increasing, -1 = decreasing)
117176
/// </summary>
118-
public Func<float> UpdateValue;
177+
public float UpdateValue() => Host.UpdateValue;
178+
119179
/// <summary>
120180
/// Gives the list of notches
121181
/// </summary>
122-
public Func<List<MSTSNotch>> Notches;
182+
public List<MSTSNotch> Notches() => Host.Notches;
183+
123184
/// <summary>
124185
/// Fraction of train brake demanded by cruise control
125186
/// </summary>
126-
public Func<float> CruiseControlBrakeDemand;
187+
public float CruiseControlBrakeDemand() => Locomotive.CruiseControl != null ? Locomotive.CruiseControl.TrainBrakePercent/100 : 0;
188+
189+
/// <summary>
190+
/// Current notch of the brake controller
191+
/// </summary>
192+
public int CurrentNotch
193+
{
194+
get
195+
{
196+
return Host.CurrentNotch;
197+
}
198+
set
199+
{
200+
Host.CurrentNotch = value;
201+
}
202+
}
127203

128204
/// <summary>
129205
/// Sets the current value of the brake controller lever
130206
/// </summary>
131-
public Action<float> SetCurrentValue;
207+
public void SetCurrentValue(float value)
208+
{
209+
Host.CurrentValue = value;
210+
}
211+
132212
/// <summary>
133213
/// Sets the state of the brake pressure (1 = increasing, -1 = decreasing)
134214
/// </summary>
135-
public Action<float> SetUpdateValue;
215+
public void SetUpdateValue(float value)
216+
{
217+
Host.UpdateValue = value;
218+
}
219+
136220
/// <summary>
137221
/// Sets the dynamic brake intervention value
138222
/// </summary>
139-
public Action<float> SetDynamicBrakeIntervention;
223+
public void SetDynamicBrakeIntervention(float value)
224+
{
225+
// TODO: Set dynamic brake intervention instead of controller position
226+
// There are some issues that need to be identified and fixed before setting the intervention directly
227+
if (Locomotive.DynamicBrakeController == null) return;
228+
Locomotive.DynamicBrakeChangeActiveState(value > 0);
229+
Locomotive.DynamicBrakeController.SetValue(value);
230+
}
140231

141232
/// <summary>
142233
/// Called once at initialization time.

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

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ namespace Orts.Simulation.RollingStocks.SubSystems.Controllers
2727
{
2828
public class ScriptedBrakeController : IController
2929
{
30-
readonly MSTSLocomotive Locomotive;
31-
readonly Simulator Simulator;
30+
public readonly MSTSLocomotive Locomotive;
31+
public readonly Simulator Simulator;
3232

3333
public bool Activated;
3434
string ScriptName = "MSTS";
@@ -155,12 +155,12 @@ public bool OverchargeButtonPressed
155155
/// <summary>
156156
/// Needed for proper mouse operation in the cabview
157157
/// </summary>
158-
public float IntermediateValue { get { return Script is MSTSBrakeController ? (Script as MSTSBrakeController).NotchController.IntermediateValue : CurrentValue; } }
158+
public float IntermediateValue { get; set; }
159159

160160
/// <summary>
161161
/// Knowing actual notch and its change is needed for proper repeatability of mouse and RailDriver operation
162162
/// </summary>
163-
public int CurrentNotch { get { return Script is MSTSBrakeController ? (Script as MSTSBrakeController).NotchController.CurrentNotch : 0; } set { } }
163+
public int CurrentNotch { get; set; }
164164

165165
public ControllerState TrainBrakeControllerState
166166
{
@@ -337,6 +337,8 @@ public void Initialize()
337337
(Script as MSTSBrakeController).ForceControllerReleaseGraduated = Simulator.Settings.GraduatedRelease;
338338
}
339339

340+
Script.AttachToHost(this);
341+
340342
// AbstractScriptClass
341343
Script.ClockTime = () => (float)Simulator.ClockTime;
342344
Script.GameTime = () => (float)Simulator.GameTime;
@@ -354,52 +356,6 @@ public void Initialize()
354356
}
355357
};
356358

357-
// BrakeController
358-
Script.EmergencyBrakingPushButton = () => EmergencyBrakingPushButton;
359-
Script.TCSEmergencyBraking = () => TCSEmergencyBraking;
360-
Script.TCSFullServiceBraking = () => TCSFullServiceBraking;
361-
Script.QuickReleaseButtonPressed = () => QuickReleaseButtonPressed;
362-
Script.OverchargeButtonPressed = () => OverchargeButtonPressed;
363-
Script.IsLowVoltagePowerSupplyOn = () => Locomotive.LocomotivePowerSupply.LowVoltagePowerSupplyOn;
364-
Script.IsCabPowerSupplyOn = () => Locomotive.LocomotivePowerSupply.CabPowerSupplyOn;
365-
366-
Script.MainReservoirPressureBar = () =>
367-
{
368-
if (Locomotive.Train != null)
369-
return Bar.FromPSI(Locomotive.MainResPressurePSI);
370-
else
371-
return float.MaxValue;
372-
};
373-
Script.MaxPressureBar = () => Bar.FromPSI(MaxPressurePSI);
374-
Script.MaxOverchargePressureBar = () => Bar.FromPSI(MaxOverchargePressurePSI);
375-
Script.ReleaseRateBarpS = () => BarpS.FromPSIpS(ReleaseRatePSIpS);
376-
Script.QuickReleaseRateBarpS = () => BarpS.FromPSIpS(QuickReleaseRatePSIpS);
377-
Script.OverchargeEliminationRateBarpS = () => BarpS.FromPSIpS(OverchargeEliminationRatePSIpS);
378-
Script.SlowApplicationRateBarpS = () => BarpS.FromPSIpS(SlowApplicationRatePSIpS);
379-
Script.ApplyRateBarpS = () => BarpS.FromPSIpS(ApplyRatePSIpS);
380-
Script.EmergencyRateBarpS = () => BarpS.FromPSIpS(EmergencyRatePSIpS);
381-
Script.FullServReductionBar = () => Bar.FromPSI(FullServReductionPSI);
382-
Script.MinReductionBar = () => Bar.FromPSI(MinReductionPSI);
383-
Script.CurrentValue = () => CurrentValue;
384-
Script.MinimumValue = () => MinimumValue;
385-
Script.MaximumValue = () => MaximumValue;
386-
Script.StepSize = () => StepSize;
387-
Script.UpdateValue = () => UpdateValue;
388-
Script.Notches = () => Notches;
389-
Script.CruiseControlBrakeDemand = () => Locomotive.CruiseControl != null ? Locomotive.CruiseControl.TrainBrakePercent/100 : 0;
390-
391-
Script.SetCurrentValue = (value) => CurrentValue = value;
392-
Script.SetUpdateValue = (value) => UpdateValue = value;
393-
394-
Script.SetDynamicBrakeIntervention = (value) =>
395-
{
396-
// TODO: Set dynamic brake intervention instead of controller position
397-
// There are some issues that need to be identified and fixed before setting the intervention directly
398-
if (Locomotive.DynamicBrakeController == null) return;
399-
Locomotive.DynamicBrakeChangeActiveState(value > 0);
400-
Locomotive.DynamicBrakeController.SetValue(value);
401-
};
402-
403359
Script.Initialize();
404360
}
405361
}
@@ -573,4 +529,4 @@ public void Restore(BinaryReader inf)
573529
TCSFullServiceBraking = inf.ReadBoolean();
574530
}
575531
}
576-
}
532+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +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.Scripting.Api;
1918
using System;
20-
using System.Diagnostics;
19+
using ORTS.Scripting.Api;
2120

2221
namespace Orts.Simulation.RollingStocks.SubSystems.Controllers
2322
{
@@ -61,13 +60,16 @@ public override void InitializeMoving()
6160
NotchController.SetValue(0);
6261
if (NotchController.NotchCount() > 0) NotchController.CurrentNotch = 0;
6362
else NotchController.CurrentNotch = -1;
63+
CurrentNotch = NotchController.CurrentNotch;
6464
}
6565

6666
public override float Update(float elapsedSeconds)
6767
{
6868
float value = NotchController.Update(elapsedSeconds);
6969
SetCurrentValue(value);
70+
IntermediateValue = NotchController.IntermediateValue;
7071
SetUpdateValue(NotchController.UpdateValue);
72+
CurrentNotch = NotchController.CurrentNotch;
7173
return value;
7274
}
7375

0 commit comments

Comments
 (0)