Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change: Move cab handles storage to the car #934

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions source/ObjectViewer/Trains/NearestTrain.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using OpenBveApi.Trains;
using TrainManager;
using TrainManager.BrakeSystems;
@@ -45,17 +45,7 @@ static NearestTrain()
private static TrainBase CreateDummyTrain()
{
TrainBase train = new TrainBase(TrainState.Available);
train.Handles.Power = new PowerHandle(Specs.PowerNotches, Specs.PowerNotches, new double[] { }, new double[] { }, train);
if (Specs.IsAirBrake)
{
train.Handles.Brake = new AirBrakeHandle(train);
}
else
{
train.Handles.Brake = new BrakeHandle(Specs.BrakeNotches, Specs.BrakeNotches, null, new double[] { }, new double[] { }, train);
train.Handles.HasHoldBrake = Specs.HasHoldBrake;
}
train.Handles.HoldBrake = new HoldBrakeHandle(train);

train.Specs.HasConstSpeed = Specs.HasConstSpeed;

Array.Resize(ref train.Cars, Specs.NumberOfCars);
@@ -84,6 +74,20 @@ private static TrainBase CreateDummyTrain()
train.Cars[i].Doors[1] = new Door(1, 1000.0, 0.0);
}

CabHandles Handles = new CabHandles(train)
{
Power = new PowerHandle(Specs.PowerNotches, Specs.PowerNotches, new double[] { }, new double[] { }, train),
};
if (Specs.IsAirBrake)
{
Handles.Brake = new AirBrakeHandle(train);
}
else
{
Handles.Brake = new BrakeHandle(Specs.BrakeNotches, Specs.BrakeNotches, null, new double[] { }, new double[] { }, train);
Handles.HasHoldBrake = Specs.HasHoldBrake;
}
train.Cars[train.DriverCar].Handles = Handles;
return train;
}

1 change: 0 additions & 1 deletion source/ObjectViewer/Trains/NearestTrainStatus.cs
Original file line number Diff line number Diff line change
@@ -68,7 +68,6 @@ internal void Apply(TrainBase train)
car.Doors[1].State = RightDoorState;
car.Doors[1].AnticipatedOpen = RightDoorAnticipatedOpen;
}

