From 5c7f77725394d47ce0b907aabf885ea207a507bd Mon Sep 17 00:00:00 2001 From: autocommitter Date: Tue, 15 Oct 2024 22:37:32 +0200 Subject: State at 2024-10-15 --- m4/init_code.lua | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 m4/init_code.lua (limited to 'm4/init_code.lua') diff --git a/m4/init_code.lua b/m4/init_code.lua new file mode 100644 index 0000000..8133311 --- /dev/null +++ b/m4/init_code.lua @@ -0,0 +1,96 @@ +--env-m4.lua +if S.trains == nil then S.trains = {} end +if S.d == nil then S.d = {} end +if S.datetime == nil then S.datetime = "" end +if S.stop_display == nil then S.stop_display = false end + +F.print = function (str) if F.debug then print("".. (str or "nil") ) end end +F.isempty = function (s) return s == nil or s == "" end +F.get_rc_safe = function() return get_rc() or "" end +F.get_line_safe = function() return get_line() or "" end +F.get_train_length_safe = function() return train_length() or 0 end +F.avg = function(t) + local sum = 0 + local count = 0 + for k,v in pairs(t) do + if type(v) == "number" then + sum = sum + v + count = count + 1 + end + end + return (sum / count) +end + +if event.init then + F.debug = true + F.printAllTrainsInfo = true + F.max_displays = 4 + F.print("Initialized") +end + +F.does_train_have_rc = function(wanted_rc) + local rc = F.get_rc_safe() + if rc:match(wanted_rc) then return true end +end + +--old splitter and cpl for demo +function F.cpllooparound(ln) + if not (event.train) then + return + end + if not (F.get_line_safe() == ln) then return end + + if "ENGINE" == F.get_rc_safe() then + atc_send("CplS0WRSM") + set_rc("") + return + end + + split_at_index(F.get_train_length_safe()-1,"A0S0") --Stopping the wagons is handled here by SO + set_rc("ENGINE") + atc_send("A1") + +end + +--new ones +function F.split_looparound(ln) + if not (event.train) then return end + if not (F.get_line_safe() == ln) then return end + + split_at_index(F.get_train_length_safe()-1,"A0S0OC") --Stopping the wagons is handled here by SO + set_rc("ENGINE") + atc_send("A1") + +end + +function F.split_looparound_left(ln) + if not (event.train) then return end + if not (F.get_line_safe() == ln) then return end + + split_at_index(F.get_train_length_safe()-1,"A0S0OL") --Stopping the wagons is handled here by SO + set_rc("ENGINE") + atc_send("A1") + +end +function F.split_looparound_right(ln) + if not (event.train) then return end + if not (F.get_line_safe() == ln) then return end + + split_at_index(F.get_train_length_safe()-1,"A0S0OR") --Stopping the wagons is handled here by SO + set_rc("ENGINE") + atc_send("A1") + +end + +function F.cpl_looparound(ln) + if not (event.train) then return end + if not (F.get_line_safe() == ln) then return end + + if "ENGINE" == F.get_rc_safe() then + atc_send("CplS0WD2RS4") + set_rc("") + return + end + +end +--end of new cpl functions -- cgit v1.2.3