1 --- Functions for creating primitive shapes.
\r
2 -- @module worldedit.primitives
\r
4 local mh = worldedit.manip_helpers
\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
16 local data = mh.get_empty_data(area)
\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
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
41 mh.finish(manip, data)
\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
58 min_y, max_y = -radius, 0
\r
61 local manip, area = mh.init_axis_radius(pos, "y", radius)
\r
62 local data = mh.get_empty_data(area)
\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
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
86 mh.finish(manip, data)
\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
102 -- Handle negative lengths
\r
103 local current_pos = {x=pos.x, y=pos.y, z=pos.z}
\r
106 current_pos[axis] = current_pos[axis] - length
\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
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
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
122 local min_slice, max_slice = offset[axis], offset[axis] + length - 1
\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
137 count = count + length
\r
142 mh.finish(manip, data)
\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
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
163 -- Handle inverted pyramids
\r
164 local start_axis, end_axis, step
\r
166 height = height - 1
\r
169 height = height + 1
\r
174 local node_id = minetest.get_content_id(node_name)
\r
175 local stride = {x=1, y=area.ystride, z=area.zstride}
\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
181 local size = math.abs(height * step)
\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
200 mh.finish(manip, data)
\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
216 local manip, area = mh.init_axis_radius_length(pos, "y", extent, height)
\r
217 local data = mh.get_empty_data(area)
\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
225 -- Add first column
\r
226 local count = height
\r
228 for y = 1, height do
\r
229 data[column] = node_id
\r
230 column = column + stride.y
\r
233 -- Add spiral segments
\r
234 local stride_axis, stride_other = stride.x, stride.z
\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
243 segment_length = segment_length + spacer
\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
251 for y = 1, height do
\r
252 data[column] = node_id
\r
253 column = column + stride.y
\r
256 count = count + segment_length * height
\r
257 stride_axis, stride_other = stride_other, stride_axis -- Swap axes
\r
260 -- Add shorter final segment
\r
262 for index = 1, segment_length do
\r
263 i = i + stride_axis * sign
\r
266 for y = 1, height do
\r
267 data[column] = node_id
\r
268 column = column + stride.y
\r
271 count = count + segment_length * height
\r
273 mh.finish(manip, data)
\r