Skip to content

Commit e9a66c1

Browse files
committed
Gradient - commit 2024-16-12
1 parent 3fb566e commit e9a66c1

File tree

3 files changed

+143
-2
lines changed

3 files changed

+143
-2
lines changed

Source/Orts.Simulation/Simulation/AIs/AITrain.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3646,7 +3646,7 @@ public void AdjustControlsThrottleOff()
36463646
}
36473647
}
36483648

3649-
public void AdjustControlsAccelMore(float reqAccelMpSS, float timeS, int stepSize)
3649+
public virtual void AdjustControlsAccelMore(float reqAccelMpSS, float timeS, int stepSize)
36503650
{
36513651
if (AITrainBrakePercent > 0)
36523652
{

Source/Orts.Simulation/Simulation/Timetables/ProcessTimetable.cs

+55
Original file line numberDiff line numberDiff line change
@@ -2168,6 +2168,61 @@ public void ProcessSpeedInfo(string speedInfo, float actSpeedConv)
21682168
}
21692169
break;
21702170

2171+
case "gradient":
2172+
foreach (TTTrainCommands.TTTrainComQualifiers thisComValue in thisCommand.CommandQualifiers)
2173+
{
2174+
switch (thisComValue.QualifierName)
2175+
{
2176+
case "perc":
2177+
try
2178+
{
2179+
float gradPerc = Convert.ToSingle(thisComValue.QualifierValues[0]);
2180+
if (gradPerc <= 0 || gradPerc > 25)
2181+
{
2182+
Trace.TraceInformation("Train {0} : invalid value for gradient percent in speed setting : {1} \n",
2183+
TTTrain.Name, gradPerc);
2184+
}
2185+
else
2186+
{
2187+
TTTrain.SpeedSettings.gradPerc = gradPerc;
2188+
}
2189+
}
2190+
catch
2191+
{
2192+
Trace.TraceInformation("Train {0} : invalid value for gradient percent in speed setting : {1} \n",
2193+
TTTrain.Name, thisComValue.QualifierValues[0]);
2194+
}
2195+
break;
2196+
2197+
case "speed":
2198+
try
2199+
{
2200+
float gradSpeed = Convert.ToSingle(thisComValue.QualifierValues[0]);
2201+
TTTrain.SpeedSettings.gradMinSpeed = gradSpeed;
2202+
}
2203+
catch
2204+
{
2205+
Trace.TraceInformation("Train {0} : invalid value for gradient min speed in speed setting : {1} \n",
2206+
TTTrain.Name, thisComValue.QualifierValues[0]);
2207+
}
2208+
break;
2209+
2210+
default:
2211+
Trace.TraceInformation("Invalid qualifier in gradient speed command : {0} for train : {1}", thisComValue.QualifierName, TTTrain.Name);
2212+
break;
2213+
}
2214+
}
2215+
2216+
if (TTTrain.SpeedSettings.gradPerc.HasValue && TTTrain.SpeedSettings.gradMinSpeed.HasValue)
2217+
{
2218+
TTTrain.SpeedSettings.gradient = true;
2219+
}
2220+
else
2221+
{
2222+
Trace.TraceInformation("Train {0} : incomplete definition for gradient\n", TTTrain.Name);
2223+
}
2224+
break;
2225+
21712226
default:
21722227
Trace.TraceInformation("Invalid token in speed command : {0} for train : {1}", thisCommand.CommandToken, TTTrain.Name);
21732228
break;

Source/Orts.Simulation/Simulation/Timetables/TTTrain.cs

+87-1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ public struct SpeedValues
181181
public float routeSpeedMpS; // Route defined max speed
182182
public float consistSpeedMpS; // Consist defined max speed
183183
public bool restrictedSet; // Special speed has been set
184+
public bool gradient; // gradient definition has been set
185+
public float? gradPerc; // gradient percentage
186+
public float? gradMinSpeed; // gradient minimum speed
184187
}
185188

186189
public DelayedStartValues DelayedStartSettings = new DelayedStartValues();
@@ -259,6 +262,9 @@ public TTTrain(Simulator simulator)
259262
SpeedSettings.detachSpeedMpS = null;
260263
SpeedSettings.movingtableSpeedMpS = null;
261264
SpeedSettings.restrictedSet = false;
265+
SpeedSettings.gradient = false;
266+
SpeedSettings.gradPerc = null;
267+
SpeedSettings.gradMinSpeed = null;
262268
}
263269

