aboutsummaryrefslogtreecommitdiff
path: root/assets/blender/gleis
ModeNameSize
-rw-r--r--advtrains_dtrack_shared.xcf75136logplain
d---------before_redo669logplain
d---------infos79logplain
-rw-r--r--licence.txt~0logplain
-rw-r--r--rail_redo.blend1929280logplain
-rw-r--r--rail_redo_bk_curves.blend791704logplain
-rw-r--r--rail_redo_bk_rswit.blend1567640logplain
-rw-r--r--rail_redo_bk_straight.blend707632logplain
-rw-r--r--rail_redo_bk_swit.blend1189696logplain
d---------texturen70logplain
-rw-r--r--uv_rails_all.png3692logplain
'#n210'>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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
local def
local F = minetest.formspec_escape
local ifthenelse = ch_core.ifthenelse
local rwt = assert(advtrains.lines.rwt)

local function load_stations()
	local result = {}
	local by_stn = {}
	local all_lines_set = {["*"] = true}
	local lines_set = {}
	for stn, data in pairs(advtrains.lines.stations) do
		local new_station = {
			stn = stn,
			name = data.name or "",
			owner = data.owner or "",
			tracks = {},
		}
		by_stn[stn] = new_station
		lines_set[stn] = {}
		table.insert(result, new_station)
	end
	for epos, data in pairs(advtrains.lines.stops) do
		if data.stn ~= nil and data.stn ~= "" and by_stn[data.stn] ~= nil then
			local st = by_stn[data.stn]
			local new_track = {
				epos = epos,
				pos = assert(advtrains.decode_pos(epos)),
				track = data.track or "",
			}
			table.insert(st.tracks, new_track)
			local ars = data.ars
			if ars ~= nil then
				if ars.default then
					lines_set[data.stn] = all_lines_set
				else
					local set = lines_set[data.stn]
					for _, rule in ipairs(ars) do
						if not rule.n and rule.ln ~= nil then
							set[rule.ln] = true
						end
					end
				end
			end
		end
	end
	for _, st in ipairs(result) do
		local set = lines_set[st.stn]
		if set["*"] then
			st.lines = {"*"}
		else
			local lines = {}
			for line, _ in pairs(set) do
				table.insert(lines, line)
			end
			table.sort(lines)
			st.lines = lines
		end
	end
	table.sort(result, function(a, b) return a.stn < b.stn end)
	return result
end

advtrains.lines.load_stations_for_formspec = load_stations

local function station_distance(player_pos, st)
	if st.tracks[1] == nil then
		return -- no tracks
	end
	local distance = vector.distance(player_pos, st.tracks[1].pos)
	for i = 2, #st.tracks do
		local d2 = vector.distance(player_pos, st.tracks[i].pos)
		if d2 < distance then
			distance = d2
		end
	end
	return distance
end

local function station_distance_s(player_pos, st)
	local result = station_distance(player_pos, st)
	if result ~= nil then
		return string.format("%d m", result)
	else
		return ""
	end
end


local function filter_all()
	return true
end

local function filter_mine(a, player_info)
	return a.owner == player_info.name
end

local function sort_by_distance(a, b, player_info)
	return (station_distance(player_info.pos, a) or 1.0e+20) < (station_distance(player_info.pos, b) or 1.0e+20)
end

local function sort_by_stn(a, b)
	return tostring(a.stn) < tostring(b.stn)
end

local function sort_by_name(a, b)
	local a_key, b_key = ch_core.utf8_radici_klic(a.name, false), ch_core.utf8_radici_klic(b.name, false)
	if a_key ~= b_key then
		return a_key < b_key
	else
		return sort_by_stn(a, b)
	end
end

local filters = {
	{
		description = "od nejbližší",
		-- filter = filter_all,
		sorter = sort_by_distance,
	},
	{
		description = "podle kódu A-Z",
		-- filter = filter_all,
		sorter = sort_by_stn,
	},
	{
		description = "podle názvu A-Z",
		-- filter = filter_all,
		sorter = sort_by_name,
	},
	{
		description = "moje (podle kódu A-Z)",
		filter = filter_mine,
		sorter = sort_by_stn,
	},
	{
		description = "moje (podle názvu A-Z)",
		filter = filter_mine,
		sorter = sort_by_name,
	},
	{
		description = "moje (od nejbližší)",
		filter = filter_mine,
		sorter = sort_by_distance,
	},
}


