Add mesecon:register_on_mvps_move, fix mesecon:mvps_pull_all

This commit is contained in:
Novatux 2014-01-03 11:04:19 +01:00
parent d9cc1d5aa1
commit b0f81a8c62

@ -2,6 +2,7 @@
mesecon.mvps_stoppers = {} mesecon.mvps_stoppers = {}
mesecon.mvps_unmov = {} mesecon.mvps_unmov = {}
mesecon.on_mvps_move = {}
function mesecon:is_mvps_stopper(node, pushdir, stack, stackid) function mesecon:is_mvps_stopper(node, pushdir, stack, stackid)
local get_stopper = mesecon.mvps_stoppers[node.name] local get_stopper = mesecon.mvps_stoppers[node.name]
@ -27,6 +28,17 @@ function mesecon:is_mvps_unmov(objectname)
return mesecon.mvps_unmov[objectname] return mesecon.mvps_unmov[objectname]
end end
-- Functions to be called on mvps movement
function mesecon:register_on_mvps_move(callback)
mesecon.on_mvps_move[#mesecon.on_mvps_move+1] = callback
end
local function on_mvps_move(moved_nodes)
for _, callback in ipairs(mesecon.on_mvps_move) do
callback(moved_nodes)
end
end
function mesecon:mvps_process_stack(stack) function mesecon:mvps_process_stack(stack)
-- update mesecons for placed nodes ( has to be done after all nodes have been added ) -- update mesecons for placed nodes ( has to be done after all nodes have been added )
for _, n in ipairs(stack) do for _, n in ipairs(stack) do
@ -89,20 +101,30 @@ function mesecon:mvps_push(pos, dir, maximum) -- pos: pos of mvps; dir: directio
minetest.add_node(np, n.node) minetest.add_node(np, n.node)
minetest.get_meta(np):from_table(n.meta) minetest.get_meta(np):from_table(n.meta)
end end
local moved_nodes = {}
local oldstack = mesecon:tablecopy(nodes) local oldstack = mesecon:tablecopy(nodes)
for i in ipairs(nodes) do for i in ipairs(nodes) do
moved_nodes[i] = {}
moved_nodes[i].oldpos = nodes[i].pos
nodes[i].pos = mesecon:addPosRule(nodes[i].pos, dir) nodes[i].pos = mesecon:addPosRule(nodes[i].pos, dir)
moved_nodes[i].pos = nodes[i].pos
moved_nodes[i].node = nodes[i].node
moved_nodes[i].meta = nodes[i].meta
end end
for _, n in ipairs(nodes) do on_mvps_move(moved_nodes)
mesecon.on_placenode(n.pos, n.node)
mesecon:update_autoconnect(n.pos)
end
return true, nodes, oldstack return true, nodes, oldstack
end end
mesecon:register_on_mvps_move(function(moved_nodes)
for _, n in ipairs(moved_nodes) do
mesecon.on_placenode(n.pos, n.node)
mesecon:update_autoconnect(n.pos)
end
end)
function mesecon:mvps_pull_single(pos, dir) -- pos: pos of mvps; direction: direction of pull (matches push direction for sticky pistons) function mesecon:mvps_pull_single(pos, dir) -- pos: pos of mvps; direction: direction of pull (matches push direction for sticky pistons)
np = mesecon:addPosRule(pos, dir) np = mesecon:addPosRule(pos, dir)
nn = minetest.get_node(np) nn = minetest.get_node(np)
@ -119,8 +141,7 @@ function mesecon:mvps_pull_single(pos, dir) -- pos: pos of mvps; direction: dire
nodeupdate(pos) nodeupdate(pos)
mesecon.on_dignode(np, nn) mesecon.on_dignode(np, nn)
mesecon:update_autoconnect(np) mesecon:update_autoconnect(np)
mesecon:update_autoconnect(pos) on_mvps_move({{pos = pos, oldpos = np, node = nn, meta = meta}})
mesecon.on_placenode(pos, nn)
end end
return {{pos = np, node = {param2 = 0, name = "air"}}, {pos = pos, node = nn}} return {{pos = np, node = {param2 = 0, name = "air"}}, {pos = pos, node = nn}}
end end
@ -147,10 +168,14 @@ function mesecon:mvps_pull_all(pos, direction) -- pos: pos of mvps; direction: d
return return
end end
local moved_nodes = {}
local oldpos = {x=lpos2.x + direction.x, y=lpos2.y + direction.y, z=lpos2.z + direction.z} local oldpos = {x=lpos2.x + direction.x, y=lpos2.y + direction.y, z=lpos2.z + direction.z}
repeat repeat
lnode2 = minetest.get_node(lpos2) lnode2 = minetest.get_node(lpos2)
minetest.add_node(oldpos, {name=lnode2.name}) local meta = minetest.get_meta(lnode2):to_table()
minetest.add_node(oldpos, lnode2)
minetest.get_meta(oldpos):from_table(meta)
moved_nodes[#moved_nodes+1] = {pos = oldpos, oldpos = lnode2, node = lnode2, meta = meta}
nodeupdate(oldpos) nodeupdate(oldpos)
oldpos = {x=lpos2.x, y=lpos2.y, z=lpos2.z} oldpos = {x=lpos2.x, y=lpos2.y, z=lpos2.z}
lpos2.x = lpos2.x-direction.x lpos2.x = lpos2.x-direction.x
@ -159,9 +184,12 @@ function mesecon:mvps_pull_all(pos, direction) -- pos: pos of mvps; direction: d
lnode = minetest.get_node(lpos2) lnode = minetest.get_node(lpos2)
until lnode.name == "air" until lnode.name == "air"
or lnode.name == "ignore" or lnode.name == "ignore"
or (minetest.registered_nodes[lnode2.name] or (minetest.registered_nodes[lnode.name]
and minetest.registered_nodes[lnode2.name].liquidtype ~= "none") and minetest.registered_nodes[lnode.name].liquidtype ~= "none")
minetest.remove_node(oldpos) minetest.remove_node(oldpos)
mesecon.on_dignode(oldpos, lnode2)
mesecon:update_autoconnect(oldpos)
on_mvps_move(moved_nodes)
end end
function mesecon:mvps_move_objects(pos, dir, nodestack) function mesecon:mvps_move_objects(pos, dir, nodestack)