diff options
Diffstat (limited to 'advtrains')
-rw-r--r-- | advtrains/occupation.lua | 20 | ||||
-rw-r--r-- | advtrains/trainlogic.lua | 29 |
2 files changed, 32 insertions, 17 deletions
diff --git a/advtrains/occupation.lua b/advtrains/occupation.lua index dd0235a..568f308 100644 --- a/advtrains/occupation.lua +++ b/advtrains/occupation.lua @@ -208,7 +208,7 @@ function o.get_occupations(train, index) return r, pos end -- Gets a mapping of train id's to indexes of trains that stand or drive over --- returns (table with train_id->index), position +-- returns (table with train_id->index) function o.get_trains_at(ppos) local pos = advtrains.round_vector_floor_y(ppos) local t = occget(pos) @@ -226,4 +226,22 @@ function o.get_trains_at(ppos) return r end +-- Gets a mapping of train id's to indexes of trains that have a path +-- generated over this node +-- returns (table with train_id->index) +function o.get_trains_over(ppos) + local pos = advtrains.round_vector_floor_y(ppos) + local t = occget(pos) + if not t then return {} end + local r = {} + local i = 1 + while t[i] do + local train = advtrains.trains[t[i]] + local idx = t[i+1] + r[t[i]] = idx + i = i + 2 + end + return r +end + advtrains.occ = o diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index f26f7da..7147787 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -1037,26 +1037,23 @@ function advtrains.get_train_at_pos(pos) end end + +-- ehm... I never adapted this function to the new path system ?! function advtrains.invalidate_all_paths(pos) - --if a position is given, only invalidate inside a radius to save performance - local inv_radius=50 - atprint("invalidating all paths") - for k,v in pairs(advtrains.trains) do - local exec=true - if pos and v.path and v.index and v.end_index then - --start and end pos of the train - local cmp1=v.path[atround(v.index)] - local cmp2=v.path[atround(v.end_index)] - if vector.distance(pos, cmp1)>inv_radius and vector.distance(pos, cmp2)>inv_radius then - exec=false - end - end - if exec then - advtrains.invalidate_path(k) - end + local tab + if pos then + -- if position given, check occupation system + tab = advtrains.occ.get_trains_over(pos) + else + tab = advtrains.trains + end + + for id, _ in pairs(tab) do + advtrains.invalidate_path(id) end end function advtrains.invalidate_path(id) + atdebug("Path invalidate:",id) local v=advtrains.trains[id] if not v then return end advtrains.path_invalidate(v) |