local function get_formspec(custom_state)
	local pinfo = ch_core.normalize_player(assert(custom_state.player_name))
	if pinfo.player == nil then
		minetest.log("error", "Expected player not in game!")
		return ""
	end
	local player_info = {
		name = assert(custom_state.player_name),
		pos = assert(custom_state.player_pos),
	}

	local stations = custom_state.stations
	if stations == nil then
		local filter = filters[custom_state.active_filter]
		local filter_func = filter.filter or filter_all
		local sorter_func = filter.sorter
		stations = {}
		for _, st in ipairs(load_stations()) do
			if filter_func(st, player_info) then
				table.insert(stations, st)
			end
		end
		table.sort(stations, function(a, b) return sorter_func(a, b, player_info) end)
		custom_state.stations = stations
		if custom_state.selection_index ~= nil and custom_state.selection_index > #stations + 1 then
			custom_state.selection_index = nil
		end
	end

	local selection_index = custom_state.selection_index
    local formspec = {
        ch_core.formspec_header({formspec_version = 6, size = {20, 10}, auto_background = true}),
		"label[0.5,0.6;Editor dopraven]",
    }

	table.insert(formspec, "tablecolumns[image,"..
		"0=ch_core_empty.png,"..
		"1=basic_materials_padlock.png\\^[resize:16x16"..
		";text;text,width=25;color,span=1;text,width=7;text;text]"..
		"table[0.5,1.25;19,5;dopravna;0,KÓD,NÁZEV,#ffffff,SPRAVUJE,VZDÁLENOST,INFO")
	for _, st in ipairs(stations) do
		local n_tracks = #st.tracks
		table.insert(formspec, ",0,"..F(st.stn)..","..F(st.name)..",#ffffff,"..F(ch_core.prihlasovaci_na_zobrazovaci(st.owner))..","..
			F(station_distance_s(custom_state.player_pos, st))..","..n_tracks.." kolej")
		if n_tracks < 1 or n_tracks > 4 then
			table.insert(formspec, "í")
		elseif n_tracks ~= 1 then
			table.insert(formspec, "e")
		end
		if n_tracks > 0 then
			table.insert(formspec, "\\, linky " ..F(table.concat(st.lines, ",")))
		end
	end
	table.insert(formspec, ";"..(selection_index or "").."]")

	-- rozbalovací pole s volbou filtru a řazení
	table.insert(formspec, "dropdown[8,0.3;7,0.6;filter;"..F(assert(filters[1].description)))
	for i = 2, #filters do
		table.insert(formspec, ","..F(filters[i].description))
	end
	table.insert(formspec, ";"..custom_state.active_filter..";true]")

	table.insert(formspec, "button_exit[18.75,0.3;0.75,0.75;close;X]")

	local st = selection_index ~= nil and stations[selection_index - 1]
	local stn, nazev, spravuje
	if st then
		stn = F(st.stn)
		nazev = F(st.name)
		spravuje = F(ch_core.prihlasovaci_na_zobrazovaci(st.owner))
	else
		stn, nazev, spravuje = "", "", F(pinfo.viewname)
	end
	table.insert(formspec,
		"field[0.5,7;2.5,0.75;stn;kód:;"..stn.."]"..
		"field[3.25,7;7,0.75;name;název:;"..nazev.."]"..
		ifthenelse(pinfo.role == "admin", "field[10.5,7;4,0.75;owner;spravuje:;", "label[10.5,6.75;spravuje:\n")..
		spravuje.."]")
	if pinfo.role ~= "new" then
		table.insert(formspec, "button[0.5,8;4.5,0.75;vytvorit;vytvořit novou]"..
			"button[10,8;4.5,0.75;jrad;jízdní řády...]")
		if st and (pinfo.role == "admin" or st.owner == pinfo.player_name) then
			table.insert(formspec, "button[5.25,8;4.5,0.75;ulozit;uložit změny]")
			if st.tracks[1] == nil then
				table.insert(formspec, "button[15.25,8;3,0.75;smazat;smazat]")
			end
			if custom_state.linevars[1] ~= nil then
				table.insert(formspec, "label[0.5,9.4;přiřadit kolej]"..
					"field[2.75,9.1;1,0.6;kolej;;]"..
					"label[3.9,9.4;lince]"..
					"dropdown[5,9.1;5,0.6;klinevar;")
				for i, lvar in ipairs(custom_state.linevars) do
					if i ~= 1 then
						table.insert(formspec, ",")
					end
					table.insert(formspec, F(lvar.linevar.." | "..lvar.dep.." "..stn.." ["..lvar.track.."]"))
				end
				table.insert(formspec, ";"..custom_state.current_linevar..";true]"..
					"button[10.25,9;4.25,0.75;priradit_kolej;přiřadit]"..
					"tooltip[klinevar;")
				table.insert(formspec, F("Vysvětlení formátu:\n<linka>/<kód vých.dop.>/<sm.kód> | <odjezd> <kód dop.> [<stávající kolej>]"))
				table.insert(formspec, "]")
			end
		end
	end
	if st and st.tracks[1] ~= nil then
		table.insert(formspec, "textarea[14.75,7;4.75,2.5;;pozice kolejí:;"..F(minetest.pos_to_string(st.tracks[1].pos)))
		for i = 2, #st.tracks do
			table.insert(formspec, "\n"..F(minetest.pos_to_string(st.tracks[i].pos)))
		end
		table.insert(formspec, "]")
	end
	return table.concat(formspec)
