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

Dual-based cylinder

This commit is contained in:
Kyle 2012-10-18 18:10:30 -07:00 committed by sfan5
parent 6ceb56c3dc
commit bf8e2a8233
4 changed files with 77 additions and 35 deletions

View File

@ -175,24 +175,36 @@ Add dome centered at WorldEdit position 1 with radius `<radius>`, composed of `<
//dome -12 glass //dome -12 glass
//dome 17 mesecons:wire_00000000_off //dome 17 mesecons:wire_00000000_off
### `//hollowcylinder x/y/z/? <length> <radius> <node>` ### `//hollowcylinder x/y/z/? <length> <radius1> [radius2] <node>`
Add hollow cylinder at WorldEdit position 1 along the x/y/z/? axis with length `<length>` and radius `<radius>`, composed of `<node>`. Add hollow cylinder at WorldEdit position 1 along the x/y/z/? axis with length `<length>`, base radius `<radius1>` (and top radius `[radius2]`), composed of `<node>`.
Despite its name this command allows you to create cones (`radius2` = 0) as well as any shapes inbetween (0 < `radius2` < `radius1`).
Swapping `radius1` and `radius2` will create the same object but upside-down.
//hollowcylinder x +5 8 Bronze Block //hollowcylinder x +5 8 Bronze Block
//hollowcylinder y 28 10 glass //hollowcylinder y 28 10 glass
//hollowcylinder z -12 3 mesecons:wire_00000000_off //hollowcylinder z -12 3 mesecons:wire_00000000_off
//hollowcylinder ? 2 4 default:stone //hollowcylinder ? 2 4 default:stone
### `//cylinder x/y/z/? <length> <radius> <node>` //hollowcylinder y 10 10 0 walls:cobble
//hollowcylinder x 6 0 5 Dirt
//hollowcylinder z 20 10 20 default:desert_stone
Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length `<length>` and radius `<radius>`, composed of `<node>`. ### `//cylinder x/y/z/? <length> <radius1> [radius2] <node>`
Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length `<length>`, base radius `<radius1>` (and top radius `[radius2]`), composed of `<node>`.
Can also create shapes other than cylinders, e.g. cones (see documentation above).
//cylinder x +5 8 Bronze Block //cylinder x +5 8 Bronze Block
//cylinder y 28 10 glass //cylinder y 28 10 glass
//cylinder z -12 3 mesecons:wire_00000000_off //cylinder z -12 3 mesecons:wire_00000000_off
//cylinder ? 2 4 default:stone //cylinder ? 2 4 default:stone
//cylinder y 10 10 0 walls:cobble
//cylinder x 6 0 5 Dirt
//cylinder z 20 10 20 default:desert_stone
### `//hollowpyramid x/y/z? <height> <node>` ### `//hollowpyramid x/y/z? <height> <node>`
Add hollow pyramid centered at WorldEdit position 1 along the x/y/z/? axis with height `<height>`, composed of `<node>`. Add hollow pyramid centered at WorldEdit position 1 along the x/y/z/? axis with height `<height>`, composed of `<node>`.

View File

@ -127,9 +127,9 @@ Adds a dome centered at `pos` with radius `radius`, composed of `node_name`.
Returns the number of nodes added. Returns the number of nodes added.
### count = worldedit.cylinder(pos, axis, length, radius, node_name, hollow) ### count = worldedit.cylinder(pos, axis, length, radius1, radius2, node_name, hollow)
Adds a cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `length` and radius `radius`, composed of `node_name`. Adds a cylinder-like at `pos` along the `axis` axis ("x" or "y" or "z") with length `length`, base radius `radius1` and top radius `radius2`, composed of `node_name`.
Returns the number of nodes added. Returns the number of nodes added.

View File

