Skip to content

Commit 6fb9c1b

Browse files
committed
refactor: Consolidate over-junction check to new scanner
1 parent 46a2e0a commit 6fb9c1b

File tree

3 files changed

+11
-80
lines changed

3 files changed

+11
-80
lines changed

Source/Orts.Simulation/Simulation/RollingStocks/MSTSLocomotive.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// COPYRIGHT 2009, 2010, 2011, 2012, 2013 by the Open Rails project.
1+
// COPYRIGHT 2009, 2010, 2011, 2012, 2013 by the Open Rails project.
22
//
33
// This file is part of Open Rails.
44
//
@@ -3108,7 +3108,7 @@ public virtual void UpdateWaterTroughRefill(float elapsedClockSeconds, float abs
31083108
Simulator.Confirmer.Message(ConfirmLevel.Error, Simulator.Catalog.GetString("Scoop is broken, can't refill"));
31093109
RefillingFromTrough = false;
31103110
}
3111-
else if (IsOverJunction())
3111+
else if (IsOverJunction)
31123112
{
31133113
if (!ScoopIsBroken) // Only display message first time scoop is broken
31143114
{

Source/Orts.Simulation/Simulation/RollingStocks/TrainCar.cs

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// COPYRIGHT 2009 - 2022 by the Open Rails project.
1+
// COPYRIGHT 2009 - 2022 by the Open Rails project.
22
//
33
// This file is part of Open Rails.
44
//
@@ -3093,6 +3093,8 @@ private void AddVibrations(float factor)
30933093
}
30943094
#endregion
30953095

3096+
public bool IsOverJunction { get; private set; }
3097+
30963098
void UpdatePositionFlags()
30973099
{
30983100
// Position flags can only change when we're moving!
@@ -3104,6 +3106,8 @@ void UpdatePositionFlags()
31043106
rearOffsetM += Train.Cars[i - 1].CouplerSlackM + Train.Cars[i - 1].GetCouplerZeroLengthM() + Train.Cars[i].CarLengthM;
31053107
var frontOffsetM = rearOffsetM + CarLengthM;
31063108

3109+
var isOverJunction = false;
3110+
31073111
// Scan through the track sections forwards from the REAR of the train (`Train.PresentPosition[1]`),
31083112
// stopping as soon as we've passed this car (`checkedM`) or run out of track (`currentPin.Link`)
31093113
var checkedM = 0f;
@@ -3116,13 +3120,16 @@ void UpdatePositionFlags()
31163120
// Does this car overlap this track section?
31173121
if (checkedM <= frontOffsetM && rearOffsetM <= checkedM + section.Length)
31183122
{
3123+
if (section.CircuitType == TrackCircuitSection.TrackCircuitType.Junction || section.CircuitType == TrackCircuitSection.TrackCircuitType.Crossover) isOverJunction = true;
31193124
}
31203125
checkedM += section.Length;
31213126

31223127
var nextPin = section.GetNextActiveLink(currentPin.Direction, lastPin.Link);
31233128
lastPin = currentPin;
31243129
currentPin = nextPin;
31253130
}
3131+
3132+
IsOverJunction = isOverJunction;
31263133
}
31273134

31283135
// TODO These three fields should be in the TrainCarViewer.
@@ -3208,58 +3215,6 @@ public bool IsOverTrough()
32083215
return isOverTrough;
32093216
}
32103217

3211-
/// <summary>
3212-
/// Checks if traincar is over junction or crossover. Used to check if water scoop breaks
3213-
/// </summary>
3214-
/// <returns> returns true if car is over junction</returns>
3215-
3216-
public bool IsOverJunction()
3217-
{
3218-
3219-
// To Do - This identifies the start of the train, but needs to be further refined to work for each carriage.
3220-
var isOverJunction = false;
3221-
// start at front of train
3222-
int thisSectionIndex = Train.PresentPosition[0].TCSectionIndex;
3223-
float thisSectionOffset = Train.PresentPosition[0].TCOffset;
3224-
int thisSectionDirection = Train.PresentPosition[0].TCDirection;
3225-
3226-
3227-
float usedCarLength = CarLengthM;
3228-
3229-
if (Train.PresentPosition[0].TCSectionIndex != Train.PresentPosition[1].TCSectionIndex)
3230-
{
3231-
try
3232-
{
3233-
var copyOccupiedTrack = Train.OccupiedTrack.ToArray();
3234-
foreach (var thisSection in copyOccupiedTrack)
3235-
{
3236-
3237-
// Trace.TraceInformation(" Track Section - Index {0} Ciruit Type {1}", thisSectionIndex, thisSection.CircuitType);
3238-
3239-
if (thisSection.CircuitType == TrackCircuitSection.TrackCircuitType.Junction || thisSection.CircuitType == TrackCircuitSection.TrackCircuitType.Crossover)
3240-
{
3241-
3242-
// train is on a switch; let's see if car is on a switch too
3243-
WorldLocation switchLocation = TileLocation(Simulator.TDB.TrackDB.TrackNodes[thisSection.OriginalIndex].UiD);
3244-
var distanceFromSwitch = WorldLocation.GetDistanceSquared(WorldPosition.WorldLocation, switchLocation);
3245-
if (distanceFromSwitch < CarLengthM * CarLengthM + Math.Min(SpeedMpS * 3, 150))
3246-
{
3247-
isOverJunction = true;
3248-
return isOverJunction;
3249-
}
3250-
}
3251-
}
3252-
}
3253-
catch
3254-
{
3255-
3256-
}
3257-
}
3258-
3259-
return isOverJunction;
3260-
}
3261-
3262-
32633218
public static WorldLocation TileLocation(UiD uid)
32643219
{
32653220
return new WorldLocation(uid.TileX, uid.TileZ, uid.X, uid.Y, uid.Z);

Source/RunActivity/Viewer3D/Sound.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -395,32 +395,8 @@ public bool UpdateCarOnSwitchAndCurve()
395395

396396
var CarBehind = Car.Train.Cars[CarNo + CarIncr];
397397
var carPreviouslyOnSwitch = CarOnSwitch;
398-
CarOnSwitch = false;
399-
if (Car.Train.PresentPosition[0].TCSectionIndex != Car.Train.PresentPosition[1].TCSectionIndex)
400-
{
401-
try
402-
{
403-
var copyOccupiedTrack = Car.Train.OccupiedTrack.ToArray();
404-
foreach (var thisSection in copyOccupiedTrack)
405-
{
406-
if (thisSection.CircuitType == TrackCircuitSection.TrackCircuitType.Junction || thisSection.CircuitType == TrackCircuitSection.TrackCircuitType.Crossover)
407-
{
408-
// train is on a switch; let's see if car is on a switch too
409-
WorldLocation switchLocation = UidLocation(Viewer.Simulator.TDB.TrackDB.TrackNodes[thisSection.OriginalIndex].UiD);
410-
var distanceFromSwitch = WorldLocation.GetDistanceSquared(Car.WorldPosition.WorldLocation, switchLocation);
411-
if (distanceFromSwitch < Car.CarLengthM * Car.CarLengthM + Math.Min(Car.SpeedMpS * 3, 150))
412-
{
413-
CarOnSwitch = true;
414-
break;
415-
}
416-
}
417-
}
418-
}
419-
catch
420-
{
398+
CarOnSwitch = Car.IsOverJunction;
421399

422-
}
423-
}
424400
// here check for curve
425401
var carPreviouslyOnCurve = CarOnCurve;
426402
CarOnCurve = false;

0 commit comments

Comments
 (0)