end

local function formspec_callback(custom_state, player, formname, fields)
	local update_formspec = false
	if fields.vytvorit then
		local new_stn, new_name, new_owner = fields.stn, fields.name or "", fields.owner
		local pinfo = ch_core.normalize_player(player)
		if pinfo.role ~= "admin" or new_owner == nil or new_owner == "" then
			new_owner = pinfo.player_name
		else
			new_owner = ch_core.jmeno_na_prihlasovaci(new_owner)
		end
		if new_stn == nil or new_stn == "" then
			ch_core.systemovy_kanal(custom_state.player_name, "CHYBA: kód nesmí být prázdný!")
			return
		end
		local als = advtrains.lines.stations
		if als[new_stn] ~= nil then
			ch_core.systemovy_kanal(custom_state.player_name, "CHYBA: zastávka s kódem "..new_stn.." již existuje!")
			return
		end
		als[new_stn] = {name = assert(new_name), owner = assert(new_owner)}
		custom_state.stations = nil
		update_formspec = true
		ch_core.systemovy_kanal(custom_state.player_name, "Dopravna úspěšně vytvořena.")
	elseif fields.ulozit then
		local new_stn, new_name, new_owner = fields.stn, fields.name or "", fields.owner
		local pinfo = ch_core.normalize_player(player)
		local st = custom_state.stations[(custom_state.selection_index or 0) - 1]
		if st == nil then
			ch_core.systemovy_kanal(custom_state.player_name, "CHYBA: není vybrána žádná zastávka!")
			return
		end
		local change_stn, change_name = st.stn ~= new_stn, st.name ~= new_name
		local change_owner = pinfo.role == "admin" and fields.owner ~= nil and fields.owner ~= "" and
			ch_core.jmeno_na_prihlasovaci(fields.owner) ~= st.owner
		if not change_stn and not change_name and not change_owner then
			ch_core.systemovy_kanal(custom_state.player_name, "Nic nezměněno.")
			return
		end
		local t = advtrains.lines.stations[st.stn]
		if t == nil then
			ch_core.systemovy_kanal(custom_state.player_name, "CHYBA: zastávka k úpravě nebyla nalezena! Toto je pravděpodobně vnitřní chyba editoru.")
			return
		end
		if change_stn then
			-- zkontrolovat, že cílový kód je volný
			if advtrains.lines.stations[new_stn] ~= nil then
				ch_core.systemovy_kanal(custom_state.player_name, "CHYBA: zastávka s kódem "..new_stn.." již existuje!")
				return
			end
		end
		if change_owner then
			t.owner = ch_core.jmeno_na_prihlasovaci(fields.owner)
			ch_core.systemovy_kanal(custom_state.player_name, "Správa zastávky změněna.")
		end
		if change_name then
			t.name = new_name
			ch_core.systemovy_kanal(custom_state.player_name, "Jmeno zastávky změněno.")
		end
		if change_stn then
			advtrains.lines.stations[new_stn] = t
			local stops = advtrains.lines.stops
			local count = 0
			for _, trackinfo in ipairs(st.tracks) do
				local stop = stops[trackinfo.epos]
				if stop == nil then
					minetest.log("error", "Expected track at position "..trackinfo.epos.." not found!")
				elseif stop.stn ~= st.stn then
					minetest.log("error", "Track at position "..trackinfo.epos.." has unexpected stn '"..tostring(stop.stn).."' instead of '"..st.stn.."'!")
				else
					stop.stn = new_stn
					count = count + 1
				end
			end
			advtrains.lines.stations[st.stn] = nil
			ch_core.systemovy_kanal(custom_state.player_name, "Kód zastávky změněn, "..count.." bloků kolejí aktualizováno.")
		end
		custom_state.stations = nil
		update_formspec = true
	elseif fields.smazat then
		local pinfo = ch_core.normalize_player(player)
		local st = custom_state.stations[(custom_state.selection_index or 0) - 1]
		if st == nil then
			ch_core.systemovy_kanal(custom_state.player_name, "CHYBA: není vybrána žádná zastávka!")
			return
		end
		if st.tracks[1] ~= nil then
			ch_core.systemovy_kanal(custom_state.player_name, "Nelze smazat zastávku, k níž jsou přiřazeny koleje!")
			return
		end
		local t = advtrains.lines.stations[st.stn]
		if t == nil then
			ch_core.systemovy_kanal(custom_state.player_name, "CHYBA: zastávka k úpravě nebyla nalezena! Toto je pravděpodobně vnitřní chyba editoru.")
			return
		end
		advtrains.lines.stations[st.stn] = nil
		ch_core.systemovy_kanal(custom_state.player_name, "Zastávka úspěšně smazána.")
		custom_state.stations = nil
		update_formspec = true
	elseif fields.jrad then
		local st = custom_state.stations[(custom_state.selection_index or 0) - 1]
		if st == nil then
			ch_core.systemovy_kanal(custom_state.player_name, "CHYBA: není vybrána žádná zastávka!")
			return
		end
		advtrains.lines.show_jr_formspec(player, nil, assert(st.stn))
		return
	elseif fields.priradit_kolej then
		local st = custom_state.stations[(custom_state.selection_index or 0) - 1]
		if st == nil then
			ch_core.systemovy_kanal(custom_state.player_name, "CHYBA: není vybrána žádná zastávka!")
			return
		end
		local linevar_to_change = custom_state.linevars[custom_state.current_linevar]
		if linevar_to_change == nil then
			ch_core.systemovy_kanal(custom_state.player_name, "CHYBA: vnitřní chyba 1!")
			return
		end
		local linevar_def = advtrains.lines.try_get_linevar_def(linevar_to_change.linevar, linevar_to_change.stn)
		if linevar_def == nil then
			ch_core.systemovy_kanal(custom_state.player_name, "CHYBA: vnitřní chyba 2!")
			return
		end
		local stop = linevar_def.stops[linevar_to_change.linevar_index]
		if stop == nil then
			ch_core.systemovy_kanal(custom_state.player_name, "CHYBA: vnitřní chyba 3!")
			return
		end
		if stop.stn ~= st.stn then
			ch_core.systemovy_kanal(custom_state.player_name, "CHYBA: vnitřní chyba 4!")
			return
		end
		stop.track = tostring(fields.kolej)
		linevar_to_change.track = stop.track
		ch_core.systemovy_kanal(custom_state.player_name, "Přiřazená kolej úspěšně nastavena.")
		update_formspec = true
	elseif fields.quit then
		return
	end

	if fields.dopravna then
		local event = minetest.explode_table_event(fields.dopravna)
		if event.type == "CHG" then
			custom_state.selection_index = ifthenelse(event.row > 1, event.row, nil)
			local st = custom_state.stations[(custom_state.selection_index or 0) - 1]
			local new_linevars = {}
			custom_state.linevars = new_linevars
			custom_state.current_linevar = 1
			if st ~= nil then
				for _, r in ipairs(advtrains.lines.get_linevars_by_station(st.stn)) do
					local line, stn = advtrains.lines.linevar_decompose(r.linevar)
					for _, i in ipairs(r.indices) do
						local stop = r.linevar_def.stops[i]
						table.insert(new_linevars, {
							stn = stn,
							linevar = r.linevar,
							linevar_index = i,
							dep = assert(stop.dep),
							track = stop.track or "",
						})
					end
				end
			end
			update_formspec = true
		end
	end

	if fields.klinevar then
		local n = tonumber(fields.klinevar)
		if n ~= nil and custom_state.linevars[n] ~= nil then
			custom_state.current_linevar = n
		end
	end

	-- filter (must be the last)
	if fields.filter then
		local new_filter = tonumber(fields.filter)
		if filters[new_filter] ~= nil and new_filter ~= custom_state.active_filter then
			custom_state.active_filter = new_filter
			custom_state.stations = nil
			update_formspec = true
		end
	end
	if update_formspec then
		return get_formspec(custom_state)
	end
