1 worldedit = worldedit or {}
\r
3 --adds a hollow sphere at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
\r
4 worldedit.hollow_sphere = function(pos, radius, nodename, env)
\r
5 local node = {name=nodename}
\r
6 local pos1 = {x=0, y=0, z=0}
\r
7 local min_radius = radius * (radius - 1)
\r
8 local max_radius = radius * (radius + 1)
\r
10 if env == nil then env = minetest.env end
\r
11 for x = -radius, radius do
\r
13 for y = -radius, radius do
\r
15 for z = -radius, radius do
\r
16 if x*x+y*y+z*z >= min_radius and x*x+y*y+z*z <= max_radius then
\r
18 env:add_node(pos1, node)
\r
27 --adds a sphere at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
\r
28 worldedit.sphere = function(pos, radius, nodename, env)
\r
29 local node = {name=nodename}
\r
30 local pos1 = {x=0, y=0, z=0}
\r
31 local max_radius = radius * (radius + 1)
\r
33 if env == nil then env = minetest.env end
\r
34 for x = -radius, radius do
\r
36 for y = -radius, radius do
\r
38 for z = -radius, radius do
\r
39 if x*x+y*y+z*z <= max_radius then
\r
41 env:add_node(pos1, node)
\r
50 --adds a hollow dome at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
\r
51 worldedit.hollow_dome = function(pos, radius, nodename, env) --wip: use bresenham sphere for maximum speed
\r
52 local node = {name=nodename}
\r
53 local pos1 = {x=0, y=0, z=0}
\r
54 local min_radius = radius * (radius - 1)
\r
55 local max_radius = radius * (radius + 1)
\r
57 if env == nil then env = minetest.env end
\r
58 for x = -radius, radius do
\r
60 for y = 0, radius do
\r
62 for z = -radius, radius do
\r
63 if x*x+y*y+z*z >= min_radius and x*x+y*y+z*z <= max_radius then
\r
65 env:add_node(pos1, node)
\r
74 --adds a dome at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
\r
75 worldedit.dome = function(pos, radius, nodename, env) --wip: use bresenham sphere for maximum speed
\r
76 local node = {name=nodename}
\r
77 local pos1 = {x=0, y=0, z=0}
\r
78 local max_radius = radius * (radius + 1)
\r
80 if env == nil then env = minetest.env end
\r
81 for x = -radius, radius do
\r
83 for y = 0, radius do
\r
85 for z = -radius, radius do
\r
86 if x*x+y*y+z*z <= max_radius then
\r
88 env:add_node(pos1, node)
\r
97 --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
98 worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename, env)
\r
99 local other1, other2
\r
100 if axis == "x" then
\r
101 other1, other2 = "y", "z"
\r
102 elseif axis == "y" then
\r
103 other1, other2 = "x", "z"
\r
105 other1, other2 = "x", "y"
\r
108 if env == nil then env = minetest.env end
\r
109 local currentpos = {x=pos.x, y=pos.y, z=pos.z}
\r
110 local node = {name=nodename}
\r
117 for i = 1, length do
\r
118 local offset1, offset2 = 0, radius
\r
119 local delta = -radius
\r
120 while offset1 <= offset2 do
\r
121 --add node at each octant
\r
122 local first1, first2 = pos[other1] + offset1, pos[other1] - offset1
\r
123 local second1, second2 = pos[other2] + offset2, pos[other2] - offset2
\r
124 currentpos[other1], currentpos[other2] = first1, second1
\r
125 env:add_node(currentpos, node) --octant 1
\r
126 currentpos[other1] = first2
\r
127 env:add_node(currentpos, node) --octant 4
\r
128 currentpos[other2] = second2
\r
129 env:add_node(currentpos, node) --octant 5
\r
130 currentpos[other1] = first1
\r
131 env:add_node(currentpos, node) --octant 8
\r
132 local first1, first2 = pos[other1] + offset2, pos[other1] - offset2
\r
133 local second1, second2 = pos[other2] + offset1, pos[other2] - offset1
\r
134 currentpos[other1], currentpos[other2] = first1, second1
\r
135 env:add_node(currentpos, node) --octant 2
\r
136 currentpos[other1] = first2
\r
137 env:add_node(currentpos, node) --octant 3
\r
138 currentpos[other2] = second2
\r
139 env:add_node(currentpos, node) --octant 6
\r
140 currentpos[other1] = first1
\r
141 env:add_node(currentpos, node) --octant 7
\r
143 count = count + 8 --wip: broken
\r
145 --move to next location
\r
146 delta = delta + (offset1 * 2) + 1
\r
148 offset2 = offset2 - 1
\r
149 delta = delta - (offset2 * 2)
\r
151 offset1 = offset1 + 1
\r
153 currentpos[axis] = currentpos[axis] + step
\r
158 --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
159 worldedit.cylinder = function(pos, axis, length, radius, nodename, env)
\r
160 local other1, other2
\r
161 if axis == "x" then
\r
162 other1, other2 = "y", "z"
\r
163 elseif axis == "y" then
\r
164 other1, other2 = "x", "z"
\r
166 other1, other2 = "x", "y"
\r
169 if env == nil then env = minetest.env end
\r
170 local currentpos = {x=pos.x, y=pos.y, z=pos.z}
\r
171 local node = {name=nodename}
\r
178 for i = 1, length do
\r
179 local offset1, offset2 = 0, radius
\r
180 local delta = -radius
\r
181 while offset1 <= offset2 do
\r
182 --connect each pair of octants
\r
183 currentpos[other1] = pos[other1] - offset1
\r
184 local second1, second2 = pos[other2] + offset2, pos[other2] - offset2
\r
185 for i = 0, offset1 * 2 do
\r
186 currentpos[other2] = second1
\r
187 env:add_node(currentpos, node) --octant 1 to 4
\r
188 currentpos[other2] = second2
\r
189 env:add_node(currentpos, node) --octant 5 to 8
\r
190 currentpos[other1] = currentpos[other1] + 1
\r
192 currentpos[other1] = pos[other1] - offset2
\r
193 local second1, second2 = pos[other2] + offset1, pos[other2] - offset1
\r
194 for i = 0, offset2 * 2 do
\r
195 currentpos[other2] = second1
\r
196 env:add_node(currentpos, node) --octant 2 to 3
\r
197 currentpos[other2] = second2
\r
198 env:add_node(currentpos, node) --octant 6 to 7
\r
199 currentpos[other1] = currentpos[other1] + 1
\r
202 count = count + (offset1 * 4) + (offset2 * 4) + 4 --wip: broken
\r
204 --move to next location
\r
205 delta = delta + (offset1 * 2) + 1
\r
206 offset1 = offset1 + 1
\r
208 offset2 = offset2 - 1
\r
209 delta = delta - (offset2 * 2)
\r
212 currentpos[axis] = currentpos[axis] + step
\r
217 --adds a pyramid at `pos` with height `height`, composed of `nodename`, returning the number of nodes added
\r
218 worldedit.pyramid = function(pos, height, nodename, env)
\r
219 local pos1x, pos1y, pos1z = pos.x - height, pos.y, pos.z - height
\r
220 local pos2x, pos2y, pos2z = pos.x + height, pos.y + height, pos.z + height
\r
221 local pos = {x=0, y=pos1y, z=0}
\r
224 local node = {name=nodename}
\r
225 if env == nil then env = minetest.env end
\r
226 while pos.y <= pos2y do --each vertical level of the pyramid
\r
228 while pos.x <= pos2x do
\r
230 while pos.z <= pos2z do
\r
231 env:add_node(pos, node)
\r
236 count = count + ((pos2y - pos.y) * 2 + 1) ^ 2
\r
239 pos1x, pos2x = pos1x + 1, pos2x - 1
\r
240 pos1z, pos2z = pos1z + 1, pos2z - 1
\r
246 --adds a spiral at `pos` with width `width`, height `height`, space between walls `spacer`, composed of `nodename`, returning the number of nodes added
\r
247 worldedit.spiral = function(pos, width, height, spacer, nodename, env) --wip: clean this up
\r
248 -- spiral matrix - http://rosettacode.org/wiki/Spiral_matrix#Lua
\r
249 av, sn = math.abs, function(s) return s~=0 and s/av(s) or 0 end
\r
250 local function sindex(z, x) -- returns the value at (x, z) in a spiral that starts at 1 and goes outwards
\r
251 if z == -x and z >= x then return (2*z+1)^2 end
\r
252 local l = math.max(av(z), av(x))
\r
253 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
255 local function spiralt(side)
\r
256 local ret, id, start, stop = {}, 0, math.floor((-side+1)/2), math.floor((side-1)/2)
\r
259 local id = side^2 - sindex(stop - i + 1,start + j - 1)
\r
260 ret[id] = {x=i,z=j}
\r
265 if env == nil then env = minetest.env end
\r
266 -- connect the joined parts
\r
267 local spiral = spiralt(width)
\r
268 height = tonumber(height)
\r
269 if height < 1 then height = 1 end
\r
270 spacer = tonumber(spacer)-1
\r
271 if spacer < 1 then spacer = 1 end
\r
273 local node = {name=nodename}
\r
277 for _,v in ipairs(spiral) do
\r
278 np = {x=pos.x+v.x*spacer, y=pos.y+y, z=pos.z+v.z*spacer}
\r
280 if lp.x~=np.x then
\r
282 for i=lp.x+1,np.x do
\r
283 env:add_node({x=i, y=np.y, z=np.z}, node)
\r
287 for i=np.x,lp.x-1 do
\r
288 env:add_node({x=i, y=np.y, z=np.z}, node)
\r
293 if lp.z~=np.z then
\r
295 for i=lp.z+1,np.z do
\r
296 env:add_node({x=np.x, y=np.y, z=i}, node)
\r
300 for i=np.z,lp.z-1 do
\r
301 env:add_node({x=np.x, y=np.y, z=i}, node)
\r