1 worldedit.register_command("outset", {
2 params = "[h/v] <amount>",
3 description = "Outset the selected region.",
4 privs = {worldedit=true},
6 parse = function(param)
7 local find, _, dir, amount = param:find("(%a*)%s*([+-]?%d+)")
12 local hv_test = dir:find("[^hv]+")
13 if hv_test ~= nil then
14 return false, "Invalid direction."
17 return true, dir, tonumber(amount)
19 func = function(name, dir, amount)
20 if dir == "" or dir == "hv" or dir == "vh" then
21 assert(worldedit.cuboid_volumetric_expand(name, amount))
22 elseif dir == "h" then
23 assert(worldedit.cuboid_linear_expand(name, 'x', 1, amount))
24 assert(worldedit.cuboid_linear_expand(name, 'x', -1, amount))
25 assert(worldedit.cuboid_linear_expand(name, 'z', 1, amount))
26 assert(worldedit.cuboid_linear_expand(name, 'z', -1, amount))
27 elseif dir == "v" then
28 assert(worldedit.cuboid_linear_expand(name, 'y', 1, amount))
29 assert(worldedit.cuboid_linear_expand(name, 'y', -1, amount))
31 return false, "Invalid number of arguments"
34 worldedit.marker_update(name)
35 return true, "Region outset by " .. amount .. " blocks"
40 worldedit.register_command("inset", {
41 params = "[h/v] <amount>",
42 description = "Inset the selected region.",
43 privs = {worldedit=true},
45 parse = function(param)
46 local find, _, dir, amount = param:find("(%a*)%s*([+-]?%d+)")
50 if dir:find("[^hv]") ~= nil then
51 return false, "Invalid direction."
54 return true, dir, tonumber(amount)
56 func = function(name, dir, amount)
57 if dir == "" or dir == "vh" or dir == "hv" then
58 assert(worldedit.cuboid_volumetric_expand(name, -amount))
59 elseif dir == "h" then
60 assert(worldedit.cuboid_linear_expand(name, 'x', 1, -amount))
61 assert(worldedit.cuboid_linear_expand(name, 'x', -1, -amount))
62 assert(worldedit.cuboid_linear_expand(name, 'z', 1, -amount))
63 assert(worldedit.cuboid_linear_expand(name, 'z', -1, -amount))
64 elseif dir == "v" then
65 assert(worldedit.cuboid_linear_expand(name, 'y', 1, -amount))
66 assert(worldedit.cuboid_linear_expand(name, 'y', -1, -amount))
68 return false, "Invalid number of arguments"
71 worldedit.marker_update(name)
72 return true, "Region inset by " .. amount .. " blocks"
77 worldedit.register_command("shift", {
78 params = "x/y/z/?/up/down/left/right/front/back [+/-]<amount>",
79 description = "Shifts the selection area without moving its contents",
80 privs = {worldedit=true},
82 parse = function(param)
83 local find, _, direction, amount = param:find("([%?%l]+)%s*([+-]?%d+)")
88 return true, direction, tonumber(amount)
90 func = function(name, direction, amount)
92 if direction == "x" or direction == "y" or direction == "z" then
93 axis, dir = direction, 1
94 elseif direction == "?" then
95 axis, dir = worldedit.player_axis(name)
97 axis, dir = worldedit.translate_direction(name, direction)
100 if axis == nil or dir == nil then
101 return false, "Invalid if looking straight up or down"
104 assert(worldedit.cuboid_shift(name, axis, amount * dir))
105 worldedit.marker_update(name)
107 return true, "Region shifted by " .. amount .. " nodes"
112 worldedit.register_command("expand", {
113 params = "[+/-]x/y/z/?/up/down/left/right/front/back <amount> [reverse amount]",
114 description = "Expands the selection in the selected absolute or relative axis",
115 privs = {worldedit=true},
117 parse = function(param)
118 local find, _, sign, direction, amount,
119 rev_amount = param:find("([+-]?)([%?%l]+)%s*(%d+)%s*(%d*)")
124 if rev_amount == "" then
128 return true, sign, direction, tonumber(amount), tonumber(rev_amount)
130 func = function(name, sign, direction, amount, rev_amount)
131 local absolute = direction:find("[xyz?]")
134 if absolute == nil then
135 axis, dir = worldedit.translate_direction(name, direction)
137 if axis == nil or dir == nil then
138 return false, "Invalid if looking straight up or down"
141 if direction == "?" then
142 axis, dir = worldedit.player_axis(name)
153 worldedit.cuboid_linear_expand(name, axis, dir, amount)
154 worldedit.cuboid_linear_expand(name, axis, -dir, rev_amount)
155 worldedit.marker_update(name)
156 return true, "Region expanded by " .. (amount + rev_amount) .. " nodes"
161 worldedit.register_command("contract", {
162 params = "[+/-]x/y/z/?/up/down/left/right/front/back <amount> [reverse amount]",
163 description = "Contracts the selection in the selected absolute or relative axis",
164 privs = {worldedit=true},
166 parse = function(param)
167 local find, _, sign, direction, amount,
168 rev_amount = param:find("([+-]?)([%?%l]+)%s*(%d+)%s*(%d*)")
173 if rev_amount == "" then
177 return true, sign, direction, tonumber(amount), tonumber(rev_amount)
179 func = function(name, sign, direction, amount, rev_amount)
180 local absolute = direction:find("[xyz?]")
183 if absolute == nil then
184 axis, dir = worldedit.translate_direction(name, direction)
186 if axis == nil or dir == nil then
187 return false, "Invalid if looking straight up or down"
190 if direction == "?" then
191 axis, dir = worldedit.player_axis(name)
202 worldedit.cuboid_linear_expand(name, axis, dir, -amount)
203 worldedit.cuboid_linear_expand(name, axis, -dir, -rev_amount)
204 worldedit.marker_update(name)
205 return true, "Region contracted by " .. (amount + rev_amount) .. " nodes"
209 worldedit.register_command("cubeapply", {
210 params = "<size>/(<sizex> <sizey> <sizez>) <command> [parameters]",
211 description = "Select a cube with side length <size> around position 1 and run <command> on region",
212 privs = {worldedit=true},
214 parse = function(param)
215 local found, _, sidex, sidey, sidez, cmd, args =
216 param:find("^(%d+)%s+(%d+)%s+(%d+)%s+([^%s]+)%s*(.*)$")
218 found, _, sidex, cmd, args = param:find("^(%d+)%s+([^%s]+)%s*(.*)$")
225 sidex = tonumber(sidex)
226 sidey = tonumber(sidey)
227 sidez = tonumber(sidez)
228 if sidex < 1 or sidey < 1 or sidez < 1 then
231 local cmddef = worldedit.registered_commands[cmd]
232 if cmddef == nil or cmddef.require_pos ~= 2 then
233 return false, "invalid usage: //" .. cmd .. " cannot be used with cubeapply"
235 -- run parsing of target command
236 local parsed = {cmddef.parse(args)}
237 if not table.remove(parsed, 1) then
238 return false, parsed[1]
240 return true, sidex, sidey, sidez, cmd, parsed
242 nodes_needed = function(name, sidex, sidey, sidez, cmd, parsed)
243 -- its not possible to defer to the target command at this point
244 return sidex * sidey * sidez
246 func = function(name, sidex, sidey, sidez, cmd, parsed)
247 local cmddef = assert(worldedit.registered_commands[cmd])
248 local success, missing_privs = minetest.check_player_privs(name, cmddef.privs)
250 worldedit.player_notify(name, "Missing privileges: " ..
251 table.concat(missing_privs, ", "))
255 -- update region to be the cuboid the user wanted
256 local half = vector.divide(vector.new(sidex, sidey, sidez), 2)
257 local sizea, sizeb = vector.apply(half, math.floor), vector.apply(half, math.ceil)
258 local center = worldedit.pos1[name]
259 worldedit.pos1[name] = vector.subtract(center, sizea)
260 worldedit.pos2[name] = vector.add(center, vector.subtract(sizeb, 1))
261 worldedit.marker_update(name)
263 -- actually run target command
264 return cmddef.func(name, unpack(parsed))