@ -92,49 +92,60 @@ end
-- @param pos Position to center base of cylinder at. -- @param pos Position to center base of cylinder at.
-- @param axis Axis ("x", "y", or "z") -- @param axis Axis ("x", "y", or "z")
-- @param length Cylinder length. -- @param length Cylinder length.
-- @param radius Cylinder radius. -- @param radius1 Cylinder base radius.
-- @param radius2 Cylinder top radius.
-- @param node_name Name of node to make cylinder of. -- @param node_name Name of node to make cylinder of.
-- @param hollow Whether the cylinder should be hollow. -- @param hollow Whether the cylinder should be hollow.
-- @return The number of nodes added. -- @return The number of nodes added.
function worldedit.cylinder(pos, axis, length, radius, node_name, hollow) function worldedit.cylinder(pos, axis, length, radius1, radius2, node_name, hollow)
local other1, other2 = worldedit.get_axis_others(axis) local other1, other2 = worldedit.get_axis_others(axis)
-- Backwards compatibility
if type(radius2) == "string" then
hollow = node_name
node_name = radius2
radius2 = radius1 -- straight cylinder
end
-- Handle negative lengths -- Handle negative lengths
local current_pos = {x=pos.x, y=pos.y, z=pos.z} local current_pos = {x=pos.x, y=pos.y, z=pos.z}
if length < 0 then if length < 0 then
length = -length length = -length
current_pos[axis] = current_pos[axis] - length current_pos[axis] = current_pos[axis] - length
radius1, radius2 = radius2, radius1
end end
-- Set up voxel manipulator -- Set up voxel manipulator
local manip, area = mh.init_axis_radius_length(current_pos, axis, radius, length) local manip, area = mh.init_axis_radius_length(current_pos, axis, math.max(radius1, radius2), length)
local data = mh.get_empty_data(area) local data = mh.get_empty_data(area)
-- Add cylinder -- Add desired shape (anything inbetween cylinder & cone)
local node_id = minetest.get_content_id(node_name) local node_id = minetest.get_content_id(node_name)
local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
local stride = {x=1, y=area.ystride, z=area.zstride} local stride = {x=1, y=area.ystride, z=area.zstride}
local offset = { local offset = {
x = current_pos.x - area.MinEdge.x, x = current_pos.x - area.MinEdge.x,
y = current_pos.y - area.MinEdge.y, y = current_pos.y - area.MinEdge.y,
z = current_pos.z - area.MinEdge.z, z = current_pos.z - area.MinEdge.z,
} }
local min_slice, max_slice = offset[axis], offset[axis] + length - 1
local count = 0 local count = 0
for index2 = -radius, radius do for i = 0, length - 1 do
-- Offset contributed by other axis 1 plus 1 to make it 1-indexed -- Calulate radius for this "height" in the cylinder
local new_index2 = (index2 + offset[other1]) * stride[other1] + 1 local radius = radius1 + (radius2 - radius1) * (i - 1) / (length - 1)
for index3 = -radius, radius do radius = math.floor(radius + 0.5) -- round
local new_index3 = new_index2 + (index3 + offset[other2]) * stride[other2] local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
local squared = index2 * index2 + index3 * index3
if squared <= max_radius and (not hollow or squared >= min_radius) then for index2 = -radius, radius do
-- Position is in cylinder -- Offset contributed by other axis 1 plus 1 to make it 1-indexed
-- Add column along axis local new_index2 = (index2 + offset[other1]) * stride[other1] + 1
for index1 = min_slice, max_slice do for index3 = -radius, radius do
local vi = new_index3 + index1 * stride[axis] local new_index3 = new_index2 + (index3 + offset[other2]) * stride[other2]
local squared = index2 * index2 + index3 * index3
if squared <= max_radius and (not hollow or squared >= min_radius) then
-- Position is in cylinder, add node here
local vi = new_index3 + (offset[axis] + i) * stride[axis]
data[vi] = node_id data[vi] = node_id
count = count + 1
end end
count = count + length
end end
end end
end end

View File

