]> git.lizzy.rs Git - worldedit.git/blob - worldedit/cuboid.lua
ce207615c768a3abb8c082dcbdad3ef8d18d2777
[worldedit.git] / worldedit / cuboid.lua
1 -- Expands or contracts the cuboid in all axes by amount (positive or negative)
2 worldedit.cuboid_volumetric_expand = function(name, amount)
3         local pos1 = worldedit.pos1[name]
4         local pos2 = worldedit.pos2[name]
5         
6         if pos1 == nil or pos2 == nil then
7                 return false, "Undefined cuboid"
8         end
9         
10         local delta1 = vector.new()
11         local delta2 = vector.new()
12         local delta_dir1
13         local delta_dir2
14         
15         delta1 = vector.add(delta1, amount)
16         delta2 = vector.add(delta2, amount)
17         delta_dir1, delta_dir2 = worldedit.get_expansion_directions(pos1, pos2)
18         delta1 = vector.multiply(delta1, delta_dir1)
19         delta2 = vector.multiply(delta2, delta_dir2)
20         worldedit.pos1[name] = vector.add(pos1, delta1)
21         worldedit.pos2[name] = vector.add(pos2, delta2)
22         
23         return true
24 end
25
26
27 -- Expands or contracts the cuboid in a single axis by amount (positive or negative)
28 worldedit.cuboid_linear_expand = function(name, axis, direction, amount)
29         local pos1 = worldedit.pos1[name]
30         local pos2 = worldedit.pos2[name]
31         
32         if pos1 == nil or pos2 == nil then
33                 return false, "undefined cuboid"
34         end
35         
36         if direction ~= 1 and direction ~= -1 then
37                 return false, "invalid marker"
38         end
39         
40         local marker = worldedit.marker_get_closest_to_axis(name, axis, direction)
41         local deltavect = vector.new()
42         
43         if axis == 'x' then
44                 deltavect.x = amount * direction
45         elseif axis == 'y' then
46                 deltavect.y = amount * direction
47         elseif axis == 'z' then
48                 deltavect.z = amount * direction
49         else
50                 return false, "invalid axis"
51         end
52         
53         worldedit.marker_move(name, marker, deltavect)
54         return true
55 end
56
57
58 -- Shifts the cuboid by '+-amount' in axis 'axis'
59 worldedit.cuboid_shift = function(name, axis, amount)
60         local pos1 = worldedit.pos1[name]
61         local pos2 = worldedit.pos2[name]
62         
63         if pos1 == nil or pos2 == nil then
64                 return false, "undefined cuboid"
65         end
66         
67         if axis == 'x' then
68                 worldedit.pos1[name].x = pos1.x + amount
69                 worldedit.pos2[name].x = pos2.x + amount
70         elseif axis == 'y' then
71                 worldedit.pos1[name].y = pos1.y + amount
72                 worldedit.pos2[name].y = pos2.y + amount
73         elseif axis == 'z' then
74                 worldedit.pos1[name].z = pos1.z + amount
75                 worldedit.pos2[name].z = pos2.z + amount
76         else
77                 return false, "invalid axis"
78         end
79         
80         return true
81 end
82
83
84 -- Moves the location of a single marker by adding deltavector
85 worldedit.marker_move = function(name, marker, deltavector)
86         if marker ~= 1 and marker ~= 2 then
87                 return false
88         end
89         
90         if marker == 1 then
91                 local pos = worldedit.pos1[name]
92                 worldedit.pos1[name] = vector.add(deltavector, pos)
93         else
94                 local pos = worldedit.pos2[name]
95                 worldedit.pos2[name] = vector.add(deltavector, pos)
96         end
97         
98         return true
99 end
100
101 -- Updates the location ingame of the markers
102 worldedit.marker_update = function(name, marker)
103         if marker == nil then
104                 worldedit.mark_pos1(name)
105                 worldedit.mark_pos2(name)
106         elseif marker == 1 then
107                 worldedit.mark_pos1(name)
108         elseif marker == 2 then
109                 worldedit.mark_pos2(name)
110         else
111                 minetest.debug(
112                         "worldedit: Invalid execution of function update_markers")
113         end
114 end
115
116
117 -- Returns two vectors with the directions for volumetric expansion
118 worldedit.get_expansion_directions = function(mark1, mark2)
119         if mark1 == nil or mark2 == nil then
120                 return
121         end
122         local dir1 = vector.new()
123         local dir2 = vector.new()
124
125         if mark1.x < mark2.x then
126                 dir1.x = -1
127                 dir2.x = 1
128         else
129                 dir1.x = 1
130                 dir2.x = -1
131         end
132         if mark1.y < mark2.y then
133                 dir1.y = -1
134                 dir2.y = 1
135         else
136                 dir1.y = 1
137                 dir2.y = -1
138         end
139         if mark1.z < mark2.z then
140                 dir1.z = -1
141                 dir2.z = 1
142         else
143                 dir1.z = 1
144                 dir2.z = -1
145         end
146         return dir1, dir2
147 end
148
149
150 -- Return the marker that is closest to the player
151 worldedit.marker_get_closest_to_player = function(name)
152         local playerpos = minetest.get_player_by_name(name):getpos()
153         local dist1 = vector.distance(playerpos, worldedit.pos1[name])
154         local dist2 = vector.distance(playerpos, worldedit.pos2[name])
155         
156         if dist1 < dist2 then
157                 return 1
158         else
159                 return 2
160         end
161 end
162
163
164 -- Returns the closest marker to the specified axis and direction
165 worldedit.marker_get_closest_to_axis = function(name, axis, direction)
166         local pos1 = vector.new()
167         local pos2 = vector.new()
168         
169         if direction ~= 1 and direction ~= -1 then
170                 return nil
171         end
172
173         if axis == 'x' then
174                 pos1.x = worldedit.pos1[name].x * direction
175                 pos2.x = worldedit.pos2[name].x * direction
176                 if pos1.x > pos2.x then
177                         return 1
178                 else
179                         return 2
180                 end
181         elseif axis == 'y' then
182                 pos1.y = worldedit.pos1[name].y * direction
183                 pos2.y = worldedit.pos2[name].y * direction
184                 if pos1.y > pos2.y then
185                         return 1
186                 else
187                         return 2
188                 end
189         elseif axis == 'z' then
190                 pos1.z = worldedit.pos1[name].z * direction
191                 pos2.z = worldedit.pos2[name].z * direction
192                 if pos1.z > pos2.z then
193                         return 1
194                 else
195                         return 2
196                 end
197         else
198                 return nil
199         end
200 end
201
202
203 -- Translates up, down, left, right, front, back to their corresponding axes and 
204 -- directions according to faced direction
205 worldedit.translate_direction = function(name, direction)
206         local axis, dir = worldedit.player_axis(name)
207         local resaxis, resdir
208         
209         if direction == "up" then
210                 return 'y', 1
211         end
212         
213         if direction == "down" then
214                 return 'y', -1
215         end
216         
217         if direction == "front" then
218                 if axis == "y" then
219                         resaxis = nil
220                         resdir = nil
221                 else
222                         resaxis = axis
223                         resdir = dir
224                 end
225         end
226         
227         if direction == "back" then
228                 if axis == "y" then
229                         resaxis = nil
230                         resdir = nil
231                 else
232                         resaxis = axis
233                         resdir = -dir
234                 end
235         end
236         
237         if direction == "left" then
238                 if axis == 'x' then
239                         resaxis = 'z'
240                         resdir = dir
241                 elseif axis == 'z' then
242                         resaxis = 'x'
243                         resdir = -dir
244                 end
245         end
246         
247         if direction == "right" then
248                 if axis == 'x' then
249                         resaxis = 'z'
250                         resdir = -dir
251                 elseif axis == 'z' then
252                         resaxis = 'x'
253                         resdir = dir
254                 end
255         end
256         
257         return resaxis, resdir
258 end