Skip to content

Commit f143fcc

Browse files
committed
Correct gear operation from Control Car
1 parent 6d52299 commit f143fcc

File tree

5 files changed

+122
-23
lines changed

5 files changed

+122
-23
lines changed

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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 115 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ namespace Orts.Simulation.RollingStocks
4949
public class MSTSControlTrailerCar : MSTSLocomotive
5050
{
5151

52-
public int ControlGearBoxNumberOfGears = 1;
52+
public int ControllerNumberOfGears = 1;
53+
bool HasGearController = false;
54+
bool ControlGearUp = false;
55+
bool ControlGearDown = false;
56+
int ControlGearIndex;
57+
int ControlGearIndication;
5358

5459

5560
public MSTSControlTrailerCar(Simulator simulator, string wagFile)
@@ -63,18 +68,25 @@ public MSTSControlTrailerCar(Simulator simulator, string wagFile)
6368
public override void LoadFromWagFile(string wagFilePath)
6469
{
6570
base.LoadFromWagFile(wagFilePath);
66-
67-
Trace.TraceInformation("Control Trailer");
68-
6971
}
7072

7173

7274
public override void Initialize()
7375
{
74-
75-
base.Initialize();
7676

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

8092

@@ -101,7 +113,7 @@ public override void Parse(string lowercasetoken, STFReader stf)
101113
break;
102114

103115
// to setup gearbox controller
104-
case "engine(gearboxnumberofgears": ControlGearBoxNumberOfGears = stf.ReadIntBlock(1); break;
116+
case "engine(gearboxcontrollernumberofgears": ControllerNumberOfGears = stf.ReadIntBlock(null); break;
105117

106118

107119
default:
@@ -123,7 +135,7 @@ public override void Copy(MSTSWagon copy)
123135

124136
MSTSControlTrailerCar locoCopy = (MSTSControlTrailerCar)copy;
125137

126-
ControlGearBoxNumberOfGears = locoCopy.ControlGearBoxNumberOfGears;
138+
ControllerNumberOfGears = locoCopy.ControllerNumberOfGears;
127139

128140

129141
}
@@ -135,6 +147,8 @@ public override void Copy(MSTSWagon copy)
135147
public override void Save(BinaryWriter outf)
136148
{
137149
ControllerFactory.Save(GearBoxController, outf);
150+
outf.Write(ControlGearIndication);
151+
outf.Write(ControlGearIndex);
138152
}
139153

140154
/// <summary>
@@ -145,7 +159,8 @@ public override void Restore(BinaryReader inf)
145159
{
146160
base.Restore(inf);
147161
ControllerFactory.Restore(GearBoxController, inf);
148-
162+
ControlGearIndication = inf.ReadInt32();
163+
ControlGearIndex = inf.ReadInt32();
149164
}
150165

151166

@@ -159,14 +174,6 @@ public override void InitializeMoving()
159174
WheelSpeedMpS = SpeedMpS;
160175

161176
ThrottleController.SetValue(Train.MUThrottlePercent / 100);
162-
163-
// Initialise gearbox controller
164-
if (ControlGearBoxNumberOfGears > 0)
165-
{
166-
GearBoxController = new MSTSNotchController(ControlGearBoxNumberOfGears + 1);
167-
}
168-
169-
170177
}
171178

172179
/// <summary>
@@ -177,6 +184,61 @@ public override void Update(float elapsedClockSeconds)
177184
base.Update(elapsedClockSeconds);
178185
WheelSpeedMpS = SpeedMpS; // Set wheel speed for control car, required to make wheels go around.
179186

187+
188+
if (ControllerNumberOfGears > 0 && IsLeadLocomotive())
189+
{
190+
// pass gearbox command key to other locomotives in train, don't treat the player locomotive in this fashion.
191+
foreach (TrainCar car in Train.Cars)
192+
{
193+
194+
195+
var locog = car as MSTSDieselLocomotive;
196+
197+
if (locog != null && car != this && !locog.IsLeadLocomotive() && GearBoxController != null)
198+
{
199+
200+
if (ControlGearUp)
201+
{
202+
203+
locog.GearBoxController.CurrentNotch = GearBoxController.CurrentNotch;
204+
locog.GearBoxController.SetValue((float)locog.GearBoxController.CurrentNotch);
205+
206+
locog.ChangeGearUp();
207+
208+
ControlGearUp = false;
209+
}
210+
211+
212+
if (ControlGearDown)
213+
{
214+
215+
locog.GearBoxController.CurrentNotch = GearBoxController.CurrentNotch;
216+
locog.GearBoxController.SetValue((float)locog.GearBoxController.CurrentNotch);
217+
218+
locog.ChangeGearDown();
219+
220+
ControlGearDown = false;
221+
}
222+
223+
// Read values for the HuD
224+
ControlGearIndex = locog.DieselEngines[0].GearBox.CurrentGearIndex;
225+
ControlGearIndication = locog.DieselEngines[0].GearBox.GearIndication;
226+
}
227+
228+
}
229+
}
230+
231+
}
232+
233+
public override string GetStatus()
234+
{
235+
var status = new StringBuilder();
236+
if (HasGearController)
237+
status.AppendFormat("{0} = {1}\n", Simulator.Catalog.GetString("Gear"),
238+
ControlGearIndex < 0 ? Simulator.Catalog.GetParticularString("Gear", "N") : (ControlGearIndication).ToString());
239+
status.AppendLine();
240+
241+
return status.ToString();
180242
}
181243

