Move passive API to the advtrains core

to remove dependency of interlocking on luaautomation
h137
orwell96 2018-08-16 19:18:03 +02:00
parent 5fad61e9c9
commit 05cb6090ac
11 changed files with 162 additions and 112 deletions

View File

@ -172,6 +172,7 @@ dofile(advtrains.modpath.."/crafting.lua")
dofile(advtrains.modpath.."/craft_items.lua")
dofile(advtrains.modpath.."/log.lua")
dofile(advtrains.modpath.."/passive.lua")
--load/save

99
advtrains/passive.lua Normal file
View File

@ -0,0 +1,99 @@
-- passive.lua
-- API to passive components, as described in passive_api.txt of advtrains_luaautomation
-- This has been moved to the advtrains core in turn with the interlocking system,
-- to prevent a dependency on luaautomation.
local deprecation_warned = {}
function advtrains.getstate(parpos, pnode)
local pos
if atlatc then
pos = atlatc.pcnaming.resolve_pos(parpos)
else
pos = advtrains.round_vector_floor_y(parpos)
end
if type(pos)~="table" or (not pos.x or not pos.y or not pos.z) then
debug.sethook()
error("Invalid position supplied to getstate")
end
local node=pnode or advtrains.ndb.get_node(pos)
local ndef=minetest.registered_nodes[node.name]
local st
if ndef and ndef.advtrains and ndef.advtrains.getstate then
st=ndef.advtrains.getstate
elseif ndef and ndef.luaautomation and ndef.luaautomation.getstate then
if not deprecation_warned[node.name] then
minetest.log("warning", node.name.." uses deprecated definition of ATLATC functions in the 'luaautomation' field. Please move them to the 'advtrains' field!")
end
st=ndef.luaautomation.getstate
else
return nil
end
if type(st)=="function" then
return st(pos, node)
else
return st
end
end
function advtrains.setstate(parpos, newstate, pnode)
local pos
if atlatc then
pos = atlatc.pcnaming.resolve_pos(parpos)
else
pos = advtrains.round_vector_floor_y(parpos)
end
if type(pos)~="table" or (not pos.x or not pos.y or not pos.z) then
debug.sethook()
error("Invalid position supplied to getstate")
end
local node=pnode or advtrains.ndb.get_node(pos)
local ndef=minetest.registered_nodes[node.name]
local st
if ndef and ndef.advtrains and ndef.advtrains.setstate then
st=ndef.advtrains.setstate
elseif ndef and ndef.luaautomation and ndef.luaautomation.setstate then
if not deprecation_warned[node.name] then
minetest.log("warning", node.name.." uses deprecated definition of ATLATC functions in the 'luaautomation' field. Please move them to the 'advtrains' field!")
end
st=ndef.luaautomation.setstate
else
return nil
end
if advtrains.get_train_at_pos(pos) then
return false
end
if advtrains.interlocking and advtrains.interlocking.route.has_route_lock(minetest.pos_to_string(pos)) then
return false
end
st(pos, node, newstate)
return true
end
function advtrains.is_passive(parpos, pnode)
local pos
if atlatc then
pos = atlatc.pcnaming.resolve_pos(parpos)
else
pos = advtrains.round_vector_floor_y(parpos)
end
if type(pos)~="table" or (not pos.x or not pos.y or not pos.z) then
debug.sethook()
error("Invalid position supplied to getstate")
end
local node=pnode or advtrains.ndb.get_node(pos)
local ndef=minetest.registered_nodes[node.name]
if ndef and ndef.advtrains and ndef.advtrains.getstate then
return true
elseif ndef and ndef.luaautomation and ndef.luaautomation.getstate then
if not deprecation_warned[node.name] then
minetest.log("warning", node.name.." uses deprecated definition of ATLATC functions in the 'luaautomation' field. Please move them to the 'advtrains' field!")
end
return true
else
return false
end
end

View File

