advtrains/advtrains_interlocking/signal_aspect_accessors.lua

158 lines
3.8 KiB
Lua
Raw Normal View History

2022-06-11 18:07:00 +02:00
local A = advtrains.interlocking.aspects
local D = advtrains.distant
local I = advtrains.interlocking
local N = advtrains.ndb
local pts = advtrains.roundfloorpts
2022-10-24 13:51:03 +02:00
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,
}
2022-06-11 18:07:00 +02:00
local get_aspect
local supposed_aspects = {}
function I.load_supposed_aspects(tbl)
if tbl then
supposed_aspects = tbl
2022-10-24 13:51:03 +02:00
for _, v in pairs(tbl) do
setmetatable(v, signal_aspect_metatable)
end
2022-06-11 18:07:00 +02:00
end
end
function I.save_supposed_aspects()
return supposed_aspects
end
local function get_supposed_aspect(pos)
return supposed_aspects[pts(pos)]
end
local function set_supposed_aspect(pos, asp)
supposed_aspects[pts(pos)] = asp
end
local function get_ndef(pos)
local node = N.get_node(pos)
return minetest.registered_nodes[node.name] or {}
end
local function get_supported_aspects(pos)
local ndef = get_ndef(pos)
if ndef.advtrains and ndef.advtrains.supported_aspects then
return ndef.advtrains.supported_aspects
end
return nil
end
local function adjust_aspect(pos, asp)
asp = table.copy(I.signal_convert_aspect_if_necessary(asp))
2022-10-24 13:51:03 +02:00
setmetatable(asp, signal_aspect_metatable)
2022-06-11 18:07:00 +02:00
local mainpos = D.get_main(pos)
local nxtasp
2022-10-24 13:51:03 +02:00
if mainpos then
2022-06-11 18:07:00 +02:00
nxtasp = get_aspect(mainpos)
2022-10-24 13:51:03 +02:00
end
if asp.main ~= 0 and mainpos then
2022-06-11 18:07:00 +02:00
asp.dst = nxtasp.main
else
asp.dst = nil
end
local suppasp = get_supported_aspects(pos)
if not suppasp then
return asp, asp
end
local stype = suppasp.type
if stype == 2 then
local group = suppasp.group
local name
2022-10-24 13:51:03 +02:00
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
2022-06-11 18:07:00 +02:00
name = A.get_type2_dst(group, nxtasp.type2name)
else
name = A.type1_to_type2main(asp, group)
end
asp.type2group = group
asp.type2name = name
return asp, name
end
asp.type2name = nil
asp.type2group = nil
return asp, asp
end
local function get_real_aspect(pos)
local ndef = get_ndef(pos)
if ndef.advtrains and ndef.advtrains.get_aspect then
local asp = ndef.advtrains.get_aspect(pos, node) or I.DANGER
local suppasp = get_supported_aspects(pos)
if suppasp.type == 2 then
2022-10-24 13:51:03 +02:00
asp = A.type2_to_type1(suppasp, asp)
2022-06-11 18:07:00 +02:00
end
return adjust_aspect(pos, asp)
end
return nil
end
get_aspect = function(pos)
local asp = get_supposed_aspect(pos)
if not asp then
asp = get_real_aspect(pos)
set_supposed_aspect(pos, asp)
end
return asp
end
2022-07-03 15:34:42 +02:00
local function set_aspect(pos, asp, skipdst)
2022-06-11 18:07:00 +02:00
local node = N.get_node(pos)
local ndef = minetest.registered_nodes[node.name]
if ndef and ndef.advtrains and ndef.advtrains.set_aspect then
local oldasp = I.signal_get_aspect(pos) or DANGER
local newasp, aspval = adjust_aspect(pos, asp)
set_supposed_aspect(pos, newasp)
ndef.advtrains.set_aspect(pos, node, aspval)
I.signal_on_aspect_changed(pos)
2022-06-11 18:07:00 +02:00
local aspect_changed = A.not_equalp(oldasp, newasp)
2022-07-03 15:34:42 +02:00
if (not skipdst) and aspect_changed then
2022-06-11 18:07:00 +02:00
D.update_main(pos)
end
end
end
local function clear_aspect(pos)
set_supposed_aspect(pos, nil)
end
local function readjust_aspect(pos)
set_aspect(pos, get_aspect(pos))
end
I.signal_get_supported_aspects = get_supported_aspects
I.signal_get_real_aspect = get_real_aspect
I.signal_get_aspect = get_aspect
I.signal_set_aspect = set_aspect
I.signal_clear_aspect = clear_aspect
I.signal_readjust_aspect = readjust_aspect