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 --register stoppers for movestones/pistons
mesecon.mvps_stoppers = {} mesecon.mvps_stoppers = {}
-- Register nodes which drop as item when pushed or pulled
mesecon.mvps_droppers = {} mesecon.mvps_droppers = {}
mesecon.on_mvps_move = {}
mesecon.mvps_unmov = {}
function mesecon.is_mvps_stopper(node, pushdir, stack, stackid) --- Objects (entities) that cannot be moved
local get_stopper = mesecon.mvps_stoppers[node.name] function mesecon.register_mvps_unmov(objectname)
if type (get_stopper) == "function" then mesecon.mvps_unmov[objectname] = true;
get_stopper = get_stopper(node, pushdir, stack, stackid)
end
return get_stopper
end end
function mesecon.register_mvps_stopper(nodename, get_stopper) function mesecon.is_mvps_unmov(objectname)
if get_stopper == nil then return mesecon.mvps_unmov[objectname]
get_stopper = true
end
mesecon.mvps_stoppers[nodename] = get_stopper
end end
function mesecon.is_mvps_dropper(node, pushdir, stack, stackid) 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 mesecon.mvps_droppers[nodename] = get_dropper
end end
-- 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_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
core.check_for_falling(n.pos)
mesecon.on_placenode(n.pos, minetest.get_node(n.pos)) mesecon.on_placenode(n.pos, minetest.get_node(n.pos))
mesecon.update_autoconnect(n.pos)
end end
end end
function mesecon.mvps_push(pos, dir, maximum) -- pos: pos of mvps; dir: direction of push; maximum: maximum nodes to be pushed -- tests if the node can be pushed into, e.g. air, water, grass
np = {x = pos.x, y = pos.y, z = pos.z} local function node_replaceable(name)
if name == "ignore" then return true end
if minetest.registered_nodes[name] then
return minetest.registered_nodes[name].buildable_to or false
end
return false
end
function mesecon.mvps_get_stack(pos, dir, maximum, all_pull_sticky)
-- determine the number of nodes to be pushed -- determine the number of nodes to be pushed
local nodes = {} local nodes = {}
while true do local frontiers = {pos}
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
if nn.name == "air" while #frontiers > 0 do
or minetest.registered_nodes[nn.name].liquidtype ~= "none" then --is liquid local np = frontiers[1]
break local nn = minetest.get_node(np)
end
if not node_replaceable(nn.name) then
table.insert(nodes, {node = nn, pos = np}) table.insert(nodes, {node = nn, pos = np})
if #nodes > maximum then return nil end
np = vector.add(np, dir) -- 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 end
-- determine if one of the nodes blocks the push 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 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 return
end end
end end
@ -79,10 +185,10 @@ function mesecon.mvps_push(pos, dir, maximum) -- pos: pos of mvps; dir: directio
-- remove all nodes -- remove all nodes
for id, n in ipairs(nodes) do for id, n in ipairs(nodes) do
n.meta = minetest.get_meta(n.pos):to_table() 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 if is_dropper then
local drops = minetest.get_node_drops(n.node.name, "") 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) minetest.handle_node_drops(droppos, drops, nil)
end end
minetest.remove_node(n.pos) minetest.remove_node(n.pos)
@ -98,7 +204,6 @@ function mesecon.mvps_push(pos, dir, maximum) -- pos: pos of mvps; dir: directio
break break
end end
mesecon.on_dignode(n.pos, n.node) mesecon.on_dignode(n.pos, n.node)
mesecon.update_autoconnect(n.pos)
end end
-- add nodes -- 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 if first_dropper and id >= first_dropper then
break break
end end
np = vector.add(n.pos, dir) np = vector.add(n.pos, movedir)
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
@ -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 if first_dropper and i >= first_dropper then
break break
end end
nodes[i].pos = vector.add(nodes[i].pos, dir) nodes[i].pos = vector.add(nodes[i].pos, movedir)
end end
return true, nodes 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 end
function mesecon.mvps_pull_single(pos, dir) -- pos: pos of mvps; direction: direction of pull (matches push direction for sticky pistons) on_mvps_move(moved_nodes)
np = vector.add(pos, dir)
nn = minetest.get_node(np)
if minetest.registered_nodes[nn.name].liquidtype == "none" return true, nodes, oldstack
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)
end
return {{pos = np, node = {param2 = 0, name = "air"}}, {pos = pos, node = nn}}
end end
function mesecon.mvps_pull_all(pos, direction) -- pos: pos of mvps; direction: direction of pull mesecon.register_on_mvps_move(function(moved_nodes)
local lpos = {x=pos.x-direction.x, y=pos.y-direction.y, z=pos.z-direction.z} -- 1 away for _, n in ipairs(moved_nodes) do
local lnode = minetest.get_node(lpos) mesecon.on_placenode(n.pos, n.node)
local lpos2 = {x=pos.x-direction.x*2, y=pos.y-direction.y*2, z=pos.z-direction.z*2} -- 2 away end
local lnode2 = minetest.get_node(lpos2) end)
if lnode.name ~= "ignore" and lnode.name ~= "air" and minetest.registered_nodes[lnode.name].liquidtype == "none" then return end function mesecon.mvps_move_objects(pos, dir, nodestack)
if lnode2.name == "ignore" or lnode2.name == "air" or not(minetest.registered_nodes[lnode2.name].liquidtype == "none") then return end local objects_to_move = {}
local oldpos = {x=lpos2.x+direction.x, y=lpos2.y+direction.y, z=lpos2.z+direction.z} -- Move object at tip of stack, pushpos is position at tip of stack
repeat local pushpos = vector.add(pos, vector.multiply(dir, #nodestack))
lnode2 = minetest.get_node(lpos2)
minetest.add_node(oldpos, {name=lnode2.name}) local objects = minetest.get_objects_inside_radius(pushpos, 1)
core.check_for_falling(oldpos) for _, obj in ipairs(objects) do
oldpos = {x=lpos2.x, y=lpos2.y, z=lpos2.z} table.insert(objects_to_move, obj)
lpos2.x = lpos2.x-direction.x end
lpos2.y = lpos2.y-direction.y
lpos2.z = lpos2.z-direction.z -- Move objects lying/standing on the stack (before it was pushed - oldstack)
lnode = minetest.get_node(lpos2) if tonumber(minetest.setting_get("movement_gravity")) > 0 and dir.y == 0 then
until lnode.name=="air" or lnode.name=="ignore" or not(minetest.registered_nodes[lnode2.name].liquidtype == "none") -- If gravity positive and dir horizontal, push players standing on the stack
minetest.remove_node(oldpos) 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 end
mesecon.register_mvps_stopper("mcl_core:obsidian") 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_solarpanel:solar_panel_inverted_on")
mesecon.register_mvps_stopper("mesecons_noteblock:noteblock") mesecon.register_mvps_stopper("mesecons_noteblock:noteblock")
mesecon.register_mvps_stopper("3d_armor_stand:armor_stand") 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 piston_on = function (pos, node)
local pistonspec = minetest.registered_nodes[node.name].mesecons_piston 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) 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 if success then
minetest.add_node(pos, {param2 = node.param2, name = pistonspec.onname}) minetest.add_node(pos, {param2 = node.param2, name = pistonspec.onname})
minetest.add_node(np, {param2 = node.param2, name = pistonspec.pusher}) 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"}) minetest.set_node({x=np.x,y=np.y-1,z=np.z}, {name = "mcl_core:dirt"})
end end
mesecon.mvps_process_stack(stack) mesecon.mvps_process_stack(stack)
mesecon.mvps_move_objects(np, dir, oldstack)
minetest.sound_play("piston_extend", { minetest.sound_play("piston_extend", {
pos = pos, pos = pos,
max_hear_distance = 20, max_hear_distance = 20,
@ -102,10 +103,10 @@ local piston_off = function (pos, node)
piston_remove_pusher (pos, node) piston_remove_pusher (pos, node)
if pistonspec.sticky then if pistonspec.sticky then
dir = piston_get_direction(pistonspec.dir, node) local dir = piston_get_direction(pistonspec.dir, node)
pullpos = vector.add(pos, dir) local pullpos = vector.add(pos, vector.multiply(dir, 2))
stack = mesecon.mvps_pull_single(pullpos, dir) local stack = mesecon.mvps_pull_single(pullpos, vector.multiply(dir, -1), PISTON_MAXIMUM_PUSH)
mesecon.mvps_process_stack(stack) mesecon.mvps_process_stack(pos, dir, stack)
end end
end end