@ -96,24 +96,16 @@ for r,f in pairs({on={as="off", ls="green", als="red"}, off={as="on", ls="red",
mesecons = {effector = {
rules=advtrains.meseconrules,
["action_"..f.as] = function (pos, node)
advtrains.ndb.swap_node(pos, {name = "advtrains:signal_"..f.as..rotation, param2 = node.param2}, true)
advtrains.setstate(pos, f.als, node)
end
}},
luaautomation = {
getstate = f.ls,
setstate = function(pos, node, newstate)
if newstate == f.als then
advtrains.ndb.swap_node(pos, {name = "advtrains:signal_"..f.as..rotation, param2 = node.param2}, true)
end
end,
},
on_rightclick=function(pos, node, player)
local pname = player:get_player_name()
local sigd = advtrains.interlocking and advtrains.interlocking.db.get_sigd_for_signal(pos)
if sigd then
advtrains.interlocking.show_signalling_form(sigd, pname)
elseif advtrains.check_turnout_signal_protection(pos, player:get_player_name()) then
advtrains.ndb.swap_node(pos, {name = "advtrains:signal_"..f.as..rotation, param2 = node.param2}, true)
advtrains.setstate(pos, f.als, node)
end
end,
-- new signal API
@ -124,7 +116,13 @@ for r,f in pairs({on={as="off", ls="green", als="red"}, off={as="on", ls="red",
else
advtrains.ndb.swap_node(pos, {name = "advtrains:signal_off"..rotation, param2 = node.param2}, true)
end
end
end,
getstate = f.ls,
setstate = function(pos, node, newstate)
if newstate == f.als then
advtrains.ndb.swap_node(pos, {name = "advtrains:signal_"..f.as..rotation, param2 = node.param2}, true)
end
end,
},
can_dig = can_dig_func,
})
@ -161,24 +159,16 @@ for r,f in pairs({on={as="off", ls="green", als="red"}, off={as="on", ls="red",
mesecons = {effector = {
rules = mrules_wallsignal,
["action_"..f.as] = function (pos, node)
advtrains.ndb.swap_node(pos, {name = "advtrains:signal_wall_"..loc.."_"..f.as, param2 = node.param2}, true)
advtrains.setstate(pos, f.als, node)
end
}},
luaautomation = {
getstate = f.ls,
setstate = function(pos, node, newstate)
if newstate == f.als then
advtrains.ndb.swap_node(pos, {name = "advtrains:signal_wall_"..loc.."_"..f.as, param2 = node.param2}, true)
end
end,
},
on_rightclick=function(pos, node, player)
local pname = player:get_player_name()
local sigd = advtrains.interlocking and advtrains.interlocking.db.get_sigd_for_signal(pos)
if sigd then
advtrains.interlocking.show_signalling_form(sigd, pname)
elseif advtrains.check_turnout_signal_protection(pos, player:get_player_name()) then
advtrains.ndb.swap_node(pos, {name = "advtrains:signal_wall_"..loc.."_"..f.as, param2 = node.param2}, true)
advtrains.setstate(pos, f.als, node)
end
end,
-- new signal API
@ -189,7 +179,13 @@ for r,f in pairs({on={as="off", ls="green", als="red"}, off={as="on", ls="red",
else
advtrains.ndb.swap_node(pos, {name = "advtrains:signal_wall_"..loc.."_off", param2 = node.param2}, true)
end
end
end,
getstate = f.ls,
setstate = function(pos, node, newstate)
if newstate == f.als then
advtrains.ndb.swap_node(pos, {name = "advtrains:signal_wall_"..loc.."_"..f.as, param2 = node.param2}, true)
end
end,
},
can_dig = can_dig_func,
})
@ -225,7 +221,7 @@ minetest.register_node("advtrains:across_off", {
advtrains.ndb.swap_node(pos, {name = "advtrains:across_on", param2 = node.param2}, true)
end
}},
luaautomation = {
advtrains = {
getstate = "off",
setstate = function(pos, node, newstate)
if newstate == "on" then
@ -266,7 +262,7 @@ minetest.register_node("advtrains:across_on", {
advtrains.ndb.swap_node(pos, {name = "advtrains:across_off", param2 = node.param2}, true)
end
}},
luaautomation = {
advtrains = {
getstate = "on",
setstate = function(pos, node, newstate)
if newstate == "off" then

View File

@ -302,27 +302,32 @@ function advtrains.register_tracks(tracktype, def, preset)
--connections
ndef.at_conns = advtrains.rotate_conn_by(var.conns, (rotid-1)*preset.regstep)
local ndef_avt_table
if var.switchalt and var.switchst then
local switchfunc=function(pos, node, newstate)
if newstate~=var.switchst and not advtrains.get_train_at_pos(pos)
and not (advtrains.interlocking and advtrains.interlocking.route.has_route_lock(advtrains.roundfloorpts(pos)) ) then --TODO schöner machen
-- this code is only called from the internal setstate function, which
-- ensures that it is safe to switch the turnout
if newstate~=var.switchst then
advtrains.ndb.swap_node(pos, {name=def.nodename_prefix.."_"..var.switchalt..rotation, param2=node.param2})
advtrains.invalidate_all_paths(pos)
end
end
ndef.on_rightclick = function(pos, node, player)
if advtrains.check_turnout_signal_protection(pos, player:get_player_name()) then
switchfunc(pos, node)
advtrains.setstate(pos, newstate, node)
advtrains.log("Switch", player:get_player_name(), pos)
end
end
if var.switchmc then
ndef.mesecons = {effector = {
["action_"..var.switchmc] = switchfunc,
["action_"..var.switchmc] = function(pos, node)
advtrains.setstate(pos, nil, node)
end,
rules=advtrains.meseconrules
}}
end
ndef.luaautomation = {
ndef_avt_table = {
getstate = var.switchst,
setstate = switchfunc,
}
@ -333,6 +338,11 @@ function advtrains.register_tracks(tracktype, def, preset)
adef=def.get_additional_definiton(def, preset, suffix, rotation)
end
ndef = advtrains.merge_tables(ndef, adef)
-- insert getstate/setstate functions after merging the additional definitions
if ndef_avt_table then
ndef.advtrains = advtrains.merge_tables(ndef.advtrains or {}, ndef_avt_table)
end
minetest.register_node(":"..def.nodename_prefix.."_"..suffix..rotation, ndef)
--trackplacer

View File

@ -303,7 +303,6 @@ local function merge_ts(root_id, merge_id)
track_sections[merge_id] = nil
end
-- TODO temporary
local lntrans = { "A", "B" }
local function sigd_to_string(sigd)
return minetest.pos_to_string(sigd.p).." / "..lntrans[sigd.s]

View File

@ -282,7 +282,7 @@ minetest.register_chatcommand("at_rp_set",
{
params = "<name>", -- Short parameter description
description = "Completes route programming procedure", -- Full description
privs = {interlocking = true}, -- TODO
privs = {interlocking = true},
func = function(pname, param)
return advtrains.pcall(function()
if param=="" then
@ -315,7 +315,7 @@ minetest.register_chatcommand("at_rp_back",
{
params = "", -- Short parameter description
description = "Remove last route segment", -- Full description
privs = {interlocking = true}, -- Require the "privs" privilege to run
privs = {interlocking = true},
func = function(pname, param)
return advtrains.pcall(function()
local rp = player_rte_prog[pname]
@ -336,7 +336,7 @@ minetest.register_chatcommand("at_rp_mark",
{
params = "", -- Short parameter description
description = "Re-set route programming markers", -- Full description
privs = {interlocking = true}, -- TODO
privs = {interlocking = true},
func = function(pname, param)
return advtrains.pcall(function()
local rp = player_rte_prog[pname]
@ -352,7 +352,7 @@ minetest.register_chatcommand("at_rp_discard",
{
params = "", -- Short parameter description
description = "Discards the currently programmed route", -- Full description
privs = {interlocking = true}, -- Require the "privs" privilege to run
privs = {interlocking = true},
func = function(pname, param)
return advtrains.pcall(function()
player_rte_prog[pname] = nil
@ -365,5 +365,4 @@ minetest.register_chatcommand("at_rp_discard",
--TODO on route setting
-- unify luaautomation get/setstate interface to the core
-- privileges for route programming
-- routes should end at signals. complete route setting by punching a signal, and command as exceptional route completion

View File

@ -73,18 +73,15 @@ function ilrs.set_route(signal, route, try)
local confl = ilrs.has_route_lock(pts, state)
local pos = minetest.string_to_pos(pts)
local node = advtrains.ndb.get_node(pos)
local ndef = minetest.registered_nodes[node.name]
if ndef and ndef.luaautomation and ndef.luaautomation.setstate and ndef.luaautomation.getstate then
local cstate = ndef.luaautomation.getstate
if type(cstate)=="function" then cstate = cstate(pos) end
if advtrains.is_passive(pos) then
local cstate = advtrains.getstate(pos)
if cstate ~= state then
local confl = ilrs.has_route_lock(pts)
if confl then
if not try then atwarn("Encountered route lock while a real run of routesetting routine, at position",pts,"while setting route",rtename,"of",signal) end
return false, "Lock conflict at "..pts..", Held locked by:\n"..confl, nil, pts
elseif not try then
ndef.luaautomation.setstate(pos, node, state)
advtrains.setstate(pos, state)
end
end
if not try then
@ -188,7 +185,11 @@ end
-- frees all route locks, even manual ones set with the tool, at a specific position
function ilrs.remove_route_locks(pts, nocallbacks)
ilrs.rte_locks[pts] = nil
--TODO callbacks
-- This must be delayed, because this code is executed in-between a train step
-- TODO use luaautomation timers?
if not nocallbacks then
minetest.after(0, ilrs.update_waiting, "lck", pts)
end
end
local function sigd_equal(sigd, cmp)

View File

@ -86,7 +86,6 @@ local function safe_string_find(...)
end
local mp=minetest.get_modpath("advtrains_luaautomation")
local p_api_getstate, p_api_setstate, p_api_is_passive = dofile(mp.."/passive.lua")
local static_env = {
--core LUA functions
@ -148,12 +147,13 @@ local static_env = {
date = safe_date,
},
POS = function(x,y,z) return {x=x, y=y, z=z} end,
getstate = p_api_getstate,
setstate = p_api_setstate,
is_passive = p_api_is_passive,
getstate = advtrains.getstate,
setstate = advtrains.setstate,
is_passive = advtrains.is_passive,
--interrupts are handled per node, position unknown. (same goes for digilines)
--however external interrupts can be set here.
interrupt_pos = function(pos, imesg)
interrupt_pos = function(parpos, imesg)
local pos=atlatc.pcnaming.resolve_pos(parpos)
if not type(pos)=="table" or not pos.x or not pos.y or not pos.z then
debug.sethook()
error("Invalid position supplied to interrupt_pos")

View File

@ -1,52 +0,0 @@
-- passive.lua
-- API to passive components, as described in passive_api.txt
local function getstate(parpos)
local pos=atlatc.pcnaming.resolve_pos(parpos)
if type(pos)~="table" or (not pos.x or not pos.y or not pos.z) then
debug.sethook()
error("Invalid position supplied to getstate")
end
local node=advtrains.ndb.get_node(pos)
local ndef=minetest.registered_nodes[node.name]
if ndef and ndef.luaautomation and ndef.luaautomation.getstate then
local st=ndef.luaautomation.getstate
if type(st)=="function" then
return st(pos, node)
else
return st
end
end
return nil
end
local function setstate(parpos, newstate)
local pos=atlatc.pcnaming.resolve_pos(parpos)
if type(pos)~="table" or (not pos.x or not pos.y or not pos.z) then
debug.sethook()
error("Invalid position supplied to setstate")
end
local node=advtrains.ndb.get_node(pos)
local ndef=minetest.registered_nodes[node.name]
if ndef and ndef.luaautomation and ndef.luaautomation.setstate then
local st=ndef.luaautomation.setstate
st(pos, node, newstate)
end
end
local function is_passive(parpos)
local pos=atlatc.pcnaming.resolve_pos(parpos)
if type(pos)~="table" or (not pos.x or not pos.y or not pos.z) then
return false
end
local node=advtrains.ndb.get_node(pos)
local ndef=minetest.registered_nodes[node.name]
if ndef and ndef.luaautomation and ndef.luaautomation.getstate then
return true
end
return false
end
-- gets called from environment.lua
-- return the values here to keep them local
return getstate, setstate, is_passive

View File

@ -5,10 +5,11 @@ Switches
Signals
Displays
Mesecon Transmitter
Those passive components can also be used inside interlocking systems.
All passive components have a table called 'luaautomation' in their node definition and have the group 'save_in_nodedb' set, so they work in unloaded chunks.
All passive components have a table called 'advtrains' in their node definition and have the group 'save_in_nodedb' set, so they work in unloaded chunks.
Example for a switch:
luaautomation = {
advtrains = {
getstate = function(pos, node)
return "st"
end,

View File

@ -38,19 +38,15 @@ minetest.register_craftitem("advtrains_luaautomation:pcnaming",{
minetest.record_protection_violation(pos, pname)
return
end
local node=minetest.get_node(pos)
local ndef=minetest.registered_nodes[node.name]
if ndef then
if ndef.luaautomation then
--look if this one already has a name
local pn=""
for name, npos in pairs(atlatc.pcnaming.name_map) do
if vector.equals(npos, pos) then
pn=name
end
if advtrains.is_passive(pos) then
--look if this one already has a name
local pn=""
for name, npos in pairs(atlatc.pcnaming.name_map) do
if vector.equals(npos, pos) then
pn=name
end
minetest.show_formspec(pname, "atlatc_naming_"..minetest.pos_to_string(pos), "field[pn;Set name of component (empty to clear);"..pn.."]")
end
minetest.show_formspec(pname, "atlatc_naming_"..minetest.pos_to_string(pos), "field[pn;Set name of component (empty to clear);"..pn.."]")
end
end
end,