-
Notifications
You must be signed in to change notification settings - Fork 1
/
diesel-electric-processor.txt
850 lines (737 loc) · 26.2 KB
/
diesel-electric-processor.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
@name OpenControl/Diesel-Electric-Processor
@inputs Locomotive:entity HandBrake [LOCOMOTIVE_AIRBRAKE_DATA CONTROL_STAND_DATA CONFIGURATION_DATA ENGINE_SOUND_DATA Trucks SpanBolsters]:array
@outputs [LOCOMOTIVE_PROCESSOR]:array
@outputs Load MPH KPH DynamicBrakeWarning GroundRelay BE BEAuto Force TE CarCount MotorTemp DynamicTemp TrainWeight WheelSlip Sanding CurrentBO RampedMotorVoltage SlippingFactor TransitionNumber LowSpeedCurve
@outputs Dynamics CEMF Current
@persist [E O W]:entity [SmoothTable]:table LastProps:array CEMF R MainGeneratorVoltage MaxGeneratorVoltage MotorVoltage CircuitType CurrentBO Current DBCurrent Load
@persist TE V BHQ Braking_Train Braking_Loco BrakeMul MaxTrainBrakeForce Holding TE_Effective TE_EffectiveLoad AutoMux CarCount BEVLB TrainWeight
@persist DynamicTemp MotorTemp DynamicShort MotorShort GroundRelay DBArking TMArking FixNils RampedMotorVoltage EngineRPMCal DynoHPINCR FBWD FFWD
@persist Motors MaxGeneratorCurrent MaxTractionMotorCurrent MaxContinuousTractionMotorCurrent MaxDynamicCurrent MaxContinuousDynamicCurrent EngineIdleRPM EngineRedlineRPM NumNotches Axles
@persist Horsepower Weight BaseSpeed TopSpeed ClaspBrakes [AlarmBellStart AlarmBellEnd]:string Adhesion WeightOnAxle TE_Starting Dynamics LowSpeedCurve SlippingFactor FeedPressure
@persist HasPM HasTM SlugMode Sander ForceSand TotalMotors HP_Effective HEP HEP_Subtract HEPTYPE HEP_Notch HEPQ LoadAfterSlip [TransitionThresholds TransitionGroups TransitionShunts]:array TransitionNumber
@persist TransitionNotches LSX
@trigger none
@model
if(dupefinished() | duped()){ reset() }
if(first()){
E = entity()
O = owner()
W = E:isWeldedTo()
Motors = CONFIGURATION_DATA[34,number]
MaxGeneratorCurrent = CONFIGURATION_DATA[33,number]
MaxTractionMotorCurrent = CONFIGURATION_DATA[36,number]
MaxContinuousTractionMotorCurrent = CONFIGURATION_DATA[37,number]
MaxDynamicCurrent = CONFIGURATION_DATA[39,number]
MaxContinuousDynamicCurrent = CONFIGURATION_DATA[40,number]
EngineIdleRPM = CONFIGURATION_DATA[41,number]
EngineRedlineRPM = CONFIGURATION_DATA[42,number]
NumNotches = CONFIGURATION_DATA[43,number]
Axles = CONFIGURATION_DATA[35,number]
Horsepower = CONFIGURATION_DATA[44,number]
Weight = CONFIGURATION_DATA[45,number]
BaseSpeed = CONFIGURATION_DATA[46,number]
TopSpeed = CONFIGURATION_DATA[47,number]
ClaspBrakes = CONFIGURATION_DATA[48,number]
AlarmBellStart = CONFIGURATION_DATA[32,string]
AlarmBellEnd = CONFIGURATION_DATA[49,string]
Adhesion = CONFIGURATION_DATA[53,number]
FeedPressure = CONFIGURATION_DATA[53,number]
LSX = CONFIGURATION_DATA[85,number]
MainGeneratorVoltage = 0
MaxGeneratorVoltage = 1500
CircuitType = 4
R = 0
Current = 0
Force = 0
Load = 0
CEMF = 0
MotorVoltage = 0
RampedMotorVoltage = 0
FieldShunt = 1
BrakeMul = (775*2)
MaxTrainBrakeForce = 0
Holding = 0
AutoMux = 1
BHQ = 0
CarCount = 0
DynamicTemp = 0
MotorTemp = 0
DynamicBrakeWarning = 0
DynamicShort = 0
MotorShort = 0
GroundRelay = 0
DBArking = 0
TMArking = 0
FixNils = 0
EngineRPMCal = 0
Dynamics = 0
Sander = 0
HEPQ = 0
HEP_Notch = 0
LoadAfterSlip = 0
WeightOnAxle = Weight/Axles
TE_Starting = WeightOnAxle*Adhesion
DPM = CONFIGURATION_DATA[59,number]
HEP = CONFIGURATION_DATA[60,number]
HEP_Subtract = CONFIGURATION_DATA[61,number]
HEPTYPE = CONFIGURATION_DATA[62,number]
UnitType = CONFIGURATION_DATA[58,number]
TransitionGroups = array(CONFIGURATION_DATA[69,number],CONFIGURATION_DATA[70,number],CONFIGURATION_DATA[71,number],CONFIGURATION_DATA[72,number])
TransitionShunts = array(CONFIGURATION_DATA[73,number],CONFIGURATION_DATA[74,number],CONFIGURATION_DATA[75,number],CONFIGURATION_DATA[76,number])
TransitionVoltage = ( -(4*3000) - sqrt((4*3000)^2 - 4*4*746*Horsepower) )/(2*-4)
TransitionNotches = TransitionGroups:count()
for(N=1,TransitionNotches-1){
TransitionThresholds[N+1,number] = TransitionVoltage*TopSpeed/(3000*TransitionShunts[N+1,number]*TransitionGroups[N+1,number])
}
HasPM = (UnitType==0) | (UnitType==2)
HasTM = (UnitType==0) | (UnitType==1)
SlugMode = UnitType==1
if(!HasPM){
HEP = 0
DPM = 0
}
function number getWeatherCondition(){
return 0.5
}
function number smooth(Var,SmoothAmount,ID:string,DefaultVal){
if(!SmoothTable:exists(ID)){
SmoothTable[ID,number] = DefaultVal
}
local Smooth = SmoothTable[ID,number]
Smooth += (Var-Smooth) / SmoothAmount
SmoothTable[ID,number] = Smooth
return Smooth
}
function number carCount(BP_Anglecock){
local Cars = 0
if(BP_Anglecock){
Cars = 0
}
else{
local Props = E:getConstraints()
local Cnt = 0
local ImportantProps = array()
foreach(K,V:entity = Props){
if(V:mass()>1000){
ImportantProps[Cnt,entity] = V
Cnt++
}
}
local ExtraProps = ImportantProps:count()
Cars = floor(ExtraProps/3)
if(Cars<0){Cars = 0}
}
MaxTrainBrakeForce = BrakeMul*(Cars-1)*0.5
if(MaxTrainBrakeForce<0){MaxTrainBrakeForce = 0}
return Cars
}
function number trainWeight(){
local WeightProps = E:getConstraints()
local TotalMass = 0
foreach(K,V:entity=WeightProps){
TotalMass += V:mass()
}
return TotalMass
}
function void abRelease(){
local Ents = E:getConstraints()
foreach(K,V:entity=Ents){
V:propPhysicalMaterial("friction_00")
V:propDrag(0)
}
return void
}
function void abRest(Ents:array){
foreach(K,V:entity=Ents){
V:propPhysicalMaterial("phx_tire_normal")
}
return void
}
function void setWeight(Carbody:entity,Bolsters:array,Trucks:array,Pounds){
local KG = fromUnit("lb",Pounds/2)
local CarbodyWeight = clamp(round(KG/2),10,50000)
Carbody:setMass(CarbodyWeight)
KG -= CarbodyWeight
if(Bolsters:count()>0){
local BolsterWgt = round(KG/(Bolsters:count()*2))
for(N=1,Bolsters:count()){
Bolsters[N,entity]:setMass(BolsterWgt)
}
KG -= BolsterWgt*Bolsters:count()
}
local TruckWgt = round(KG/Trucks:count())
for(N=1,Trucks:count()){
Trucks[N,entity]:setMass(TruckWgt)
}
return void
}
setWeight(W,SpanBolsters,Trucks,Weight)
timer("checkcars",5000)
timer("fixnil",1000)
}
if(CONTROL_STAND_DATA[19,number]){
interval(150)
}
if(clk("checkcars")){
CarCount = carCount(!LOCOMOTIVE_AIRBRAKE_DATA[11,number])
TrainWeight = trainWeight()
timer("checkcars",5000)
LastProps = E:getConstraints()
}
if(ENGINE_SOUND_DATA[7,number] | ENGINE_SOUND_DATA[8,number]){
EngineRPMCal = (((EngineRedlineRPM-EngineIdleRPM)/NumNotches)*ENGINE_SOUND_DATA[10,number])
}
else{
EngineRPMCal = 0
}
local EngineRPMSmoothed = smooth(EngineRPMCal,15,"engine_rpm_smoothing",0)
EngineRPM = EngineRPMSmoothed+EngineIdleRPM
V = -E:velL():z()
MPH = toUnit("mph",abs(V))*4/3
KPH = toUnit("km/h",abs(V))*4/3
local DynamicsMode = CONFIGURATION_DATA[79,number]
local HasSelector = CONFIGURATION_DATA[2,number]
local TransitionMode = CONFIGURATION_DATA[82,number] #0 Manual, 1 Automatic with Selector, 2 Full Automatic
if(TransitionMode == 0){
if(HasSelector){
if(CONTROL_STAND_DATA[29,number] == 0){
CircuitType = 0
FieldShunt = 1
}
elseif(CONTROL_STAND_DATA[29,number]>0){
CircuitType = TransitionGroups[CONTROL_STAND_DATA[29,number],number]
FieldShunt = TransitionShunts[CONTROL_STAND_DATA[29,number],number]
}
elseif(CONTROL_STAND_DATA[29,number] == -1){
CircuitType = TransitionGroups[1,number]
FieldShunt = TransitionShunts[1,number]
}
}
}
elseif(TransitionMode == 1){
if(HasSelector){
if(CONTROL_STAND_DATA[29,number] == 0){
CircuitType = 0
FieldShunt = 1
TransitionNumber = 0
}
elseif(CONTROL_STAND_DATA[29,number] != 0){
TransitionNumber = 1
if(TransitionNumber < 4){
if(MPH > TransitionThresholds[TransitionNumber+1,number]){
TransitionNumber += 1
}
}
if(TransitionNumber > 1){
if(MPH <= TransitionThresholds[TransitionNumber,number]){
TransitionNumber -= 1
}
}
CircuitType = TransitionGroups[TransitionNumber,number]
FieldShunt = TransitionShunts[TransitionNumber,number]
}
}
}
elseif(TransitionMode == 2){
if(!HasSelector){
TransitionNumber = 1
if(TransitionNumber < 4){
if(MPH > TransitionThresholds[TransitionNumber+1,number]){
TransitionNumber += 1
}
}
if(TransitionNumber > 1){
if(MPH <= TransitionThresholds[TransitionNumber,number]){
TransitionNumber -= 1
}
}
CircuitType = TransitionGroups[TransitionNumber,number]
FieldShunt = TransitionShunts[TransitionNumber,number]
}
}
if(HasPM){
if(CONTROL_STAND_DATA[24,number] == 1){
if(CONTROL_STAND_DATA[18,number]>0){
GenEngineRPM = EngineRPMSmoothed+EngineIdleRPM
MainGeneratorVoltage = (MaxGeneratorVoltage/(EngineRedlineRPM/GenEngineRPM))
MotorVoltage = MainGeneratorVoltage
}
elseif(CONTROL_STAND_DATA[18,number] == 0){
GenEngineRPM = EngineIdleRPM
MainGeneratorVoltage = GenEngineRPM
MotorVoltage = (CircuitType*CEMF)
}
if(CONTROL_STAND_DATA[18,number]>0){
if(CONTROL_STAND_DATA[21,number]>0){
RampedMotorVoltage = MotorVoltage
}
else{
RampedMotorVoltage = 0
}
}
else{
RampedMotorVoltage = 0
}
}
else{
RampedMotorVoltage = 0
}
}
elseif(!HasPM & HasTM){
RampedMotorVoltage = CONTROL_STAND_DATA[34,number]
}
local HEP_Up = CONTROL_STAND_DATA[35,number]
local HEP_Dn = CONTROL_STAND_DATA[35,number]
if(HEP & (CONTROL_STAND_DATA[16,number] == 0) & (CONTROL_STAND_DATA[18,number] == 0)){
if(HEP_Up & !HEPQ & (HEP_Notch<2)){
HEPQ = 1
HEP_Notch++
}
elseif(HEP_Dn & !HEPQ & (HEP_Notch>0)){
HEPQ = 1
HEP_Notch--
}
elseif(!HEP_Up & !HEP_Dn & HEPQ){
HEPQ = 0
}
}
Horsepower = HEP_Notch ? (Horsepower-HEP_Subtract) : Horsepower
local BlendedBraking = CONFIGURATION_DATA[81,number]
local BatteryKnife = CONTROL_STAND_DATA[19,number]
local Notch = CONTROL_STAND_DATA[18,number]
local Isolation = CONTROL_STAND_DATA[24,number]
local GenField = CONTROL_STAND_DATA[21,number]
local Reverser = CONTROL_STAND_DATA[16,number]
local DynamicNotch = CONTROL_STAND_DATA[12,number]
local DynamicPowerZone = CONTROL_STAND_DATA[14,number]
local EMRGBrake = CONTROL_STAND_DATA[11,number]
local AutomaticBrakeCutout = CONTROL_STAND_DATA[7,number]
local BrakeNotch = CONTROL_STAND_DATA[3,number]
local SelectorNotch = CONTROL_STAND_DATA[29,number]
if(HasTM){
if(BatteryKnife & Isolation & GenField){
if(Reverser != 0){
if(Notch > 0 & !DynamicNotch){ #Power
if(!HasSelector){
local CurrentNR = (RampedMotorVoltage - (CircuitType*CEMF))
CEMF = 5+(MPH/(Horsepower/MaxGeneratorCurrent))
R = RampedMotorVoltage/CurrentNR
if(!MotorShort & !GroundRelay){
CurrentBO = (CurrentNR/R)*FieldShunt
}
elseif(MotorShort | GroundRelay){
CurrentBO = 0
}
}
else{
if(SelectorNotch > 0){
local CurrentNR = (RampedMotorVoltage - (CircuitType*CEMF))
CEMF = 5+(MPH/(Horsepower/MaxGeneratorCurrent))
R = RampedMotorVoltage/CurrentNR
if(!MotorShort & !GroundRelay){
CurrentBO = (CurrentNR/R)*FieldShunt
}
elseif(MotorShort | GroundRelay){
CurrentBO = 0
}
}
else{
CurrentBO = 0
}
}
}
elseif(Notch == 0){ #Dynamics
if(DynamicsMode > 0){
if(BlendedBraking & !DynamicNotch){ #Blended Braking
if(!HasSelector){
if(!EMRGBrake){
if(AutomaticBrakeCutout > 0){
if(LOCOMOTIVE_AIRBRAKE_DATA[2,number] < FeedPressure){
if(BrakeNotch > 0){
Dynamics = 1
local GRID_RESISTOR_OHMS = 0.923
local WheelDiameter = 40
local WheelCircumference = WheelDiameter*pi()
local IPH = MPH*(12*5280)
local AxleRPM = (IPH/60)/WheelCircumference
CEMF = (MPH/TopSpeed)
local DBMotorVoltage = ((AxleRPM/MaxDynamicCurrent)*((FeedPressure/LOCOMOTIVE_AIRBRAKE_DATA[2,number])*30))*100
local DBMotorCurrent = ((DBMotorVoltage-CircuitType)*CEMF)*FieldShunt
if(!DynamicShort & !GroundRelay){
CurrentBO = -(DBMotorCurrent/(GRID_RESISTOR_OHMS))*2.5
}
elseif(DynamicShort | GroundRelay){
CurrentBO = 0
}
}
else{
CurrentBO = 0
}
}
else{
CurrentBO = 0
}
}
else{
CurrentBO = 0
}
}
}
}
}
if(DynamicNotch == 0){
CurrentBO = 0
Current = 0
TE = 0
}
}
else{
if(DynamicsMode > 0){
if(DynamicNotch == 2){ #Normal Dynamics
Dynamics = 1
local GRID_RESISTOR_OHMS = 0.923
local WheelDiameter = 40
local WheelCircumference = WheelDiameter*pi()
local IPH = MPH*(12*5280)
local AxleRPM = (IPH/60)/WheelCircumference
CEMF = (MPH/TopSpeed)
local DBMotorVoltage = ((AxleRPM/MaxDynamicCurrent)*DynamicPowerZone)*100
local DBMotorCurrent = ((DBMotorVoltage-4)*CEMF)*1
if(!DynamicShort & !GroundRelay){
CurrentBO = -(DBMotorCurrent/(GRID_RESISTOR_OHMS))*2.5
}
elseif(DynamicShort | GroundRelay){
CurrentBO = 0
}
}
}
else{
CurrentBO = 0
}
if(DynamicNotch != 2){
CurrentBO = 0
Current = 0
TE = 0
}
}
}
else{
CurrentBO = 0
Current = 0
TE = 0
}
}
else{
CurrentBO = 0
Current = 0
TE = 0
}
}
else{
CurrentBO = 0
Current = 0
TE = 0
}
if(Dynamics == 1 & DynamicNotch < 2){
Dynamics = 0
}
if(DynamicNotch > 0 & DynamicPowerZone == 0){
CurrentBO = 0
Current = 0
TE = 0
}
Current = clamp(CurrentBO,-MaxDynamicCurrent,MaxTractionMotorCurrent)
if(CONTROL_STAND_DATA[16,number] != 0){
if(CONTROL_STAND_DATA[12,number]>0){
if(DynamicsMode>0){
if(!DynamicShort){
if(Current<(-MaxContinuousDynamicCurrent)){
DynamicTemp += (Current/(DynamicsMode == 1 ? 100 : 200))
DynamicBrakeWarning = 1
}
if(DynamicTemp>150 & DynamicTemp<450){
DBArking = 1
if(changed(DBArking)&DBArking){
soundPlay("DB-Arking",0,"gsgtrainsounds/misc/electrical_arking.wav")
soundPlay(0,4.2,"ambient/alarms/alarm_citizen_loop1.wav")
print("Warning your Dynmaics becoming overheated! Reduce your dynmaics to: "+MaxContinuousDynamicCurrent+" Amps or further damage will occour.")
}
}
}
if(DynamicTemp>450){
DynamicShort = 1
local DBShort = 1
if(changed(DBShort)&DBShort){
soundPlay("DB-Flashover",0,"ambient/energy/weld2.wav")
}
if(DBArking & GroundRelay){
soundStop("DB-Arking",5)
DBArking = 0
}
}
}
}
}
soundVolume("DB-Arking",((DynamicTemp-150)/100))
if(CONTROL_STAND_DATA[16,number] != 0){
if(CONTROL_STAND_DATA[12,number] == 0){
if(CONTROL_STAND_DATA[18,number]>0){
if(!MotorShort){
if(Current>MaxContinuousTractionMotorCurrent){
MotorTemp += ((Current/1000)/4)
}
if(MotorTemp>1000 & MotorTemp<1500){
TMArking = 1
if(changed(TMArking)&TMArking){
soundPlay("TM-Arking",0,"gsgtrainsounds/misc/electrical_arking.wav")
soundPlay(0,4.2,"ambient/alarms/alarm_citizen_loop1.wav")
print("Warning your Traction Motors becoming overheated! Reduce your throttle to: "+MaxContinuousTractionMotorCurrent+" Amps or further damage will occour.")
}
}
}
if(MotorTemp>1500){
MotorShort = 1
local TMShort = 1
if(changed(TMShort)&TMShort){
soundPlay("TM-Flashover",0,"ambient/energy/weld2.wav")
}
if(TMArking & GroundRelay){
soundStop("TM-Arking",5)
TMArking = 0
}
}
}
}
}
soundVolume("TM-Arking",((MotorTemp-1000)/300))
if(Current>-MaxContinuousDynamicCurrent){
if(round(DynamicTemp)>0){
DynamicTemp -= (CONTROL_STAND_DATA[12,number] ? 0.5 : 0.25)
}
if(DynamicTemp<0){
DynamicTemp = 0
}
DynamicBrakeWarning = 0
}
if(Current<MaxContinuousTractionMotorCurrent){
if(round(MotorTemp)>0){
MotorTemp -= 5
}
if(MotorTemp<0){
MotorTemp = 0
}
}
if(changed(DynamicShort)&DynamicShort){
GroundRelay = 1
}
if(changed(MotorShort)&MotorShort){
GroundRelay = 1
}
if(changed(GroundRelay)&GroundRelay){
W:soundPlay("LocomotiveAlarmBell",0,AlarmBellStart)
}
elseif(changed(CONTROL_STAND_DATA[25,number])&CONTROL_STAND_DATA[25,number]){
GroundRelay = 0
W:soundPlay("LocomotiveAlarmBell",0,AlarmBellEnd)
}
if(LOCOMOTIVE_AIRBRAKE_DATA[4,number]<5){
BEVLB = 0
BE = 0
}
elseif(LOCOMOTIVE_AIRBRAKE_DATA[4,number]>5){
BEVLB = 40000*(abs((LOCOMOTIVE_AIRBRAKE_DATA[4,number]))/65)
BE = (LOCOMOTIVE_AIRBRAKE_DATA[4,number]*(Holding ? 0 : BEVLB*(ClaspBrakes ? 1.25 : 1)))
}
if((Holding & round(LOCOMOTIVE_AIRBRAKE_DATA[3,number])<(FeedPressure))){
AutoMux = 1
}
elseif((!Holding & round(LOCOMOTIVE_AIRBRAKE_DATA[3,number])>(FeedPressure-1))){
AutoMux = 0
}
if(CONTROL_STAND_DATA[7,number] == 1){
if(CONTROL_STAND_DATA[26,number] == 0){
if(round(LOCOMOTIVE_AIRBRAKE_DATA[3,number])<(FeedPressure-1)){
if(MaxTrainBrakeForce == 0){
BEAuto = ((round(LOCOMOTIVE_AIRBRAKE_DATA[3,number])-(FeedPressure))*(AutoMux ? 0 : 150000))/(CONTROL_STAND_DATA[6,number] ? 2.5 : 1.45)
}
elseif(MaxTrainBrakeForce>0){
BEAuto = ((MaxTrainBrakeForce)*(round(1+LOCOMOTIVE_AIRBRAKE_DATA[3,number])-(FeedPressure))*(AutoMux ? 0 : 40))/(CONTROL_STAND_DATA[6,number] ? 2.5 : 1.45)
}
}
else{
BEAuto = 0
}
}
elseif(CONTROL_STAND_DATA[26,number] == 1){
BEAuto = 0
}
}
elseif(CONTROL_STAND_DATA[7,number] == 0){
BEAuto = 0
}
if((LOCOMOTIVE_AIRBRAKE_DATA[2,number]<(FeedPressure-1) & !BHQ & MPH<1)){
Holding = 1
BHQ = 1
abRest(Trucks)
}
if((LOCOMOTIVE_AIRBRAKE_DATA[4,number]>5) & !BHQ & MPH<1){
Holding = 1
BHQ = 1
abRest(Trucks)
}
if(HandBrake){
Holding = 1
BHQ = 1
abRest(Trucks)
}
local Overcome = (abs(TE) - BE)>0
if(((LOCOMOTIVE_AIRBRAKE_DATA[2,number]>(FeedPressure-1) & LOCOMOTIVE_AIRBRAKE_DATA[4,number]<5 & !HandBrake & BHQ))){
Holding = 0
BHQ = 0
abRelease()
}
elseif(Overcome == 1){
Holding = 0
BHQ = 0
abRelease()
}
if(clk("fixnil")){
Load = 0
TE = 0
Force = 0
FixNils = 1
stoptimer("fixnil")
}
if(changed(CONTROL_STAND_DATA[30,number])&CONTROL_STAND_DATA[30,number] & !Sander){
Sander = 1
}
elseif(changed(CONTROL_STAND_DATA[30,number])&!CONTROL_STAND_DATA[30,number] & Sander){
Sander = 0
}
local ForceSand = CONTROL_STAND_DATA[31,number]
if(!CONTROL_STAND_DATA[33,number]){
if(Sander | ForceSand){
Sanding = 1
}
elseif(!Sander & !ForceSand){
Sanding = 0
}
}
else{
Sanding = 1
}
if(changed(Sanding)&Sanding){
soundPlay("sander",0,CONFIGURATION_DATA[83,string])
}
if(changed(Sanding)&!Sanding){
soundStop("sander")
soundPlay("sander",0,CONFIGURATION_DATA[84,string])
}
local FrictionCoefficient = (Sanding ? 1 : getWeatherCondition())
local InperfectionFactor = 1.5
local SlipCoefficient = TE_Effective/WeightOnAxle*((TrainWeight/2)/((1+Load)*85))
local FrictionFactor = (1+(round(-(LOCOMOTIVE_AIRBRAKE_DATA[3,number]-(FeedPressure)))/(FeedPressure)))
SlippingFactor = ((SlipCoefficient/(FrictionFactor^2))/(1+((MPH/TopSpeed)/10)))*(random()*InperfectionFactor)
if(!MotorShort){
if(Current>0){
if(SlippingFactor > FrictionCoefficient){
WheelSlip = 1
LoadAfterSlip = (Load-randint(0,Load))
}
elseif(SlippingFactor < FrictionCoefficient){
WheelSlip = 0
LoadAfterSlip = Load
}
}
else{
WheelSlip = 0
LoadAfterSlip = Load
}
}
else{
WheelSlip = 0
LoadAfterSlip = Load
}
if(changed(WheelSlip)&WheelSlip){
W:soundPlay("wheelsliploop",0,CONFIGURATION_DATA[56,string])
}
if(changed(WheelSlip)&!WheelSlip){
soundStop("wheelsliploop")
W:soundPlay("wheelslipend",0,CONFIGURATION_DATA[57,string])
}
if(FixNils){
local Curve = CONFIGURATION_DATA[79,number]
local LowSpeedCurveEnable = CONFIGURATION_DATA[80,number]
if(LowSpeedCurveEnable == 1){
if(Dynamics == 0){
LowSpeedCurve = clamp(Current,LowSpeedCurve-Curve,LowSpeedCurve+2)
}
elseif(Dynamics == 1){
LowSpeedCurve = Curve*10
}
if(LowSpeedCurve >= Curve*10){
LowSpeedCurve = Curve*10
}
}
else{
LowSpeedCurve = Curve*10
}
Load = clamp(Current,Load-(Curve),Load+(abs(LowSpeedCurve/10)))
TE_EffectiveLoad = LoadAfterSlip/(1+(round(-(LOCOMOTIVE_AIRBRAKE_DATA[3,number]-(FeedPressure)))/(FeedPressure)))
TE_Effective = TE_EffectiveLoad
}
if(CONTROL_STAND_DATA[21,number]){
TotalMotors = Motors+CONTROL_STAND_DATA[32,number]
}
else{
TotalMotors = Motors
}
if(MPH > TopSpeed){
TE = 0
}
elseif(MPH < TopSpeed){
if(CONTROL_STAND_DATA[12,number] == 2){
DynoHPINCR = 1.8
}
elseif(CONTROL_STAND_DATA[12,number]<2){
DynoHPINCR = 1.8
}
TE = (TE_Effective*(BaseSpeed/TopSpeed)*TotalMotors*MaxGeneratorCurrent/8)*(0.85*DynoHPINCR)*((0.25+clamp(MPH,0,15)/15)/LSX)
}
if(BHQ){
TE = 0
}
BEF = abs(BE)
BEAutoF = abs(BEAuto)
if(Dynamics == 0){
TELocal = TE*sign(CONTROL_STAND_DATA[16,number])
}
elseif(Dynamics == 1){
TELocal = TE*sign(V)
}
if(MPH>=0.5){
BEForce = BEF + BEAutoF
if(abs(BEF)>0){
Braking_Loco = 1
}
elseif(abs(BEF)<=0){
Braking_Loco = 0
}
if(abs(BEAutoF)>0){
Braking_Train = 1
}
elseif(abs(BEAutoF)<=0){
Braking_Train = 0
}
}
if(MPH<0.5){
BEForce = 0
Braking_Loco = 0
Braking_Train = 0
}
Force = TELocal - (BEForce*sign(V))
if(HasTM){
W:applyForce(-E:up()*(Force/5))
}
LOCOMOTIVE_PROCESSOR = array(CarCount,LoadAfterSlip,MPH,TrainWeight,Braking_Loco,Braking_Train,V,E,O,W,DynamicTemp,MotorTemp,DynamicShort,MotorShort,GroundRelay,DynamicBrakeWarning,WheelSlip,
Motors,RampedMotorVoltage,HEP_Notch,EngineRPMSmoothed)