Skip to content

Commit

Permalink
Add //hollowcube and //cube
Browse files Browse the repository at this point in the history
closes #143
  • Loading branch information
sfan5 committed Aug 31, 2017
1 parent e356f45 commit 870873a
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ChatCommands.md
Expand Up @@ -17,6 +17,7 @@ Many commands also have shorter names that can be typed faster. For example, if
| `//s` | `//set` |
| `//r` | `//replace` |
| `//ri` | `//replaceinverse` |
| `//hcube` | `//hollowcube` |
| `//hspr` | `//hollowsphere` |
| `//spr` | `//sphere` |
| `//hdo` | `//hollowdome` |
Expand Down Expand Up @@ -143,6 +144,19 @@ Replace all nodes other than `<search node>` with `<replace node>` in the curren
//replaceinverse dirt Bronze Block
//replaceinverse mesecons:wire_00000000_off flowers:flower_tulip

### `//hollowcube <width> <height> <length> <node>`

Adds a hollow cube with its ground level centered at WorldEdit position 1 with dimensions `<width>` x `<height>` x `<length>`, composed of `<node>`.

//hollowcube 6 5 6 Diamond Block

### `//cube <width> <height> <length> <node>`

Adds a cube with its ground level centered at WorldEdit position 1 with dimensions `<width>` x `<height>` x `<length>`, composed of `<node>`.

//cube 6 5 6 Diamond Block
//cube 7 2 1 default:cobble

### `//hollowsphere <radius> <node>`

Add hollow sphere centered at WorldEdit position 1 with radius `<radius>`, composed of `<node>`.
Expand Down
6 changes: 6 additions & 0 deletions WorldEdit API.md
Expand Up @@ -115,6 +115,12 @@ Primitives
----------
Contained in primitives.lua, this module allows the creation of several geometric primitives.

### count = worldedit.cube(pos, width, height, length, node_name, hollow)

Adds a cube with its ground level centered at `pos`, the dimensions `width` x `height` x `length`, composed of `node_name`.

Returns the number of nodes added.

### count = worldedit.sphere(pos, radius, node_name, hollow)

Adds a sphere centered at `pos` with radius `radius`, composed of `node_name`.
Expand Down
41 changes: 41 additions & 0 deletions worldedit/primitives.lua
Expand Up @@ -4,6 +4,47 @@
local mh = worldedit.manip_helpers


--- Adds a cube
-- @param pos Position of ground level center of cube
-- @param width Cube width. (x)
-- @param height Cube height. (y)
-- @param length Cube length. (z)
-- @param node_name Name of node to make cube of.
-- @param hollow Whether the cube should be hollow.
-- @return The number of nodes added.
function worldedit.cube(pos, width, height, length, node_name, hollow)
-- Set up voxel manipulator
local basepos = vector.subtract(pos, {x=math.floor(width/2), y=0, z=math.floor(length/2)})
local manip, area = mh.init(basepos, vector.add(basepos, {x=width, y=height, z=length}))
local data = mh.get_empty_data(area)

-- Add cube
local node_id = minetest.get_content_id(node_name)
local stride = {x=1, y=area.ystride, z=area.zstride}
local offset = vector.subtract(basepos, area.MinEdge)
local count = 0

for z = 0, length-1 do
local index_z = (offset.z + z) * stride.z + 1 -- +1 for 1-based indexing
for y = 0, height-1 do
local index_y = index_z + (offset.y + y) * stride.y
for x = 0, width-1 do
local is_wall = z == 0 or z == length-1
or y == 0 or y == height-1
or x == 0 or x == width-1
if not hollow or is_wall then
local i = index_y + (offset.x + x)
data[i] = node_id
count = count + 1
end
end
end
end

mh.finish(manip, data)
return count
end

--- Adds a sphere of `node_name` centered at `pos`.
-- @param pos Position to center sphere at.
-- @param radius Sphere radius.
Expand Down
39 changes: 39 additions & 0 deletions worldedit_commands/init.lua
Expand Up @@ -455,6 +455,45 @@ minetest.register_chatcommand("/replaceinverse", {
end, check_replace),
})

local check_cube = function(name, param)
if worldedit.pos1[name] == nil then
worldedit.player_notify(name, "no position 1 selected")
return nil
end
local found, _, w, h, l, nodename = param:find("^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$")
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 tonumber(w) * tonumber(h) * tonumber(l)
end

minetest.register_chatcommand("/hollowcube", {
params = "<width> <height> <length> <node>",
description = "Add a hollow cube with its ground level centered at WorldEdit position 1 with dimensions <width> x <height> x <length>, composed of <node>.",
privs = {worldedit=true},
func = safe_region(function(name, param)
local found, _, w, h, l, nodename = param:find("^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$")
local node = get_node(name, nodename)
local count = worldedit.cube(worldedit.pos1[name], tonumber(w), tonumber(h), tonumber(l), node, true)
worldedit.player_notify(name, count .. " nodes added")
end, check_cube),
})

minetest.register_chatcommand("/cube", {
params = "<width> <height> <length> <node>",
description = "Add a cube with its ground level centered at WorldEdit position 1 with dimensions <width> x <height> x <length>, composed of <node>.",
privs = {worldedit=true},
func = safe_region(function(name, param)
local found, _, w, h, l, nodename = param:find("^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$")
local node = get_node(name, nodename)
local count = worldedit.cube(worldedit.pos1[name], tonumber(w), tonumber(h), tonumber(l), node)
worldedit.player_notify(name, count .. " nodes added")
end, check_cube),
})

local check_sphere = function(name, param)
if worldedit.pos1[name] == nil then
worldedit.player_notify(name, "no position 1 selected")
Expand Down
1 change: 1 addition & 0 deletions worldedit_shortcommands/init.lua
Expand Up @@ -25,6 +25,7 @@ worldedit.alias_chatcommand("/v", "/volume")
worldedit.alias_chatcommand("/s", "/set")
worldedit.alias_chatcommand("/r", "/replace")
worldedit.alias_chatcommand("/ri", "/replaceinverse")
worldedit.alias_chatcommand("/hcube", "/hollowcube")
worldedit.alias_chatcommand("/hspr", "/hollowsphere")
worldedit.alias_chatcommand("/spr", "/sphere")
worldedit.alias_chatcommand("/hdo", "/hollowdome")
Expand Down

0 comments on commit 870873a

Please sign in to comment.