Skip to content

Commit 01607fd

Browse files
authored
Merge pull request #766 from peternewell/control_car_gear_controller
Control Car additional functionality
2 parents d19a96a + f1759de commit 01607fd

File tree

7 files changed

+229
-34
lines changed

7 files changed

+229
-34
lines changed

Source/Documentation/Manual/physics.rst

+4
Original file line numberDiff line numberDiff line change
@@ -2159,6 +2159,10 @@ as the compressor and main air reservoir, and some of the diesel effects (as it
21592159
Some of the cab monitoring gauges provide visibility of what is happening on the powered car. To do this OR searches for
21602160
the "closest" powered car near the Control car and uses its information.
21612161

2162+
If the Control Car is "linked" with a geared power car then it will be necessary for the control car to have a gear box
2163+
controller. To get OR to set it up, it will be necessary for the ``GearBoxControllerNumberofGears ( x )``, where x = number
2164+
of gears in the associated power car, to be added to the engine section of the ENG file.
2165+
21622166

21632167
Multiple Units of Locomotives in Same Consist
21642168
=============================================

Source/Orts.Simulation/Simulation/Physics/Train.cs

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ public int ActivityDurationS
192192
bool AuxTenderFound = false;
193193
string PrevWagonType;
194194

195+
public bool HasControlCarWithGear = false;
195196

196197
//To investigate coupler breaks on route
197198
private bool numOfCouplerBreaksNoted = false;

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

+190-6
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,22 @@
4242
using System.Text;
4343
using Event = Orts.Common.Event;
4444
using ORTS.Scripting.Api;
45+
using static Orts.Simulation.RollingStocks.MSTSDieselLocomotive;
4546

4647
namespace Orts.Simulation.RollingStocks
4748
{
4849
public class MSTSControlTrailerCar : MSTSLocomotive
4950
{
5051

52+
public int ControllerNumberOfGears = 1;
53+
bool HasGearController = false;
54+
bool ControlGearUp = false;
55+
bool ControlGearDown = false;
56+
int ControlGearIndex;
57+
int ControlGearIndication;
58+
string ControlGearBoxType;
59+
60+
5161
public MSTSControlTrailerCar(Simulator simulator, string wagFile)
5262
: base(simulator, wagFile)
5363
{
@@ -59,18 +69,25 @@ public MSTSControlTrailerCar(Simulator simulator, string wagFile)
5969
public override void LoadFromWagFile(string wagFilePath)
6070
{
6171
base.LoadFromWagFile(wagFilePath);
62-
63-
Trace.TraceInformation("Control Trailer");
64-
6572
}
6673

6774

6875
public override void Initialize()
6976
{
70-
71-
base.Initialize();
7277

73-
78+
79+
// Initialise gearbox controller
80+
if (ControllerNumberOfGears > 0)
81+
{
82+
GearBoxController = new MSTSNotchController(ControllerNumberOfGears + 1);
83+
if (Simulator.Settings.VerboseConfigurationMessages)
84+
HasGearController = true;
85+
Trace.TraceInformation("Control Car Gear Controller created");
86+
ControlGearIndex = 0;
87+
Train.HasControlCarWithGear = true;
88+
}
89+
90+
base.Initialize();
7491
}
7592

7693

@@ -97,12 +114,58 @@ public override void Parse(string lowercasetoken, STFReader stf)
97114
LocomotivePowerSupply.Parse(lowercasetoken, stf);
98115
break;
99116

117+
// to setup gearbox controller
118+
case "engine(gearboxcontrollernumberofgears": ControllerNumberOfGears = stf.ReadIntBlock(null); break;
119+
120+
100121
default:
101122
base.Parse(lowercasetoken, stf); break;
102123
}
103124

104125
}
105126

127+
/// <summary>
128+
/// This initializer is called when we are making a new copy of a locomotive already
129+
/// loaded in memory. We use this one to speed up loading by eliminating the
130+
/// need to parse the wag file multiple times.
131+
/// NOTE: you must initialize all the same variables as you parsed above
132+
/// </summary>
133+
public override void Copy(MSTSWagon copy)
134+
{
135+
136+
base.Copy(copy); // each derived level initializes its own variables
137+
138+
MSTSControlTrailerCar locoCopy = (MSTSControlTrailerCar)copy;
139+
140+
ControllerNumberOfGears = locoCopy.ControllerNumberOfGears;
141+
142+
143+
}
144+
145+
/// <summary>
146+
/// We are saving the game. Save anything that we'll need to restore the
147+
/// status later.
148+
/// </summary>
149+
public override void Save(BinaryWriter outf)
150+
{
151+
ControllerFactory.Save(GearBoxController, outf);
152+
outf.Write(ControlGearIndication);
153+
outf.Write(ControlGearIndex);
154+
}
155+
156+
/// <summary>
157+
/// We are restoring a saved game. The TrainCar class has already
158+
/// been initialized. Restore the game state.
159+
/// </summary>
160+
public override void Restore(BinaryReader inf)
161+
{
162+
base.Restore(inf);
163+
ControllerFactory.Restore(GearBoxController, inf);
164+
ControlGearIndication = inf.ReadInt32();
165+
ControlGearIndex = inf.ReadInt32();
166+
}
167+
168+
106169
/// <summary>
107170
/// Set starting conditions when initial speed > 0
108171
///
@@ -123,6 +186,68 @@ public override void Update(float elapsedClockSeconds)
123186
base.Update(elapsedClockSeconds);
124187
WheelSpeedMpS = SpeedMpS; // Set wheel speed for control car, required to make wheels go around.
125188

189+
190+
if (ControllerNumberOfGears > 0 && IsLeadLocomotive() && GearBoxController != null)
191+
{
192+
// Pass gearbox command key to other locomotives in train, don't treat the player locomotive in this fashion.
193+
// This assumes that Contol cars have been "matched" with motor cars. Also return values will be on thebasis of the last motor car in the train.
194+
195+
foreach (TrainCar car in Train.Cars)
196+
{
197+
var locog = car as MSTSDieselLocomotive;
198+
199+
if (locog != null && car != this && !locog.IsLeadLocomotive() && (ControlGearDown || ControlGearUp))
200+
{
201+
202+
if (ControlGearUp)
203+
{
204+
205+
locog.GearBoxController.CurrentNotch = GearBoxController.CurrentNotch;
206+
locog.GearBoxController.SetValue((float)locog.GearBoxController.CurrentNotch);
207+
208+
locog.ChangeGearUp();
209+
}
210+
211+
if (ControlGearDown)
212+
{
213+
214+
locog.GearBoxController.CurrentNotch = GearBoxController.CurrentNotch;
215+
locog.GearBoxController.SetValue((float)locog.GearBoxController.CurrentNotch);
216+
217+
locog.ChangeGearDown();
218+
}
219+
}
220+
221+
// Read values for the HuD and other requirements, will be based upon the last motorcar
222+
if (locog != null)
223+
{
224+
ControlGearIndex = locog.DieselEngines[0].GearBox.CurrentGearIndex;
225+
ControlGearIndication = locog.DieselEngines[0].GearBox.GearIndication;
226+
if (locog.DieselEngines[0].GearBox.GearBoxType == TypesGearBox.C)
227+
{
228+
ControlGearBoxType = "C";
229+
}
230+
}
231+
}
232+
233+
234+
// Rest gear flags once all the cars have been processed
235+
ControlGearUp = false;
236+
ControlGearDown = false;
237+
238+
}
239+
240+
}
241+
242+
public override string GetStatus()
243+
{
244+
var status = new StringBuilder();
245+
if (HasGearController)
246+
status.AppendFormat("{0} = {1}\n", Simulator.Catalog.GetString("Gear"),
247+
ControlGearIndex < 0 ? Simulator.Catalog.GetParticularString("Gear", "N") : (ControlGearIndication).ToString());
248+
status.AppendLine();
249+
250+
return status.ToString();
126251
}
127252

128253
/// <summary>
@@ -148,10 +273,69 @@ protected override void UpdateSoundVariables(float elapsedClockSeconds)
148273
}
149274

