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+)")
51 local hv_test = dir:find("[^hv]+")
52 if hv_test ~= nil then
53 return false, "Invalid direction."
56 return true, dir, tonumber(amount)
58 func = function(name, dir, amount)
59 if dir == "" or dir == "vh" or dir == "hv" then
60 assert(worldedit.cuboid_volumetric_expand(name, -amount))
61 elseif dir == "h" then
62 assert(worldedit.cuboid_linear_expand(name, 'x', 1, -amount))
63 assert(worldedit.cuboid_linear_expand(name, 'x', -1, -amount))
64 assert(worldedit.cuboid_linear_expand(name, 'z', 1, -amount))
65 assert(worldedit.cuboid_linear_expand(name, 'z', -1, -amount))
66 elseif dir == "v" then
67 assert(worldedit.cuboid_linear_expand(name, 'y', 1, -amount))
68 assert(worldedit.cuboid_linear_expand(name, 'y', -1, -amount))
70 return false, "Invalid number of arguments"
73 worldedit.marker_update(name)
74 return true, "Region inset by " .. amount .. " blocks"
79 worldedit.register_command("shift", {
80 params = "[x|y|z|?|up|down|left|right|front|back] [+|-]<amount>",
81 description = "Moves the selection region. Does not move contents.",
82 privs = {worldedit=true},
84 parse = function(param)
85 local find, _, direction, amount = param:find("([%?%l]+)%s*([+-]?%d+)")
90 return true, direction, tonumber(amount)
92 func = function(name, direction, amount)
94 if direction == "x" or direction == "y" or direction == "z" then
95 axis, dir = direction, 1
96 elseif direction == "?" then
97 axis, dir = worldedit.player_axis(name)
99 axis, dir = worldedit.translate_direction(name, direction)
102 if axis == nil or dir == nil then
103 return false, "Invalid if looking straight up or down"
106 assert(worldedit.cuboid_shift(name, axis, amount * dir))
107 worldedit.marker_update(name)
109 return true, "Region shifted by " .. amount .. " nodes"
114 worldedit.register_command("expand", {
115 params = "[+|-]<x|y|z|?|up|down|left|right|front|back> <amount> [reverse-amount]",
116 description = "expand the selection in one or two directions at once",
117 privs = {worldedit=true},
119 parse = function(param)
120 local find, _, sign, direction, amount,
121 rev_amount = param:find("([+-]?)([%?%l]+)%s*(%d+)%s*(%d*)")
126 if rev_amount == "" then
130 return true, sign, direction, tonumber(amount), tonumber(rev_amount)
132 func = function(name, sign, direction, amount, rev_amount)
133 local absolute = direction:find("[xyz?]")
136 if absolute == nil then
137 axis, dir = worldedit.translate_direction(name, direction)
139 if axis == nil or dir == nil then
140 return false, "Invalid if looking straight up or down"
143 if direction == "?" then
144 axis, dir = worldedit.player_axis(name)
155 worldedit.cuboid_linear_expand(name, axis, dir, amount)
156 worldedit.cuboid_linear_expand(name, axis, -dir, rev_amount)
157 worldedit.marker_update(name)
158 return true, "Region expanded by " .. (amount + rev_amount) .. " nodes"
163 worldedit.register_command("contract", {
164 params = "[+|-]<x|y|z|?|up|down|left|right|front|back> <amount> [reverse-amount]",
165 description = "contract the selection in one or two directions at once",
166 privs = {worldedit=true},
168 parse = function(param)
169 local find, _, sign, direction, amount,
170 rev_amount = param:find("([+-]?)([%?%l]+)%s*(%d+)%s*(%d*)")
175 if rev_amount == "" then
179 return true, sign, direction, tonumber(amount), tonumber(rev_amount)
181 func = function(name, sign, direction, amount, rev_amount)
182 local absolute = direction:find("[xyz?]")
185 if absolute == nil then
186 axis, dir = worldedit.translate_direction(name, direction)
188 if axis == nil or dir == nil then
189 return false, "Invalid if looking straight up or down"
192 if direction == "?" then
193 axis, dir = worldedit.player_axis(name)
204 worldedit.cuboid_linear_expand(name, axis, dir, -amount)
205 worldedit.cuboid_linear_expand(name, axis, -dir, -rev_amount)
206 worldedit.marker_update(name)
207 return true, "Region contracted by " .. (amount + rev_amount) .. " nodes"
211 worldedit.register_command("cubeapply", {
212 params = "<size> <command> [parameters]",
213 description = "Select a cube with side length <size> around position 1 and run <command> on region",
214 privs = {worldedit=true},
216 parse = function(param)
217 local found, _, side_length, cmd, args = param:find("^(%d+)%s+([^%s]+)%s*(.*)$")
221 side_length = tonumber(side_length)
222 if side_length < 1 then
225 local cmddef = worldedit.registered_commands[cmd]
226 if cmddef == nil or cmddef.require_pos ~= 2 then
227 return false, "invalid usage: //" .. cmd .. " cannot be used with cubeapply"
229 -- run parsing of target command
230 local parsed = {cmddef.parse(args)}
231 if not table.remove(parsed, 1) then
232 return false, parsed[1]
234 return true, side_length, cmd, parsed
236 nodes_needed = function(name, side_length, cmd, parsed)
237 -- its not possible to defer to the target command at this point
238 return side_length * side_length * side_length
240 func = function(name, side_length, cmd, parsed)
241 local cmddef = assert(worldedit.registered_commands[cmd])
242 local success, missing_privs = minetest.check_player_privs(name, cmddef.privs)
244 worldedit.player_notify(name, "Missing privileges: " ..
245 table.concat(missing_privs, ", "))
249 -- update region to be the cube the user wanted
250 local sizea, sizeb = math.floor(side_length / 2), math.ceil(side_length / 2)
251 local center = worldedit.pos1[name]
252 worldedit.pos1[name] = vector.subtract(center, sizea)
253 worldedit.pos2[name] = vector.add(center, sizeb - 1)
254 worldedit.marker_update(name)
256 -- actually run target command
257 return cmddef.func(name, unpack(parsed))