@@ -75,6 +75,7 @@ public class AITrain : Train
7575 public float DoorOpenTimer = - 1f ;
7676 public float DoorCloseTimer = - 1f ;
7777 public AILevelCrossingHornPattern LevelCrossingHornPattern { get ; set ; }
78+ public bool ApproachTriggerSet = false ; // station approach trigger for AI trains has been set
7879
7980 public float PathLength ;
8081
@@ -246,6 +247,7 @@ public AITrain(Simulator simulator, BinaryReader inf, AI airef)
246247 UncondAttach = inf . ReadBoolean ( ) ;
247248 DoorCloseTimer = inf . ReadSingle ( ) ;
248249 DoorOpenTimer = inf . ReadSingle ( ) ;
250+ ApproachTriggerSet = inf . ReadBoolean ( ) ;
249251 if ( ! Simulator . TimetableMode && DoorOpenTimer <= 0 && DoorCloseTimer > 0 && Simulator . OpenDoorsInAITrains &&
250252 MovementState == AI_MOVEMENT_STATE . STATION_STOP && StationStops . Count > 0 )
251253 {
@@ -339,6 +341,7 @@ public override void Save(BinaryWriter outf)
339341 outf . Write ( UncondAttach ) ;
340342 outf . Write ( DoorCloseTimer ) ;
341343 outf . Write ( DoorOpenTimer ) ;
344+ outf . Write ( ApproachTriggerSet ) ;
342345 if ( LevelCrossingHornPattern != null )
343346 {
344347 outf . Write ( 0 ) ;
@@ -1302,6 +1305,7 @@ public virtual void SetNextStationAction(bool fromAutopilotSwitch = false)
13021305 AIActionItem newAction = new AIActionItem ( null , AIActionItem . AI_ACTION_TYPE . STATION_STOP ) ;
13031306 newAction . SetParam ( distancesM [ 1 ] , 0.0f , distancesM [ 0 ] , DistanceTravelledM ) ;
13041307 requiredActions . InsertAction ( newAction ) ;
1308+ ApproachTriggerSet = false ;
13051309
13061310#if DEBUG_REPORTS
13071311 if ( StationStops [ 0 ] . ActualStopType == StationStop . STOPTYPE . STATION_STOP )
@@ -2061,6 +2065,7 @@ public virtual void UpdateStationState(float elapsedClockSeconds, int presentTim
20612065
20622066 Delay = TimeSpan . FromSeconds ( ( presentTime - thisStation . DepartTime ) % ( 24 * 3600 ) ) ;
20632067 }
2068+ if ( Cars [ 0 ] is MSTSLocomotive ) Cars [ 0 ] . SignalEvent ( Event . AITrainLeavingStation ) ;
20642069
20652070#if DEBUG_REPORTS
20662071 DateTime baseDTd = new DateTime ( ) ;
@@ -2622,6 +2627,13 @@ public virtual void UpdateBrakingState(float elapsedClockSeconds, int presentTim
26222627 }
26232628 }
26242629
2630+ if ( nextActionInfo != null && nextActionInfo . NextAction == AIActionItem . AI_ACTION_TYPE . STATION_STOP &&
2631+ distanceToGoM < 150 + StationStops [ 0 ] . PlatformItem . Length && ! ApproachTriggerSet )
2632+ {
2633+ if ( Cars [ 0 ] is MSTSLocomotive ) Cars [ 0 ] . SignalEvent ( Event . AITrainApproachingStation ) ;
2634+ ApproachTriggerSet = true ;
2635+ }
2636+
26252637 if ( nextActionInfo != null && nextActionInfo . NextAction == AIActionItem . AI_ACTION_TYPE . STATION_STOP )
26262638 creepDistanceM = 0.0f ;
26272639 if ( nextActionInfo == null && requiredSpeedMpS == 0 )
@@ -4369,6 +4381,7 @@ public void CoupleAI(Train attachTrain, bool thisTrainFront, bool attachTrainFro
43694381 AI . AITrains . Add ( this ) ;
43704382 AI . aiListChanged = true ;
43714383 }
4384+ else if ( attachTrain is AITrain ) RedefineAITriggers ( attachTrain as AITrain ) ;
43724385 if ( ! UncondAttach )
43734386 {
43744387 RemoveTrain ( ) ;
@@ -4477,6 +4490,7 @@ public void CoupleAIToStatic(Train attachTrain, bool thisTrainFront, bool attach
44774490 AddTrackSections ( ) ;
44784491 ResetActions ( true ) ;
44794492 physicsUpdate ( 0 ) ;
4493+ RedefineAITriggers ( this ) ;
44804494 }
44814495
44824496 //================================================================================================//
@@ -4718,7 +4732,8 @@ public void TerminateCoupling(Train attachTrain, bool thisTrainFront, bool attac
47184732 // Move WP, if any, just under the loco;
47194733 AuxActionsContain . MoveAuxActionAfterReversal ( this ) ;
47204734 ResetActions ( true ) ;
4721-
4735+ RedefineAITriggers ( this ) ;
4736+ if ( attachTrain is AITrain ) RedefineAITriggers ( attachTrain as AITrain ) ;
47224737 physicsUpdate ( 0 ) ; // Stop the wheels from moving etc
47234738
47244739 }
@@ -6580,6 +6595,27 @@ public void RestartWaitingTrain(RestartWaitingTrain restartWaitingTrain)
65806595 }
65816596 }
65826597
6598+ //================================================================================================//
6599+ /// <summary>
6600+ /// Redefine sound triggers for AI trains
6601+ /// </summary>
6602+ public void RedefineAITriggers ( AITrain train )
6603+ {
6604+ var leadFound = false ;
6605+ foreach ( var car in train . Cars )
6606+ {
6607+ if ( car is MSTSLocomotive )
6608+ {
6609+ if ( ! leadFound )
6610+ {
6611+ car . SignalEvent ( Event . AITrainLeadLoco ) ;
6612+ leadFound = true ;
6613+ }
6614+ else car . SignalEvent ( Event . AITrainHelperLoco ) ;
6615+ }
6616+ }
6617+ }
6618+
65836619 }
65846620
65856621
0 commit comments