264270
//================================================================================================//
@@ -528,9 +534,18 @@ public TTTrain(Simulator simulator, BinaryReader inf, AI airef)
528534
attachSpeedMpS = inf.ReadBoolean() ? inf.ReadSingle() : (float?)null,
529535
detachSpeedMpS = inf.ReadBoolean() ? inf.ReadSingle() : (float?)null,
530536
movingtableSpeedMpS = inf.ReadBoolean() ? inf.ReadSingle() : (float?)null,
531-
restrictedSet = inf.ReadBoolean()
537+
restrictedSet = inf.ReadBoolean(),
538+
gradient = inf.ReadBoolean(),
539+
gradPerc = null,
540+
gradMinSpeed = null
532541
};
533542

543+
if (SpeedSettings.gradient)
544+
{
545+
SpeedSettings.gradPerc = inf.ReadSingle();
546+
SpeedSettings.gradMinSpeed = inf.ReadSingle();
547+
}
548+
534549
DriverOnlyOperation = inf.ReadBoolean();
535550
ForceReversal = inf.ReadBoolean();
536551

@@ -840,6 +855,13 @@ public override void Save(BinaryWriter outf)
840855
outf.Write(SpeedSettings.movingtableSpeedMpS.Value);
841856
}
842857
outf.Write(SpeedSettings.restrictedSet);
858+
outf.Write(SpeedSettings.gradient);
859+
if (SpeedSettings.gradient)
860+
{
861+
outf.Write(SpeedSettings.gradPerc.Value);
862+
outf.Write(SpeedSettings.gradMinSpeed.Value);
863+
}
864+
843865
outf.Write(DriverOnlyOperation);
844866
outf.Write(ForceReversal);
845867
outf.Write(Briefing);
@@ -5792,6 +5814,70 @@ public override void StartMoving(AI_START_MOVEMENT reason)
57925814
#endif
57935815
}
57945816

5817+
//================================================================================================//
5818+
/// <summary>
5819+
/// Train control routine for full acceleration
5820+
/// Overrride for AITrain.cs
5821+
/// </summary>
5822+
5823+
public override void AdjustControlsAccelMore(float reqAccelMpSS, float timeS, int stepSize)
5824+
{
5825+
if (AITrainBrakePercent > 0)
5826+
{
5827+
AdjustControlsBrakeOff();
5828+
}
5829+
5830+
if (AITrainThrottlePercent < 100)
5831+
{
5832+
AITrainThrottlePercent += stepSize;
5833+
if (AITrainThrottlePercent > 100)
5834+
AITrainThrottlePercent = 100;
5835+
}
5836+
else if (LastSpeedMpS == 0 || (((SpeedMpS - LastSpeedMpS) / timeS) < 0.5f * MaxAccelMpSS))
5837+
{
5838+
bool forceaccreq = true;
5839+
5840+
// test for train on gradient
5841+
if (SpeedSettings.gradient)
5842+
{
5843+
bool ongrad = false;
5844+
foreach (TrainCar car in Cars)
5845+
{
5846+
if (car.CurrentElevationPercent > SpeedSettings.gradPerc)
5847+
{
5848+
ongrad = true;
5849+
continue;
5850+
}
5851+
}
5852+
if (ongrad && SpeedMpS > SpeedSettings.gradMinSpeed)
5853+
{
5854+
forceaccreq = false;
5855+
}
5856+
}
5857+
5858+
// forced acc is required
5859+
if (forceaccreq)
5860+
{
5861+
float ds = timeS * (reqAccelMpSS);
5862+
SpeedMpS = LastSpeedMpS + ds;
5863+
foreach (TrainCar car in Cars)
5864+
{
5865+
//TODO: next code line has been modified to flip trainset physics in order to get viewing direction coincident with loco direction when using rear cab.
5866+
// To achieve the same result with other means, without flipping trainset physics, the line should be changed as follows:
5867+
// car.SpeedMpS = car.Flipped ? -SpeedMpS : SpeedMpS;
5868+
car.SpeedMpS = car.Flipped ^ (car.IsDriveable && car.Train.IsActualPlayerTrain && ((MSTSLocomotive)car).UsingRearCab) ? -SpeedMpS : SpeedMpS;
5869+
}
5870+
5871+
if (CheckTrain)
5872+
{
5873+
File.AppendAllText(@"C:\temp\checktrain.txt", "Forced speed increase : was " + LastSpeedMpS + " - now " + SpeedMpS + "\n");
5874+
}
5875+
}
5876+
}
5877+
5878+
SetPercentsFromTrainToTrainset();
5879+
}
5880+
57955881
//================================================================================================//
57965882
/// <summary>
57975883
/// Calculate initial position

0 commit comments

Comments
 (0)