2012-03-05 19:21:26 +01:00
|
|
|
--register stoppers for movestones/pistons
|
|
|
|
|
2013-04-02 11:56:37 +02:00
|
|
|
mesecon.mvps_stoppers = {}
|
|
|
|
mesecon.mvps_unmov = {}
|
2012-03-05 19:21:26 +01:00
|
|
|
|
2012-12-26 22:54:28 +01:00
|
|
|
function mesecon:is_mvps_stopper(node, pushdir, stack, stackid)
|
|
|
|
local get_stopper = mesecon.mvps_stoppers[node.name]
|
|
|
|
if type (get_stopper) == "function" then
|
|
|
|
get_stopper = get_stopper(node, pushdir, stack, stackid)
|
|
|
|
end
|
|
|
|
return get_stopper
|
2012-03-05 19:21:26 +01:00
|
|
|
end
|
|
|
|
|
2012-12-26 22:54:28 +01:00
|
|
|
function mesecon:register_mvps_stopper(nodename, get_stopper)
|
|
|
|
if get_stopper == nil then
|
|
|
|
get_stopper = true
|
|
|
|
end
|
|
|
|
mesecon.mvps_stoppers[nodename] = get_stopper
|
2012-03-05 19:21:26 +01:00
|
|
|
end
|
|
|
|
|
2013-04-02 11:56:37 +02:00
|
|
|
-- Objects that cannot be moved (e.g. movestones)
|
|
|
|
function mesecon:register_mvps_unmov(objectname)
|
|
|
|
mesecon.mvps_unmov[objectname] = true;
|
|
|
|
end
|
|
|
|
|
|
|
|
function mesecon:is_mvps_unmov(objectname)
|
|
|
|
return mesecon.mvps_unmov[objectname]
|
|
|
|
end
|
|
|
|
|
2012-12-26 22:54:28 +01:00
|
|
|
function mesecon:mvps_process_stack(stack)
|
|
|
|
-- update mesecons for placed nodes ( has to be done after all nodes have been added )
|
|
|
|
for _, n in ipairs(stack) do
|
2013-01-20 13:01:40 +01:00
|
|
|
nodeupdate(n.pos)
|
2012-12-27 09:28:04 +01:00
|
|
|
mesecon.on_placenode(n.pos, minetest.env:get_node(n.pos))
|
2012-12-26 22:54:28 +01:00
|
|
|
mesecon:update_autoconnect(n.pos)
|
|
|
|
end
|
|
|
|
end
|
2012-06-23 13:24:03 +02:00
|
|
|
|
2013-04-02 11:56:37 +02:00
|
|
|
function mesecon:mvps_get_stack(pos, dir, maximum)
|
2012-12-26 22:54:28 +01:00
|
|
|
-- determine the number of nodes to be pushed
|
2013-04-02 11:56:37 +02:00
|
|
|
local np = {x = pos.x, y = pos.y, z = pos.z}
|
2012-12-26 22:54:28 +01:00
|
|
|
local nodes = {}
|
|
|
|
while true do
|
2013-04-02 11:56:37 +02:00
|
|
|
local nn = minetest.env:get_node_or_nil(np)
|
2012-12-26 22:54:28 +01:00
|
|
|
if not nn or #nodes > maximum then
|
|
|
|
-- don't push at all, something is in the way (unloaded map or too many nodes)
|
2013-04-02 12:09:59 +02:00
|
|
|
return nil
|
2012-12-26 22:54:28 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if nn.name == "air"
|
|
|
|
or minetest.registered_nodes[nn.name].liquidtype ~= "none" then --is liquid
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert (nodes, {node = nn, pos = np})
|
|
|
|
|
|
|
|
np = mesecon:addPosRule(np, dir)
|
|
|
|
end
|
2013-04-02 11:56:37 +02:00
|
|
|
return nodes
|
|
|
|
end
|
|
|
|
|
|
|
|
function mesecon:mvps_push(pos, dir, maximum) -- pos: pos of mvps; dir: direction of push; maximum: maximum nodes to be pushed
|
|
|
|
local nodes = mesecon:mvps_get_stack(pos, dir, maximum)
|
2012-12-26 22:54:28 +01:00
|
|
|
|
2013-04-02 12:09:59 +02:00
|
|
|
if not nodes then return end
|
2012-12-26 22:54:28 +01:00
|
|
|
-- determine if one of the nodes blocks the push
|
|
|
|
for id, n in ipairs(nodes) do
|
|
|
|
if mesecon:is_mvps_stopper(n.node, dir, nodes, id) then
|
|
|
|
return
|
2012-06-23 13:24:03 +02:00
|
|
|
end
|
2012-12-26 22:54:28 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- remove all nodes
|
|
|
|
for _, n in ipairs(nodes) do
|
2013-03-11 22:37:50 +01:00
|
|
|
n.meta = minetest.env:get_meta(n.pos):to_table()
|
2012-12-26 22:54:28 +01:00
|
|
|
minetest.env:remove_node(n.pos)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- update mesecons for removed nodes ( has to be done after all nodes have been removed )
|
|
|
|
for _, n in ipairs(nodes) do
|
|
|
|
mesecon.on_dignode(n.pos, n.node)
|
|
|
|
mesecon:update_autoconnect(n.pos)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- add nodes
|
|
|
|
for _, n in ipairs(nodes) do
|
|
|
|
np = mesecon:addPosRule(n.pos, dir)
|
|
|
|
minetest.env:add_node(np, n.node)
|
2013-03-11 22:37:50 +01:00
|
|
|
minetest.env:get_meta(np):from_table(n.meta)
|
2012-12-26 22:54:28 +01:00
|
|
|
end
|
|
|
|
|
2013-04-02 11:56:37 +02:00
|
|
|
local oldstack = mesecon:tablecopy(nodes)
|
2012-12-26 22:54:28 +01:00
|
|
|
for i in ipairs(nodes) do
|
|
|
|
nodes[i].pos = mesecon:addPosRule(nodes[i].pos, dir)
|
|
|
|
end
|
|
|
|
|
2013-04-02 11:56:37 +02:00
|
|
|
return true, nodes, oldstack
|
2012-12-26 22:54:28 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
|
|
|
nn = minetest.env:get_node(np)
|
|
|
|
|
|
|
|
if minetest.registered_nodes[nn.name].liquidtype == "none"
|
|
|
|
and not mesecon:is_mvps_stopper(nn, {x = -dir.x, y = -dir.y, z = -dir.z}, {{pos = np, node = nn}}, 1) then
|
2013-03-11 22:37:50 +01:00
|
|
|
local meta = minetest.env:get_meta(np):to_table()
|
2012-12-26 22:54:28 +01:00
|
|
|
minetest.env:remove_node(np)
|
|
|
|
minetest.env:add_node(pos, nn)
|
2013-03-11 22:37:50 +01:00
|
|
|
minetest.env:get_meta(pos):from_table(meta)
|
2012-12-26 22:54:28 +01:00
|
|
|
|
|
|
|
nodeupdate(np)
|
|
|
|
nodeupdate(pos)
|
|
|
|
mesecon.on_dignode(np, nn)
|
|
|
|
mesecon:update_autoconnect(np)
|
|
|
|
end
|
2012-12-27 20:38:12 +01:00
|
|
|
return {{pos = np, node = {param2 = 0, name = "air"}}, {pos = pos, node = nn}}
|
2012-06-23 13:24:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function mesecon:mvps_pull_all(pos, direction) -- pos: pos of mvps; direction: direction of pull
|
2013-04-02 11:56:37 +02:00
|
|
|
local lpos = {x=pos.x-direction.x, y=pos.y-direction.y, z=pos.z-direction.z} -- 1 away
|
|
|
|
local lnode = minetest.env:get_node(lpos)
|
|
|
|
local lpos2 = {x=pos.x-direction.x*2, y=pos.y-direction.y*2, z=pos.z-direction.z*2} -- 2 away
|
|
|
|
local lnode2 = minetest.env:get_node(lpos2)
|
|
|
|
|
|
|
|
if lnode.name ~= "ignore" and lnode.name ~= "air" and minetest.registered_nodes[lnode.name].liquidtype == "none" then return end
|
|
|
|
if lnode2.name == "ignore" or lnode2.name == "air" or not(minetest.registered_nodes[lnode2.name].liquidtype == "none") then return end
|
|
|
|
|
|
|
|
local oldpos = {x=lpos2.x+direction.x, y=lpos2.y+direction.y, z=lpos2.z+direction.z}
|
|
|
|
repeat
|
|
|
|
lnode2 = minetest.env:get_node(lpos2)
|
|
|
|
minetest.env:add_node(oldpos, {name=lnode2.name})
|
|
|
|
nodeupdate(oldpos)
|
|
|
|
oldpos = {x=lpos2.x, y=lpos2.y, z=lpos2.z}
|
|
|
|
lpos2.x = lpos2.x-direction.x
|
|
|
|
lpos2.y = lpos2.y-direction.y
|
|
|
|
lpos2.z = lpos2.z-direction.z
|
|
|
|
lnode = minetest.env:get_node(lpos2)
|
|
|
|
until lnode.name=="air" or lnode.name=="ignore" or not(minetest.registered_nodes[lnode2.name].liquidtype == "none")
|
|
|
|
minetest.env:remove_node(oldpos)
|
|
|
|
end
|
|
|
|
|
|
|
|
function mesecon:mvps_move_objects(pos, dir, nodestack)
|
2013-04-02 12:09:59 +02:00
|
|
|
local objects_to_move = {}
|
|
|
|
|
2013-04-02 11:56:37 +02:00
|
|
|
-- Move object at tip of stack
|
|
|
|
local pushpos = mesecon:addPosRule(pos, -- get pos at tip of stack
|
|
|
|
{x = dir.x * (#nodestack),
|
|
|
|
y = dir.y * (#nodestack),
|
|
|
|
z = dir.z * (#nodestack)})
|
|
|
|
|
|
|
|
|
|
|
|
local objects = minetest.env:get_objects_inside_radius(pushpos, 1)
|
|
|
|
for _, obj in ipairs(objects) do
|
2013-04-02 12:09:59 +02:00
|
|
|
table.insert(objects_to_move, obj)
|
2013-04-02 11:56:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Move objects lying/standing on the stack (before it was pushed - oldstack)
|
|
|
|
if tonumber(minetest.setting_get("movement_gravity")) > 0 and dir.y == 0 then
|
|
|
|
-- If gravity positive and dir horizontal, push players standing on the stack
|
|
|
|
for _, n in ipairs(nodestack) do
|
|
|
|
local p_above = mesecon:addPosRule(n.pos, {x=0, y=1, z=0})
|
|
|
|
local objects = minetest.env:get_objects_inside_radius(p_above, 1)
|
|
|
|
for _, obj in ipairs(objects) do
|
2013-04-02 12:09:59 +02:00
|
|
|
table.insert(objects_to_move, obj)
|
2013-04-02 11:56:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-02 12:09:59 +02:00
|
|
|
for _, obj in ipairs(objects_to_move) do
|
2013-04-02 11:56:37 +02:00
|
|
|
local entity = obj:get_luaentity()
|
|
|
|
if not entity or not mesecon:is_mvps_unmov(entity.name) then
|
2013-04-02 12:09:59 +02:00
|
|
|
local np = mesecon:addPosRule(obj:getpos(), dir)
|
|
|
|
local nn = minetest.env:get_node(np)
|
|
|
|
if not minetest.registered_nodes[nn.name].walkable then
|
|
|
|
obj:setpos(np)
|
|
|
|
end
|
2013-04-02 11:56:37 +02:00
|
|
|
end
|
|
|
|
end
|
2012-06-23 13:24:03 +02:00
|
|
|
end
|
|
|
|
|
2012-03-05 19:21:26 +01:00
|
|
|
mesecon:register_mvps_stopper("default:chest_locked")
|
|
|
|
mesecon:register_mvps_stopper("default:furnace")
|