forked from mtcontrib/Minetest-WorldEdit
Super-speed a few primitives and visualization functions, including hollowsphere, sphere, hollowdome, dome, and suppress.
This commit is contained in:
parent
58970e7fab
commit
1e5f623cbb
|
@ -67,7 +67,7 @@ worldedit.replaceinverse = function(pos1, pos2, searchnode, replacenode, env)
|
|||
local pos = {x=pos1.x, y=0, z=0}
|
||||
local node = {name=replacenode}
|
||||
local count = 0
|
||||
while pos.x <= pos2.x do --wip: see if this can be sped up like worldedit.replace
|
||||
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
|
||||
pos.y = pos1.y
|
||||
while pos.y <= pos2.y do
|
||||
pos.z = pos1.z
|
||||
|
|
|
@ -1,101 +1,97 @@
|
|||
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, env)
|
||||
local node = {name=nodename}
|
||||
local pos1 = {x=0, y=0, z=0}
|
||||
local min_radius = radius * (radius - 1)
|
||||
local max_radius = radius * (radius + 1)
|
||||
local count = 0
|
||||
if env == nil then env = minetest.env end
|
||||
worldedit.hollow_sphere = function(pos, radius, nodename)
|
||||
local insert = table.insert
|
||||
local node = {nodename, 0, 0}
|
||||
local ignore = {"ignore", 0, 0}
|
||||
local nodes = {}
|
||||
local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
|
||||
for x = -radius, radius do
|
||||
pos1.x = pos.x + x
|
||||
for y = -radius, radius do
|
||||
pos1.y = pos.y + y
|
||||
for z = -radius, radius do
|
||||
if x*x+y*y+z*z >= min_radius and x*x+y*y+z*z <= max_radius then
|
||||
pos1.z = pos.z + z
|
||||
env:add_node(pos1, node)
|
||||
count = count + 1
|
||||
local squared = x * x + y * y + z * z
|
||||
if squared >= min_radius and squared <= max_radius then
|
||||
insert(nodes, node)
|
||||
else
|
||||
insert(nodes, ignore)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return count
|
||||
minetest.place_schematic({x=pos.x - radius, y=pos.y - radius, z=pos.z - radius}, {size={x=radius * 2, y=radius * 2, z=radius * 2}, data=nodes})
|
||||
return #nodes
|
||||
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, env)
|
||||
local node = {name=nodename}
|
||||
local pos1 = {x=0, y=0, z=0}
|
||||
worldedit.sphere = function(pos, radius, nodename)
|
||||
local insert = table.insert
|
||||
local node = {nodename, 0, 0}
|
||||
local ignore = {"ignore", 0, 0}
|
||||
local nodes = {}
|
||||
local max_radius = radius * (radius + 1)
|
||||
local count = 0
|
||||
if env == nil then env = minetest.env end
|
||||
for x = -radius, radius do
|
||||
pos1.x = pos.x + x
|
||||
for y = -radius, radius do
|
||||
pos1.y = pos.y + y
|
||||
for z = -radius, radius do
|
||||
if x*x+y*y+z*z <= max_radius then
|
||||
pos1.z = pos.z + z
|
||||
env:add_node(pos1, node)
|
||||
count = count + 1
|
||||
if x * x + y * y + z * z <= max_radius then
|
||||
insert(nodes, node)
|
||||
else
|
||||
insert(nodes, ignore)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return count
|
||||
minetest.place_schematic({x=pos.x - radius, y=pos.y - radius, z=pos.z - radius}, {size={x=radius * 2, y=radius * 2, z=radius * 2}, data=nodes})
|
||||
return #nodes
|
||||
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, env) --wip: use bresenham sphere for maximum speed
|
||||
local node = {name=nodename}
|
||||
local pos1 = {x=0, y=0, z=0}
|
||||
local min_radius = radius * (radius - 1)
|
||||
local max_radius = radius * (radius + 1)
|
||||
local count = 0
|
||||
if env == nil then env = minetest.env end
|
||||
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 nodes = {}
|
||||
local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
|
||||
for x = -radius, radius do
|
||||
pos1.x = pos.x + x
|
||||
for y = 0, radius do
|
||||
pos1.y = pos.y + y
|
||||
for z = -radius, radius do
|
||||
if x*x+y*y+z*z >= min_radius and x*x+y*y+z*z <= max_radius then
|
||||
pos1.z = pos.z + z
|
||||
env:add_node(pos1, node)
|
||||
count = count + 1
|
||||
local squared = x * x + y * y + z * z
|
||||
if squared >= min_radius and squared <= max_radius then
|
||||
insert(nodes, node)
|
||||
else
|
||||
insert(nodes, ignore)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return count
|
||||
minetest.place_schematic({x=pos.x - radius, y=pos.y, z=pos.z - radius}, {size={x=radius * 2, y=radius * 2, z=radius * 2}, data=nodes})
|
||||
return #nodes
|
||||
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, env) --wip: use bresenham sphere for maximum speed
|
||||
local node = {name=nodename}
|
||||
local pos1 = {x=0, y=0, z=0}
|
||||
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 nodes = {}
|
||||
local max_radius = radius * (radius + 1)
|
||||
local count = 0
|
||||
if env == nil then env = minetest.env end
|
||||
for x = -radius, radius do
|
||||
pos1.x = pos.x + x
|
||||
for y = 0, radius do
|
||||
pos1.y = pos.y + y
|
||||
for z = -radius, radius do
|
||||
if x*x+y*y+z*z <= max_radius then
|
||||
pos1.z = pos.z + z
|
||||
env:add_node(pos1, node)
|
||||
count = count + 1
|
||||
if x * x + y * y + z * z <= max_radius then
|
||||
insert(nodes, node)
|
||||
else
|
||||
insert(nodes, ignore)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return count
|
||||
minetest.place_schematic({x=pos.x - radius, y=pos.y, z=pos.z - radius}, {size={x=radius * 2, y=radius * 2, z=radius * 2}, data=nodes})
|
||||
return #nodes
|
||||
end
|
||||
|
||||
--adds a hollow cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `length` and radius `radius`, composed of `nodename`, returning the number of nodes added
|
||||
worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename, env)
|
||||
worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename)
|
||||
local other1, other2
|
||||
if axis == "x" then
|
||||
other1, other2 = "y", "z"
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user