Added option to add trains to a pre-generated map.
This should save lots of cpu time by removing the higly cpu-intensive map generating steps. Benchmark: Generating map completely: 30.493s Adding trains: 0.157s This benchmark was obtained from the data of the Linuxworks server, which has about 500k nodes and about 600 trains.
This commit is contained in:
parent
61fbbc5909
commit
65f145afaf
58
main.lua
58
main.lua
@ -93,6 +93,32 @@ dofile("track_defs.lua")
|
||||
dofile("nodedb.lua")
|
||||
|
||||
|
||||
function parse_args(argv)
|
||||
local i = 1
|
||||
local no_trains = false
|
||||
local datapath, mappath
|
||||
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
|
||||
else
|
||||
datapath = a
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
return datapath, mappath, no_trains
|
||||
end
|
||||
|
||||
datapath, mappath, no_trains = parse_args(arg)
|
||||
|
||||
-- Load saves
|
||||
local file, err = io.open(datapath.."advtrains", "r")
|
||||
local tbl = minetest.deserialize(file:read("*a"))
|
||||
@ -101,7 +127,9 @@ if type(tbl) ~= "table" then
|
||||
end
|
||||
if tbl.version then
|
||||
advtrains.trains = tbl.trains
|
||||
if not mappath then
|
||||
advtrains.ndb.load_data(tbl.ndb)
|
||||
end
|
||||
|
||||
else
|
||||
error("Incompatible save format!")
|
||||
@ -112,6 +140,12 @@ file:close()
|
||||
|
||||
local svgfile = io.open(datapath.."out.svg", "w")
|
||||
|
||||
|
||||
if mappath then
|
||||
mapfile = io.open(mappath, "r")
|
||||
cont = mapfile:read("*a"):sub(1, -7) -- remove </svg> end tag
|
||||
svgfile:write(cont)
|
||||
else
|
||||
svgfile:write([[
|
||||
<?xml version="1.0" standalone="no" ?>
|
||||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
|
||||
@ -122,11 +156,10 @@ svgfile:write([[
|
||||
|
||||
svgfile:write('viewBox="'..(-maxc)..' '..(-maxc)..' '..(2*maxc)..' '..(2*maxc)..'" >')
|
||||
|
||||
|
||||
svgfile:write([[
|
||||
<circle cx="0" cy="0" r="5" stroke="red" stroke-width="1" />
|
||||
]])
|
||||
|
||||
end
|
||||
if wimg then
|
||||
local wimx = -(wimresx*wimscale/2)
|
||||
local wimy = -(wimresy*wimscale/2)
|
||||
@ -272,8 +305,11 @@ local function polyline_write(pl)
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
-- while there are entries in the nodedb
|
||||
-- 1. find a starting point
|
||||
if not mappath then
|
||||
local stpos, conns = advtrains.ndb.mapper_find_starting_point()
|
||||
while stpos do
|
||||
|
||||
@ -301,24 +337,38 @@ while stpos do
|
||||
end
|
||||
stpos, conns = advtrains.ndb.mapper_find_starting_point()
|
||||
end
|
||||
|
||||
end
|
||||
-- draw trains
|
||||
trains = 0
|
||||
stopped = 0
|
||||
lines = {}
|
||||
running = {}
|
||||
if not no_trains then
|
||||
for i,v in pairs(advtrains.trains) do
|
||||
pos = v.last_pos
|
||||
color = "green"
|
||||
if v.velocity == 0 then
|
||||
color = "orange"
|
||||
stopped = stopped + 1
|
||||
end
|
||||
svgfile:write("<circle cx=\""..pos.x.."\" cy=\""..-pos.z.."\" r=\"3\" stroke=\""..color.."\" stroke-width=\"1\" fill=\"none\" />")
|
||||
if v.line then
|
||||
lines[v.line] = (lines[v.line] or 0) + 1
|
||||
if v.velocity ~= 0 then
|
||||
running[v.line] = (running[v.line] or 0) + 1
|
||||
end
|
||||
svgfile:write(" <text x=\""..(pos.x+5).."\" y=\""..-pos.z.."\" class=\"trainline\">"..v.line.."</text>")
|
||||
end
|
||||
trains = trains+1
|
||||
end
|
||||
end
|
||||
|
||||
svgfile:write("</svg>")
|
||||
svgfile:close()
|
||||
|
||||
print("\nWrote",plcnt,"polylines. Processed", ndb_nodes_handled, "track,",ndb_nodes_notrack, "non-track nodes out of", ndb_nodes_total)
|
||||
print("Drew "..trains.." trains")
|
||||
print("Drew "..trains.." trains. "..stopped.." stopped trains.")
|
||||
print("\n Number of trains moving/total:")
|
||||
for i,v in pairs(lines) do
|
||||
print(i..": "..(running[i] or 0).."/"..v)
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user