]> git.lizzy.rs Git - worldedit.git/blob - worldedit/primitives.lua
d0c93a717cb20aa53656ca4d294e247838a01008
[worldedit.git] / worldedit / primitives.lua
1 worldedit = worldedit or {}\r
2 \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
9         local count = 0\r
10         if env == nil then env = minetest.env end\r
11         for x = -radius, radius do\r
12                 pos1.x = pos.x + x\r
13                 for y = -radius, radius do\r
14                         pos1.y = pos.y + y\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
17                                         pos1.z = pos.z + z\r
18                                         env:add_node(pos1, node)\r
19                                         count = count + 1\r
20                                 end\r
21                         end\r
22                 end\r
23         end\r
24         return count\r
25 end\r
26 \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
32         local count = 0\r
33         if env == nil then env = minetest.env end\r
34         for x = -radius, radius do\r
35                 pos1.x = pos.x + x\r
36                 for y = -radius, radius do\r
37                         pos1.y = pos.y + y\r
38                         for z = -radius, radius do\r
39                                 if x*x+y*y+z*z <= max_radius then\r
40                                         pos1.z = pos.z + z\r
41                                         env:add_node(pos1, node)\r
42                                         count = count + 1\r
43                                 end\r
44                         end\r
45                 end\r
46         end\r
47         return count\r
48 end\r
49 \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
56         local count = 0\r
57         if env == nil then env = minetest.env end\r
58         for x = -radius, radius do\r
59                 pos1.x = pos.x + x\r
60                 for y = 0, radius do\r
61                         pos1.y = pos.y + y\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
64                                         pos1.z = pos.z + z\r
65                                         env:add_node(pos1, node)\r
66                                         count = count + 1\r
67                                 end\r
68                         end\r
69                 end\r
70         end\r
71         return count\r
72 end\r
73 \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
79         local count = 0\r
80         if env == nil then env = minetest.env end\r
81         for x = -radius, radius do\r
82                 pos1.x = pos.x + x\r
83                 for y = 0, radius do\r
84                         pos1.y = pos.y + y\r
85                         for z = -radius, radius do\r
86                                 if x*x+y*y+z*z <= max_radius then\r
87                                         pos1.z = pos.z + z\r
88                                         env:add_node(pos1, node)\r
89                                         count = count + 1\r
90                                 end\r
91                         end\r
92                 end\r
93         end\r
94         return count\r
95 end\r
96 \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
104         else --axis == "z"\r
105                 other1, other2 = "x", "y"\r
106         end\r
107 \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
111         local count = 0\r
112         local step = 1\r
113         if length < 0 then\r
114                 length = -length\r
115                 step = -1\r
116         end\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
142 \r
143                         count = count + 8 --wip: broken\r
144 \r
145                         --move to next location\r
146                         delta = delta + (offset1 * 2) + 1\r
147                         if delta >= 0 then\r
148                                 offset2 = offset2 - 1\r
149                                 delta = delta - (offset2 * 2)\r
150                         end\r
151                         offset1 = offset1 + 1\r
152                 end\r
153                 currentpos[axis] = currentpos[axis] + step\r
154         end\r
155         return count\r
156 end\r
157 \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
165         else --axis == "z"\r
166                 other1, other2 = "x", "y"\r
167         end\r
168 \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
172         local count = 0\r
173         local step = 1\r
174         if length < 0 then\r
175                 length = -length\r
176                 step = -1\r
177         end\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
191                         end\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
200                         end\r
201 \r
202                         count = count + (offset1 * 4) + (offset2 * 4) + 4 --wip: broken\r
203 \r
204                         --move to next location\r
205                         delta = delta + (offset1 * 2) + 1\r
206                         offset1 = offset1 + 1\r
207                         if delta >= 0 then\r
208                                 offset2 = offset2 - 1\r
209                                 delta = delta - (offset2 * 2)\r
210                         end\r
211                 end\r
212                 currentpos[axis] = currentpos[axis] + step\r
213         end\r
214         return count\r
215 end\r
216 \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
222 \r
223         local count = 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
227                 pos.x = pos1x\r
228                 while pos.x <= pos2x do\r
229                         pos.z = pos1z\r
230                         while pos.z <= pos2z do\r
231                                 env:add_node(pos, node)\r
232                                 pos.z = pos.z + 1\r
233                         end\r
234                         pos.x = pos.x + 1\r
235                 end\r
236                 count = count + ((pos2y - pos.y) * 2 + 1) ^ 2\r
237                 pos.y = pos.y + 1\r
238 \r
239                 pos1x, pos2x = pos1x + 1, pos2x - 1\r
240                 pos1z, pos2z = pos1z + 1, pos2z - 1\r
241 \r
242         end\r
243         return count\r
244 end\r
245 \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
254         end\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
257                 for i = 1, side do\r
258                         for j = 1, side do\r
259                                 local id = side^2 - sindex(stop - i + 1,start + j - 1)\r
260                                 ret[id] = {x=i,z=j}\r
261                         end\r
262                 end\r
263                 return ret\r
264         end\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
272         local count = 0\r
273         local node = {name=nodename}\r
274         local np,lp\r
275         for y=0,height do\r
276                 lp = nil\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
279                         if lp~=nil then\r
280                                 if lp.x~=np.x then \r
281                                         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
284                                                         count = count + 1\r
285                                                 end\r
286                                         else\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
289                                                         count = count + 1\r
290                                                 end\r
291                                         end\r
292                                 end\r
293                                 if lp.z~=np.z then \r
294                                         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
297                                                         count = count + 1\r
298                                                 end\r
299                                         else\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
302                                                         count = count + 1\r
303                                                 end\r
304                                         end\r
305                                 end\r
306                         end\r
307                         lp = np\r
308                 end\r
309         end\r
310         return count\r
311 end\r