1 worldedit = worldedit or {}
\r
2 local minetest = minetest --local copy of global
\r
4 --adds a hollow sphere centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
\r
5 worldedit.hollow_sphere = function(pos, radius, nodename)
\r
6 --set up voxel manipulator
\r
7 local manip = minetest.get_voxel_manip()
\r
8 local pos1 = {x=pos.x - radius, y=pos.y - radius, z=pos.z - radius}
\r
9 local pos2 = {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}
\r
10 local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)
\r
11 local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})
\r
13 --fill emerged area with ignore
\r
15 local ignore = minetest.get_content_id("ignore")
\r
16 for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do
\r
20 --fill selected area with node
\r
21 local node_id = minetest.get_content_id(nodename)
\r
22 local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
\r
23 local offsetx, offsety, offsetz = pos.x - emerged_pos1.x, pos.y - emerged_pos1.y, pos.z - emerged_pos1.z
\r
24 local zstride, ystride = area.zstride, area.ystride
\r
26 for z = -radius, radius do
\r
27 local newz = (z + offsetz) * zstride
\r
28 for y = -radius, radius do
\r
29 local newy = newz + (y + offsety) * ystride
\r
30 for x = -radius, radius do
\r
31 local squared = x * x + y * y + z * z
\r
32 if squared >= min_radius and squared <= max_radius then --position is on surface of sphere
\r
33 local i = newy + (x + offsetx) + 1
\r
42 manip:set_data(nodes)
\r
43 manip:write_to_map()
\r
49 --adds a sphere centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
\r
50 worldedit.sphere = function(pos, radius, nodename)
\r
51 --set up voxel manipulator
\r
52 local manip = minetest.get_voxel_manip()
\r
53 local pos1 = {x=pos.x - radius, y=pos.y - radius, z=pos.z - radius}
\r
54 local pos2 = {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}
\r
55 local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)
\r
56 local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})
\r
58 --fill emerged area with ignore
\r
60 local ignore = minetest.get_content_id("ignore")
\r
61 for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do
\r
65 --fill selected area with node
\r
66 local node_id = minetest.get_content_id(nodename)
\r
67 local max_radius = radius * (radius + 1)
\r
68 local offsetx, offsety, offsetz = pos.x - emerged_pos1.x, pos.y - emerged_pos1.y, pos.z - emerged_pos1.z
\r
69 local zstride, ystride = area.zstride, area.ystride
\r
71 for z = -radius, radius do
\r
72 local newz = (z + offsetz) * zstride
\r
73 for y = -radius, radius do
\r
74 local newy = newz + (y + offsety) * ystride
\r
75 for x = -radius, radius do
\r
76 if x * x + y * y + z * z <= max_radius then --position is inside sphere
\r
77 local i = newy + (x + offsetx) + 1
\r
86 manip:set_data(nodes)
\r
87 manip:write_to_map()
\r
93 --adds a hollow dome centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
\r
94 worldedit.hollow_dome = function(pos, radius, nodename)
\r
95 --set up voxel manipulator
\r
96 local manip = minetest.get_voxel_manip()
\r
97 local pos1 = {x=pos.x - radius, y=pos.y, z=pos.z - radius}
\r
98 local pos2 = {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}
\r
99 local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)
\r
100 local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})
\r
102 --fill emerged area with ignore
\r
104 local ignore = minetest.get_content_id("ignore")
\r
105 for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do
\r
109 --fill selected area with node
\r
110 local node_id = minetest.get_content_id(nodename)
\r
111 local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
\r
112 local offsetx, offsety, offsetz = pos.x - emerged_pos1.x, pos.y - emerged_pos1.y, pos.z - emerged_pos1.z
\r
113 local zstride, ystride = area.zstride, area.ystride
\r
115 for z = -radius, radius do
\r
116 local newz = (z + offsetz) * zstride
\r
117 for y = 0, radius do
\r
118 local newy = newz + (y + offsety) * ystride
\r
119 for x = -radius, radius do
\r
120 local squared = x * x + y * y + z * z
\r
121 if squared >= min_radius and squared <= max_radius then --position is on surface of sphere
\r
122 local i = newy + (x + offsetx) + 1
\r
131 manip:set_data(nodes)
\r
132 manip:write_to_map()
\r
138 --adds a dome centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
\r
139 worldedit.dome = function(pos, radius, nodename) --wip: use bresenham sphere for maximum speed
\r
140 --set up voxel manipulator
\r
141 local manip = minetest.get_voxel_manip()
\r
142 local pos1 = {x=pos.x - radius, y=pos.y, z=pos.z - radius}
\r
143 local pos2 = {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}
\r
144 local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)
\r
145 local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})
\r
147 --fill emerged area with ignore
\r
149 local ignore = minetest.get_content_id("ignore")
\r
150 for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do
\r
154 --fill selected area with node
\r
155 local node_id = minetest.get_content_id(nodename)
\r
156 local max_radius = radius * (radius + 1)
\r
157 local offsetx, offsety, offsetz = pos.x - emerged_pos1.x, pos.y - emerged_pos1.y, pos.z - emerged_pos1.z
\r
158 local zstride, ystride = area.zstride, area.ystride
\r
160 for z = -radius, radius do
\r
161 local newz = (z + offsetz) * zstride
\r
162 for y = 0, radius do
\r
163 local newy = newz + (y + offsety) * ystride
\r
164 for x = -radius, radius do
\r
165 if x * x + y * y + z * z <= max_radius then --position is inside sphere
\r
166 local i = newy + (x + offsetx) + 1
\r
175 manip:set_data(nodes)
\r
176 manip:write_to_map()
\r
182 --adds a hollow cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `length` and radius `radius`, composed of `nodename`, returning the number of nodes added
\r
183 worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename) --wip: rewrite this using voxelmanip
\r
184 local other1, other2
\r
185 if axis == "x" then
\r
186 other1, other2 = "y", "z"
\r
187 elseif axis == "y" then
\r
188 other1, other2 = "x", "z"
\r
190 other1, other2 = "x", "y"
\r
193 --handle negative lengths
\r
194 local currentpos = {x=pos.x, y=pos.y, z=pos.z}
\r
197 currentpos[axis] = currentpos[axis] - length
\r
200 --create schematic for single node column along the axis
\r
201 local node = {name=nodename, param1=0, param2=0}
\r
203 for i = 1, length do
\r
206 local schematic = {size={[axis]=length, [other1]=1, [other2]=1}, data=nodes}
\r
208 --add columns in a circle around axis to form cylinder
\r
209 local place_schematic = minetest.place_schematic
\r
211 local offset1, offset2 = 0, radius
\r
212 local delta = -radius
\r
213 while offset1 <= offset2 do
\r
214 --add node at each octant
\r
215 local first1, first2 = pos[other1] + offset1, pos[other1] - offset1
\r
216 local second1, second2 = pos[other2] + offset2, pos[other2] - offset2
\r
217 currentpos[other1], currentpos[other2] = first1, second1
\r
218 place_schematic(currentpos, schematic) --octant 1
\r
219 currentpos[other1] = first2
\r
220 place_schematic(currentpos, schematic) --octant 4
\r
221 currentpos[other2] = second2
\r
222 place_schematic(currentpos, schematic) --octant 5
\r
223 currentpos[other1] = first1
\r
224 place_schematic(currentpos, schematic) --octant 8
\r
225 local first1, first2 = pos[other1] + offset2, pos[other1] - offset2
\r
226 local second1, second2 = pos[other2] + offset1, pos[other2] - offset1
\r
227 currentpos[other1], currentpos[other2] = first1, second1
\r
228 place_schematic(currentpos, schematic) --octant 2
\r
229 currentpos[other1] = first2
\r
230 place_schematic(currentpos, schematic) --octant 3
\r
231 currentpos[other2] = second2
\r
232 place_schematic(currentpos, schematic) --octant 6
\r
233 currentpos[other1] = first1
\r
234 place_schematic(currentpos, schematic) --octant 7
\r
236 count = count + 8 --wip: broken because sometimes currentpos is repeated
\r
238 --move to next location
\r
239 delta = delta + (offset1 * 2) + 1
\r
241 offset2 = offset2 - 1
\r
242 delta = delta - (offset2 * 2)
\r
244 offset1 = offset1 + 1
\r
246 count = count * length --apply the length to the number of nodes
\r
250 --adds a cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `length` and radius `radius`, composed of `nodename`, returning the number of nodes added
\r
251 worldedit.cylinder = function(pos, axis, length, radius, nodename, env) --wip: rewrite this using voxelmanip
\r
252 local other1, other2
\r
253 if axis == "x" then
\r
254 other1, other2 = "y", "z"
\r
255 elseif axis == "y" then
\r
256 other1, other2 = "x", "z"
\r
258 other1, other2 = "x", "y"
\r
261 --handle negative lengths
\r
262 local currentpos = {x=pos.x, y=pos.y, z=pos.z}
\r
265 currentpos[axis] = currentpos[axis] - length
\r
268 --set up voxel manipulator
\r
269 local manip = minetest.get_voxel_manip()
\r
271 [axis]=currentpos[axis],
\r
272 [other1]=currentpos[other1] - radius,
\r
273 [other2]=currentpos[other2] - radius
\r
276 [axis]=currentpos[axis] + length - 1,
\r
277 [other1]=currentpos[other1] + radius,
\r
278 [other2]=currentpos[other2] + radius
\r
280 local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)
\r
281 local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})
\r
283 --fill emerged area with ignore
\r
285 local ignore = minetest.get_content_id("ignore")
\r
286 for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do
\r
290 --fill selected area with node
\r
291 local node_id = minetest.get_content_id(nodename)
\r
292 local max_radius = radius * (radius + 1)
\r
293 local stride = {x=1, y=area.ystride, z=area.zstride}
\r
294 local offset = {x=currentpos.x - emerged_pos1.x, y=currentpos.y - emerged_pos1.y, z=currentpos.z - emerged_pos1.z}
\r
295 local min_slice, max_slice = offset[axis], offset[axis] + length - 1
\r
297 for axis1 = -radius, radius do
\r
298 local newaxis1 = (axis1 + offset[other1]) * stride[other1]
\r
299 for axis2 = -radius, radius do
\r
300 local newaxis2 = newaxis1 + (axis2 + offset[other2]) * stride[other2]
\r
301 if axis1 * axis1 + axis2 * axis2 <= max_radius then
\r
302 for slice = min_slice, max_slice do
\r
303 local i = newaxis2 + slice * stride[axis] + 1
\r
306 count = count + length
\r
312 manip:set_data(nodes)
\r
313 manip:write_to_map()
\r
319 --adds a pyramid centered at `pos` with height `height`, composed of `nodename`, returning the number of nodes added
\r
320 worldedit.pyramid = function(pos, height, nodename, env) --wip: rewrite this using voxelmanip
\r
321 local pos1x, pos1y, pos1z = pos.x - height, pos.y, pos.z - height
\r
322 local pos2x, pos2y, pos2z = pos.x + height, pos.y + height, pos.z + height
\r
323 local pos = {x=0, y=pos1y, z=0}
\r
325 local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
\r
327 --set up voxel manipulator
\r
328 local manip = minetest.get_voxel_manip()
\r
329 local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)
\r
330 local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})
\r
332 --fill emerged area with ignore
\r
334 local ignore = minetest.get_content_id("ignore")
\r
335 for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do
\r
339 --fill selected area with node
\r
340 local node_id = minetest.get_content_id(nodename)
\r
341 for i in area:iterp(pos1, pos2) do
\r
346 manip:set_data(nodes)
\r
347 manip:write_to_map()
\r
351 local node = {name=nodename}
\r
352 if env == nil then env = minetest.env end
\r
353 while pos.y <= pos2y do --each vertical level of the pyramid
\r
355 while pos.x <= pos2x do
\r
357 while pos.z <= pos2z do
\r
358 env:add_node(pos, node)
\r
363 count = count + ((pos2y - pos.y) * 2 + 1) ^ 2
\r
366 pos1x, pos2x = pos1x + 1, pos2x - 1
\r
367 pos1z, pos2z = pos1z + 1, pos2z - 1
\r
373 --adds a spiral centered at `pos` with width `width`, height `height`, space between walls `spacer`, composed of `nodename`, returning the number of nodes added
\r
374 worldedit.spiral = function(pos, width, height, spacer, nodename, env) --wip: clean this up
\r
375 -- spiral matrix - http://rosettacode.org/wiki/Spiral_matrix#Lua
\r
376 --wip: rewrite this whole thing, nobody can understand it anyways
\r
377 av, sn = math.abs, function(s) return s~=0 and s/av(s) or 0 end
\r
378 local function sindex(z, x) -- returns the value at (x, z) in a spiral that starts at 1 and goes outwards
\r
379 if z == -x and z >= x then return (2*z+1)^2 end
\r
380 local l = math.max(av(z), av(x))
\r
381 return (2*l-1)^2+4*l+2*l*sn(x+z)+sn(z^2-x^2)*(l-(av(z)==l and sn(z)*x or sn(x)*z)) -- OH GOD WHAT
\r
383 local function spiralt(side)
\r
384 local ret, id, start, stop = {}, 0, math.floor((-side+1)/2), math.floor((side-1)/2)
\r
387 local id = side^2 - sindex(stop - i + 1,start + j - 1)
\r
388 ret[id] = {x=i,z=j}
\r
393 if env == nil then env = minetest.env end
\r
394 -- connect the joined parts
\r
395 local spiral = spiralt(width)
\r
396 height = tonumber(height)
\r
397 if height < 1 then height = 1 end
\r
398 spacer = tonumber(spacer)-1
\r
399 if spacer < 1 then spacer = 1 end
\r
401 local node = {name=nodename}
\r
405 for _,v in ipairs(spiral) do
\r
406 np = {x=pos.x+v.x*spacer, y=pos.y+y, z=pos.z+v.z*spacer}
\r
408 if lp.x~=np.x then
\r
410 for i=lp.x+1,np.x do
\r
411 env:add_node({x=i, y=np.y, z=np.z}, node)
\r
415 for i=np.x,lp.x-1 do
\r
416 env:add_node({x=i, y=np.y, z=np.z}, node)
\r
421 if lp.z~=np.z then
\r
423 for i=lp.z+1,np.z do
\r
424 env:add_node({x=np.x, y=np.y, z=i}, node)
\r
428 for i=np.z,lp.z-1 do
\r
429 env:add_node({x=np.x, y=np.y, z=i}, node)
\r