]> git.lizzy.rs Git - worldedit.git/blob - worldedit/cuboid.lua
Fix deserialization of schematics with node names table
[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
102 -- Returns two vectors with the directions for volumetric expansion
103 worldedit.get_expansion_directions = function(mark1, mark2)
104         if mark1 == nil or mark2 == nil then
105                 return
106         end
107         local dir1 = vector.new()
108         local dir2 = vector.new()
109
110         if mark1.x < mark2.x then
111                 dir1.x = -1
112                 dir2.x = 1
113         else
114                 dir1.x = 1
115                 dir2.x = -1
116         end
117         if mark1.y < mark2.y then
118                 dir1.y = -1
119                 dir2.y = 1
120         else
121                 dir1.y = 1
122                 dir2.y = -1
123         end
124         if mark1.z < mark2.z then
125                 dir1.z = -1
126                 dir2.z = 1
127         else
128                 dir1.z = 1
129                 dir2.z = -1
130         end
131         return dir1, dir2
132 end
133
134
135 -- Return the marker that is closest to the player
136 worldedit.marker_get_closest_to_player = function(name)
137         local playerpos = minetest.get_player_by_name(name):get_pos()
138         local dist1 = vector.distance(playerpos, worldedit.pos1[name])
139         local dist2 = vector.distance(playerpos, worldedit.pos2[name])
140         
141         if dist1 < dist2 then
142                 return 1
143         else
144                 return 2
145         end
146 end
147
148
149 -- Returns the closest marker to the specified axis and direction
150 worldedit.marker_get_closest_to_axis = function(name, axis, direction)
151         local pos1 = vector.new()
152         local pos2 = vector.new()
153         
154         if direction ~= 1 and direction ~= -1 then
155                 return nil
156         end
157
158         if axis == 'x' then
159                 pos1.x = worldedit.pos1[name].x * direction
160                 pos2.x = worldedit.pos2[name].x * direction
161                 if pos1.x > pos2.x then
162                         return 1
163                 else
164                         return 2
165                 end
166         elseif axis == 'y' then
167                 pos1.y = worldedit.pos1[name].y * direction
168                 pos2.y = worldedit.pos2[name].y * direction
169                 if pos1.y > pos2.y then
170                         return 1
171                 else
172                         return 2
173                 end
174         elseif axis == 'z' then
175                 pos1.z = worldedit.pos1[name].z * direction
176                 pos2.z = worldedit.pos2[name].z * direction
177                 if pos1.z > pos2.z then
178                         return 1
179                 else
180                         return 2
181                 end
182         else
183                 return nil
184         end
185 end
186
187
188 -- Translates up, down, left, right, front, back to their corresponding axes and 
189 -- directions according to faced direction
190 worldedit.translate_direction = function(name, direction)
191         local axis, dir = worldedit.player_axis(name)
192         local resaxis, resdir
193         
194         if direction == "up" then
195                 return 'y', 1
196         end
197         
198         if direction == "down" then
199                 return 'y', -1
200         end
201         
202         if direction == "front" then
203                 if axis == "y" then
204                         resaxis = nil
205                         resdir = nil
206                 else
207                         resaxis = axis
208                         resdir = dir
209                 end
210         end
211         
212         if direction == "back" then
213                 if axis == "y" then
214                         resaxis = nil
215                         resdir = nil
216                 else
217                         resaxis = axis
218                         resdir = -dir
219                 end
220         end
221         
222         if direction == "left" then
223                 if axis == 'x' then
224                         resaxis = 'z'
225                         resdir = dir
226                 elseif axis == 'z' then
227                         resaxis = 'x'
228                         resdir = -dir
229                 end
230         end
231         
232         if direction == "right" then
233                 if axis == 'x' then
234                         resaxis = 'z'
235                         resdir = -dir
236                 elseif axis == 'z' then
237                         resaxis = 'x'
238                         resdir = dir
239                 end
240         end
241         
242         return resaxis, resdir
243 end