]> git.lizzy.rs Git - worldedit.git/blob - worldedit/primitives.lua
fe22fffe8f820acfa7be542e9a8b53b168925ae1
[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 -- @param hollow Whether the pyramid should be hollow.\r
154 -- @return The number of nodes added.\r
155 function worldedit.pyramid(pos, axis, height, node_name, hollow)\r
156         local other1, other2 = worldedit.get_axis_others(axis)\r
157 \r
158         -- Set up voxel manipulator\r
159         local manip, area = mh.init_axis_radius(pos, axis,\r
160                         height >= 0 and height or -height)\r
161         local data = mh.get_empty_data(area)\r
162 \r
163         -- Handle inverted pyramids\r
164         local start_axis, end_axis, step\r
165         if height > 0 then\r
166                 height = height - 1\r
167                 step = 1\r
168         else\r
169                 height = height + 1\r
170                 step = -1\r
171         end\r
172 \r
173         -- Add pyramid\r
174         local node_id = minetest.get_content_id(node_name)\r
175         local stride = {x=1, y=area.ystride, z=area.zstride}\r
176         local offset = {\r
177                 x = pos.x - area.MinEdge.x,\r
178                 y = pos.y - area.MinEdge.y,\r
179                 z = pos.z - area.MinEdge.z,\r
180         }\r
181         local size = math.abs(height * step)\r
182         local count = 0\r
183         -- For each level of the pyramid\r
184         for index1 = 0, height, step do\r
185                 -- Offset contributed by axis plus 1 to make it 1-indexed\r
186                 local new_index1 = (index1 + offset[axis]) * stride[axis] + 1\r
187                 for index2 = -size, size do\r
188                         local new_index2 = new_index1 + (index2 + offset[other1]) * stride[other1]\r
189                         for index3 = -size, size do\r
190                                 local i = new_index2 + (index3 + offset[other2]) * stride[other2]\r
191                                 if (not hollow or size - math.abs(index2) < 2 or size - math.abs(index3) < 2) then\r
192                                        data[i] = node_id\r
193                                        count = count + 1\r
194                                 end\r
195                         end\r
196                 end\r
197                 size = size - 1\r
198         end\r
199 \r
200         mh.finish(manip, data)\r
201 \r
202         return count\r
203 end\r
204 \r
205 --- Adds a spiral.\r
206 -- @param pos Position to center spiral at.\r
207 -- @param length Spral length.\r
208 -- @param height Spiral height.\r
209 -- @param spacer Space between walls.\r
210 -- @param node_name Name of node to make spiral of.\r
211 -- @return Number of nodes added.\r
212 -- TODO: Add axis option.\r
213 function worldedit.spiral(pos, length, height, spacer, node_name)\r
214         local extent = math.ceil(length / 2)\r
215 \r
216         local manip, area = mh.init_axis_radius_length(pos, "y", extent, height)\r
217         local data = mh.get_empty_data(area)\r
218 \r
219         -- Set up variables\r
220         local node_id = minetest.get_content_id(node_name)\r
221         local stride = {x=1, y=area.ystride, z=area.zstride}\r
222         local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z\r
223         local i = offset_z * stride.z + offset_y * stride.y + offset_x + 1\r
224 \r
225         -- Add first column\r
226         local count = height\r
227         local column = i\r
228         for y = 1, height do\r
229                 data[column] = node_id\r
230                 column = column + stride.y\r
231         end\r
232 \r
233         -- Add spiral segments\r
234         local stride_axis, stride_other = stride.x, stride.z\r
235         local sign = -1\r
236         local segment_length = 0\r
237         spacer = spacer + 1\r
238         -- Go through each segment except the last\r
239         for segment = 1, math.floor(length / spacer) * 2 do\r
240                 -- Change sign and length every other turn starting with the first\r
241                 if segment % 2 == 1 then\r
242                         sign = -sign\r
243                         segment_length = segment_length + spacer\r
244                 end\r
245                 -- Fill segment\r
246                 for index = 1, segment_length do\r
247                         -- Move along the direction of the segment\r
248                         i = i + stride_axis * sign\r
249                         local column = i\r
250                         -- Add column\r
251                         for y = 1, height do\r
252                                 data[column] = node_id\r
253                                 column = column + stride.y\r
254                         end\r
255                 end\r
256                 count = count + segment_length * height\r
257                 stride_axis, stride_other = stride_other, stride_axis -- Swap axes\r
258         end\r
259 \r
260         -- Add shorter final segment\r
261         sign = -sign\r
262         for index = 1, segment_length do\r
263                 i = i + stride_axis * sign\r
264                 local column = i\r
265                 -- Add column\r
266                 for y = 1, height do\r
267                         data[column] = node_id\r
268                         column = column + stride.y\r
269                 end\r
270         end\r
271         count = count + segment_length * height\r
272 \r
273         mh.finish(manip, data)\r
274 \r
275         return count\r
276 end\r