1 --- Functions for creating primitive shapes.
\r
2 -- @module worldedit.primitives
\r
4 local mh = worldedit.manip_helpers
\r
8 -- @param pos Position of ground level center of cube
\r
9 -- @param width Cube width. (x)
\r
10 -- @param height Cube height. (y)
\r
11 -- @param length Cube length. (z)
\r
12 -- @param node_name Name of node to make cube of.
\r
13 -- @param hollow Whether the cube should be hollow.
\r
14 -- @return The number of nodes added.
\r
15 function worldedit.cube(pos, width, height, length, node_name, hollow)
\r
16 -- Set up voxel manipulator
\r
17 local basepos = vector.subtract(pos, {x=math.floor(width/2), y=0, z=math.floor(length/2)})
\r
18 local manip, area = mh.init(basepos, vector.add(basepos, {x=width, y=height, z=length}))
\r
19 local data = mh.get_empty_data(area)
\r
22 local node_id = minetest.get_content_id(node_name)
\r
23 local stride = {x=1, y=area.ystride, z=area.zstride}
\r
24 local offset = vector.subtract(basepos, area.MinEdge)
\r
27 for z = 0, length-1 do
\r
28 local index_z = (offset.z + z) * stride.z + 1 -- +1 for 1-based indexing
\r
29 for y = 0, height-1 do
\r
30 local index_y = index_z + (offset.y + y) * stride.y
\r
31 for x = 0, width-1 do
\r
32 local is_wall = z == 0 or z == length-1
\r
33 or y == 0 or y == height-1
\r
34 or x == 0 or x == width-1
\r
35 if not hollow or is_wall then
\r
36 local i = index_y + (offset.x + x)
\r
44 mh.finish(manip, data)
\r
48 --- Adds a sphere of `node_name` centered at `pos`.
\r
49 -- @param pos Position to center sphere at.
\r
50 -- @param radius Sphere radius.
\r
51 -- @param node_name Name of node to make shere of.
\r
52 -- @param hollow Whether the sphere should be hollow.
\r
53 -- @return The number of nodes added.
\r
54 function worldedit.sphere(pos, radius, node_name, hollow)
\r
55 local manip, area = mh.init_radius(pos, radius)
\r
57 local data = mh.get_empty_data(area)
\r
59 -- Fill selected area with node
\r
60 local node_id = minetest.get_content_id(node_name)
\r
61 local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
\r
62 local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z
\r
63 local stride_z, stride_y = area.zstride, area.ystride
\r
65 for z = -radius, radius do
\r
66 -- Offset contributed by z plus 1 to make it 1-indexed
\r
67 local new_z = (z + offset_z) * stride_z + 1
\r
68 for y = -radius, radius do
\r
69 local new_y = new_z + (y + offset_y) * stride_y
\r
70 for x = -radius, radius do
\r
71 local squared = x * x + y * y + z * z
\r
72 if squared <= max_radius and (not hollow or squared >= min_radius) then
\r
73 -- Position is on surface of sphere
\r
74 local i = new_y + (x + offset_x)
\r
82 mh.finish(manip, data)
\r
89 -- @param pos Position to center dome at.
\r
90 -- @param radius Dome radius. Negative for concave domes.
\r
91 -- @param node_name Name of node to make dome of.
\r
92 -- @param hollow Whether the dome should be hollow.
\r
93 -- @return The number of nodes added.
\r
94 -- TODO: Add axis option.
\r
95 function worldedit.dome(pos, radius, node_name, hollow)
\r
96 local min_y, max_y = 0, radius
\r
99 min_y, max_y = -radius, 0
\r
102 local manip, area = mh.init_axis_radius(pos, "y", radius)
\r
103 local data = mh.get_empty_data(area)
\r
106 local node_id = minetest.get_content_id(node_name)
\r
107 local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
\r
108 local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z
\r
109 local stride_z, stride_y = area.zstride, area.ystride
\r
111 for z = -radius, radius do
\r
112 local new_z = (z + offset_z) * stride_z + 1 --offset contributed by z plus 1 to make it 1-indexed
\r
113 for y = min_y, max_y do
\r
114 local new_y = new_z + (y + offset_y) * stride_y
\r
115 for x = -radius, radius do
\r
116 local squared = x * x + y * y + z * z
\r
117 if squared <= max_radius and (not hollow or squared >= min_radius) then
\r
118 -- Position is in dome
\r
119 local i = new_y + (x + offset_x)
\r
127 mh.finish(manip, data)
\r
132 --- Adds a cylinder.
\r
133 -- @param pos Position to center base of cylinder at.
\r
134 -- @param axis Axis ("x", "y", or "z")
\r
135 -- @param length Cylinder length.
\r
136 -- @param radius1 Cylinder base radius.
\r
137 -- @param radius2 Cylinder top radius.
\r
138 -- @param node_name Name of node to make cylinder of.
\r
139 -- @param hollow Whether the cylinder should be hollow.
\r
140 -- @return The number of nodes added.
\r
141 function worldedit.cylinder(pos, axis, length, radius1, radius2, node_name, hollow)
\r
142 local other1, other2 = worldedit.get_axis_others(axis)
\r
144 -- Backwards compatibility
\r
145 if type(radius2) == "string" then
\r
147 node_name = radius2
\r
148 radius2 = radius1 -- straight cylinder
\r
151 -- Handle negative lengths
\r
152 local current_pos = {x=pos.x, y=pos.y, z=pos.z}
\r
155 current_pos[axis] = current_pos[axis] - length
\r
156 radius1, radius2 = radius2, radius1
\r
159 -- Set up voxel manipulator
\r
160 local manip, area = mh.init_axis_radius_length(current_pos, axis, math.max(radius1, radius2), length)
\r
161 local data = mh.get_empty_data(area)
\r
163 -- Add desired shape (anything inbetween cylinder & cone)
\r
164 local node_id = minetest.get_content_id(node_name)
\r
165 local stride = {x=1, y=area.ystride, z=area.zstride}
\r
167 x = current_pos.x - area.MinEdge.x,
\r
168 y = current_pos.y - area.MinEdge.y,
\r
169 z = current_pos.z - area.MinEdge.z,
\r
172 for i = 0, length - 1 do
\r
173 -- Calulate radius for this "height" in the cylinder
\r
174 local radius = radius1 + (radius2 - radius1) * (i + 1) / length
\r
175 radius = math.floor(radius + 0.5) -- round
\r
176 local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
\r
178 for index2 = -radius, radius do
\r
179 -- Offset contributed by other axis 1 plus 1 to make it 1-indexed
\r
180 local new_index2 = (index2 + offset[other1]) * stride[other1] + 1
\r
181 for index3 = -radius, radius do
\r
182 local new_index3 = new_index2 + (index3 + offset[other2]) * stride[other2]
\r
183 local squared = index2 * index2 + index3 * index3
\r
184 if squared <= max_radius and (not hollow or squared >= min_radius) then
\r
185 -- Position is in cylinder, add node here
\r
186 local vi = new_index3 + (offset[axis] + i) * stride[axis]
\r
194 mh.finish(manip, data)
\r
200 --- Adds a pyramid.
\r
201 -- @param pos Position to center base of pyramid at.
\r
202 -- @param axis Axis ("x", "y", or "z")
\r
203 -- @param height Pyramid height.
\r
204 -- @param node_name Name of node to make pyramid of.
\r
205 -- @param hollow Whether the pyramid should be hollow.
\r
206 -- @return The number of nodes added.
\r
207 function worldedit.pyramid(pos, axis, height, node_name, hollow)
\r
208 local other1, other2 = worldedit.get_axis_others(axis)
\r
210 -- Set up voxel manipulator
\r
211 -- FIXME: passing negative <radius> causes mis-sorted pos to be passed
\r
212 -- into mh.init() which is technically not allowed but works
\r
213 local manip, area = mh.init_axis_radius(pos, axis, height)
\r
214 local data = mh.get_empty_data(area)
\r
216 -- Handle inverted pyramids
\r
219 height = height - 1
\r
222 height = height + 1
\r
227 local node_id = minetest.get_content_id(node_name)
\r
228 local stride = {x=1, y=area.ystride, z=area.zstride}
\r
230 x = pos.x - area.MinEdge.x,
\r
231 y = pos.y - area.MinEdge.y,
\r
232 z = pos.z - area.MinEdge.z,
\r
234 local size = math.abs(height * step)
\r
236 -- For each level of the pyramid
\r
237 for index1 = 0, height, step do
\r
238 -- Offset contributed by axis plus 1 to make it 1-indexed
\r
239 local new_index1 = (index1 + offset[axis]) * stride[axis] + 1
\r
240 for index2 = -size, size do
\r
241 local new_index2 = new_index1 + (index2 + offset[other1]) * stride[other1]
\r
242 for index3 = -size, size do
\r
243 local i = new_index2 + (index3 + offset[other2]) * stride[other2]
\r
244 if (not hollow or size - math.abs(index2) < 2 or size - math.abs(index3) < 2) then
\r
253 mh.finish(manip, data)
\r
259 -- @param pos Position to center spiral at.
\r
260 -- @param length Spral length.
\r
261 -- @param height Spiral height.
\r
262 -- @param spacer Space between walls.
\r
263 -- @param node_name Name of node to make spiral of.
\r
264 -- @return Number of nodes added.
\r
265 -- TODO: Add axis option.
\r
266 function worldedit.spiral(pos, length, height, spacer, node_name)
\r
267 local extent = math.ceil(length / 2)
\r
269 local manip, area = mh.init_axis_radius_length(pos, "y", extent, height)
\r
270 local data = mh.get_empty_data(area)
\r
272 -- Set up variables
\r
273 local node_id = minetest.get_content_id(node_name)
\r
274 local stride = {x=1, y=area.ystride, z=area.zstride}
\r
275 local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z
\r
276 local i = offset_z * stride.z + offset_y * stride.y + offset_x + 1
\r
278 -- Add first column
\r
279 local count = height
\r
281 for y = 1, height do
\r
282 data[column] = node_id
\r
283 column = column + stride.y
\r
286 -- Add spiral segments
\r
287 local stride_axis, stride_other = stride.x, stride.z
\r
289 local segment_length = 0
\r
290 spacer = spacer + 1
\r
291 -- Go through each segment except the last
\r
292 for segment = 1, math.floor(length / spacer) * 2 do
\r
293 -- Change sign and length every other turn starting with the first
\r
294 if segment % 2 == 1 then
\r
296 segment_length = segment_length + spacer
\r
299 for index = 1, segment_length do
\r
300 -- Move along the direction of the segment
\r
301 i = i + stride_axis * sign
\r
304 for y = 1, height do
\r
305 data[column] = node_id
\r
306 column = column + stride.y
\r
309 count = count + segment_length * height
\r
310 stride_axis, stride_other = stride_other, stride_axis -- Swap axes
\r
313 -- Add shorter final segment
\r
315 for index = 1, segment_length do
\r
316 i = i + stride_axis * sign
\r
319 for y = 1, height do
\r
320 data[column] = node_id
\r
321 column = column + stride.y
\r
324 count = count + segment_length * height
\r
326 mh.finish(manip, data)
\r