From 23d524df71e66c443345a043c9767b7c947d5a5c Mon Sep 17 00:00:00 2001 From: Maverick2797 Date: Sun, 4 Jul 2021 16:34:04 +0800 Subject: Luaautomation: add section_occupancy() Returns a table of train ids for the specified section. Returns nil if the section id is not provided.. Returns false if the section id is invalid. Returns an empty table if the section id is valid but empty of trains. --- advtrains_luaautomation/README.md | 5 ++++- advtrains_luaautomation/environment.lua | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) mode change 100644 => 100755 advtrains_luaautomation/README.md mode change 100644 => 100755 advtrains_luaautomation/environment.lua diff --git a/advtrains_luaautomation/README.md b/advtrains_luaautomation/README.md old mode 100644 new mode 100755 index 88fc1ef..67a4b80 --- a/advtrains_luaautomation/README.md +++ b/advtrains_luaautomation/README.md @@ -314,13 +314,16 @@ Deprecated: #### Interlocking -This additional function is available when advtrains_interlocking is enabled: +These additional functions are available when advtrains_interlocking is enabled: - `atc_set_ars_disable(boolean)` Disables (true) or enables (false) the use of ARS for this train. The train will not trigger ARS (automatic route setting) on signals then. Note: If you want to disable ARS from an approach callback, the call to `atc_set_ars_disable(true)` *must* happen during the approach callback, and may not be deferred to an interrupt(). Else the train might trigger an ARS before the interrupt fires. + - `section_occupancy(section_id)` + Returns a table of train ids for the specified section, nil if no section id is provided, false if the section id is invalid, an empty table if the section id is valid but empty of trains. + #### Approach callbacks The LuaATC interface provides a way to hook into the approach callback system, which is for example used in the TSR rails (provided by advtrains_interlocking) or the station tracks (provided by advtrains_lines). However, for compatibility reasons, this behavior needs to be explicitly enabled. diff --git a/advtrains_luaautomation/environment.lua b/advtrains_luaautomation/environment.lua old mode 100644 new mode 100755 index e93b9c3..6b1a283 --- a/advtrains_luaautomation/environment.lua +++ b/advtrains_luaautomation/environment.lua @@ -223,6 +223,18 @@ if advtrains.interlocking then local pos = atlatc.pcnaming.resolve_pos(signal) return advtrains.interlocking.signal_set_aspect(pos) end + + --section_occupancy() + static_env.section_occupancy = function(ts_id) + if not ts_id then return nil end + ts_id = tostring(ts_id) + local response = advtrains.interlocking.db.get_ts(ts_id) + if response == nil then + return false + else + return response.trains + end + end end -- Lines-specific: -- cgit v1.2.3 '>path: root/advtrains_interlocking/signal_api.lua
blob: a7ef72453c7f2415598fb77a0441a15abc4e9708 (plain)
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
-- Signal API implementation

local F = advtrains.formspec

local signal = {}

signal.MASP_HALT = {
	name = "_halt",
	halt = true,
}

signal.MASP_DEFAULT = {
	name = "_default",
	default = true,
}

signal.ASPI_HALT = {
	main = 0,
	shunt = false,
}

signal.ASPI_FREE = {
	main = -1,
	shunt = false,
	proceed_as_main = true,
}

--[[
Implementation plan orwell 2024-01-28:
Most parts of ywang's implementation are fine, especially I like the formspecs. But I would like to change a few aspects (no pun intended) of this.
- Signal gets distant assigned via field in signal aspect table (instead of explicitly)
- Signal speed/shunt are no longer free-text but rather they need to be predefined in the node definition
To do this: Differentiation between:
== Main Aspect ==
This is what a signal is assigned by either the route system or the user.
It is a string key which has an appropriate entry in the node definition (where it has a description assigned)
The signal mod defines a function to set a signal to the most appropriate aspect. This function gets
a) the main aspect table (straight from node def)
b) the distant signal's aspect group name & aspect table