150275

276+
public override void ChangeGearUp()
277+
{
151278

279+
if (ControlGearBoxType == "C")
280+
{
281+
if (ThrottlePercent == 0)
282+
{
283+
GearBoxController.CurrentNotch += 1;
284+
}
285+
else
286+
{
287+
Simulator.Confirmer.Message(ConfirmLevel.Warning, Simulator.Catalog.GetString("Throttle must be reduced to Idle before gear change can happen."));
288+
}
289+
}
290+
else
291+
{
292+
GearBoxController.CurrentNotch += 1;
293+
}
152294

295+
if (GearBoxController.CurrentNotch > ControllerNumberOfGears)
296+
{
297+
GearBoxController.CurrentNotch = ControllerNumberOfGears;
298+
}
299+
else if (GearBoxController.CurrentNotch < 0)
300+
{
301+
GearBoxController.CurrentNotch = 0;
302+
}
303+
304+
ControlGearUp = true;
305+
ControlGearDown = false;
306+
307+
}
308+
309+
public override void ChangeGearDown()
310+
{
311+
if (ControlGearBoxType == "C")
312+
{
313+
if (ThrottlePercent == 0)
314+
{
315+
GearBoxController.CurrentNotch -= 1;
316+
}
317+
else
318+
{
319+
Simulator.Confirmer.Message(ConfirmLevel.Warning, Simulator.Catalog.GetString("Throttle must be reduced to Idle before gear change can happen."));
320+
}
321+
}
322+
else
323+
{
324+
GearBoxController.CurrentNotch -= 1;
325+
}
153326

327+
if (GearBoxController.CurrentNotch > ControllerNumberOfGears)
328+
{
329+
GearBoxController.CurrentNotch = ControllerNumberOfGears;
330+
}
331+
else if (GearBoxController.CurrentNotch < 0)
332+
{
333+
GearBoxController.CurrentNotch = 0;
334+
}
154335

336+
ControlGearUp = false;
337+
ControlGearDown = true;
155338

339+
}
156340
}
157341
}

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

