Update mesecons_mvps

This commit is contained in:
Wuzzy 2017-07-31 00:38:50 +02:00
parent b299b95fac
commit 29037a05a6
2 changed files with 213 additions and 91 deletions

View file

@ -1,22 +1,17 @@
-- Register stoppers for pistons
mesecon.mvps_stoppers={}
--register stoppers for movestones/pistons
-- Register nodes which drop as item when pushed or pulled
mesecon.mvps_droppers={}
mesecon.mvps_stoppers = {}
mesecon.mvps_droppers = {}
mesecon.on_mvps_move = {}
mesecon.mvps_unmov = {}
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
--- Objects (entities) that cannot be moved
function mesecon.register_mvps_unmov(objectname)
mesecon.mvps_unmov[objectname] = true;
end
function mesecon.register_mvps_stopper(nodename, get_stopper)
if get_stopper == nil then
get_stopper = true
end
mesecon.mvps_stoppers[nodename] = get_stopper
function mesecon.is_mvps_unmov(objectname)
return mesecon.mvps_unmov[objectname]
end
function mesecon.is_mvps_dropper(node, pushdir, stack, stackid)
@ -37,40 +32,151 @@ function mesecon.register_mvps_dropper(nodename, get_dropper)
mesecon.mvps_droppers[nodename] = get_dropper
end
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
core.check_for_falling(n.pos)
mesecon.on_placenode(n.pos, minetest.get_node(n.pos))
mesecon.update_autoconnect(n.pos)
-- Nodes that cannot be pushed / pulled by movestones, pistons
function mesecon.is_mvps_stopper(node, pushdir, stack, stackid)
-- unknown nodes are always stoppers
if not minetest.registered_nodes[node.name] then
return true
end
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
end
function mesecon.register_mvps_stopper(nodename, get_stopper)
if get_stopper == nil then
get_stopper = true
end
mesecon.mvps_stoppers[nodename] = get_stopper
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_push(pos, dir, maximum) -- pos: pos of mvps; dir: direction of push; maximum: maximum nodes to be pushed
np = {x = pos.x, y = pos.y, z = pos.z}
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
mesecon.on_placenode(n.pos, minetest.get_node(n.pos))
end
end
-- determine the number of nodes to be pushed
local nodes = {}
while true do
nn = minetest.get_node_or_nil(np)
if not nn or #nodes > maximum then
-- don't push at all, something is in the way (unloaded map or too many nodes)
return
end
-- tests if the node can be pushed into, e.g. air, water, grass
local function node_replaceable(name)
if name == "ignore" then return true 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 = vector.add(np, dir)
if minetest.registered_nodes[name] then
return minetest.registered_nodes[name].buildable_to or false
end
-- determine if one of the nodes blocks the push
return false
end
function mesecon.mvps_get_stack(pos, dir, maximum, all_pull_sticky)
-- determine the number of nodes to be pushed
local nodes = {}
local frontiers = {pos}
while #frontiers > 0 do
local np = frontiers[1]
local nn = minetest.get_node(np)
if not node_replaceable(nn.name) then
table.insert(nodes, {node = nn, pos = np})
if #nodes > maximum then return nil end
-- add connected nodes to frontiers, connected is a vector list
-- the vectors must be absolute positions
local connected = {}
if minetest.registered_nodes[nn.name]
and minetest.registered_nodes[nn.name].mvps_sticky then
connected = minetest.registered_nodes[nn.name].mvps_sticky(np, nn)
end
table.insert(connected, vector.add(np, dir))
-- If adjacent node is sticky block and connects add that
-- position to the connected table
for _, r in ipairs(mesecon.rules.alldirs) do
local adjpos = vector.add(np, r)
local adjnode = minetest.get_node(adjpos)
if minetest.registered_nodes[adjnode.name]
and minetest.registered_nodes[adjnode.name].mvps_sticky then
local sticksto = minetest.registered_nodes[adjnode.name]
.mvps_sticky(adjpos, adjnode)
-- connects to this position?
for _, link in ipairs(sticksto) do
if vector.equals(link, np) then
table.insert(connected, adjpos)
end
end
end
end
if all_pull_sticky then
table.insert(connected, vector.subtract(np, dir))
end
-- Make sure there are no duplicates in frontiers / nodes before
-- adding nodes in "connected" to frontiers
for _, cp in ipairs(connected) do
local duplicate = false
for _, rp in ipairs(nodes) do
if vector.equals(cp, rp.pos) then
duplicate = true
end
end
for _, fp in ipairs(frontiers) do
if vector.equals(cp, fp) then
duplicate = true
end
end
if not duplicate then
table.insert(frontiers, cp)
end
end
end
table.remove(frontiers, 1)
end
return nodes
end
function mesecon.mvps_push(pos, dir, maximum)
return mesecon.mvps_push_or_pull(pos, dir, dir, maximum)
end
function mesecon.mvps_pull_all(pos, dir, maximum)
return mesecon.mvps_push_or_pull(pos, vector.multiply(dir, -1), dir, maximum, true)
end
function mesecon.mvps_pull_single(pos, dir, maximum)
return mesecon.mvps_push_or_pull(pos, vector.multiply(dir, -1), dir, maximum)
end
-- pos: pos of mvps; stackdir: direction of building the stack
-- movedir: direction of actual movement
-- maximum: maximum nodes to be pushed
-- all_pull_sticky: All nodes are sticky in the direction that they are pulled from
function mesecon.mvps_push_or_pull(pos, stackdir, movedir, maximum, all_pull_sticky)
local nodes = mesecon.mvps_get_stack(pos, movedir, maximum, all_pull_sticky)
if not nodes then return end
-- determine if one of the nodes blocks the push / pull
for id, n in ipairs(nodes) do
if mesecon.is_mvps_stopper(n.node, dir, nodes, id) then
if mesecon.is_mvps_stopper(n.node, movedir, nodes, id) then
return
end
end
@ -79,10 +185,10 @@ function mesecon.mvps_push(pos, dir, maximum) -- pos: pos of mvps; dir: directio
-- remove all nodes
for id, n in ipairs(nodes) do
n.meta = minetest.get_meta(n.pos):to_table()
local is_dropper = mesecon.is_mvps_dropper(n.node, dir, nodes, id)
local is_dropper = mesecon.is_mvps_dropper(n.node, movedir, nodes, id)
if is_dropper then
local drops = minetest.get_node_drops(n.node.name, "")
local droppos = vector.add(n.pos, dir)
local droppos = vector.add(n.pos, movedir)
minetest.handle_node_drops(droppos, drops, nil)
end
minetest.remove_node(n.pos)
@ -98,7 +204,6 @@ function mesecon.mvps_push(pos, dir, maximum) -- pos: pos of mvps; dir: directio
break
end
mesecon.on_dignode(n.pos, n.node)
mesecon.update_autoconnect(n.pos)
end
-- add nodes
@ -106,7 +211,7 @@ function mesecon.mvps_push(pos, dir, maximum) -- pos: pos of mvps; dir: directio
if first_dropper and id >= first_dropper then
break
end
np = vector.add(n.pos, dir)
np = vector.add(n.pos, movedir)
minetest.add_node(np, n.node)
minetest.get_meta(np):from_table(n.meta)
end
@ -115,53 +220,67 @@ function mesecon.mvps_push(pos, dir, maximum) -- pos: pos of mvps; dir: directio
if first_dropper and i >= first_dropper then
break
end
nodes[i].pos = vector.add(nodes[i].pos, dir)
nodes[i].pos = vector.add(nodes[i].pos, movedir)
end
return true, nodes
end
function mesecon.mvps_pull_single(pos, dir) -- pos: pos of mvps; direction: direction of pull (matches push direction for sticky pistons)
np = vector.add(pos, dir)
nn = minetest.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)
and not mesecon.is_mvps_dropper(nn, {x = -dir.x, y = -dir.y, z = -dir.z}, {{pos = np, node = nn}}, 1) then
local meta = minetest.get_meta(np):to_table()
minetest.remove_node(np)
minetest.add_node(pos, nn)
minetest.get_meta(pos):from_table(meta)
core.check_for_falling(np)
core.check_for_falling(pos)
mesecon.on_dignode(np, nn)
mesecon.update_autoconnect(np)
local moved_nodes = {}
local oldstack = mesecon.tablecopy(nodes)
for i in ipairs(nodes) do
moved_nodes[i] = {}
moved_nodes[i].oldpos = nodes[i].pos
nodes[i].pos = vector.add(nodes[i].pos, movedir)
moved_nodes[i].pos = nodes[i].pos
moved_nodes[i].node = nodes[i].node
moved_nodes[i].meta = nodes[i].meta
end
return {{pos = np, node = {param2 = 0, name = "air"}}, {pos = pos, node = nn}}
on_mvps_move(moved_nodes)
return true, nodes, oldstack
end
function mesecon.mvps_pull_all(pos, direction) -- pos: pos of mvps; direction: direction of pull
local lpos = {x=pos.x-direction.x, y=pos.y-direction.y, z=pos.z-direction.z} -- 1 away
local lnode = minetest.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.get_node(lpos2)
mesecon.register_on_mvps_move(function(moved_nodes)
for _, n in ipairs(moved_nodes) do
mesecon.on_placenode(n.pos, n.node)
end
end)
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
function mesecon.mvps_move_objects(pos, dir, nodestack)
local objects_to_move = {}
local oldpos = {x=lpos2.x+direction.x, y=lpos2.y+direction.y, z=lpos2.z+direction.z}
repeat
lnode2 = minetest.get_node(lpos2)
minetest.add_node(oldpos, {name=lnode2.name})
core.check_for_falling(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.get_node(lpos2)
until lnode.name=="air" or lnode.name=="ignore" or not(minetest.registered_nodes[lnode2.name].liquidtype == "none")
minetest.remove_node(oldpos)
-- Move object at tip of stack, pushpos is position at tip of stack
local pushpos = vector.add(pos, vector.multiply(dir, #nodestack))
local objects = minetest.get_objects_inside_radius(pushpos, 1)
for _, obj in ipairs(objects) do
table.insert(objects_to_move, obj)
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 = vector.add(n.pos, {x=0, y=1, z=0})
local objects = minetest.get_objects_inside_radius(p_above, 1)
for _, obj in ipairs(objects) do
table.insert(objects_to_move, obj)
end
end
end
for _, obj in ipairs(objects_to_move) do
local entity = obj:get_luaentity()
if not entity or not mesecon.is_mvps_unmov(entity.name) then
local np = vector.add(obj:getpos(), dir)
--move only if destination is not solid
local nn = minetest.get_node(np)
if not ((not minetest.registered_nodes[nn.name])
or minetest.registered_nodes[nn.name].walkable) then
obj:setpos(np)
end
end
end
end
mesecon.register_mvps_stopper("mcl_core:obsidian")
@ -203,3 +322,5 @@ mesecon.register_mvps_stopper("mesecons_solarpanel:solar_panel_inverted_off")
mesecon.register_mvps_stopper("mesecons_solarpanel:solar_panel_inverted_on")
mesecon.register_mvps_stopper("mesecons_noteblock:noteblock")
mesecon.register_mvps_stopper("3d_armor_stand:armor_stand")
mesecon.register_on_mvps_move(mesecon.move_hot_nodes)

View file

@ -77,9 +77,9 @@ end
local piston_on = function (pos, node)
local pistonspec = minetest.registered_nodes[node.name].mesecons_piston
dir = piston_get_direction(pistonspec.dir, node)
local dir = piston_get_direction(pistonspec.dir, node)
local np = vector.add(pos, dir)
success, stack = mesecon.mvps_push(np, dir, PISTON_MAXIMUM_PUSH)
local success, stack, oldstack = mesecon.mvps_push(np, dir, PISTON_MAXIMUM_PUSH)
if success then
minetest.add_node(pos, {param2 = node.param2, name = pistonspec.onname})
minetest.add_node(np, {param2 = node.param2, name = pistonspec.pusher})
@ -88,6 +88,7 @@ local piston_on = function (pos, node)
minetest.set_node({x=np.x,y=np.y-1,z=np.z}, {name = "mcl_core:dirt"})
end
mesecon.mvps_process_stack(stack)
mesecon.mvps_move_objects(np, dir, oldstack)
minetest.sound_play("piston_extend", {
pos = pos,
max_hear_distance = 20,
@ -102,10 +103,10 @@ local piston_off = function (pos, node)
piston_remove_pusher (pos, node)
if pistonspec.sticky then
dir = piston_get_direction(pistonspec.dir, node)
pullpos = vector.add(pos, dir)
stack = mesecon.mvps_pull_single(pullpos, dir)
mesecon.mvps_process_stack(stack)
local dir = piston_get_direction(pistonspec.dir, node)
local pullpos = vector.add(pos, vector.multiply(dir, 2))
local stack = mesecon.mvps_pull_single(pullpos, vector.multiply(dir, -1), PISTON_MAXIMUM_PUSH)
mesecon.mvps_process_stack(pos, dir, stack)
end
end