train.Handles.Reverser.Driver = (ReverserPosition)Reverser;
train.Handles.Reverser.Actual = (ReverserPosition)Reverser;
train.Handles.Power.Driver = PowerNotch;
39 changes: 21 additions & 18 deletions source/Plugins/Train.OpenBve/Train/BVE/TrainDatParser.cs
Original file line number Diff line number Diff line change
@@ -230,7 +230,10 @@ internal void Parse(string FileName, Encoding Encoding, TrainBase Train) {
double DoorTolerance = 0.0;
ReadhesionDeviceType ReAdhesionDevice = ReadhesionDeviceType.TypeA;
PassAlarmType passAlarm = PassAlarmType.None;
Train.Handles.HasLocoBrake = false;
CabHandles Handles = new CabHandles(Train)
{
HasLocoBrake = false
};
double[] powerDelayUp = { }, powerDelayDown = { }, brakeDelayUp = { }, brakeDelayDown = { }, locoBrakeDelayUp = { }, locoBrakeDelayDown = { };
double electricBrakeDelayUp = 0, electricBrakeDelayDown = 0;
int powerNotches = 0, brakeNotches = 0, locoBrakeNotches = 0, powerReduceSteps = -1, locoBrakeType = 0, driverPowerNotches = 0, driverBrakeNotches = 0;
@@ -603,12 +606,12 @@ internal void Parse(string FileName, Encoding Encoding, TrainBase Train) {
break;
case 1:
//Notched air brake
Train.Handles.HasLocoBrake = true;
Handles.HasLocoBrake = true;
locomotiveBrakeType = BrakeSystemType.ElectromagneticStraightAirBrake;
break;
case 2:
//Automatic air brake
Train.Handles.HasLocoBrake = true;
Handles.HasLocoBrake = true;
locomotiveBrakeType = BrakeSystemType.AutomaticAirBrake;
break;
}
@@ -661,19 +664,19 @@ internal void Parse(string FileName, Encoding Encoding, TrainBase Train) {
switch (a)
{
case 0:
Train.Handles.HandleType = HandleType.TwinHandle;
Handles.HandleType = HandleType.TwinHandle;
break;
case 1:
Train.Handles.HandleType = HandleType.SingleHandle;
Handles.HandleType = HandleType.SingleHandle;
break;
case 2:
Train.Handles.HandleType = HandleType.InterlockedTwinHandle;
Handles.HandleType = HandleType.InterlockedTwinHandle;
break;
case 3:
Train.Handles.HandleType = HandleType.InterlockedReverserHandle;
Handles.HandleType = HandleType.InterlockedReverserHandle;
break;
default:
Train.Handles.HandleType = HandleType.TwinHandle;
Handles.HandleType = HandleType.TwinHandle;
break;
}
break;
@@ -865,7 +868,7 @@ internal void Parse(string FileName, Encoding Encoding, TrainBase Train) {
case 3:
Train.Specs.HasConstSpeed = a == 1.0; break;
case 4:
Train.Handles.HasHoldBrake = a == 1.0; break;
Handles.HasHoldBrake = a == 1.0; break;
case 5:
int dt = (int) Math.Round(a);
if (dt < 4 && dt > -1)
@@ -1026,32 +1029,31 @@ internal void Parse(string FileName, Encoding Encoding, TrainBase Train) {
}
driverBrakeNotches = brakeNotches;
}
Train.Handles.Power = new PowerHandle(powerNotches, driverPowerNotches, powerDelayUp, powerDelayDown, Train);
Handles.Power = new PowerHandle(powerNotches, driverPowerNotches, powerDelayUp, powerDelayDown, Train);
if (powerReduceSteps != -1)
{
Train.Handles.Power.ReduceSteps = powerReduceSteps;
}

if (trainBrakeType == BrakeSystemType.AutomaticAirBrake)
{
Train.Handles.Brake = new AirBrakeHandle(Train);
Handles.Brake = new AirBrakeHandle(Train);
}
else
{
Train.Handles.Brake = new BrakeHandle(brakeNotches, driverBrakeNotches, Train.Handles.EmergencyBrake, brakeDelayUp, brakeDelayDown, Train);
Handles.Brake = new BrakeHandle(brakeNotches, driverBrakeNotches, Train.Handles.EmergencyBrake, brakeDelayUp, brakeDelayDown, Train);

}

if (locomotiveBrakeType == BrakeSystemType.AutomaticAirBrake)
{
Train.Handles.LocoBrake = new LocoAirBrakeHandle(Train);
Handles.LocoBrake = new LocoAirBrakeHandle(Train);
}
else
{
Train.Handles.LocoBrake = new LocoBrakeHandle(locoBrakeNotches, Train.Handles.EmergencyBrake, locoBrakeDelayUp, locoBrakeDelayDown, Train);
Handles.LocoBrake = new LocoBrakeHandle(locoBrakeNotches, Train.Handles.EmergencyBrake, locoBrakeDelayUp, locoBrakeDelayDown, Train);
}
Train.Handles.LocoBrakeType = (LocoBrakeType)locoBrakeType;
Train.Handles.HoldBrake = new HoldBrakeHandle(Train);
Handles.LocoBrakeType = (LocoBrakeType)locoBrakeType;
// apply data
if (MotorCars < 1) MotorCars = 1;
if (TrailerCars < 0) TrailerCars = 0;
@@ -1256,8 +1258,8 @@ internal void Parse(string FileName, Encoding Encoding, TrainBase Train) {
Train.Handles.Brake.Safety = 0;
Train.Handles.Brake.Actual = 0;
if (trainBrakeType == BrakeSystemType.AutomaticAirBrake) {
Train.Handles.HandleType = HandleType.TwinHandle;
Train.Handles.HasHoldBrake = false;
Handles.HandleType = HandleType.TwinHandle;
Handles.HasHoldBrake = false;
}
Train.SafetySystems.PassAlarm = new PassAlarm(passAlarm, Train.Cars[DriverCar]);
Train.SafetySystems.PilotLamp = new PilotLamp(Train.Cars[DriverCar]);
@@ -1414,6 +1416,7 @@ internal void Parse(string FileName, Encoding Encoding, TrainBase Train) {
Train.Cars[Train.DriverCar].Driver.X = Driver.X;
Train.Cars[Train.DriverCar].Driver.Y = Driver.Y;
Train.Cars[Train.DriverCar].Driver.Z = 0.5 * CarLength + Driver.Z;
Train.Cars[Train.DriverCar].Handles = Handles;
if (Train.IsPlayerTrain)
{
Train.Cars[DriverCar].HasInteriorView = true;
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ namespace Train.OpenBve
{
partial class TrainXmlParser
{
private void ParseBrakeNode(XmlNode Node, string fileName, int Car, ref TrainBase Train)
private void ParseBrakeNode(XmlNode Node, string fileName, int Car, ref TrainBase Train, ref CabHandles Handles)
{
double compressorRate = 5000.0, compressorMinimumPressure = 690000.0, compressorMaximumPressure = 780000.0;
double auxiliaryReservoirChargeRate = 200000.0;
@@ -228,7 +228,7 @@ private void ParseBrakeNode(XmlNode Node, string fileName, int Car, ref TrainBas
}
break;
case "handle":
ParseHandleNode(c, ref Train.Handles.Brake, Car, Train, fileName);
ParseHandleNode(c, ref Handles.Brake, Car, Train, fileName);
break;

}
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ namespace Train.OpenBve
{
partial class TrainXmlParser
{
private void ParseCarNode(XmlNode Node, string fileName, int Car, ref TrainBase Train, ref UnifiedObject[] CarObjects, ref UnifiedObject[] BogieObjects, ref bool visibleFromInterior)
private void ParseCarNode(XmlNode Node, string fileName, int Car, ref TrainBase Train, ref CabHandles Handles, ref UnifiedObject[] CarObjects, ref UnifiedObject[] BogieObjects, ref bool visibleFromInterior)
{
string interiorFile = string.Empty;
ReadhesionDeviceType readhesionDevice = ReadhesionDeviceType.NotFitted;
@@ -85,7 +85,7 @@ private void ParseCarNode(XmlNode Node, string fileName, int Car, ref TrainBase
Train.Cars[Car].CarBrake.brakeType = BrakeType.Auxiliary;
if (c.ChildNodes.OfType<XmlElement>().Any())
{
ParseBrakeNode(c, fileName, Car, ref Train);
ParseBrakeNode(c, fileName, Car, ref Train, ref Handles);
}
else if (!String.IsNullOrEmpty(c.InnerText))
{
@@ -98,7 +98,7 @@ private void ParseCarNode(XmlNode Node, string fileName, int Car, ref TrainBase
//We need to save and restore the current path to make relative paths within the child file work correctly
string savedPath = currentPath;
currentPath = Path.GetDirectoryName(childFile);
ParseBrakeNode(childNodes[0], fileName, Car, ref Train);
ParseBrakeNode(childNodes[0], fileName, Car, ref Train, ref Handles);
currentPath = savedPath;
}
catch(Exception ex)
7 changes: 5 additions & 2 deletions source/Plugins/Train.OpenBve/Train/XML/TrainXmlParser.cs
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
using OpenBveApi.Interface;
using OpenBveApi.Math;
using OpenBveApi.Objects;
using TrainManager.Handles;
using TrainManager.Trains;
using Path = OpenBveApi.Path;

@@ -38,6 +39,7 @@ internal void Parse(string fileName, TrainBase Train, ref UnifiedObject[] CarObj
CarObjectsReversed = new bool[Train.Cars.Length];
BogieObjectsReversed = new bool[Train.Cars.Length * 2];
interiorVisible = new bool[Train.Cars.Length];
CabHandles Handles = Train.Cars[Train.DriverCar].Handles;
if (currentXML.DocumentElement != null)
{
XmlNodeList DocumentNodes = currentXML.DocumentElement.SelectNodes("/openBVE/Train/DriverCar");
@@ -79,7 +81,7 @@ internal void Parse(string fileName, TrainBase Train, ref UnifiedObject[] CarObj
{
if (DocumentNodes[i].Name == "Car")
{
ParseCarNode(DocumentNodes[i], fileName, carIndex, ref Train, ref CarObjects, ref BogieObjects, ref interiorVisible[carIndex]);
ParseCarNode(DocumentNodes[i], fileName, carIndex, ref Train, ref Handles, ref CarObjects, ref BogieObjects, ref interiorVisible[carIndex]);
}
else
{
@@ -140,7 +142,7 @@ internal void Parse(string fileName, TrainBase Train, ref UnifiedObject[] CarObj
//We need to save and restore the current path to make relative paths within the child file work correctly
string savedPath = currentPath;
currentPath = Path.GetDirectoryName(childFile);
ParseCarNode(childNodes[0], fileName, carIndex, ref Train, ref CarObjects, ref BogieObjects, ref interiorVisible[carIndex]);
ParseCarNode(childNodes[0], fileName, carIndex, ref Train, ref Handles, ref CarObjects, ref BogieObjects, ref interiorVisible[carIndex]);
currentPath = savedPath;
}
catch(Exception ex)
@@ -267,6 +269,7 @@ internal void Parse(string fileName, TrainBase Train, ref UnifiedObject[] CarObj
/*
* Add final properties and stuff
*/
Train.Cars[Train.DriverCar].Handles = Handles;
for (int i = 0; i < Train.Cars.Length; i++)
{
if (CarObjects[i] != null)
3 changes: 0 additions & 3 deletions source/RouteViewer/TrainManagerR.cs
Original file line number Diff line number Diff line change
@@ -26,9 +26,6 @@ public TrainManager(HostInterface host, BaseRenderer renderer, BaseOptions optio
internal class Train : TrainBase {
internal Train() : base(TrainState.Pending)
{
Handles.Power = new PowerHandle(8, 8, new double[] {}, new double[] {}, this);
Handles.Brake = new BrakeHandle(8, 8, null, new double[] {}, new double[] {}, this);
Handles.HoldBrake = new HoldBrakeHandle(this);
}
public override int NumberOfCars => this.Cars.Length;

23 changes: 14 additions & 9 deletions source/TrainManager/Car/CarBase.cs
Original file line number Diff line number Diff line change
@@ -28,6 +28,9 @@ public class CarBase : AbstractCar
{
/// <summary>A reference to the base train</summary>
public TrainBase baseTrain;
/// <summary>The cab handles</summary>
/// <remarks>May be null</remarks>
public CabHandles Handles;
/// <summary>The front bogie</summary>
public Bogie FrontBogie;
/// <summary>The rear bogie</summary>
@@ -360,15 +363,6 @@ public override void Uncouple(bool Front, bool Rear)
}
// Create new train
TrainBase newTrain = new TrainBase(TrainState.Available);
newTrain.Handles.Power = new PowerHandle(0, 0, new double[0], new double[0], newTrain)
{
DelayedChanges = new HandleChange[0]
};
newTrain.Handles.Brake = new BrakeHandle(0, 0, newTrain.Handles.EmergencyBrake, new double[0], new double[0], newTrain)
{
DelayedChanges = new HandleChange[0]
};
newTrain.Handles.HoldBrake = new HoldBrakeHandle(newTrain);
if (Front)
{
int totalPreceedingCars = trainCarIndex;
@@ -391,6 +385,17 @@ public override void Uncouple(bool Front, bool Rear)
{
baseTrain.DriverCar -= totalPreceedingCars;
}

newTrain.DriverCar = 0;
CabHandles Handles = new CabHandles(newTrain)
{
Power = new PowerHandle(0, 0, new double[0], new double[0], newTrain)
{
DelayedChanges = new HandleChange[0]
},
Brake = new BrakeHandle(0, 0, newTrain.Handles.EmergencyBrake, new double[0], new double[0], newTrain)
};
newTrain.Cars[0].Handles = Handles;
}

if (Rear)
19 changes: 14 additions & 5 deletions source/TrainManager/Handles/CabHandles.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
namespace TrainManager.Handles
using TrainManager.Trains;

namespace TrainManager.Handles
{
/// <summary>The cab handles (controls) of a train</summary>
public struct CabHandles
public class CabHandles
{
/// <summary>The Reverser</summary>
public ReverserHandle Reverser;
public readonly ReverserHandle Reverser;
/// <summary>The Power</summary>
public PowerHandle Power;
/// <summary>The Brake</summary>
public AbstractHandle Brake;
/// <summary>The Loco brake handle</summary>
public AbstractHandle LocoBrake;
/// <summary>The Emergency Brake</summary>
public EmergencyHandle EmergencyBrake;
public readonly EmergencyHandle EmergencyBrake;
/// <summary>The Hold Brake</summary>
public HoldBrakeHandle HoldBrake;
public readonly HoldBrakeHandle HoldBrake;
/// <summary>Whether the train has a combined power and brake handle</summary>
public HandleType HandleType;
/// <summary>Whether the train has the Hold Brake fitted</summary>
@@ -23,5 +25,12 @@ public struct CabHandles
public bool HasLocoBrake;
/// <summary>The loco brake type</summary>
public LocoBrakeType LocoBrakeType;

public CabHandles(TrainBase train)
{
Reverser = new ReverserHandle(train);
EmergencyBrake = new EmergencyHandle(train);
HoldBrake = new HoldBrakeHandle(train);
}
}
}
8 changes: 4 additions & 4 deletions source/TrainManager/Train/TrainBase.cs
Original file line number Diff line number Diff line change
@@ -26,8 +26,7 @@ public partial class TrainBase : AbstractTrain
{
/// <summary>Contains information on the specifications of the train</summary>
public TrainSpecs Specs;
/// <summary>The cab handles</summary>
public CabHandles Handles;

/// <summary>Holds the safety systems for the train</summary>
public TrainSafetySystems SafetySystems;
/// <summary>Holds the cars</summary>
@@ -97,6 +96,9 @@ public override double Length
/// <summary>The direction of travel on the current track</summary>
public TrackDirection CurrentDirection => TrainManagerBase.CurrentRoute.Tracks[Cars[DriverCar].FrontAxle.Follower.TrackIndex].Direction;

/// <summary>The cab handles</summary>
public CabHandles Handles => Cars[DriverCar].Handles;

public TrainBase(TrainState state)
{
State = state;
@@ -110,8 +112,6 @@ public TrainBase(TrainState state)
Specs.DoorOpenMode = DoorMode.AutomaticManualOverride;
Specs.DoorCloseMode = DoorMode.AutomaticManualOverride;
DriverBody = new DriverBody(this);
Handles.Reverser = new ReverserHandle(this);
Handles.EmergencyBrake = new EmergencyHandle(this);
}

/// <summary>Called once when the simulation loads to initalize the train</summary>