+29-25
Original file line numberDiff line numberDiff line change
@@ -1525,8 +1525,8 @@ public override void Initialize()
15251525
// Initialise Brake Pipe Quick Charging Rate
15261526
if (BrakePipeQuickChargingRatePSIpS == 0) BrakePipeQuickChargingRatePSIpS = BrakePipeChargingRatePSIorInHgpS;
15271527

1528-
// Initialise Exhauster Charging rate in diesel and electric locomotives. The equivalent ejector charging rates are set in the steam locomotive.
1529-
if (this is MSTSDieselLocomotive || this is MSTSElectricLocomotive)
1528+
// Initialise Exhauster Charging rate in diesel, control cars and electric locomotives. The equivalent ejector charging rates are set in the steam locomotive.
1529+
if (this is MSTSDieselLocomotive || this is MSTSElectricLocomotive || this is MSTSControlTrailerCar)
15301530
{
15311531
ExhausterHighSBPChargingRatePSIorInHgpS = BrakePipeChargingRatePSIorInHgpS;
15321532
ExhausterLowSBPChargingRatePSIorInHgpS = BrakePipeChargingRatePSIorInHgpS / 5.0f; // Low speed exhauster setting is 1/5 of high speed
@@ -1878,42 +1878,47 @@ public override void Update(float elapsedClockSeconds)
18781878
var gearloco = this as MSTSDieselLocomotive;
18791879

18801880
// Pass Gearbox commands
1881+
1882+
1883+
1884+
18811885
// Note - at the moment there is only one GearBox Controller created, but a gearbox for each diesel engine is created.
18821886
// This code keeps all gearboxes in the locomotive aligned with the first engine and gearbox.
18831887
if (gearloco != null && gearloco.DieselTransmissionType == MSTSDieselLocomotive.DieselTransmissionTypes.Mechanic && GearBoxController.CurrentNotch != previousChangedGearBoxNotch)
18841888
{
1885-
// pass gearbox command key to other gearboxes in the same locomotive, only do the current locomotive
1889+
// pass gearbox command key to other gearboxes in the same locomotive, only do the current locomotive
18861890

1887-
if (gearloco == this)
1891+
int ii = 0;
1892+
foreach (var eng in gearloco.DieselEngines.DEList)
18881893
{
1889-
1890-
int ii = 0;
1891-
foreach (var eng in gearloco.DieselEngines.DEList)
1894+
// don't change the first engine as this is the reference for all the others
1895+
if (ii != 0)
18921896
{
1893-
// don't change the first engine as this is the reference for all the others
1894-
if (ii != 0)
1895-
{
1896-
gearloco.DieselEngines[ii].GearBox.currentGearIndex = gearloco.DieselEngines[0].GearBox.CurrentGearIndex;
1897-
}
1898-
1899-
ii = ii + 1;
1897+
gearloco.DieselEngines[ii].GearBox.currentGearIndex = gearloco.DieselEngines[0].GearBox.CurrentGearIndex;
19001898
}
1899+
1900+
ii = ii + 1;
19011901
}
19021902

1903-
// pass gearbox command key to other locomotives in train, don't treat the player locomotive in this fashion.
1903+
}
1904+
1905+
// The lead locomotive passes gearbox commands position to other locomotives in train, don't treat the player locomotive in this fashion.
1906+
1907+
if (gearloco != null && gearloco.DieselTransmissionType == MSTSDieselLocomotive.DieselTransmissionTypes.Mechanic && GearBoxController.CurrentNotch != previousChangedGearBoxNotch && IsLeadLocomotive())
1908+
{
1909+
19041910
foreach (TrainCar car in Train.Cars)
19051911
{
1906-
var dieselloco = this as MSTSDieselLocomotive;
19071912
var locog = car as MSTSDieselLocomotive;
19081913

1909-
if (locog != null && dieselloco != null && car != this && !locog.IsLeadLocomotive())
1914+
if (locog != null && gearloco != null && car != this && !locog.IsLeadLocomotive())
19101915
{
19111916

1912-
locog.DieselEngines[0].GearBox.currentGearIndex = dieselloco.DieselEngines[0].GearBox.CurrentGearIndex;
1917+
locog.DieselEngines[0].GearBox.currentGearIndex = gearloco.DieselEngines[0].GearBox.CurrentGearIndex;
19131918

1914-
locog.GearBoxController.CurrentNotch = dieselloco.DieselEngines[0].GearBox.CurrentGearIndex + 1;
1915-
locog.GearboxGearIndex = dieselloco.DieselEngines[0].GearBox.CurrentGearIndex + 1;
1916-
locog.GearBoxController.SetValue((float)dieselloco.GearBoxController.CurrentNotch);
1919+
locog.GearBoxController.CurrentNotch = gearloco.DieselEngines[0].GearBox.CurrentGearIndex + 1;
1920+
locog.GearboxGearIndex = gearloco.DieselEngines[0].GearBox.CurrentGearIndex + 1;
1921+
locog.GearBoxController.SetValue((float)gearloco.GearBoxController.CurrentNotch);
19171922

19181923
locog.Simulator.Confirmer.ConfirmWithPerCent(CabControl.GearBox, CabSetting.Increase, locog.GearBoxController.CurrentNotch);
19191924
locog.AlerterReset(TCSEvent.GearBoxChanged);
@@ -4013,16 +4018,16 @@ public float GetCombinedHandleValue(bool intermediateValue)
40134018
#region GearBoxController
40144019
public virtual void ChangeGearUp()
40154020
{
4021+
40164022
}
40174023

40184024
public virtual void StartGearBoxIncrease()
40194025
{
40204026
if (GearBoxController != null)
40214027
{
4022-
4028+
40234029
if (this is MSTSDieselLocomotive)
40244030
{
4025-
40264031
var dieselloco = this as MSTSDieselLocomotive;
40274032

40284033
if (dieselloco.DieselTransmissionType == MSTSDieselLocomotive.DieselTransmissionTypes.Mechanic)
@@ -4078,6 +4083,7 @@ public virtual void StopGearBoxIncrease()
40784083

40794084
public virtual void ChangeGearDown()
40804085
{
4086+
40814087
}
40824088

40834089
public virtual void StartGearBoxDecrease()
@@ -4105,7 +4111,6 @@ public virtual void StartGearBoxDecrease()
41054111
if (ThrottlePercent == 0)
41064112
{
41074113
GearBoxController.StartDecrease();
4108-
Trace.TraceInformation("Controller Decrease - Current Notch {0} Indication {1} GearIndex {2}", GearBoxController.CurrentNotch, dieselloco.DieselEngines[0].GearBox.GearIndication, dieselloco.DieselEngines[0].GearBox.CurrentGearIndex);
41094114
Simulator.Confirmer.ConfirmWithPerCent(CabControl.GearBox, CabSetting.Decrease, dieselloco.DieselEngines[0].GearBox.GearIndication);
41104115
AlerterReset(TCSEvent.GearBoxChanged);
41114116
SignalGearBoxChangeEvents();
@@ -4127,7 +4132,6 @@ public virtual void StartGearBoxDecrease()
41274132
}
41284133
}
41294134
}
4130-
41314135
ChangeGearDown();
41324136
}
41334137

0 commit comments

Comments
 (0)