]> git.lizzy.rs Git - worldedit.git/blob - worldedit/primitives.lua
Fixed Issue #83 : upsidedown pyramid not working well
[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 radius Cylinder radius.\r
96 -- @param node_name Name of node to make cylinder of.\r
97 -- @param hollow Whether the cylinder should be hollow.\r
98 -- @return The number of nodes added.\r
99 function worldedit.cylinder(pos, axis, length, radius, node_name, hollow)\r
100         local other1, other2 = worldedit.get_axis_others(axis)\r
101 \r
102         -- Handle negative lengths\r
103         local current_pos = {x=pos.x, y=pos.y, z=pos.z}\r
104         if length < 0 then\r
105                 length = -length\r
106                 current_pos[axis] = current_pos[axis] - length\r
107         end\r
108 \r
109         -- Set up voxel manipulator\r
110         local manip, area = mh.init_axis_radius_length(current_pos, axis, radius, length)\r
111         local data = mh.get_empty_data(area)\r
112 \r
113         -- Add cylinder\r
114         local node_id = minetest.get_content_id(node_name)\r
115         local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)\r
116         local stride = {x=1, y=area.ystride, z=area.zstride}\r
117         local offset = {\r
118                 x = current_pos.x - area.MinEdge.x,\r
119                 y = current_pos.y - area.MinEdge.y,\r
120                 z = current_pos.z - area.MinEdge.z,\r
121         }\r
122         local min_slice, max_slice = offset[axis], offset[axis] + length - 1\r
123         local count = 0\r
124         for index2 = -radius, radius do\r
125                 -- Offset contributed by other axis 1 plus 1 to make it 1-indexed\r
126                 local new_index2 = (index2 + offset[other1]) * stride[other1] + 1\r
127                 for index3 = -radius, radius do\r
128                         local new_index3 = new_index2 + (index3 + offset[other2]) * stride[other2]\r
129                         local squared = index2 * index2 + index3 * index3\r
130                         if squared <= max_radius and (not hollow or squared >= min_radius) then\r
131                                 -- Position is in cylinder\r
132                                 -- Add column along axis\r
133                                 for index1 = min_slice, max_slice do\r
134                                         local vi = new_index3 + index1 * stride[axis]\r
135                                         data[vi] = node_id\r
136                                 end\r
137                                 count = count + length\r
138                         end\r
139                 end\r
140         end\r
141 \r
142         mh.finish(manip, data)\r
143 \r
144         return count\r
145 end\r
146 \r
147 \r
148 --- Adds a pyramid.\r
149 -- @param pos Position to center base of pyramid at.\r
150 -- @param axis Axis ("x", "y", or "z")\r
151 -- @param height Pyramid height.\r
152 -- @param node_name Name of node to make pyramid of.\r
153 -- @return The number of nodes added.\r
154 function worldedit.pyramid(pos, axis, height, node_name)\r
155         local other1, other2 = worldedit.get_axis_others(axis)\r
156 \r
157         -- Set up voxel manipulator\r
158         local manip, area = mh.init_axis_radius(pos, axis,\r
159                         height >= 0 and height or -height)\r
160         local data = mh.get_empty_data(area)\r
161 \r
162         -- Handle inverted pyramids\r
163         local start_axis, end_axis, step\r
164         if height > 0 then\r
165                 height = height - 1\r
166                 step = 1\r
167         else\r
168                 height = height + 1\r
169                 step = -1\r
170         end\r
171 \r
172         -- Add pyramid\r
173         local node_id = minetest.get_content_id(node_name)\r
174         local stride = {x=1, y=area.ystride, z=area.zstride}\r
175         local offset = {\r
176                 x = pos.x - area.MinEdge.x,\r
177                 y = pos.y - area.MinEdge.y,\r
178                 z = pos.z - area.MinEdge.z,\r
179         }\r
180         local size = math.abs(height * step)\r
181         local count = 0\r
182         -- For each level of the pyramid\r
183         for index1 = 0, height, step do\r
184                 -- Offset contributed by axis plus 1 to make it 1-indexed\r
185                 local new_index1 = (index1 + offset[axis]) * stride[axis] + 1\r
186                 for index2 = -size, size do\r
187                         local new_index2 = new_index1 + (index2 + offset[other1]) * stride[other1]\r
188                         for index3 = -size, size do\r
189                                 local i = new_index2 + (index3 + offset[other2]) * stride[other2]\r
190                                 data[i] = node_id\r
191                         end\r
192                 end\r
193                 count = count + (size * 2 + 1) ^ 2\r
194                 size = size - 1\r
195         end\r
196 \r
197         mh.finish(manip, data)\r
198 \r
199         return count\r
200 end\r
201 \r
202 --- Adds a spiral.\r
203 -- @param pos Position to center spiral at.\r
204 -- @param length Spral length.\r
205 -- @param height Spiral height.\r
206 -- @param spacer Space between walls.\r
207 -- @param node_name Name of node to make spiral of.\r
208 -- @return Number of nodes added.\r
209 -- TODO: Add axis option.\r
210 function worldedit.spiral(pos, length, height, spacer, node_name)\r
211         local extent = math.ceil(length / 2)\r
212 \r
213         local manip, area = mh.init_axis_radius_length(pos, "y", extent, height)\r
214         local data = mh.get_empty_data(area)\r
215 \r
216         -- Set up variables\r
217         local node_id = minetest.get_content_id(node_name)\r
218         local stride = {x=1, y=area.ystride, z=area.zstride}\r
219         local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z\r
220         local i = offset_z * stride.z + offset_y * stride.y + offset_x + 1\r
221 \r
222         -- Add first column\r
223         local count = height\r
224         local column = i\r
225         for y = 1, height do\r
226                 data[column] = node_id\r
227                 column = column + stride.y\r
228         end\r
229 \r
230         -- Add spiral segments\r
231         local stride_axis, stride_other = stride.x, stride.z\r
232         local sign = -1\r
233         local segment_length = 0\r
234         spacer = spacer + 1\r
235         -- Go through each segment except the last\r
236         for segment = 1, math.floor(length / spacer) * 2 do\r
237                 -- Change sign and length every other turn starting with the first\r
238                 if segment % 2 == 1 then\r
239                         sign = -sign\r
240                         segment_length = segment_length + spacer\r
241                 end\r
242                 -- Fill segment\r
243                 for index = 1, segment_length do\r
244                         -- Move along the direction of the segment\r
245                         i = i + stride_axis * sign\r
246                         local column = i\r
247                         -- Add column\r
248                         for y = 1, height do\r
249                                 data[column] = node_id\r
250                                 column = column + stride.y\r
251                         end\r
252                 end\r
253                 count = count + segment_length * height\r
254                 stride_axis, stride_other = stride_other, stride_axis -- Swap axes\r
255         end\r
256 \r
257         -- Add shorter final segment\r
258         sign = -sign\r
259         for index = 1, segment_length do\r
260                 i = i + stride_axis * sign\r
261                 local column = i\r
262                 -- Add column\r
263                 for y = 1, height do\r
264                         data[column] = node_id\r
265                         column = column + stride.y\r
266                 end\r
267         end\r
268         count = count + segment_length * height\r
269 \r
270         mh.finish(manip, data)\r
271 \r
272         return count\r
273 end\r