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) --wip: use bresenham sphere for maximum speed
\r
5 local node = {name=nodename}
\r
6 local pos1 = {x=0, y=0, z=0}
\r
7 local full_radius = radius * radius + radius
\r
9 local env = minetest.env
\r
10 for x = -radius, radius do
\r
12 for y = -radius, radius do
\r
14 for z = -radius, radius do
\r
15 if x*x+y*y+z*z >= (radius-1) * (radius-1) + (radius-1) and x*x+y*y+z*z <= full_radius then
\r
17 env:add_node({x=pos.x+x,y=pos.y+y,z=pos.z+z}, node)
\r
26 --adds a sphere at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
\r
27 worldedit.sphere = function(pos, radius, nodename) --wip: use bresenham sphere for maximum speed
\r
28 local node = {name=nodename}
\r
29 local pos1 = {x=0, y=0, z=0}
\r
30 local full_radius = radius * radius + radius
\r
32 local env = minetest.env
\r
33 for x = -radius, radius do
\r
35 for y = -radius, radius do
\r
37 for z = -radius, radius do
\r
38 if x*x+y*y+z*z <= full_radius then
\r
40 env:add_node(pos1, node)
\r
49 --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
50 worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename)
\r
51 local other1, other2
\r
53 other1, other2 = "y", "z"
\r
54 elseif axis == "y" then
\r
55 other1, other2 = "x", "z"
\r
57 other1, other2 = "x", "y"
\r
60 local env = minetest.env
\r
61 local currentpos = {x=pos.x, y=pos.y, z=pos.z}
\r
62 local node = {name=nodename}
\r
69 for i = 1, length do
\r
70 local offset1, offset2 = 0, radius
\r
71 local delta = -radius
\r
72 while offset1 <= offset2 do
\r
73 --add node at each octant
\r
74 local first1, first2 = pos[other1] + offset1, pos[other1] - offset1
\r
75 local second1, second2 = pos[other2] + offset2, pos[other2] - offset2
\r
76 currentpos[other1], currentpos[other2] = first1, second1
\r
77 env:add_node(currentpos, node) --octant 1
\r
78 currentpos[other1] = first2
\r
79 env:add_node(currentpos, node) --octant 4
\r
80 currentpos[other2] = second2
\r
81 env:add_node(currentpos, node) --octant 5
\r
82 currentpos[other1] = first1
\r
83 env:add_node(currentpos, node) --octant 8
\r
84 local first1, first2 = pos[other1] + offset2, pos[other1] - offset2
\r
85 local second1, second2 = pos[other2] + offset1, pos[other2] - offset1
\r
86 currentpos[other1], currentpos[other2] = first1, second1
\r
87 env:add_node(currentpos, node) --octant 2
\r
88 currentpos[other1] = first2
\r
89 env:add_node(currentpos, node) --octant 3
\r
90 currentpos[other2] = second2
\r
91 env:add_node(currentpos, node) --octant 6
\r
92 currentpos[other1] = first1
\r
93 env:add_node(currentpos, node) --octant 7
\r
95 count = count + 8 --wip: broken
\r
97 --move to next location
\r
98 delta = delta + (offset1 * 2) + 1
\r
100 offset2 = offset2 - 1
\r
101 delta = delta - (offset2 * 2)
\r
103 offset1 = offset1 + 1
\r
105 currentpos[axis] = currentpos[axis] + step
\r
110 --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
111 worldedit.cylinder = function(pos, axis, length, radius, nodename)
\r
112 local other1, other2
\r
113 if axis == "x" then
\r
114 other1, other2 = "y", "z"
\r
115 elseif axis == "y" then
\r
116 other1, other2 = "x", "z"
\r
118 other1, other2 = "x", "y"
\r
121 local env = minetest.env
\r
122 local currentpos = {x=pos.x, y=pos.y, z=pos.z}
\r
123 local node = {name=nodename}
\r
130 for i = 1, length do
\r
131 local offset1, offset2 = 0, radius
\r
132 local delta = -radius
\r
133 while offset1 <= offset2 do
\r
134 --connect each pair of octants
\r
135 currentpos[other1] = pos[other1] - offset1
\r
136 local second1, second2 = pos[other2] + offset2, pos[other2] - offset2
\r
137 for i = 0, offset1 * 2 do
\r
138 currentpos[other2] = second1
\r
139 env:add_node(currentpos, node) --octant 1 to 4
\r
140 currentpos[other2] = second2
\r
141 env:add_node(currentpos, node) --octant 5 to 8
\r
142 currentpos[other1] = currentpos[other1] + 1
\r
144 currentpos[other1] = pos[other1] - offset2
\r
145 local second1, second2 = pos[other2] + offset1, pos[other2] - offset1
\r
146 for i = 0, offset2 * 2 do
\r
147 currentpos[other2] = second1
\r
148 env:add_node(currentpos, node) --octant 2 to 3
\r
149 currentpos[other2] = second2
\r
150 env:add_node(currentpos, node) --octant 6 to 7
\r
151 currentpos[other1] = currentpos[other1] + 1
\r
154 count = count + (offset1 * 4) + (offset2 * 4) + 4 --wip: broken
\r
156 --move to next location
\r
157 delta = delta + (offset1 * 2) + 1
\r
158 offset1 = offset1 + 1
\r
160 offset2 = offset2 - 1
\r
161 delta = delta - (offset2 * 2)
\r
164 currentpos[axis] = currentpos[axis] + step
\r
169 --adds a pyramid at `pos` with height `height`, composed of `nodename`, returning the number of nodes added
\r
170 worldedit.pyramid = function(pos, height, nodename)
\r
171 local pos1x, pos1y, pos1z = pos.x - height, pos.y, pos.z - height
\r
172 local pos2x, pos2y, pos2z = pos.x + height, pos.y + height, pos.z + height
\r
173 local pos = {x=0, y=pos1y, z=0}
\r
176 local node = {name=nodename}
\r
177 local env = minetest.env
\r
178 while pos.y <= pos2y do --each vertical level of the pyramid
\r
180 while pos.x <= pos2x do
\r
182 while pos.z <= pos2z do
\r
183 env:add_node(pos, node)
\r
188 count = count + ((pos2y - pos.y) * 2 + 1) ^ 2
\r
191 pos1x, pos2x = pos1x + 1, pos2x - 1
\r
192 pos1z, pos2z = pos1z + 1, pos2z - 1
\r
198 --adds a spiral at `pos` with width `width`, height `height`, space between walls `spacer`, composed of `nodename`, returning the number of nodes added
\r
199 worldedit.spiral = function(pos, width, height, spacer, nodename) --wip: clean this up
\r
200 -- spiral matrix - http://rosettacode.org/wiki/Spiral_matrix#Lua
\r
201 av, sn = math.abs, function(s) return s~=0 and s/av(s) or 0 end
\r
202 local function sindex(z, x) -- returns the value at (x, z) in a spiral that starts at 1 and goes outwards
\r
203 if z == -x and z >= x then return (2*z+1)^2 end
\r
204 local l = math.max(av(z), av(x))
\r
205 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
207 local function spiralt(side)
\r
208 local ret, id, start, stop = {}, 0, math.floor((-side+1)/2), math.floor((side-1)/2)
\r
211 local id = side^2 - sindex(stop - i + 1,start + j - 1)
\r
212 ret[id] = {x=i,z=j}
\r
217 -- connect the joined parts
\r
218 local spiral = spiralt(width)
\r
219 height = tonumber(height)
\r
220 if height < 1 then height = 1 end
\r
221 spacer = tonumber(spacer)-1
\r
222 if spacer < 1 then spacer = 1 end
\r
224 local node = {name=nodename}
\r
228 for _,v in ipairs(spiral) do
\r
229 np = {x=pos.x+v.x*spacer, y=pos.y+y, z=pos.z+v.z*spacer}
\r
231 if lp.x~=np.x then
\r
233 for i=lp.x+1,np.x do
\r
234 minetest.env:add_node({x=i, y=np.y, z=np.z}, node)
\r
238 for i=np.x,lp.x-1 do
\r
239 minetest.env:add_node({x=i, y=np.y, z=np.z}, node)
\r
244 if lp.z~=np.z then
\r
246 for i=lp.z+1,np.z do
\r
247 minetest.env:add_node({x=np.x, y=np.y, z=i}, node)
\r
251 for i=np.z,lp.z-1 do
\r
252 minetest.env:add_node({x=np.x, y=np.y, z=i}, node)
\r