]> git.lizzy.rs Git - worldedit.git/blob - worldedit/primitives.lua
worldedit.hollow_sphere returns non-nil count
[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) --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
8         local count = 0\r
9         local env = minetest.env\r
10         for x = -radius, radius do\r
11                 pos1.x = pos.x + x\r
12                 for y = -radius, radius do\r
13                         pos1.y = pos.y + y\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
16                                         pos1.z = pos.z + z\r
17                                         env:add_node({x=pos.x+x,y=pos.y+y,z=pos.z+z}, node)\r
18                                         count = count + 1\r
19                                 end\r
20                         end\r
21                 end\r
22         end\r
23         return count\r
24 end\r
25 \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
31         local count = 0\r
32         local env = minetest.env\r
33         for x = -radius, radius do\r
34                 pos1.x = pos.x + x\r
35                 for y = -radius, radius do\r
36                         pos1.y = pos.y + y\r
37                         for z = -radius, radius do\r
38                                 if x*x+y*y+z*z <= full_radius then\r
39                                         pos1.z = pos.z + z\r
40                                         env:add_node(pos1, node)\r
41                                         count = count + 1\r
42                                 end\r
43                         end\r
44                 end\r
45         end\r
46         return count\r
47 end\r
48 \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
52         if axis == "x" then\r
53                 other1, other2 = "y", "z"\r
54         elseif axis == "y" then\r
55                 other1, other2 = "x", "z"\r
56         else --axis == "z"\r
57                 other1, other2 = "x", "y"\r
58         end\r
59 \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
63         local count = 0\r
64         local step = 1\r
65         if length < 0 then\r
66                 length = -length\r
67                 step = -1\r
68         end\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
94 \r
95                         count = count + 8 --wip: broken\r
96 \r
97                         --move to next location\r
98                         delta = delta + (offset1 * 2) + 1\r
99                         if delta >= 0 then\r
100                                 offset2 = offset2 - 1\r
101                                 delta = delta - (offset2 * 2)\r
102                         end\r
103                         offset1 = offset1 + 1\r
104                 end\r
105                 currentpos[axis] = currentpos[axis] + step\r
106         end\r
107         return count\r
108 end\r
109 \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
117         else --axis == "z"\r
118                 other1, other2 = "x", "y"\r
119         end\r
120 \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
124         local count = 0\r
125         local step = 1\r
126         if length < 0 then\r
127                 length = -length\r
128                 step = -1\r
129         end\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
143                         end\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
152                         end\r
153 \r
154                         count = count + (offset1 * 4) + (offset2 * 4) + 4 --wip: broken\r
155 \r
156                         --move to next location\r
157                         delta = delta + (offset1 * 2) + 1\r
158                         offset1 = offset1 + 1\r
159                         if delta >= 0 then\r
160                                 offset2 = offset2 - 1\r
161                                 delta = delta - (offset2 * 2)\r
162                         end\r
163                 end\r
164                 currentpos[axis] = currentpos[axis] + step\r
165         end\r
166         return count\r
167 end\r
168 \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
174 \r
175         local count = 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
179                 pos.x = pos1x\r
180                 while pos.x <= pos2x do\r
181                         pos.z = pos1z\r
182                         while pos.z <= pos2z do\r
183                                 env:add_node(pos, node)\r
184                                 pos.z = pos.z + 1\r
185                         end\r
186                         pos.x = pos.x + 1\r
187                 end\r
188                 count = count + ((pos2y - pos.y) * 2 + 1) ^ 2\r
189                 pos.y = pos.y + 1\r
190 \r
191                 pos1x, pos2x = pos1x + 1, pos2x - 1\r
192                 pos1z, pos2z = pos1z + 1, pos2z - 1\r
193 \r
194         end\r
195         return count\r
196 end\r
197 \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
206         end\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
209                 for i = 1, side do\r
210                         for j = 1, side do\r
211                                 local id = side^2 - sindex(stop - i + 1,start + j - 1)\r
212                                 ret[id] = {x=i,z=j}\r
213                         end\r
214                 end\r
215                 return ret\r
216         end\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
223         local count = 0\r
224         local node = {name=nodename}\r
225         local np,lp\r
226         for y=0,height do\r
227                 lp = nil\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
230                         if lp~=nil then\r
231                                 if lp.x~=np.x then \r
232                                         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
235                                                         count = count + 1\r
236                                                 end\r
237                                         else\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
240                                                         count = count + 1\r
241                                                 end\r
242                                         end\r
243                                 end\r
244                                 if lp.z~=np.z then \r
245                                         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
248                                                         count = count + 1\r
249                                                 end\r
250                                         else\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
253                                                         count = count + 1\r
254                                                 end\r
255                                         end\r
256                                 end\r
257                         end\r
258                         lp = np\r
259                 end\r
260         end\r
261         return count\r
262 end