Skip to content

Commit

Permalink
Correct gear operation from Control Car
Browse files Browse the repository at this point in the history
  • Loading branch information
peternewell committed Jan 15, 2023
1 parent 6d52299 commit f143fcc
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 23 deletions.
1 change: 1 addition & 0 deletions Source/Orts.Simulation/Simulation/Physics/Train.cs
Expand Up @@ -192,6 +192,7 @@ public int ActivityDurationS
bool AuxTenderFound = false;
string PrevWagonType;

public bool HasControlCarWithGear = false;

//To investigate coupler breaks on route
private bool numOfCouplerBreaksNoted = false;
Expand Down
133 changes: 115 additions & 18 deletions Source/Orts.Simulation/Simulation/RollingStocks/MSTSControlTrailerCar.cs
Expand Up @@ -49,7 +49,12 @@ namespace Orts.Simulation.RollingStocks
public class MSTSControlTrailerCar : MSTSLocomotive
{

public int ControlGearBoxNumberOfGears = 1;
public int ControllerNumberOfGears = 1;
bool HasGearController = false;
bool ControlGearUp = false;
bool ControlGearDown = false;
int ControlGearIndex;
int ControlGearIndication;


public MSTSControlTrailerCar(Simulator simulator, string wagFile)
Expand All @@ -63,18 +68,25 @@ public MSTSControlTrailerCar(Simulator simulator, string wagFile)
public override void LoadFromWagFile(string wagFilePath)
{
base.LoadFromWagFile(wagFilePath);

Trace.TraceInformation("Control Trailer");

}


public override void Initialize()
{

base.Initialize();



// Initialise gearbox controller
if (ControllerNumberOfGears > 0)
{
GearBoxController = new MSTSNotchController(ControllerNumberOfGears + 1);
if (Simulator.Settings.VerboseConfigurationMessages)
HasGearController = true;
Trace.TraceInformation("Control Car Gear Controller created");
ControlGearIndex = 0;
Train.HasControlCarWithGear = true;
}

base.Initialize();
}


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

// to setup gearbox controller
case "engine(gearboxnumberofgears": ControlGearBoxNumberOfGears = stf.ReadIntBlock(1); break;
case "engine(gearboxcontrollernumberofgears": ControllerNumberOfGears = stf.ReadIntBlock(null); break;


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

MSTSControlTrailerCar locoCopy = (MSTSControlTrailerCar)copy;

ControlGearBoxNumberOfGears = locoCopy.ControlGearBoxNumberOfGears;
ControllerNumberOfGears = locoCopy.ControllerNumberOfGears;


}
Expand All @@ -135,6 +147,8 @@ public override void Copy(MSTSWagon copy)
public override void Save(BinaryWriter outf)
{
ControllerFactory.Save(GearBoxController, outf);
outf.Write(ControlGearIndication);
outf.Write(ControlGearIndex);
}

/// <summary>
Expand All @@ -145,7 +159,8 @@ public override void Restore(BinaryReader inf)
{
base.Restore(inf);
ControllerFactory.Restore(GearBoxController, inf);

ControlGearIndication = inf.ReadInt32();
ControlGearIndex = inf.ReadInt32();
}


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

ThrottleController.SetValue(Train.MUThrottlePercent / 100);

// Initialise gearbox controller
if (ControlGearBoxNumberOfGears > 0)
{
GearBoxController = new MSTSNotchController(ControlGearBoxNumberOfGears + 1);
}


}

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


if (ControllerNumberOfGears > 0 && IsLeadLocomotive())
{
// pass gearbox command key to other locomotives in train, don't treat the player locomotive in this fashion.
foreach (TrainCar car in Train.Cars)
{


var locog = car as MSTSDieselLocomotive;

if (locog != null && car != this && !locog.IsLeadLocomotive() && GearBoxController != null)
{

if (ControlGearUp)
{

locog.GearBoxController.CurrentNotch = GearBoxController.CurrentNotch;
locog.GearBoxController.SetValue((float)locog.GearBoxController.CurrentNotch);

locog.ChangeGearUp();

ControlGearUp = false;
}


if (ControlGearDown)
{

locog.GearBoxController.CurrentNotch = GearBoxController.CurrentNotch;
locog.GearBoxController.SetValue((float)locog.GearBoxController.CurrentNotch);

locog.ChangeGearDown();

ControlGearDown = false;
}

// Read values for the HuD
ControlGearIndex = locog.DieselEngines[0].GearBox.CurrentGearIndex;
ControlGearIndication = locog.DieselEngines[0].GearBox.GearIndication;
}

}
}

}

