]> git.lizzy.rs Git - worldedit.git/blob - worldedit_commands/cuboid.lua
88f026087074c3673c3c6f9b4e5380b8d9b4f8c9
[worldedit.git] / worldedit_commands / cuboid.lua
1 minetest.register_chatcommand("/outset", {
2         params = "[h|v] <amount>",
3         description = "outset the selection",
4         privs = {worldedit=true},
5         func = function(name, param)
6                 local find, _, dir, amount = param:find("(%a*)%s*([+-]?%d+)")
7                 
8                 if find == nil then
9                         return false, "invalid usage: " .. param
10                 end
11                 
12                 local pos1 = worldedit.pos1[name]
13                 local pos2 = worldedit.pos2[name]
14                 
15                 if pos1 == nil or pos2 == nil then
16                         return false, 
17                                 "Undefined region. Region must be defined beforehand."
18                 end
19                 
20                 local hv_test = dir:find("[^hv]+")
21                 
22                 if hv_test ~= nil then
23                         return false, "Invalid direction."
24                 end
25                 
26                 if dir == "" or dir == "hv" or dir == "vh" then
27                         assert(worldedit.cuboid_volumetric_expand(name, amount))
28                 elseif dir == "h" then
29                         assert(worldedit.cuboid_linear_expand(name, 'x', 1, amount))
30                         assert(worldedit.cuboid_linear_expand(name, 'x', -1, amount))
31                         assert(worldedit.cuboid_linear_expand(name, 'z', 1, amount))
32                         assert(worldedit.cuboid_linear_expand(name, 'z', -1, amount))
33                 elseif dir == "v" then
34                         assert(worldedit.cuboid_linear_expand(name, 'y', 1, amount))
35                         assert(worldedit.cuboid_linear_expand(name, 'y', -1, amount))
36                 else
37                         return false, "Invalid number of arguments"
38                 end
39                 
40                 worldedit.marker_update(name)
41                 return true, "Region outset by " .. amount .. " blocks"
42       end,
43   }
44 )
45
46
47 minetest.register_chatcommand("/inset", {
48         params = "[h|v] <amount>",
49         description = "inset the selection",
50         privs = {worldedit=true},
51         func = function(name, param)
52                 local find, _, dir, amount = param:find("(%a*)%s*([+-]?%d+)")
53                 
54                 if find == nil then
55                         return false, "invalid usage: " .. param
56                 end
57                 
58                 local pos1 = worldedit.pos1[name]
59                 local pos2 = worldedit.pos2[name]
60                 
61                 if pos1 == nil or pos2 == nil then
62                         return false, 
63                                 "Undefined region. Region must be defined beforehand."
64                 end
65                 
66                 local hv_test = dir:find("[^hv]+")
67                 
68                 if hv_test ~= nil then
69                         return false, "Invalid direction."
70                 end
71                 
72                 if dir == "" or dir == "vh" or dir == "hv" then
73                         assert(worldedit.cuboid_volumetric_expand(name, -amount))
74                 elseif dir == "h" then
75                         assert(worldedit.cuboid_linear_expand(name, 'x', 1, -amount))
76                         assert(worldedit.cuboid_linear_expand(name, 'x', -1, -amount))
77                         assert(worldedit.cuboid_linear_expand(name, 'z', 1, -amount))
78                         assert(worldedit.cuboid_linear_expand(name, 'z', -1, -amount))
79                 elseif dir == "v" then
80                         assert(worldedit.cuboid_linear_expand(name, 'y', 1, -amount))
81                         assert(worldedit.cuboid_linear_expand(name, 'y', -1, -amount))
82                 else
83                         return false, "Invalid number of arguments"
84                 end
85                 
86                 worldedit.marker_update(name)
87                 return true, "Region inset by " .. amount .. " blocks"
88       end,
89   }
90 )
91
92
93 minetest.register_chatcommand("/shift", {
94         params = "[x|y|z|?|up|down|left|right|front|back] [+|-]<amount>",
95         description = "Moves the selection region. Does not move contents.",
96         privs = {worldedit=true},
97         func = function(name, param)
98                 local pos1 = worldedit.pos1[name]
99                 local pos2 = worldedit.pos2[name]
100                 local find, _, direction, amount = param:find("([%?%l]+)%s*([+-]?%d+)")
101                 
102                 if find == nil then
103                         worldedit.player_notify(name, "invalid usage: " .. param)
104                         return
105                 end
106                 
107                 if pos1 == nil or pos2 == nil then
108                         worldedit.player_notify(name, 
109                                 "Undefined region. Region must be defined beforehand.")
110                         return
111                 end
112                 
113                 local axis, dir
114                 if direction == "x" or direction == "y" or direction == "z" then
115                         axis, dir = direction, 1
116                 elseif direction == "?" then
117                         axis, dir = worldedit.player_axis(name)
118                 else
119                         axis, dir = worldedit.translate_direction(name, direction)
120                 end
121                 
122                 if axis == nil or dir == nil then
123                         return false, "Invalid if looking straight up or down"
124                 end
125                 
126                 assert(worldedit.cuboid_shift(name, axis, amount * dir))
127                 worldedit.marker_update(name)
128                 
129                 return true, "Region shifted by " .. amount .. " nodes"
130       end,
131   }
132 )
133
134
135 minetest.register_chatcommand("/expand", {
136         params = "[+|-]<x|y|z|?|up|down|left|right|front|back> <amount> [reverse-amount]",
137         description = "expand the selection in one or two directions at once",
138         privs = {worldedit=true},
139         func = function(name, param)
140         local find, _, sign, direction, amount, 
141                         rev_amount = param:find("([+-]?)([%?%l]+)%s*(%d+)%s*(%d*)")
142         
143         if find == nil then
144                 worldedit.player_notify(name, "invalid use: " .. param)
145                 return
146         end
147         
148         if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then
149                 worldedit.player_notify(name, 
150                 "Undefined region. Region must be defined beforehand.")
151                 return
152         end
153         
154         local absolute = direction:find("[xyz?]")
155         local dir, axis
156         
157         if rev_amount == "" then
158                 rev_amount = 0
159         end
160         
161         if absolute == nil then
162                 axis, dir = worldedit.translate_direction(name, direction)
163                 
164                 if axis == nil or dir == nil then
165                         return false, "Invalid if looking straight up or down"
166                 end
167         else
168                 if direction == "?" then
169                         axis, dir = worldedit.player_axis(name)
170                 else
171                         axis = direction
172                         dir = 1
173                 end
174         end
175         
176         if sign == "-" then
177                 dir = -dir
178         end
179         
180         worldedit.cuboid_linear_expand(name, axis, dir, amount)
181         worldedit.cuboid_linear_expand(name, axis, -dir, rev_amount)
182         worldedit.marker_update(name)
183         return true, "Region expanded by " .. (amount + rev_amount) .. " nodes"
184       end,
185   }
186 )
187
188
189 minetest.register_chatcommand("/contract", {
190         params = "[+|-]<x|y|z|?|up|down|left|right|front|back> <amount> [reverse-amount]",
191         description = "contract the selection in one or two directions at once",
192         privs = {worldedit=true},
193         func = function(name, param)
194         local find, _, sign, direction, amount, 
195                         rev_amount = param:find("([+-]?)([%?%l]+)%s*(%d+)%s*(%d*)")
196         
197         if find == nil then
198                 worldedit.player_notify(name, "invalid use: " .. param)
199                 return
200         end
201         
202         if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then
203                 worldedit.player_notify(name, 
204                 "Undefined region. Region must be defined beforehand.")
205                 return
206         end
207         
208         local absolute = direction:find("[xyz?]")
209         local dir, axis
210         
211         if rev_amount == "" then
212                 rev_amount = 0
213         end
214         
215         if absolute == nil then
216                 axis, dir = worldedit.translate_direction(name, direction)
217                 
218                 if axis == nil or dir == nil then
219                         return false, "Invalid if looking straight up or down"
220                 end
221         else
222                 if direction == "?" then
223                         axis, dir = worldedit.player_axis(name)
224                 else
225                         axis = direction
226                         dir = 1
227                 end
228         end
229         
230         if sign == "-" then
231                 dir = -dir
232         end
233         
234         worldedit.cuboid_linear_expand(name, axis, dir, -amount)
235         worldedit.cuboid_linear_expand(name, axis, -dir, -rev_amount)
236         worldedit.marker_update(name)
237         return true, "Region contracted by " .. (amount + rev_amount) .. " nodes"
238       end,
239   }
240 )