1
0
mirror of https://github.com/Uberi/Minetest-WorldEdit.git synced 2024-12-25 18:20:38 +01:00

Avoid using slower and deprecated EnvRef, fix schematic data MapNodes, huge speed boost to worldedit.hollow_cylinder, fix some bugs.

This commit is contained in:
Anthony Zhang 2013-06-23 12:46:59 -04:00
parent 1e5f623cbb
commit 9db6192eba
3 changed files with 120 additions and 134 deletions

View File

@ -33,7 +33,7 @@ worldedit.set = function(pos1, pos2, nodename)
local nodes = {}
--fill nodes table with node to be set
local node = {nodename, 0, 0}
local node = {name=nodename, param1=0, param2=0}
for i = 1, (size.x * size.y * size.z) do
nodes[i] = node
end
@ -43,38 +43,34 @@ worldedit.set = function(pos1, pos2, nodename)
end
--replaces all instances of `searchnode` with `replacenode` in a region defined by positions `pos1` and `pos2`, returning the number of nodes replaced
worldedit.replace = function(pos1, pos2, searchnode, replacenode, env)
worldedit.replace = function(pos1, pos2, searchnode, replacenode)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
if env == nil then env = minetest.env end
local node = {name=replacenode}
local add_node = minetest.add_node
local nodes = minetest.find_nodes_in_area(pos1, pos2, searchnode)
for _, pos in ipairs(nodes) do
env:add_node(pos, node)
add_node(pos, node)
end
return #nodes
end
--replaces all nodes other than `searchnode` with `replacenode` in a region defined by positions `pos1` and `pos2`, returning the number of nodes replaced
worldedit.replaceinverse = function(pos1, pos2, searchnode, replacenode, env)
worldedit.replaceinverse = function(pos1, pos2, searchnode, replacenode)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
if env == nil then env = minetest.env end
if minetest.registered_nodes[searchnode] == nil then
searchnode = "default:" .. searchnode
end
local pos = {x=pos1.x, y=0, z=0}
local node = {name=replacenode}
local get_node, add_node = minetest.get_node, minetest.add_node
local count = 0
while pos.x <= pos2.x do --wip: see if this can be sped up like worldedit.replace, except with hashed found node positions and testing against the set
while pos.x <= pos2.x do
pos.y = pos1.y
while pos.y <= pos2.y do
pos.z = pos1.z
while pos.z <= pos2.z do
local name = env:get_node(pos).name
local name = get_node(pos).name
if name ~= "ignore" and name ~= searchnode then
env:add_node(pos, node)
add_node(pos, node)
count = count + 1
end
pos.z = pos.z + 1
@ -206,13 +202,12 @@ worldedit.stack = function(pos1, pos2, axis, count, env)
end
--scales the region defined by positions `pos1` and `pos2` by an factor of positive integer `factor` with `pos1` as the origin, returning the number of nodes scaled, the new scaled position 1, and the new scaled position 2
worldedit.scale = function(pos1, pos2, factor, env)
worldedit.scale = function(pos1, pos2, factor)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
if env == nil then env = minetest.env end
--prepare schematic of large node
local place_schematic = minetest.place_schematic
local placeholder_node = {"", 0, 0}
local get_node, get_meta, place_schematic = minetest.get_node, minetest.get_meta, minetest.place_schematic
local placeholder_node = {name="", param1=0, param2=0}
local nodes = {}
for i = 1, size ^ 3 do
nodes[i] = placeholder_node
@ -227,8 +222,8 @@ worldedit.scale = function(pos1, pos2, factor, 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 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
local posx, posy, posz = pos1.x + (pos.x - pos1.x) * factor, pos1.y + (pos.y - pos1.y) * factor, pos1.z + (pos.z - pos1.z) * factor
@ -241,7 +236,7 @@ worldedit.scale = function(pos1, pos2, factor, env)
for y = 0, size do
for z = 0, size do
bigpos.x, bigpos.y, bigpos.z = posx + x, posy + y, posz + z
env:get_meta(bigpos):from_table(meta) --set metadata of new node
get_meta(bigpos):from_table(meta) --set metadata of new node
end
end
end
@ -278,7 +273,7 @@ worldedit.transpose = function(pos1, pos2, axis1, axis2, env)
newpos2[axis2] = pos1[axis2] + extent1
local pos = {x=pos1.x, y=0, z=0}
if env == nil then env = minetest.env end
local get_node, get_meta, add_node = minetest.get_node, minetest.get_meta, minetest.add_node
while pos.x <= pos2.x do
pos.y = pos1.y
while pos.y <= pos2.y do
@ -286,17 +281,17 @@ worldedit.transpose = function(pos1, pos2, axis1, axis2, env)
while pos.z <= pos2.z do
local extent1, extent2 = pos[axis1] - pos1[axis1], pos[axis2] - pos1[axis2]
if compare(extent1, extent2) then --transpose only if below the diagonal
local node1 = env:get_node(pos)
local meta1 = env:get_meta(pos):to_table()
local node1 = get_node(pos)
local meta1 = get_meta(pos):to_table()
local value1, value2 = pos[axis1], pos[axis2] --save position values
pos[axis1], pos[axis2] = pos1[axis1] + extent2, pos1[axis2] + extent1 --swap axis extents
local node2 = env:get_node(pos)
local meta2 = env:get_meta(pos):to_table()
env:add_node(pos, node1)
env:get_meta(pos):from_table(meta1)
local node2 = get_node(pos)
local meta2 = get_meta(pos):to_table()
add_node(pos, node1)
get_meta(pos):from_table(meta1)
pos[axis1], pos[axis2] = value1, value2 --restore position values
env:add_node(pos, node2)
env:get_meta(pos):from_table(meta2)
add_node(pos, node2)
get_meta(pos):from_table(meta2)
end
pos.z = pos.z + 1
end
@ -372,8 +367,8 @@ end
--rotates all oriented nodes in a region defined by the positions `pos1` and `pos2` by `angle` degrees clockwise (90 degree increment) around the Y axis, returning the number of nodes oriented
worldedit.orient = function(pos1, pos2, angle, env)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
local nodes = minetest.registered_nodes
if env == nil then env = minetest.env end
local registered_nodes = minetest.registered_nodes
local wallmounted = {
[90]={[0]=0, [1]=1, [2]=5, [3]=4, [4]=2, [5]=3},
[180]={[0]=0, [1]=1, [2]=3, [3]=2, [4]=5, [5]=4},
@ -393,26 +388,27 @@ worldedit.orient = function(pos1, pos2, angle, env)
local facedir_substitution = facedir[angle]
local count = 0
local get_node, get_meta, add_node = minetest.get_node, minetest.get_meta, minetest.add_node
local pos = {x=pos1.x, y=0, z=0}
while pos.x <= pos2.x do
pos.y = pos1.y
while pos.y <= pos2.y do
pos.z = pos1.z
while pos.z <= pos2.z do
local node = env:get_node(pos)
local def = nodes[node.name]
local node = get_node(pos)
local def = registered_nodes[node.name]
if def then
if def.paramtype2 == "wallmounted" then
node.param2 = wallmounted_substitution[node.param2]
local meta = env:get_meta(pos):to_table()
env:add_node(pos, node)
env:get_meta(pos):from_table(meta)
local meta = get_meta(pos):to_table()
add_node(pos, node)
get_meta(pos):from_table(meta)
count = count + 1
elseif def.paramtype2 == "facedir" then
node.param2 = facedir_substitution[node.param2]
local meta = env:get_meta(pos):to_table()
env:add_node(pos, node)
env:get_meta(pos):from_table(meta)
local meta = get_meta(pos):to_table()
add_node(pos, node)
get_meta(pos):from_table(meta)
count = count + 1
end
end
@ -428,20 +424,10 @@ end
--fixes the lighting in a region defined by positions `pos1` and `pos2`, returning the number of nodes updated
worldedit.fixlight = function(pos1, pos2, env)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
if env == nil then env = minetest.env end
local count = 0
local pos = {x=pos1.x, y=pos2.y, z=0}
while pos.x <= pos2.x do
pos.z = pos1.z
while pos.z <= pos2.z do
if env:get_node(pos).name == "air" then
env:dig_node(pos)
count = count + 1
end
pos.z = pos.z + 1
end
pos.x = pos.x + 1
local nodes = minetest.find_nodes_in_area(pos1, pos2, "air")
local dig_node = minetest.dig_node
for _, pos in ipairs(nodes) do
dig_node(pos)
end
return count
return #nodes
end

View File

@ -3,8 +3,8 @@ worldedit = worldedit or {}
--adds a hollow sphere centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
worldedit.hollow_sphere = function(pos, radius, nodename)
local insert = table.insert
local node = {nodename, 0, 0}
local ignore = {"ignore", 0, 0}
local node = {name=nodename, param1=0, param2=0}
local ignore = {name="ignore", param1=0, param2=0}
local nodes = {}
local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
for x = -radius, radius do
@ -26,8 +26,8 @@ end
--adds a sphere centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
worldedit.sphere = function(pos, radius, nodename)
local insert = table.insert
local node = {nodename, 0, 0}
local ignore = {"ignore", 0, 0}
local node = {name=nodename, param1=0, param2=0}
local ignore = {name="ignore", param1=0, param2=0}
local nodes = {}
local max_radius = radius * (radius + 1)
for x = -radius, radius do
@ -48,8 +48,8 @@ end
--adds a hollow dome centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
worldedit.hollow_dome = function(pos, radius, nodename) --wip: use bresenham sphere for maximum speed
local insert = table.insert
local node = {nodename, 0, 0}
local ignore = {"ignore", 0, 0}
local node = {name=nodename, param1=0, param2=0}
local ignore = {name="ignore", param1=0, param2=0}
local nodes = {}
local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
for x = -radius, radius do
@ -71,8 +71,8 @@ end
--adds a dome centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
worldedit.dome = function(pos, radius, nodename) --wip: use bresenham sphere for maximum speed
local insert = table.insert
local node = {nodename, 0, 0}
local ignore = {"ignore", 0, 0}
local node = {name=nodename, param1=0, param2=0}
local ignore = {name="ignore", param1=0, param2=0}
local nodes = {}
local max_radius = radius * (radius + 1)
for x = -radius, radius do
@ -101,52 +101,57 @@ worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename)
other1, other2 = "x", "y"
end
if env == nil then env = minetest.env end
local currentpos = {x=pos.x, y=pos.y, z=pos.z}
local node = {name=nodename}
local count = 0
local step = 1
if length < 0 then
length = -length
step = -1
currentpos[axis] = currentpos[axis] - length
end
--create schematic for single node column along the axis
local node = {name=nodename, param1=0, param2=0}
local nodes = {}
for i = 1, length do
local offset1, offset2 = 0, radius
local delta = -radius
while offset1 <= offset2 do
--add node at each octant
local first1, first2 = pos[other1] + offset1, pos[other1] - offset1
local second1, second2 = pos[other2] + offset2, pos[other2] - offset2
currentpos[other1], currentpos[other2] = first1, second1
env:add_node(currentpos, node) --octant 1
currentpos[other1] = first2
env:add_node(currentpos, node) --octant 4
currentpos[other2] = second2
env:add_node(currentpos, node) --octant 5
currentpos[other1] = first1
env:add_node(currentpos, node) --octant 8
local first1, first2 = pos[other1] + offset2, pos[other1] - offset2
local second1, second2 = pos[other2] + offset1, pos[other2] - offset1
currentpos[other1], currentpos[other2] = first1, second1
env:add_node(currentpos, node) --octant 2
currentpos[other1] = first2
env:add_node(currentpos, node) --octant 3
currentpos[other2] = second2
env:add_node(currentpos, node) --octant 6
currentpos[other1] = first1
env:add_node(currentpos, node) --octant 7
nodes[i] = node
end
local schematic = {size={[axis]=length, [other1]=1, [other2]=1}, data=nodes}
count = count + 8 --wip: broken
--add columns in a circle around axis to form cylinder
local place_schematic = minetest.place_schematic
local count = 0
local offset1, offset2 = 0, radius
local delta = -radius
while offset1 <= offset2 do
--add node at each octant
local first1, first2 = pos[other1] + offset1, pos[other1] - offset1
local second1, second2 = pos[other2] + offset2, pos[other2] - offset2
currentpos[other1], currentpos[other2] = first1, second1
place_schematic(currentpos, schematic) --octant 1
currentpos[other1] = first2
place_schematic(currentpos, schematic) --octant 4
currentpos[other2] = second2
place_schematic(currentpos, schematic) --octant 5
currentpos[other1] = first1
place_schematic(currentpos, schematic) --octant 8
local first1, first2 = pos[other1] + offset2, pos[other1] - offset2
local second1, second2 = pos[other2] + offset1, pos[other2] - offset1
currentpos[other1], currentpos[other2] = first1, second1
place_schematic(currentpos, schematic) --octant 2
currentpos[other1] = first2
place_schematic(currentpos, schematic) --octant 3
currentpos[other2] = second2
place_schematic(currentpos, schematic) --octant 6
currentpos[other1] = first1
place_schematic(currentpos, schematic) --octant 7
--move to next location
delta = delta + (offset1 * 2) + 1
if delta >= 0 then
offset2 = offset2 - 1
delta = delta - (offset2 * 2)
end
offset1 = offset1 + 1
count = count + (length *8) --wip: broken because sometimes currentpos is repeated
--move to next location
delta = delta + (offset1 * 2) + 1
if delta >= 0 then
offset2 = offset2 - 1
delta = delta - (offset2 * 2)
end
currentpos[axis] = currentpos[axis] + step
offset1 = offset1 + 1
end
return count
end
@ -162,6 +167,7 @@ worldedit.cylinder = function(pos, axis, length, radius, nodename, env)
other1, other2 = "x", "y"
end
--wip: make this faster using the schematic method by adding columns in a circle pattern like in hollow_cylinder, or by adding whole 2D slices using custom sized schematics
if env == nil then env = minetest.env end
local currentpos = {x=pos.x, y=pos.y, z=pos.z}
local node = {name=nodename}
@ -216,6 +222,7 @@ worldedit.pyramid = function(pos, height, nodename, env)
local pos2x, pos2y, pos2z = pos.x + height, pos.y + height, pos.z + height
local pos = {x=0, y=pos1y, z=0}
--wip: make this faster using base sized schematics that are then resized while moving upwards, or if that's not possible, add new rows/columns while looping
local count = 0
local node = {name=nodename}
if env == nil then env = minetest.env end
@ -242,6 +249,7 @@ 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: clean this up
-- spiral matrix - http://rosettacode.org/wiki/Spiral_matrix#Lua
--wip: rewrite this whole thing, nobody can understand it anyways
av, sn = math.abs, 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

View File

@ -31,22 +31,21 @@ minetest.register_node("worldedit:placeholder", {
})
--hides all nodes in a region defined by positions `pos1` and `pos2` by non-destructively replacing them with invisible nodes, returning the number of nodes hidden
worldedit.hide = function(pos1, pos2, tenv)
worldedit.hide = function(pos1, pos2)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
if env == nil then env = minetest.env end
local pos = {x=pos1.x, y=0, z=0}
local placeholder = {name="worldedit:placeholder", param1=0, param2=0}
local get_node, get_meta, add_node = minetest.get_node, minetest.get_meta, minetest.add_node
while pos.x <= pos2.x do
pos.y = pos1.y
while pos.y <= pos2.y do
pos.z = pos1.z
while pos.z <= pos2.z do
local node = env:get_node(pos)
local node = get_node(pos)
placeholder.param1, placeholder.param2 = node.param1, node.param2 --copy node's param1 and param2
local data = env:get_meta(pos):to_table() --obtain metadata of original node
env:add_node(pos, placeholder) --add placeholder node
local meta = env:get_meta(pos) --obtain placeholder meta
local data = get_meta(pos):to_table() --obtain metadata of original node
add_node(pos, placeholder) --add placeholder node
local meta = get_meta(pos) --obtain placeholder meta
meta:from_table(data) --set placeholder metadata to the original node's metadata
meta:set_string("worldedit_placeholder", node.name) --add the node's name
pos.z = pos.z + 1
@ -59,18 +58,17 @@ worldedit.hide = function(pos1, pos2, tenv)
end
--suppresses all instances of `nodename` in a region defined by positions `pos1` and `pos2` by non-destructively replacing them with invisible nodes, returning the number of nodes suppressed
worldedit.suppress = function(pos1, pos2, nodename, tenv)
worldedit.suppress = function(pos1, pos2, nodename)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
if env == nil then env = minetest.env end
local placeholder = {name="worldedit:placeholder", param1=0, param2=0}
local nodes = minetest.find_nodes_in_area(pos1, pos2, nodename)
local get_node, get_meta, add_node = minetest.get_node, minetest.get_meta, minetest.add_node
for _, pos in ipairs(nodes) do
local node = env:get_node(pos)
local node = get_node(pos)
placeholder.param1, placeholder.param2 = node.param1, node.param2 --copy node's param1 and param2
local data = env:get_meta(pos):to_table() --obtain metadata of original node
env:add_node(pos, placeholder) --add placeholder node
local meta = env:get_meta(pos) --obtain placeholder meta
local data = get_meta(pos):to_table() --obtain metadata of original node
add_node(pos, placeholder) --add placeholder node
local meta = get_meta(pos) --obtain placeholder meta
meta:from_table(data) --set placeholder metadata to the original node's metadata
meta:set_string("worldedit_placeholder", nodename) --add the node's name
end
@ -78,30 +76,25 @@ worldedit.suppress = function(pos1, pos2, nodename, tenv)
end
--highlights all instances of `nodename` in a region defined by positions `pos1` and `pos2` by non-destructively hiding all other nodes, returning the number of nodes found
worldedit.highlight = function(pos1, pos2, nodename, tenv)
worldedit.highlight = function(pos1, pos2, nodename)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
if env == nil then env = minetest.env end
if minetest.registered_nodes[nodename] == nil then
nodename = "default:" .. nodename
end
local pos = {x=pos1.x, y=0, z=0}
local placeholder = {name="worldedit:placeholder", param1=0, param2=0}
local get_node, get_meta, add_node = minetest.get_node, minetest.get_meta, minetest.add_node
local count = 0
while pos.x <= pos2.x do --wip: see if this can be sped up like worldedit.suppress, except with hashed found node positions and testing against the set
while pos.x <= pos2.x do
pos.y = pos1.y
while pos.y <= pos2.y do
pos.z = pos1.z
while pos.z <= pos2.z do
local node = env:get_node(pos)
local node = get_node(pos)
if node.name == nodename then --node found
count = count + 1
else --hide other nodes
placeholder.param1, placeholder.param2 = node.param1, node.param2 --copy node's param1 and param2
local data = env:get_meta(pos):to_table() --obtain metadata of original node
env:add_node(pos, placeholder) --add placeholder node
local meta = env:get_meta(pos) --obtain placeholder meta
local data = get_meta(pos):to_table() --obtain metadata of original node
add_node(pos, placeholder) --add placeholder node
local meta = get_meta(pos) --obtain placeholder meta
meta:from_table(data) --set placeholder metadata to the original node's metadata
meta:set_string("worldedit_placeholder", node.name) --add the node's name
end
@ -115,20 +108,19 @@ worldedit.highlight = function(pos1, pos2, nodename, tenv)
end
--restores all nodes hidden with WorldEdit functions in a region defined by positions `pos1` and `pos2`, returning the number of nodes restored
worldedit.restore = function(pos1, pos2, tenv)
worldedit.restore = function(pos1, pos2)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
if env == nil then env = minetest.env end
local node = {name="", param1=0, param2=0}
local nodes = minetest.find_nodes_in_area(pos1, pos2, nodename)
local nodes = minetest.find_nodes_in_area(pos1, pos2, "worldedit:placeholder")
local get_node, get_meta, add_node = minetest.get_node, minetest.get_meta, minetest.add_node
for _, pos in ipairs(nodes) do
local currentnode = env:get_node(pos)
local currentnode = get_node(pos)
node.param1, node.param2 = currentnode.param1, currentnode.param2 --copy node's param1 and param2
local data = env:get_meta(pos):to_table() --obtain node metadata
local data = get_meta(pos):to_table() --obtain node metadata
node.name = data.fields.worldedit_placeholder --set node name
data.fields.worldedit_placeholder = nil --delete old nodename
env:add_node(pos, placeholder) --add original node
env:get_meta(pos):from_table(data) --set original node metadata
add_node(pos, placeholder) --add original node
get_meta(pos):from_table(data) --set original node metadata
end
return #nodes
end