From 3ce98cae9aa588fba0be1e86fc81e7ca6040bd9d Mon Sep 17 00:00:00 2001 From: orwell96 Date: Tue, 15 Jan 2019 17:23:03 +0100 Subject: Actual working ARS --- advtrains_interlocking/ars.lua | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'advtrains_interlocking/ars.lua') diff --git a/advtrains_interlocking/ars.lua b/advtrains_interlocking/ars.lua index db67a93..80ed638 100644 --- a/advtrains_interlocking/ars.lua +++ b/advtrains_interlocking/ars.lua @@ -20,3 +20,43 @@ {default=true} -> * See also route_ui.lua ]] + +local il = advtrains.interlocking + + +local function find_rtematch(routes, train) + local default + local line = train.line + local routingcode + for rteid, route in ipairs(routes) do + if route.ars then + if route.ars.default then + default = rteid + else + for arskey, arsent in ipairs(route.ars) do + if arsent.ln and line and arsent.ln == line then + return rteid + elseif arsent.rc and routingcode and string.match(" "..routingcode.." ", " "..arsent.rc.." ", nil, true) then + return rteid + end + end + end + end + end + return default +end + +function advtrains.interlocking.ars_check(sigd, train) + local tcbs = il.db.get_tcbs(sigd) + if not tcbs or not tcbs.routes then return end + + if tcbs.routeset then + -- ARS is not in effect when a route is already set + return + end + + local rteid = find_rtematch(tcbs.routes, train) + if rteid then + il.route.update_route(sigd, tcbs, rteid, nil) + end +end -- cgit v1.2.3