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 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
103 -- Backwards compatibility
\r
104 if type(radius2) == "string" then
\r
106 node_name = radius2
\r
107 radius2 = radius1 -- straight cylinder
\r
110 -- Handle negative lengths
\r
111 local current_pos = {x=pos.x, y=pos.y, z=pos.z}
\r
114 current_pos[axis] = current_pos[axis] - length
\r
115 radius1, radius2 = radius2, radius1
\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
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
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
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
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
153 mh.finish(manip, data)
\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
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
174 -- Handle inverted pyramids
\r
175 local start_axis, end_axis, step
\r
177 height = height - 1
\r
180 height = height + 1
\r
185 local node_id = minetest.get_content_id(node_name)
\r
186 local stride = {x=1, y=area.ystride, z=area.zstride}
\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
192 local size = math.abs(height * step)
\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
211 mh.finish(manip, data)
\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
227 local manip, area = mh.init_axis_radius_length(pos, "y", extent, height)
\r
228 local data = mh.get_empty_data(area)
\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
236 -- Add first column
\r
237 local count = height
\r
239 for y = 1, height do
\r
240 data[column] = node_id
\r
241 column = column + stride.y
\r
244 -- Add spiral segments
\r
245 local stride_axis, stride_other = stride.x, stride.z
\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
254 segment_length = segment_length + spacer
\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
262 for y = 1, height do
\r
263 data[column] = node_id
\r
264 column = column + stride.y
\r
267 count = count + segment_length * height
\r
268 stride_axis, stride_other = stride_other, stride_axis -- Swap axes
\r
271 -- Add shorter final segment
\r
273 for index = 1, segment_length do
\r
274 i = i + stride_axis * sign
\r
277 for y = 1, height do
\r
278 data[column] = node_id
\r
279 column = column + stride.y
\r
282 count = count + segment_length * height
\r
284 mh.finish(manip, data)
\r