From 1e5f623cbb24bd312d54a23597ba8501359c0d6f Mon Sep 17 00:00:00 2001 From: Anthony Zhang Date: Sat, 22 Jun 2013 23:08:51 -0400 Subject: Super-speed a few primitives and visualization functions, including hollowsphere, sphere, hollowdome, dome, and suppress. --- worldedit/visualization.lua | 72 +++++++++++++-------------------------------- 1 file changed, 21 insertions(+), 51 deletions(-) (limited to 'worldedit/visualization.lua') diff --git a/worldedit/visualization.lua b/worldedit/visualization.lua index 9e99659..8e27c27 100644 --- a/worldedit/visualization.lua +++ b/worldedit/visualization.lua @@ -63,35 +63,18 @@ worldedit.suppress = function(pos1, pos2, nodename, tenv) 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 count = 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) - if node.name == nodename then - 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 - meta:from_table(data) --set placeholder metadata to the original node's metadata - meta:set_string("worldedit_placeholder", nodename) --add the node's name - count = count + 1 - end - pos.z = pos.z + 1 - end - pos.y = pos.y + 1 - end - pos.x = pos.x + 1 + local nodes = minetest.find_nodes_in_area(pos1, pos2, nodename) + for _, pos in ipairs(nodes) do + local node = env: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 + 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 - return count + return #nodes 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 @@ -106,7 +89,7 @@ worldedit.highlight = function(pos1, pos2, nodename, tenv) local pos = {x=pos1.x, y=0, z=0} local placeholder = {name="worldedit:placeholder", param1=0, param2=0} local count = 0 - while pos.x <= pos2.x do + 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 pos.y = pos1.y while pos.y <= pos2.y do pos.z = pos1.z @@ -136,29 +119,16 @@ worldedit.restore = function(pos1, pos2, tenv) 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 node = {name="", param1=0, param2=0} - local count = 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 currentnode = env:get_node(pos) - if currentnode.name == "worldedit:placeholder" then - 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 - node.name = data.fields.worldedit_placeholder --set node name - data.fields.worldedit_placeholder = nil --delete old nodename - env:add_node(pos, node) --add original node - env:get_meta(pos):from_table(data) --set original node metadata - count = count + 1 - end - pos.z = pos.z + 1 - end - pos.y = pos.y + 1 - end - pos.x = pos.x + 1 + local nodes = minetest.find_nodes_in_area(pos1, pos2, nodename) + for _, pos in ipairs(nodes) do + local currentnode = env: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 + 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 end - return count + return #nodes end -- cgit v1.2.3