diff --git a/ChatCommands.md b/ChatCommands.md index be885c0..9d3f9d8 100644 --- a/ChatCommands.md +++ b/ChatCommands.md @@ -175,23 +175,35 @@ Add dome centered at WorldEdit position 1 with radius ``, composed of `< //dome -12 glass //dome 17 mesecons:wire_00000000_off -### `//hollowcylinder x/y/z/? ` +### `//hollowcylinder x/y/z/? [radius2] ` -Add hollow cylinder at WorldEdit position 1 along the x/y/z/? axis with length `` and radius ``, composed of ``. +Add hollow cylinder at WorldEdit position 1 along the x/y/z/? axis with length ``, base radius `` (and top radius `[radius2]`), composed of ``. + +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 y 28 10 glass //hollowcylinder z -12 3 mesecons:wire_00000000_off //hollowcylinder ? 2 4 default:stone -### `//cylinder x/y/z/? ` + //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 `` and radius ``, composed of ``. +### `//cylinder x/y/z/? [radius2] ` + +Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length ``, base radius `` (and top radius `[radius2]`), composed of ``. +Can also create shapes other than cylinders, e.g. cones (see documentation above). //cylinder x +5 8 Bronze Block //cylinder y 28 10 glass //cylinder z -12 3 mesecons:wire_00000000_off //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? ` @@ -432,4 +444,4 @@ or vertically in the y axis `[v]`. Contracts the selection in all directions by ``. If specified, the selection can be contracted horizontally in the x and z axes `[h]` or vertically in the y axis `[v]`. - //outset v 5 \ No newline at end of file + //outset v 5 diff --git a/WorldEdit API.md b/WorldEdit API.md index 5867dfc..8488305 100644 --- a/WorldEdit API.md +++ b/WorldEdit API.md @@ -127,9 +127,9 @@ Adds a dome centered at `pos` with radius `radius`, composed of `node_name`. 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. diff --git a/worldedit/primitives.lua b/worldedit/primitives.lua index fe22fff..9b86cd8 100644 --- a/worldedit/primitives.lua +++ b/worldedit/primitives.lua @@ -92,49 +92,60 @@ end -- @param pos Position to center base of cylinder at. -- @param axis Axis ("x", "y", or "z") -- @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 hollow Whether the cylinder should be hollow. -- @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) + -- Backwards compatibility + if type(radius2) == "string" then + hollow = node_name + node_name = radius2 + radius2 = radius1 -- straight cylinder + end + -- Handle negative lengths local current_pos = {x=pos.x, y=pos.y, z=pos.z} if length < 0 then length = -length current_pos[axis] = current_pos[axis] - length + radius1, radius2 = radius2, radius1 end -- 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) - -- Add cylinder + -- Add desired shape (anything inbetween cylinder & cone) 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 offset = { x = current_pos.x - area.MinEdge.x, y = current_pos.y - area.MinEdge.y, z = current_pos.z - area.MinEdge.z, } - local min_slice, max_slice = offset[axis], offset[axis] + length - 1 local count = 0 - for index2 = -radius, radius do - -- Offset contributed by other axis 1 plus 1 to make it 1-indexed - local new_index2 = (index2 + offset[other1]) * stride[other1] + 1 - for index3 = -radius, radius do - 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 column along axis - for index1 = min_slice, max_slice do - local vi = new_index3 + index1 * stride[axis] + for i = 0, length - 1 do + -- Calulate radius for this "height" in the cylinder + local radius = radius1 + (radius2 - radius1) * (i - 1) / (length - 1) + radius = math.floor(radius + 0.5) -- round + local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1) + + for index2 = -radius, radius do + -- Offset contributed by other axis 1 plus 1 to make it 1-indexed + local new_index2 = (index2 + offset[other1]) * stride[other1] + 1 + for index3 = -radius, radius do + 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 + count = count + 1 end - count = count + length end end end diff --git a/worldedit_commands/init.lua b/worldedit_commands/init.lua index 325a31c..f423d67 100644 --- a/worldedit_commands/init.lua +++ b/worldedit_commands/init.lua @@ -538,46 +538,65 @@ local check_cylinder = function(name, param) worldedit.player_notify(name, "no position 1 selected") return nil 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 worldedit.player_notify(name, "invalid usage: " .. param) return nil end local node = get_node(name, nodename) 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 minetest.register_chatcommand("/hollowcylinder", { - params = "x/y/z/? ", - description = "Add hollow cylinder at WorldEdit position 1 along the x/y/z/? axis with length and radius , composed of ", + params = "x/y/z/? [radius2] ", + description = "Add hollow cylinder at WorldEdit position 1 along the x/y/z/? axis with length , base radius (and top radius [radius2]), composed of ", privs = {worldedit=true}, 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) if axis == "?" then axis, sign = worldedit.player_axis(name) length = length * sign end 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") end, check_cylinder), }) minetest.register_chatcommand("/cylinder", { - params = "x/y/z/? ", - description = "Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length and radius , composed of ", + params = "x/y/z/? [radius2] ", + description = "Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length , base radius (and top radius [radius2]), composed of ", privs = {worldedit=true}, 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) if axis == "?" then axis, sign = worldedit.player_axis(name) length = length * sign end 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") end, check_cylinder), })