182244
/// <summary>
@@ -202,7 +264,42 @@ protected override void UpdateSoundVariables(float elapsedClockSeconds)
202264
}
203265

204266

267+
public override void ChangeGearUp()
268+
{
269+
270+
GearBoxController.CurrentNotch += 1;
205271

272+
if(GearBoxController.CurrentNotch > ControllerNumberOfGears)
273+
{
274+
GearBoxController.CurrentNotch = ControllerNumberOfGears;
275+
}
276+
else if (GearBoxController.CurrentNotch < 0)
277+
{
278+
GearBoxController.CurrentNotch = 0;
279+
}
280+
281+
ControlGearUp = true;
282+
ControlGearDown = false;
283+
284+
}
285+
286+
public override void ChangeGearDown()
287+
{
288+
GearBoxController.CurrentNotch -= 1;
289+
290+
if (GearBoxController.CurrentNotch > ControllerNumberOfGears)
291+
{
292+
GearBoxController.CurrentNotch = ControllerNumberOfGears;
293+
}
294+
else if (GearBoxController.CurrentNotch < 0)
295+
{
296+
GearBoxController.CurrentNotch = 0;
297+
}
298+
299+
ControlGearUp = false;
300+
ControlGearDown = true;
301+
302+
}
206303

207304

208305

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,7 +1830,7 @@ public override void Update(float elapsedClockSeconds)
18301830
// Pass Gearbox commands
18311831
// Note - at the moment there is only one GearBox Controller created, but a gearbox for each diesel engine is created.
18321832
// This code keeps all gearboxes in the locomotive aligned with the first engine and gearbox.
1833-
if (gearloco != null && gearloco.DieselTransmissionType == MSTSDieselLocomotive.DieselTransmissionTypes.Mechanic && GearBoxController.CurrentNotch != previousChangedGearBoxNotch)
1833+
if (gearloco != null && gearloco.DieselTransmissionType == MSTSDieselLocomotive.DieselTransmissionTypes.Mechanic && GearBoxController.CurrentNotch != previousChangedGearBoxNotch && IsLeadLocomotive())
18341834
{
18351835
// pass gearbox command key to other gearboxes in the same locomotive, only do the current locomotive
18361836

@@ -3625,6 +3625,7 @@ public float GetCombinedHandleValue(bool intermediateValue)
36253625
#region GearBoxController
36263626
public virtual void ChangeGearUp()
36273627
{
3628+
36283629
}
36293630

36303631
public virtual void StartGearBoxIncrease()
@@ -3690,6 +3691,7 @@ public virtual void StopGearBoxIncrease()
36903691

36913692
public virtual void ChangeGearDown()
36923693
{
3694+
36933695
}
36943696

36953697
public virtual void StartGearBoxDecrease()
@@ -3717,7 +3719,6 @@ public virtual void StartGearBoxDecrease()
37173719
if (ThrottlePercent == 0)
37183720
{
37193721
GearBoxController.StartDecrease();
3720-
Trace.TraceInformation("Controller Decrease - Current Notch {0} Indication {1} GearIndex {2}", GearBoxController.CurrentNotch, dieselloco.DieselEngines[0].GearBox.GearIndication, dieselloco.DieselEngines[0].GearBox.CurrentGearIndex);
37213722
Simulator.Confirmer.ConfirmWithPerCent(CabControl.GearBox, CabSetting.Decrease, dieselloco.DieselEngines[0].GearBox.GearIndication);
37223723
AlerterReset(TCSEvent.GearBoxChanged);
37233724
SignalGearBoxChangeEvents();
@@ -3739,7 +3740,6 @@ public virtual void StartGearBoxDecrease()
37393740
}
37403741
}
37413742
}
3742-
37433743
ChangeGearDown();
37443744
}
37453745

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@ public void Update(float elapsedClockSeconds)
15621562

15631563
if (GearBox != null)
15641564
{
1565-
if ((Locomotive.IsLeadLocomotive()))
1565+
if ((Locomotive.IsLeadLocomotive()) || Locomotive.Train.HasControlCarWithGear)
15661566
{
15671567
if (GearBox.GearBoxOperation == GearBoxOperation.Manual)
15681568
{
@@ -1577,6 +1577,7 @@ public void Update(float elapsedClockSeconds)
15771577
if (GearBox.GearBoxOperation == GearBoxOperation.Manual)
15781578
{
15791579
if (Locomotive.GearboxGearIndex > 0)
1580+
15801581
GearBox.NextGear = GearBox.Gears[Locomotive.GearboxGearIndex - 1];
15811582
else
15821583
GearBox.NextGear = null;

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerTransmissions/GearBox.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public Gear NextGear
271271
}
272272
set
273273
{
274-
switch(GearBoxOperation)
274+
switch (GearBoxOperation)
275275
{
276276
case GearBoxOperation.Manual:
277277
case GearBoxOperation.Semiautomatic:

0 commit comments

Comments
 (0)