4242using System . Text ;
4343using Event = Orts . Common . Event ;
4444using ORTS . Scripting . Api ;
45+ using static Orts . Simulation . RollingStocks . MSTSDieselLocomotive ;
4546
4647namespace Orts . Simulation . RollingStocks
4748{
4849 public class MSTSControlTrailerCar : MSTSLocomotive
4950 {
5051
52+ public int ControllerNumberOfGears = 1 ;
53+ bool HasGearController = false ;
54+ bool ControlGearUp = false ;
55+ bool ControlGearDown = false ;
56+ int ControlGearIndex ;
57+ int ControlGearIndication ;
58+ string ControlGearBoxType ;
59+
60+
5161 public MSTSControlTrailerCar ( Simulator simulator , string wagFile )
5262 : base ( simulator , wagFile )
5363 {
@@ -59,18 +69,25 @@ public MSTSControlTrailerCar(Simulator simulator, string wagFile)
5969 public override void LoadFromWagFile ( string wagFilePath )
6070 {
6171 base . LoadFromWagFile ( wagFilePath ) ;
62-
63- Trace . TraceInformation ( "Control Trailer" ) ;
64-
6572 }
6673
6774
6875 public override void Initialize ( )
6976 {
70-
71- base . Initialize ( ) ;
7277
73-
78+
79+ // Initialise gearbox controller
80+ if ( ControllerNumberOfGears > 0 )
81+ {
82+ GearBoxController = new MSTSNotchController ( ControllerNumberOfGears + 1 ) ;
83+ if ( Simulator . Settings . VerboseConfigurationMessages )
84+ HasGearController = true ;
85+ Trace . TraceInformation ( "Control Car Gear Controller created" ) ;
86+ ControlGearIndex = 0 ;
87+ Train . HasControlCarWithGear = true ;
88+ }
89+
90+ base . Initialize ( ) ;
7491 }
7592
7693
@@ -97,12 +114,58 @@ public override void Parse(string lowercasetoken, STFReader stf)
97114 LocomotivePowerSupply . Parse ( lowercasetoken , stf ) ;
98115 break ;
99116
117+ // to setup gearbox controller
118+ case "engine(gearboxcontrollernumberofgears" : ControllerNumberOfGears = stf . ReadIntBlock ( null ) ; break ;
119+
120+
100121 default :
101122 base . Parse ( lowercasetoken , stf ) ; break ;
102123 }
103124
104125 }
105126
127+ /// <summary>
128+ /// This initializer is called when we are making a new copy of a locomotive already
129+ /// loaded in memory. We use this one to speed up loading by eliminating the
130+ /// need to parse the wag file multiple times.
131+ /// NOTE: you must initialize all the same variables as you parsed above
132+ /// </summary>
133+ public override void Copy ( MSTSWagon copy )
134+ {
135+
136+ base . Copy ( copy ) ; // each derived level initializes its own variables
137+
138+ MSTSControlTrailerCar locoCopy = ( MSTSControlTrailerCar ) copy ;
139+
140+ ControllerNumberOfGears = locoCopy . ControllerNumberOfGears ;
141+
142+
143+ }
144+
145+ /// <summary>
146+ /// We are saving the game. Save anything that we'll need to restore the
147+ /// status later.
148+ /// </summary>
149+ public override void Save ( BinaryWriter outf )
150+ {
151+ ControllerFactory . Save ( GearBoxController , outf ) ;
152+ outf . Write ( ControlGearIndication ) ;
153+ outf . Write ( ControlGearIndex ) ;
154+ }
155+
156+ /// <summary>
157+ /// We are restoring a saved game. The TrainCar class has already
158+ /// been initialized. Restore the game state.
159+ /// </summary>
160+ public override void Restore ( BinaryReader inf )
161+ {
162+ base . Restore ( inf ) ;
163+ ControllerFactory . Restore ( GearBoxController , inf ) ;
164+ ControlGearIndication = inf . ReadInt32 ( ) ;
165+ ControlGearIndex = inf . ReadInt32 ( ) ;
166+ }
167+
168+
106169 /// <summary>
107170 /// Set starting conditions when initial speed > 0
108171 ///
@@ -123,6 +186,68 @@ public override void Update(float elapsedClockSeconds)
123186 base . Update ( elapsedClockSeconds ) ;
124187 WheelSpeedMpS = SpeedMpS ; // Set wheel speed for control car, required to make wheels go around.
125188
189+
190+ if ( ControllerNumberOfGears > 0 && IsLeadLocomotive ( ) && GearBoxController != null )
191+ {
192+ // Pass gearbox command key to other locomotives in train, don't treat the player locomotive in this fashion.
193+ // This assumes that Contol cars have been "matched" with motor cars. Also return values will be on thebasis of the last motor car in the train.
194+
195+ foreach ( TrainCar car in Train . Cars )
196+ {
197+ var locog = car as MSTSDieselLocomotive ;
198+
199+ if ( locog != null && car != this && ! locog . IsLeadLocomotive ( ) && ( ControlGearDown || ControlGearUp ) )
200+ {
201+
202+ if ( ControlGearUp )
203+ {
204+
205+ locog . GearBoxController . CurrentNotch = GearBoxController . CurrentNotch ;
206+ locog . GearBoxController . SetValue ( ( float ) locog . GearBoxController . CurrentNotch ) ;
207+
208+ locog . ChangeGearUp ( ) ;
209+ }
210+
211+ if ( ControlGearDown )
212+ {
213+
214+ locog . GearBoxController . CurrentNotch = GearBoxController . CurrentNotch ;
215+ locog . GearBoxController . SetValue ( ( float ) locog . GearBoxController . CurrentNotch ) ;
216+
217+ locog . ChangeGearDown ( ) ;
218+ }
219+ }
220+
221+ // Read values for the HuD and other requirements, will be based upon the last motorcar
222+ if ( locog != null )
223+ {
224+ ControlGearIndex = locog . DieselEngines [ 0 ] . GearBox . CurrentGearIndex ;
225+ ControlGearIndication = locog . DieselEngines [ 0 ] . GearBox . GearIndication ;
226+ if ( locog . DieselEngines [ 0 ] . GearBox . GearBoxType == TypesGearBox . C )
227+ {
228+ ControlGearBoxType = "C" ;
229+ }
230+ }
231+ }
232+
233+
234+ // Rest gear flags once all the cars have been processed
235+ ControlGearUp = false ;
236+ ControlGearDown = false ;
237+
238+ }
239+
240+ }
241+
242+ public override string GetStatus ( )
243+ {
244+ var status = new StringBuilder ( ) ;
245+ if ( HasGearController )
246+ status . AppendFormat ( "{0} = {1}\n " , Simulator . Catalog . GetString ( "Gear" ) ,
247+ ControlGearIndex < 0 ? Simulator . Catalog . GetParticularString ( "Gear" , "N" ) : ( ControlGearIndication ) . ToString ( ) ) ;
248+ status . AppendLine ( ) ;
249+
250+ return status . ToString ( ) ;
126251 }
127252
128253 /// <summary>
@@ -148,10 +273,69 @@ protected override void UpdateSoundVariables(float elapsedClockSeconds)
148273 }
149274
150275
276+ public override void ChangeGearUp ( )
277+ {
151278
279+ if ( ControlGearBoxType == "C" )
280+ {
281+ if ( ThrottlePercent == 0 )
282+ {
283+ GearBoxController . CurrentNotch += 1 ;
284+ }
285+ else
286+ {
287+ Simulator . Confirmer . Message ( ConfirmLevel . Warning , Simulator . Catalog . GetString ( "Throttle must be reduced to Idle before gear change can happen." ) ) ;
288+ }
289+ }
290+ else
291+ {
292+ GearBoxController . CurrentNotch += 1 ;
293+ }
152294
295+ if ( GearBoxController . CurrentNotch > ControllerNumberOfGears )
296+ {
297+ GearBoxController . CurrentNotch = ControllerNumberOfGears ;
298+ }
299+ else if ( GearBoxController . CurrentNotch < 0 )
300+ {
301+ GearBoxController . CurrentNotch = 0 ;
302+ }
303+
304+ ControlGearUp = true ;
305+ ControlGearDown = false ;
306+
307+ }
308+
309+ public override void ChangeGearDown ( )
310+ {
311+ if ( ControlGearBoxType == "C" )
312+ {
313+ if ( ThrottlePercent == 0 )
314+ {
315+ GearBoxController . CurrentNotch -= 1 ;
316+ }
317+ else
318+ {
319+ Simulator . Confirmer . Message ( ConfirmLevel . Warning , Simulator . Catalog . GetString ( "Throttle must be reduced to Idle before gear change can happen." ) ) ;
320+ }
321+ }
322+ else
323+ {
324+ GearBoxController . CurrentNotch -= 1 ;
325+ }
153326
327+ if ( GearBoxController . CurrentNotch > ControllerNumberOfGears )
328+ {
329+ GearBoxController . CurrentNotch = ControllerNumberOfGears ;
330+ }
331+ else if ( GearBoxController . CurrentNotch < 0 )
332+ {
333+ GearBoxController . CurrentNotch = 0 ;
334+ }
154335
336+ ControlGearUp = false ;
337+ ControlGearDown = true ;
155338
339+ }
156340 }
157341}
0 commit comments