Improve movestones (#366)

Improve the code and add vertical movestones
This commit is contained in:
DS 2017-10-08 18:41:22 +02:00 committed by Vitaliy
parent b08e93fc8b
commit 7094f0b606

@ -8,126 +8,151 @@
-- Pushes all block in front of it -- Pushes all block in front of it
-- Pull all blocks in its back -- Pull all blocks in its back
function mesecon.get_movestone_direction(pos) -- settings:
local lpos local timer_interval = 1 / mesecon.setting("movestone_speed", 3)
local rules = { local max_push = mesecon.setting("movestone_max_push", 50)
{x=0, y=1, z=-1}, local max_pull = mesecon.setting("movestone_max_pull", 50)
{x=0, y=0, z=-1},
{x=0, y=-1, z=-1},
{x=0, y=1, z=1},
{x=0, y=-1, z=1},
{x=0, y=0, z=1},
{x=1, y=0, z=0},
{x=1, y=1, z=0},
{x=1, y=-1, z=0},
{x=-1, y=1, z=0},
{x=-1, y=-1, z=0},
{x=-1, y=0, z=0}}
lpos = {x=pos.x+1, y=pos.y, z=pos.z} -- helper functions:
for n = 1, 3 do local function get_movestone_direction(rulename, is_vertical)
if mesecon.is_power_on(lpos, rules[n]) then if is_vertical then
return {x=0, y=0, z=-1} if rulename.z > 0 then
return {x = 0, y = -1, z = 0}
elseif rulename.z < 0 then
return {x = 0, y = 1, z = 0}
elseif rulename.x > 0 then
return {x = 0, y = -1, z = 0}
elseif rulename.x < 0 then
return {x = 0, y = 1, z = 0}
end end
end else
if rulename.z > 0 then
lpos = {x = pos.x-1, y = pos.y, z = pos.z} return {x = -1, y = 0, z = 0}
for n=4, 6 do elseif rulename.z < 0 then
if mesecon.is_power_on(lpos, rules[n]) then return {x = 1, y = 0, z = 0}
return {x=0, y=0, z=1} elseif rulename.x > 0 then
end return {x = 0, y = 0, z = -1}
end elseif rulename.x < 0 then
return {x = 0, y = 0, z = 1}
lpos = {x = pos.x, y = pos.y, z = pos.z+1}
for n=7, 9 do
if mesecon.is_power_on(lpos, rules[n]) then
return {x=-1, y=0, z=0}
end
end
lpos = {x = pos.x, y = pos.y, z = pos.z-1}
for n=10, 12 do
if mesecon.is_power_on(lpos, rules[n]) then
return {x=1, y=0, z=0}
end end
end end
end end
function mesecon.register_movestone(name, def, is_sticky) -- registration functions:
local timer_interval = 1 / mesecon.setting("movestone_speed", 3) function mesecon.register_movestone(name, def, is_sticky, is_vertical)
local name_active = name.."_active" local function movestone_move(pos, node, rulename)
local direction = get_movestone_direction(rulename, is_vertical)
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
minetest.set_node(pos, {name = name})
return
end
local frontpos = vector.add(pos, direction) local frontpos = vector.add(pos, direction)
local backpos = vector.subtract(pos, direction)
-- ### Step 1: Push nodes in front ### -- ### Step 1: Push nodes in front ###
local maxpush = mesecon.setting("movestone_max_push", 50) local success, stack, oldstack = mesecon.mvps_push(frontpos, direction, max_push)
local maxpull = mesecon.setting("movestone_max_pull", 50) if not success then
local success, stack, oldstack = mesecon.mvps_push(frontpos, direction, maxpush) minetest.get_node_timer(pos):start(timer_interval)
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 return
end end
mesecon.mvps_process_stack(stack)
mesecon.mvps_move_objects(frontpos, direction, oldstack)
-- ### Step 2: Move the movestone ### -- ### Step 2: Move the movestone ###
local node = minetest.get_node(pos)
minetest.set_node(frontpos, node) minetest.set_node(frontpos, node)
minetest.remove_node(pos) minetest.remove_node(pos)
mesecon.on_dignode(pos, node) mesecon.on_dignode(pos, node)
mesecon.on_placenode(frontpos, node) mesecon.on_placenode(frontpos, node)
minetest.after(timer_interval, movestone_move, frontpos) minetest.get_node_timer(frontpos):start(timer_interval)
-- ### Step 3: If sticky, pull stack behind ### -- ### Step 3: If sticky, pull stack behind ###
if is_sticky then if is_sticky then
mesecon.mvps_pull_all(backpos, direction, maxpull) local backpos = vector.subtract(pos, direction)
mesecon.mvps_pull_all(backpos, direction, max_pull)
end end
end end
def.mesecons = {effector = { def.mesecons = {effector = {
action_on = function (pos) action_on = function(pos, node, rulename)
if minetest.get_node(pos).name ~= name_active then if rulename and not minetest.get_node_timer(pos):is_started() then
minetest.set_node(pos, {name = name_active}) movestone_move(pos, node, rulename)
movestone_move(pos)
end end
end, end,
action_off = function (pos) rules = mesecon.rules.default,
minetest.set_node(pos, {name = name})
end
}} }}
def.drop = name def.on_timer = function(pos, elapsed)
local sourcepos = mesecon.is_powered(pos)
if not sourcepos then
return
end
local rulename = vector.subtract(sourcepos[1], pos)
mesecon.activate(pos, minetest.get_node(pos), rulename, 0)
end
def.on_blast = mesecon.on_blastnode
def.on_blast = mesecon.on_blastnode def.on_blast = mesecon.on_blastnode
minetest.register_node(name, def) minetest.register_node(name, def)
-- 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 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)
-- registration:
mesecon.register_movestone("mesecons_movestones:movestone", {
tiles = {
"jeija_movestone_side.png",
"jeija_movestone_side.png",
"jeija_movestone_arrows.png^[transformFX",
"jeija_movestone_arrows.png^[transformFX",
"jeija_movestone_arrows.png",
"jeija_movestone_arrows.png",
},
groups = {cracky = 3},
description = "Movestone",
sounds = default.node_sound_stone_defaults()
}, false, false)
mesecon.register_movestone("mesecons_movestones:sticky_movestone", {
tiles = {
"jeija_movestone_side.png",
"jeija_movestone_side.png",
"jeija_sticky_movestone.png^[transformFX",
"jeija_sticky_movestone.png^[transformFX",
"jeija_sticky_movestone.png",
"jeija_sticky_movestone.png",
},
groups = {cracky = 3},
description = "Sticky Movestone",
sounds = default.node_sound_stone_defaults(),
}, true, false)
mesecon.register_movestone("mesecons_movestones:movestone_vertical", {
tiles = {
"jeija_movestone_side.png",
"jeija_movestone_side.png",
"jeija_movestone_arrows.png^[transformFXR90",
"jeija_movestone_arrows.png^[transformR90",
"jeija_movestone_arrows.png^[transformFXR90",
"jeija_movestone_arrows.png^[transformR90",
},
groups = {cracky = 3},
description = "Vertical Movestone",
sounds = default.node_sound_stone_defaults()
}, false, true)
mesecon.register_movestone("mesecons_movestones:sticky_movestone_vertical", {
tiles = {
"jeija_movestone_side.png",
"jeija_movestone_side.png",
"jeija_sticky_movestone.png^[transformFXR90",
"jeija_sticky_movestone.png^[transformR90",
"jeija_sticky_movestone.png^[transformFXR90",
"jeija_sticky_movestone.png^[transformR90",
},
groups = {cracky = 3},
description = "Vertical Sticky Movestone",
sounds = default.node_sound_stone_defaults(),
}, true, true)
-- crafting:
-- base recipe:
minetest.register_craft({ minetest.register_craft({
output = "mesecons_movestones:movestone 2", output = "mesecons_movestones:movestone 2",
recipe = { recipe = {
@ -137,15 +162,32 @@ minetest.register_craft({
} }
}) })
-- STICKY_MOVESTONE -- conversation:
mesecon.register_movestone("mesecons_movestones:sticky_movestone", { minetest.register_craft({
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"}, type = "shapeless",
inventory_image = minetest.inventorycube("jeija_sticky_movestone.png", "jeija_movestone_side.png", "jeija_movestone_side.png"), output = "mesecons_movestones:movestone",
groups = {cracky=3}, recipe = {"mesecons_movestones:movestone_vertical"},
description="Sticky Movestone", })
sounds = default.node_sound_stone_defaults(),
}, true)
minetest.register_craft({
type = "shapeless",
output = "mesecons_movestones:movestone_vertical",
recipe = {"mesecons_movestones:movestone"},
})
minetest.register_craft({
type = "shapeless",
output = "mesecons_movestones:sticky_movestone",
recipe = {"mesecons_movestones:sticky_movestone_vertical"},
})
minetest.register_craft({
type = "shapeless",
output = "mesecons_movestones:sticky_movestone_vertical",
recipe = {"mesecons_movestones:sticky_movestone"},
})
-- make sticky:
minetest.register_craft({ minetest.register_craft({
output = "mesecons_movestones:sticky_movestone", output = "mesecons_movestones:sticky_movestone",
recipe = { recipe = {
@ -153,6 +195,18 @@ minetest.register_craft({
} }
}) })
-- Don't allow pushing movestones while they're active minetest.register_craft({
mesecon.register_mvps_stopper("mesecons_movestones:movestone_active") output = "mesecons_movestones:sticky_movestone_vertical",
mesecon.register_mvps_stopper("mesecons_movestones:sticky_movestone_active") recipe = {
{"mesecons_materials:glue"},
{"mesecons_movestones:movestone_vertical"},
{"mesecons_materials:glue"},
}
})
-- legacy code:
minetest.register_alias("mesecons_movestones:movestone_active",
"mesecons_movestones:movestone")
minetest.register_alias("mesecons_movestones:sticky_movestone_active",
"mesecons_movestones:sticky_movestone")