]> git.lizzy.rs Git - worldedit.git/blob - worldedit/primitives.lua
9b86cd884959112965ff3fb6b514d03f4bd107c9
[worldedit.git] / worldedit / primitives.lua
1 --- Functions for creating primitive shapes.\r
2 -- @module worldedit.primitives\r
3 \r
4 local mh = worldedit.manip_helpers\r
5 \r
6 \r
7 --- Adds a sphere of `node_name` centered at `pos`.\r
8 -- @param pos Position to center sphere at.\r
9 -- @param radius Sphere radius.\r
10 -- @param node_name Name of node to make shere of.\r
11 -- @param hollow Whether the sphere should be hollow.\r
12 -- @return The number of nodes added.\r
13 function worldedit.sphere(pos, radius, node_name, hollow)\r
14         local manip, area = mh.init_radius(pos, radius)\r
15 \r
16         local data = mh.get_empty_data(area)\r
17 \r
18         -- Fill selected area with node\r
19         local node_id = minetest.get_content_id(node_name)\r
20         local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)\r
21         local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z\r
22         local stride_z, stride_y = area.zstride, area.ystride\r
23         local count = 0\r
24         for z = -radius, radius do\r
25                 -- Offset contributed by z plus 1 to make it 1-indexed\r
26                 local new_z = (z + offset_z) * stride_z + 1\r
27                 for y = -radius, radius do\r
28                         local new_y = new_z + (y + offset_y) * stride_y\r
29                         for x = -radius, radius do\r
30                                 local squared = x * x + y * y + z * z\r
31                                 if squared <= max_radius and (not hollow or squared >= min_radius) then\r
32                                         -- Position is on surface of sphere\r
33                                         local i = new_y + (x + offset_x)\r
34                                         data[i] = node_id\r
35                                         count = count + 1\r
36                                 end\r
37                         end\r
38                 end\r
39         end\r
40 \r
41         mh.finish(manip, data)\r
42 \r
43         return count\r
44 end\r
45 \r
46 \r
47 --- Adds a dome.\r
48 -- @param pos Position to center dome at.\r
49 -- @param radius Dome radius.  Negative for concave domes.\r
50 -- @param node_name Name of node to make dome of.\r
51 -- @param hollow Whether the dome should be hollow.\r
52 -- @return The number of nodes added.\r
53 -- TODO: Add axis option.\r
54 function worldedit.dome(pos, radius, node_name, hollow)\r
55         local min_y, max_y = 0, radius\r
56         if radius < 0 then\r
57                 radius = -radius\r
58                 min_y, max_y = -radius, 0\r
59         end\r
60 \r
61         local manip, area = mh.init_axis_radius(pos, "y", radius)\r
62         local data = mh.get_empty_data(area)\r
63 \r
64         -- Add dome\r
65         local node_id = minetest.get_content_id(node_name)\r
66         local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)\r
67         local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z\r
68         local stride_z, stride_y = area.zstride, area.ystride\r
69         local count = 0\r
70         for z = -radius, radius do\r
71                 local new_z = (z + offset_z) * stride_z + 1 --offset contributed by z plus 1 to make it 1-indexed\r
72                 for y = min_y, max_y do\r
73                         local new_y = new_z + (y + offset_y) * stride_y\r
74                         for x = -radius, radius do\r
75                                 local squared = x * x + y * y + z * z\r
76                                 if squared <= max_radius and (not hollow or squared >= min_radius) then\r
77                                         -- Position is in dome\r
78                                         local i = new_y + (x + offset_x)\r
79                                         data[i] = node_id\r
80                                         count = count + 1\r
81                                 end\r
82                         end\r
83                 end\r
84         end\r
85 \r
86         mh.finish(manip, data)\r
87 \r
88         return count\r
89 end\r
90 \r
91 --- Adds a cylinder.\r
92 -- @param pos Position to center base of cylinder at.\r
93 -- @param axis Axis ("x", "y", or "z")\r
94 -- @param length Cylinder length.\r
95 -- @param radius1 Cylinder base radius.\r
96 -- @param radius2 Cylinder top radius.\r
97 -- @param node_name Name of node to make cylinder of.\r
98 -- @param hollow Whether the cylinder should be hollow.\r
99 -- @return The number of nodes added.\r
100 function worldedit.cylinder(pos, axis, length, radius1, radius2, node_name, hollow)\r
101         local other1, other2 = worldedit.get_axis_others(axis)\r
102 \r
103         -- Backwards compatibility\r
104         if type(radius2) == "string" then\r
105                 hollow = node_name\r
106                 node_name = radius2\r
107                 radius2 = radius1 -- straight cylinder\r
108         end\r
109 \r
110         -- Handle negative lengths\r
111         local current_pos = {x=pos.x, y=pos.y, z=pos.z}\r
112         if length < 0 then\r
113                 length = -length\r
114                 current_pos[axis] = current_pos[axis] - length\r
115                 radius1, radius2 = radius2, radius1\r
116         end\r
117 \r
118         -- Set up voxel manipulator\r
119         local manip, area = mh.init_axis_radius_length(current_pos, axis, math.max(radius1, radius2), length)\r
120         local data = mh.get_empty_data(area)\r
121 \r
122         -- Add desired shape (anything inbetween cylinder & cone)\r
123         local node_id = minetest.get_content_id(node_name)\r
124         local stride = {x=1, y=area.ystride, z=area.zstride}\r
125         local offset = {\r
126                 x = current_pos.x - area.MinEdge.x,\r
127                 y = current_pos.y - area.MinEdge.y,\r
128                 z = current_pos.z - area.MinEdge.z,\r
129         }\r
130         local count = 0\r
131         for i = 0, length - 1 do\r
132                 -- Calulate radius for this "height" in the cylinder\r
133                 local radius = radius1 + (radius2 - radius1) * (i - 1) / (length - 1)\r
134                 radius = math.floor(radius + 0.5) -- round\r
135                 local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)\r
136 \r
137                 for index2 = -radius, radius do\r
138                         -- Offset contributed by other axis 1 plus 1 to make it 1-indexed\r
139                         local new_index2 = (index2 + offset[other1]) * stride[other1] + 1\r
140                         for index3 = -radius, radius do\r
141                                 local new_index3 = new_index2 + (index3 + offset[other2]) * stride[other2]\r
142                                 local squared = index2 * index2 + index3 * index3\r
143                                 if squared <= max_radius and (not hollow or squared >= min_radius) then\r
144                                         -- Position is in cylinder, add node here\r
145                                         local vi = new_index3 + (offset[axis] + i) * stride[axis]\r
146                                         data[vi] = node_id\r
147                                         count = count + 1\r
148                                 end\r
149                         end\r
150                 end\r
151         end\r
152 \r
153         mh.finish(manip, data)\r
154 \r
155         return count\r
156 end\r
157 \r
158 \r
159 --- Adds a pyramid.\r
160 -- @param pos Position to center base of pyramid at.\r
161 -- @param axis Axis ("x", "y", or "z")\r
162 -- @param height Pyramid height.\r
163 -- @param node_name Name of node to make pyramid of.\r
164 -- @param hollow Whether the pyramid should be hollow.\r
165 -- @return The number of nodes added.\r
166 function worldedit.pyramid(pos, axis, height, node_name, hollow)\r
167         local other1, other2 = worldedit.get_axis_others(axis)\r
168 \r
169         -- Set up voxel manipulator\r
170         local manip, area = mh.init_axis_radius(pos, axis,\r
171                         height >= 0 and height or -height)\r
172         local data = mh.get_empty_data(area)\r
173 \r
174         -- Handle inverted pyramids\r
175         local start_axis, end_axis, step\r
176         if height > 0 then\r
177                 height = height - 1\r
178                 step = 1\r
179         else\r
180                 height = height + 1\r
181                 step = -1\r
182         end\r
183 \r
184         -- Add pyramid\r
185         local node_id = minetest.get_content_id(node_name)\r
186         local stride = {x=1, y=area.ystride, z=area.zstride}\r
187         local offset = {\r
188                 x = pos.x - area.MinEdge.x,\r
189                 y = pos.y - area.MinEdge.y,\r
190                 z = pos.z - area.MinEdge.z,\r
191         }\r
192         local size = math.abs(height * step)\r
193         local count = 0\r
194         -- For each level of the pyramid\r
195         for index1 = 0, height, step do\r
196                 -- Offset contributed by axis plus 1 to make it 1-indexed\r
197                 local new_index1 = (index1 + offset[axis]) * stride[axis] + 1\r
198                 for index2 = -size, size do\r
199                         local new_index2 = new_index1 + (index2 + offset[other1]) * stride[other1]\r
200                         for index3 = -size, size do\r
201                                 local i = new_index2 + (index3 + offset[other2]) * stride[other2]\r
202                                 if (not hollow or size - math.abs(index2) < 2 or size - math.abs(index3) < 2) then\r
203                                        data[i] = node_id\r
204                                        count = count + 1\r
205                                 end\r
206                         end\r
207                 end\r
208                 size = size - 1\r
209         end\r
210 \r
211         mh.finish(manip, data)\r
212 \r
213         return count\r
214 end\r
215 \r
216 --- Adds a spiral.\r
217 -- @param pos Position to center spiral at.\r
218 -- @param length Spral length.\r
219 -- @param height Spiral height.\r
220 -- @param spacer Space between walls.\r
221 -- @param node_name Name of node to make spiral of.\r
222 -- @return Number of nodes added.\r
223 -- TODO: Add axis option.\r
224 function worldedit.spiral(pos, length, height, spacer, node_name)\r
225         local extent = math.ceil(length / 2)\r
226 \r
227         local manip, area = mh.init_axis_radius_length(pos, "y", extent, height)\r
228         local data = mh.get_empty_data(area)\r
229 \r
230         -- Set up variables\r
231         local node_id = minetest.get_content_id(node_name)\r
232         local stride = {x=1, y=area.ystride, z=area.zstride}\r
233         local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z\r
234         local i = offset_z * stride.z + offset_y * stride.y + offset_x + 1\r
235 \r
236         -- Add first column\r
237         local count = height\r
238         local column = i\r
239         for y = 1, height do\r
240                 data[column] = node_id\r
241                 column = column + stride.y\r
242         end\r
243 \r
244         -- Add spiral segments\r
245         local stride_axis, stride_other = stride.x, stride.z\r
246         local sign = -1\r
247         local segment_length = 0\r
248         spacer = spacer + 1\r
249         -- Go through each segment except the last\r
250         for segment = 1, math.floor(length / spacer) * 2 do\r
251                 -- Change sign and length every other turn starting with the first\r
252                 if segment % 2 == 1 then\r
253                         sign = -sign\r
254                         segment_length = segment_length + spacer\r
255                 end\r
256                 -- Fill segment\r
257                 for index = 1, segment_length do\r
258                         -- Move along the direction of the segment\r
259                         i = i + stride_axis * sign\r
260                         local column = i\r
261                         -- Add column\r
262                         for y = 1, height do\r
263                                 data[column] = node_id\r
264                                 column = column + stride.y\r
265                         end\r
266                 end\r
267                 count = count + segment_length * height\r
268                 stride_axis, stride_other = stride_other, stride_axis -- Swap axes\r
269         end\r
270 \r
271         -- Add shorter final segment\r
272         sign = -sign\r
273         for index = 1, segment_length do\r
274                 i = i + stride_axis * sign\r
275                 local column = i\r
276                 -- Add column\r
277                 for y = 1, height do\r
278                         data[column] = node_id\r
279                         column = column + stride.y\r
280                 end\r
281         end\r
282         count = count + segment_length * height\r
283 \r
284         mh.finish(manip, data)\r
285 \r
286         return count\r
287 end\r