1
0
mirror of https://github.com/Uberi/Minetest-WorldEdit.git synced 2024-12-26 02:30:40 +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 = {} local nodes = {}
--fill nodes table with node to be set --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 for i = 1, (size.x * size.y * size.z) do
nodes[i] = node nodes[i] = node
end end
@ -43,38 +43,34 @@ worldedit.set = function(pos1, pos2, nodename)
end end
--replaces all instances of `searchnode` with `replacenode` in a region defined by positions `pos1` and `pos2`, returning the number of nodes replaced --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) local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
if env == nil then env = minetest.env end
local node = {name=replacenode} local node = {name=replacenode}
local add_node = minetest.add_node
local nodes = minetest.find_nodes_in_area(pos1, pos2, searchnode) local nodes = minetest.find_nodes_in_area(pos1, pos2, searchnode)
for _, pos in ipairs(nodes) do for _, pos in ipairs(nodes) do
env:add_node(pos, node) add_node(pos, node)
end end
return #nodes return #nodes
end end
--replaces all nodes other than `searchnode` with `replacenode` in a region defined by positions `pos1` and `pos2`, returning the number of nodes replaced --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) 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 pos = {x=pos1.x, y=0, z=0}
local node = {name=replacenode} local node = {name=replacenode}
local get_node, add_node = minetest.get_node, minetest.add_node
local count = 0 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 pos.y = pos1.y
while pos.y <= pos2.y do while pos.y <= pos2.y do
pos.z = pos1.z pos.z = pos1.z
while pos.z <= pos2.z do 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 if name ~= "ignore" and name ~= searchnode then
env:add_node(pos, node) add_node(pos, node)
count = count + 1 count = count + 1
end end
pos.z = pos.z + 1 pos.z = pos.z + 1
@ -206,13 +202,12 @@ worldedit.stack = function(pos1, pos2, axis, count, env)
end 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 --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) local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
if env == nil then env = minetest.env end
--prepare schematic of large node --prepare schematic of large node
local place_schematic = minetest.place_schematic local get_node, get_meta, place_schematic = minetest.get_node, minetest.get_meta, minetest.place_schematic
local placeholder_node = {"", 0, 0} local placeholder_node = {name="", param1=0, param2=0}
local nodes = {} local nodes = {}
for i = 1, size ^ 3 do for i = 1, size ^ 3 do
nodes[i] = placeholder_node nodes[i] = placeholder_node
@ -227,8 +222,8 @@ worldedit.scale = function(pos1, pos2, factor, env)
while pos.y >= pos1.y do while pos.y >= pos1.y do
pos.z = pos2.z pos.z = pos2.z
while pos.z >= pos1.z do while pos.z >= pos1.z do
local node = env:get_node(pos) --obtain current node local node = get_node(pos) --obtain current node
local meta = env:get_meta(pos):to_table() --get meta of current node local meta = get_meta(pos):to_table() --get meta of current node
local value = pos[axis] --store current position 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 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 y = 0, size do
for z = 0, size do for z = 0, size do
bigpos.x, bigpos.y, bigpos.z = posx + x, posy + y, posz + z 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 end
end end
@ -278,7 +273,7 @@ worldedit.transpose = function(pos1, pos2, axis1, axis2, env)
newpos2[axis2] = pos1[axis2] + extent1 newpos2[axis2] = pos1[axis2] + extent1
local pos = {x=pos1.x, y=0, z=0} 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 while pos.x <= pos2.x do
pos.y = pos1.y pos.y = pos1.y
while pos.y <= pos2.y do while pos.y <= pos2.y do
@ -286,17 +281,17 @@ worldedit.transpose = function(pos1, pos2, axis1, axis2, env)
while pos.z <= pos2.z do while pos.z <= pos2.z do
local extent1, extent2 = pos[axis1] - pos1[axis1], pos[axis2] - pos1[axis2] local extent1, extent2 = pos[axis1] - pos1[axis1], pos[axis2] - pos1[axis2]
if compare(extent1, extent2) then --transpose only if below the diagonal if compare(extent1, extent2) then --transpose only if below the diagonal
local node1 = env:get_node(pos) local node1 = get_node(pos)
local meta1 = env:get_meta(pos):to_table() local meta1 = get_meta(pos):to_table()
local value1, value2 = pos[axis1], pos[axis2] --save position values local value1, value2 = pos[axis1], pos[axis2] --save position values
pos[axis1], pos[axis2] = pos1[axis1] + extent2, pos1[axis2] + extent1 --swap axis extents pos[axis1], pos[axis2] = pos1[axis1] + extent2, pos1[axis2] + extent1 --swap axis extents
local node2 = env:get_node(pos) local node2 = get_node(pos)
local meta2 = env:get_meta(pos):to_table() local meta2 = get_meta(pos):to_table()
env:add_node(pos, node1) add_node(pos, node1)
env:get_meta(pos):from_table(meta1) get_meta(pos):from_table(meta1)
pos[axis1], pos[axis2] = value1, value2 --restore position values pos[axis1], pos[axis2] = value1, value2 --restore position values
env:add_node(pos, node2) add_node(pos, node2)
env:get_meta(pos):from_table(meta2) get_meta(pos):from_table(meta2)
end end
pos.z = pos.z + 1 pos.z = pos.z + 1
end 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 --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) worldedit.orient = function(pos1, pos2, angle, env)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2) local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
local nodes = minetest.registered_nodes local registered_nodes = minetest.registered_nodes
if env == nil then env = minetest.env end
local wallmounted = { local wallmounted = {
[90]={[0]=0, [1]=1, [2]=5, [3]=4, [4]=2, [5]=3}, [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}, [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 facedir_substitution = facedir[angle]
local count = 0 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} local pos = {x=pos1.x, y=0, z=0}
while pos.x <= pos2.x do while pos.x <= pos2.x do
pos.y = pos1.y pos.y = pos1.y
while pos.y <= pos2.y do while pos.y <= pos2.y do
pos.z = pos1.z pos.z = pos1.z
while pos.z <= pos2.z do while pos.z <= pos2.z do
local node = env:get_node(pos) local node = get_node(pos)
local def = nodes[node.name] local def = registered_nodes[node.name]
if def then if def then
if def.paramtype2 == "wallmounted" then if def.paramtype2 == "wallmounted" then
node.param2 = wallmounted_substitution[node.param2] node.param2 = wallmounted_substitution[node.param2]
local meta = env:get_meta(pos):to_table() local meta = get_meta(pos):to_table()
env:add_node(pos, node) add_node(pos, node)
env:get_meta(pos):from_table(meta) get_meta(pos):from_table(meta)
count = count + 1 count = count + 1
elseif def.paramtype2 == "facedir" then elseif def.paramtype2 == "facedir" then
node.param2 = facedir_substitution[node.param2] node.param2 = facedir_substitution[node.param2]
local meta = env:get_meta(pos):to_table() local meta = get_meta(pos):to_table()
env:add_node(pos, node) add_node(pos, node)
env:get_meta(pos):from_table(meta) get_meta(pos):from_table(meta)
count = count + 1 count = count + 1
end end
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 --fixes the lighting in a region defined by positions `pos1` and `pos2`, returning the number of nodes updated
worldedit.fixlight = function(pos1, pos2, env) worldedit.fixlight = function(pos1, pos2, env)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2) local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
if env == nil then env = minetest.env end local nodes = minetest.find_nodes_in_area(pos1, pos2, "air")
local count = 0 local dig_node = minetest.dig_node
for _, pos in ipairs(nodes) do
local pos = {x=pos1.x, y=pos2.y, z=0} dig_node(pos)
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 end
pos.z = pos.z + 1 return #nodes
end
pos.x = pos.x + 1
end
return count
end 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 --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) worldedit.hollow_sphere = function(pos, radius, nodename)
local insert = table.insert local insert = table.insert
local node = {nodename, 0, 0} local node = {name=nodename, param1=0, param2=0}
local ignore = {"ignore", 0, 0} local ignore = {name="ignore", param1=0, param2=0}
local nodes = {} local nodes = {}
local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1) local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
for x = -radius, radius do 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 --adds a sphere centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
worldedit.sphere = function(pos, radius, nodename) worldedit.sphere = function(pos, radius, nodename)
local insert = table.insert local insert = table.insert
local node = {nodename, 0, 0} local node = {name=nodename, param1=0, param2=0}
local ignore = {"ignore", 0, 0} local ignore = {name="ignore", param1=0, param2=0}
local nodes = {} local nodes = {}
local max_radius = radius * (radius + 1) local max_radius = radius * (radius + 1)
for x = -radius, radius do 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 --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 worldedit.hollow_dome = function(pos, radius, nodename) --wip: use bresenham sphere for maximum speed
local insert = table.insert local insert = table.insert
local node = {nodename, 0, 0} local node = {name=nodename, param1=0, param2=0}
local ignore = {"ignore", 0, 0} local ignore = {name="ignore", param1=0, param2=0}
local nodes = {} local nodes = {}
local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1) local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
for x = -radius, radius do 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 --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 worldedit.dome = function(pos, radius, nodename) --wip: use bresenham sphere for maximum speed
local insert = table.insert local insert = table.insert
local node = {nodename, 0, 0} local node = {name=nodename, param1=0, param2=0}
local ignore = {"ignore", 0, 0} local ignore = {name="ignore", param1=0, param2=0}
local nodes = {} local nodes = {}
local max_radius = radius * (radius + 1) local max_radius = radius * (radius + 1)
for x = -radius, radius do for x = -radius, radius do
@ -101,16 +101,23 @@ worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename)
other1, other2 = "x", "y" other1, other2 = "x", "y"
end end
if env == nil then env = minetest.env end
local currentpos = {x=pos.x, y=pos.y, z=pos.z} 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 if length < 0 then
length = -length length = -length
step = -1 currentpos[axis] = currentpos[axis] - length
end 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 for i = 1, length do
nodes[i] = node
end
local schematic = {size={[axis]=length, [other1]=1, [other2]=1}, data=nodes}
--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 offset1, offset2 = 0, radius
local delta = -radius local delta = -radius
while offset1 <= offset2 do while offset1 <= offset2 do
@ -118,25 +125,25 @@ worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename)
local first1, first2 = pos[other1] + offset1, pos[other1] - offset1 local first1, first2 = pos[other1] + offset1, pos[other1] - offset1
local second1, second2 = pos[other2] + offset2, pos[other2] - offset2 local second1, second2 = pos[other2] + offset2, pos[other2] - offset2
currentpos[other1], currentpos[other2] = first1, second1 currentpos[other1], currentpos[other2] = first1, second1
env:add_node(currentpos, node) --octant 1 place_schematic(currentpos, schematic) --octant 1
currentpos[other1] = first2 currentpos[other1] = first2
env:add_node(currentpos, node) --octant 4 place_schematic(currentpos, schematic) --octant 4
currentpos[other2] = second2 currentpos[other2] = second2
env:add_node(currentpos, node) --octant 5 place_schematic(currentpos, schematic) --octant 5
currentpos[other1] = first1 currentpos[other1] = first1
env:add_node(currentpos, node) --octant 8 place_schematic(currentpos, schematic) --octant 8
local first1, first2 = pos[other1] + offset2, pos[other1] - offset2 local first1, first2 = pos[other1] + offset2, pos[other1] - offset2
local second1, second2 = pos[other2] + offset1, pos[other2] - offset1 local second1, second2 = pos[other2] + offset1, pos[other2] - offset1
currentpos[other1], currentpos[other2] = first1, second1 currentpos[other1], currentpos[other2] = first1, second1
env:add_node(currentpos, node) --octant 2 place_schematic(currentpos, schematic) --octant 2
currentpos[other1] = first2 currentpos[other1] = first2
env:add_node(currentpos, node) --octant 3 place_schematic(currentpos, schematic) --octant 3
currentpos[other2] = second2 currentpos[other2] = second2
env:add_node(currentpos, node) --octant 6 place_schematic(currentpos, schematic) --octant 6
currentpos[other1] = first1 currentpos[other1] = first1
env:add_node(currentpos, node) --octant 7 place_schematic(currentpos, schematic) --octant 7
count = count + 8 --wip: broken count = count + (length *8) --wip: broken because sometimes currentpos is repeated
--move to next location --move to next location
delta = delta + (offset1 * 2) + 1 delta = delta + (offset1 * 2) + 1
@ -146,8 +153,6 @@ worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename)
end end
offset1 = offset1 + 1 offset1 = offset1 + 1
end end
currentpos[axis] = currentpos[axis] + step
end
return count return count
end end
@ -162,6 +167,7 @@ worldedit.cylinder = function(pos, axis, length, radius, nodename, env)
other1, other2 = "x", "y" other1, other2 = "x", "y"
end 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 if env == nil then env = minetest.env end
local currentpos = {x=pos.x, y=pos.y, z=pos.z} local currentpos = {x=pos.x, y=pos.y, z=pos.z}
local node = {name=nodename} 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 pos2x, pos2y, pos2z = pos.x + height, pos.y + height, pos.z + height
local pos = {x=0, y=pos1y, z=0} 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 count = 0
local node = {name=nodename} local node = {name=nodename}
if env == nil then env = minetest.env end 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 --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 worldedit.spiral = function(pos, width, height, spacer, nodename, env) --wip: clean this up
-- spiral matrix - http://rosettacode.org/wiki/Spiral_matrix#Lua -- 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 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 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 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 --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) 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 pos = {x=pos1.x, y=0, z=0}
local placeholder = {name="worldedit:placeholder", param1=0, param2=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 while pos.x <= pos2.x do
pos.y = pos1.y pos.y = pos1.y
while pos.y <= pos2.y do while pos.y <= pos2.y do
pos.z = pos1.z pos.z = pos1.z
while pos.z <= pos2.z do 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 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 local data = get_meta(pos):to_table() --obtain metadata of original node
env:add_node(pos, placeholder) --add placeholder node add_node(pos, placeholder) --add placeholder node
local meta = env:get_meta(pos) --obtain placeholder meta local meta = get_meta(pos) --obtain placeholder meta
meta:from_table(data) --set placeholder metadata to the original node's metadata 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 meta:set_string("worldedit_placeholder", node.name) --add the node's name
pos.z = pos.z + 1 pos.z = pos.z + 1
@ -59,18 +58,17 @@ worldedit.hide = function(pos1, pos2, tenv)
end 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 --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) 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 placeholder = {name="worldedit:placeholder", param1=0, param2=0}
local nodes = minetest.find_nodes_in_area(pos1, pos2, nodename) 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 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 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 local data = get_meta(pos):to_table() --obtain metadata of original node
env:add_node(pos, placeholder) --add placeholder node add_node(pos, placeholder) --add placeholder node
local meta = env:get_meta(pos) --obtain placeholder meta local meta = get_meta(pos) --obtain placeholder meta
meta:from_table(data) --set placeholder metadata to the original node's metadata meta:from_table(data) --set placeholder metadata to the original node's metadata
meta:set_string("worldedit_placeholder", nodename) --add the node's name meta:set_string("worldedit_placeholder", nodename) --add the node's name
end end
@ -78,30 +76,25 @@ worldedit.suppress = function(pos1, pos2, nodename, tenv)
end 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 --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) 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 pos = {x=pos1.x, y=0, z=0}
local placeholder = {name="worldedit:placeholder", param1=0, param2=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 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 pos.y = pos1.y
while pos.y <= pos2.y do while pos.y <= pos2.y do
pos.z = pos1.z pos.z = pos1.z
while pos.z <= pos2.z do while pos.z <= pos2.z do
local node = env:get_node(pos) local node = get_node(pos)
if node.name == nodename then --node found if node.name == nodename then --node found
count = count + 1 count = count + 1
else --hide other nodes else --hide other nodes
placeholder.param1, placeholder.param2 = node.param1, node.param2 --copy node's param1 and param2 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 local data = get_meta(pos):to_table() --obtain metadata of original node
env:add_node(pos, placeholder) --add placeholder node add_node(pos, placeholder) --add placeholder node
local meta = env:get_meta(pos) --obtain placeholder meta local meta = get_meta(pos) --obtain placeholder meta
meta:from_table(data) --set placeholder metadata to the original node's metadata 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 meta:set_string("worldedit_placeholder", node.name) --add the node's name
end end
@ -115,20 +108,19 @@ worldedit.highlight = function(pos1, pos2, nodename, tenv)
end end
--restores all nodes hidden with WorldEdit functions in a region defined by positions `pos1` and `pos2`, returning the number of nodes restored --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) local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
if env == nil then env = minetest.env end
local node = {name="", param1=0, param2=0} 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 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 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 node.name = data.fields.worldedit_placeholder --set node name
data.fields.worldedit_placeholder = nil --delete old nodename data.fields.worldedit_placeholder = nil --delete old nodename
env:add_node(pos, placeholder) --add original node add_node(pos, placeholder) --add original node
env:get_meta(pos):from_table(data) --set original node metadata get_meta(pos):from_table(data) --set original node metadata
end end
return #nodes return #nodes
end end