From 61329c11a28ca0d68f95f189d3a66c373d7dddc3 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Fri, 12 Jun 2020 12:32:46 +0200 Subject: Start using path_invalidate_ahead() --- advtrains/lzb.lua | 8 +++++--- advtrains/nodedb.lua | 2 +- advtrains/path.lua | 18 +++++++++++++----- advtrains/signals.lua | 17 ++++++++++++++++- advtrains/trainlogic.lua | 12 ++++++++++++ 5 files changed, 47 insertions(+), 10 deletions(-) (limited to 'advtrains') diff --git a/advtrains/lzb.lua b/advtrains/lzb.lua index c4b3a27..ca92170 100644 --- a/advtrains/lzb.lua +++ b/advtrains/lzb.lua @@ -190,15 +190,16 @@ function advtrains.lzb_invalidate_ahead(train, start_idx) local idx = atfloor(start_idx) local i = 1 while train.lzb.checkpoints[i] do - if train.lzb.checkpoints[i].idx >= idx then + if train.lzb.checkpoints[i].index >= idx then table.remove(train.lzb.checkpoints, i) else i=i+1 end end + train.lzb.trav_index = idx -- re-apply all checkpoints to path_speed train.path_speed = {} - for _,ckp in train.lzb.checkpoints do + for _,ckp in ipairs(train.lzb.checkpoints) do apply_checkpoint_to_path(train, ckp) end end @@ -236,8 +237,9 @@ advtrains.te_register_on_new_path(function(id, train) look_ahead(id, train) end) -advtrains.te_register_on_invalidate_ahead(function(id, train) +advtrains.te_register_on_invalidate_ahead(function(id, train, start_idx) advtrains.lzb_invalidate_ahead(train, start_idx) + look_ahead(id, train) end) advtrains.te_register_on_update(function(id, train) diff --git a/advtrains/nodedb.lua b/advtrains/nodedb.lua index 048d5b4..4539529 100644 --- a/advtrains/nodedb.lua +++ b/advtrains/nodedb.lua @@ -246,7 +246,7 @@ function ndb.update(pos, pnode) local resid = (nid * 4) + (l2b(node.param2 or 0)) ndbset(pos.x, pos.y, pos.z, resid ) --atdebug("nodedb: updating node", pos, "stored nid",nid,"assigned",ndb_nodeids[nid],"resulting cid",resid) - minetest.after(0, advtrains.invalidate_all_paths, pos) + advtrains.invalidate_all_paths_ahead(pos) else --at this position there is no longer a node that needs to be tracked. --atdebug("nodedb: updating node", pos, "cleared") diff --git a/advtrains/path.lua b/advtrains/path.lua index cd7d94a..7226a7b 100644 --- a/advtrains/path.lua +++ b/advtrains/path.lua @@ -141,22 +141,30 @@ end -- Keeps the path intact, but invalidates all path nodes from the specified index (inclusive) -- onwards. This has the advantage that we don't need to recalculate the whole path, and we can do it synchronously. -function advtrains.path_invalidate_ahead(train, start_idx) +function advtrains.path_invalidate_ahead(train, start_idx, ignore_when_passed) local idx = atfloor(start_idx) + --atdebug("Invalidate_ahead:",train.id,"start_index",start_idx,"cur_idx",train.index) - if(idx <= train.index) then + if(idx <= train.index - 0.5) then + if ignore_when_passed then + --atdebug("ignored passed") + return + end advtrains.path_print(train, atwarn) error("Train "+train.id+": Cannot path_invalidate_ahead start_idx="+idx+" as train has already passed!") end - local i = idx + -- leave current node in path, it won't change. What might change is the path onward from here (e.g. switch) + local i = idx + 1 while train.path[i] do advtrains.occ.clear_item(train.id, advtrains.round_vector_floor_y(train.path[i])) + i = i+1 end - train.path_ext_f=idx - 1 - train.path_trk_f=idx - 1 + train.path_ext_f=idx + train.path_trk_f=math.min(idx, train.path_trk_f) + -- callbacks called anyway for current node, because of LZB advtrains.run_callbacks_invahead(train.id, train, idx) end diff --git a/advtrains/signals.lua b/advtrains/signals.lua index 68cbc70..9df2f99 100644 --- a/advtrains/signals.lua +++ b/advtrains/signals.lua @@ -20,6 +20,18 @@ return { } end +local suppasp = { + main = {0, false}, + dst = {false}, + shunt = nil, + proceed_as_main = true, + info = { + call_on = false, + dead_end = false, + w_speed = nil, + } +} + for r,f in pairs({on={as="off", ls="green", als="red"}, off={as="on", ls="red", als="green"}}) do advtrains.trackplacer.register_tracktype("advtrains:retrosignal", "") @@ -81,7 +93,8 @@ for r,f in pairs({on={as="off", ls="green", als="red"}, off={as="on", ls="red", end, get_aspect = function(pos, node) return aspect(r=="on") - end + end, + supported_aspects = suppasp, }, can_dig = can_dig_func, }) @@ -141,6 +154,7 @@ for r,f in pairs({on={as="off", ls="green", als="red"}, off={as="on", ls="red", get_aspect = function(pos, node) return aspect(r=="on") end, + supported_aspects = suppasp, getstate = f.ls, setstate = function(pos, node, newstate) if newstate == f.als then @@ -209,6 +223,7 @@ for r,f in pairs({on={as="off", ls="green", als="red"}, off={as="on", ls="red", get_aspect = function(pos, node) return aspect(r=="on") end, + supported_aspects = suppasp, getstate = f.ls, setstate = function(pos, node, newstate) if newstate == f.als then diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index bb1e1b9..3870ef6 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -1277,6 +1277,18 @@ function advtrains.invalidate_all_paths(pos) advtrains.invalidate_path(id) end end + +-- Calls invalidate_path_ahead on all trains occupying (having paths over) this node +-- Can be called during train step. +function advtrains.invalidate_all_paths_ahead(pos) + local tab = advtrains.occ.get_trains_over(pos) + + for id,index in pairs(tab) do + local train = advtrains.trains[id] + advtrains.path_invalidate_ahead(train, index, true) + end +end + function advtrains.invalidate_path(id) --atdebug("Path invalidate:",id) local v=advtrains.trains[id] -- cgit v1.2.3