== Aspect ==
One concrete combination of lights/shapes that a signal signal shows. Handling these is at the discretion of
the signal mod defining the signal, and they are typically combinations of main aspect and distant aspect
Example:
- A Ks signal has the main_aspect="proceed_12" set for a route
- The signal at the end of the route shows main_aspect="proceed_8", advtrains also passes on that this means {main=8, shunt=false}
- The ndef.afunction(pos, node, main_aspect, rem_aspect, rem_aspinfo) determines that the signal should now show
		blinking green with main indicator 12 and dst indicator 8, and sets the nodes accordingly.
		This function can now return the Aspect Info table, which will be cached by advtrains until the aspect changes again
		and will be used when a train approaches the signal. If nil is returned, then the aspect will be queried next time
		by calling ndef.advtrains.get_aspect_info(pos)

Note that once apply_aspect returns, there is no need for advtrains anymore to query the aspect info.
When the signal, for any reason, wants to change its aspect by itself *without* going through the signal API then
it should update the aspect info cache by calling advtrains.interlocking.signal.update_aspect_info(pos)

Apply_aspect may also receive the special main aspect { name = "_halt", halt = true }. It usually means that the signal is not assigned to anything particular,
and it should cause the signal to show its most restrictive aspect. Typically it is a halt aspect, but e.g. for distant-only
signals this would be "expect stop".

A special case occurs for pure distant signals: Such signals must set apply_aspect, but must not set main_aspects. Behavior is as follows:
- Signal is uninitialized, distant signal is not assigned to a main signal, or no route is set: main_aspect == { name = "_halt", halt = true } and rem_aspect == nil
- A remote main signal is assigned (either by user or by route): main_aspect is always { name = "_default" } and rem_aspect / rem_aspinfo give the correct information

Main aspect names starting with underscore (e.g. "_default") are reserved and must not be used!

== Aspect Info ==
The actual signal aspect in the already-known format. This is what the trains use to determine halt/proceed and speed.
asp = {
	main = 0 (halt) / -1 (max speed) / false (no info) / <number> (speed limit)
	shunt = true (shunt free) / false (shunt not free)
	proceed_as_main = true (shunt move can proceed and become train move when main!=0) / false (no)
	dst = speed of the remote signal (like main, informative character, not actually used)
}

Node definition of signals:
- The signal needs some logic to figure out, for each combination of its own aspect group and the distant signal's aspect, what aspect info it can/will show.
ndef.advtrains = {
	main_aspects = {
		{ name = "proceed" description = "Proceed at full speed", <more data at discretion of signal>}
		{ name = "reduced" description = "Proceed at reduced speed", <more data at discretion of signal>}
	}
		-- This list is mainly for the selection dialog. Order of entries determines list order in the dropdown.
		-- Some fields have special meaning:
		-- name: A unique key to identify the main aspect. Might be required by some code.
		-- description: Text shown in UI dropdown
		-- Node can set any other fields at its discretion. They are not touched.
		-- Note: Pure distant signals (that cannot show halt) should NOT have a main_aspects table.
		--       For these signals no main aspect selection UI is shown and they cannot be startpoint of a route
	apply_aspect = function(pos, node, main_aspect, rem_aspect, rem_aspinfo)
		-- set the node to show the desired aspect
		-- called by advtrains when this signal's aspect group or the remote signal's aspect changes
		-- main_aspect is never nil, but can be one of the special aspects { halt = true } or { default = true }
		-- MAY return the aspect_info. If it returns nil then get_aspect_info will be queried at a later point.
	get_aspect_info(pos, main_aspect)
		-- Returns the aspect info table (main, shunt, dst etc.)
	distant_support = true or false
		-- If true, signal is considered in distant signalling. If false or nil, rem_aspect and rem_aspinfo are never set.
	route_role = one of "main", "main_distant", "shunt", "distant", "distant_repeater", "end"
		-- Determines how the signal behaves when routes are set. Only in effect when signal is assigned to a TCB.
		-- main: The signal is a possible endpoint for a train move route. Distant signals before it refer to it.
		-- shunt: The signal is a possible endpoint for a shunt move route. Ignored for distant signals.
		-- distant, distant_repeater: The next signal with role="main" is set as the remote signal. main_aspects may be undefined, the main aspect passed to apply_aspect is a dummy one in this case.
		-- distant: if more than one distant signal is before a main signal, only the last one is assigned (but any number of distant_repeater signals are allowed)