From e0a26617002564db0c3d06f4f9e6d509b55b106c Mon Sep 17 00:00:00 2001 From: sfan5 Date: Tue, 5 Jan 2016 13:57:48 +0100 Subject: [PATCH] Fix //stack2 not working (closes #94) --- WorldEdit API.md | 6 ++++++ worldedit/manipulations.lua | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/WorldEdit API.md b/WorldEdit API.md index f50b506..7b99c8b 100644 --- a/WorldEdit API.md +++ b/WorldEdit API.md @@ -45,6 +45,12 @@ Copies the region defined by positions `pos1` and `pos2` along the `axis` axis ( Returns the number of nodes copied. +### count = worldedit.copy2(pos1, pos2, off) + +Copies the region defined by positions `pos1` and `pos2` by the offset vector `off`. + +Returns the number of nodes copied. + ### count = worldedit.move(pos1, pos2, axis, amount) Moves the region defined by positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z") by `amount` nodes. diff --git a/worldedit/manipulations.lua b/worldedit/manipulations.lua index 365d7b3..cf95517 100644 --- a/worldedit/manipulations.lua +++ b/worldedit/manipulations.lua @@ -90,7 +90,7 @@ function worldedit.stack2(pos1, pos2, direction, amount, finished) translated.x = translated.x + direction.x translated.y = translated.y + direction.y translated.z = translated.z + direction.z - worldedit.copy2(pos1, pos2, translated, volume) + worldedit.copy2(pos1, pos2, translated) minetest.after(0, next_one) else if finished then @@ -164,6 +164,38 @@ function worldedit.copy(pos1, pos2, axis, amount) return worldedit.volume(pos1, pos2) end +--- Copies a region by offset vector `off`. +-- @param pos1 +-- @param pos2 +-- @param off +-- @return The number of nodes copied. +function worldedit.copy2(pos1, pos2, off) + local pos1, pos2 = worldedit.sort_pos(pos1, pos2) + + worldedit.keep_loaded(pos1, pos2) + + local get_node, get_meta, set_node = minetest.get_node, + minetest.get_meta, minetest.set_node + local pos = {} + pos.x = pos2.x + while pos.x >= pos1.x do + pos.y = pos2.y + while pos.y >= pos1.y do + pos.z = pos2.z + while pos.z >= pos1.z do + local node = get_node(pos) -- Obtain current node + local meta = get_meta(pos):to_table() -- Get meta of current node + local newpos = vector.add(pos, off) -- Calculate new position + set_node(newpos, node) -- Copy node to new position + get_meta(newpos):from_table(meta) -- Set metadata of new node + pos.z = pos.z - 1 + end + pos.y = pos.y - 1 + end + pos.x = pos.x - 1 + end + return worldedit.volume(pos1, pos2) +end --- Moves a region along `axis` by `amount` nodes. -- @return The number of nodes moved.