@@ -486,6 +486,7 @@ public MSTSLocomotive(Simulator simulator, string wagPath)
486486 TrainBrakeController = new ScriptedBrakeController ( this ) ;
487487 EngineBrakeController = new ScriptedBrakeController ( this ) ;
488488 BrakemanBrakeController = new ScriptedBrakeController ( this ) ;
489+ MultiPositionControllers = new List < MultiPositionController > ( ) ;
489490 ThrottleController = new MSTSNotchController ( ) ;
490491 DynamicBrakeController = new MSTSNotchController ( ) ;
491492 TrainControlSystem = new ScriptedTrainControlSystem ( this ) ;
@@ -1221,8 +1222,7 @@ public override void Copy(MSTSWagon copy)
12211222 WaterScoopWidthM = locoCopy . WaterScoopWidthM ;
12221223 MoveParamsToAxle ( ) ;
12231224 CruiseControl = locoCopy . CruiseControl ? . Clone ( this ) ;
1224- if ( locoCopy . MultiPositionControllers != null )
1225- MultiPositionControllers = locoCopy . CloneMPC ( this ) ;
1225+ MultiPositionControllers = locoCopy . CloneMPC ( this ) ;
12261226 }
12271227
12281228 /// <summary>
@@ -1423,6 +1423,10 @@ public override void Initialize()
14231423 LocomotivePowerSupply ? . Initialize ( ) ;
14241424 TrainControlSystem . Initialize ( ) ;
14251425 CruiseControl ? . Initialize ( ) ;
1426+ foreach ( MultiPositionController mpc in MultiPositionControllers )
1427+ {
1428+ mpc . Initialize ( ) ;
1429+ }
14261430
14271431 if ( MaxSteamHeatPressurePSI == 0 ) // Check to see if steam heating is fitted to locomotive
14281432 {
@@ -1708,10 +1712,6 @@ public void SetUpMPC(STFReader stf)
17081712 {
17091713 var multiPositionController = new MultiPositionController ( this ) ;
17101714 multiPositionController . Parse ( stf ) ;
1711- if ( MultiPositionControllers == null )
1712- {
1713- MultiPositionControllers = new List < MultiPositionController > ( ) ;
1714- }
17151715 MultiPositionControllers . Add ( multiPositionController ) ;
17161716 }
17171717
@@ -1905,10 +1905,9 @@ public override void Update(float elapsedClockSeconds)
19051905 // TODO this is a wild simplification for electric and diesel electric
19061906 UpdateTractiveForce ( elapsedClockSeconds , ThrottlePercent / 100f , AbsSpeedMpS , AbsWheelSpeedMpS ) ;
19071907
1908- if ( MultiPositionControllers != null )
1908+ foreach ( MultiPositionController mpc in MultiPositionControllers )
19091909 {
1910- foreach ( MultiPositionController mpc in MultiPositionControllers )
1911- mpc . Update ( elapsedClockSeconds ) ;
1910+ mpc . Update ( elapsedClockSeconds ) ;
19121911 }
19131912
19141913 ApplyDirectionToTractiveForce ( ) ;
@@ -3377,20 +3376,15 @@ public void StartThrottleIncrease()
33773376 }
33783377 CruiseControl . SkipThrottleDisplay = false ;
33793378 }
3380- if ( MultiPositionControllers != null )
3379+ var mpc = MultiPositionControllers . Where ( x => x . controllerBinding == ControllerBinding . Throttle ) . FirstOrDefault ( ) ;
3380+ if ( mpc != null )
33813381 {
3382- foreach ( MultiPositionController mpc in MultiPositionControllers )
3382+ if ( ! mpc . StateChanged )
33833383 {
3384- if ( mpc . controllerBinding == MultiPositionController . ControllerBinding . Throttle )
3385- {
3386- if ( ! mpc . StateChanged )
3387- {
3388- mpc . StateChanged = true ;
3389- mpc . DoMovement ( MultiPositionController . Movement . Forward ) ;
3390- }
3391- return ;
3392- }
3384+ mpc . StateChanged = true ;
3385+ mpc . DoMovement ( MultiPositionController . Movement . Forward ) ;
33933386 }
3387+ return ;
33943388 }
33953389 if ( CruiseControl != null && ( CombinedControlType == CombinedControl . None || CruiseControl . UseThrottleInCombinedControl ) )
33963390 {
@@ -3436,20 +3430,15 @@ public void StartThrottleIncrease()
34363430
34373431 public void StopThrottleIncrease ( )
34383432 {
3439- if ( MultiPositionControllers != null )
3433+ var mpc = MultiPositionControllers . Where ( x => x . controllerBinding == ControllerBinding . Throttle ) . FirstOrDefault ( ) ;
3434+ if ( mpc != null )
34403435 {
3441- foreach ( MultiPositionController mpc in MultiPositionControllers )
3436+ if ( mpc . StateChanged )
34423437 {
3443- if ( mpc . controllerBinding == MultiPositionController . ControllerBinding . Throttle )
3444- {
3445- if ( mpc . StateChanged )
3446- {
3447- mpc . StateChanged = false ;
3448- mpc . DoMovement ( MultiPositionController . Movement . Neutral ) ;
3449- }
3450- return ;
3451- }
3438+ mpc . StateChanged = false ;
3439+ mpc . DoMovement ( MultiPositionController . Movement . Neutral ) ;
34523440 }
3441+ return ;
34533442 }
34543443 if ( CruiseControl != null )
34553444 {
@@ -3517,20 +3506,15 @@ public void StartThrottleDecrease()
35173506 }
35183507 CruiseControl . SkipThrottleDisplay = false ;
35193508 }
3520- if ( MultiPositionControllers != null )
3509+ var mpc = MultiPositionControllers . Where ( x => x . controllerBinding == ControllerBinding . Throttle ) . FirstOrDefault ( ) ;
3510+ if ( mpc != null )
35213511 {
3522- foreach ( MultiPositionController mpc in MultiPositionControllers )
3512+ if ( ! mpc . StateChanged )
35233513 {
3524- if ( mpc . controllerBinding == MultiPositionController . ControllerBinding . Throttle )
3525- {
3526- if ( ! mpc . StateChanged )
3527- {
3528- mpc . StateChanged = true ;
3529- mpc . DoMovement ( MultiPositionController . Movement . Aft ) ;
3530- }
3531- return ;
3532- }
3514+ mpc . StateChanged = true ;
3515+ mpc . DoMovement ( MultiPositionController . Movement . Aft ) ;
35333516 }
3517+ return ;
35343518 }
35353519 if ( CruiseControl != null && ( CombinedControlType == CombinedControl . None || CruiseControl . UseThrottleInCombinedControl ) )
35363520 {
@@ -3564,20 +3548,15 @@ public void StartThrottleDecrease()
35643548
35653549 public void StopThrottleDecrease ( )
35663550 {
3567- if ( MultiPositionControllers != null )
3551+ var mpc = MultiPositionControllers . Where ( x => x . controllerBinding == ControllerBinding . Throttle ) . FirstOrDefault ( ) ;
3552+ if ( mpc != null )
35683553 {
3569- foreach ( MultiPositionController mpc in MultiPositionControllers )
3554+ if ( mpc . StateChanged )
35703555 {
3571- if ( mpc . controllerBinding == MultiPositionController . ControllerBinding . Throttle )
3572- {
3573- if ( mpc . StateChanged )
3574- {
3575- mpc . StateChanged = false ;
3576- mpc . DoMovement ( MultiPositionController . Movement . Neutral ) ;
3577- }
3578- return ;
3579- }
3556+ mpc . StateChanged = false ;
3557+ mpc . DoMovement ( MultiPositionController . Movement . Neutral ) ;
35803558 }
3559+ return ;
35813560 }
35823561 if ( CruiseControl != null )
35833562 {
@@ -5865,12 +5844,8 @@ public virtual float GetDataOf(CabViewControl cvc)
58655844 case CABViewControlTypes . ORTS_MULTI_POSITION_CONTROLLER :
58665845 if ( MultiPositionControllers != null )
58675846 {
5868- foreach ( var mpc in MultiPositionControllers )
5869- if ( mpc . ControllerId == cvc . ControlId )
5870- {
5871- data = mpc . GetDataOf ( cvc ) ;
5872- break ;
5873- }
5847+ var mpc = MultiPositionControllers . Where ( x => x . ControllerId == cvc . ControlId ) . FirstOrDefault ( ) ;
5848+ if ( mpc != null ) data = mpc . GetDataOf ( cvc ) ;
58745849 }
58755850 break ;
58765851
0 commit comments