diff options
author | orwell96 <orwell@bleipb.de> | 2018-08-16 19:18:03 +0200 |
---|---|---|
committer | orwell96 <orwell@bleipb.de> | 2018-08-16 19:18:03 +0200 |
commit | 05cb6090ac9537650a900b64768bf3ed959cebed (patch) | |
tree | 11b7e3ab4bb846ab069fc10832b07cb36d6eb6dd /advtrains | |
parent | 5fad61e9c981115a183527ffe58a7bbe26fea4e7 (diff) | |
download | advtrains-05cb6090ac9537650a900b64768bf3ed959cebed.tar.gz advtrains-05cb6090ac9537650a900b64768bf3ed959cebed.tar.bz2 advtrains-05cb6090ac9537650a900b64768bf3ed959cebed.zip |
Move passive API to the advtrains core
to remove dependency of interlocking on luaautomation
Diffstat (limited to 'advtrains')
-rw-r--r-- | advtrains/init.lua | 1 | ||||
-rw-r--r-- | advtrains/passive.lua | 99 | ||||
-rw-r--r-- | advtrains/signals.lua | 44 | ||||
-rw-r--r-- | advtrains/tracks.lua | 20 |
4 files changed, 135 insertions, 29 deletions
diff --git a/advtrains/init.lua b/advtrains/init.lua index 2037ef1..92f8a9c 100644 --- a/advtrains/init.lua +++ b/advtrains/init.lua @@ -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 diff --git a/advtrains/passive.lua b/advtrains/passive.lua new file mode 100644 index 0000000..07cab42 --- /dev/null +++ b/advtrains/passive.lua @@ -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 diff --git a/advtrains/signals.lua b/advtrains/signals.lua index 1bbd7d4..75f9213 100644 --- a/advtrains/signals.lua +++ b/advtrains/signals.lua @@ -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 diff --git a/advtrains/tracks.lua b/advtrains/tracks.lua index 092a1ec..b1979f2 100644 --- a/advtrains/tracks.lua +++ b/advtrains/tracks.lua @@ -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
|