-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathETCS.cs
More file actions
438 lines (422 loc) · 18.8 KB
/
Copy pathETCS.cs
File metadata and controls
438 lines (422 loc) · 18.8 KB
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
using Orts.Simulation.Signalling;
using ORTS.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.IO;
using System.Runtime.CompilerServices;
namespace ORTS.Scripting.Script
{
public abstract class ETCS : CsSignalScript
{
static protected readonly int KeyNextEurobaliseID=151;
static protected readonly int KeyNextEurobaliseBackfacingID=152;
static protected readonly int KeyPacketNeedsUpdate=1001;
static public readonly int KeyNID_BG=100;
static public readonly int KeyN_PIG=101;
static public readonly int KeyN_TOTAL=102;
static public readonly int KeyNID_C=103;
static public readonly int KeyGroupReaction=110;
static public readonly int KeyBaliseReaction=111;
static public readonly int KeyGroupReactionBackfacing=112;
static public readonly int KeyBaliseReactionBackfacing=113;
static public readonly int KeyGroupLinkBackfacing=114;
static public readonly int KeyBaliseLinkBackfacing=115;
static public readonly int KeyGroupProvidesLinking=116;
static public readonly int KeyBaliseProvidesLinking=117;
static public readonly int KeyBackfacingSignalId=120;
static public readonly int KeyNextSignalId=150;
static public readonly int DefaultNID_C;
protected int NID_C=-1;
protected bool Convencional;
protected int M_VERSION=33;
static public readonly Random rand = new Random();
static Dictionary<string, Aspecto> textoAAspecto;
public static string RouteDirectoryPath = null;
public static void InitializeScriptDirectoryPath([CallerFilePath] string sourceFilePath = "")
{
RouteDirectoryPath = Path.Combine(Path.GetDirectoryName(Path.GetFullPath(sourceFilePath)), "..", "..");
}
static ETCS()
{
textoAAspecto = Enum.GetNames(typeof(Aspecto)).ToDictionary(x => x, x => (Aspecto)Enum.Parse(typeof(Aspecto), x), StringComparer.OrdinalIgnoreCase);
InitializeScriptDirectoryPath();
LoadParameter("NID_C", "Default", ref DefaultNID_C);
}
public override void Initialize()
{
}
public override void Update()
{
if (NID_C < 0)
{
int id = NextSignalId("ETCS_CFG");
if (id >= 0)
{
int next = IdSignalLocalVariable(id, 1);
if (next == NextSignalId("NORMAL")) NID_C = IdSignalLocalVariable(id, 0);
else if (next > -2) NID_C = DefaultNID_C;
}
else NID_C = DefaultNID_C;
if (NID_C >= 0)
{
string section = string.Format("NID_C.{0}", NID_C);
string rules = null;
LoadParameter(section, "Rules", ref rules);
Convencional = rules == "ADIF_CONV";
LoadParameter(section, "M_VERSION", ref M_VERSION);
}
}
SharedVariables[KeyNextEurobaliseID] = NextSignalId("ETCS");
SharedVariables[KeyNextEurobaliseBackfacingID] = NextSignalId("ETCS_BACKFACING");
}
protected Aspecto GetAspectoSenal(int id, string tipo = "NORMAL")
{
if (id < 0) return Aspecto.Parada;
var aspectoSiguienteSenalTexto = IdTextSignalAspect(id, tipo);
if (textoAAspecto.ContainsKey(aspectoSiguienteSenalTexto))
{
return textoAAspecto[aspectoSiguienteSenalTexto];
}
else
{
switch (IdSignalAspect(id, tipo))
{
case Aspect.Stop:
return Aspecto.Parada;
case Aspect.StopAndProceed:
return Aspecto.RebaseAutorizado;
case Aspect.Restricting:
return Aspecto.RebaseAutorizadoDestellos;
case Aspect.Approach_1:
return Aspecto.AnuncioParada;
case Aspect.Approach_2:
return Aspecto.AnuncioPrecaucion;
case Aspect.Approach_3:
return Aspecto.PreanuncioParada;
case Aspect.Clear_1:
return Aspecto.ViaLibreCondicional;
case Aspect.Clear_2:
return Aspecto.ViaLibre;
}
}
return Aspecto.Parada;
}
protected static string format_binary(int value, int size)
{
if (value < 0) value = 0;
return format_binary((ulong)value, size);
}
protected static string format_binary(ulong value, int size)
{
var sb = new StringBuilder(size);
for (int i=size-1; i>=0; i--)
{
sb.Append(((value>>i)&1) != 0 ? '1' : '0');
}
return sb.ToString();
}
protected static string format_etcs_speed(double speedmps)
{
int val = Math.Min((int)Math.Round(speedmps*3.6)/5, 120);
return format_binary(val, 7);
}
protected static string format_etcs_speedKpH(double speedkph)
{
int val = Math.Min((int)Math.Round(speedkph)/5, 120);
return format_binary(val, 7);
}
protected static string format_etcs_distance(double distm)
{
if (distm > 32767) distm = 32767;
if (distm < 0) distm = 0;
int val = (int)distm;
return format_binary(val, 15);
}
protected static string create_packet(int nid_packet, string info, int dir)
{
string infocorr = Regex.Replace(info, @"{([^}]*)}", match => {
return format_etcs_distance(0);
});
return format_binary(nid_packet, 8)+format_binary(dir, 2)+format_binary(infocorr.Length+23,13)+info;
}
protected string get_linking(bool backfacing=false)
{
string link="01";
var links = new List<string>();
string prevref = "0";
int nomCount = 0;
int revCount = 0;
int countLimit = 2;
int current_nid_c = NID_C;
while (links.Count < countLimit)
{
int id1 = NextSignalId("ETCS", nomCount);
int id2 = NextSignalId("ETCS_BACKFACING", revCount);
if (id1 < 0 && id2 < 0) break;
int id;
bool reverse = false;
if (id2 > 0 && (id1 < 0 || IdSignalLocalVariable(id2, KeyNextEurobaliseID) == id1))
{
id = IdSignalLocalVariable(id2, KeyBackfacingSignalId);
++revCount;
reverse = true;
}
else
{
id = id1;
++nomCount;
}
if (IdSignalLocalVariable(id, KeyN_PIG) != 0) continue;
if (reverse && IdSignalLocalVariable(id2, KeyGroupLinkBackfacing) == 0) continue;
if (countLimit < 15 && (reverse || IdSignalLocalVariable(id, KeyGroupProvidesLinking) == 0)) ++countLimit;
string r = (reverse ? "bg_reference_back(" : "bg_reference(") + id + ")";
string l = "{" + r + "-(" + prevref + ")}";
int nid_c = IdSignalLocalVariable(id, KeyNID_C);
if (nid_c != current_nid_c) l += "1" + format_binary(nid_c, 10);
else l += "0";
current_nid_c = nid_c;
l += format_binary(IdSignalLocalVariable(id, KeyNID_BG), 14) + (reverse ? "0" : "1") + format_binary(IdSignalLocalVariable(id, reverse ? KeyGroupReactionBackfacing : KeyGroupReaction), 2) + format_binary(3, 6);
links.Add(l);
prevref = r;
}
if (links.Count == 0) return "";
link += links[0]+format_binary(links.Count-1, 5);
for (int i=1; i<links.Count; i++) link += links[i];
return create_packet(5, link, backfacing ? 0 : 1);
}
protected string level_table(int tableId, float lack)
{
List<int> levels = new List<int>();
for (int i=0; ; i++)
{
int lv = IdSignalLocalVariable(tableId, 602+i);
if (lv == 0) break;
levels.Add(lv);
}
return level_table(levels, lack);
}
protected string level_table(List<int> levels, float lack)
{
if ((EtcsRbcManager.DefaultRbc == null || !EtcsRbcManager.DefaultRbc.Active) && levels.Contains(3))
{
levels.Remove(3);
levels.Add(3); // Lowest priority
}
var sb = new StringBuilder();
for (int i=0; i<levels.Count; i++)
{
if (levels[i] == 1) sb.Append("000");
else if (levels[i] == 2) sb.Append("010");
else if (levels[i] == 3) sb.Append("011");
else if (levels[i] == 4) sb.Append("100");
else
{
sb.Append("001");
sb.Append(format_binary(levels[i]-5, 8));
}
sb.Append(format_etcs_distance(lack));
if (i == 0) sb.Append(format_binary(levels.Count - 1, 5));
}
return sb.ToString();
}
protected string level_tr(float distance, string table, bool backfacing=false)
{
string dist;
if (distance == 0) dist = format_binary(32767, 15);
else if (distance < 0 && !backfacing) dist = "{(NextSignalDistanceM(0)+50)*1.05}";
else dist = format_etcs_distance(distance);
return create_packet(41, "01"+dist+table, backfacing ? 0 : 1);
}
public string get_ma(int SenalAsociada, bool Infill)
{
string ma = "01";
Aspecto aspectoAsociada = GetAspectoSenal(SenalAsociada);
int maxToClear = 0;
switch (aspectoAsociada)
{
case Aspecto.Parada:
case Aspecto.ParadaSelectiva:
case Aspecto.ParadaPermisiva:
case Aspecto.ParadaLZB:
maxToClear = 0;
break;
case Aspecto.RebaseAutorizado:
case Aspecto.RebaseAutorizadoCortaDistancia:
case Aspecto.RebaseAutorizadoDestellos:
case Aspecto.AnuncioParadaInmediata:
maxToClear = 1;
break;
default:
maxToClear = 2;
break;
}
if (maxToClear == 0)
{
TipoSeñal t = (TipoSeñal)IdSignalLocalVariable(SenalAsociada, 201);
float dangerPoint = 50;
int vrelease = 15;
if (t != TipoSeñal.Ninguno && t.HasFlag(TipoSeñal.Intermedia))
{
vrelease = 30;
dangerPoint = 100;
}
ma += "0000000"+"0000000"+"0000000000"+"00000";
int ahead = -1;
for (int i=0;; i++)
{
int sig = NextSignalId("NORMAL", i);
if (sig == SenalAsociada)
{
ahead = i;
break;
}
if (sig < 0) break;
}
if (ahead < 0) ma += "000000000000000";
else ma += "{NextSignalDistanceM("+ahead+")-"+(Infill ? "ilref" : "bgref")+"}";
ma += "0"+"0"+"1"+format_etcs_distance(dangerPoint)+format_etcs_speedKpH(vrelease)+"0";
}
else
{
int vrelease = 15;
ma += format_etcs_speedKpH(300)+format_etcs_speed(0)+format_binary(0, 10);
int nsignals = 0;
int proximidad = 0;
int section = 0;
int maxsections = 2;
int senalUltimaSeccion=-1;
int npn = 0;
string sect = "";
int sig = -1;
string startRef = Infill ? "ilref" : "bgref";
string sectionLength="";
float dangerPoint=50;
bool first = false;
for(int i=0;; i++)
{
sig = NextSignalId("NORMAL", i);
for (int j=npn;; j++)
{
int pn = NextSignalId("OLPN_T", npn);
if (sig >= 0 && pn >= 0 && IdSignalLocalVariable(pn, KeyNextSignalId) == sig)
{
if (first)
{
sectionLength = "{(NextSignalDistanceM(OLPN_T,"+j+")-50)-("+startRef+")}";
startRef = "NextSignalDistanceM(OLPN_T,"+j+")-50";
sect += construirSeccion(SenalAsociada, Infill, sectionLength, section, senalUltimaSeccion, -1, nsignals);
senalUltimaSeccion = -1;
section++;
maxsections++;
}
npn++;
}
else break;
}
if (sig != SenalAsociada && !first)
{
if (sig < 0) return "";
continue;
}
first = true;
if (IdSignalHasNormalSubtype(sig, "PANTALLA_ERTMS") || IdSignalHasNormalSubtype(sig, "RETROCESO")) continue;
Aspecto a = GetAspectoSenal(sig);
SistemaSeñalizacion sist = (SistemaSeñalizacion)IdSignalLocalVariable(sig, 200);
TipoSeñal t = (TipoSeñal)IdSignalLocalVariable(sig, 201);
bool stop = sig == -1 || a == Aspecto.Parada || a == Aspecto.ParadaPermisiva || a == Aspecto.ParadaSelectiva || a == Aspecto.ParadaLZB || a == Aspecto.RebaseAutorizado || a == Aspecto.RebaseAutorizadoCortaDistancia;
if (a == Aspecto.ParadaSelectivaDestellos && (sist & SistemaSeñalizacion.ETCS_N1) == 0 && (sist & SistemaSeñalizacion.ETCS_N2) == 0 && (sist & SistemaSeñalizacion.LZB) == 0) stop = true;
bool esInicioRuta = t != TipoSeñal.Ninguno && (t.HasFlag(TipoSeñal.Entrada) || t.HasFlag(TipoSeñal.Salida) || t.HasFlag(TipoSeñal.Interior)) && !t.HasFlag(TipoSeñal.Liberacion);
if (esInicioRuta)
{
int reqProximidad = IdSignalLocalVariable(sig, 902);
if (proximidad > reqProximidad) stop = true;
}
if (maxToClear == 1 && nsignals > 0) stop = true;
if (stop)
{
if (sig < 0)
{
vrelease = 10;
dangerPoint = 10;
sectionLength = "{(EoADistanceM(0)-10)-(" + startRef + ")}";
}
else sectionLength = "{NextSignalDistanceM(" + i + ")-(" + startRef + ")}";
if (t != TipoSeñal.Ninguno && (t.HasFlag(TipoSeñal.Intermedia) || t.HasFlag(TipoSeñal.Avanzada)))
{
vrelease = 30;
dangerPoint = 100;
}
else if (t != TipoSeñal.Ninguno && t.HasFlag(TipoSeñal.Topera))
{
vrelease = 10;
dangerPoint = 10;
}
break;
}
if (esInicioRuta && section < maxsections)
{
sectionLength = "{NextSignalDistanceM("+i+")-("+startRef+")}";
startRef = "NextSignalDistanceM("+i+")";
sect += construirSeccion(SenalAsociada, Infill, sectionLength, section, senalUltimaSeccion, sig, nsignals);
senalUltimaSeccion = sig;
section++;
}
nsignals++;
proximidad += 1 - IdSignalLocalVariable(sig, 900);
}
ma += format_binary(section, 5) + sect;
ma += construirSeccion(SenalAsociada, Infill, sectionLength, section, senalUltimaSeccion, sig, nsignals);
ma += "0"+"1"+format_etcs_distance(dangerPoint)+format_etcs_speedKpH(vrelease)+"0";
}
return create_packet(12, ma, 1);
}
string construirSeccion(int SenalAsociada, bool Infill, string longitud, int sectionNumber, int senalInicioSeccion, int senalFinSeccion, int nsignals)
{
var tipo = (TipoSeñal)IdSignalLocalVariable(senalInicioSeccion, 201);
bool esSalida = tipo != TipoSeñal.Ninguno && tipo.HasFlag(TipoSeñal.Salida);
string sect = longitud;
if (sectionNumber > 0)
{
int T = 30;
float d = 200;
if (senalInicioSeccion == SenalAsociada && !Infill) T = Convencional ? 180 : 360; // DEI
else if (senalInicioSeccion < 0)
{
T = 180;
d = 0;
}
else
{
if (Convencional) T = /*(senalInicioSeccion == SenalAsociada && esSalida) ? 30 : */150; // DAI
else if (nsignals == 1) T = 240; // DAI zona 2
else T = 360; // DAI zona 1
}
sect += "1"+format_binary(T, 10)+format_etcs_distance(d);
}
else if (senalFinSeccion == SenalAsociada && !Infill)
{
int T = Convencional ? 150 : 240; // DAI
sect += "1"+format_binary(T, 10)+longitud;
}
else sect += "0"; // Sin temporizar
return sect;
}
protected static void LoadParameter<T>(string sectionName, string keyName, ref T value)
{
string file = Path.Combine(RouteDirectoryPath, @"etcs.ini");
if (File.Exists(file))
{
string buffer = new string('\0', 256);
int length = NativeMethods.GetPrivateProfileString(sectionName, keyName, null, buffer, buffer.Length, file);
if (length > 0)
{
value = (T)Convert.ChangeType(buffer.Trim('\0').Trim(), typeof(T), System.Globalization.CultureInfo.InvariantCulture);
}
}
}
}
}