]> git.lizzy.rs Git - worldedit.git/blob - worldedit_commands/cuboid.lua
Update help texts to be consistent
[worldedit.git] / worldedit_commands / cuboid.lua
1 worldedit.register_command("outset", {
2         params = "[h/v] <amount>",
3         description = "Outset the selected region.",
4         privs = {worldedit=true},
5         require_pos = 2,
6         parse = function(param)
7                 local find, _, dir, amount = param:find("(%a*)%s*([+-]?%d+)")
8                 if find == nil then
9                         return false
10                 end
11
12                 local hv_test = dir:find("[^hv]+")
13                 if hv_test ~= nil then
14                         return false, "Invalid direction."
15                 end
16
17                 return true, dir, tonumber(amount)
18         end,
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))
30                 else
31                         return false, "Invalid number of arguments"
32                 end
33
34                 worldedit.marker_update(name)
35                 return true, "Region outset by " .. amount .. " blocks"
36       end,
37 })
38
39
40 worldedit.register_command("inset", {
41         params = "[h/v] <amount>",
42         description = "Inset the selected region.",
43         privs = {worldedit=true},
44         require_pos = 2,
45         parse = function(param)
46                 local find, _, dir, amount = param:find("(%a*)%s*([+-]?%d+)")
47                 if find == nil then
48                         return false
49                 end
50                 if dir:find("[^hv]") ~= nil then
51                         return false, "Invalid direction."
52                 end
53
54                 return true, dir, tonumber(amount)
55         end,
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))
67                 else
68                         return false, "Invalid number of arguments"
69                 end
70
71                 worldedit.marker_update(name)
72                 return true, "Region inset by " .. amount .. " blocks"
73       end,
74 })
75
76
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},
81         require_pos = 2,
82         parse = function(param)
83                 local find, _, direction, amount = param:find("([%?%l]+)%s*([+-]?%d+)")
84                 if find == nil then
85                         return false
86                 end
87
88                 return true, direction, tonumber(amount)
89         end,
90         func = function(name, direction, amount)
91                 local axis, dir
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)
96                 else
97                         axis, dir = worldedit.translate_direction(name, direction)
98                 end
99
100                 if axis == nil or dir == nil then
101                         return false, "Invalid if looking straight up or down"
102                 end
103
104                 assert(worldedit.cuboid_shift(name, axis, amount * dir))
105                 worldedit.marker_update(name)
106
107                 return true, "Region shifted by " .. amount .. " nodes"
108       end,
109 })
110
111
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},
116         require_pos = 2,
117         parse = function(param)
118                 local find, _, sign, direction, amount,
119                                 rev_amount = param:find("([+-]?)([%?%l]+)%s*(%d+)%s*(%d*)")
120                 if find == nil then
121                         return false
122                 end
123
124                 if rev_amount == "" then
125                         rev_amount = "0"
126                 end
127
128                 return true, sign, direction, tonumber(amount), tonumber(rev_amount)
129         end,
130         func = function(name, sign, direction, amount, rev_amount)
131                 local absolute = direction:find("[xyz?]")
132                 local dir, axis
133
134                 if absolute == nil then
135                         axis, dir = worldedit.translate_direction(name, direction)
136
137                         if axis == nil or dir == nil then
138                                 return false, "Invalid if looking straight up or down"
139                         end
140                 else
141                         if direction == "?" then
142                                 axis, dir = worldedit.player_axis(name)
143                         else
144                                 axis = direction
145                                 dir = 1
146                         end
147                 end
148
149                 if sign == "-" then
150                         dir = -dir
151                 end
152
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"
157         end,
158 })
159
160
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},
165         require_pos = 2,
166         parse = function(param)
167                 local find, _, sign, direction, amount,
168                                 rev_amount = param:find("([+-]?)([%?%l]+)%s*(%d+)%s*(%d*)")
169                 if find == nil then
170                         return false
171                 end
172
173                 if rev_amount == "" then
174                         rev_amount = "0"
175                 end
176
177                 return true, sign, direction, tonumber(amount), tonumber(rev_amount)
178         end,
179         func = function(name, sign, direction, amount, rev_amount)
180                 local absolute = direction:find("[xyz?]")
181                 local dir, axis
182
183                 if absolute == nil then
184                         axis, dir = worldedit.translate_direction(name, direction)
185
186                         if axis == nil or dir == nil then
187                                 return false, "Invalid if looking straight up or down"
188                         end
189                 else
190                         if direction == "?" then
191                                 axis, dir = worldedit.player_axis(name)
192                         else
193                                 axis = direction
194                                 dir = 1
195                         end
196                 end
197
198                 if sign == "-" then
199                         dir = -dir
200                 end
201
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"
206         end,
207 })
208
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},
213         require_pos = 1,
214         parse = function(param)
215                 local found, _, sidex, sidey, sidez, cmd, args =
216                         param:find("^(%d+)%s+(%d+)%s+(%d+)%s+([^%s]+)%s*(.*)$")
217                 if found == nil then
218                         found, _, sidex, cmd, args = param:find("^(%d+)%s+([^%s]+)%s*(.*)$")
219                         if found == nil then
220                                 return false
221                         end
222                         sidey = sidex
223                         sidez = sidex
224                 end
225                 sidex = tonumber(sidex)
226                 sidey = tonumber(sidey)
227                 sidez = tonumber(sidez)
228                 if sidex < 1 or sidey < 1 or sidez < 1 then
229                         return false
230                 end
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"
234                 end
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]
239                 end
240                 return true, sidex, sidey, sidez, cmd, parsed
241         end,
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
245         end,
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)
249                 if not success then
250                         worldedit.player_notify(name, "Missing privileges: " ..
251                                 table.concat(missing_privs, ", "))
252                         return
253                 end
254
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)
262
263                 -- actually run target command
264                 return cmddef.func(name, unpack(parsed))
265         end,
266 })