/
SignalHead.cs
674 lines (571 loc) · 25.1 KB
/
SignalHead.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
// COPYRIGHT 2021 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/>.
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using Orts.Formats.Msts;
using Orts.MultiPlayer;
using ORTS.Common;
namespace Orts.Simulation.Signalling
{
public class SignalHead
{
public SignalType signalType; // from sigcfg file
public CsSignalScript usedCsSignalScript = null;
public SignalScripts.SCRScripts usedSigScript = null; // used sigscript
public MstsSignalAspect state = MstsSignalAspect.STOP;
public string TextSignalAspect = String.Empty;
public int draw_state;
public int trItemIndex; // Index to trItem
public uint TrackJunctionNode; // Track Junction Node (= 0 if not set)
public uint JunctionPath; // Required Junction Path
public int JunctionMainNode; // Main node following junction
public int TDBIndex; // Index to TDB Signal Item
protected ObjectSpeedInfo[] speed_info; // speed limit info (per aspect)
public ObjectSpeedInfo CurrentSpeedInfo => SpeedInfoSetBySignalScript ? SignalScriptSpeedInfo : speed_info[(int)state];
public bool SpeedInfoSetBySignalScript = false;
public ObjectSpeedInfo SignalScriptSpeedInfo = null; // speed limit info set by C# signal script
public SignalObject mainSignal; // This is the signal which this head forms a part.
public float? ApproachControlLimitPositionM;
public float? ApproachControlLimitSpeedMpS;
public SignalFunction Function { get; protected set; }
public int ORTSNormalSubtypeIndex; // subtype index form sigcfg file
public string SignalTypeName => signalType?.Name ?? string.Empty;
/// <summary>
/// Constructor for signals
/// </summary>
public SignalHead(SignalObject sigObject, int trItem, int TDBRef, SignalItem sigItem)
{
mainSignal = sigObject;
trItemIndex = trItem;
TDBIndex = TDBRef;
if (sigItem.NoSigDirs > 0)
{
TrackJunctionNode = sigItem.TrSignalDirs[0].TrackNode;
JunctionPath = sigItem.TrSignalDirs[0].LinkLRPath;
}
var sigasp_values = Enum.GetValues(typeof(MstsSignalAspect));
speed_info = new ObjectSpeedInfo[sigasp_values.Length];
}
/// <summary>
/// Constructor for speedposts
/// </summary>
public SignalHead(SignalObject sigObject, int trItem, int TDBRef, SpeedPostItem speedItem)
{
mainSignal = sigObject;
trItemIndex = trItem;
TDBIndex = TDBRef;
draw_state = 1;
state = MstsSignalAspect.CLEAR_2;
signalType = new SignalType(SignalFunction.SPEED, MstsSignalAspect.CLEAR_2);
Function = SignalFunction.SPEED;
var sigasp_values = Enum.GetValues(typeof(MstsSignalAspect));
speed_info = new ObjectSpeedInfo[sigasp_values.Length];
float speedMpS = MpS.ToMpS(speedItem.SpeedInd, !speedItem.IsMPH);
if (speedItem.IsResume)
speedMpS = 999f;
float passSpeed = speedItem.IsPassenger ? speedMpS : -1;
float freightSpeed = speedItem.IsFreight ? speedMpS : -1;
ObjectSpeedInfo speedinfo = new ObjectSpeedInfo(passSpeed, freightSpeed, false, false, speedItem is TempSpeedPostItem ? (speedMpS == 999f ? 2 : 1) : 0, speedItem.IsWarning);
speed_info[(int)state] = speedinfo;
}
/// <summary>
/// Set the signal type object from the CIGCFG file
/// </summary>
public void SetSignalType(TrItem[] TrItems, SignalConfigurationFile sigCFG)
{
if (TrItems[TDBIndex] is SignalItem sigItem)
{
// set signal type
if (sigCFG.SignalTypes.ContainsKey(sigItem.SignalType))
{
// set signal type
signalType = sigCFG.SignalTypes[sigItem.SignalType];
Function = signalType.Function;
// get related signalscript
Signals.scrfile.SignalScripts.Scripts.TryGetValue(signalType, out usedSigScript);
usedCsSignalScript = Signals.CsSignalScripts.LoadSignalScript(signalType.Script)
?? Signals.CsSignalScripts.LoadSignalScript(signalType.Name);
usedCsSignalScript?.AttachToHead(this);
// set signal speeds
foreach (SignalAspect thisAspect in signalType.Aspects)
{
int arrindex = (int)thisAspect.Aspect;
speed_info[arrindex] = new ObjectSpeedInfo(thisAspect.SpeedMpS, thisAspect.SpeedMpS, thisAspect.Asap, thisAspect.Reset, thisAspect.NoSpeedReduction ? 1 : 0, false);
}
// set normal subtype
ORTSNormalSubtypeIndex = signalType.ORTSSubtypeIndex;
// update overall SignalNumClearAhead
if (Function == SignalFunction.NORMAL)
{
mainSignal.SignalNumClearAhead_MSTS = Math.Max(mainSignal.SignalNumClearAhead_MSTS, signalType.NumClearAhead_MSTS);
mainSignal.SignalNumClearAhead_ORTS = Math.Max(mainSignal.SignalNumClearAhead_ORTS, signalType.NumClearAhead_ORTS);
mainSignal.SignalNumClearAheadActive = mainSignal.SignalNumClearAhead_ORTS;
}
// set approach control limits
if (signalType.ApproachControlDetails != null)
{
ApproachControlLimitPositionM = signalType.ApproachControlDetails.ApproachControlPositionM;
ApproachControlLimitSpeedMpS = signalType.ApproachControlDetails.ApproachControlSpeedMpS;
}
else
{
ApproachControlLimitPositionM = null;
ApproachControlLimitSpeedMpS = null;
}
}
else
{
Trace.TraceWarning("SignalObject trItem={0}, trackNode={1} has SignalHead with undefined SignalType {2}.",
mainSignal.trItem, mainSignal.trackNode, sigItem.SignalType);
}
}
}
public void Initialize()
{
usedCsSignalScript?.Initialize();
}
/// <summary>
/// Set of methods called per signal head from signal script processing
/// All methods link through to the main method set for signal objec
/// </summary>
public MstsSignalAspect next_sig_mr(SignalFunction function)
{
return mainSignal.next_sig_mr(function);
}
public MstsSignalAspect next_sig_lr(SignalFunction function)
{
return mainSignal.next_sig_lr(function);
}
public MstsSignalAspect this_sig_lr(SignalFunction function)
{
return mainSignal.this_sig_lr(function);
}
public MstsSignalAspect this_sig_lr(SignalFunction function, ref bool sigfound)
{
return mainSignal.this_sig_lr(function, ref sigfound);
}
public MstsSignalAspect this_sig_mr(SignalFunction function)
{
return mainSignal.this_sig_mr(function);
}
public MstsSignalAspect this_sig_mr(SignalFunction function, ref bool sigfound)
{
return mainSignal.this_sig_mr(function, ref sigfound);
}
public MstsSignalAspect opp_sig_mr(SignalFunction function)
{
return mainSignal.opp_sig_mr(function);
}
public MstsSignalAspect opp_sig_mr(SignalFunction function, ref SignalObject signalFound) // for debug purposes
{
return mainSignal.opp_sig_mr(function, ref signalFound);
}
public MstsSignalAspect opp_sig_lr(SignalFunction function)
{
return mainSignal.opp_sig_lr(function);
}
public MstsSignalAspect opp_sig_lr(SignalFunction function, ref SignalObject signalFound) // for debug purposes
{
return mainSignal.opp_sig_lr(function, ref signalFound);
}
public MstsSignalAspect next_nsig_lr(SignalFunction function, int nsignals, string dumpfile)
{
return mainSignal.next_nsig_lr(function, nsignals, dumpfile);
}
public int next_sig_id(SignalFunction function)
{
return mainSignal.next_sig_id(function);
}
public int next_nsig_id(SignalFunction function, int nsignal)
{
return mainSignal.next_nsig_id(function, nsignal);
}
public int opp_sig_id(SignalFunction function)
{
return mainSignal.opp_sig_id(function);
}
public MstsSignalAspect id_sig_lr(int sigId, SignalFunction function)
{
if (sigId >= 0 && sigId < mainSignal.signalRef.SignalObjects.Length)
{
SignalObject reqSignal = mainSignal.signalRef.SignalObjects[sigId];
return reqSignal.this_sig_lr(function);
}
return MstsSignalAspect.STOP;
}
public int id_sig_enabled(int sigId)
{
bool sigEnabled = false;
if (sigId >= 0 && sigId < mainSignal.signalRef.SignalObjects.Length)
{
SignalObject reqSignal = mainSignal.signalRef.SignalObjects[sigId];
sigEnabled = reqSignal.enabled;
}
return sigEnabled ? 1 : 0;
}
public void store_lvar(int index, int value)
{
mainSignal.store_lvar(index, value);
}
public int this_sig_lvar(int index)
{
return mainSignal.this_sig_lvar(index);
}
public int next_sig_lvar(SignalFunction function, int index)
{
return mainSignal.next_sig_lvar(function, index);
}
public int id_sig_lvar(int sigId, int index)
{
if (sigId >= 0 && sigId < mainSignal.signalRef.SignalObjects.Length)
{
SignalObject reqSignal = mainSignal.signalRef.SignalObjects[sigId];
return reqSignal.this_sig_lvar(index);
}
return 0;
}
public int next_sig_hasnormalsubtype(int reqSubtype)
{
return mainSignal.next_sig_hasnormalsubtype(reqSubtype);
}
public int this_sig_hasnormalsubtype(int reqSubtype)
{
return mainSignal.this_sig_hasnormalsubtype(reqSubtype);
}
public int id_sig_hasnormalsubtype(int sigId, int reqSubtype)
{
if (sigId >= 0 && sigId < mainSignal.signalRef.SignalObjects.Length)
{
SignalObject reqSignal = mainSignal.signalRef.SignalObjects[sigId];
return reqSignal.this_sig_hasnormalsubtype(reqSubtype);
}
return 0;
}
public int switchstand(int aspect1, int aspect2)
{
return mainSignal.switchstand(aspect1, aspect2);
}
/// <summary>
/// Returns most restrictive state of signal type A, for all type A upto type B
/// Uses Most Restricted state per signal, but checks for valid routing
/// </summary>
public MstsSignalAspect dist_multi_sig_mr(SignalFunction function1, SignalFunction function2, string dumpfile)
{
MstsSignalAspect foundState = MstsSignalAspect.CLEAR_2;
bool foundValid = false;
// get signal of type 2 (end signal)
if (dumpfile.Length > 1)
{
File.AppendAllText(dumpfile,
String.Format("DIST_MULTI_SIG_MR for {0} + upto {1}\n",
function1, function2));
}
int sig2Index = mainSignal.sigfound[function2];
if (sig2Index < 0) // try renewed search with full route
{
sig2Index = mainSignal.SONextSignal(function2);
mainSignal.sigfound[function2] = sig2Index;
}
if (dumpfile.Length > 1)
{
if (sig2Index < 0)
File.AppendAllText(dumpfile, " no signal type 2 found\n");
}
if (dumpfile.Length > 1)
{
var sob = new StringBuilder();
sob.AppendFormat(" signal type 2 : {0}", mainSignal.sigfound[function2]);
if (mainSignal.sigfound[function2] > 0)
{
SignalObject otherSignal = mainSignal.signalRef.SignalObjects[mainSignal.sigfound[function2]];
sob.AppendFormat(" (");
foreach (SignalHead otherHead in otherSignal.SignalHeads)
{
sob.AppendFormat(" {0} ", otherHead.TDBIndex);
}
sob.AppendFormat(") ");
}
sob.AppendFormat("\n");
File.AppendAllText(dumpfile, sob.ToString());
}
SignalObject thisSignal = mainSignal;
// ensure next signal of type 1 is located correctly (cannot be done for normal signals searching next normal signal)
if (!thisSignal.isSignalNormal() || function1 != SignalFunction.NORMAL)
{
thisSignal.sigfound[function1] = thisSignal.SONextSignal(function1);
}
// loop through all available signals of type 1
while (thisSignal.sigfound[function1] >= 0)
{
thisSignal = thisSignal.signalRef.SignalObjects[thisSignal.sigfound[function1]];
MstsSignalAspect thisState = thisSignal.this_sig_mr_routed(function1, dumpfile);
// ensure correct next signals are located
if (function1 != SignalFunction.NORMAL || !thisSignal.isSignalNormal())
{
var sigFound = thisSignal.SONextSignal(function1);
if (sigFound >= 0) thisSignal.sigfound[function1] = thisSignal.SONextSignal(function1);
}
if (function2 != SignalFunction.NORMAL || !thisSignal.isSignalNormal())
{
var sigFound = thisSignal.SONextSignal(function2);
if (sigFound >= 0) thisSignal.sigfound[function2] = thisSignal.SONextSignal(function2);
}
if (sig2Index == thisSignal.thisRef) // this signal also contains type 2 signal and is therefor valid
{
foundValid = true;
foundState = foundState < thisState ? foundState : thisState;
return foundState;
}
else if (sig2Index >= 0 && thisSignal.sigfound[function2] != sig2Index) // we are beyond type 2 signal
{
return foundValid ? foundState : MstsSignalAspect.STOP;
}
foundValid = true;
foundState = foundState < thisState ? foundState : thisState;
}
return foundValid ? foundState : MstsSignalAspect.STOP; // no type 2 or running out of signals before finding type 2
}
/// <summary>
/// Returns most restrictive state of signal type A, for all type A upto type B
/// Uses Least Restrictive state per signal
/// </summary>
public MstsSignalAspect dist_multi_sig_mr_of_lr(SignalFunction function1, SignalFunction function2, string dumpfile)
{
MstsSignalAspect foundState = MstsSignalAspect.CLEAR_2;
bool foundValid = false;
// get signal of type 2 (end signal)
if (dumpfile.Length > 1)
{
File.AppendAllText(dumpfile,
String.Format("DIST_MULTI_SIG_MR_OF_LR for {0} + upto {1}\n",
function1, function2));
}
int sig2Index = mainSignal.sigfound[function2];
if (sig2Index < 0) // try renewed search with full route
{
sig2Index = mainSignal.SONextSignal(function2);
mainSignal.sigfound[function2] = sig2Index;
}
if (dumpfile.Length > 1)
{
if (sig2Index < 0)
File.AppendAllText(dumpfile, " no signal type 2 found\n");
}
if (dumpfile.Length > 1)
{
var sob = new StringBuilder();
sob.AppendFormat(" signal type 2 : {0}", mainSignal.sigfound[function2]);
if (mainSignal.sigfound[function2] > 0)
{
SignalObject otherSignal = mainSignal.signalRef.SignalObjects[mainSignal.sigfound[function2]];
sob.AppendFormat(" (");
foreach (SignalHead otherHead in otherSignal.SignalHeads)
{
sob.AppendFormat(" {0} ", otherHead.TDBIndex);
}
sob.AppendFormat(") ");
}
sob.AppendFormat("\n");
File.AppendAllText(dumpfile, sob.ToString());
}
SignalObject thisSignal = mainSignal;
// ensure next signal of type 1 is located correctly (cannot be done for normal signals searching next normal signal)
if (!thisSignal.isSignalNormal() || function1 != SignalFunction.NORMAL)
{
thisSignal.sigfound[function1] = thisSignal.SONextSignal(function1);
}
// loop through all available signals of type 1
while (thisSignal.sigfound[function1] >= 0)
{
thisSignal = thisSignal.signalRef.SignalObjects[thisSignal.sigfound[function1]];
MstsSignalAspect thisState = thisSignal.this_sig_lr(function1);
if (dumpfile.Length > 1)
{
File.AppendAllText(dumpfile, "Found lr state : " + thisState.ToString() + "\n");
}
// ensure correct next signals are located
if (function1 != SignalFunction.NORMAL || !thisSignal.isSignalNormal())
{
var sigFound = thisSignal.SONextSignal(function1);
if (sigFound >= 0) thisSignal.sigfound[function1] = thisSignal.SONextSignal(function1);
}
if (function2 != SignalFunction.NORMAL || !thisSignal.isSignalNormal())
{
var sigFound = thisSignal.SONextSignal(function2);
if (sigFound >= 0) thisSignal.sigfound[function2] = thisSignal.SONextSignal(function2);
}
if (sig2Index == thisSignal.thisRef) // this signal also contains type 2 signal and is therefor valid
{
foundValid = true;
foundState = foundState < thisState ? foundState : thisState;
return foundState;
}
else if (sig2Index >= 0 && thisSignal.sigfound[function2] != sig2Index) // we are beyond type 2 signal
{
return foundValid ? foundState : MstsSignalAspect.STOP;
}
foundValid = true;
foundState = foundState < thisState ? foundState : thisState;
}
return foundValid ? foundState : MstsSignalAspect.STOP; // no type 2 or running out of signals before finding type 2
}
/// </summary>
/// Return state of requested feature through signal head flags
/// </summary>
public bool sig_feature(int feature)
{
bool flag_value = true;
if (mainSignal.WorldObject != null)
{
if (feature < mainSignal.WorldObject.FlagsSet.Length)
{
flag_value = mainSignal.WorldObject.FlagsSet[feature];
}
}
return flag_value;
}
/// <summary>
/// Returns the default draw state for this signal head from the SIGCFG file
/// Retruns -1 id no draw state.
/// </summary>
public int def_draw_state(MstsSignalAspect state)
{
if (signalType != null)
return signalType.def_draw_state(state);
else
return -1;
}//def_draw_state
/// <summary>
/// Sets the state to the most restrictive aspect for this head.
/// </summary>
public void RequestMostRestrictiveAspect()
{
if (usedCsSignalScript != null)
{
usedCsSignalScript.HandleEvent(SignalEvent.RequestMostRestrictiveAspect);
usedCsSignalScript.Update();
}
else
{
if (signalType != null)
state = signalType.GetMostRestrictiveAspect();
else
state = MstsSignalAspect.STOP;
draw_state = def_draw_state(state);
}
}
public void RequestApproachAspect()
{
if (usedCsSignalScript != null)
{
usedCsSignalScript.HandleEvent(SignalEvent.RequestApproachAspect);
usedCsSignalScript.Update();
}
else
{
var drawstate1 = def_draw_state(MstsSignalAspect.APPROACH_1);
var drawstate2 = def_draw_state(MstsSignalAspect.APPROACH_2);
if (drawstate1 > 0)
{
state = MstsSignalAspect.APPROACH_1;
}
else if (drawstate2 > 0)
{
state = MstsSignalAspect.APPROACH_2;
}
else
{
state = MstsSignalAspect.APPROACH_3;
}
draw_state = def_draw_state(state);
}
}
/// <summary>
/// Sets the state to the least restrictive aspect for this head.
/// </summary>
public void RequestLeastRestrictiveAspect()
{
if (usedCsSignalScript != null)
{
usedCsSignalScript.HandleEvent(SignalEvent.RequestLeastRestrictiveAspect);
usedCsSignalScript.Update();
}
else
{
if (signalType != null)
state = signalType.GetLeastRestrictiveAspect();
else
state = MstsSignalAspect.CLEAR_2;
draw_state = def_draw_state(state);
}
}
/// <summary>
/// check if linked route is set
/// </summary>
public int route_set()
{
bool juncfound = true;
// call route_set routine from main signal
if (TrackJunctionNode > 0)
{
juncfound = mainSignal.route_set(JunctionMainNode, TrackJunctionNode);
}
//added by JTang
else if (MPManager.IsMultiPlayer())
{
var node = mainSignal.signalRef.trackDB.TrackNodes[mainSignal.trackNode];
if (node.TrJunctionNode == null && node.TrPins != null && mainSignal.TCDirection < node.TrPins.Length)
{
node = mainSignal.signalRef.trackDB.TrackNodes[node.TrPins[mainSignal.TCDirection].Link];
if (node.TrJunctionNode == null) return 0;
for (var pin = node.Inpins; pin < node.Inpins + node.Outpins; pin++)
{
if (node.TrPins[pin].Link == mainSignal.trackNode && pin - node.Inpins != node.TrJunctionNode.SelectedRoute)
{
juncfound = false;
break;
}
}
}
}
if (juncfound)
{
return 1;
}
else
{
return 0;
}
}
/// <summary>
/// Default update process
/// </summary>
public void Update()
{
if (usedCsSignalScript is CsSignalScript)
{
usedCsSignalScript.Update();
}
else
{
SIGSCRfile.SH_update(this, Signals.scrfile);
}
}
}
}