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

New: Implement sprung handle types #755

Merged
merged 6 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions source/CarXMLConvertor/Convert.ExtensionsCfg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,8 @@ internal static void GenerateCarXML(ref TabbedList newLines, int i)
newLines.Add("<ReadhesionDevice>NotFitted</ReadhesionDevice>");
break;
}
newLines.Add("<Power>");
newLines.Add("<Notches>" + ConvertTrainDat.PowerNotches + "</Notches>");
newLines.Add("<AccelerationCurves>");
foreach (ConvertTrainDat.AccelerationCurve curve in ConvertTrainDat.AccelerationCurves)
{
Expand All @@ -461,6 +463,7 @@ internal static void GenerateCarXML(ref TabbedList newLines, int i)
newLines.Add("</OpenBVE>");
}
newLines.Add("</AccelerationCurves>");
newLines.Add("</Power>");
}
else
{
Expand Down Expand Up @@ -518,6 +521,12 @@ internal static void GenerateCarXML(ref TabbedList newLines, int i)
}
}
newLines.Add("<Brake>");
if (i == ConvertTrainDat.DriverCar)
{
newLines.Add("<Handle>");
newLines.Add("<Notches>" + ConvertTrainDat.BrakeNotches + "</Notches>");
newLines.Add("</Handle>");
}
if (ConvertTrainDat.MotorCars[i])
{

Expand Down Expand Up @@ -604,6 +613,7 @@ internal static void GenerateDefaultXML()
newLines.Add("<RearAxle>" + -(0.4 * ConvertTrainDat.CarLength) + "</RearAxle>");
newLines.Add("</Car>");
}
newLines.Add("<DriverCar>" + ConvertTrainDat.DriverCar + "</DriverCar>");
string pluginFile = ConvertAts.DllPath(System.IO.Path.GetDirectoryName(FileName));
if (!string.IsNullOrEmpty(pluginFile))
{
Expand Down
28 changes: 28 additions & 0 deletions source/CarXMLConvertor/Convert.TrainDat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class ConvertTrainDat
internal static int ReadhesionDeviceType = 0;
internal static double DoorWidth = 1000.0;
internal static double DoorTolerance = 0.0;
internal static int PowerNotches = 0;
internal static int BrakeNotches = 0;
private static MainForm mainForm;
internal static List<AccelerationCurve> AccelerationCurves = new List<AccelerationCurve>();

Expand Down Expand Up @@ -281,6 +283,32 @@ internal static void Process(MainForm form)
} i++; n++;
AccelerationCurves.Add(curve);
} i--; break;
case "#handle":
i++;
while (i < Lines.Length && !Lines[i].StartsWith("#", StringComparison.Ordinal))
{
double a; if (NumberFormats.TryParseDoubleVb6(Lines[i], out a))
{
switch (n)
{
case 1:
if (a > 0)
{
PowerNotches = (int)a;
}
break;
case 2:
if (a > 0)
{
BrakeNotches = (int)a;
}
break;
}
}
i++; n++;
}
i--;
break;
default:
{
i++;
Expand Down
9 changes: 9 additions & 0 deletions source/OpenBVE/System/Input/Keyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using LibRender2.Screens;
using OpenTK.Input;
using OpenBveApi.Interface;
using TrainManager.Handles;

namespace OpenBve
{
Expand Down Expand Up @@ -62,6 +63,10 @@ internal static void keyDownEvent(object sender, KeyboardKeyEventArgs e)
}
}
}
// Attempt to reset handle spring
TrainManager.PlayerTrain.Handles.Power.ResetSpring();
TrainManager.PlayerTrain.Handles.Brake.ResetSpring();

BlockKeyRepeat = false;
//Remember to reset the keyboard modifier after we're done, else it repeats.....
CurrentKeyboardModifier = KeyboardModifier.None;
Expand Down Expand Up @@ -102,6 +107,10 @@ internal static void keyUpEvent(object sender, KeyboardKeyEventArgs e)
}
}
}

