Skip to content

Commit c064a55

Browse files
committed
Add useful train information to all scripts
1 parent 47e7732 commit c064a55

File tree

8 files changed

+28
-20
lines changed

8 files changed

+28
-20
lines changed

Source/Orts.Simulation/Common/Scripting/BrakeController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public abstract class BrakeController : AbstractTrainScriptClass
3838
internal void AttachToHost(ScriptedBrakeController host)
3939
{
4040
Host = host;
41+
Car = Locomotive;
4142
}
4243

4344
/// <summary>
@@ -188,7 +189,7 @@ public float IntermediateValue
188189
/// <summary>
189190
/// Fraction of train brake demanded by cruise control
190191
/// </summary>
191-
public float CruiseControlBrakeDemand() => Locomotive.CruiseControl != null ? Locomotive.CruiseControl.TrainBrakePercent/100 : 0;
192+
public float CruiseControlBrakeDemand() => Locomotive.CruiseControl != null ? Locomotive.CruiseControl.TrainBrakePercent / 100 : 0;
192193

193194
/// <summary>
194195
/// Current notch of the brake controller

Source/Orts.Simulation/Common/Scripting/Common.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
using Orts.Common;
1919
using Orts.Simulation;
20+
using Orts.Simulation.Physics;
2021
using Orts.Simulation.RollingStocks;
2122
using Orts.Simulation.RollingStocks.SubSystems;
2223
using System;
@@ -47,6 +48,8 @@ public abstract class AbstractScriptClass
4748
/// </summary>
4849
public abstract class AbstractTrainScriptClass : AbstractScriptClass
4950
{
51+
internal TrainCar Car;
52+
internal Train Train => Car.Train;
5053
/// <summary>
5154
/// Running total of distance travelled - always positive, updated by train physics.
5255
/// </summary>
@@ -71,6 +74,25 @@ public abstract class AbstractTrainScriptClass : AbstractScriptClass
7174
/// Sends an event to the train.
7275
/// </summary>
7376
public Action<Event> SignalEventToTrain;
77+
/// <summary>
78+
/// Train's length
79+
/// </summary>
80+
public float TrainLengthM()
81+
{
82+
return Car.Train?.Length ?? 0.0f;
83+
}
84+
/// <summary>
85+
/// Returns the number of locomotives in the train
86+
/// </summary>
87+
public int NumberOfLocomotives()
88+
{
89+
int count = 0;
90+
for (int i = 0; i < Train.Cars.Count; i++)
91+
{
92+
if (Train.Cars[i] is MSTSLocomotive) count++;
93+
}
94+
return count;
95+
}
7496
}
7597

7698
/// <summary>

Source/Orts.Simulation/Common/Scripting/PowerSupply/LocomotivePowerSupply.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public abstract class LocomotivePowerSupply : PowerSupply
3131
// Internal members and methods (inaccessible from script)
3232
internal ScriptedLocomotivePowerSupply LpsHost => Host as ScriptedLocomotivePowerSupply;
3333
internal MSTSLocomotive Locomotive => LpsHost.Locomotive;
34-
internal Train Train => Locomotive.Train;
3534
internal Simulator Simulator => Locomotive.Simulator;
3635

3736
/// <summary>
@@ -188,19 +187,6 @@ protected float MaxThrottlePercent
188187
/// </summary>
189188
protected float ElectricTrainSupplyPowerW => LpsHost.ElectricTrainSupplyPowerW;
190189

191-
/// <summary>
192-
/// Returns the number of locomotives in the train
193-
/// </summary>
194-
public int NumberOfLocomotives()
195-
{
196-
int count=0;
197-
for (int i=0; i<Train.Cars.Count; i++)
198-
{
199-
if (Train.Cars[i] is MSTSLocomotive) count++;
200-
}
201-
return count;
202-
}
203-
204190
/// <summary>
205191
/// Returns the index of the current locomotive in the train (taking into account only locomotives)
206192
/// </summary>

Source/Orts.Simulation/Common/Scripting/PowerSupply/PowerSupply.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public abstract class PowerSupply : AbstractTrainScriptClass
3131
internal void AttachToHost(IPowerSupply host)
3232
{
3333
Host = host;
34+
Car = host.Car;
3435
}
3536

3637
/// <summary>

Source/Orts.Simulation/Common/Scripting/PowerSupply/TractionCutOffSubsytem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public abstract class TractionCutOffSubsystem : AbstractTrainScriptClass
3737
internal void AttachToHost(ITractionCutOffSubsystem host)
3838
{
3939
Host = host;
40+
Car = Locomotive;
4041
}
4142

4243
/// <summary>

Source/Orts.Simulation/Common/Scripting/TrainControlSystem.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,6 @@ internal void AttachToHost(ScriptedTrainControlSystem host)
168168
/// </summary>
169169
public Func<int, float> EOADistanceM;
170170
/// <summary>
171-
/// Train's length
172-
/// </summary>
173-
public Func<float> TrainLengthM;
174-
/// <summary>
175171
/// Locomotive direction.
176172
/// </summary>
177173
public Func<Direction> CurrentDirection;

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/Controllers/BrakeController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ public void Initialize()
361361
Script.AttachToHost(this);
362362

363363
// AbstractScriptClass
364+
Script.Car = Locomotive;
364365
Script.ClockTime = () => (float)Simulator.ClockTime;
365366
Script.GameTime = () => (float)Simulator.GameTime;
366367
Script.PreUpdate = () => Simulator.PreUpdate;

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/TrainControlSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ public void Initialize()
264264
}
265265

266266
// AbstractScriptClass
267+
Script.Car = Locomotive;
267268
Script.ClockTime = () => (float)Simulator.ClockTime;
268269
Script.GameTime = () => (float)Simulator.GameTime;
269270
Script.PreUpdate = () => Simulator.PreUpdate;
@@ -330,7 +331,6 @@ public void Initialize()
330331
return new MilepostInfo(list[value].DistanceToTrainM, float.Parse(list[value].ThisMile));
331332
};
332333
Script.EOADistanceM = (value) => Locomotive.Train.DistanceToEndNodeAuthorityM[value];
333-
Script.TrainLengthM = () => Locomotive.Train != null ? Locomotive.Train.Length : 0f;
334334
Script.SpeedMpS = () => Math.Abs(Locomotive.SpeedMpS);
335335
Script.CurrentDirection = () => Locomotive.Direction; // Direction of locomotive, may be different from direction of train
336336
Script.IsDirectionForward = () => Locomotive.Direction == Direction.Forward;

0 commit comments

Comments
 (0)