2022-10-31 10:57:37 +01:00
|
|
|
--- Signal aspect accessors
|
|
|
|
-- @module advtrains.interlocking
|
|
|
|
|
2023-03-26 11:53:00 +02:00
|
|
|
local A = advtrains.interlocking.aspect
|
2022-06-11 18:07:00 +02:00
|
|
|
local D = advtrains.distant
|
|
|
|
local I = advtrains.interlocking
|
|
|
|
local N = advtrains.ndb
|
|
|
|
local pts = advtrains.roundfloorpts
|
|
|
|
|
|
|
|
local get_aspect
|
|
|
|
|
|
|
|
local supposed_aspects = {}
|
|
|
|
|
2022-10-31 10:57:37 +01:00
|
|
|
--- Replace the signal aspect cache.
|
|
|
|
-- @function load_supposed_aspects
|
|
|
|
-- @param db The new database.
|
2022-06-11 18:07:00 +02:00
|
|
|
function I.load_supposed_aspects(tbl)
|
|
|
|
if tbl then
|
2023-03-26 11:53:00 +02:00
|
|
|
supposed_aspects = {}
|
|
|
|
for k, v in pairs(tbl) do
|
|
|
|
supposed_aspects[k] = A(v)
|
2022-10-24 13:51:03 +02:00
|
|
|
end
|
2022-06-11 18:07:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-31 10:57:37 +01:00
|
|
|
--- Retrieve the signal aspect cache.
|
|
|
|
-- @function save_supposed_aspects
|
|
|
|
-- @return The current database in use.
|
2022-06-11 18:07:00 +02:00
|
|
|
function I.save_supposed_aspects()
|
2023-03-26 11:53:00 +02:00
|
|
|
local t = {}
|
|
|
|
for k, v in pairs(supposed_aspects) do
|
|
|
|
t[k] = v:plain(true)
|
|
|
|
end
|
|
|
|
return t
|
2022-06-11 18:07:00 +02:00
|
|
|
end
|
|
|
|
|
2022-10-31 10:57:37 +01:00
|
|
|
--- Read the aspect of a signal strictly from cache.
|
|
|
|
-- @param pos The position of the signal.
|
|
|
|
-- @return[1] The aspect of the signal (if present in cache).
|
|
|
|
-- @return[2] The nil constant (otherwise).
|
2022-06-11 18:07:00 +02:00
|
|
|
local function get_supposed_aspect(pos)
|
|
|
|
return supposed_aspects[pts(pos)]
|
|
|
|
end
|
|
|
|
|
2022-10-31 10:57:37 +01:00
|
|
|
--- Update the signal aspect information in cache.
|
|
|
|
-- @param pos The position of the signal.
|
|
|
|
-- @param asp The new signal aspect
|
2022-06-11 18:07:00 +02:00
|
|
|
local function set_supposed_aspect(pos, asp)
|
|
|
|
supposed_aspects[pts(pos)] = asp
|
|
|
|
end
|
|
|
|
|
2022-10-31 10:57:37 +01:00
|
|
|
--- Get the definition of a node.
|
|
|
|
-- @param pos The position of the node.
|
|
|
|
-- @return[1] The definition of the node (if present).
|
|
|
|
-- @return[2] An empty table (otherwise).
|
2022-06-11 18:07:00 +02:00
|
|
|
local function get_ndef(pos)
|
|
|
|
local node = N.get_node(pos)
|
2023-03-26 11:53:00 +02:00
|
|
|
return (minetest.registered_nodes[node.name] or {}), node
|
2022-06-11 18:07:00 +02:00
|
|
|
end
|
|
|
|
|
2022-10-31 10:57:37 +01:00
|
|
|
--- Get the aspects supported by a signal.
|
|
|
|
-- @function signal_get_supported_aspects
|
|
|
|
-- @param pos The position of the signal.
|
|
|
|
-- @return[1] The table of supported aspects (if present).
|
|
|
|
-- @return[2] The nil constant (otherwise).
|
2022-06-11 18:07:00 +02:00
|
|
|
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
|
|
|
|
|
2022-10-31 10:57:37 +01:00
|
|
|
--- Adjust a new signal aspect to fit a signal.
|
|
|
|
-- @param pos The position of the signal.
|
|
|
|
-- @param asp The new signal aspect.
|
|
|
|
-- @return The adjusted signal aspect.
|
|
|
|
-- @return The information to pass to the `advtrains.set_aspect` field in the node definitions.
|
2022-06-11 18:07:00 +02:00
|
|
|
local function adjust_aspect(pos, asp)
|
2023-03-26 11:53:00 +02:00
|
|
|
local asp = A(asp)
|
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
|
2022-06-11 18:07:00 +02:00
|
|
|
local suppasp = get_supported_aspects(pos)
|
|
|
|
if not suppasp then
|
2023-03-26 11:53:00 +02:00
|
|
|
return asp
|
2022-06-11 18:07:00 +02:00
|
|
|
end
|
2023-03-26 11:53:00 +02:00
|
|
|
return asp:adjust_distant(nxtasp, suppasp.dst_shift):to_group(suppasp.group)
|
2022-06-11 18:07:00 +02:00
|
|
|
end
|
|
|
|
|
2022-10-31 10:57:37 +01:00
|
|
|
--- Get the aspect of a signal without accessing the cache.
|
|
|
|
-- For most cases, `get_aspect` should be used instead.
|
|
|
|
-- @function signal_get_real_aspect
|
|
|
|
-- @param pos The position of the signal.
|
|
|
|
-- @return[1] The signal aspect adjusted using `adjust_aspect` (if present).
|
|
|
|
-- @return[2] The nil constant (otherwise).
|
2022-06-11 18:07:00 +02:00
|
|
|
local function get_real_aspect(pos)
|
2023-03-26 11:53:00 +02:00
|
|
|
local ndef, node = get_ndef(pos)
|
2022-06-11 18:07:00 +02:00
|
|
|
if ndef.advtrains and ndef.advtrains.get_aspect then
|
|
|
|
local asp = ndef.advtrains.get_aspect(pos, node) or I.DANGER
|
|
|
|
return adjust_aspect(pos, asp)
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2022-10-31 10:57:37 +01:00
|
|
|
--- Get the aspect of a signal.
|
|
|
|
-- @function signal_get_aspect
|
|
|
|
-- @param pos The position of the signal.
|
|
|
|
-- @return[1] The aspect of the signal (if present).
|
|
|
|
-- @return[2] The nil constant (otherwise).
|
2022-06-11 18:07:00 +02:00
|
|
|
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-10-31 10:57:37 +01:00
|
|
|
--- Set the aspect of a signal.
|
|
|
|
-- @function signal_set_aspect
|
|
|
|
-- @param pos The position of the signal.
|
|
|
|
-- @param asp The new signal aspect.
|
|
|
|
-- @param[opt=false] skipdst Whether to skip updating distant signals.
|
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
|
2023-03-26 11:53:00 +02:00
|
|
|
local newasp = adjust_aspect(pos, asp)
|
2022-06-11 18:07:00 +02:00
|
|
|
set_supposed_aspect(pos, newasp)
|
2023-03-26 11:53:00 +02:00
|
|
|
ndef.advtrains.set_aspect(pos, node, newasp)
|
2022-07-03 12:45:27 +02:00
|
|
|
I.signal_on_aspect_changed(pos)
|
2023-03-26 11:53:00 +02:00
|
|
|
local aspect_changed = 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
|
|
|
|
|
2022-10-31 10:57:37 +01:00
|
|
|
--- Remove a signal from cache.
|
|
|
|
-- @function signal_clear_aspect
|
|
|
|
-- @param pos The position of the signal.
|
2022-06-11 18:07:00 +02:00
|
|
|
local function clear_aspect(pos)
|
|
|
|
set_supposed_aspect(pos, nil)
|
|
|
|
end
|
|
|
|
|
2022-10-31 10:57:37 +01:00
|
|
|
--- Readjust the aspect of a signal.
|
|
|
|
-- @function signal_readjust_aspect
|
|
|
|
-- @param pos The position of the signal.
|
2022-06-11 18:07:00 +02:00
|
|
|
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
|