@@ -49,7 +49,12 @@ namespace Orts.Simulation.RollingStocks
4949 public class MSTSControlTrailerCar : MSTSLocomotive
5050 {
5151
52- public int ControlGearBoxNumberOfGears = 1 ;
52+ public int ControllerNumberOfGears = 1 ;
53+ bool HasGearController = false ;
54+ bool ControlGearUp = false ;
55+ bool ControlGearDown = false ;
56+ int ControlGearIndex ;
57+ int ControlGearIndication ;
5358
5459
5560 public MSTSControlTrailerCar ( Simulator simulator , string wagFile )
@@ -63,18 +68,25 @@ public MSTSControlTrailerCar(Simulator simulator, string wagFile)
6368 public override void LoadFromWagFile ( string wagFilePath )
6469 {
6570 base . LoadFromWagFile ( wagFilePath ) ;
66-
67- Trace . TraceInformation ( "Control Trailer" ) ;
68-
6971 }
7072
7173
7274 public override void Initialize ( )
7375 {
74-
75- base . Initialize ( ) ;
7676
77-
77+
78+ // Initialise gearbox controller
79+ if ( ControllerNumberOfGears > 0 )
80+ {
81+ GearBoxController = new MSTSNotchController ( ControllerNumberOfGears + 1 ) ;
82+ if ( Simulator . Settings . VerboseConfigurationMessages )
83+ HasGearController = true ;
84+ Trace . TraceInformation ( "Control Car Gear Controller created" ) ;
85+ ControlGearIndex = 0 ;
86+ Train . HasControlCarWithGear = true ;
87+ }
88+
89+ base . Initialize ( ) ;
7890 }
7991
8092
@@ -101,7 +113,7 @@ public override void Parse(string lowercasetoken, STFReader stf)
101113 break ;
102114
103115 // to setup gearbox controller
104- case "engine(gearboxnumberofgears " : ControlGearBoxNumberOfGears = stf . ReadIntBlock ( 1 ) ; break ;
116+ case "engine(gearboxcontrollernumberofgears " : ControllerNumberOfGears = stf . ReadIntBlock ( null ) ; break ;
105117
106118
107119 default :
@@ -123,7 +135,7 @@ public override void Copy(MSTSWagon copy)
123135
124136 MSTSControlTrailerCar locoCopy = ( MSTSControlTrailerCar ) copy ;
125137
126- ControlGearBoxNumberOfGears = locoCopy . ControlGearBoxNumberOfGears ;
138+ ControllerNumberOfGears = locoCopy . ControllerNumberOfGears ;
127139
128140
129141 }
@@ -135,6 +147,8 @@ public override void Copy(MSTSWagon copy)
135147 public override void Save ( BinaryWriter outf )
136148 {
137149 ControllerFactory . Save ( GearBoxController , outf ) ;
150+ outf . Write ( ControlGearIndication ) ;
151+ outf . Write ( ControlGearIndex ) ;
138152 }
139153
140154 /// <summary>
@@ -145,7 +159,8 @@ public override void Restore(BinaryReader inf)
145159 {
146160 base . Restore ( inf ) ;
147161 ControllerFactory . Restore ( GearBoxController , inf ) ;
148-
162+ ControlGearIndication = inf . ReadInt32 ( ) ;
163+ ControlGearIndex = inf . ReadInt32 ( ) ;
149164 }
150165
151166
@@ -159,14 +174,6 @@ public override void InitializeMoving()
159174 WheelSpeedMpS = SpeedMpS ;
160175
161176 ThrottleController . SetValue ( Train . MUThrottlePercent / 100 ) ;
162-
163- // Initialise gearbox controller
164- if ( ControlGearBoxNumberOfGears > 0 )
165- {
166- GearBoxController = new MSTSNotchController ( ControlGearBoxNumberOfGears + 1 ) ;
167- }
168-
169-
170177 }
171178
172179 /// <summary>
@@ -177,6 +184,61 @@ public override void Update(float elapsedClockSeconds)
177184 base . Update ( elapsedClockSeconds ) ;
178185 WheelSpeedMpS = SpeedMpS ; // Set wheel speed for control car, required to make wheels go around.
179186
187+
188+ if ( ControllerNumberOfGears > 0 && IsLeadLocomotive ( ) )
189+ {
190+ // pass gearbox command key to other locomotives in train, don't treat the player locomotive in this fashion.
191+ foreach ( TrainCar car in Train . Cars )
192+ {
193+
194+
195+ var locog = car as MSTSDieselLocomotive ;
196+
197+ if ( locog != null && car != this && ! locog . IsLeadLocomotive ( ) && GearBoxController != null )
198+ {
199+
200+ if ( ControlGearUp )
201+ {
202+
203+ locog . GearBoxController . CurrentNotch = GearBoxController . CurrentNotch ;
204+ locog . GearBoxController . SetValue ( ( float ) locog . GearBoxController . CurrentNotch ) ;
205+
206+ locog . ChangeGearUp ( ) ;
207+
208+ ControlGearUp = false ;
209+ }
210+
211+
212+ if ( ControlGearDown )
213+ {
214+
215+ locog . GearBoxController . CurrentNotch = GearBoxController . CurrentNotch ;
216+ locog . GearBoxController . SetValue ( ( float ) locog . GearBoxController . CurrentNotch ) ;
217+
218+ locog . ChangeGearDown ( ) ;
219+
220+ ControlGearDown = false ;
221+ }
222+
223+ // Read values for the HuD
224+ ControlGearIndex = locog . DieselEngines [ 0 ] . GearBox . CurrentGearIndex ;
225+ ControlGearIndication = locog . DieselEngines [ 0 ] . GearBox . GearIndication ;
226+ }
227+
228+ }
229+ }
230+
231+ }
232+
233+ public override string GetStatus ( )
234+ {
235+ var status = new StringBuilder ( ) ;
236+ if ( HasGearController )
237+ status . AppendFormat ( "{0} = {1}\n " , Simulator . Catalog . GetString ( "Gear" ) ,
238+ ControlGearIndex < 0 ? Simulator . Catalog . GetParticularString ( "Gear" , "N" ) : ( ControlGearIndication ) . ToString ( ) ) ;
239+ status . AppendLine ( ) ;
240+
241+ return status . ToString ( ) ;
180242 }
181243
182244 /// <summary>
@@ -202,7 +264,42 @@ protected override void UpdateSoundVariables(float elapsedClockSeconds)
202264 }
203265
204266
267+ public override void ChangeGearUp ( )
268+ {
269+
270+ GearBoxController . CurrentNotch += 1 ;
205271
272+ if ( GearBoxController . CurrentNotch > ControllerNumberOfGears )
273+ {
274+ GearBoxController . CurrentNotch = ControllerNumberOfGears ;
275+ }
276+ else if ( GearBoxController . CurrentNotch < 0 )
277+ {
278+ GearBoxController . CurrentNotch = 0 ;
279+ }
280+
281+ ControlGearUp = true ;
282+ ControlGearDown = false ;
283+
284+ }
285+
286+ public override void ChangeGearDown ( )
287+ {
288+ GearBoxController . CurrentNotch -= 1 ;
289+
290+ if ( GearBoxController . CurrentNotch > ControllerNumberOfGears )
291+ {
292+ GearBoxController . CurrentNotch = ControllerNumberOfGears ;
293+ }
294+ else if ( GearBoxController . CurrentNotch < 0 )
295+ {
296+ GearBoxController . CurrentNotch = 0 ;
297+ }
298+
299+ ControlGearUp = false ;
300+ ControlGearDown = true ;
301+
302+ }
206303
207304
208305
0 commit comments