From 65f145afaf20b4e64196cfdf0336e72d0cbd1641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20P=C3=A9rez-Cerezo?= Date: Fri, 22 Feb 2019 22:56:11 +0100 Subject: [PATCH] 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. --- main.lua | 134 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 92 insertions(+), 42 deletions(-) diff --git a/main.lua b/main.lua index 6f2abfa..f87a338 100644 --- a/main.lua +++ b/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 - advtrains.ndb.load_data(tbl.ndb) + if not mappath then + advtrains.ndb.load_data(tbl.ndb) + end else error("Incompatible save format!") @@ -112,7 +140,13 @@ file:close() local svgfile = io.open(datapath.."out.svg", "w") -svgfile:write([[ + +if mappath then + mapfile = io.open(mappath, "r") + cont = mapfile:read("*a"):sub(1, -7) -- remove end tag + svgfile:write(cont) +else + svgfile:write([[ @@ -120,13 +154,12 @@ svgfile:write([[ xmlns:xlink="http://www.w3.org/1999/xlink" ]]) -svgfile:write('viewBox="'..(-maxc)..' '..(-maxc)..' '..(2*maxc)..' '..(2*maxc)..'" >') + svgfile:write('viewBox="'..(-maxc)..' '..(-maxc)..' '..(2*maxc)..' '..(2*maxc)..'" >') - -svgfile:write([[ + svgfile:write([[ ]]) - +end if wimg then local wimx = -(wimresx*wimscale/2) local wimy = -(wimresy*wimscale/2) @@ -272,53 +305,70 @@ 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 - - writec("Restart at position "..pts(stpos)) - for connid, conn in ipairs(conns) do - table.insert(bfs_rsp, {pos = stpos, connid = connid, conn = conn}) + while stpos do + + writec("Restart at position "..pts(stpos)) + for connid, conn in ipairs(conns) do + table.insert(bfs_rsp, {pos = stpos, connid = connid, conn = conn}) + end + advtrains.ndb.clear(stpos) + + -- 2. while there are BFS entries + while #bfs_rsp > 0 do + -- make polylines + local current_rsp = bfs_rsp[#bfs_rsp] + bfs_rsp[#bfs_rsp] = nil + --print("Starting polyline at "..pts(current_rsp.pos).."/"..current_rsp.connid) + + + current_polyline = {} + + gen_rsp_polyline(current_rsp) + + polyline_write(current_polyline) + + io.write("Progress ", ndb_nodes_handled, "+", ndb_nodes_notrack, "/", ndb_nodes_total, "=", math.floor(((ndb_nodes_handled+ndb_nodes_notrack)/ndb_nodes_total)*100), "%\r") + end + stpos, conns = advtrains.ndb.mapper_find_starting_point() end - advtrains.ndb.clear(stpos) - - -- 2. while there are BFS entries - while #bfs_rsp > 0 do - -- make polylines - local current_rsp = bfs_rsp[#bfs_rsp] - bfs_rsp[#bfs_rsp] = nil - --print("Starting polyline at "..pts(current_rsp.pos).."/"..current_rsp.connid) - - - current_polyline = {} - - gen_rsp_polyline(current_rsp) - - polyline_write(current_polyline) - - io.write("Progress ", ndb_nodes_handled, "+", ndb_nodes_notrack, "/", ndb_nodes_total, "=", math.floor(((ndb_nodes_handled+ndb_nodes_notrack)/ndb_nodes_total)*100), "%\r") - end - stpos, conns = advtrains.ndb.mapper_find_starting_point() end - -- draw trains trains = 0 -for i,v in pairs(advtrains.trains) do - pos = v.last_pos - color = "green" - if v.velocity == 0 then - color = "orange" +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("") + 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(" "..v.line.."") + end + trains = trains+1 end - svgfile:write("") - if v.line then - svgfile:write(" "..v.line.."") - end - trains = trains+1 end svgfile:write("") 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