Skip to content

Commit efae37a

Browse files
committed
refactor: Consolidate over-water trough check to new scanner
1 parent 6fb9c1b commit efae37a

File tree

5 files changed

+18
-85
lines changed

5 files changed

+18
-85
lines changed

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

Lines changed: 4 additions & 4 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
//
@@ -3118,7 +3118,7 @@ public virtual void UpdateWaterTroughRefill(float elapsedClockSeconds, float abs
31183118
RefillingFromTrough = false;
31193119
SignalEvent(Event.WaterScoopBroken);
31203120
}
3121-
else if (!IsOverTrough())
3121+
else if (!IsOverTrough)
31223122
{
31233123
if (!WaterScoopOverTroughFlag)
31243124
{
@@ -3163,7 +3163,7 @@ public virtual void UpdateWaterTroughRefill(float elapsedClockSeconds, float abs
31633163
}
31643164

31653165
}
3166-
else if (HasWaterScoop && MSTSWagon.RefillProcess.OkToRefill == true && IsOverTrough())// water scoop has been raised, stop water filling
3166+
else if (HasWaterScoop && MSTSWagon.RefillProcess.OkToRefill == true && IsOverTrough)// water scoop has been raised, stop water filling
31673167
{
31683168
MSTSWagon.RefillProcess.OkToRefill = false;
31693169
MSTSWagon.RefillProcess.ActivePickupObjectUID = 0;
@@ -3234,7 +3234,7 @@ public virtual void UpdateWaterTroughRefill(float elapsedClockSeconds, float abs
32343234
WaterScoopInputAmountL = 0;
32353235
WaterScoopVelocityMpS = 0;
32363236

3237-
if (!IsOverTrough()) // Only reset once train moves off the trough
3237+
if (!IsOverTrough) // Only reset once train moves off the trough
32383238
{
32393239
WaterScoopTotalWaterL = 0.0f; // Reset amount of water picked up by water sccop.
32403240
}

Source/Orts.Simulation/Simulation/RollingStocks/MSTSWagon.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3525,7 +3525,7 @@ private void UpdateSpecialEffects(float elapsedClockSeconds)
35253525
}
35263526

35273527
// Water scoop spray effects control - always on when scoop over trough, regardless of whether above minimum speed or not
3528-
if (ProcessWaterEffects && LocomotiveParameters.IsWaterScoopDown && IsOverTrough() && AbsSpeedMpS > 0.1)
3528+
if (ProcessWaterEffects && LocomotiveParameters.IsWaterScoopDown && IsOverTrough && AbsSpeedMpS > 0.1)
35293529
{
35303530
float SpeedRatio = AbsSpeedMpS / MpS.FromMpH(100); // Ratio to reduce water disturbance with speed - an arbitary value of 100mph has been chosen as the reference
35313531

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

Lines changed: 11 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3094,6 +3094,7 @@ private void AddVibrations(float factor)
30943094
#endregion
30953095

30963096
public bool IsOverJunction { get; private set; }
3097+
public bool IsOverTrough { get; private set; }
30973098

30983099
void UpdatePositionFlags()
30993100
{
@@ -3107,6 +3108,7 @@ void UpdatePositionFlags()
31073108
var frontOffsetM = rearOffsetM + CarLengthM;
31083109

31093110
var isOverJunction = false;
3111+
var isOverTrough = false;
31103112

31113113
// Scan through the track sections forwards from the REAR of the train (`Train.PresentPosition[1]`),
31123114
// stopping as soon as we've passed this car (`checkedM`) or run out of track (`currentPin.Link`)
@@ -3121,6 +3123,14 @@ void UpdatePositionFlags()
31213123
if (checkedM <= frontOffsetM && rearOffsetM <= checkedM + section.Length)
31223124
{
31233125
if (section.CircuitType == TrackCircuitSection.TrackCircuitType.Junction || section.CircuitType == TrackCircuitSection.TrackCircuitType.Crossover) isOverJunction = true;
3126+
if (section.TroughInfo != null)
3127+
{
3128+
foreach (var troughs in section.TroughInfo)
3129+
{
3130+
var trough = troughs[currentPin.Direction];
3131+
if (checkedM + trough.TroughStart <= frontOffsetM && rearOffsetM <= checkedM + trough.TroughEnd) isOverTrough = true;
3132+
}
3133+
}
31243134
}
31253135
checkedM += section.Length;
31263136

@@ -3130,91 +3140,14 @@ void UpdatePositionFlags()
31303140
}
31313141

31323142
IsOverJunction = isOverJunction;
3143+
IsOverTrough = isOverTrough;
31333144
}
31343145

31353146
// TODO These three fields should be in the TrainCarViewer.
31363147
public int TrackSoundType = 0;
31373148
public WorldLocation TrackSoundLocation = WorldLocation.None;
31383149
public float TrackSoundDistSquared = 0;
31393150

3140-
3141-
/// <summary>
3142-
/// Checks if traincar is over trough. Used to check if refill possible
3143-
/// </summary>
3144-
/// <returns> returns true if car is over trough</returns>
3145-
3146-
public bool IsOverTrough()
3147-
{
3148-
var isOverTrough = false;
3149-
// start at front of train
3150-
int thisSectionIndex = Train.PresentPosition[0].TCSectionIndex;
3151-
if (thisSectionIndex < 0) return isOverTrough;
3152-
float thisSectionOffset = Train.PresentPosition[0].TCOffset;
3153-
int thisSectionDirection = Train.PresentPosition[0].TCDirection;
3154-
3155-
3156-
float usedCarLength = CarLengthM;
3157-
float processedCarLength = 0;
3158-
bool validSections = true;
3159-
3160-
while (validSections)
3161-
{
3162-
TrackCircuitSection thisSection = Train.signalRef.TrackCircuitList[thisSectionIndex];
3163-
isOverTrough = false;
3164-
3165-
// car spans sections
3166-
if ((CarLengthM - processedCarLength) > thisSectionOffset)
3167-
{
3168-
usedCarLength = thisSectionOffset - processedCarLength;
3169-
}
3170-
3171-
// section has troughs
3172-
if (thisSection.TroughInfo != null)
3173-
{
3174-
foreach (TrackCircuitSection.troughInfoData[] thisTrough in thisSection.TroughInfo)
3175-
{
3176-
float troughStartOffset = thisTrough[thisSectionDirection].TroughStart;
3177-
float troughEndOffset = thisTrough[thisSectionDirection].TroughEnd;
3178-
3179-
if (troughStartOffset > 0 && troughStartOffset > thisSectionOffset) // start of trough is in section beyond present position - cannot be over this trough nor any following
3180-
{
3181-
return isOverTrough;
3182-
}
3183-
3184-
if (troughEndOffset > 0 && troughEndOffset < (thisSectionOffset - usedCarLength)) // beyond end of trough, test next
3185-
{
3186-
continue;
3187-
}
3188-
3189-
if (troughStartOffset <= 0 || troughStartOffset < (thisSectionOffset - usedCarLength)) // start of trough is behind
3190-
{
3191-
isOverTrough = true;
3192-
return isOverTrough;
3193-
}
3194-
}
3195-
}
3196-
// tested this section, any need to go beyond?
3197-
3198-
processedCarLength += usedCarLength;
3199-
{
3200-
// go back one section
3201-
int thisSectionRouteIndex = Train.ValidRoute[0].GetRouteIndexBackward(thisSectionIndex, Train.PresentPosition[0].RouteListIndex);
3202-
if (thisSectionRouteIndex >= 0)
3203-
{
3204-
thisSectionIndex = thisSectionRouteIndex;
3205-
thisSection = Train.signalRef.TrackCircuitList[thisSectionIndex];
3206-
thisSectionOffset = thisSection.Length; // always at end of next section
3207-
thisSectionDirection = Train.ValidRoute[0][thisSectionRouteIndex].Direction;
3208-
}
3209-
else // ran out of train
3210-
{
3211-
validSections = false;
3212-
}
3213-
}
3214-
}
3215-
return isOverTrough;
3216-
}
3217-
32183151
public static WorldLocation TileLocation(UiD uid)
32193152
{
32203153
return new WorldLocation(uid.TileX, uid.TileZ, uid.X, uid.Y, uid.Z);

Source/RunActivity/Viewer3D/Popups/TrainDrivingWindow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ void AddSeparator() => AddLabel(new ListLabel
11871187
}
11881188
else if (locomotive.IsWaterScoopDown && !locomotive.ScoopIsBroken)
11891189
{
1190-
waterScoopIndicator = Viewer.Catalog.GetString("Down") + (locomotive.IsOverTrough() ? ColorCode[Color.Cyan] : ColorCode[Color.Orange]);
1190+
waterScoopIndicator = Viewer.Catalog.GetString("Down") + (locomotive.IsOverTrough ? ColorCode[Color.Cyan] : ColorCode[Color.Orange]);
11911191
waterScoopKey = Symbols.ArrowToRight + ColorCode[Color.Yellow];
11921192
}
11931193
else

Source/RunActivity/Viewer3D/WebServices/TrainDrivingDisplay.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ void AddSeparator() => AddLabel(new ListLabel
729729
}
730730
else if (locomotive.WaterScoopDown && !locomotive.ScoopIsBroken)
731731
{
732-
waterScoopIndicator = Viewer.Catalog.GetString("Down") + (locomotive.IsOverTrough() ? ColorCode[Color.Cyan] : ColorCode[Color.Orange]);
732+
waterScoopIndicator = Viewer.Catalog.GetString("Down") + (locomotive.IsOverTrough ? ColorCode[Color.Cyan] : ColorCode[Color.Orange]);
733733
waterScoopKey = Symbols.ArrowToRight + ColorCode[Color.Yellow];
734734
}
735735
else

0 commit comments

Comments
 (0)