From c9e1df06b462e513057ac3e0e7489c74b56e2af8 Mon Sep 17 00:00:00 2001 From: autocommitter Date: Mon, 22 Apr 2024 15:02:54 +0200 Subject: State at 2023-11-07 --- Crossroads/init_code.lua | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'Crossroads/init_code.lua') diff --git a/Crossroads/init_code.lua b/Crossroads/init_code.lua index 67c2544..067c474 100644 --- a/Crossroads/init_code.lua +++ b/Crossroads/init_code.lua @@ -22,6 +22,52 @@ function F.error(errorcode) error(F.errlist[errorcode]) end +-- Level crossing handling +if not S.crossings then S.crossings = {} end + +local crossing_light_count = { + NRG_1 = 2, + NRG_2 = 4, + NRG_3 = 3, + NRG_4 = 2, + NRG_5 = 4, +} + +local function update_light(loc, k, v) + local t = S.crossings[loc] + if not t then + t = {} + S.crossings[loc] = t + end + t[k] = v + local val = "on" + if next(t) == nil then + val = "off" + end + local pfx = loc .. "_" + local count = crossing_light_count[loc] + if not count then return end + for i = 1, count do + setstate(pfx .. i, val) + end +end + +function F.x_on(t) + if event.type ~= "train" then return end + if not atc_arrow then return end + for loc, k in pairs(t) do + update_light(loc, k, true) + end +end + +function F.x_off(t) + if event.type ~= "train" then return end + if not atc_arrow then return end + for loc, k in pairs(t) do + update_light(loc, k, nil) + end +end + --[[ F.stnbasic - Basis for all station functions: (By default, nil is defined as false or "no changes") stn = Station Code -- cgit v1.2.3