public override string GetStatus()
{
var status = new StringBuilder();
if (HasGearController)
status.AppendFormat("{0} = {1}\n", Simulator.Catalog.GetString("Gear"),
ControlGearIndex < 0 ? Simulator.Catalog.GetParticularString("Gear", "N") : (ControlGearIndication).ToString());
status.AppendLine();

return status.ToString();
}

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


public override void ChangeGearUp()
{

GearBoxController.CurrentNotch += 1;

if(GearBoxController.CurrentNotch > ControllerNumberOfGears)
{
GearBoxController.CurrentNotch = ControllerNumberOfGears;
}
else if (GearBoxController.CurrentNotch < 0)
{
GearBoxController.CurrentNotch = 0;
}

ControlGearUp = true;
ControlGearDown = false;

}

public override void ChangeGearDown()
{
GearBoxController.CurrentNotch -= 1;

if (GearBoxController.CurrentNotch > ControllerNumberOfGears)
{
GearBoxController.CurrentNotch = ControllerNumberOfGears;
}
else if (GearBoxController.CurrentNotch < 0)
{
GearBoxController.CurrentNotch = 0;
}

ControlGearUp = false;
ControlGearDown = true;

}



Expand Down
Expand Up @@ -1830,7 +1830,7 @@ public override void Update(float elapsedClockSeconds)
// Pass Gearbox commands
// Note - at the moment there is only one GearBox Controller created, but a gearbox for each diesel engine is created.
// This code keeps all gearboxes in the locomotive aligned with the first engine and gearbox.
if (gearloco != null && gearloco.DieselTransmissionType == MSTSDieselLocomotive.DieselTransmissionTypes.Mechanic && GearBoxController.CurrentNotch != previousChangedGearBoxNotch)
if (gearloco != null && gearloco.DieselTransmissionType == MSTSDieselLocomotive.DieselTransmissionTypes.Mechanic && GearBoxController.CurrentNotch != previousChangedGearBoxNotch && IsLeadLocomotive())
{
// pass gearbox command key to other gearboxes in the same locomotive, only do the current locomotive

Expand Down Expand Up @@ -3625,6 +3625,7 @@ public float GetCombinedHandleValue(bool intermediateValue)
#region GearBoxController
public virtual void ChangeGearUp()
{

}

public virtual void StartGearBoxIncrease()
Expand Down Expand Up @@ -3690,6 +3691,7 @@ public virtual void StopGearBoxIncrease()

public virtual void ChangeGearDown()
{

}

public virtual void StartGearBoxDecrease()
Expand Down Expand Up @@ -3717,7 +3719,6 @@ public virtual void StartGearBoxDecrease()
if (ThrottlePercent == 0)
{
GearBoxController.StartDecrease();
Trace.TraceInformation("Controller Decrease - Current Notch {0} Indication {1} GearIndex {2}", GearBoxController.CurrentNotch, dieselloco.DieselEngines[0].GearBox.GearIndication, dieselloco.DieselEngines[0].GearBox.CurrentGearIndex);
Simulator.Confirmer.ConfirmWithPerCent(CabControl.GearBox, CabSetting.Decrease, dieselloco.DieselEngines[0].GearBox.GearIndication);
AlerterReset(TCSEvent.GearBoxChanged);
SignalGearBoxChangeEvents();
Expand All @@ -3739,7 +3740,6 @@ public virtual void StartGearBoxDecrease()
}
}
}

ChangeGearDown();
}

Expand Down
Expand Up @@ -1562,7 +1562,7 @@ public void Update(float elapsedClockSeconds)

if (GearBox != null)
{
if ((Locomotive.IsLeadLocomotive()))
if ((Locomotive.IsLeadLocomotive()) || Locomotive.Train.HasControlCarWithGear)
{
if (GearBox.GearBoxOperation == GearBoxOperation.Manual)
{
Expand All @@ -1577,6 +1577,7 @@ public void Update(float elapsedClockSeconds)
if (GearBox.GearBoxOperation == GearBoxOperation.Manual)
{
if (Locomotive.GearboxGearIndex > 0)

GearBox.NextGear = GearBox.Gears[Locomotive.GearboxGearIndex - 1];
else
GearBox.NextGear = null;
Expand Down
Expand Up @@ -271,7 +271,7 @@ public Gear NextGear
}
set
{
switch(GearBoxOperation)
switch (GearBoxOperation)
{
case GearBoxOperation.Manual:
case GearBoxOperation.Semiautomatic:
Expand Down

0 comments on commit f143fcc

Please sign in to comment.