@ -538,46 +538,65 @@ local check_cylinder = function(name, param)
worldedit.player_notify(name, "no position 1 selected") worldedit.player_notify(name, "no position 1 selected")
return nil return nil
end end
local found, _, axis, length, radius, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$") -- two radii
local found, _, axis, length, radius1, radius2, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(%d+)%s+(.+)$")
if found == nil then
-- single radius
found, _, axis, length, radius1, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$")
radius2 = radius1
end
if found == nil then if found == nil then
worldedit.player_notify(name, "invalid usage: " .. param) worldedit.player_notify(name, "invalid usage: " .. param)
return nil return nil
end end
local node = get_node(name, nodename) local node = get_node(name, nodename)
if not node then return nil end if not node then return nil end
return math.ceil(math.pi * (tonumber(radius) ^ 2) * tonumber(length)) local radius = math.max(tonumber(radius1), tonumber(radius2))
return math.ceil(math.pi * (radius ^ 2) * tonumber(length))
end end
minetest.register_chatcommand("/hollowcylinder", { minetest.register_chatcommand("/hollowcylinder", {
params = "x/y/z/? <length> <radius> <node>", params = "x/y/z/? <length> <radius1> [radius2] <node>",
description = "Add hollow cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length> and radius <radius>, composed of <node>", description = "Add hollow cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length>, base radius <radius1> (and top radius [radius2]), composed of <node>",
privs = {worldedit=true}, privs = {worldedit=true},
func = safe_region(function(name, param) func = safe_region(function(name, param)
local found, _, axis, length, radius, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$") -- two radii
local found, _, axis, length, radius1, radius2, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(%d+)%s+(.+)$")
if found == nil then
-- single radius
found, _, axis, length, radius1, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$")
radius2 = radius1
end
length = tonumber(length) length = tonumber(length)
if axis == "?" then if axis == "?" then
axis, sign = worldedit.player_axis(name) axis, sign = worldedit.player_axis(name)
length = length * sign length = length * sign
end end
local node = get_node(name, nodename) local node = get_node(name, nodename)
local count = worldedit.cylinder(worldedit.pos1[name], axis, length, tonumber(radius), node, true) local count = worldedit.cylinder(worldedit.pos1[name], axis, length, tonumber(radius1), tonumber(radius2), node, true)
worldedit.player_notify(name, count .. " nodes added") worldedit.player_notify(name, count .. " nodes added")
end, check_cylinder), end, check_cylinder),
}) })
minetest.register_chatcommand("/cylinder", { minetest.register_chatcommand("/cylinder", {
params = "x/y/z/? <length> <radius> <node>", params = "x/y/z/? <length> <radius1> [radius2] <node>",
description = "Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length> and radius <radius>, composed of <node>", description = "Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length>, base radius <radius1> (and top radius [radius2]), composed of <node>",
privs = {worldedit=true}, privs = {worldedit=true},
func = safe_region(function(name, param) func = safe_region(function(name, param)
local found, _, axis, length, radius, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$") -- two radii
local found, _, axis, length, radius1, radius2, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(%d+)%s+(.+)$")
if found == nil then
-- single radius
found, _, axis, length, radius1, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$")
radius2 = radius1
end
length = tonumber(length) length = tonumber(length)
if axis == "?" then if axis == "?" then
axis, sign = worldedit.player_axis(name) axis, sign = worldedit.player_axis(name)
length = length * sign length = length * sign
end end
local node = get_node(name, nodename) local node = get_node(name, nodename)
local count = worldedit.cylinder(worldedit.pos1[name], axis, length, tonumber(radius), node) local count = worldedit.cylinder(worldedit.pos1[name], axis, length, tonumber(radius1), tonumber(radius2), node)
worldedit.player_notify(name, count .. " nodes added") worldedit.player_notify(name, count .. " nodes added")
end, check_cylinder), end, check_cylinder),
}) })