summaryrefslogtreecommitdiff
path: root/routes.lua
blob: 04b96fed0c506b7290a434179cfc085ccde036e1 (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
-- Small script to generate json files for lifomapserver to use for routing

advtrains = {}
minetest = {}
core = minetest

math.hypot = function(a,b) return math.sqrt(a*a + b*b) end
function attrans(str) return str end

dofile("vector.lua")
dofile("serialize.lua")
dofile("helpers.lua")


function parse_args(argv) 
	local i = 1
	local no_trains = false
	local datapath, mappath, worldimage
	while i <= #argv do
		local a = argv[i]
		if (a == "-m") or (a == "--map-file") then
			-- only draw trains – requires specifying an already drawn file
			i = i+1
			if not argv[i] then
				error(("missing filename after `%s'"):format(a))
			end
			mappath = argv[i]
		elseif (a == "-t") or (a == "--no-trains") then
			-- do not draw trains
			no_trains = true
		elseif (a == "-w") or (a == "--world-image") then
			-- overlay over world image
			i = i+1
			if not argv[i] then
				error(("missing filename after `%s'"):format(a))
			end
			worldimage = argv[i]
		else
			datapath = a
		end
		
		i = i + 1
	end
	return datapath, mappath, no_trains, worldimage
end

datapath, mappath, no_trains, worldimage = parse_args(arg)

function ars_to_text(arstab)
	if not arstab then
		return "{}"
	end
	
	local txt = {}
	local ln = {}
	local rc = {}
	for i, arsent in ipairs(arstab) do
		local n = ""
		if arsent.n then
			n = "!"
		end
		if arsent.ln then
			ln[#ln+1] = '"'..n..arsent.ln..'"'
		elseif arsent.rc then
			rc[#ln+1] = '"'..n..arsent.rc..'"'
		elseif arsent.c then
			txt[#txt+1] = "#"..arsent.c
		end
	end

	return '{"LN": ['..table.concat(ln,',')..'], "RC" : ['..table.concat(rc,",")..']'.. ',"default": '..(arstab.default and "true" or "false").."}\n"
end


local file = io.open(datapath.."advtrains_interlocking_tcbs", "r")
local tbl = minetest.deserialize(file:read("*a"))
advtrains.tcbs = tbl
file:close()

local jsonfile = io.open(datapath.."signals.json", "w")


tcbstr = {}
for k,v in pairs(advtrains.tcbs) do
	local routes = v[1].routes or {}
	if v[2].routes then
		for a,b in ipairs(v[2].routes) do
			routes[#routes+1] = b
		end
	end
	local pos = k:sub(2,-2)
	local sp = {}
	for j in (pos..","):gmatch("([^,]+),") do
		sp[#sp+1] = j
	end
		
	local tcbsidestr = {}
	local hasroutes = false
	for s=1,2 do
		local routestr = {}
		local side = v[s]
		local routes = side.routes or {}
		local signame = side.signal_name or ""
		local auto = nil

		for i,r in pairs(routes) do
			hasroutes = true

			local tcbps = {}
			local tcbs = {}
			--		svgfile:write("<circle cx=\""..sp[1].."\" cy=\""..-sp[3].."\" r=\"3\" stroke=\"".."purple".."\" stroke-width=\"1\" fill=\"none\" />")
			for ind,ps in ipairs(r) do
				if ps.next then
					local tcb = ps.next.p
					--				print(minetest.serialize(ps.next))
					if tcb and tcb.x then
						tcbps[#tcbps+1] = tcb.x..","..tcb.y..","..tcb.z
						tcbs[#tcbs+1] = ps.next.s
					end
				end
			end
			local auto = '"auto": false'
			if side.routeset and i == side.routeset and side.route_auto then
				auto = '"auto": true'
			end
			if #tcbps > 0 then
				routestr[#routestr+1] = '{ "name": "'..r.name..'",\n"endpoint": "'..tcbps[#tcbps]..'",\n"ars": '..ars_to_text(r.ars)..","..auto..', "endpoint_side": '..tcbs[#tcbs]..' }'
			end
		end
		tcbsidestr[#tcbsidestr+1] = '{ "routes": [ '..table.concat(routestr, ",\n")..' ], "signal_name": "'..signame..'"}'
	end
	if hasroutes then
		tcbstr[#tcbstr+1] = '"'..pos..'": { "type" : "Feature", "geometry": { "type": "Point", "coordinates": [ '..sp[1]..","..sp[3]..']}, "properties" : { "pos": "'..pos..'", "sides" : [\n'..table.concat(tcbsidestr,",\n").."]}}"
	end
end

jsonfile:write("{"..table.concat(tcbstr,",\n").."}")
jsonfile:close()