Rewrite movestones, make them nodes instead of entities

This commit is contained in:
Jeija 2015-02-14 11:54:27 +01:00
parent b7076e635c
commit 1df6e5a03b
3 changed files with 90 additions and 209 deletions

@ -9,9 +9,7 @@
-- Pull all blocks in its back
function mesecon.get_movestone_direction(pos)
getactivated = 0
local lpos
local getactivated = 0
local rules = {
{x=0, y=1, z=-1},
{x=0, y=0, z=-1},
@ -55,75 +53,75 @@ function mesecon.get_movestone_direction(pos)
end
end
minetest.register_node("mesecons_movestones:movestone", {
tiles = {"jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_arrows.png", "jeija_movestone_arrows.png"},
paramtype2 = "facedir",
legacy_facedir_simple = true,
groups = {cracky=3},
description="Movestone",
sounds = default.node_sound_stone_defaults(),
mesecons = {effector = {
action_on = function (pos, node)
function mesecon.register_movestone(name, def, is_sticky)
local timer_interval = 1 / mesecon.setting("movestone_speed", 3)
local name_active = name.."_active"
local function movestone_move (pos)
if minetest.get_node(pos).name ~= name_active then
return
end
local direction = mesecon.get_movestone_direction(pos)
if not direction then return end
local frontpos = vector.add(pos, direction)
local backpos = vector.subtract(pos, direction)
-- ### Step 1: Push nodes in front ###
local maxpush = mesecon.setting("movestone_max_push", 50)
local maxpull = mesecon.setting("movestone_max_pull", 50)
local success, stack, oldstack = mesecon.mvps_push(frontpos, direction, maxpush)
if success then
mesecon.mvps_process_stack(stack)
mesecon.mvps_move_objects(frontpos, direction, oldstack)
-- Too large stack/stopper in the way: try again very soon
else
minetest.after(0.05, movestone_move, pos)
return
end
-- ### Step 2: Move the movestone ###
local node = minetest.get_node(pos)
minetest.set_node(frontpos, node)
minetest.remove_node(pos)
mesecon.on_dignode(pos, node)
minetest.add_entity(pos, "mesecons_movestones:movestone_entity")
mesecon.on_placenode(frontpos, node)
minetest.after(timer_interval, movestone_move, frontpos)
-- ### Step 3: If sticky, pull stack behind ###
if is_sticky then
mesecon.mvps_pull_all(backpos, direction, maxpull)
end
end
def.mesecons = {effector = {
action_on = function (pos)
if minetest.get_node(pos).name ~= name_active then
minetest.set_node(pos, {name = name_active})
movestone_move(pos)
end
end,
action_off = function (pos)
minetest.set_node(pos, {name = name})
end
}}
})
minetest.register_entity("mesecons_movestones:movestone_entity", {
physical = false,
visual = "sprite",
textures = {"jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_arrows.png", "jeija_movestone_arrows.png"},
collisionbox = {-0.5,-0.5,-0.5, 0.5, 0.5, 0.5},
visual = "cube",
lastdir = {x=0, y=0, z=0},
def.drop = name
on_punch = function(self, hitter)
self.object:remove()
hitter:get_inventory():add_item("main", "mesecons_movestones:movestone")
end,
minetest.register_node(name, def)
on_step = function(self, dtime)
local pos = self.object:getpos()
pos.x, pos.y, pos.z = math.floor(pos.x+0.5), math.floor(pos.y+0.5), math.floor(pos.z+0.5)
local direction = mesecon.get_movestone_direction(pos)
local maxpush = mesecon.setting("movestone_max_push", 50)
if not direction then -- no mesecon power
--push only solid nodes
local name = minetest.get_node(pos).name
if name ~= "air" and name ~= "ignore"
and ((not minetest.registered_nodes[name])
or minetest.registered_nodes[name].liquidtype == "none") then
mesecon.mvps_push(pos, self.lastdir, maxpush)
end
local nn = {name="mesecons_movestones:movestone"}
minetest.add_node(pos, nn)
self.object:remove()
mesecon.on_placenode(pos, nn)
return
-- active node only
local def_active = table.copy(def)
def_active.groups.not_in_creative_inventory = 1
minetest.register_node(name_active, def_active)
end
local success, stack, oldstack =
mesecon.mvps_push(pos, direction, maxpush)
if not success then -- Too large stack/stopper in the way
local nn = {name="mesecons_movestones:movestone"}
minetest.add_node(pos, nn)
self.object:remove()
mesecon.on_placenode(pos, nn)
return
else
mesecon.mvps_process_stack (stack)
mesecon.mvps_move_objects (pos, direction, oldstack)
self.lastdir = direction
end
self.object:setvelocity({x=direction.x*2, y=direction.y*2, z=direction.z*2})
end,
})
mesecon.register_movestone("mesecons_movestones:movestone", {
tiles = {"jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_arrows.png", "jeija_movestone_arrows.png"},
groups = {cracky=3},
description="Movestone",
sounds = default.node_sound_stone_defaults()
}, false)
minetest.register_craft({
output = "mesecons_movestones:movestone 2",
@ -134,28 +132,14 @@ minetest.register_craft({
}
})
-- STICKY_MOVESTONE
minetest.register_node("mesecons_movestones:sticky_movestone", {
mesecon.register_movestone("mesecons_movestones:sticky_movestone", {
tiles = {"jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_sticky_movestone.png", "jeija_sticky_movestone.png"},
inventory_image = minetest.inventorycube("jeija_sticky_movestone.png", "jeija_movestone_side.png", "jeija_movestone_side.png"),
paramtype2 = "facedir",
legacy_facedir_simple = true,
groups = {cracky=3},
description="Sticky Movestone",
sounds = default.node_sound_stone_defaults(),
mesecons = {effector = {
action_on = function (pos, node)
local direction=mesecon.get_movestone_direction(pos)
if not direction then return end
minetest.remove_node(pos)
mesecon.on_dignode(pos, node)
minetest.add_entity(pos, "mesecons_movestones:sticky_movestone_entity")
end
}}
})
}, true)
minetest.register_craft({
output = "mesecons_movestones:sticky_movestone 2",
@ -164,63 +148,6 @@ minetest.register_craft({
}
})
minetest.register_entity("mesecons_movestones:sticky_movestone_entity", {
physical = false,
visual = "sprite",
textures = {"jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_sticky_movestone.png", "jeija_sticky_movestone.png"},
collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
visual = "cube",
lastdir = {x=0, y=0, z=0},
on_punch = function(self, hitter)
self.object:remove()
hitter:get_inventory():add_item("main", 'mesecons_movestones:sticky_movestone')
end,
on_step = function(self, dtime)
local pos = self.object:getpos()
pos.x, pos.y, pos.z = math.floor(pos.x+0.5), math.floor(pos.y+0.5), math.floor(pos.z+0.5)
local direction = mesecon.get_movestone_direction(pos)
if not direction then -- no mesecon power
--push only solid nodes
local name = minetest.get_node(pos).name
if name ~= "air" and name ~= "ignore"
and ((not minetest.registered_nodes[name])
or minetest.registered_nodes[name].liquidtype == "none") then
mesecon.mvps_push(pos, self.lastdir,
mesecon.setting("movestone_max_push", 50))
--STICKY
mesecon.mvps_pull_all(pos, self.lastdir)
end
local nn = {name="mesecons_movestones:sticky_movestone"}
minetest.add_node(pos, nn)
self.object:remove()
mesecon.on_placenode(pos, nn)
return
end
local success, stack, oldstack =
mesecon.mvps_push(pos, direction, mesecon.setting("movestone_max_push", 50))
if not success then -- Too large stack/stopper in the way
local nn = {name="mesecons_movestones:sticky_movestone"}
minetest.add_node(pos, nn)
self.object:remove()
mesecon.on_placenode(pos, nn)
return
else
mesecon.mvps_process_stack (stack)
mesecon.mvps_move_objects (pos, direction, oldstack)
self.lastdir = direction
end
self.object:setvelocity({x=direction.x*2, y=direction.y*2, z=direction.z*2})
--STICKY
mesecon.mvps_pull_all(pos, direction)
end,
})
mesecon.register_mvps_unmov("mesecons_movestones:movestone_entity")
mesecon.register_mvps_unmov("mesecons_movestones:sticky_movestone_entity")
-- Don't allow pushing movestones while they're active
mesecon.register_mvps_stopper("mesecons_movestones:movestone_active")
mesecon.register_mvps_stopper("mesecons_movestones:sticky_movestone_active")

@ -1,7 +1,6 @@
--register stoppers for movestones/pistons
mesecon.mvps_stoppers = {}
mesecon.mvps_unmov = {}
mesecon.on_mvps_move = {}
function mesecon.is_mvps_stopper(node, pushdir, stack, stackid)
@ -9,6 +8,7 @@ function mesecon.is_mvps_stopper(node, pushdir, stack, stackid)
if type (get_stopper) == "function" then
get_stopper = get_stopper(node, pushdir, stack, stackid)
end
if (get_stopper) then print(node.name) end
return get_stopper
end
@ -19,15 +19,6 @@ function mesecon.register_mvps_stopper(nodename, get_stopper)
mesecon.mvps_stoppers[nodename] = get_stopper
end
-- 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
-- Functions to be called on mvps movement
function mesecon.register_on_mvps_move(callback)
mesecon.on_mvps_move[#mesecon.on_mvps_move+1] = callback
@ -70,13 +61,21 @@ function mesecon.mvps_get_stack(pos, dir, maximum)
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)
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)
end
function mesecon.mvps_push_or_pull(pos, stackdir, movedir, maximum) -- pos: pos of mvps; stackdir: direction of building the stack; movedir: direction of actual movement; maximum: maximum nodes to be pushed
local nodes = mesecon.mvps_get_stack(pos, stackdir, maximum)
if not nodes then return end
-- determine if one of the nodes blocks the push
-- 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
@ -94,7 +93,8 @@ function mesecon.mvps_push(pos, dir, maximum) -- pos: pos of mvps; dir: directio
-- add nodes
for _, n in ipairs(nodes) do
local np = mesecon.addPosRule(n.pos, dir)
local np = mesecon.addPosRule(n.pos, movedir)
minetest.add_node(np, n.node)
minetest.get_meta(np):from_table(n.meta)
end
@ -104,7 +104,7 @@ function mesecon.mvps_push(pos, dir, maximum) -- pos: pos of mvps; dir: directio
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, movedir)
moved_nodes[i].pos = nodes[i].pos
moved_nodes[i].node = nodes[i].node
moved_nodes[i].meta = nodes[i].meta
@ -128,7 +128,7 @@ function mesecon.mvps_pull_single(pos, dir) -- pos: pos of mvps; direction: dire
if ((not minetest.registered_nodes[nn.name]) --unregistered node
or minetest.registered_nodes[nn.name].liquidtype == "none") --non-liquid node
and not mesecon.is_mvps_stopper(nn, {x = -dir.x, y = -dir.y, z = -dir.z}, {{pos = np, node = nn}}, 1) then --non-stopper node
and not mesecon.is_mvps_stopper(nn, dir, {{pos = np, node = nn}}, 1) then --non-stopper node
local meta = minetest.get_meta(np):to_table()
minetest.remove_node(np)
minetest.add_node(pos, nn)
@ -143,52 +143,6 @@ function mesecon.mvps_pull_single(pos, dir) -- pos: pos of mvps; direction: dire
return {{pos = np, node = {param2 = 0, name = "air"}}, {pos = pos, node = nn}}
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)
--avoid pulling solid nodes
if lnode.name ~= "ignore"
and lnode.name ~= "air"
and ((not minetest.registered_nodes[lnode.name])
or minetest.registered_nodes[lnode.name].liquidtype == "none") then
return
end
--avoid pulling empty or liquid nodes
if lnode2.name == "ignore"
or lnode2.name == "air"
or (minetest.registered_nodes[lnode2.name]
and minetest.registered_nodes[lnode2.name].liquidtype ~= "none") then
return
end
local moved_nodes = {}
local oldpos = {x=lpos2.x + direction.x, y=lpos2.y + direction.y, z=lpos2.z + direction.z}
repeat
lnode2 = minetest.get_node(lpos2)
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 = lpos2, node = lnode2, meta = meta}
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.get_node(lpos2)
until lnode.name == "air"
or lnode.name == "ignore"
or (minetest.registered_nodes[lnode.name]
and minetest.registered_nodes[lnode.name].liquidtype ~= "none")
minetest.remove_node(oldpos)
mesecon.on_dignode(oldpos, lnode2)
mesecon.update_autoconnect(oldpos)
on_mvps_move(moved_nodes)
end
function mesecon.mvps_move_objects(pos, dir, nodestack)
local objects_to_move = {}
@ -218,7 +172,7 @@ function mesecon.mvps_move_objects(pos, dir, nodestack)
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
if not entity then
local np = mesecon.addPosRule(obj:getpos(), dir)
--move only if destination is not solid