aboutsummaryrefslogtreecommitdiff
path: root/advtrains/passive.lua
diff options
context:
space:
mode:
Diffstat (limited to 'advtrains/passive.lua')
-rw-r--r--advtrains/passive.lua94
1 files changed, 40 insertions, 54 deletions
diff --git a/advtrains/passive.lua b/advtrains/passive.lua
index fe4790c..37b79e4 100644
--- a/advtrains/passive.lua
+++ b/advtrains/passive.lua
@@ -1,9 +1,5 @@
-- 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 = {}
+-- Rework for advtrains 2.5: The passive API now uses the reworked node_state system. Please see the comment in tracks.lua
function advtrains.getstate(parpos, pnode)
local pos
@@ -19,20 +15,8 @@ function advtrains.getstate(parpos, pnode)
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
+ if ndef and ndef.advtrains then
+ return ndef.advtrains.node_state
end
end
@@ -45,31 +29,48 @@ function advtrains.setstate(parpos, newstate, pnode)
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")
+ error("Invalid position supplied to setstate")
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
+
+ if not ndef or not ndef.advtrains then
+ return false, "missing_node_def"
+ end
+ local old_state = ndef.advtrains.node_state
+
+ if old_state == newstate then
+ -- nothing needs to be done
+ return true
end
+ if not ndef.advtrains.node_state_map then
+ return false, "missing_node_state_map"
+ end
+ local new_node_name = ndef.advtrains.node_state_map[newstate]
+ if not new_node_name then
+ return false, "no_such_state"
+ end
+
+ -- prevent state switching when route lock or train is present
if advtrains.get_train_at_pos(pos) then
- return false
+ return false, "train_here"
end
- if advtrains.interlocking and advtrains.interlocking.route.has_route_lock(minetest.pos_to_string(pos)) then
- return false
+ if advtrains.interlocking and advtrains.interlocking.route.has_route_lock(advtrains.encode_pos(pos)) then
+ return false, "route_lock_here"
end
- st(pos, node, newstate)
+ -- perform the switch
+ local new_node = {name = new_node_name, param2 = node.param2}
+ advtrains.ndb.swap_node(pos, new_node)
+ -- if callback is present, call it
+ if ndef.advtrains.node_on_switch_state then
+ ndef.advtrains.node_on_switch_state(pos, new_node, old_state, newstate)
+ end
+ -- invalidate paths (only relevant if this is a track)
+ advtrains.invalidate_all_paths(pos)
+
return true
end
@@ -86,12 +87,7 @@ function advtrains.is_passive(parpos, pnode)
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
+ if ndef and ndef.advtrains and ndef.advtrains.node_state_map then
return true
else
return false
@@ -102,20 +98,10 @@ end
function advtrains.set_fallback_state(pos, pnode)
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
- and ndef.advtrains.fallback_state then
- 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
-
- ndef.advtrains.setstate(pos, node, ndef.advtrains.fallback_state)
- return true
- end
+ if not ndef or not ndef.advtrains or not ndef.advtrains.node_fallback_state then
+ return false, "no_fallback_state"
+ end
+ return advtrains.setstate(pos, ndef.advtrains.node_fallback_state, node)
end