UTF-8 Last-Modified: Mon, 30 Nov 2026 05:31:55 GMT Expires: Thu, 27 Nov 2036 05:31:55 GMT Properly handle repeater signals - advtrains.git - Advtrains mod for minetest.
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorY. Wang <yw05@forksworld.de>2022-10-24 13:51:03 +0200
committerY. Wang <yw05@forksworld.de>2023-03-23 20:06:02 +0100
commit30a0f862488a0cb67f9a8e2aecaf17797ad44a93 (patch)
tree3da8bae4f0bd0deefe471818b031e91b3bdc08a3
parent34405b8431b7c758988b56cc09d21d6de74b727b (diff)
Properly handle repeater signals
-rw-r--r--advtrains_interlocking/signal_aspect_accessors.lua41
-rw-r--r--advtrains_interlocking/signal_aspect_ui.lua2
-rw-r--r--advtrains_interlocking/signal_aspects.lua40
-rw-r--r--advtrains_interlocking/spec/basic_signalling_spec.lua2
-rwxr-xr-xadvtrains_interlocking/tcb_ts_ui.lua28
-rw-r--r--advtrains_signals_japan/init.lua1
6 files changed, 77 insertions, 37 deletions
diff --git a/advtrains_interlocking/signal_aspect_accessors.lua b/advtrains_interlocking/signal_aspect_accessors.lua
index a1cbd4e..060f923 100644
--- a/advtrains_interlocking/signal_aspect_accessors.lua
+++ b/advtrains_interlocking/signal_aspect_accessors.lua
@@ -4,6 +4,27 @@ local I = advtrains.interlocking
local N = advtrains.ndb
local pts = advtrains.roundfloorpts
+local signal_aspect_metatable = {
+ __tostring = function(asp)
+ local st = {}
+ if asp.type2group and asp.type2name then
+ table.insert(st, string.format("%q in group %q", asp.type2name, asp.type2group))
+ end
+ if asp.main then
+ table.insert(st, string.format("current %d", asp.main))
+ end
+ if asp.main ~= 0 then
+ if asp.dst then
+ table.insert(st, string.format("next %d", asp.dst))
+ end
+ if asp.proceed_as_main then
+ table.insert(st, "proceed as main")
+ end
+ end
+ return string.format("[%s]", table.concat(st, ", "))
+ end,
+}
+
local get_aspect
local supposed_aspects = {}
@@ -11,6 +32,9 @@ local supposed_aspects = {}
function I.load_supposed_aspects(tbl)
if tbl then
supposed_aspects = tbl
+ for _, v in pairs(tbl) do
+ setmetatable(v, signal_aspect_metatable)
+ end
end
end
@@ -41,11 +65,14 @@ end
local function adjust_aspect(pos, asp)
asp = table.copy(I.signal_convert_aspect_if_necessary(asp))
+ setmetatable(asp, signal_aspect_metatable)
local mainpos = D.get_main(pos)
local nxtasp
- if asp.main ~= 0 and mainpos then
+ if mainpos then
nxtasp = get_aspect(mainpos)
+ end
+ if asp.main ~= 0 and mainpos then
asp.dst = nxtasp.main
else
asp.dst = nil
@@ -59,7 +86,10 @@ local function adjust_aspect(pos, asp)
if stype == 2 then
local group = suppasp.group
local name
- if asp.main ~= 0 and nxtasp and nxtasp.type2group == group and nxtasp.type2name then
+ if suppasp.dst_shift and nxtasp then
+ asp.main = nil
+ name = A.type1_to_type2main(nxtasp, group, suppasp.dst_shift)
+ elseif asp.main ~= 0 and nxtasp and nxtasp.type2group == group and nxtasp.type2name then
name = A.get_type2_dst(group, nxtasp.type2name)
else
name = A.type1_to_type2main(asp, group)
@@ -79,7 +109,7 @@ local function get_real_aspect(pos)
local asp = ndef.advtrains.get_aspect(pos, node) or I.DANGER
local suppasp = get_supported_aspects(pos)
if suppasp.type == 2 then
- asp = A.type2main_to_type1(suppasp.group, asp)
+ asp = A.type2_to_type1(suppasp, asp)
end
return adjust_aspect(pos, asp)
end
@@ -108,11 +138,6 @@ local function set_aspect(pos, asp, skipdst)
if (not skipdst) and aspect_changed then
D.update_main(pos)
end
- --[[
- local dbgmsg = string.format("[%s]set_aspect(%s,%s,%s)", os.clock(), minetest.pos_to_string(pos), minetest.serialize(asp), tostring(skipdst))
- dbgmsg = debug.traceback(dbgmsg, 2)
- minetest.chat_send_all(dbgmsg)
- --]]
end
end
diff --git a/advtrains_interlocking/signal_aspect_ui.lua b/advtrains_interlocking/signal_aspect_ui.lua
index 30b5165..ccedb01 100644
--- a/advtrains_interlocking/signal_aspect_ui.lua
+++ b/advtrains_interlocking/signal_aspect_ui.lua
@@ -219,7 +219,7 @@ local function get_aspect_from_formspec_t1(suppasp, fields)
end
local function get_aspect_from_formspec_t2(suppasp, fields)
- local asp = advtrains.interlocking.aspects.type2main_to_type1(suppasp.group, tonumber(fields.asp))
+ local asp = advtrains.interlocking.aspects.type2_to_type1(suppasp, tonumber(fields.asp))
return asp
end
diff --git a/advtrains_interlocking/signal_aspects.lua b/advtrains_interlocking/signal_aspects.lua
index 5c4948b..c381fd2 100644
--- a/advtrains_interlocking/signal_aspects.lua
+++ b/advtrains_interlocking/signal_aspects.lua
@@ -67,7 +67,9 @@ local function get_type2_dst(group, name)
return def.main[math.max(1, aspidx-1)].name
end
-local function type2main_to_type1(name, asp)
+local function type2_to_type1(suppasp, asp)
+ local name = suppasp.group
+ local shift = suppasp.dst_shift
local def = type2defs[name]
if not def then
return nil
@@ -78,18 +80,26 @@ local function type2main_to_type1(name, asp)
else
aspidx = def.main[asp] or 2
end
- local asptbl = def.main[aspidx]
+ local realidx = math.min(#def.main, aspidx+(shift or 0))
+ local asptbl = def.main[realidx]
if not asptbl then
return nil
end
if type(asp) == "number" then
asp = asptbl.name
end
- local dst = def.main[math.min(#def.main, aspidx+1)].main
+ local main, shunt, dst
+ if shift then
+ dst = asptbl.main
+ else
+ main = asptbl.main
+ shunt = asptbl.shunt
+ dst = def.main[math.min(#def.main, aspidx+1)].main
+ end
local t = {
- main = asptbl.main,
- shunt = asptbl.shunt,
+ main = main,
+ shunt = shunt,
proceed_as_main = asptbl.proceed_as_main,
type2name = asp,
type2group = name,
@@ -101,31 +111,28 @@ local function type2main_to_type1(name, asp)
return t
end
-local function type1_to_type2main(asp, group)
+local function type1_to_type2main(asp, group, shift)
local def = type2defs[group]
if not def then
return nil
end
- if group == asp.type2group and def.main[asp.type2name] then
- return asp.type2name
- end
local t_main = def.main
local idx
- if not asp.main or asp.main == -1 then
+ if group == asp.type2group and t_main[asp.type2name] then
+ idx = t_main[asp.type2name]
+ elseif not asp.main or asp.main == -1 then
idx = 1
elseif asp.main == 0 then
idx = #t_main
else
- idx = math.max(#t_main-1, 1)
+ idx = #t_main-1
end
- return t_main[idx].name
+ return t_main[math.max(1, idx-(shift or 0))].name
end
local function equalp(asp1, asp2)
if asp1 == asp2 then -- same reference
return true
- elseif asp1.type2group and asp1.type2group == asp2.type2group then -- type2 with the same group
- return asp1.type2name == asp2.type2name
else
for _, k in pairs {"main", "shunt", "dst"} do
if asp1[k] ~= asp2[k] then
@@ -133,6 +140,9 @@ local function equalp(asp1, asp2)
end
end
end
+ if asp1.type2group and asp1.type2group == asp2.type2group then
+ return asp1.type2name == asp2.type2name
+ end
return true
end
@@ -144,7 +154,7 @@ return {
register_type2 = register_type2,
get_type2_definition = get_type2_definition,
get_type2_dst = get_type2_dst,
- type2main_to_type1 = type2main_to_type1,
+ type2_to_type1 = type2_to_type1,
type1_to_type2main = type1_to_type2main,
equalp = equalp,
not_equalp = not_equalp,
diff --git a/advtrains_interlocking/spec/basic_signalling_spec.lua b/advtrains_interlocking/spec/basic_signalling_spec.lua
index 0b79972..720b274 100644
--- a/advtrains_interlocking/spec/basic_signalling_spec.lua
+++ b/advtrains_interlocking/spec/basic_signalling_spec.lua
@@ -44,7 +44,7 @@ world.layout {
describe("API for supposed signal aspects", function()
it("should load and save data properly", function()
- local tbl = {_foo = true}
+ local tbl = {_foo = {}}
I.load_supposed_aspects(tbl)
assert.same(tbl, I.save_supposed_aspects())
end)
diff --git a/advtrains_interlocking/tcb_ts_ui.lua b/advtrains_interlocking/tcb_ts_ui.lua
index 9f88296..3898d73 100755
--- a/advtrains_interlocking/tcb_ts_ui.lua
+++ b/advtrains_interlocking/tcb_ts_ui.lua
@@ -198,20 +198,24 @@ minetest.register_on_punchnode(function(pos, node, player, pointed_thing)
if is_signal then
local ndef = minetest.registered_nodes[node.name]
if ndef and ndef.advtrains and ndef.advtrains.set_aspect then
- local tcbs = ildb.get_tcbs(sigd)
- if tcbs then
- tcbs.signal = pos
- if not tcbs.signal_name then
- tcbs.signal_name = "Signal at "..minetest.pos_to_string(sigd.p)
+ if ndef.advtrains.supported_aspects and not ndef.advtrains.supported_aspects.dst_shift then
+ local tcbs = ildb.get_tcbs(sigd)
+ if tcbs then
+ tcbs.signal = pos
+ if not tcbs.signal_name then
+ tcbs.signal_name = "Signal at "..minetest.pos_to_string(sigd.p)
+ end
+ if not tcbs.routes then
+ tcbs.routes = {}
+ end
+ ildb.set_sigd_for_signal(pos, sigd)
+ minetest.chat_send_player(pname, "Configuring TCB: Successfully assigned signal.")
+ advtrains.interlocking.show_ip_form(pos, pname, true)
+ else
+ minetest.chat_send_player(pname, "Configuring TCB: Internal error, TCBS doesn't exist. Aborted.")
end
- if not tcbs.routes then
- tcbs.routes = {}
- end
- ildb.set_sigd_for_signal(pos, sigd)
- minetest.chat_send_player(pname, "Configuring TCB: Successfully assigned signal.")
- advtrains.interlocking.show_ip_form(pos, pname, true)
else
- minetest.chat_send_player(pname, "Configuring TCB: Internal error, TCBS doesn't exist. Aborted.")
+ minetest.chat_send_player(pname, "Configuring TCB: Cannot use distant signal. Aborted.")
end
else
minetest.chat_send_player(pname, "Configuring TCB: Cannot use static signals for routesetting. Aborted.")
diff --git a/advtrains_signals_japan/init.lua b/advtrains_signals_japan/init.lua
index 2062a21..59640b1 100644
--- a/advtrains_signals_japan/init.lua
+++ b/advtrains_signals_japan/init.lua
@@ -401,6 +401,7 @@ for _, rtab in ipairs {
supported_aspects = {
type = 2,
group = siginfo.typename,
+ dst_shift = siginfo.isdst and 0,
},
get_aspect = function()
return asp
c_ts_id) if c_rseg.locks then for pts, state in pairs(c_rseg.locks) do local pos = advtrains.decode_pos(pts) itab(i, "L "..pts.." -> "..state, "lock", pos) if not advtrains.is_passive(pos) then itab(i, "-!- "..S("Turnout/component missing at @1", minetest.pos_to_string(pos)), "err", nil) break end end end -- sanity check, is section at next the same as the current? local nvar = c_rseg.next if nvar then local re_tcbs = ildb.get_tcbs({p = nvar.p, s = (nvar.s==1) and 2 or 1}) if not re_tcbs then itab(i, "-!- "..S("TCB at @1 is missing", minetest.pos_to_string(nvar.p)), "err", nil) break elseif not re_tcbs.ts_id then itab(i, "-!- "..S("TCB at @1 is not assigned to previous track section", minetest.pos_to_string(nvar.p)), "err", nil) break elseif re_tcbs.ts_id~=c_ts_id then itab(i, "-!- "..S("TCB at @1 has different section than previous TCB", minetest.pos_to_string(nvar.p)), "err", nil) break end end -- advance c_sigd = nvar i = i + 1 end if c_sigd then local e_tcbs = ildb.get_tcbs(c_sigd) local signame = "-" if e_tcbs and e_tcbs.signal then signame = e_tcbs.signal_name or "o" end itab(i, "E "..sigd_to_string(c_sigd).." ("..signame..")", "end", c_sigd) else itab(i, "E (none)", "end", nil) end if not sel_rpartidx then sel_rpartidx = 1 end form = form.."textlist[0.5,2;3.5,3.9;routelog;"..table.concat(tab, ",")..";"..(sel_rpartidx or 1)..";false]" -- to the right of rtelog, controls are displayed for the thing in focus -- What is in focus is determined by the parameter sel_rpartidx local sel_rpart = tabref[sel_rpartidx] --atdebug("sel rpart",sel_rpart) if sel_rpart and sel_rpart.signal then -- get TCBS here and rseg selected local s_tcbs = ildb.get_tcbs(sel_rpart.param) local rseg = route[sel_rpart.seg] -- main aspect list local signalpos = s_tcbs and s_tcbs.signal if signalpos and rseg then form = form..F.label(4.5, 2, S("Signal Aspect:")) local ndef = signalpos and advtrains.ndb.get_ndef(signalpos) if ndef and ndef.advtrains and ndef.advtrains.main_aspects then local entries = { S("<Default Aspect>") } local sel = 1 for i, mae in ipairs(ndef.advtrains.main_aspects) do entries[i+1] = mae.description if mae.name == rseg.main_aspect then sel = i+1 end end form = form..F.dropdown(4.5, 3.0, 4, "sa_main_aspect", entries, sel, true) end -- checkbox for assign distant signal local assign_dst = rseg.assign_dst if assign_dst == nil then assign_dst = (sel_rpart.seg~=1) -- special behavior when assign_dst is nil (and not false): -- defaults to false for the very first signal and true for all others (= minimal user configuration overhead) -- Note: on save, the value will be fixed at either false or true end form = form..string.format("checkbox[4.5,4.0;sa_distant;"..S("Announce distant signal")..";%s]", assign_dst) else form = form..F.label(4.5, 2, S("No Signal at this TCB")) end elseif sel_rpart and sel_rpart.section then local rseg = route[sel_rpart.seg] if rseg then form = form..F.label(4.5, 2, S("Section Options:")) -- checkbox for call-on form = form..string.format("checkbox[4.5,4.0;se_callon;"..S("Call-on (section may be occupied)")..";%s]", rseg.call_on) end elseif sel_rpart and sel_rpart.err then form = form.."textarea[4.5,2.5;4,4;errorta;"..S("Error:")..";"..tab[sel_rpartidx].."]" else form = form..F.label(4.5, 2, S("<< Select a route part to edit options")) end form = form.."button[0.5,6;1,1;prev;<<<]" form = form.."button[1.5,6;1,1;back;"..routeid.."/"..#tcbs.routes.."]" form = form.."button[2.5,6;1,1;next;>>>]" --if route.smartroute_generated or route.default_autoworking then -- form = form.."button[3.5,6;2,1;noautogen;Clr Autogen]" --end form = form.."button[5.5,6;3,1;delete;"..S("Delete Route").."]" form = form.."button[0.5,7;3,1;back;"..S("Back to signal").."]" form = form.."button[3.5,7;2,1;clone;"..S("Clone Route").."]" form = form.."button[5.5,7;3,1;newfrom;"..S("New From Route").."]" --atdebug(route.ars) form = form.."style[ars;font=mono]" form = form.."textarea[0.8,8.3;5,3;ars;"..S("ARS Rule List")..";"..atil.ars_to_text(route.ars).."]" form = form.."button[5.5,8.23;3,1;savears;"..S("Save ARS List").."]" local formname = "at_il_routeedit_"..minetest.pos_to_string(sigd.p).."_"..sigd.s.."_"..routeid minetest.show_formspec(pname, formname, form) -- record selected entry from routelog for receive fields callback sel_rpartcache[pname] = sel_rpart end minetest.register_on_player_receive_fields(function(player, formname, fields) local pname = player:get_player_name() -- retreive sel_rpart from the cache in any case and clear it out local sel_rpart = sel_rpartcache[pname] if not minetest.check_player_privs(pname, {train_operator=true, interlocking=true}) then return end local pts, connids, routeids = string.match(formname, "^at_il_routeedit_([^_]+)_(%d)_(%d+)$") local pos, connid, routeid if pts then pos = minetest.string_to_pos(pts) connid = tonumber(connids) routeid = tonumber(routeids) if not connid or connid<1 or connid>2 then return end if not routeid then return end end if pos and connid and routeid and not fields.quit then local sigd = {p=pos, s=connid} local tcbs = ildb.get_tcbs(sigd) if not tcbs then return end local route = tcbs.routes[routeid] if not route then return end if fields.prev then atil.show_route_edit_form(pname, sigd, routeid - 1) return end if fields.next then atil.show_route_edit_form(pname, sigd, routeid + 1) return end if fields.setname and fields.name then route.name = fields.name end if fields.sa_main_aspect and sel_rpart and sel_rpart.signal then local idx = tonumber(fields.sa_main_aspect) -- get TCBS here and rseg selected local s_tcbs = ildb.get_tcbs(sel_rpart.param) local rseg = route[sel_rpart.seg] -- main aspect list local signalpos = s_tcbs and s_tcbs.signal if rseg then rseg.main_aspect = nil if idx > 1 then local ndef = signalpos and advtrains.ndb.get_ndef(signalpos) if ndef and ndef.advtrains and ndef.advtrains.main_aspects then rseg.main_aspect = ndef.advtrains.main_aspects[idx - 1].name end end end end if fields.sa_distant and sel_rpart and sel_rpart.signal then local rseg = route[sel_rpart.seg] if rseg then rseg.assign_dst = minetest.is_yes(fields.sa_distant) end end if fields.se_callon and sel_rpart and sel_rpart.section then local rseg = route[sel_rpart.seg] if rseg then rseg.call_on = minetest.is_yes(fields.se_callon) -- reshow form to update CO marker atil.show_route_edit_form(pname, sigd, routeid, sel_rpart.idx) return end end --if fields.noautogen then -- route.smartroute_generated = nil -- route.default_autoworking = nil -- -- reshow form for the button to disappear -- atil.show_route_edit_form(pname, sigd, routeid, sel_rpart and sel_rpart.idx) -- return --end if fields.delete then -- if something set the route in the meantime, make sure this doesn't break. atil.route.update_route(sigd, tcbs, nil, true) table.remove(tcbs.routes, routeid) advtrains.interlocking.show_signalling_form(sigd, pname) -- cleanup sel_rpartcache[pname] = nil return end if fields.clone then -- if something set the route in the meantime, make sure this doesn't break. atil.route.update_route(sigd, tcbs, nil, true) local rcopy = table.copy(route) rcopy.name = route.name.."_copy" rcopy.smartroute_generated = nil table.insert(tcbs.routes, routeid+1, rcopy) advtrains.interlocking.show_signalling_form(sigd, pname) -- cleanup sel_rpartcache[pname] = nil return end if fields.newfrom then advtrains.interlocking.init_route_prog(pname, sigd, route) minetest.close_formspec(pname, formname) tcbs.ars_ignore_next = nil -- cleanup sel_rpartcache[pname] = nil return end if fields.ars and fields.savears then route.ars = atil.text_to_ars(fields.ars) --atdebug(route.ars) end if fields.back then advtrains.interlocking.show_signalling_form(sigd, pname) -- cleanup sel_rpartcache[pname] = nil return end -- if an entry was selected in the textlist (and its not the current one) update the form if fields.routelog then local prev_idx = sel_rpart and sel_rpart.idx or 1 local tev = minetest.explode_textlist_event(fields.routelog) local new_idx = tev and tev.index if new_idx and new_idx ~= prev_idx then atil.show_route_edit_form(pname, sigd, routeid, new_idx) return end end if fields.quit then -- cleanup sel_rpartcache[pname] = nil end end end)