42
42
using System . Text ;
43
43
using Event = Orts . Common . Event ;
44
44
using ORTS . Scripting . Api ;
45
+ using static Orts . Simulation . RollingStocks . MSTSDieselLocomotive ;
45
46
46
47
namespace Orts . Simulation . RollingStocks
47
48
{
48
49
public class MSTSControlTrailerCar : MSTSLocomotive
49
50
{
50
51
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
+
51
61
public MSTSControlTrailerCar ( Simulator simulator , string wagFile )
52
62
: base ( simulator , wagFile )
53
63
{
@@ -59,18 +69,25 @@ public MSTSControlTrailerCar(Simulator simulator, string wagFile)
59
69
public override void LoadFromWagFile ( string wagFilePath )
60
70
{
61
71
base . LoadFromWagFile ( wagFilePath ) ;
62
-
63
- Trace . TraceInformation ( "Control Trailer" ) ;
64
-
65
72
}
66
73
67
74
68
75
public override void Initialize ( )
69
76
{
70
-
71
- base . Initialize ( ) ;
72
77
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 ( ) ;
74
91
}
75
92
76
93
@@ -97,12 +114,58 @@ public override void Parse(string lowercasetoken, STFReader stf)
97
114
LocomotivePowerSupply . Parse ( lowercasetoken , stf ) ;
98
115
break ;
99
116
117
+ // to setup gearbox controller
118
+ case "engine(gearboxcontrollernumberofgears" : ControllerNumberOfGears = stf . ReadIntBlock ( null ) ; break ;
119
+
120
+
100
121
default :
101
122
base . Parse ( lowercasetoken , stf ) ; break ;
102
123
}
103
124
104
125
}
105
126
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
+
106
169
/// <summary>
107
170
/// Set starting conditions when initial speed > 0
108
171
///
@@ -123,6 +186,68 @@ public override void Update(float elapsedClockSeconds)
123
186
base . Update ( elapsedClockSeconds ) ;
124
187
WheelSpeedMpS = SpeedMpS ; // Set wheel speed for control car, required to make wheels go around.
125
188
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 ( ) ;
126
251
}
127
252
128
253
/// <summary>
@@ -148,10 +273,69 @@ protected override void UpdateSoundVariables(float elapsedClockSeconds)
148
273
}
149
274
150
275
276
+ public override void ChangeGearUp ( )
277
+ {
151
278
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
+ }
152
294
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
+ }
153
326
327
+ if ( GearBoxController . CurrentNotch > ControllerNumberOfGears )
328
+ {
329
+ GearBoxController . CurrentNotch = ControllerNumberOfGears ;
330
+ }
331
+ else if ( GearBoxController . CurrentNotch < 0 )
332
+ {
333
+ GearBoxController . CurrentNotch = 0 ;
334
+ }
154
335
336
+ ControlGearUp = false ;
337
+ ControlGearDown = true ;
155
338
339
+ }
156
340
}
157
341
}
0 commit comments