From 8c55442076c9d5cfed89bac3f4acd22ea7948dd3 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Wed, 6 Nov 2019 22:07:10 +0100 Subject: Add "discard all" function to scheduler events and use it to clear schedules in luaatc rails --- advtrains_line_automation/scheduler.lua | 11 +++++++++++ advtrains_luaautomation/active_common.lua | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/advtrains_line_automation/scheduler.lua b/advtrains_line_automation/scheduler.lua index b641597..3d8778a 100644 --- a/advtrains_line_automation/scheduler.lua +++ b/advtrains_line_automation/scheduler.lua @@ -116,4 +116,15 @@ function sched.enqueue_in(rwtime, handler, evtdata, unitid, unitlim) sched.enqueue(ctime + rwtime, handler, evtdata, unitid, unitlim) end +-- Discards all schedules for unit "unitid" (removes them from the queue) +function sched.discard_all(unitid) + for i=1,#queue do + if queue[i].u == unitid then + table.remove(queue,i) + i=i-1 + end + end + units_cnt[unitid] = 0 +end + ln.sched = sched diff --git a/advtrains_luaautomation/active_common.lua b/advtrains_luaautomation/active_common.lua index 8d0975f..3dc018d 100644 --- a/advtrains_luaautomation/active_common.lua +++ b/advtrains_luaautomation/active_common.lua @@ -71,7 +71,12 @@ function ac.on_receive_fields(pos, formname, fields, player) nodetbl.code=fields.code end if fields.save then + -- reset certain things nodetbl.err=nil + if advtrains.lines and advtrains.lines.sched then + -- discard all schedules for this node + advtrains.lines.sched.discard_all(advtrains.encode_pos(pos)) + end end if fields.cle then nodetbl.data={} -- cgit v1.2.3