// Attempt to reset handle spring
TrainManager.PlayerTrain.Handles.Power.ResetSpring();
TrainManager.PlayerTrain.Handles.Brake.ResetSpring();
BlockKeyRepeat = false;
}
}
Expand Down
9 changes: 9 additions & 0 deletions source/OpenBVE/System/MainLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ internal static void ProcessKeyboard()
switch (Interface.CurrentControls[i].Component)
{
case JoystickComponent.Axis:
// Assume that a joystick axis fulfills the criteria for the handle to be 'held' in place
TrainManager.PlayerTrain.Handles.Power.ResetSpring();
TrainManager.PlayerTrain.Handles.Brake.ResetSpring();
var axisState = Program.Joysticks.GetAxis(currentDevice, Interface.CurrentControls[i].Element);
if (axisState.ToString(CultureInfo.InvariantCulture) != Interface.CurrentControls[i].LastState)
{
Expand Down Expand Up @@ -444,6 +447,9 @@ internal static void ProcessKeyboard()
//Test whether the state is the same as the last frame
if (buttonState.ToString() != Interface.CurrentControls[i].LastState)
{
// Attempt to reset handle spring
TrainManager.PlayerTrain.Handles.Power.ResetSpring();
TrainManager.PlayerTrain.Handles.Brake.ResetSpring();
if (buttonState == ButtonState.Pressed)
{
Interface.CurrentControls[i].AnalogState = 1.0;
Expand All @@ -466,6 +472,9 @@ internal static void ProcessKeyboard()
//Test if the state is the same as last frame
if (hatState.ToString() != Interface.CurrentControls[i].LastState)
{
// Attempt to reset handle spring
TrainManager.PlayerTrain.Handles.Power.ResetSpring();
TrainManager.PlayerTrain.Handles.Brake.ResetSpring();
if ((int)hatState == Interface.CurrentControls[i].Direction)
{
Interface.CurrentControls[i].AnalogState = 1.0;
Expand Down
2 changes: 2 additions & 0 deletions source/Plugins/Train.OpenBve/Train.OpenBve.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@
<Compile Include="Train\BVE\ExtensionsCfgParser.cs" />
<Compile Include="Train\BVE\TrainDatParser.cs" />
<Compile Include="Train\BVE\TrainDatparser.Formats.cs" />
<Compile Include="Train\XML\TrainXmlParser.AccelerationNode.cs" />
<Compile Include="Train\XML\TrainXmlParser.BrakeNode.cs" />
<Compile Include="Train\XML\TrainXmlParser.CarNode.cs" />
<Compile Include="Train\XML\TrainXmlParser.cs" />
<Compile Include="Train\XML\TrainXmlParser.HandleNode.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\LibRender2\LibRender2.csproj">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using OpenBveApi.Interface;
using OpenBveApi.Math;
using TrainManager.Power;

namespace Train.OpenBve
{
partial class TrainXmlParser
{
private AccelerationCurve[] ParseAccelerationNode(XmlNode c, string fileName)
{
if (c.ChildNodes.OfType<XmlElement>().Any())
{
List<AccelerationCurve> accelerationCurves = new List<AccelerationCurve>();
foreach (XmlNode cc in c.ChildNodes)
{
switch (cc.Name.ToLowerInvariant())
{
case "openbve": // don't support legacy BVE2 curves in XML, but at the same time specify that this is deliberately BVE4 / OpenBVE format
BveAccelerationCurve curve = new BveAccelerationCurve();
foreach (XmlNode sc in cc.ChildNodes)
{
switch (sc.Name.ToLowerInvariant())
{
case "stagezeroacceleration":
if (!NumberFormats.TryParseDoubleVb6(sc.InnerText, out curve.StageZeroAcceleration))
{
Plugin.currentHost.AddMessage(MessageType.Warning, false, "Stage zero acceleration was invalid for curve " + accelerationCurves.Count + " in XML file " + fileName);
}

curve.StageZeroAcceleration *= 0.277777777777778;
break;
case "stageoneacceleration":
if (!NumberFormats.TryParseDoubleVb6(sc.InnerText, out curve.StageOneAcceleration))
{
Plugin.currentHost.AddMessage(MessageType.Warning, false, "Stage one acceleration was invalid for curve " + accelerationCurves.Count + " in XML file " + fileName);
}

curve.StageOneAcceleration *= 0.277777777777778;
break;
case "stageonespeed":
if (!NumberFormats.TryParseDoubleVb6(sc.InnerText, out curve.StageOneSpeed))
{
Plugin.currentHost.AddMessage(MessageType.Warning, false, "Stage one speed was invalid for curve " + accelerationCurves.Count + " in XML file " + fileName);
}

curve.StageOneSpeed *= 0.277777777777778;
break;
case "stagetwospeed":
if (!NumberFormats.TryParseDoubleVb6(sc.InnerText, out curve.StageTwoSpeed))
{
Plugin.currentHost.AddMessage(MessageType.Warning, false, "Stage two speed was invalid for curve " + accelerationCurves.Count + " in XML file " + fileName);
}

curve.StageTwoSpeed *= 0.277777777777778;
break;
case "stagetwoexponent":
if (!NumberFormats.TryParseDoubleVb6(sc.InnerText, out curve.StageTwoExponent))
{
Plugin.currentHost.AddMessage(MessageType.Warning, false, "Stage two exponent was invalid for curve " + accelerationCurves.Count + " in XML file " + fileName);
}

break;
}
}
accelerationCurves.Add(curve);
break;
}
}
return accelerationCurves.ToArray();
}
Plugin.currentHost.AddMessage(MessageType.Warning, false, "An empty list of acceleration curves was provided in XML file " + fileName);
return new AccelerationCurve[] { };
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using System.Xml;
using OpenBveApi.Interface;
using OpenBveApi.Math;
Expand Down Expand Up @@ -227,8 +228,13 @@ private void ParseBrakeNode(XmlNode Node, string fileName, int Car, ref TrainBas
}
}
break;
case "handle":
ParseHandleNode(c, ref Train.Handles.Brake, Car, Train, fileName);
break;

}
}

Train.Cars[Car].CarBrake.mainReservoir = new MainReservoir(compressorMinimumPressure, compressorMaximumPressure, 0.01, (Train.Handles.Brake is AirBrakeHandle ? 0.25 : 0.075) / Train.Cars.Length);
Train.Cars[Car].CarBrake.airCompressor = new Compressor(compressorRate, Train.Cars[Car].CarBrake.mainReservoir, Train.Cars[Car]);
Train.Cars[Car].CarBrake.equalizingReservoir = new EqualizingReservoir(equalizingReservoirServiceRate, equalizingReservoirEmergencyRate, equalizingReservoirChargeRate);
Expand Down
62 changes: 17 additions & 45 deletions source/Plugins/Train.OpenBve/Train/XML/TrainXmlParser.CarNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using TrainManager.BrakeSystems;
using TrainManager.Car;
using TrainManager.Cargo;
using TrainManager.Handles;
using TrainManager.Power;
using TrainManager.Trains;

Expand Down Expand Up @@ -394,61 +395,32 @@ private void ParseCarNode(XmlNode Node, string fileName, int Car, ref TrainBase
}
break;
case "accelerationcurves":
/*
* NOTE: This was initially implemented here.
* It has moved to being a child-node of the power node
* Retain this for the minute in case someone has actually used the thing (although the format is an ongoing WIP)....
*/
CopyAccelerationCurves = false;
Train.Cars[Car].Specs.AccelerationCurves = ParseAccelerationNode(c, fileName);
break;
case "power":
if (c.ChildNodes.OfType<XmlElement>().Any())
{
List<AccelerationCurve> accelerationCurves = new List<AccelerationCurve>();
foreach (XmlNode cc in c.ChildNodes)
{
switch (cc.Name.ToLowerInvariant())
{
case "openbve": // don't support legacy BVE2 curves in XML, but at the same time specify that this is deliberately BVE4 / OpenBVE format
BveAccelerationCurve curve = new BveAccelerationCurve();
foreach (XmlNode sc in cc.ChildNodes)
{
switch (sc.Name.ToLowerInvariant())
{
case "stagezeroacceleration":
if (!NumberFormats.TryParseDoubleVb6(sc.InnerText, out curve.StageZeroAcceleration))
{
Plugin.currentHost.AddMessage(MessageType.Warning, false, "Stage zero acceleration was invalid for curve " + accelerationCurves.Count + " in XML file " + fileName);
}
curve.StageZeroAcceleration *= 0.277777777777778;
break;
case "stageoneacceleration":
if (!NumberFormats.TryParseDoubleVb6(sc.InnerText, out curve.StageOneAcceleration))
{
Plugin.currentHost.AddMessage(MessageType.Warning, false, "Stage one acceleration was invalid for curve " + accelerationCurves.Count + " in XML file " + fileName);
}
curve.StageOneAcceleration *= 0.277777777777778;
break;
case "stageonespeed":
if (!NumberFormats.TryParseDoubleVb6(sc.InnerText, out curve.StageOneSpeed))
{
Plugin.currentHost.AddMessage(MessageType.Warning, false, "Stage one speed was invalid for curve " + accelerationCurves.Count + " in XML file " + fileName);
}
curve.StageOneSpeed *= 0.277777777777778;
break;
case "stagetwospeed":
if (!NumberFormats.TryParseDoubleVb6(sc.InnerText, out curve.StageTwoSpeed))
{
Plugin.currentHost.AddMessage(MessageType.Warning, false, "Stage two speed was invalid for curve " + accelerationCurves.Count + " in XML file " + fileName);
}
curve.StageTwoSpeed *= 0.277777777777778;
break;
case "stagetwoexponent":
if (!NumberFormats.TryParseDoubleVb6(sc.InnerText, out curve.StageTwoExponent))
{
Plugin.currentHost.AddMessage(MessageType.Warning, false, "Stage two exponent was invalid for curve " + accelerationCurves.Count + " in XML file " + fileName);
}
break;
}
}
accelerationCurves.Add(curve);
case "handle":
AbstractHandle p = Train.Handles.Power; // yuck, but we can't store this as the base type due to constraints elsewhere
ParseHandleNode(cc, ref p, Car, Train, fileName);
break;
case "accelerationcurves":
CopyAccelerationCurves = false;
Train.Cars[Car].Specs.AccelerationCurves = ParseAccelerationNode(c, fileName);
break;
}
}
Train.Cars[Car].Specs.AccelerationCurves = accelerationCurves.ToArray();

}
break;
case "doors":
Expand Down
Loading