end

local function show_formspec(player)

	local custom_state = {
		player_name = assert(player:get_player_name()),
		player_pos = vector.round(player:get_pos()),
		active_filter = 1,
		-- selection_index = nil,
		linevars = {--[[
			{stn = string, linevar = string, linevar_index = int, track = string}...
		]]},
		current_linevar = 1,
	}
	-- ch_core.show_formspec(player_or_player_name, formname, formspec, formspec_callback, custom_state, options)
	ch_core.show_formspec(player, "advtrains_line_automation:editor_dopraven", get_formspec(custom_state), formspec_callback, custom_state, {})
end

advtrains.lines.open_station_editor = show_formspec

def = {
    -- params = "",
    description = "Otevře editor dopraven (stanic, zastávek a odboček)",
    privs = {ch_registered_player = true},
    func = function(player_name, param) show_formspec(minetest.get_player_by_name(player_name)) end,
}
core.register_chatcommand("zastavky", def)
core.register_chatcommand("zastávky", def)


-- Jízdní řád

--[[
	custom_state = {
		pos = vector or nil, -- node position (will use metadata to determine the owner)
		player_name = string, -- player to whom the formspec is to be shown (will use this to determine privs)
		stn = int, -- station selection from stns
		stns = {
			{stn = "", fs = "(vyberte dopravnu)", name_fs = ""},
			{stn = string, fs = string, name_fs = string}...
		}, -- the list of stations to select from; the first must be "", which means no station
		track = int, -- selection of track from "tracks" list
		tracks = {"", string...}, -- the list of tracks to select from; the first must be "", which means all tracks
		linevar = int,
		linevars = {{
			stn = string, -- station from linevar
			linevar = string, -- linevar
			line_fs = string, -- line for formspec
			target_fs = string, -- terminus name for formspec
			track_fs = string, -- track info for formspec ("" if not available)
			status_fs = string, -- line status for formspec (including color column)
			train_name_fs = string, -- train name for formspec ("" if not available)
			linevar_index = int, -- index of the selected station (stn) in linevar_def.stops of this linevar
		}...},
		stop = int,
		stops = {{
			stn = string,
			name_fs = string,
			track_fs = string,
			dep = int,
			wait = int,
			linevar_index = int, -- index of this stop in linevar_def.stops
		}...},
		message = string, -- message for label[] on the bottom; "" to disable
	}
]]

local all_stations_record = {stn = "", fs = F("(vyberte dopravnu)"), name_fs = ""}
local is_visible_mode = assert(advtrains.lines.is_visible_mode)

-- refresh custom_state.stops according to custom_state.linevar
local function jr_refresh_stops(custom_state, stn_to_select)
	if stn_to_select == "" then
		stn_to_select = nil
	end
	local linevar_info = custom_state.linevars[custom_state.linevar]
	local index_to_select = 0
	local result = {}
	if linevar_info ~= nil then
		local linevar_def = advtrains.lines.try_get_linevar_def(linevar_info.linevar, linevar_info.stn)
		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
	if index_to_select == 0 and result[1] ~= nil then
		index_to_select = 1
	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,
				train_name_fs = F(linevar_def.train_name or ""),
				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,
							train_name_fs = F(linevar_def.train_name or ""),
							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