t/html; charset=UTF-8 Last-Modified: Tue, 20 Oct 2026 12:24:26 GMT Expires: Fri, 17 Oct 2036 12:24:26 GMT CMakeLists.txt « src - minetest.git - modified minetest for gpcfs purposes
aboutsummaryrefslogtreecommitdiff
path: root/src/CMakeLists.txt
blob: fcdb3ccbc16c81d2c69df0dda8ff9840d9a92b73 (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
project(minetest-delta)
cmake_minimum_required( VERSION 2.6 )

if(RUN_IN_PLACE)
	add_definitions ( -DRUN_IN_PLACE )
endif(RUN_IN_PLACE)

# user-visible option to enable/disable gettext usage
OPTION(ENABLE_GETTEXT "Use GetText for internationalization" ON)

# this is only set to 1 if gettext is enabled _and_ available
set(USE_GETTEXT 0)

find_package(GettextLib)

if(GETTEXT_FOUND AND ENABLE_GETTEXT)
	message(STATUS "gettext include path: ${GETTEXT_INCLUDE_DIR}")
	message(STATUS "gettext msgfmt path: ${GETTEXT_MSGFMT}")
	if(WIN32)
		message(STATUS "gettext library: ${GETTEXT_LIBRARY}")
		message(STATUS "gettext dll: ${GETTEXT_DLL}")
		message(STATUS "gettext iconv dll: ${GETTEXT_ICONV_DLL}")
	endif()
	set(USE_GETTEXT 1)
	message(STATUS "GetText enabled; locales found: ${GETTEXT_AVAILABLE_LOCALES}")
elseif(GETTEXT_FOUND AND NOT ENABLE_GETTEXT)
	MESSAGE(STATUS "GetText found but disabled;")
else(GETTEXT_FOUND AND ENABLE_GETTEXT)
	message(STATUS "GetText disabled")
endif(GETTEXT_FOUND AND ENABLE_GETTEXT)

if(NOT MSVC)
	set(USE_GPROF 0 CACHE BOOL "Use -pg flag for g++")
endif()

# Use cmake_config.h
add_definitions ( -DUSE_CMAKE_CONFIG_H )

if(WIN32)
	# Windows
	if(MSVC) # MSVC Specifics
		# Surpress some useless warnings
		add_definitions ( /D "_CRT_SECURE_NO_DEPRECATE" /W1 )
	else() # Probably MinGW = GCC
		set(PLATFORM_LIBS ws2_32.lib)
	endif()
	# Zlib stuff
	set(ZLIB_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/../../zlib/zlib-1.2.5"
			CACHE PATH "Zlib include directory")
	set(ZLIB_LIBRARIES "${PROJECT_SOURCE_DIR}/../../zlib125dll/dll32/zlibwapi.lib"
			CACHE FILEPATH "Path to zlibwapi.lib")
	set(ZLIB_DLL "${PROJECT_SOURCE_DIR}/../../zlib125dll/dll32/zlibwapi.dll"
			CACHE FILEPATH "Path to zlibwapi.dll (for installation)")
	set(IRRLICHT_SOURCE_DIR "${PROJECT_SOURCE_DIR}/../../irrlicht-1.7.2"
			CACHE PATH "irrlicht dir")
else()
	# Unix probably
	if(BUILD_CLIENT)
		find_package(X11 REQUIRED)
		find_package(OpenGL REQUIRED)
		find_package(JPEG REQUIRED)
		find_package(BZip2 REQUIRED)
		find_package(PNG REQUIRED)
		if(APPLE)
			FIND_LIBRARY(CARBON_LIB Carbon)
			FIND_LIBRARY(COCOA_LIB Cocoa)
			FIND_LIBRARY(IOKIT_LIB IOKit)
			mark_as_advanced(
				CARBON_LIB
				COCOA_LIB
				IOKIT_LIB
			)
			SET(CLIENT_PLATFORM_LIBS ${CLIENT_PLATFORM_LIBS} ${CARBON_LIB} ${COCOA_LIB} ${IOKIT_LIB})
		endif(APPLE)
	endif(BUILD_CLIENT)
	find_package(ZLIB REQUIRED)
	set(PLATFORM_LIBS -lpthread)
	#set(CLIENT_PLATFORM_LIBS -lXxf86vm)
	# This way Xxf86vm is found on OpenBSD too
	find_library(XXF86VM_LIBRARY Xxf86vm)
	set(CLIENT_PLATFORM_LIBS ${CLIENT_PLATFORM_LIBS} ${XXF86VM_LIBRARY})
endif()

find_package(Jthread REQUIRED)
find_package(Sqlite3 REQUIRED)

configure_file(
	"${PROJECT_SOURCE_DIR}/cmake_config.h.in"
	"${PROJECT_BINARY_DIR}/cmake_config.h"
)

set(common_SRCS
	content_sao.cpp
	mapgen.cpp
	content_inventory.cpp
	content_nodemeta.cpp
	content_craft.cpp
	content_mapblock.cpp
	content_mapnode.cpp
	auth.cpp
	collision.cpp
	nodemetadata.cpp
	serverobject.cpp
	noise.cpp
	mineral.cpp
	porting.cpp
	materials.cpp
	defaultsettings.cpp
	mapnode.cpp
	voxel.cpp
	mapblockobject.cpp
	inventory.cpp
	debug.cpp
	serialization.cpp
	light.cpp
	filesys.cpp
	connection.cpp
	environment.cpp
	server.cpp
	servercommand.cpp
	socket.cpp
	mapblock.cpp
	mapsector.cpp
	map.cpp
	player.cpp
	utility.cpp
	test.cpp
	sha1.cpp
	base64.cpp
)

# Client sources
set(minetest_SRCS
	${common_SRCS}
	content_cao.cpp
	mapblock_mesh.cpp
	farmesh.cpp
	keycode.cpp
	clouds.cpp
	clientobject.cpp
	guiMainMenu.cpp
	guiKeyChangeMenu.cpp
	guiMessageMenu.cpp
	guiTextInputMenu.cpp
	guiInventoryMenu.cpp
	guiPauseMenu.cpp
	guiPasswordChange.cpp
	client.cpp
	tile.cpp
	game.cpp
	main.cpp
)

# Server sources
set(minetestserver_SRCS
	${common_SRCS}
	servermain.cpp
)

include_directories(
	${PROJECT_BINARY_DIR}
	${IRRLICHT_INCLUDE_DIR}
	${ZLIB_INCLUDE_DIR}
	${CMAKE_BUILD_TYPE}
	${PNG_INCLUDE_DIR}
	${GETTEXT_INCLUDE_DIR}
	${JTHREAD_INCLUDE_DIR}
	${SQLITE3_INCLUDE_DIR}
)

set(EXECUTABLE_OUTPUT_PATH ../bin)

if(BUILD_CLIENT)
	add_executable(${PROJECT_NAME} ${minetest_SRCS})
	target_link_libraries(
		${PROJECT_NAME}
		${ZLIB_LIBRARIES}
		${IRRLICHT_LIBRARY}
		${OPENGL_LIBRARIES}
		${JPEG_LIBRARIES}
		${BZIP2_LIBRARIES}
		${PNG_LIBRARIES}
		${X11_LIBRARIES}
		${GETTEXT_LIBRARY}
		${PLATFORM_LIBS}
		${CLIENT_PLATFORM_LIBS}
		${JTHREAD_LIBRARY}
		${SQLITE3_LIBRARY}
	)
endif(BUILD_CLIENT)

if(BUILD_SERVER)
	add_executable(${PROJECT_NAME}server ${minetestserver_SRCS})
	target_link_libraries(
		${PROJECT_NAME}server
		${ZLIB_LIBRARIES}
		${PLATFORM_LIBS}
		${JTHREAD_LIBRARY}
		${SQLITE3_LIBRARY}
	)
endif(BUILD_SERVER)

#
# Set some optimizations and tweaks
#

if(MSVC)
	# Visual Studio

	# EHa enables SEH exceptions (used for catching segfaults)
	set(CMAKE_CXX_FLAGS_RELEASE "/EHa /MD /O2 /Ob2 /Oi /Ot /Oy /GL /FD /MT /GS- /arch:SSE /fp:fast /D NDEBUG /D _HAS_ITERATOR_DEBUGGING=0 /TP")
	#set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/LTCG /NODEFAULTLIB:\"libcmtd.lib\" /NODEFAULTLIB:\"libcmt.lib\"")
	set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/LTCG")

	# Debug build doesn't catch exceptions by itself
	# Add some optimizations because otherwise it's VERY slow
	set(CMAKE_CXX_FLAGS_DEBUG "/MDd /Zi /Ob0 /Od /RTC1")
	
	if(BUILD_SERVER)
		set_target_properties(${PROJECT_NAME}server PROPERTIES
				COMPILE_DEFINITIONS "SERVER")
	endif(BUILD_SERVER)

else()
	# Probably GCC
	
	if(WARN_ALL)
		set(RELEASE_WARNING_FLAGS "-Wall")
	else()
		set(RELEASE_WARNING_FLAGS "")
	endif()
	
	if(NOT APPLE)
		set(WARNING_FLAGS "${WARNING_FLAGS} -Wno-unused-but-set-variable")
	endif()
	
	if(APPLE)
		set(CMAKE_OSX_ARCHITECTURES i386 CACHE STRING "do not build for 64-bit" FORCE)
		set(ARCH i386)
	endif()

	set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG ${RELEASE_WARNING_FLAGS} ${WARNING_FLAGS} -O3 -ffast-math -Wall -fomit-frame-pointer -pipe -funroll-loops")
	set(CMAKE_CXX_FLAGS_DEBUG "-g -O1 -Wall ${WARNING_FLAGS}")

	if(USE_GPROF)
		set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -pg")
	endif()
	
	if(BUILD_SERVER)
		set_target_properties(${PROJECT_NAME}server PROPERTIES
				COMPILE_DEFINITIONS "SERVER")
	endif(BUILD_SERVER)

endif()

#MESSAGE(STATUS "CMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}")
#MESSAGE(STATUS "CMAKE_CXX_FLAGS_DEBUG=${CMAKE_CXX_FLAGS_DEBUG}")

#
# Installation
#

# Example configuration file
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/../minetest.conf.example" DESTINATION ${EXAMPLE_CONF_DIR})

if(BUILD_CLIENT)
	install(TARGETS ${PROJECT_NAME} DESTINATION ${BINDIR})

	file(GLOB images "${CMAKE_CURRENT_SOURCE_DIR}/../data/*.png")

	install(FILES ${images} DESTINATION ${DATADIR})

	if(USE_GETTEXT)
		foreach(LOCALE ${GETTEXT_AVAILABLE_LOCALES})
			set_mo_paths(MO_BUILD_PATH MO_DEST_PATH ${LOCALE})
			set(MO_BUILD_PATH "${MO_BUILD_PATH}/${PROJECT_NAME}.mo")
			install(FILES ${MO_BUILD_PATH} DESTINATION ${MO_DEST_PATH})
		endforeach(LOCALE ${GETTEXT_AVAILABLE_LOCALES})
	endif()

	if(WIN32)
		if(DEFINED IRRLICHT_DLL)
			install(FILES ${IRRLICHT_DLL} DESTINATION ${BINDIR})
		endif()
		if(DEFINED ZLIB_DLL)
			install(FILES ${ZLIB_DLL} DESTINATION ${BINDIR})
		endif()
		if(USE_GETTEXT)
			if(DEFINED GETTEXT_DLL)
				install(FILES ${GETTEXT_DLL} DESTINATION ${BINDIR})
			endif()
			if(DEFINED GETTEXT_ICONV_DLL)
				install(FILES ${GETTEXT_ICONV_DLL} DESTINATION ${BINDIR})
			endif()
		endif(USE_GETTEXT)
	endif()
endif(BUILD_CLIENT)

if(BUILD_SERVER)
	install(TARGETS ${PROJECT_NAME}server DESTINATION ${BINDIR})
endif(BUILD_SERVER)

if (USE_GETTEXT)
	set(MO_FILES)

	foreach(LOCALE ${GETTEXT_AVAILABLE_LOCALES})
		set(PO_FILE_PATH "${GETTEXT_PO_PATH}/${LOCALE}/minetest.po")
		set_mo_paths(MO_BUILD_PATH MO_DEST_PATH ${LOCALE})
		set(MO_FILE_PATH "${MO_BUILD_PATH}/${PROJECT_NAME}.mo")

		add_custom_command(OUTPUT ${MO_BUILD_PATH}
			COMMAND ${CMAKE_COMMAND} -E make_directory ${MO_BUILD_PATH}
			COMMENT "mo-update [${LOCALE}]: Creating locale directory.")

		add_custom_command(
			OUTPUT ${MO_FILE_PATH}
			COMMAND ${GETTEXT_MSGFMT} -o ${MO_FILE_PATH} ${PO_FILE_PATH}
			DEPENDS ${MO_BUILD_PATH} ${PO_FILE_PATH}
			WORKING_DIRECTORY "${GETTEXT_PO_PATH}/${LOCALE}"
			COMMENT "mo-update [${LOCALE}]: Creating mo file."
			)

		set(MO_FILES ${MO_FILES} ${MO_FILE_PATH})
	endforeach(LOCALE ${GETTEXT_AVAILABLE_LOCALES})

	add_custom_target(translations ALL COMMENT "mo update" DEPENDS ${MO_FILES})
endif(USE_GETTEXT)

# Subdirectories

if (JTHREAD_FOUND)
else (JTHREAD_FOUND)
	add_subdirectory(jthread)
endif (JTHREAD_FOUND)

if (SQLITE3_FOUND)
else (SQLITE3_FOUND)
add_subdirectory(sqlite)
endif (SQLITE3_FOUND)

#end
'n604' href='#n604'>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 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
local def
local F = minetest.formspec_escape
local ch_core = advtrains.lines.ch_core
--[[
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 rwt = assert(advtrains.lines.rwt)

-- Get current translator
local S = advtrains.lines.translate

-- Singularis-Merge: Emulate certain ch functionality
local function systemovy_kanal(pname, message)
	core.chat_send_player(pname, S("[Station Editor]")..message)
end
local function prihlasovaci_na_zobrazovaci(prihlasovaci)
	return prihlasovaci -- no-op. On CH used to colorize the name to reflect the rank of the player
end

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 = S("By Distance"),
		-- filter = filter_all,
		sorter = sort_by_distance,
	},
	{
		description = S("Station Code (A-Z)"),
		-- filter = filter_all,
		sorter = sort_by_stn,
	},
	{
		description = S("Station Name (A-Z)"),
		-- filter = filter_all,
		sorter = sort_by_name,
	},
	{
		description = S("My Stations (Station Code A-Z)"),
		filter = filter_mine,
		sorter = sort_by_stn,
	},
	{
		description = S("My Stations (Station Name A-Z)"),
		filter = filter_mine,
		sorter = sort_by_name,
	},
	{
		description = S("My Stations (By Distance)"),
		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,"..S("CODE")..","..S("NAME")..",#ffffff,"..S("OWNER")..","..S("DISTANCE")..","..S("INFO").."")
	for _, st in ipairs(stations) do
		local n_tracks = #st.tracks
		table.insert(formspec, ",0,"..F(st.stn)..","..F(st.name)..",#ffffff,"..F(prihlasovaci_na_zobrazovaci(st.owner))..","..
			F(station_distance_s(custom_state.player_pos, st))..", "..S("@1 tracks", n_tracks))
		if n_tracks > 0 then
			table.insert(formspec, "\\, "..S("lines").." " ..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(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;"..S("Create New").."]"..
			"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;"..S("Save Changes").."]")
			if st.tracks[1] == nil then
				table.insert(formspec, "button[15.25,8;3,0.75;smazat;"..S("Delete").."]")
			end
			if custom_state.linevars[1] ~= nil then
				table.insert(formspec, "label[0.5,9.4;"..S("Assign Track").."]"..
					"field[2.75,9.1;1,0.6;"..S("Track")..";;]"..
					"label[3.9,9.4;"..S("Lines").."]"..
					"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;"..S("Assign").."]"..
					"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;;"..S("Positions of tracks:")..";"..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
			systemovy_kanal(custom_state.player_name, S("ERROR: Code must not be empty!"))
			return
		end
		local als = advtrains.lines.stations
		if als[new_stn] ~= nil then
			systemovy_kanal(custom_state.player_name, S("ERROR: Station '@1' already exists!", new_stn))
			return
		end
		als[new_stn] = {name = assert(new_name), owner = assert(new_owner)}
		custom_state.stations = nil
		update_formspec = true
		systemovy_kanal(custom_state.player_name, S("Station successfully created."))
	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
			systemovy_kanal(custom_state.player_name, S("ERROR: No such station!"))
			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
			fields.owner ~= st.owner -- ch_core.jmeno_na_prihlasovaci(fields.owner) ~= st.owner
		if not change_stn and not change_name and not change_owner then
			systemovy_kanal(custom_state.player_name, S("Nothing has changed."))
			return
		end
		local t = advtrains.lines.stations[st.stn]
		if t == nil then
			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
				systemovy_kanal(custom_state.player_name, S("ERROR: Station '@1' already exists!", new_stn))
				return
			end
		end
		if change_owner then
			t.owner = fields.owner -- ch_core.jmeno_na_prihlasovaci(fields.owner)
			systemovy_kanal(custom_state.player_name, S("Owner changed!"))
		end
		if change_name then
			t.name = new_name
			systemovy_kanal(custom_state.player_name, S("Name changed!"))
		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
			systemovy_kanal(custom_state.player_name, S("Code changed, @1 station tracks updated!", count))
		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
			systemovy_kanal(custom_state.player_name, S("ERROR: No stop selected!"))
			return
		end
		if st.tracks[1] ~= nil then
			systemovy_kanal(custom_state.player_name, S("ERROR: There are station tracks assigned, cannot delete the station!"))
			return
		end
		local t = advtrains.lines.stations[st.stn]
		if t == nil then
			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
		systemovy_kanal(custom_state.player_name, S("Station deleted!"))
		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
			systemovy_kanal(custom_state.player_name, S("ERROR: No stop selected!"))
			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
			systemovy_kanal(custom_state.player_name, S("ERROR: No stop selected!"))
			return
		end
		local linevar_to_change = custom_state.linevars[custom_state.current_linevar]
		if linevar_to_change == nil then
			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
			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
			systemovy_kanal(custom_state.player_name, "CHYBA: vnitřní chyba 3!")
			return
		end
		if stop.stn ~= st.stn then
			systemovy_kanal(custom_state.player_name, "CHYBA: vnitřní chyba 4!")
			return
		end
		stop.track = tostring(fields.kolej)
		linevar_to_change.track = stop.track
		systemovy_kanal(custom_state.player_name, S("Track successfully assigned!"))
		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 = S("Open station editor"),
    privs = {railway_operator = true},
    func = function(player_name, param) show_formspec(minetest.get_player_by_name(player_name)) end,
}
core.register_chatcommand("station_editor", 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 = S("(select station)"), 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,"..S("in operation")
			else
				status_fs = "#999999,"..S("unknown state")
			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,"..S("in operation")
							else
								status_fs = "#999999,"..S("unknown state")
							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
		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.now()
	local prediction = advtrains.lines.predict_station_departures(linevar_def, assert(stop_info.linevar_index), rwtime)
	if #prediction == 0 then
		custom_state.message = S("No departures for the selected line found in the near future")
		return
	end
	local deps = {}
	for i, pred in ipairs(prediction) do
		deps[i] = tostring(rwt.diff(rwtime, pred.dep))
	end
	custom_state.message = S("next departures of the selected line in: ")..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 not custom_state.force_unprivileged then
		if core.check_player_privs(custom_state.player_name, {protection_bypass=true}) then -- TODO make a lines admin privilege
			access_level = "admin"
		elseif custom_state.player_name == node_owner or custom_state.player_name == stn_owner then
			access_level = "owner"
		end
	end

	if node_owner ~= nil then
		if access_level ~= "player" then
			-- admin or owner:
			table.insert(formspec, "label[0.5,0.6;"..S("Timetables").."]"..
				"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, S("(all platforms)"))
				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;"..S("Timetables (all lines)").."]")
			else
				local track = custom_state.tracks[custom_state.track]
				if track ~= "" then
					track = F(" ["..track.."]")
				end
				table.insert(formspec, "label[0.5,0.6;"..S("Timetables: ")..stn_info.name_fs..track.."]")
			end
		end
		if access_level ~= "admin" then
			-- player/owner
			table.insert(formspec, "label[0.5,1.65;"..S("Owner: "))
			table.insert(formspec, prihlasovaci_na_zobrazovaci(node_owner))
			if stn_owner ~= nil then
				table.insert(formspec, " | "..S("Station Operator: "))
				table.insert(formspec, prihlasovaci_na_zobrazovaci(stn_owner))
			end
			table.insert(formspec, "]")
		else
			-- admin only
			table.insert(formspec, "label[0.5,1.65;"..S("Owner: ").."]"..
				"field[2.75,1.25;5,0.75;owner;;")
			table.insert(formspec, prihlasovaci_na_zobrazovaci(node_owner))
			table.insert(formspec, "]button[8,1.25;3,0.75;setowner;"..S("Set").."]")
			if stn_owner ~= nil then
				table.insert(formspec, "label[11.25,1.65;"..S("Station Operator: ")..prihlasovaci_na_zobrazovaci(stn_owner).."]")
			end
		end
	else
		table.insert(formspec, "label[0.5,0.6;"..S("Timetable for current station (all lines)").."]")
	end

	table.insert(formspec, "tablecolumns[text,align=right,tooltip=Line;text,width=12,tooltip=Terminus;text,tooltip=Track;color,span=1;text,tooltip=Status;color,span=1;text,tooltip=Train Name]"..
		"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..",#cccccc,"..r.train_name_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;"..S("Update next departures...").."]")
	return table.concat(formspec)
end

local function jr_formspec_callback(custom_state, player, formname, fields)
	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 and custom_state.pos ~= nil and is_jr_node_name(core.get_node(custom_state.pos).name) then
		local meta = core.get_meta(custom_state.pos)
		local jm = fields.owner -- ch_core.jmeno_na_existujici_prihlasovaci(fields.owner)
		if jm ~= nil then
			meta:set_string("owner", jm)
		else
			core.chat_send_player(custom_state.player_name, "*** Postava '"..fields.owner.."' neexistuje!")
		end
		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

function advtrains.lines.show_jr_formspec(player, pos, stn, track, linevar, stop_stn, force_unprivileged)
	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 = "",
		force_unprivileged = force_unprivileged,
	}
	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