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
1 changed files with 150 additions and 96 deletions

View File

@ -8,126 +8,151 @@
-- Pushes all block in front of it
-- Pull all blocks in its back
function mesecon.get_movestone_direction(pos)
local lpos
local rules = {
{x=0, y=1, z=-1},
{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}}
-- settings:
local timer_interval = 1 / mesecon.setting("movestone_speed", 3)
local max_push = mesecon.setting("movestone_max_push", 50)
local max_pull = mesecon.setting("movestone_max_pull", 50)
lpos = {x=pos.x+1, y=pos.y, z=pos.z}
for n = 1, 3 do
if mesecon.is_power_on(lpos, rules[n]) then
return {x=0, y=0, z=-1}
-- helper functions:
local function get_movestone_direction(rulename, is_vertical)
if is_vertical then
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
lpos = {x = pos.x-1, y = pos.y, z = pos.z}
for n=4, 6 do
if mesecon.is_power_on(lpos, rules[n]) then
return {x=0, y=0, z=1}
end
end
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}
else
if rulename.z > 0 then
return {x = -1, y = 0, z = 0}
elseif rulename.z < 0 then
return {x = 1, y = 0, z = 0}
elseif rulename.x > 0 then
return {x = 0, y = 0, z = -1}
elseif rulename.x < 0 then
return {x = 0, y = 0, z = 1}
end
end
end
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
minetest.set_node(pos, {name = name})
return
end
-- registration functions:
function mesecon.register_movestone(name, def, is_sticky, is_vertical)
local function movestone_move(pos, node, rulename)
local direction = get_movestone_direction(rulename, is_vertical)
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)
local success, stack, oldstack = mesecon.mvps_push(frontpos, direction, max_push)
if not success then
minetest.get_node_timer(pos):start(timer_interval)
return
end
mesecon.mvps_process_stack(stack)
mesecon.mvps_move_objects(frontpos, direction, oldstack)
-- ### 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)
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 ###
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
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)
action_on = function(pos, node, rulename)
if rulename and not minetest.get_node_timer(pos):is_started() then
movestone_move(pos, node, rulename)
end
end,
action_off = function (pos)
minetest.set_node(pos, {name = name})
end
rules = mesecon.rules.default,
}}
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
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
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({
output = "mesecons_movestones:movestone 2",
recipe = {
@ -137,15 +162,32 @@ minetest.register_craft({
}
})
-- 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"),
groups = {cracky=3},
description="Sticky Movestone",
sounds = default.node_sound_stone_defaults(),
}, true)
-- conversation:
minetest.register_craft({
type = "shapeless",
output = "mesecons_movestones:movestone",
recipe = {"mesecons_movestones:movestone_vertical"},
})
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({
output = "mesecons_movestones:sticky_movestone",
recipe = {
@ -153,6 +195,18 @@ minetest.register_craft({
}
})
-- 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")
minetest.register_craft({
output = "mesecons_movestones:sticky_movestone_vertical",
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")