/
TTTrain.cs
15321 lines (13364 loc) · 682 KB
/
TTTrain.cs
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// COPYRIGHT 2013 by the Open Rails project.
//
// This file is part of Open Rails.
//
// Open Rails is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Open Rails is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
/* AI
*
* Contains code to initialize and control AI trains.
*
*/
// #define DEBUG_REPORTS
// #define DEBUG_CHECKTRAIN
// #define DEBUG_DEADLOCK
// #define DEBUG_EXTRAINFO
// #define DEBUG_TRACEINFO
// #define DEBUG_TTANALYSIS
// #define DEBUG_DELAYS
// DEBUG flag for debug prints
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Orts.Formats.Msts;
using Orts.Simulation.AIs;
using Orts.Simulation.Physics;
using Orts.Simulation.RollingStocks;
using Orts.Simulation.Signalling;
using ORTS.Common;
using Event = Orts.Common.Event;
namespace Orts.Simulation.Timetables
{
public class TTTrain : AITrain
{
public float DefMaxDecelMpSSP = 1.0f; // maximum decelleration
public float DefMaxAccelMpSSP = 1.0f; // maximum accelleration
public float DefMaxDecelMpSSF = 0.8f; // maximum decelleration
public float DefMaxAccelMpSSF = 0.5f; // maximum accelleration
public bool Closeup = false; // closeup to other train when stabling
public static float keepDistanceCloseupM = 2.5f; // stay 2.5m from end of route when closeup required (for stabling only)
public static float keepDistanceTrainAheadCloseupM = 0.5f; // stay 0.5m from train ahead when closeup required (for stabling only)
public static float keepDistanceCloseupSignalM = 7.0f; // stay 10m from signal ahead when signalcloseup required
public static float endOfRouteDistance = 150f; // Max length to remain for train to continue on route
public int? ActivateTime; // time train is activated
public bool TriggeredActivationRequired = false; // train activation is triggered by other train
public bool Created = false; // train is created at start
public string CreateAhead = String.Empty; // train is created ahead of other train
public string CreateInPool = String.Empty; // train is to be created in pool at start of timetable
public string CreateFromPool = String.Empty; // train is to be created from pool
public TimetablePool.PoolExitDirectionEnum CreatePoolDirection = TimetablePool.PoolExitDirectionEnum.Undefined;
// required direction on leaving pool (if applicable)
public string ForcedConsistName = String.Empty; // forced consist name for extraction from pool
public string ttanalysisreport = String.Empty; // string holding last analysis report, to avoid continouos output of same string
// Timetable Commands info
public List<WaitInfo> WaitList = null; //used when in timetable mode for wait instructions
public Dictionary<int, List<WaitInfo>> WaitAnyList = null; //used when in timetable mode for waitany instructions
public bool Stable_CallOn = false; //used when in timetable mode to show stabled train is allowed to call on
public bool DriverOnlyOperation = false; //used when in timetable mode to indicate driver only operation
public bool ForceReversal = false; //used when in timetable mode to force reversal at diverging point ignoring signals
public enum FormCommand //enum to indicate type of form sequence
{
TerminationFormed,
TerminationTriggered,
Detached,
Created,
None,
}
public int Forms = -1; //indicates which train is to be formed out of this train on termination
public bool FormsStatic = false; //indicate if train is to remain as static
public string ExitPool = String.Empty; //set if train is to be stabled in pool
public int PoolAccessSection = -1; //set to last section index if train is to be stabled in pool, section is access section to pool
public enum PoolAccessState // used to indicate access state to pool, combined with storage index
// values are <0, value >= 0 is returned storage index
{
PoolClaimed = -1,
PoolOverflow = -2,
PoolInvalid = -3,
}
public int PoolStorageIndex = -1; // index in selected pool path (>=0)
public TimetablePool.PoolExitDirectionEnum PoolExitDirection = TimetablePool.PoolExitDirectionEnum.Undefined;
// required exit direction from pool (if applicable)
public TimetableTurntableControl ActiveTurntable = null; //active turntable
public int FormedOf = -1; //indicates out of which train this train is formed
public FormCommand FormedOfType = FormCommand.None; //indicates type of formed-of command
public int OrgAINumber = -1; //original AI number of formed player train
public bool SetStop = false; //indicates train must copy station stop from formed train
public bool FormsAtStation = false; //indicates train must form into next service at last station, route must be curtailed to that stop
public bool leadLocoAntiSlip = false; //anti slip indication for original leading engine
// detach details
public Dictionary<int, List<DetachInfo>> DetachDetails = new Dictionary<int, List<DetachInfo>>();
// key is platform reference (use -1 for detach at start or end), list is detach commands at that location
public int[] DetachActive = new int[2] { -1, -1 }; // detach is activated - first index is key in DetachDetails, second index is index in valuelist
// 2nd index = -1 indicates invalid (first index -1 is a valid index)
public int DetachUnits = 0; // no. of units to detach
public bool DetachPosition = false; // if true detach from front
public bool DetachPending = false; // true when player detach window is displayed
// attach details
public AttachInfo AttachDetails; // attach details
public Dictionary<int, List<int>> NeedAttach = new Dictionary<int, List<int>>();
// key is platform reference or -1 for attach to static train, list are trains which are to attach
// pickup details
public List<PickUpInfo> PickUpDetails = new List<PickUpInfo>(); // only used during train building
public List<int> PickUpTrains = new List<int>(); // list of train to be picked up
public List<int> PickUpStatic = new List<int>(); // index of locations where static consists are to be picked up
public bool PickUpStaticOnForms = false; // set if pickup of static is required when forming next train
public bool NeedPickUp = false; // indicates pickup is required
// transfer details
public Dictionary<int, TransferInfo> TransferStationDetails = new Dictionary<int, TransferInfo>();
// list of transfer to take place in station
public Dictionary<int, List<TransferInfo>> TransferTrainDetails = new Dictionary<int, List<TransferInfo>>();
// list of transfers defined per train - if int = -1, transfer is to be performed on static train
public bool NeedTransfer = false; // indicates transfer is required
public Dictionary<int, List<int>> NeedStationTransfer = new Dictionary<int, List<int>>();
// list of required station transfers, per station index
public Dictionary<int, int> NeedTrainTransfer = new Dictionary<int, int>();
// number of required train transfers per section
// delayed restart
public bool DelayedStart = false; // start is delayed
public float RestdelayS = 0.0f; // time to wait
public AITrain.AI_START_MOVEMENT DelayedStartState; // state to start
public struct DelayedStartBase
{
public int fixedPartS; // fixed part for restart delay
public int randomPartS; // random part for restart delay
}
public struct DelayedStartValues
{
public DelayedStartBase newStart; // delay on new start
public DelayedStartBase pathRestart; // delay on pathing stop restart (e.g. signal, reversal, node)
public DelayedStartBase followRestart; // delay on restart when following other train
public DelayedStartBase stationRestart; // delay on restart from station stop
public DelayedStartBase attachRestart; // delay on restart after attaching
public DelayedStartBase detachRestart; // delay between stop and detaching
public DelayedStartBase movingtableRestart; // delay for movement of train and moving table
public float reverseAddedDelaySperM; // additional delay on reversal based on train length
}
public struct SpeedValues
{
public float? maxSpeedMpS; // timetable defined max speed
public float? cruiseSpeedMpS; // timetable defined cruise speed
public int? cruiseMaxDelayS; // max. delay to maintain cruise speed
public float? creepSpeedMpS; // timetable defined creep speed
public float? attachSpeedMpS; // timetable defined attach speed
public float? detachSpeedMpS; // timetable defined detach speed
public float? movingtableSpeedMpS; // timetable defined speed for moving tables
public float routeSpeedMpS; // route defined max speed
public float consistSpeedMpS; // consist defined max speed
public bool restrictedSet; // special speed has been set
}
public DelayedStartValues DelayedStartSettings = new DelayedStartValues();
public SpeedValues SpeedSettings = new SpeedValues();
// special patch conditions
public enum LastSignalStop
{
None,
Last,
Reverse,
}
public LastSignalStop ReqLastSignalStop = LastSignalStop.None;
public enum TriggerActivationType
{
Start,
Dispose,
StationStop,
StationDepart,
}
public struct TriggerActivation
{
public int activatedTrain; // train to be activated
public TriggerActivationType activationType; // type of activation
public int platformId; // trigger platform ident (in case of station stop)
public string activatedName; // name of activated train (used in processing timetable only)
}
public List<TriggerActivation> activatedTrainTriggers = new List<TriggerActivation>();
public string Briefing { get; set; } = "";
//================================================================================================//
/// <summary>
/// Constructor
/// <\summary>
public TTTrain(Simulator simulator)
: base(simulator)
{
// set AI reference
AI = simulator.AI;
// preset accel and decel values
MaxAccelMpSSP = DefMaxAccelMpSSP;
MaxAccelMpSSF = DefMaxAccelMpSSF;
MaxDecelMpSSP = DefMaxDecelMpSSP;
MaxDecelMpSSF = DefMaxDecelMpSSF;
// preset movement state
MovementState = AI_MOVEMENT_STATE.AI_STATIC;
// preset restart delays
DelayedStartSettings.newStart.fixedPartS = 0;
DelayedStartSettings.newStart.randomPartS = 10;
DelayedStartSettings.pathRestart.fixedPartS = 1;
DelayedStartSettings.pathRestart.randomPartS = 10;
DelayedStartSettings.followRestart.fixedPartS = 15;
DelayedStartSettings.followRestart.randomPartS = 10;
DelayedStartSettings.stationRestart.fixedPartS = 0;
DelayedStartSettings.stationRestart.randomPartS = 15;
DelayedStartSettings.attachRestart.fixedPartS = 30;
DelayedStartSettings.attachRestart.randomPartS = 30;
DelayedStartSettings.detachRestart.fixedPartS = 5;
DelayedStartSettings.detachRestart.randomPartS = 20;
DelayedStartSettings.movingtableRestart.fixedPartS = 1;
DelayedStartSettings.movingtableRestart.randomPartS = 10;
DelayedStartSettings.reverseAddedDelaySperM = 0.5f;
// preset speed values
SpeedSettings.maxSpeedMpS = null;
SpeedSettings.cruiseSpeedMpS = null;
SpeedSettings.cruiseMaxDelayS = null;
SpeedSettings.creepSpeedMpS = null;
SpeedSettings.attachSpeedMpS = null;
SpeedSettings.detachSpeedMpS = null;
SpeedSettings.movingtableSpeedMpS = null;
SpeedSettings.restrictedSet = false;
}
//================================================================================================//
/// <summary>
/// Constructor using existing train
/// <\summary>
public TTTrain(Simulator simulator, TTTrain TTrain)
: base(simulator)
{
// set AI reference
AI = simulator.AI;
// preset accel and decel values
MaxAccelMpSSP = DefMaxAccelMpSSP;
MaxAccelMpSSF = DefMaxAccelMpSSF;
MaxDecelMpSSP = DefMaxDecelMpSSP;
MaxDecelMpSSF = DefMaxDecelMpSSF;
// preset movement state
MovementState = AI_MOVEMENT_STATE.AI_STATIC;
// copy restart delays
DelayedStartSettings = TTrain.DelayedStartSettings;
// copy speed values
SpeedSettings = TTrain.SpeedSettings;
}
//================================================================================================//
/// <summary>
/// Restore
/// <\summary>
public TTTrain(Simulator simulator, BinaryReader inf, AI airef)
: base(simulator, inf, airef)
{
// TTTrains own additional fields
Closeup = inf.ReadBoolean();
Created = inf.ReadBoolean();
CreateAhead = inf.ReadString();
CreateFromPool = inf.ReadString();
CreateInPool = inf.ReadString();
ForcedConsistName = inf.ReadString();
CreatePoolDirection = (TimetablePool.PoolExitDirectionEnum)inf.ReadInt32();
MaxAccelMpSSP = inf.ReadSingle();
MaxAccelMpSSF = inf.ReadSingle();
MaxDecelMpSSP = inf.ReadSingle();
MaxDecelMpSSF = inf.ReadSingle();
int activateTimeValue = inf.ReadInt32();
if (activateTimeValue < 0)
{
ActivateTime = null;
}
else
{
ActivateTime = activateTimeValue;
}
TriggeredActivationRequired = inf.ReadBoolean();
int triggerActivations = inf.ReadInt32();
activatedTrainTriggers = new List<TriggerActivation>();
for (int itrigger = 0; itrigger < triggerActivations; itrigger++)
{
TriggerActivation newTrigger = new TriggerActivation();
newTrigger.activatedName = inf.ReadString();
newTrigger.activatedTrain = inf.ReadInt32();
newTrigger.activationType = (TriggerActivationType)inf.ReadInt32();
newTrigger.platformId = inf.ReadInt32();
activatedTrainTriggers.Add(newTrigger);
}
int totalWait = inf.ReadInt32();
if (totalWait > 0)
{
WaitList = new List<WaitInfo>();
for (int iWait = 0; iWait < totalWait; iWait++)
{
WaitList.Add(new WaitInfo(inf));
}
}
int totalWaitAny = inf.ReadInt32();
if (totalWaitAny > 0)
{
WaitAnyList = new Dictionary<int, List<WaitInfo>>();
for (int iWait = 0; iWait < totalWaitAny; iWait++)
{
int keyvalue = inf.ReadInt32();
List<WaitInfo> newList = new List<WaitInfo>();
int totalWaitInfo = inf.ReadInt32();
for (int iWinfo = 0; iWinfo < totalWaitInfo; iWinfo++)
{
newList.Add(new WaitInfo(inf));
}
WaitAnyList.Add(keyvalue, newList);
}
}
Stable_CallOn = inf.ReadBoolean();
Forms = inf.ReadInt32();
FormsStatic = inf.ReadBoolean();
ExitPool = inf.ReadString();
PoolAccessSection = inf.ReadInt32();
PoolStorageIndex = inf.ReadInt32();
PoolExitDirection = (TimetablePool.PoolExitDirectionEnum)inf.ReadInt32();
ActiveTurntable = null;
if (inf.ReadBoolean())
{
ActiveTurntable = new TimetableTurntableControl(inf, AI.Simulator, this);
}
FormedOf = inf.ReadInt32();
FormedOfType = (FormCommand)inf.ReadInt32();
OrgAINumber = inf.ReadInt32();
SetStop = inf.ReadBoolean();
FormsAtStation = inf.ReadBoolean();
int totalDetachLists = inf.ReadInt32();
DetachDetails = new Dictionary<int, List<DetachInfo>>();
for (int iDetachList = 0; iDetachList < totalDetachLists; iDetachList++)
{
int detachKey = inf.ReadInt32();
int totalDetach = inf.ReadInt32();
List<DetachInfo> DetachDetailsList = new List<DetachInfo>();
for (int iDetach = 0; iDetach < totalDetach; iDetach++)
{
DetachDetailsList.Add(new DetachInfo(inf));
}
DetachDetails.Add(detachKey, DetachDetailsList);
}
DetachActive = new int[2];
DetachActive[0] = inf.ReadInt32();
DetachActive[1] = inf.ReadInt32();
DetachUnits = inf.ReadInt32();
DetachPosition = inf.ReadBoolean();
DetachPending = inf.ReadBoolean();
bool attachValid = inf.ReadBoolean();
AttachDetails = null;
if (attachValid)
{
AttachDetails = new AttachInfo(inf);
}
PickUpTrains = new List<int>();
int totalPickUpTrains = inf.ReadInt32();
for (int iPickUp = 0; iPickUp < totalPickUpTrains; iPickUp++)
{
PickUpTrains.Add(inf.ReadInt32());
}
PickUpStatic = new List<int>();
int totalPickUpStatic = inf.ReadInt32();
for (int iPickUp = 0; iPickUp < totalPickUpStatic; iPickUp++)
{
PickUpStatic.Add(inf.ReadInt32());
}
PickUpStaticOnForms = inf.ReadBoolean();
NeedPickUp = inf.ReadBoolean();
TransferStationDetails = new Dictionary<int, TransferInfo>();
int totalStationTransfers = inf.ReadInt32();
for (int iTransferList = 0; iTransferList < totalStationTransfers; iTransferList++)
{
int stationKey = inf.ReadInt32();
TransferInfo thisTransfer = new TransferInfo(inf);
TransferStationDetails.Add(stationKey, thisTransfer);
}
TransferTrainDetails = new Dictionary<int, List<TransferInfo>>();
int totalTrainTransfers = inf.ReadInt32();
for (int iTransferList = 0; iTransferList < totalTrainTransfers; iTransferList++)
{
int trainKey = inf.ReadInt32();
int totalTransfer = inf.ReadInt32();
List<TransferInfo> thisTransferList = new List<TransferInfo>();
for (int iTransfer = 0; iTransfer < totalTransfer; iTransfer++)
{
TransferInfo thisTransfer = new TransferInfo(inf);
thisTransferList.Add(thisTransfer);
}
TransferTrainDetails.Add(trainKey, thisTransferList);
}
int totalNeedAttach = inf.ReadInt32();
NeedAttach = new Dictionary<int, List<int>>();
for (int iNeedAttach = 0; iNeedAttach < totalNeedAttach; iNeedAttach++)
{
int needAttachKey = inf.ReadInt32();
int totalNeedAttachInfo = inf.ReadInt32();
List<int> allNeedAttachInfo = new List<int>();
for (int iNeedInfo = 0; iNeedInfo < totalNeedAttachInfo; iNeedInfo++)
{
int needAttachInfo = inf.ReadInt32();
allNeedAttachInfo.Add(needAttachInfo);
}
NeedAttach.Add(needAttachKey, allNeedAttachInfo);
}
int totalNeedStationTransfer = inf.ReadInt32();
NeedStationTransfer = new Dictionary<int, List<int>>();
for (int iNeedTransferList = 0; iNeedTransferList < totalNeedStationTransfer; iNeedTransferList++)
{
int transferStationKey = inf.ReadInt32();
List<int> tempList = new List<int>();
int totalTransfers = inf.ReadInt32();
for (int iTransfer = 0; iTransfer < totalTransfers; iTransfer++)
{
tempList.Add(inf.ReadInt32());
}
NeedStationTransfer.Add(transferStationKey, tempList);
}
int totalNeedTrainTransfer = inf.ReadInt32();
NeedTrainTransfer = new Dictionary<int,int>();
for (int iNeedTransferList = 0; iNeedTransferList < totalNeedTrainTransfer; iNeedTransferList++)
{
int transferTrainKey = inf.ReadInt32();
int transferTrainValue = inf.ReadInt32();
NeedTrainTransfer.Add(transferTrainKey, transferTrainValue);
}
DelayedStartSettings = new DelayedStartValues();
DelayedStartSettings.newStart.fixedPartS = inf.ReadInt32();
DelayedStartSettings.newStart.randomPartS = inf.ReadInt32();
DelayedStartSettings.pathRestart.fixedPartS = inf.ReadInt32();
DelayedStartSettings.pathRestart.randomPartS = inf.ReadInt32();
DelayedStartSettings.followRestart.fixedPartS = inf.ReadInt32();
DelayedStartSettings.followRestart.randomPartS = inf.ReadInt32();
DelayedStartSettings.stationRestart.fixedPartS = inf.ReadInt32();
DelayedStartSettings.stationRestart.randomPartS = inf.ReadInt32();
DelayedStartSettings.attachRestart.fixedPartS = inf.ReadInt32();
DelayedStartSettings.attachRestart.randomPartS = inf.ReadInt32();
DelayedStartSettings.detachRestart.fixedPartS = inf.ReadInt32();
DelayedStartSettings.detachRestart.randomPartS = inf.ReadInt32();
DelayedStartSettings.movingtableRestart.fixedPartS = inf.ReadInt32();
DelayedStartSettings.movingtableRestart.randomPartS = inf.ReadInt32();
DelayedStartSettings.reverseAddedDelaySperM = inf.ReadSingle();
DelayedStart = inf.ReadBoolean();
DelayedStartState = (AI_START_MOVEMENT)inf.ReadInt32();
RestdelayS = inf.ReadSingle();
// preset speed values
SpeedSettings = new SpeedValues();
SpeedSettings.routeSpeedMpS = inf.ReadSingle();
SpeedSettings.consistSpeedMpS = inf.ReadSingle();
if (inf.ReadBoolean())
{
SpeedSettings.maxSpeedMpS = inf.ReadSingle();
}
else
{
SpeedSettings.maxSpeedMpS = null;
}
if (inf.ReadBoolean())
{
SpeedSettings.cruiseSpeedMpS = inf.ReadSingle();
}
else
{
SpeedSettings.cruiseSpeedMpS = null;
}
if (inf.ReadBoolean())
{
SpeedSettings.cruiseMaxDelayS = inf.ReadInt32();
}
else
{
SpeedSettings.cruiseMaxDelayS = null;
}
if (inf.ReadBoolean())
{
SpeedSettings.creepSpeedMpS = inf.ReadSingle();
}
else
{
SpeedSettings.creepSpeedMpS = null;
}
if (inf.ReadBoolean())
{
SpeedSettings.attachSpeedMpS = inf.ReadSingle();
}
else
{
SpeedSettings.attachSpeedMpS = null;
}
if (inf.ReadBoolean())
{
SpeedSettings.detachSpeedMpS = inf.ReadSingle();
}
else
{
SpeedSettings.detachSpeedMpS = null;
}
if (inf.ReadBoolean())
{
SpeedSettings.movingtableSpeedMpS = inf.ReadSingle();
}
else
{
SpeedSettings.movingtableSpeedMpS = null;
}
SpeedSettings.restrictedSet = inf.ReadBoolean();
DriverOnlyOperation = inf.ReadBoolean();
ForceReversal = inf.ReadBoolean();
Briefing = inf.ReadString();
// reset actions if train is active
bool activeTrain = true;
if (TrainType == TRAINTYPE.AI_NOTSTARTED) activeTrain = false;
if (TrainType == TRAINTYPE.AI_AUTOGENERATE) activeTrain = false;
if (activeTrain)
{
if (MovementState == AI_MOVEMENT_STATE.AI_STATIC || MovementState == AI_MOVEMENT_STATE.INIT) activeTrain = false;
}
if (activeTrain)
{
ResetActions(true);
}
}
//================================================================================================//
/// <summary>
/// Save
/// Override from Train class
/// </summary>
public override void Save(BinaryWriter outf)
{
outf.Write("TT");
SaveBase(outf);
outf.Write(UiD);
outf.Write(MaxDecelMpSS);
outf.Write(MaxAccelMpSS);
if (StartTime.HasValue)
{
outf.Write(StartTime.Value);
}
else
{
outf.Write(-1);
}
outf.Write(PowerState);
outf.Write(Alpha10);
outf.Write((int)MovementState);
outf.Write(Efficiency);
outf.Write(MaxVelocityA);
outf.Write(UncondAttach);
outf.Write(doorCloseAdvance);
outf.Write(doorOpenDelay);
// dummy for level crossing horn pattern
outf.Write(-1);
// dummy for service list count
outf.Write(-1);
//TTTrains own additional fields
outf.Write(Closeup);
outf.Write(Created);
outf.Write(CreateAhead);
outf.Write(CreateFromPool);
outf.Write(CreateInPool);
outf.Write(ForcedConsistName);
outf.Write((int)CreatePoolDirection);
outf.Write(MaxAccelMpSSP);
outf.Write(MaxAccelMpSSF);
outf.Write(MaxDecelMpSSP);
outf.Write(MaxDecelMpSSF);
if (ActivateTime.HasValue)
{
outf.Write(ActivateTime.Value);
}
else
{
outf.Write(-1);
}
outf.Write(TriggeredActivationRequired);
if (activatedTrainTriggers.Count > 0)
{
outf.Write(activatedTrainTriggers.Count);
foreach (TriggerActivation thisTrigger in activatedTrainTriggers)
{
outf.Write(thisTrigger.activatedName);
outf.Write(thisTrigger.activatedTrain);
outf.Write((int)thisTrigger.activationType);
outf.Write(thisTrigger.platformId);
}
}
else
{
outf.Write(-1);
}
if (WaitList == null)
{
outf.Write(-1);
}
else
{
outf.Write(WaitList.Count);
foreach (WaitInfo thisWait in WaitList)
{
thisWait.Save(outf);
}
}
if (WaitAnyList == null)
{
outf.Write(-1);
}
else
{
outf.Write(WaitAnyList.Count);
foreach (KeyValuePair<int, List<WaitInfo>> thisWInfo in WaitAnyList)
{
outf.Write((int)thisWInfo.Key);
List<WaitInfo> thisWaitList = thisWInfo.Value;
outf.Write(thisWaitList.Count);
foreach (WaitInfo thisWaitInfo in thisWaitList)
{
thisWaitInfo.Save(outf);
}
}
}
outf.Write(Stable_CallOn);
outf.Write(Forms);
outf.Write(FormsStatic);
outf.Write(ExitPool);
outf.Write(PoolAccessSection);
outf.Write(PoolStorageIndex);
outf.Write((int)PoolExitDirection);
if (ActiveTurntable == null)
{
outf.Write(false);
}
else
{
outf.Write(true);
ActiveTurntable.Save(outf);
}
outf.Write(FormedOf);
outf.Write((int)FormedOfType);
outf.Write(OrgAINumber);
outf.Write(SetStop);
outf.Write(FormsAtStation);
outf.Write(DetachDetails.Count);
foreach (KeyValuePair<int, List<DetachInfo>> thisDetachInfo in DetachDetails)
{
outf.Write(thisDetachInfo.Key);
outf.Write(thisDetachInfo.Value.Count);
foreach (DetachInfo thisDetach in thisDetachInfo.Value)
{
thisDetach.Save(outf);
}
}
outf.Write(DetachActive[0]);
outf.Write(DetachActive[1]);
outf.Write(DetachUnits);
outf.Write(DetachPosition);
outf.Write(DetachPending);
if (AttachDetails == null)
{
outf.Write(false);
}
else
{
outf.Write(true);
AttachDetails.Save(outf);
}
outf.Write(PickUpTrains.Count);
foreach (int thisTrainNumber in PickUpTrains)
{
outf.Write(thisTrainNumber);
}
outf.Write(PickUpStatic.Count);
foreach (int thisStaticNumber in PickUpStatic)
{
outf.Write(thisStaticNumber);
}
outf.Write(PickUpStaticOnForms);
outf.Write(NeedPickUp);
outf.Write(TransferStationDetails.Count);
foreach (KeyValuePair<int, TransferInfo> thisStationTransfer in TransferStationDetails)
{
outf.Write(thisStationTransfer.Key);
TransferInfo thisTransfer = thisStationTransfer.Value;
thisTransfer.Save(outf);
}
outf.Write(TransferTrainDetails.Count);
foreach (KeyValuePair<int, List<TransferInfo>> thisTrainTransfer in TransferTrainDetails)
{
outf.Write(thisTrainTransfer.Key);
List<TransferInfo> thisTransferList = thisTrainTransfer.Value;
outf.Write(thisTransferList.Count);
foreach (TransferInfo thisTransfer in thisTransferList)
{
thisTransfer.Save(outf);
}
}
outf.Write(NeedAttach.Count);
foreach (KeyValuePair<int, List<int>> thisNeedAttach in NeedAttach)
{
outf.Write(thisNeedAttach.Key);
outf.Write(thisNeedAttach.Value.Count);
foreach (int needAttachInfo in thisNeedAttach.Value)
{
outf.Write(needAttachInfo);
}
}
outf.Write(NeedStationTransfer.Count);
foreach (KeyValuePair<int, List<int>> thisNeedTransfer in NeedStationTransfer)
{
outf.Write(thisNeedTransfer.Key);
outf.Write(thisNeedTransfer.Value.Count);
foreach (int needTransferInfo in thisNeedTransfer.Value)
{
outf.Write(needTransferInfo);
}
}
outf.Write(NeedTrainTransfer.Count);
foreach (KeyValuePair<int,int> thisNeedTransfer in NeedTrainTransfer)
{
outf.Write(thisNeedTransfer.Key);
outf.Write(thisNeedTransfer.Value);
}
outf.Write(DelayedStartSettings.newStart.fixedPartS);
outf.Write(DelayedStartSettings.newStart.randomPartS);
outf.Write(DelayedStartSettings.pathRestart.fixedPartS);
outf.Write(DelayedStartSettings.pathRestart.randomPartS);
outf.Write(DelayedStartSettings.followRestart.fixedPartS);
outf.Write(DelayedStartSettings.followRestart.randomPartS);
outf.Write(DelayedStartSettings.stationRestart.fixedPartS);
outf.Write(DelayedStartSettings.stationRestart.randomPartS);
outf.Write(DelayedStartSettings.attachRestart.fixedPartS);
outf.Write(DelayedStartSettings.attachRestart.randomPartS);
outf.Write(DelayedStartSettings.detachRestart.fixedPartS);
outf.Write(DelayedStartSettings.detachRestart.randomPartS);
outf.Write(DelayedStartSettings.movingtableRestart.fixedPartS);
outf.Write(DelayedStartSettings.movingtableRestart.randomPartS);
outf.Write(DelayedStartSettings.reverseAddedDelaySperM);
outf.Write(DelayedStart);
outf.Write((int)DelayedStartState);
outf.Write(RestdelayS);
outf.Write(SpeedSettings.routeSpeedMpS);
outf.Write(SpeedSettings.consistSpeedMpS);
outf.Write(SpeedSettings.maxSpeedMpS.HasValue);
if (SpeedSettings.maxSpeedMpS.HasValue)
{
outf.Write(SpeedSettings.maxSpeedMpS.Value);
}
outf.Write(SpeedSettings.cruiseSpeedMpS.HasValue);
if (SpeedSettings.cruiseSpeedMpS.HasValue)
{
outf.Write(SpeedSettings.cruiseSpeedMpS.Value);
}
outf.Write(SpeedSettings.cruiseMaxDelayS.HasValue);
if (SpeedSettings.cruiseMaxDelayS.HasValue)
{
outf.Write(SpeedSettings.cruiseMaxDelayS.Value);
}
outf.Write(SpeedSettings.creepSpeedMpS.HasValue);
if (SpeedSettings.creepSpeedMpS.HasValue)
{
outf.Write(SpeedSettings.creepSpeedMpS.Value);
}
outf.Write(SpeedSettings.attachSpeedMpS.HasValue);
if (SpeedSettings.attachSpeedMpS.HasValue)
{
outf.Write(SpeedSettings.attachSpeedMpS.Value);
}
outf.Write(SpeedSettings.detachSpeedMpS.HasValue);
if (SpeedSettings.detachSpeedMpS.HasValue)
{
outf.Write(SpeedSettings.detachSpeedMpS.Value);
}
outf.Write(SpeedSettings.movingtableSpeedMpS.HasValue);
if (SpeedSettings.movingtableSpeedMpS.HasValue)
{
outf.Write(SpeedSettings.movingtableSpeedMpS.Value);
}
outf.Write(SpeedSettings.restrictedSet);
outf.Write(DriverOnlyOperation);
outf.Write(ForceReversal);
outf.Write(Briefing);
}
//================================================================================================//
/// <summary>
/// Terminate route at last signal in train's direction
/// </summary>
public void EndRouteAtLastSignal()
{
// no action required
if (ReqLastSignalStop == LastSignalStop.None)
{
return;
}
int lastIndex = TCRoute.TCRouteSubpaths.Count - 1;
TCSubpathRoute lastSubpath = new TCSubpathRoute(TCRoute.TCRouteSubpaths[lastIndex]);
int lastSectionIndex = -1;
// search for last signal in required direction
for (int iIndex = lastSubpath.Count - 1; iIndex >= 0 && lastSectionIndex < 0; iIndex--)
{
TrackCircuitSection thisSection = signalRef.TrackCircuitList[lastSubpath[iIndex].TCSectionIndex];
int reqEndSignal = ReqLastSignalStop == LastSignalStop.Last ? lastSubpath[iIndex].Direction : lastSubpath[iIndex].Direction == 0 ? 1 : 0;
if (thisSection.EndSignals[reqEndSignal] != null)
{
lastSectionIndex = iIndex;
}
}
// remove sections beyond last signal
for (int iIndex = lastSubpath.Count - 1; iIndex > lastSectionIndex; iIndex--)
{
lastSubpath.RemoveAt(iIndex);
}
// reinsert subroute
TCRoute.TCRouteSubpaths.RemoveAt(lastIndex);
TCRoute.TCRouteSubpaths.Add(new TCSubpathRoute(lastSubpath));
}
//================================================================================================//
/// <summary>
/// Set alternative station stop when alternative path is selected
/// Override from Train class
/// </summary>
/// <param name="orgStop"></param>
/// <param name="newRoute"></param>
/// <returns></returns>
public override StationStop SetAlternativeStationStop(StationStop orgStop, TCSubpathRoute newRoute)
{
int altPlatformIndex = -1;
// get station platform list
if (signalRef.StationXRefList.ContainsKey(orgStop.PlatformItem.Name))
{
List<int> XRefKeys = signalRef.StationXRefList[orgStop.PlatformItem.Name];
// search through all available platforms
for (int platformIndex = 0; platformIndex <= XRefKeys.Count - 1 && altPlatformIndex < 0; platformIndex++)
{
int platformXRefIndex = XRefKeys[platformIndex];
PlatformDetails altPlatform = signalRef.PlatformDetailsList[platformXRefIndex];
// check if section is in new route
for (int iSectionIndex = 0; iSectionIndex <= altPlatform.TCSectionIndex.Count - 1 && altPlatformIndex < 0; iSectionIndex++)
{
if (newRoute.GetRouteIndex(altPlatform.TCSectionIndex[iSectionIndex], 0) > 0)
{
altPlatformIndex = platformXRefIndex;
}
}
}
// remove holding signal if set
int holdSig = -1;
if (orgStop.HoldSignal && orgStop.ExitSignal >= 0 && HoldingSignals.Contains(orgStop.ExitSignal))
{
holdSig = orgStop.ExitSignal;
HoldingSignals.Remove(holdSig);
}
// section found in new route - set new station details using old details
if (altPlatformIndex >= 0)
{
bool isNewPlatform = true;
// check if new found platform is actually same as original
foreach (int platfReference in signalRef.PlatformDetailsList[altPlatformIndex].PlatformReference)
{