]> git.lizzy.rs Git - worldedit.git/blob - worldedit_commands/cuboid.lua
Implement /expand and /contract
[worldedit.git] / worldedit_commands / cuboid.lua
1 minetest.register_chatcommand("/outset", {
2         params = "<amount> [h|v]",
3         description = "outset the selection",
4         privs = {worldedit=true},
5         func = function(name, param)
6                 local find, _, amount, dir = param:find("^(%d+)[%s+]?([hv]?)$")
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                 if dir == "" then
21                         assert(worldedit.cuboid_volumetricexpand(name, amount))
22                 elseif dir == "h" then
23                         assert(worldedit.cuboid_linealexpand(name, 'x', 1, amount))
24                         assert(worldedit.cuboid_linealexpand(name, 'x', -1, amount))
25                         assert(worldedit.cuboid_linealexpand(name, 'z', 1, amount))
26                         assert(worldedit.cuboid_linealexpand(name, 'z', -1, amount))
27                 elseif dir == "v" then
28                         assert(worldedit.cuboid_linealexpand(name, 'y', 1, amount))
29                         assert(worldedit.cuboid_linealexpand(name, 'y', -1, amount))
30                 else
31                         return false, "Unknown error"
32                 end
33                 
34                 worldedit.marker_update(name)
35                 return true, "Region outset by " .. amount .. " blocks"
36       end,
37   }
38 )
39
40 minetest.register_chatcommand("/inset", {
41         params = "<amount> [h|v]",
42         description = "inset the selection",
43         privs = {worldedit=true},
44         func = function(name, param)
45                 local find, _, amount, dir = param:find("^(%d+)[%s+]?([hv]?)$")
46                 
47                 if find == nil then
48                         return false, "invalid usage: " .. param
49                 end
50                 
51                 local pos1 = worldedit.pos1[name]
52                 local pos2 = worldedit.pos2[name]
53                 
54                 if pos1 == nil or pos2 == nil then
55                         return false, 
56                                 "Undefined region. Region must be defined beforehand."
57                 end
58                 
59                 if dir == "" then
60                         assert(worldedit.cuboid_volumetricexpand(name, -amount))
61                 elseif dir == "h" then
62                         assert(worldedit.cuboid_linealexpand(name, 'x', 1, -amount))
63                         assert(worldedit.cuboid_linealexpand(name, 'x', -1, -amount))
64                         assert(worldedit.cuboid_linealexpand(name, 'z', 1, -amount))
65                         assert(worldedit.cuboid_linealexpand(name, 'z', -1, -amount))
66                 elseif dir == "v" then
67                         assert(worldedit.cuboid_linealexpand(name, 'y', 1, -amount))
68                         assert(worldedit.cuboid_linealexpand(name, 'y', -1, -amount))
69                 else
70                         return false, "Unknown error"
71                 end
72                 
73                 worldedit.marker_update(name)
74                 return true, "Region inset by " .. amount .. " blocks"
75       end,
76   }
77 )
78
79
80 minetest.register_chatcommand("/shift", {
81         params = "<amount> [up|down|left|right|front|back]",
82         description = "Moves the selection region. Does not move contents.",
83         privs = {worldedit=true},
84         func = function(name, param)
85                 local pos1 = worldedit.pos1[name]
86                 local pos2 = worldedit.pos2[name]
87                 local find, _, amount, direction = param:find("(%d+)%s*(%l*)")
88                 
89                 if find == nil then
90                         worldedit.player_notify(name, "invalid usage: " .. param)
91                         return
92                 end
93                 
94                 if pos1 == nil or pos2 == nil then
95                         worldedit.player_notify(name, 
96                         "Undefined region. Region must be defined beforehand.")
97                         return
98                 end
99                 
100                 local axis, dir
101                 if direction ~= "" then
102                         axis, dir = worldedit.translate_direction(name, direction)
103                 else
104                         axis, dir = worldedit.player_axis(name)
105                         worldedit.player_notify(name, "entered player_axis")
106                 end
107                 
108                 assert(worldedit.cuboid_shift(name, axis, amount * dir))
109                 worldedit.marker_update(name)
110                 
111                 return true, "region shifted by " .. amount .. " blocks"
112       end,
113   }
114 )
115
116 minetest.register_chatcommand("/expand", {
117         params = "<amount> [reverse-amount] [up|down|left|right|front|back]",
118         description = "expand the selection in one or two directions at once",
119         privs = {worldedit=true},
120         func = function(name, param)
121         local find, _, amount, arg2, arg3 = param:find("(%d+)%s*(%w*)%s*(%l*)")
122         
123         if find == nil then
124                 worldedit.player_notify(name, "invalid use: " .. param)
125                 return
126         end
127         
128         if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then
129                 worldedit.player_notify(name, 
130                 "Undefined region. Region must be defined beforehand.")
131                 return
132         end
133         
134         local tmp = tonumber(arg2)
135         local axis, dir
136         local reverseamount = 0
137         
138         axis,dir = worldedit.player_axis(name)
139         
140         if arg2 ~= "" then
141                 if tmp == nil then
142                         axis, dir = worldedit.translate_direction(name, arg2)
143                 else
144                         reverseamount = tmp
145                 end
146         end
147         
148         if arg3 ~= "" then
149                 axis, dir = worldedit.translate_direction(name, arg3)
150         end
151         
152         worldedit.cuboid_linealexpand(name, axis, dir, amount)
153         worldedit.cuboid_linealexpand(name, axis, -dir, reverseamount)
154         worldedit.marker_update(name)
155       end,
156   }
157 )
158
159
160 minetest.register_chatcommand("/contract", {
161         params = "<amount> [reverse-amount] [up|down|left|right|front|back]",
162         description = "contract the selection in one or two directions at once",
163         privs = {worldedit=true},
164         func = function(name, param)
165         local find, _, amount, arg2, arg3 = param:find("(%d+)%s*(%w*)%s*(%l*)")
166         
167         if find == nil then
168                 worldedit.player_notify(name, "invalid use: " .. param)
169                 return
170         end
171         
172         if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then
173                 worldedit.player_notify(name, 
174                 "Undefined region. Region must be defined beforehand.")
175                 return
176         end
177         
178         local tmp = tonumber(arg2)
179         local axis, dir
180         local reverseamount = 0
181         
182         axis,dir = worldedit.player_axis(name)
183         
184         if arg2 ~= "" then
185                 if tmp == nil then
186                         axis, dir = worldedit.translate_direction(name, arg2)
187                 else
188                         reverseamount = tmp
189                 end
190         end
191         
192         if arg3 ~= "" then
193                 axis, dir = worldedit.translate_direction(name, arg3)
194         end
195         
196         worldedit.cuboid_linealexpand(name, axis, dir, -amount)
197         worldedit.cuboid_linealexpand(name, axis, -dir, -reverseamount)
198         worldedit.marker_update(name)
199       end,
200   }
201 )
202
203
204 dofile(minetest.get_modpath("worldedit_commands") .. "/cuboidapi.lua")
205