add small script to create json files for use with lifomapserver

These json files can then be used to help with train routing through
the network
This commit is contained in:
Gabriel Pérez-Cerezo 2020-07-02 02:04:10 +02:00
parent 49a47330b3
commit 2ba5a52fe2

131
routes.lua Executable file
View File

@ -0,0 +1,131 @@
-- 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 sigpos = sp[1]..","..-sp[3].." "
local routestr = {}
for i,r in pairs(routes) do
local pl = sigpos
local tcbps = {}
-- 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
pl = pl.." "..tcb.x..","..-tcb.z
end
end
end
if #tcbps > 0 then
routestr[#routestr+1] = '{ "name": "'..r.name..'",\n"endpoint": "'..tcbps[#tcbps]..'",\n"ars": '..ars_to_text(r.ars)..' }'
end
color = "blue"
if r.ars and r.ars.default then
color = "green"
end
if pl~="" then
-- svgfile:write("<polyline points=\""..pl.."\" fill=\"none\" stroke=\""..color.."\" />")
end
end
if #routestr > 0 then
tcbstr[#tcbstr+1] = '"'..pos..'": { "type" : "Feature", "geometry": { "type": "Point", "coordinates": [ '..sp[1]..","..sp[3]..']}, "properties" : { "pos": "'..pos..'", "routes" : [\n'..table.concat(routestr,",\n").."]}}"
end
end
jsonfile:write("{"..table.concat(tcbstr,",\n").."}")
jsonfile:close()