summaryrefslogtreecommitdiff
path: root/worldedit
diff options
context:
space:
mode:
Diffstat (limited to 'worldedit')
-rw-r--r--worldedit/init.lua1
-rw-r--r--worldedit/manipulations.lua71
-rw-r--r--worldedit/primitives.lua91
-rw-r--r--worldedit/queue.lua123
-rw-r--r--worldedit/serialization.lua7
5 files changed, 104 insertions, 189 deletions
diff --git a/worldedit/init.lua b/worldedit/init.lua
index 18e3490..e4ac8f7 100644
--- a/worldedit/init.lua
+++ b/worldedit/init.lua
@@ -18,4 +18,3 @@ loadmodule(path .. "/visualization.lua")
loadmodule(path .. "/serialization.lua")
loadmodule(path .. "/code.lua")
loadmodule(path .. "/compatibility.lua")
-loadmodule(path .. "/queue.lua")
diff --git a/worldedit/manipulations.lua b/worldedit/manipulations.lua
index 2b34e3c..54e0d2e 100644
--- a/worldedit/manipulations.lua
+++ b/worldedit/manipulations.lua
@@ -113,11 +113,11 @@ worldedit.replaceinverse = function(pos1, pos2, searchnode, replacenode)
end
--copies the region defined by positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z") by `amount` nodes, returning the number of nodes copied
-worldedit.copy = function(pos1, pos2, axis, amount, env)
+worldedit.copy = function(pos1, pos2, axis, amount)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
- if env == nil then env = minetest.env end
--wip: copy slice by slice using schematic method in the copy axis and transfer metadata in separate loop (and if the amount is greater than the length in the axis, copy whole thing at a time), use voxelmanip to keep area loaded
+ local get_node, get_meta, add_node = minetest.get_node, minetest.get_meta, minetest.add_node
if amount < 0 then
local pos = {x=pos1.x, y=0, z=0}
while pos.x <= pos2.x do
@@ -125,12 +125,12 @@ worldedit.copy = function(pos1, pos2, axis, amount, env)
while pos.y <= pos2.y do
pos.z = pos1.z
while pos.z <= pos2.z do
- local node = env:get_node(pos) --obtain current node
- local meta = env:get_meta(pos):to_table() --get meta of current node
+ local node = get_node(pos) --obtain current node
+ local meta = get_meta(pos):to_table() --get meta of current node
local value = pos[axis] --store current position
pos[axis] = value + amount --move along axis
- env:add_node(pos, node) --copy node to new position
- env:get_meta(pos):from_table(meta) --set metadata of new node
+ add_node(pos, node) --copy node to new position
+ get_meta(pos):from_table(meta) --set metadata of new node
pos[axis] = value --restore old position
pos.z = pos.z + 1
end
@@ -145,12 +145,12 @@ worldedit.copy = function(pos1, pos2, axis, amount, env)
while pos.y >= pos1.y do
pos.z = pos2.z
while pos.z >= pos1.z do
- local node = minetest.env:get_node(pos) --obtain current node
- local meta = env:get_meta(pos):to_table() --get meta of current node
+ local node = get_node(pos) --obtain current node
+ local meta = get_meta(pos):to_table() --get meta of current node
local value = pos[axis] --store current position
pos[axis] = value + amount --move along axis
- minetest.env:add_node(pos, node) --copy node to new position
- env:get_meta(pos):from_table(meta) --set metadata of new node
+ add_node(pos, node) --copy node to new position
+ get_meta(pos):from_table(meta) --set metadata of new node
pos[axis] = value --restore old position
pos.z = pos.z - 1
end
@@ -163,11 +163,11 @@ worldedit.copy = function(pos1, pos2, axis, amount, env)
end
--moves the region defined by positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z") by `amount` nodes, returning the number of nodes moved
-worldedit.move = function(pos1, pos2, axis, amount, env)
+worldedit.move = function(pos1, pos2, axis, amount)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
- if env == nil then env = minetest.env end
--wip: move slice by slice using schematic method in the move axis and transfer metadata in separate loop (and if the amount is greater than the length in the axis, copy whole thing at a time and erase original after, using schematic method), use voxelmanip to keep area loaded
+ local get_node, get_meta, add_node, remove_node = minetest.get_node, minetest.get_meta, minetest.add_node, minetest.remove_node
if amount < 0 then
local pos = {x=pos1.x, y=0, z=0}
while pos.x <= pos2.x do
@@ -175,13 +175,13 @@ worldedit.move = function(pos1, pos2, axis, amount, env)
while pos.y <= pos2.y do
pos.z = pos1.z
while pos.z <= pos2.z do
- local node = env:get_node(pos) --obtain current node
- local meta = env:get_meta(pos):to_table() --get metadata of current node
- env:remove_node(pos)
+ local node = get_node(pos) --obtain current node
+ local meta = get_meta(pos):to_table() --get metadata of current node
+ remove_node(pos)
local value = pos[axis] --store current position
pos[axis] = value + amount --move along axis
- env:add_node(pos, node) --move node to new position
- env:get_meta(pos):from_table(meta) --set metadata of new node
+ add_node(pos, node) --move node to new position
+ get_meta(pos):from_table(meta) --set metadata of new node
pos[axis] = value --restore old position
pos.z = pos.z + 1
end
@@ -196,13 +196,13 @@ worldedit.move = function(pos1, pos2, axis, amount, env)
while pos.y >= pos1.y do
pos.z = pos2.z
while pos.z >= pos1.z do
- local node = env:get_node(pos) --obtain current node
- local meta = env:get_meta(pos):to_table() --get metadata of current node
- env:remove_node(pos)
+ local node = get_node(pos) --obtain current node
+ local meta = get_meta(pos):to_table() --get metadata of current node
+ remove_node(pos)
local value = pos[axis] --store current position
pos[axis] = value + amount --move along axis
- env:add_node(pos, node) --move node to new position
- env:get_meta(pos):from_table(meta) --set metadata of new node
+ add_node(pos, node) --move node to new position
+ get_meta(pos):from_table(meta) --set metadata of new node
pos[axis] = value --restore old position
pos.z = pos.z - 1
end
@@ -249,7 +249,7 @@ worldedit.scale = function(pos1, pos2, factor)
--make area stay loaded
local manip = minetest.get_voxel_manip()
local new_pos2 = {x=pos1.x + (pos2.x - pos1.x) * factor + size, y=pos1.y + (pos2.y - pos1.y) * factor + size, z=pos1.z + (pos2.z - pos1.z) * factor + size}
- local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, new_pos2)
+ manip:read_from_map(pos1, new_pos2)
local pos = {x=pos2.x, y=0, z=0}
local bigpos = {x=0, y=0, z=0}
@@ -496,22 +496,25 @@ end
worldedit.clearobjects = function(pos1, pos2)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
- --set up voxel manipulator
+ --make area stay loaded
local manip = minetest.get_voxel_manip()
manip:read_from_map(pos1, pos2)
local pos1x, pos1y, pos1z = pos1.x, pos1.y, pos1.z
- local pos2x, pos2y, pos2z = pos2.x, pos2.y, pos2.z
- local center = {x=(pos1x + pos2x + 1) / 2, y=(pos1y + pos2y + 1) / 2, z=(pos1z + pos2z + 1) / 2}
- local radius = ((center.x - pos1x + 0.5) + (center.y - pos1y + 0.5) + (center.z - pos1z + 0.5)) ^ 0.5
+ local pos2x, pos2y, pos2z = pos2.x + 1, pos2.y + 1, pos2.z + 1
+ local center = {x=(pos1x + pos2x) / 2, y=(pos1y + pos2y) / 2, z=(pos1z + pos2z) / 2} --center of region
+ local radius = ((center.x - pos1x + 0.5) + (center.y - pos1y + 0.5) + (center.z - pos1z + 0.5)) ^ 0.5 --bounding sphere radius
local count = 0
- for _, obj in pairs(minetest.get_objects_inside_radius(center, radius)) do
- local pos = obj:getpos()
- if pos.x >= pos1x and pos.x <= pos2x
- and pos.y >= pos1y and pos.y <= pos2y
- and pos.z >= pos1z and pos.z <= pos2z then
- obj:remove()
- count = count + 1
+ for _, obj in pairs(minetest.get_objects_inside_radius(center, radius)) do --all objects in bounding sphere
+ local entity = obj:get_luaentity()
+ if not (entity and entity.name:find("^worldedit:")) then --avoid WorldEdit entities
+ local pos = obj:getpos()
+ if pos.x >= pos1x and pos.x <= pos2x
+ and pos.y >= pos1y and pos.y <= pos2y
+ and pos.z >= pos1z and pos.z <= pos2z then --inside region
+ obj:remove()
+ count = count + 1
+ end
end
end
return count
diff --git a/worldedit/primitives.lua b/worldedit/primitives.lua
index d11596a..eb4624b 100644
--- a/worldedit/primitives.lua
+++ b/worldedit/primitives.lua
@@ -209,6 +209,20 @@ worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename) --wip:
currentpos[axis] = currentpos[axis] - length
end
+ --make area stay loaded
+ local manip = minetest.get_voxel_manip()
+ local pos1 = {
+ [axis]=currentpos[axis],
+ [other1]=currentpos[other1] - radius,
+ [other2]=currentpos[other2] - radius
+ }
+ local pos2 = {
+ [axis]=currentpos[axis] + length - 1,
+ [other1]=currentpos[other1] + radius,
+ [other2]=currentpos[other2] + radius
+ }
+ manip:read_from_map(pos1, pos2)
+
--create schematic for single node column along the axis
local node = {name=nodename, param1=0, param2=0}
local nodes = {}
@@ -306,13 +320,13 @@ worldedit.cylinder = function(pos, axis, length, radius, nodename)
local offset = {x=currentpos.x - emerged_pos1.x, y=currentpos.y - emerged_pos1.y, z=currentpos.z - emerged_pos1.z}
local min_slice, max_slice = offset[axis], offset[axis] + length - 1
local count = 0
- for axis1 = -radius, radius do
- local newaxis1 = (axis1 + offset[other1]) * stride[other1] + 1 --offset contributed by other axis 1 plus 1 to make it 1-indexed
- for axis2 = -radius, radius do
- local newaxis2 = newaxis1 + (axis2 + offset[other2]) * stride[other2]
- if axis1 * axis1 + axis2 * axis2 <= max_radius then
- for slice = min_slice, max_slice do
- local i = newaxis2 + slice * stride[axis] + 1
+ for index2 = -radius, radius do
+ local newindex2 = (index2 + offset[other1]) * stride[other1] + 1 --offset contributed by other axis 1 plus 1 to make it 1-indexed
+ for index3 = -radius, radius do
+ local newindex3 = newindex2 + (index3 + offset[other2]) * stride[other2]
+ if index2 * index2 + index3 * index3 <= max_radius then
+ for index1 = min_slice, max_slice do --add column along axis
+ local i = newindex3 + index1 * stride[axis] + 1
nodes[i] = node_id
end
count = count + length
@@ -330,9 +344,34 @@ end
--adds a pyramid centered at `pos` with height `height`, composed of `nodename`, returning the number of nodes added
worldedit.pyramid = function(pos, axis, height, nodename)
- local pos1 = {x=pos.x - height, y=pos.y, z=pos.z - height}
+ local other1, other2
+ if axis == "x" then
+ other1, other2 = "y", "z"
+ elseif axis == "y" then
+ other1, other2 = "x", "z"
+ else --axis == "z"
+ other1, other2 = "x", "y"
+ end
+
+ local pos1 = {x=pos.x - height, y=pos.y - height, z=pos.z - height}
local pos2 = {x=pos.x + height, y=pos.y + height, z=pos.z + height}
+ --handle inverted pyramids
+ local startaxis, endaxis, step
+ local currentpos = {x=pos.x, y=pos.y, z=pos.z}
+ if height > 0 then
+ height = height - 1
+ startaxis, endaxis = 0, height
+ step = 1
+ pos1[axis] = pos[axis] --upper half of box
+ else
+ height = -height - 1
+ startaxis, endaxis = height, 0
+ step = -1
+ pos2[axis] = pos[axis] + 1 --lower half of box
+ currentpos[axis] = pos[axis] - height --bottom of box
+ end
+
--set up voxel manipulator
local manip = minetest.get_voxel_manip()
local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)
@@ -345,31 +384,22 @@ worldedit.pyramid = function(pos, axis, height, nodename)
nodes[i] = ignore
end
- --handle inverted pyramids
- height = height - 1
- local size = height
- local step = -1
- if height < 0 then
- size = 0
- step = 1
- end
---wip: support arbitrary axes
--fill selected area with node
local node_id = minetest.get_content_id(nodename)
- local offsetx, offsety, offsetz = pos.x - emerged_pos1.x, pos.y - emerged_pos1.y, pos.z - emerged_pos1.z
- local zstride, ystride = area.zstride, area.ystride
+ local stride = {x=1, y=area.ystride, z=area.zstride}
+ local offset = {x=currentpos.x - emerged_pos1.x, y=currentpos.y - emerged_pos1.y, z=currentpos.z - emerged_pos1.z}
local count = 0
- for y = 0, height do --go through each level of the pyramid
- local newy = (y + offsety) * ystride + 1 --offset contributed by y plus 1 to make it 1-indexed
- for z = -size, size do
- local newz = newy + (z + offsetz) * zstride
- for x = -size, size do
- local i = newz + (x + offsetx)
+ for index1 = startaxis, endaxis, step do --go through each level of the pyramid
+ local newindex1 = (index1 + offset[axis]) * stride[axis] + 1 --offset contributed by axis plus 1 to make it 1-indexed
+ for index2 = -height, height do
+ local newindex2 = newindex1 + (index2 + offset[other1]) * stride[other1]
+ for index3 = -height, height do
+ local i = newindex2 + (index3 + offset[other2]) * stride[other2]
nodes[i] = node_id
end
end
- size = size + step
- count = count + ((height - y) * 2 + 1) ^ 2
+ count = count + (height * 2 + 1) ^ 2
+ height = height - 1
end
--update map nodes
@@ -383,11 +413,12 @@ end
--adds a spiral centered at `pos` with width `width`, height `height`, space between walls `spacer`, composed of `nodename`, returning the number of nodes added
worldedit.spiral = function(pos, width, height, spacer, nodename, env) --wip: rewrite this whole thing, nobody can understand it anyways
-- spiral matrix - http://rosettacode.org/wiki/Spiral_matrix#Lua
- av, sn = math.abs, function(s) return s~=0 and s/av(s) or 0 end
+ local abs = math.abs
+ local sign = function(s) return s ~= 0 and s / av(s) or 0 end
local function sindex(z, x) -- returns the value at (x, z) in a spiral that starts at 1 and goes outwards
if z == -x and z >= x then return (2*z+1)^2 end
- local l = math.max(av(z), av(x))
- return (2*l-1)^2+4*l+2*l*sn(x+z)+sn(z^2-x^2)*(l-(av(z)==l and sn(z)*x or sn(x)*z)) -- OH GOD WHAT
+ local longest = math.max(abs(z), abs(x))
+ return (2*longest-1)^2 + 4*longest + 2*longest*sign(x+z) + sign(z^2-x^2)*(longest-(abs(z)==longest and sign(z)*x or sign(x)*z)) -- OH GOD WHAT
end
local function spiralt(side)
local ret, id, start, stop = {}, 0, math.floor((-side+1)/2), math.floor((side-1)/2)
diff --git a/worldedit/queue.lua b/worldedit/queue.lua
deleted file mode 100644
index e9f3ac3..0000000
--- a/worldedit/queue.lua
+++ /dev/null
@@ -1,123 +0,0 @@
-worldedit = worldedit or {}
-local minetest = minetest --local copy of global
-
-worldedit.queue = {}
-worldedit.lower = 1
-worldedit.higher = 0
-
-worldedit.ENABLE_QUEUE = true --enable the WorldEdit block queue
-worldedit.MAXIMUM_TIME = 0.08 --maximum time each step alloted for WorldEdit operations
-
-minetest.register_globalstep(function(dtime)
- local elapsed = 0
- local env = minetest.env
- while worldedit.lower <= worldedit.higher and elapsed <= worldedit.MAXIMUM_TIME do
- local entry = worldedit.queue[worldedit.lower]
- if entry.t == "set_node" then
- env:set_node(entry.pos, entry.node)
- elapsed = elapsed + 0.0002
- elseif entry.t == "remove_node" then
- env:remove_node(entry.pos)
- elapsed = elapsed + 0.0002
- elseif entry.t == "place_node" then
- env:place_node(entry.pos, entry.node)
- elapsed = elapsed + 0.001
- elseif entry.t == "dig_node" then
- env:dig_node(entry.pos)
- elapsed = elapsed + 0.001
- elseif entry.t == "add_entity" then
- env:add_entity(entry.pos, entry.name)
- elapsed = elapsed + 0.005
- elseif entry.t == "add_item" then
- env:add_item(entry.pos, entry.item)
- elapsed = elapsed + 0.005
- elseif entry.t == "meta_from_table" then
- env:get_meta(entry.pos):from_table(entry.table)
- elapsed = elapsed + 0.0002
- else
- print("Unknown queue event type: " .. entry.t)
- end
- worldedit.queue[worldedit.lower] = nil
- worldedit.lower = worldedit.lower + 1
- end
-end)
-
-worldedit.enqueue = function(value)
- worldedit.higher = worldedit.higher + 1
- worldedit.queue[worldedit.higher] = value
-end
-
-function table.copy(t, seen)
- seen = seen or {}
- if t == nil then return nil end
- if seen[t] then return seen[t] end
-
- local nt = {}
- for k, v in pairs(t) do
- if type(v) == 'table' then
- nt[k] = table.copy(v, seen)
- else
- nt[k] = v
- end
- end
- seen[t] = nt
- return nt
-end
-
-local queue_setnode = function(self, pos_, node_)
- worldedit.enqueue({pos=table.copy(pos_), node=table.copy(node_), t="set_node"})
-end
-
-local queue_removenode = function(self, pos_)
- worldedit.enqueue({pos=table.copy(pos_), t="remove_node"})
-end
-
-local queue_placenode = function(self, pos_, node_)
- worldedit.enqueue({pos=table.copy(pos_), node=table.copy(node_), t="place_node"})
-end
-
-local queue_dignode = function(self, pos_)
- worldedit.enqueue({pos=table.copy(pos_), t="dig_node"})
-end
-
-local queue_addentity = function(self, pos_, name_)
- worldedit.enqueue({pos=table.copy(pos_), name=name_.."", t="add_entity"})
-end
-
-local queue_additem = function(self, pos_, item_)
- worldedit.enqueue({pos=table.copy(pos_), item=item_.."", t="add_item"})
-end
-
-local queue_setmeta = function(self, pos_, table_)
- worldedit.enqueue({pos=table.copy(pos_), table=table.copy(table_), t="meta_from_table"})
-end
-
-local aliasmeta = {
--- the other functions are left out because they are not used in worldedit
- to_table = function(self) return minetest.env:get_meta(self._pos):to_table() end,
- set_string = function(self, name_, value_) minetest.env:get_meta(self._pos):set_string(name_, value_) end,
- from_table = function(self, tbl) queue_setmeta(nil, self._pos, tbl) end,
-}
-
-local get_meta_alias = function(self, pos)
- local am = table.copy(aliasmeta)
- am._pos = pos
- return am
-end
-
-worldedit.queue_aliasenv = {
--- ALL functions that are not just piped to the real minetest.env function must copy the arguments, not just reference them
- set_node = queue_setnode,
- add_node = queue_setnode,
- remove_node = queue_removenode,
- get_node = function(self, pos) return minetest.env:get_node(pos) end,
- get_node_or_nil = function(self, pos) return minetest.env:get_node_or_nil(pos) end,
- get_node_light = function(self, pos, timeofday) return minetest.env:get_node_light(pos, timeofday) end,
- place_node = queue_placenode,
- dig_node = queue_dignode,
- punch_node = function(self, pos) return minetest.env:punch_node(pos) end,
- get_meta = get_meta_alias,
- get_node_timer = function(self, pos) return minetest.env:get_node_timer(pos) end,
- add_entity = queue_addentity,
- add_item = queue_additem,
-}
diff --git a/worldedit/serialization.lua b/worldedit/serialization.lua
index 5fbc16c..7b65b25 100644
--- a/worldedit/serialization.lua
+++ b/worldedit/serialization.lua
@@ -182,7 +182,12 @@ end
--loads the nodes represented by string `value` at position `originpos`, returning the number of nodes deserialized
--contains code based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile) by ChillCode, available under the MIT license (GPL compatible)
-worldedit.deserialize = function(originpos, value) --wip: use voxelmanip to make sure the blocks are loaded
+worldedit.deserialize = function(originpos, value)
+ --make sure the area stays loaded --wip: not very performant
+ local pos1, pos2 = worldedit.allocate(originpos, value)
+ local manip = minetest.get_voxel_manip()
+ manip:read_from_map(pos1, pos2)
+
local originx, originy, originz = originpos.x, originpos.y, originpos.z
local count = 0
local add_node, get_meta = minetest.add_node, minetest.get_meta