diff --git a/source/ObjectViewer/Trains/NearestTrain.cs b/source/ObjectViewer/Trains/NearestTrain.cs
index afb6defdc..913247656 100644
--- a/source/ObjectViewer/Trains/NearestTrain.cs
+++ b/source/ObjectViewer/Trains/NearestTrain.cs
@@ -46,17 +46,7 @@ static NearestTrain()
private static TrainBase CreateDummyTrain()
{
TrainBase train = new TrainBase(TrainState.Available, TrainType.LocalPlayerTrain);
- train.Handles.Power = new PowerHandle(Specs.PowerNotches, train);
- if (Specs.IsAirBrake)
- {
- train.Handles.Brake = new AirBrakeHandle(train);
- }
- else
- {
- train.Handles.Brake = new BrakeHandle(Specs.BrakeNotches, null, train);
- train.Handles.HasHoldBrake = Specs.HasHoldBrake;
- }
- train.Handles.HoldBrake = new HoldBrakeHandle(train);
+
train.Specs.HasConstSpeed = Specs.HasConstSpeed;
Array.Resize(ref train.Cars, Specs.NumberOfCars);
@@ -88,6 +78,16 @@ private static TrainBase CreateDummyTrain()
train.Cars[i].Doors[1] = new Door(1, 1000.0, 0.0);
}
+ train.Handles.Power = new PowerHandle(Specs.PowerNotches, train);
+ if (Specs.IsAirBrake)
+ {
+ train.Handles.Brake = new AirBrakeHandle(train);
+ }
+ else
+ {
+ train.Handles.Brake = new BrakeHandle(Specs.BrakeNotches, null, train);
+ train.Handles.HasHoldBrake = Specs.HasHoldBrake;
+ }
if (Specs.HasLocoBrake)
{
train.Handles.HasLocoBrake = true;
diff --git a/source/Plugins/Route.Bve5/Components/ScriptedTrain.cs b/source/Plugins/Route.Bve5/Components/ScriptedTrain.cs
index 7c1fac361..a2ed46e85 100644
--- a/source/Plugins/Route.Bve5/Components/ScriptedTrain.cs
+++ b/source/Plugins/Route.Bve5/Components/ScriptedTrain.cs
@@ -186,7 +186,6 @@ private static void LoadScriptedTrain(string FileName, bool PreviewOnly, MapData
TrainManager.Trains.ScriptedTrain Train = new TrainManager.Trains.ScriptedTrain(TrainState.Pending);
Train.Cars = new CarBase[OtherTrain.CarObjects.Count];
- Train.Handles.Reverser = new ReverserHandle(Train);
for (int i = 0; i < Train.Cars.Length; i++)
{
diff --git a/source/Plugins/Train.MsTs/Train/ConsistParser.cs b/source/Plugins/Train.MsTs/Train/ConsistParser.cs
index b7d765583..cfcba076b 100644
--- a/source/Plugins/Train.MsTs/Train/ConsistParser.cs
+++ b/source/Plugins/Train.MsTs/Train/ConsistParser.cs
@@ -61,14 +61,6 @@ internal void ReadConsist(string fileName, ref AbstractTrain parsedTrain)
{
throw new Exception();
}
- train.Handles.Reverser = new ReverserHandle(train);
- train.Handles.EmergencyBrake = new EmergencyHandle(train);
- train.Handles.Power = new PowerHandle(8, train);
- train.Handles.Brake = new BrakeHandle(8, train.Handles.EmergencyBrake, train);
- train.Handles.LocoBrake = new LocoBrakeHandle(0, train.Handles.EmergencyBrake, train);
- train.Handles.LocoBrakeType = LocoBrakeType.Independant;
- train.Handles.HasLocoBrake = false;
- train.Handles.HoldBrake = new HoldBrakeHandle(train);
train.Specs.AveragesPressureDistribution = true;
train.SafetySystems.Headlights = new LightSource(train, 2);
if(Directory.Exists(Plugin.FileSystem.MSTSDirectory))
diff --git a/source/Plugins/Train.OpenBve/Train/BVE/TrainDatParser.cs b/source/Plugins/Train.OpenBve/Train/BVE/TrainDatParser.cs
index 6701c8629..dbf0e548d 100644
--- a/source/Plugins/Train.OpenBve/Train/BVE/TrainDatParser.cs
+++ b/source/Plugins/Train.OpenBve/Train/BVE/TrainDatParser.cs
@@ -1023,9 +1023,28 @@ internal void Parse(string FileName, Encoding Encoding, TrainBase Train) {
Plugin.CurrentHost.AddMessage(MessageType.Error, false, "TrailerCarMass is expected to be positive in " + FileName);
TrailerCarMass = 1.0;
}
-
}
-
+
+ // apply data (need to set driver car first to ensure handles are assigned to the correct place)
+ if (MotorCars < 1) MotorCars = 1;
+ if (TrailerCars < 0) TrailerCars = 0;
+ int Cars = MotorCars + TrailerCars;
+ Train.Cars = new CarBase[Cars];
+ for (int i = 0; i < Train.Cars.Length; i++)
+ {
+ Train.Cars[i] = new CarBase(Train, i, CoefficientOfStaticFriction, CoefficientOfRollingResistance, AerodynamicDragCoefficient);
+ }
+ double DistanceBetweenTheCars = 0.3;
+
+ if (DriverCar < 0 || DriverCar >= Cars)
+ {
+ Plugin.CurrentHost.AddMessage(MessageType.Error, false, "DriverCar must point to an existing car in " + FileName);
+ }
+ else
+ {
+ Train.DriverCar = DriverCar;
+ }
+
if (powerNotches == 0)
{
Plugin.CurrentHost.AddMessage(MessageType.Error, false, "NumberOfPowerNotches was not set in " + FileName);
@@ -1078,24 +1097,7 @@ internal void Parse(string FileName, Encoding Encoding, TrainBase Train) {
Train.Handles.LocoBrake = new LocoBrakeHandle(locoBrakeNotches, Train.Handles.EmergencyBrake, locoBrakeDelayUp, locoBrakeDelayDown, Train);
}
Train.Handles.LocoBrakeType = (LocoBrakeType)locoBrakeType;
- Train.Handles.HoldBrake = new HoldBrakeHandle(Train);
- // apply data
- if (MotorCars < 1) MotorCars = 1;
- if (TrailerCars < 0) TrailerCars = 0;
- int Cars = MotorCars + TrailerCars;
- Train.Cars = new CarBase[Cars];
- for (int i = 0; i < Train.Cars.Length; i++)
- {
- Train.Cars[i] = new CarBase(Train, i, CoefficientOfStaticFriction, CoefficientOfRollingResistance, AerodynamicDragCoefficient);
- }
- double DistanceBetweenTheCars = 0.3;
- if (DriverCar < 0 | DriverCar >= Cars) {
- Plugin.CurrentHost.AddMessage(MessageType.Error, false, "DriverCar must point to an existing car in " + FileName);
- DriverCar = 0;
-
- }
- Train.DriverCar = DriverCar;
// brake system
double OperatingPressure;
if (BrakePipePressure <= 0.0) {
diff --git a/source/TrainManager/Car/CarBase.cs b/source/TrainManager/Car/CarBase.cs
index 38f42d734..e569b6284 100644
--- a/source/TrainManager/Car/CarBase.cs
+++ b/source/TrainManager/Car/CarBase.cs
@@ -42,6 +42,9 @@ public class CarBase : AbstractCar
public Door[] Doors;
/// The horns attached to this car
public Horn[] Horns;
+ /// The cab handles
+ /// This property will be null if the vehicle does not posses a cab
+ public CabHandles Handles;
/// Contains the physics properties for the car
public readonly CarPhysics Specs;
/// The car brake for this car
@@ -443,9 +446,8 @@ public override void Uncouple(bool Front, bool Rear)
// Create new train
TrainBase newTrain = new TrainBase(TrainState.Available, TrainType.StaticCars);
UncouplingBehaviour uncouplingBehaviour = UncouplingBehaviour.Emergency;
- newTrain.Handles.Power = new PowerHandle(0, newTrain);
- newTrain.Handles.Brake = new BrakeHandle(0, newTrain.Handles.EmergencyBrake, newTrain);
- newTrain.Handles.HoldBrake = new HoldBrakeHandle(newTrain);
+ newTrain.Cars[0].Handles = new CabHandles(newTrain);
+
if (Front)
{
int totalPreceedingCars = trainCarIndex;
diff --git a/source/TrainManager/Handles/CabHandles.cs b/source/TrainManager/Handles/CabHandles.cs
index 64c4675c3..a032577d0 100644
--- a/source/TrainManager/Handles/CabHandles.cs
+++ b/source/TrainManager/Handles/CabHandles.cs
@@ -1,12 +1,13 @@
using OpenBveApi.Interface;
+using TrainManager.Trains;
namespace TrainManager.Handles
{
/// The cab handles (controls) of a train
- public struct CabHandles
+ public class CabHandles
{
/// The Reverser
- public ReverserHandle Reverser;
+ public readonly ReverserHandle Reverser;
/// The Power
public AbstractHandle Power;
/// The Brake
@@ -14,9 +15,9 @@ public struct CabHandles
/// The Loco brake handle
public AbstractHandle LocoBrake;
/// The Emergency Brake
- public EmergencyHandle EmergencyBrake;
+ public readonly EmergencyHandle EmergencyBrake;
/// The Hold Brake
- public HoldBrakeHandle HoldBrake;
+ public readonly HoldBrakeHandle HoldBrake;
/// Whether the train has a combined power and brake handle
public HandleType HandleType;
/// Whether the train has the Hold Brake fitted
@@ -26,6 +27,16 @@ public struct CabHandles
/// The loco brake type
public LocoBrakeType LocoBrakeType;
+ public CabHandles(TrainBase train)
+ {
+ Reverser = new ReverserHandle(train);
+ EmergencyBrake = new EmergencyHandle(train);
+ HoldBrake = new HoldBrakeHandle(train);
+
+ Power = new PowerHandle(0, train);
+ Brake = new BrakeHandle(0, EmergencyBrake, train);
+ }
+
public void ControlDown(Control Control)
{
switch (Control.Command)
diff --git a/source/TrainManager/Train/TrainBase.cs b/source/TrainManager/Train/TrainBase.cs
index a6b043142..ba02a7229 100644
--- a/source/TrainManager/Train/TrainBase.cs
+++ b/source/TrainManager/Train/TrainBase.cs
@@ -33,7 +33,7 @@ public partial class TrainBase : AbstractTrain
/// Contains information on the specifications of the train
public TrainSpecs Specs;
/// The cab handles
- public CabHandles Handles;
+ public CabHandles Handles => Cars[DriverCar].Handles;
/// Holds the safety systems for the train
public TrainSafetySystems SafetySystems;
/// Holds the cars
@@ -152,11 +152,9 @@ public TrainBase(TrainState state, TrainType type)
Specs.DoorCloseMode = DoorMode.AutomaticManualOverride;
Specs.PantographState = PantographState.Lowered;
DriverBody = new DriverBody(this);
- Handles.Reverser = new ReverserHandle(this);
- Handles.EmergencyBrake = new EmergencyHandle(this);
}
- /// Called once when the simulation loads to initalize the train
+ /// Called once when the simulation loads to initialize the train
public virtual void Initialize()
{
for (int i = 0; i < Cars.Length; i++)