From 870873ad151defd9ed59ddf5e4937a58166704d8 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Thu, 31 Aug 2017 19:07:51 +0200 Subject: [PATCH] Add //hollowcube and //cube closes #143 --- ChatCommands.md | 14 +++++++++++ WorldEdit API.md | 6 +++++ worldedit/primitives.lua | 41 ++++++++++++++++++++++++++++++++ worldedit_commands/init.lua | 39 ++++++++++++++++++++++++++++++ worldedit_shortcommands/init.lua | 1 + 5 files changed, 101 insertions(+) diff --git a/ChatCommands.md b/ChatCommands.md index 9d3f9d8..2a43c05 100644 --- a/ChatCommands.md +++ b/ChatCommands.md @@ -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` | @@ -143,6 +144,19 @@ Replace all nodes other than `` with `` in the curren //replaceinverse dirt Bronze Block //replaceinverse mesecons:wire_00000000_off flowers:flower_tulip +### `//hollowcube ` + +Adds a hollow cube with its ground level centered at WorldEdit position 1 with dimensions `` x `` x ``, composed of ``. + + //hollowcube 6 5 6 Diamond Block + +### `//cube ` + +Adds a cube with its ground level centered at WorldEdit position 1 with dimensions `` x `` x ``, composed of ``. + + //cube 6 5 6 Diamond Block + //cube 7 2 1 default:cobble + ### `//hollowsphere ` Add hollow sphere centered at WorldEdit position 1 with radius ``, composed of ``. diff --git a/WorldEdit API.md b/WorldEdit API.md index 8488305..1106fc7 100644 --- a/WorldEdit API.md +++ b/WorldEdit API.md @@ -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`. diff --git a/worldedit/primitives.lua b/worldedit/primitives.lua index 9b86cd8..4a09fba 100644 --- a/worldedit/primitives.lua +++ b/worldedit/primitives.lua @@ -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. diff --git a/worldedit_commands/init.lua b/worldedit_commands/init.lua index f423d67..61231c3 100644 --- a/worldedit_commands/init.lua +++ b/worldedit_commands/init.lua @@ -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 = " ", + description = "Add a hollow cube with its ground level centered at WorldEdit position 1 with dimensions x x , composed of .", + 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 = " ", + description = "Add a cube with its ground level centered at WorldEdit position 1 with dimensions x x , composed of .", + 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") diff --git a/worldedit_shortcommands/init.lua b/worldedit_shortcommands/init.lua index a4350ae..da02b75 100644 --- a/worldedit_shortcommands/init.lua +++ b/worldedit_shortcommands/init.lua @@ -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")