routes.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. -- Small script to generate json files for lifomapserver to use for routing
  2. advtrains = {}
  3. minetest = {}
  4. core = minetest
  5. math.hypot = function(a,b) return math.sqrt(a*a + b*b) end
  6. function attrans(str) return str end
  7. dofile("vector.lua")
  8. local serialize = dofile("serialize.lua")
  9. dofile("helpers.lua")
  10. function parse_args(argv)
  11. local i = 1
  12. local no_trains = false
  13. local datapath, mappath, worldimage
  14. while i <= #argv do
  15. local a = argv[i]
  16. if (a == "-m") or (a == "--map-file") then
  17. -- only draw trains – requires specifying an already drawn file
  18. i = i+1
  19. if not argv[i] then
  20. error(("missing filename after `%s'"):format(a))
  21. end
  22. mappath = argv[i]
  23. elseif (a == "-t") or (a == "--no-trains") then
  24. -- do not draw trains
  25. no_trains = true
  26. elseif (a == "-w") or (a == "--world-image") then
  27. -- overlay over world image
  28. i = i+1
  29. if not argv[i] then
  30. error(("missing filename after `%s'"):format(a))
  31. end
  32. worldimage = argv[i]
  33. else
  34. datapath = a
  35. end
  36. i = i + 1
  37. end
  38. return datapath, mappath, no_trains, worldimage
  39. end
  40. datapath, mappath, no_trains, worldimage = parse_args(arg)
  41. function ars_to_text(arstab)
  42. if not arstab then
  43. return "{}"
  44. end
  45. local txt = {}
  46. local ln = {}
  47. local rc = {}
  48. for i, arsent in ipairs(arstab) do
  49. local n = ""
  50. if arsent.n then
  51. n = "!"
  52. end
  53. if arsent.ln then
  54. ln[#ln+1] = '"'..n..arsent.ln..'"'
  55. elseif arsent.rc then
  56. rc[#ln+1] = '"'..n..arsent.rc..'"'
  57. elseif arsent.c then
  58. txt[#txt+1] = "#"..arsent.c
  59. end
  60. end
  61. return '{"LN": ['..table.concat(ln,',')..'], "RC" : ['..table.concat(rc,",")..']'.. ',"default": '..(arstab.default and "true" or "false").."}\n"
  62. end
  63. --local file = io.open(datapath.."advtrains_interlocking_tcbs", "r")
  64. --local tbl = minetest.deserialize(file:read("*a"))
  65. local tbl = serialize.read_from_file(datapath.."advtrains_interlocking")
  66. advtrains.tcbs = tbl.tcbs
  67. --file:close()
  68. local jsonfile = io.open(datapath.."signals.json", "w")
  69. tcbstr = {}
  70. for k,v in pairs(advtrains.tcbs) do
  71. local routes = v[1].routes or {}
  72. if v[2].routes then
  73. for a,b in ipairs(v[2].routes) do
  74. routes[#routes+1] = b
  75. end
  76. end
  77. local pos = k:sub(2,-2)
  78. local sp = {}
  79. for j in (pos..","):gmatch("([^,]+),") do
  80. sp[#sp+1] = j
  81. end
  82. local tcbsidestr = {}
  83. local hasroutes = false
  84. for s=1,2 do
  85. local routestr = {}
  86. local side = v[s]
  87. local routes = side.routes or {}
  88. local signame = side.signal_name or ""
  89. local auto = nil
  90. for i,r in pairs(routes) do
  91. hasroutes = true
  92. local tcbps = {}
  93. local tcbs = {}
  94. -- svgfile:write("<circle cx=\""..sp[1].."\" cy=\""..-sp[3].."\" r=\"3\" stroke=\"".."purple".."\" stroke-width=\"1\" fill=\"none\" />")
  95. for ind,ps in ipairs(r) do
  96. if ps.next then
  97. local tcb = ps.next.p
  98. -- print(minetest.serialize(ps.next))
  99. if tcb and tcb.x then
  100. tcbps[#tcbps+1] = tcb.x..","..tcb.y..","..tcb.z
  101. tcbs[#tcbs+1] = ps.next.s
  102. end
  103. end
  104. end
  105. local auto = '"auto": false'
  106. if side.routeset and i == side.routeset and side.route_auto then
  107. auto = '"auto": true'
  108. end
  109. if #tcbps > 0 then
  110. routestr[#routestr+1] = '{ "name": "'..r.name..'",\n"endpoint": "'..tcbps[#tcbps]..'",\n"ars": '..ars_to_text(r.ars)..","..auto..', "endpoint_side": '..tcbs[#tcbs]..' }'
  111. end
  112. end
  113. tcbsidestr[#tcbsidestr+1] = '{ "routes": [ '..table.concat(routestr, ",\n")..' ], "signal_name": "'..signame..'"}'
  114. end
  115. if hasroutes then
  116. tcbstr[#tcbstr+1] = '"'..pos..'": { "type" : "Feature", "geometry": { "type": "Point", "coordinates": [ '..sp[1]..","..sp[3]..']}, "properties" : { "pos": "'..pos..'", "sides" : [\n'..table.concat(tcbsidestr,",\n").."]}}"
  117. end
  118. end
  119. jsonfile:write("{"..table.concat(tcbstr,",\n").."}")
  120. jsonfile:close()