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
|
--advtrains_line_automation
-- Functions from Cesky Hvozd utility libraries which are used in the code integrated from Singularis are now provided here.
--[[
Jednoduchá funkce, která vyhodnotí condition jako podmínku
a podle výsledku vrátí buď true_result, nebo false_result.
]]
local function ifthenelse(condition, true_result, false_result)
if condition then
return true_result
else
return false_result
end
end
local ch_core = {}
-- gives various info about a player. we return only what is relevant outside CH
function ch_core.normalize_player(player_name_or_player)
local arg_type = type(player_name_or_player)
local player_name, player
if arg_type == "string" then
player_name = player_name_or_player
elseif arg_type == "number" then
player_name = tostring(player_name_or_player)
elseif (arg_type == "table" or arg_type == "userdata") and type(player_name_or_player.get_player_name) == "function" then
player_name = player_name_or_player:get_player_name()
if type(player_name) ~= "string" then
player_name = ""
else
if minetest.is_player(player_name_or_player) then
player = player_name_or_player
end
end
else
player_name = ""
end
if player_name == "" then
return {role = "none", player_name = "", viewname = "", privs = {}}
end
local privs = minetest.get_player_privs(player_name)
return {
role = "default",
player_name = player_name,
viewname = player_name,
privs = privs,
player = player or minetest.get_player_by_name(player_name),
}
end
-- == UTF8 helpers == --
local utf8_charlen = {}
for i = 1, 191, 1 do
-- 1 to 127 => jednobajtové znaky
-- 128 až 191 => nejsou dovoleny jako první bajt (=> vrátit 1 bajt)
utf8_charlen[i] = 1
end
for i = 192, 223, 1 do
utf8_charlen[i] = 2
end
for i = 224, 239, 1 do
utf8_charlen[i] = 3
end
for i = 240, 247, 1 do
utf8_charlen[i] = 4
end
for i = 248, 255, 1 do
utf8_charlen[i] = 1 -- neplatné UTF-8
end
local utf8_sort_data_1 = {
["\x20"] = "\x20", -- < >
["\x21"] = "\x21", -- <!>
["\x22"] = "\x22", -- <">
["\x23"] = "\x23", -- <#>
["\x25"] = "\x24", -- <%>
["\x26"] = "\x25", -- <&>
["\x27"] = "\x26", -- <'>
["\x28"] = "\x27", -- <(>
["\x29"] = "\x28", -- <)>
["\x2a"] = "\x29", -- <*>
["\x2b"] = "\x2a", -- <+>
["\x2c"] = "\x2b", -- <,>
["\x2d"] = "\x2c", -- <->
["\x2e"] = "\x2d", -- <.>
["\x2f"] = "\x2e", -- </>
["\x3a"] = "\x2f", -- <:>
["\x3b"] = "\x30", -- <;>
["\x3c"] = "\x31", -- <<>
["\x3d"] = "\x32", -- <=>
["\x3e"] = "\x33", -- <>>
["\x3f"] = "\x34", -- <?>
["\x40"] = "\x35", -- <@>
["\x5b"] = "\x36", -- <[>
["\x5c"] = "\x37", -- <\>
["\x5d"] = "\x38", -- <]>
["\x5e"] = "\x39", -- <^>
["\x5f"] = "\x3a", -- <_>
["\x60"] = "\x3b", -- <`>
["\x7b"] = "\x3c", -- <{>
["\x7c"] = "\x3d", -- <|>
["\x7d"] = "\x3e", -- <}>
["\x7e"] = "\x3f", -- <~>
["\x24"] = "\x40", -- <$>
["\x61"] = "\x41", -- <a>
["\x41"] = "\x42", -- <A>
["\x62"] = "\x47", -- <b>
["\x42"] = "\x48", -- <B>
["\x64"] = "\x4d", -- <d>
["\x44"] = "\x4e", -- <D>
["\x65"] = "\x51", -- <e>
["\x45"] = "\x52", -- <E>
["\x66"] = "\x57", -- <f>
["\x46"] = "\x58", -- <F>
["\x67"] = "\x59", -- <g>
["\x47"] = "\x5a", -- <G>
["\x68"] = "\x5b", -- <h>
["\x48"] = "\x5c", -- <H>
["\x69"] = "\x61", -- <i>
["\x49"] = "\x62", -- <I>
["\x6a"] = "\x65", -- <j>
["\x4a"] = "\x66", -- <J>
["\x6b"] = "\x67", -- <k>
["\x4b"] = "\x68", -- <K>
["\x6c"] = "\x69", -- <l>
["\x4c"] = "\x6a", -- <L>
["\x6d"] = "\x6f", -- <m>
["\x4d"] = "\x70", -- <M>
["\x6e"] = "\x71", -- <n>
["\x4e"] = "\x72", -- <N>
["\x6f"] = "\x75", -- <o>
["\x4f"] = "\x76", -- <O>
["\x70"] = "\x7b", -- <p>
["\x50"] = "\x7c", -- <P>
["\x71"] = "\x7d", -- <q>
["\x51"] = "\x7e", -- <Q>
["\x72"] = "\x7f", -- <r>
["\x52"] = "\x80", -- <R>
["\x73"] = "\x85", -- <s>
["\x53"] = "\x86", -- <S>
["\x74"] = "\x89", -- <t>
["\x54"] = "\x8a", -- <T>
["\x75"] = "\x8d", -- <u>
["\x55"] = "\x8e", -- <U>
["\x76"] = "\x93", -- <v>
["\x56"] = "\x94", -- <V>
["\x77"] = "\x95", -- <w>
["\x57"] = "\x96", -- <W>
["\x78"] = "\x97", -- <x>
["\x58"] = "\x98", -- <X>
["\x79"] = "\x99", -- <y>
["\x59"] = "\x9a", -- <Y>
["\x7a"] = "\x9d", -- <z>
["\x5a"] = "\x9e", -- <Z>
["\x30"] = "\xa1", -- <0>
["\x31"] = "\xa2", -- <1>
["\x32"] = "\xa3", -- <2>
["\x33"] = "\xa4", -- <3>
["\x34"] = "\xa5", -- <4>
["\x35"] = "\xa6", -- <5>
["\x36"] = "\xa7", -- <6>
["\x37"] = "\xa8", -- <7>
["\x38"] = "\xa9", -- <8>
["\x39"] = "\xaa", -- <9>
}
local utf8_sort_data_2 = {
["\xc3\xa1"] = "\x43", -- <á>
["\xc3\x81"] = "\x44", -- <Á>
["\xc3\xa4"] = "\x45", -- <ä>
["\xc3\x84"] = "\x46", -- <Ä>
["\xc4\x8d"] = "\x4b", -- <č>
["\xc4\x8c"] = "\x4c", -- <Č>
["\xc4\x8f"] = "\x4f", -- <ď>
["\xc4\x8e"] = "\x50", -- <Ď>
["\xc3\xa9"] = "\x53", -- <é>
["\xc3\x89"] = "\x54", -- <É>
["\xc4\x9b"] = "\x55", -- <ě>
["\xc4\x9a"] = "\x56", -- <Ě>
["\x63\x68"] = "\x5d", -- <ch>
["\x63\x48"] = "\x5e", -- <cH>
["\x43\x68"] = "\x5f", -- <Ch>
["\x43\x48"] = "\x60", -- <CH>
["\xc3\xad"] = "\x63", -- <í>
["\xc3\x8d"] = "\x64", -- <Í>
["\xc4\xba"] = "\x6b", -- <ĺ>
["\xc4\xb9"] = "\x6c", -- <Ĺ>
["\xc4\xbe"] = "\x6d", -- <ľ>
["\xc4\xbd"] = "\x6e", -- <Ľ>
["\xc5\x88"] = "\x73", -- <ň>
["\xc5\x87"] = "\x74", -- <Ň>
["\xc3\xb3"] = "\x77", -- <ó>
["\xc3\x93"] = "\x78", -- <Ó>
["\xc3\xb4"] = "\x79", -- <ô>
["\xc3\x94"] = "\x7a", -- <Ô>
["\xc5\x95"] = "\x81", -- <ŕ>
["\xc5\x94"] = "\x82", -- <Ŕ>
["\xc5\x99"] = "\x83", -- <ř>
["\xc5\x98"] = "\x84", -- <Ř>
["\xc5\xa1"] = "\x87", -- <š>
["\xc5\xa0"] = "\x88", -- <Š>
["\xc5\xa5"] = "\x8b", -- <ť>
["\xc5\xa4"] = "\x8c", -- <Ť>
["\xc3\xba"] = "\x8f", -- <ú>
["\xc3\x9a"] = "\x90", -- <Ú>
["\xc5\xaf"] = "\x91", -- <ů>
["\xc5\xae"] = "\x92", -- <Ů>
["\xc3\xbd"] = "\x9b", -- <ý>
["\xc3\x9d"] = "\x9c", -- <Ý>
["\xc5\xbe"] = "\x9f", -- <ž>
["\xc5\xbd"] = "\xa0", -- <Ž>
}
local utf8_sort_data_3 = {
["\x63"] = "\x49", -- <c>
["\x43"] = "\x4a", -- <C>
}
--[[
Vrátí počet UTF-8 znaků řetězce.
]]
function ch_core.utf8_length(s)
if s == "" then
return 0
end
local i, byte, bytes, chars
i = 1
chars = 0
bytes = string.len(s)
while i <= bytes do
byte = string.byte(s, i)
if byte < 192 then
i = i + 1
else
i = i + utf8_charlen[byte]
end
chars = chars + 1
end
return chars
end
--[[
Začne v řetězci `s` na fyzickém indexu `i` a bude se posouvat o `seek`
UTF-8 znaků doprava (pro záporný počet doleva); vrátí výsledný index
(na první bajt znaku), nebo nil, pokud posun přesáhl začátek,
resp. konec řetězce.
]]
function ch_core.utf8_seek(s, i, seek)
local bytes = string.len(s)
if i < 1 or i > bytes then
return nil
end
local b
if seek > 0 then
while true do
b = string.byte(s, i)
if b < 192 then
i = i + 1
else
i = i + utf8_charlen[b]
end
if i > bytes then
return nil
end
seek = seek - 1
if seek == 0 then
return i
end
end
elseif seek < 0 then
while true do
i = i - 1
if i < 1 then
return nil
end
b = string.byte(s, i)
if b < 128 or b >= 192 then
-- máme další znak
seek = seek + 1
if seek == 0 then
return i
end
end
end
else
return i
end
end
--[[
Je-li řetězec s delší než max_chars znaků, vrátí jeho prvních max_chars znaků
+ "...", jinak vrátí původní řetězec.
]]
function ch_core.utf8_truncate_right(s, max_chars, dots_string)
local i = ch_core.utf8_seek(s, 1, max_chars)
if i then
return s:sub(1, i - 1) .. (dots_string or "...")
else
return s
end
end
--[[
Rozdělí řetězec na pole neprázdných podřetězců o stanovené maximální délce
v UTF-8 znacích; v každé části vynechává mezery na začátku a na konci části;
přednostně dělí v místech mezer. Pro prázdný řetězec
(nebo řetězec tvořený jen mezerami) vrací prázdné pole.
]]
function ch_core.utf8_wrap(s, max_chars, options)
local i = 1 -- index do vstupního řetězce s
local s_bytes = string.len(s)
local result = {} -- výstupní pole
local r_text = "" -- výstupní řetězec
local r_chars = 0 -- počet UTF-8 znaků v řetězci r
local r_sp_begin -- index první mezery v poslední sekvenci mezer v r_text
local r_sp_end -- index poslední mezery v poslední sekvenci mezer v r_text
local b -- kód prvního bajtu aktuálního znaku
local c_bytes -- počet bajtů aktuálního znaku
-- options
local allow_empty_lines, max_result_lines, line_separator
if options then
allow_empty_lines = options.allow_empty_lines -- true or false
max_result_lines = options.max_result_lines -- nil or number
line_separator = options.line_separator -- nil or string
end
while i <= s_bytes do
b = string.byte(s, i)
-- print("byte["..i.."] = "..b.." ("..s:sub(i, i)..") r_sp = ("..(r_sp_begin or "nil")..".."..(r_sp_end or "nil")..")")
if r_chars > 0 or (b ~= 32 and (b ~= 10 or allow_empty_lines)) then -- na začátku řádky ignorovat mezery
if b < 192 then
c_bytes = 1
else
c_bytes = utf8_charlen[b]
end
-- vložit do r další znak (není-li to konec řádky)
if b ~= 10 then
r_text = r_text..s:sub(i, i + c_bytes - 1)
r_chars = r_chars + 1
if b == 32 then
-- znak je mezera
if r_sp_begin then
if r_sp_end then
-- začátek nové skupiny mezer (už nějaká byla)
r_sp_begin = string.len(r_text)
r_sp_end = nil
end
elseif not r_sp_end then
-- začátek první skupiny mezer (ještě žádná nebyla)
r_sp_begin = string.len(r_text)
end
else
-- znak není mezera ani konec řádky
if r_sp_begin and not r_sp_end then
r_sp_end = string.len(r_text) - c_bytes -- uzavřít skupinu mezer
end
end
end
if r_chars >= max_chars or b == 10 then
-- dosažen maximální počet znaků nebo znak \n => uzavřít řádku
if line_separator and #result > 0 then
result[#result] = result[#result]..line_separator
end
if r_chars < max_chars or not r_sp_begin then
-- žádné mezery => tvrdé dělení
table.insert(result, r_text)
r_text = ""
r_chars = 0
r_sp_begin, r_sp_end = nil, nil
elseif not r_sp_end then
-- průběžná skupina mezer => rozdělit zde
table.insert(result, r_text:sub(1, r_sp_begin - 1))
r_text = ""
r_chars = 0
r_sp_begin, r_sp_end = nil, nil
else
-- byla skupina mezer => rozdělit tamn class="hl opt">)
if linevar_def == nil then
-- invalid linevar => no stops
core.log("error", "Player "..custom_state.player_name.." selected invalid linevar "..linevars[linevar].linevar.."!")
else
local stops = linevar_def.stops
for linevar_index, stop_data in ipairs(linevar_def.stops) do
if is_visible_mode(stop_data.mode) then
local r = {
stn = assert(stop_data.stn),
name_fs = F(advtrains.lines.get_station_name(stop_data.stn)),
track_fs = stop_data.track or "",
dep = stop_data.dep,
wait = stop_data.wait or 10,
linevar_index = linevar_index,
}
if r.track_fs ~= "" then
r.track_fs = F("["..r.track_fs.."]")
end
table.insert(result, r)
if stn_to_select ~= nil and index_to_select == 0 and r.stn == stn_to_select then
index_to_select = #result -- stop selected
end
end
end
end
end
custom_state.stop = index_to_select
custom_state.stops = result
custom_state.message = ""
end
local function get_all_linevars()
local result = {}
local empty_table = {}
local trains_by_linevar = advtrains.lines.get_trains_by_linevar()
for stn, stdata in pairs(advtrains.lines.stations) do
for linevar, linevar_def in pairs(stdata.linevars or empty_table) do
local line, stn = advtrains.lines.linevar_decompose(linevar)
local target_fs = F(advtrains.lines.get_line_description(linevar_def, {line_number = false, last_stop = true, last_stop_prefix = "",
last_stop_uppercase = false, train_name = false}))
local status_fs
if trains_by_linevar[linevar] ~= nil then
status_fs = "#00ff00,v provozu"
else
status_fs = "#999999,neznámý"
end
table.insert(result, {
stn = stn,
linevar = linevar,
line_fs = F(line),
target_fs = target_fs,
track_fs = "",
status_fs = status_fs,
linevar_index = 1,
})
end
end
table.sort(result, function(a, b) return a.linevar < b.linevar end)
return result
end
--[[
stn_filter = string,
track_filter = string or nil,
]]
local function get_linevars_by_filter(stn_filter, track_filter)
local result = {}
local empty_table = {}
local line_description_options = {line_number = false, last_stop = true, last_stop_prefix = "", last_stop_uppercase = false, train_name = false}
local trains_by_linevar = advtrains.lines.get_trains_by_linevar()
assert(stn_filter)
if track_filter == "" then
track_filter = nil
end
for stn, stdata in pairs(advtrains.lines.stations) do
for linevar, linevar_def in pairs(stdata.linevars or empty_table) do
local last_stop_index = advtrains.lines.get_last_stop(linevar_def, false)
if last_stop_index ~= nil then
for linevar_index = 1, last_stop_index - 1 do -- NOTE: the last visible station is intentionally ignored here!
local stop_data = linevar_def.stops[linevar_index]
local initialized = false
local line, stn, line_fs, target_fs, status_fs
if
stop_data.stn == stn_filter and
is_visible_mode(stop_data.mode) and
(track_filter == nil or track_filter == stop_data.track)
then
if not initialized then
initialized = true
line, stn = advtrains.lines.linevar_decompose(linevar)
line_fs = F(line)
target_fs = F(advtrains.lines.get_line_description(linevar_def, line_description_options))
if trains_by_linevar[linevar] ~= nil then
status_fs = "#00ff00,v provozu"
else
status_fs = "#999999,neznámý"
end
end
local track_fs = stop_data.track
if track_fs == nil or track_fs == "" then
track_fs = ""
else
track_fs = F("["..track_fs.."]")
end
table.insert(result, {
stn = stn,
linevar = linevar,
line_fs = line_fs,
target_fs = target_fs,
track_fs = track_fs,
status_fs = status_fs,
linevar_index = assert(linevar_index),
})
end
end
end
end
end
table.sort(result, function(a, b)
if a.linevar ~= b.linevar then
return a.linevar < b.linevar
else
return a.linevar_index < b.linevar_index
end
end)
return result
end
local function is_jr_node_name(name)
return core.get_item_group(name, "ch_jrad") ~= 0
end
-- refresh custom_state.linevars according to custom_state.stn and custom_state.track
local function jr_refresh_linevars(custom_state, linevar_to_select, linevar_index_to_select)
if linevar_to_select == "" then
linevar_to_select = nil
end
assert(linevar_index_to_select == nil or type(linevar_index_to_select) == "number")
local stn = assert(custom_state.stn)
local stn_info = assert(custom_state.stns[stn])
local track = assert(custom_state.track)
local tracks = assert(custom_state.tracks)
local new_linevars
if stn_info.stn == "" then
new_linevars = get_all_linevars()
else
new_linevars = get_linevars_by_filter(stn_info.stn, assert(tracks[track]))
end
local index_to_select = 1
if linevar_to_select ~= nil then
for i, r in ipairs(new_linevars) do
if r.linevar == linevar_to_select and (linevar_index_to_select == nil or r.linevar_index == linevar_index_to_select) then
index_to_select = i
break
end
end
end
if new_linevars[index_to_select] == nil then
index_to_select = 0
end
custom_state.linevars = new_linevars
custom_state.linevar = index_to_select
custom_state.message = ""
local pos = custom_state.pos
if pos ~= nil and is_jr_node_name(core.get_node(pos).name) then
local meta = core.get_meta(pos)
meta:set_string("stn", custom_state.stns[custom_state.stn].stn)
meta:set_string("track", custom_state.tracks[custom_state.track])
local infotext = {"jízdní řád\n"}
if stn_info.stn == "" then
table.insert(infotext, "<všechny linky>")
else
table.insert(infotext, advtrains.lines.get_station_name(stn_info.stn))
if custom_state.tracks[custom_state.track] ~= "" then
table.insert(infotext, " ["..custom_state.tracks[custom_state.track].."]")
end
end
if new_linevars[1] ~= nil then
local prefix = "\n"
local set = {[""] = true}
for _, linevar_info in ipairs(new_linevars) do
local line = advtrains.lines.linevar_decompose(linevar_info.linevar)
if not set[line] then
set[line] = true
table.insert(infotext, prefix.."["..line.."]")
prefix = " "
end
end
end
meta:set_string("infotext", table.concat(infotext))
end
end
-- refresh custom_state.tracks according to custom_state.stn
-- and selects a specified track, if available
local function jr_refresh_tracks(custom_state, track_to_select)
if track_to_select == "" then
track_to_select = nil
end
local result = {""}
local index_to_select = 1
local current_stn = custom_state.stns[custom_state.stn].stn
if current_stn ~= "" and advtrains.lines.stations[current_stn] ~= nil then
local track_set = {[""] = true}
-- search for tracks:
for epos, stdata in pairs(advtrains.lines.stops) do
if stdata.stn == current_stn and stdata.track ~= nil and not track_set[stdata.track] then
track_set[stdata.track] = true
table.insert(result, tostring(stdata.track))
end
end
if #result > 1 then
table.sort(result)
assert(result[1] == "")
if track_to_select ~= nil then
for i, track in ipairs(result) do
if track_to_select == track then
index_to_select = i
end
end
end
end
end
custom_state.tracks = result
custom_state.track = index_to_select
custom_state.message = ""
end
--[[
-- will refresh custom_state.stns[] and (optionally) select a wanted station if it's on the list,
-- otherwise the default 'select station' option will be chosen
custom_state = table,
stn_to_select = string or nil,
]]
local function jr_refresh_stns(custom_state, stn_to_select)
if stn_to_select == "" then
stn_to_select = nil
end
local result = {all_stations_record}
local index_to_select = 1
for i, st in ipairs(load_stations()) do
result[1 + i] = {
stn = assert(st.stn),
fs = F(st.stn.." | "..st.name),
name_fs = F(st.name),
}
if stn_to_select ~= nil and st.stn == stn_to_select then
index_to_select = 1 + i
end
end
custom_state.stns = result
custom_state.stn = index_to_select
custom_state.message = ""
end
-- will refresh a departure message according to linevar + stop
local function jr_refresh_departure(custom_state)
local linevar_info = custom_state.linevars[custom_state.linevar]
local stop_info = custom_state.stops[custom_state.stop]
if linevar_info == nil or stop_info == nil then
custom_state.message = ""
return
end
local linevar_def = advtrains.lines.try_get_linevar_def(linevar_info.linevar)
if linevar_def == nil then
custom_state.message = ""
return
end
local rwtime = rwt.to_secs(rwt.get_time())
local prediction = advtrains.lines.predict_station_departures(linevar_def, assert(stop_info.linevar_index), rwtime)
if #prediction == 0 then
custom_state.message = "v nejbližší době nenalezeny žádné odjezdy zvolené linky"
return
end
local deps = {}
for i, pred in ipairs(prediction) do
deps[i] = tostring(pred.dep - rwtime)
end
custom_state.message = "nejbližší odjezdy zvolené linky za: "..table.concat(deps, ", ").." s"
end
local function get_jr_formspec(custom_state)
local formspec = {
ch_core.formspec_header({
formspec_version = 6,
size = {20, 12},
auto_background = true,
})
}
local access_level = "player" -- player | owner | admin
local node_owner
if custom_state.pos ~= nil then
node_owner = core.get_meta(custom_state.pos):get_string("owner")
if node_owner == "" then
node_owner = nil
end
end
local stn, stn_owner
if custom_state.stn > 1 and custom_state.stns[custom_state.stn] ~= nil then
stn_owner = (advtrains.lines.stations[custom_state.stns[custom_state.stn].stn] or {}).owner -- may be nil
end
if ch_core.get_player_role(custom_state.player_name) == "admin" then
access_level = "admin"
elseif custom_state.player_name == node_owner or custom_state.player_name == stn_owner then
access_level = "owner"
end
if access_level ~= "player" then
-- admin or owner:
table.insert(formspec, "label[0.5,0.6;Jízdní řády]"..
"dropdown[5,0.3;10,0.6;dopravna;")
for i, r in ipairs(custom_state.stns) do
table.insert(formspec, ifthenelse(i == 1, r.fs, ","..r.fs))
end
table.insert(formspec, ";"..custom_state.stn..";true]"..
"dropdown[15.25,0.3;3.5,0.6;kolej;")
for i, r in ipairs(custom_state.tracks) do
if i == 1 then
table.insert(formspec, "(všechny koleje)")
else
table.insert(formspec, ","..F(r))
end
end
table.insert(formspec, ";"..custom_state.track..";true]")
else
-- player (including 'new' players)
local stn_info = custom_state.stns[custom_state.stn]
if stn_info.stn == "" then
table.insert(formspec, "label[0.5,0.6;Jízdní řády (všechny linky)]")
else
local track = custom_state.tracks[custom_state.track]
if track ~= "" then
track = F(" ["..track.."]")
end
table.insert(formspec, "label[0.5,0.6;"..F("Jízdní řády: ")..stn_info.name_fs..track.."]")
end
end
if access_level ~= "admin" then
table.insert(formspec, "label[0.5,1.65;vlastník/ice j. řádu: ")
if node_owner ~= nil then
table.insert(formspec, ch_core.prihlasovaci_na_zobrazovaci(node_owner))
else
table.insert(formspec, "???")
end
if stn_owner ~= nil then
table.insert(formspec, " | dopravnu spravuje: ")
table.insert(formspec, ch_core.prihlasovaci_na_zobrazovaci(stn_owner))
end
table.insert(formspec, "]")
else
if node_owner ~= nil then
table.insert(formspec, "label[0.5,1.65;vlastník/ice:]"..
"field[2.75,1.25;5,0.75;owner;;")
table.insert(formspec, ch_core.prihlasovaci_na_zobrazovaci(node_owner))
table.insert(formspec, "]button[8,1.25;3,0.75;setowner;nastavit]")
end
if stn_owner ~= nil then
table.insert(formspec, "label[11.25,1.65;dopravnu spravuje: "..ch_core.prihlasovaci_na_zobrazovaci(stn_owner).."]")
end
end
table.insert(formspec, "tablecolumns[text,align=right,tooltip=linka;text,width=12,tooltip=cíl;text,tooltip=kolej;color;text,tooltip=stav]"..
"table[0.5,2.25;11,5;linka;")
for i, r in ipairs(custom_state.linevars) do
if i > 1 then
table.insert(formspec, ",")
end
table.insert(formspec, r.line_fs..","..r.target_fs..","..r.track_fs..","..r.status_fs)
end
table.insert(formspec, ifthenelse(custom_state.linevar > 0, ";"..custom_state.linevar.."]", ";]"))
table.insert(formspec, "tablecolumns[text,align=right;text;text]"..
"table[12.5,2.25;7,8.75;zastavka;")
if custom_state.stops[1] ~= nil then
local selected_stop_index = custom_state.stop
if custom_state.stops[selected_stop_index] == nil then
selected_stop_index = 1
end
local base_dep
for i, r in ipairs(custom_state.stops) do
if i > 1 then
table.insert(formspec, ",")
end
if i >= selected_stop_index then
if i == selected_stop_index then
base_dep = assert(r.dep)
table.insert(formspec, "0,")
else
table.insert(formspec, (r.dep - base_dep)..",")
end
else
table.insert(formspec, " ,")
end
table.insert(formspec, r.name_fs..","..r.track_fs)
end
table.insert(formspec, ";"..selected_stop_index.."]")
else
table.insert(formspec, ";]") -- empty list
end
table.insert(formspec, "button_exit[18.75,0.3;0.75,0.75;close;X]")
if custom_state.message ~= "" then
table.insert(formspec, "label[5.25,11.35;"..F(custom_state.message).."]")
end
table.insert(formspec, "button[0.5,11;4.5,0.75;refresh;zjistit nejbližší odjezdy...]")
return table.concat(formspec)
end
local function jr_formspec_callback(custom_state, player, formname, fields)
--[[
fields:
- dopravna (dropdown)
- kolej (dropdown)
- owner (field)
- setowner (button)
- linka (table)
- zastavka (table)
- close (button_exit)
- refresh (button)
]]
if fields.dopravna then
local new_stn = tonumber(fields.dopravna)
if new_stn ~= nil and new_stn ~= custom_state.stn and custom_state.stns[new_stn] ~= nil then
custom_state.stn = new_stn
local current_track = custom_state.tracks[custom_state.track] or ""
local current_linevar_info = custom_state.linevars[custom_state.linevar]
jr_refresh_tracks(custom_state, current_track)
if current_linevar_info ~= nil then
jr_refresh_linevars(custom_state, current_linevar_info.linevar, current_linevar_info.linevar_index)
else
jr_refresh_linevars(custom_state)
end
jr_refresh_stops(custom_state, custom_state.stns[new_stn].stn)
jr_refresh_departure(custom_state)
return get_jr_formspec(custom_state)
end
end
if fields.kolej then
local new_track = tonumber(fields.kolej)
if new_track ~= nil and new_track ~= custom_state.track and custom_state.tracks[new_track] ~= nil then
custom_state.track = new_track
local current_linevar_info = custom_state.linevars[custom_state.linevar]
if current_linevar_info ~= nil then
jr_refresh_linevars(custom_state, current_linevar_info.linevar, current_linevar_info.linevar_index)
else
jr_refresh_linevars(custom_state)
end
jr_refresh_stops(custom_state, custom_state.stns[custom_state.stn].stn)
jr_refresh_departure(custom_state)
return get_jr_formspec(custom_state)
end
end
if fields.setowner then
-- TODO: implementovat změnu vlastníka/ice
return get_jr_formspec(custom_state)
end
if fields.linka then
local event = core.explode_table_event(fields.linka)
local new_line
if event.type == "CHG" or event.type == "DCL" then
new_line = tonumber(event.row)
end
if new_line ~= nil and new_line ~= custom_state.linevar then
local new_linevar_info = custom_state.linevars[new_line]
if new_linevar_info ~= nil then
jr_refresh_linevars(custom_state, new_linevar_info.linevar, new_linevar_info.linevar_index)
else
jr_refresh_linevars(custom_state)
end
jr_refresh_stops(custom_state, custom_state.stns[custom_state.stn].stn)
jr_refresh_departure(custom_state)
return get_jr_formspec(custom_state)
end
end
if fields.zastavka then
local event = core.explode_table_event(fields.zastavka)
if event.type == "CHG" or event.type == "DCL" then
local new_stop = tonumber(event.row)
if new_stop ~= nil and new_stop ~= custom_state.stop and custom_state.stops[new_stop] ~= nil then
custom_state.stop = new_stop
if event.type == "DCL" then
jr_refresh_departure(custom_state)
end
return get_jr_formspec(custom_state)
end
end
end
if fields.refresh then
jr_refresh_departure(custom_state)
return get_jr_formspec(custom_state)
end
end
local function show_jr_formspec(player, pos, stn, track, linevar, stop_stn)
assert(core.is_player(player))
local custom_state = {
player_name = player:get_player_name(),
stn = 1,
stns = {all_stations_record},
track = 1,
tracks = {""},
linevar = 0,
linevars = {},
stop = 0,
stops = {},
message = "",
}
if pos ~= nil then
custom_state.pos = pos
end
jr_refresh_stns(custom_state, stn)
jr_refresh_tracks(custom_state, track)
jr_refresh_linevars(custom_state, linevar)
if stop_stn == nil then
stop_stn = custom_state.stns[custom_state.stn].stn
end
jr_refresh_stops(custom_state, stop_stn)
jr_refresh_departure(custom_state)
-- show formspec:
return ch_core.show_formspec(player, "advtrains_line_automation:jizdni_rad",
get_jr_formspec(custom_state), jr_formspec_callback, custom_state, {})
end
local visual_scale = 15/16
local node_box = {type = "fixed", fixed = {
-16/32, -16/32, 15/32 / visual_scale,
16/32, 16/32, 16/32 / visual_scale,
}}
local sbox = {type = "fixed", fixed = {
-16/32 * visual_scale, -16/32 * visual_scale, 15/32,
16/32 * visual_scale, 16/32 * visual_scale, 16/32,
}}
local def = {
description = "jízdní řád",
drawtype = "nodebox",
node_box = node_box,
selection_box = sbox,
collision_box = sbox,
tiles = {
{name = "ch_core_white_pixel.png^[multiply:#aaaaaa"},
{name = "ch_core_white_pixel.png^[multiply:#aaaaaa"},
{name = "ch_core_white_pixel.png^[multiply:#aaaaaa"},
{name = "ch_core_white_pixel.png^[multiply:#aaaaaa"},
{name = "advtrains_line_automation_jrad.png"},
{name = "advtrains_line_automation_jrad.png"},
},
paramtype = "light",
paramtype2 = "4dir",
sunlight_propagates = true,
groups = {cracky = 3, ch_jrad = 1},
sounds = default.node_sound_metal_defaults(),
visual_scale = visual_scale,
after_place_node = function(pos, placer, itemstack, pointed_thing)
local player_name = placer and placer:get_player_name()
if player_name ~= nil then
local meta = core.get_meta(pos)
meta:set_string("infotext", "jízdní řád")
meta:set_string("owner", player_name)
end
end,
can_dig = function(pos, player)
if player == nil then
return false
end
local player_name = player:get_player_name()
if ch_core.get_player_role(player_name) == "admin" then
return true
end
if core.is_protected(pos, player_name) then
core.record_protection_violation(pos, player_name)
return false
end
local meta = core.get_meta(pos)
local owner = meta:get_string("owner")
return owner == "" or owner == player_name
end,
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
if clicker ~= nil and core.is_player(clicker) then
local meta = core.get_meta(pos)
show_jr_formspec(clicker, pos, meta:get_string("stn"), meta:get_string("track"))
end
end,
}
core.register_node("advtrains_line_automation:jrad", table.copy(def))
def.description = "jízdní řád (na tyč)"
def.node_box = {type = "fixed", fixed = {
-16/32, -16/32, 27/32 / visual_scale,
16/32, 16/32, 28/32 / visual_scale,
}}
def.selection_box = {type = "fixed", fixed = {
-16/32 * visual_scale, -16/32 * visual_scale, 27/32,
16/32 * visual_scale, 16/32 * visual_scale, 28/32,
}}
def.collision_box = def.selection_box
def.tiles = table.copy(def.tiles)
def.tiles[5] = def.tiles[1]
core.register_node("advtrains_line_automation:jrad_on_pole", def)
|