]> git.lizzy.rs Git - dragonfireclient.git/blob - src/treegen.cpp
Merge pull request #505 from RealBadAngel/master
[dragonfireclient.git] / src / treegen.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2012 celeron55, Perttu Ahola <celeron55@gmail.com>,
4                           2012-2013 RealBadAngel, Maciej Kasatkin <mk@realbadangel.pl>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "irr_v3d.h"
21 #include <stack>
22 #include "util/numeric.h"
23 #include "util/mathconstants.h"
24 #include "map.h"
25 #include "environment.h"
26 #include "nodedef.h"
27 #include "treegen.h"
28
29 namespace treegen
30 {
31 void make_tree(ManualMapVoxelManipulator &vmanip, v3s16 p0,
32                 bool is_apple_tree, INodeDefManager *ndef,int seed)
33 {
34         MapNode treenode(ndef->getId("mapgen_tree"));
35         MapNode leavesnode(ndef->getId("mapgen_leaves"));
36         MapNode applenode(ndef->getId("mapgen_apple"));
37
38         PseudoRandom ps(seed);
39         s16 trunk_h = ps.range(4, 5);
40         v3s16 p1 = p0;
41         for(s16 ii=0; ii<trunk_h; ii++)
42         {
43                 if(vmanip.m_area.contains(p1))
44                         if(ii == 0 || vmanip.getNodeNoExNoEmerge(p1).getContent() == CONTENT_AIR)
45                                 vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
46                 p1.Y++;
47         }
48
49         // p1 is now the last piece of the trunk
50         p1.Y -= 1;
51
52         VoxelArea leaves_a(v3s16(-2,-1,-2), v3s16(2,2,2));
53         //SharedPtr<u8> leaves_d(new u8[leaves_a.getVolume()]);
54         Buffer<u8> leaves_d(leaves_a.getVolume());
55         for(s32 i=0; i<leaves_a.getVolume(); i++)
56                 leaves_d[i] = 0;
57
58         // Force leaves at near the end of the trunk
59         {
60                 s16 d = 1;
61                 for(s16 z=-d; z<=d; z++)
62                 for(s16 y=-d; y<=d; y++)
63                 for(s16 x=-d; x<=d; x++)
64                 {
65                         leaves_d[leaves_a.index(v3s16(x,y,z))] = 1;
66                 }
67         }
68
69         // Add leaves randomly
70         for(u32 iii=0; iii<7; iii++)
71         {
72                 s16 d = 1;
73
74                 v3s16 p(
75                         ps.range(leaves_a.MinEdge.X, leaves_a.MaxEdge.X-d),
76                         ps.range(leaves_a.MinEdge.Y, leaves_a.MaxEdge.Y-d),
77                         ps.range(leaves_a.MinEdge.Z, leaves_a.MaxEdge.Z-d)
78                 );
79
80                 for(s16 z=0; z<=d; z++)
81                 for(s16 y=0; y<=d; y++)
82                 for(s16 x=0; x<=d; x++)
83                 {
84                         leaves_d[leaves_a.index(p+v3s16(x,y,z))] = 1;
85                 }
86         }
87
88         // Blit leaves to vmanip
89         for(s16 z=leaves_a.MinEdge.Z; z<=leaves_a.MaxEdge.Z; z++)
90         for(s16 y=leaves_a.MinEdge.Y; y<=leaves_a.MaxEdge.Y; y++)
91         for(s16 x=leaves_a.MinEdge.X; x<=leaves_a.MaxEdge.X; x++)
92         {
93                 v3s16 p(x,y,z);
94                 p += p1;
95                 if(vmanip.m_area.contains(p) == false)
96                         continue;
97                 u32 vi = vmanip.m_area.index(p);
98                 if(vmanip.m_data[vi].getContent() != CONTENT_AIR
99                                 && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
100                         continue;
101                 u32 i = leaves_a.index(x,y,z);
102                 if(leaves_d[i] == 1) {
103                         bool is_apple = ps.range(0,99) < 10;
104                         if(is_apple_tree && is_apple) {
105                                 vmanip.m_data[vi] = applenode;
106                         } else {
107                                 vmanip.m_data[vi] = leavesnode;
108                         }
109                 }
110         }
111 }
112
113 // L-System tree LUA spawner
114 void spawn_ltree (ServerEnvironment *env, v3s16 p0, INodeDefManager *ndef, TreeDef tree_definition)
115 {
116         ServerMap *map = &env->getServerMap();
117         core::map<v3s16, MapBlock*> modified_blocks;
118         ManualMapVoxelManipulator vmanip(map);
119         v3s16 tree_blockp = getNodeBlockPos(p0);
120         vmanip.initialEmerge(tree_blockp - v3s16(1,1,1), tree_blockp + v3s16(1,3,1));
121         make_ltree (vmanip, p0, ndef, tree_definition);
122         vmanip.blitBackAll(&modified_blocks);
123
124         // update lighting
125         core::map<v3s16, MapBlock*> lighting_modified_blocks;
126         for(core::map<v3s16, MapBlock*>::Iterator
127                 i = modified_blocks.getIterator();
128                 i.atEnd() == false; i++)
129         {
130                 lighting_modified_blocks.insert(i.getNode()->getKey(), i.getNode()->getValue());
131         }
132         map->updateLighting(lighting_modified_blocks, modified_blocks);
133         // Send a MEET_OTHER event
134         MapEditEvent event;
135         event.type = MEET_OTHER;
136         for(core::map<v3s16, MapBlock*>::Iterator
137                 i = modified_blocks.getIterator();
138                 i.atEnd() == false; i++)
139         {
140                 v3s16 p = i.getNode()->getKey();
141                 event.modified_blocks.insert(p, true);
142         }
143         map->dispatchEvent(&event);
144 }
145
146 //L-System tree generator
147 void make_ltree(ManualMapVoxelManipulator &vmanip, v3s16 p0, INodeDefManager *ndef,
148                 TreeDef tree_definition)
149 {
150         MapNode dirtnode(ndef->getId("mapgen_dirt"));
151
152         PseudoRandom ps(tree_definition.seed+14002);
153         // chance of inserting abcd rules
154         double prop_a = 9;
155         double prop_b = 8;
156         double prop_c = 7;
157         double prop_d = 6;
158
159         //randomize tree growth level, minimum=2
160         s16 iterations = tree_definition.iterations;
161         if (tree_definition.iterations_random_level>0)
162                 iterations -= ps.range(0,tree_definition.iterations_random_level);
163         if (iterations<2)
164                 iterations=2;
165
166         s16 MAX_ANGLE_OFFSET = 5;
167         double angle_in_radians = (double)tree_definition.angle*M_PI/180;
168         double angleOffset_in_radians = (s16)(ps.range(0,1)%MAX_ANGLE_OFFSET)*M_PI/180;
169
170         //initialize rotation matrix, position and stacks for branches
171         core::matrix4 rotation;
172         rotation = setRotationAxisRadians(rotation, M_PI/2,v3f(0,0,1));
173         v3f position;
174         position.X = p0.X;
175         position.Y = p0.Y;
176         position.Z = p0.Z;
177         std::stack <core::matrix4> stack_orientation;
178         std::stack <v3f> stack_position;
179
180         //generate axiom
181         std::string axiom = tree_definition.initial_axiom;
182         for(s16 i=0; i<iterations; i++)
183         {
184                 std::string temp = "";
185                 for(s16 j=0; j<(s16)axiom.size(); j++)
186                 {
187                         char axiom_char = axiom.at(j);
188                         switch (axiom_char)
189                         {
190                         case 'A':
191                                 temp+=tree_definition.rules_a;
192                                 break;
193                         case 'B':
194                                 temp+=tree_definition.rules_b;
195                                 break;
196                         case 'C':
197                                 temp+=tree_definition.rules_c;
198                                 break;
199                         case 'D':
200                                 temp+=tree_definition.rules_d;
201                                 break;
202                         case 'a':
203                                 if (prop_a >= ps.range(1,10))
204                                         temp+=tree_definition.rules_a;
205                                 break;
206                         case 'b':
207                                 if (prop_b >= ps.range(1,10))
208                                         temp+=tree_definition.rules_b;
209                                 break;
210                         case 'c':
211                                 if (prop_c >= ps.range(1,10))
212                                         temp+=tree_definition.rules_c;
213                                 break;
214                         case 'd':
215                                 if (prop_d >= ps.range(1,10))
216                                         temp+=tree_definition.rules_d;
217                                 break;
218                         default:
219                                 temp+=axiom_char;
220                                 break;
221                         }
222                 }
223                 axiom=temp;
224         }
225
226         //make sure tree is not floating in the air
227         if (tree_definition.trunk_type == "double")
228         {
229                 tree_node_placement(vmanip,v3f(position.X+1,position.Y-1,position.Z),dirtnode);
230                 tree_node_placement(vmanip,v3f(position.X,position.Y-1,position.Z+1),dirtnode);
231                 tree_node_placement(vmanip,v3f(position.X+1,position.Y-1,position.Z+1),dirtnode);
232         }
233         if (tree_definition.trunk_type == "crossed")
234         {
235                 tree_node_placement(vmanip,v3f(position.X+1,position.Y-1,position.Z),dirtnode);
236                 tree_node_placement(vmanip,v3f(position.X-1,position.Y-1,position.Z),dirtnode);
237                 tree_node_placement(vmanip,v3f(position.X,position.Y-1,position.Z+1),dirtnode);
238                 tree_node_placement(vmanip,v3f(position.X,position.Y-1,position.Z-1),dirtnode);
239         }
240
241         /* build tree out of generated axiom
242
243         Key for Special L-System Symbols used in Axioms
244
245     G  - move forward one unit with the pen up
246     F  - move forward one unit with the pen down drawing trunks and branches
247     f  - move forward one unit with the pen down drawing leaves (100% chance)
248     T  - move forward one unit with the pen down drawing trunks only
249     R  - move forward one unit with the pen down placing fruit
250     A  - replace with rules set A
251     B  - replace with rules set B
252     C  - replace with rules set C
253     D  - replace with rules set D
254     a  - replace with rules set A, chance 90%
255     b  - replace with rules set B, chance 80%
256     c  - replace with rules set C, chance 70%
257     d  - replace with rules set D, chance 60%
258     +  - yaw the turtle right by angle degrees
259     -  - yaw the turtle left by angle degrees
260     &  - pitch the turtle down by angle degrees
261     ^  - pitch the turtle up by angle degrees
262     /  - roll the turtle to the right by angle degrees
263     *  - roll the turtle to the left by angle degrees
264     [  - save in stack current state info
265     ]  - recover from stack state info
266
267     */
268
269         s16 x,y,z;
270         for(s16 i=0; i<(s16)axiom.size(); i++)
271         {
272                 char axiom_char = axiom.at(i);
273                 core::matrix4 temp_rotation;
274                 temp_rotation.makeIdentity();
275                 v3f dir;
276                 switch (axiom_char)
277                 {
278                 case 'G':
279                         dir = v3f(1,0,0);
280                         dir = transposeMatrix(rotation,dir);
281                         position+=dir;
282                         break;
283                 case 'T':
284                         tree_trunk_placement(vmanip,v3f(position.X,position.Y,position.Z),tree_definition);
285                         if (tree_definition.trunk_type == "double" && !tree_definition.thin_branches)
286                         {
287                                 tree_trunk_placement(vmanip,v3f(position.X+1,position.Y,position.Z),tree_definition);
288                                 tree_trunk_placement(vmanip,v3f(position.X,position.Y,position.Z+1),tree_definition);
289                                 tree_trunk_placement(vmanip,v3f(position.X+1,position.Y,position.Z+1),tree_definition);
290                         }
291                         if (tree_definition.trunk_type == "crossed" && !tree_definition.thin_branches)
292                         {
293                                 tree_trunk_placement(vmanip,v3f(position.X+1,position.Y,position.Z),tree_definition);
294                                 tree_trunk_placement(vmanip,v3f(position.X-1,position.Y,position.Z),tree_definition);
295                                 tree_trunk_placement(vmanip,v3f(position.X,position.Y,position.Z+1),tree_definition);
296                                 tree_trunk_placement(vmanip,v3f(position.X,position.Y,position.Z-1),tree_definition);
297                         }
298                         dir = v3f(1,0,0);
299                         dir = transposeMatrix(rotation,dir);
300                         position+=dir;
301                         break;
302                 case 'F':
303                         tree_trunk_placement(vmanip,v3f(position.X,position.Y,position.Z),tree_definition);
304                         if ((stack_orientation.empty() && tree_definition.trunk_type == "double") ||
305                                 (!stack_orientation.empty() && tree_definition.trunk_type == "double" && !tree_definition.thin_branches))
306                         {
307                                 tree_trunk_placement(vmanip,v3f(position.X+1,position.Y,position.Z),tree_definition);
308                                 tree_trunk_placement(vmanip,v3f(position.X,position.Y,position.Z+1),tree_definition);
309                                 tree_trunk_placement(vmanip,v3f(position.X+1,position.Y,position.Z+1),tree_definition);
310                         }
311                         if ((stack_orientation.empty() && tree_definition.trunk_type == "crossed") ||
312                                 (!stack_orientation.empty() && tree_definition.trunk_type == "crossed" && !tree_definition.thin_branches))
313                         {
314                                 tree_trunk_placement(vmanip,v3f(position.X+1,position.Y,position.Z),tree_definition);
315                                 tree_trunk_placement(vmanip,v3f(position.X-1,position.Y,position.Z),tree_definition);
316                                 tree_trunk_placement(vmanip,v3f(position.X,position.Y,position.Z+1),tree_definition);
317                                 tree_trunk_placement(vmanip,v3f(position.X,position.Y,position.Z-1),tree_definition);
318                         }
319                         if (stack_orientation.empty() == false)
320                         {
321                                 s16 size = 1;
322                                 for(x=-size; x<=size; x++)
323                                         for(y=-size; y<=size; y++)
324                                                 for(z=-size; z<=size; z++)
325                                                         if (abs(x) == size && abs(y) == size && abs(z) == size)
326                                                         {
327                                                                 tree_leaves_placement(vmanip,v3f(position.X+x+1,position.Y+y,position.Z+z),ps.next(), tree_definition);
328                                                                 tree_leaves_placement(vmanip,v3f(position.X+x-1,position.Y+y,position.Z+z),ps.next(), tree_definition);
329                                                                 tree_leaves_placement(vmanip,v3f(position.X+x,position.Y+y,position.Z+z+1),ps.next(), tree_definition);
330                                                                 tree_leaves_placement(vmanip,v3f(position.X+x,position.Y+y,position.Z+z-1),ps.next(), tree_definition);
331                                                         }
332                         }
333                         dir = v3f(1,0,0);
334                         dir = transposeMatrix(rotation,dir);
335                         position+=dir;
336                         break;
337                 case 'f':
338                         tree_single_leaves_placement(vmanip,v3f(position.X,position.Y,position.Z),ps.next() ,tree_definition);
339                         dir = v3f(1,0,0);
340                         dir = transposeMatrix(rotation,dir);
341                         position+=dir;
342                         break;
343                 case 'R':
344                         tree_fruit_placement(vmanip,v3f(position.X,position.Y,position.Z),tree_definition);
345                         dir = v3f(1,0,0);
346                         dir = transposeMatrix(rotation,dir);
347                         position+=dir;
348                         break;
349
350                 // turtle orientation commands
351                 case '[':
352                         stack_orientation.push(rotation);
353                         stack_position.push(position);
354                         break;
355                 case ']':
356                         rotation=stack_orientation.top();
357                         stack_orientation.pop();
358                         position=stack_position.top();
359                         stack_position.pop();
360                         break;
361                 case '+':
362                         temp_rotation.makeIdentity();
363                         temp_rotation=setRotationAxisRadians(temp_rotation, angle_in_radians+angleOffset_in_radians,v3f(0,0,1));
364                         rotation*=temp_rotation;
365                         break;
366                 case '-':
367                         temp_rotation.makeIdentity();
368                         temp_rotation=setRotationAxisRadians(temp_rotation, angle_in_radians+angleOffset_in_radians,v3f(0,0,-1));
369                         rotation*=temp_rotation;
370                         break;
371                 case '&':
372                         temp_rotation.makeIdentity();
373                         temp_rotation=setRotationAxisRadians(temp_rotation, angle_in_radians+angleOffset_in_radians,v3f(0,1,0));
374                         rotation*=temp_rotation;
375                         break;
376                 case '^':
377                         temp_rotation.makeIdentity();
378                         temp_rotation=setRotationAxisRadians(temp_rotation, angle_in_radians+angleOffset_in_radians,v3f(0,-1,0));
379                         rotation*=temp_rotation;
380                         break;
381                 case '*':
382                         temp_rotation.makeIdentity();
383                         temp_rotation=setRotationAxisRadians(temp_rotation, angle_in_radians,v3f(1,0,0));
384                         rotation*=temp_rotation;
385                         break;
386                 case '/':
387                         temp_rotation.makeIdentity();
388                         temp_rotation=setRotationAxisRadians(temp_rotation, angle_in_radians,v3f(-1,0,0));
389                         rotation*=temp_rotation;
390                         break;
391                 default:
392                         break;
393                 }
394         }
395 }
396
397 void tree_node_placement(ManualMapVoxelManipulator &vmanip, v3f p0,
398                 MapNode node)
399 {
400         v3s16 p1 = v3s16(myround(p0.X),myround(p0.Y),myround(p0.Z));
401         if(vmanip.m_area.contains(p1) == false)
402                 return;
403         u32 vi = vmanip.m_area.index(p1);
404         if(vmanip.m_data[vi].getContent() != CONTENT_AIR
405                         && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
406                 return;
407         vmanip.m_data[vmanip.m_area.index(p1)] = node;
408 }
409
410 void tree_trunk_placement(ManualMapVoxelManipulator &vmanip, v3f p0,
411                 TreeDef &tree_definition)
412 {
413         v3s16 p1 = v3s16(myround(p0.X),myround(p0.Y),myround(p0.Z));
414         if(vmanip.m_area.contains(p1) == false)
415                 return;
416         u32 vi = vmanip.m_area.index(p1);
417         if(vmanip.m_data[vi].getContent() != CONTENT_AIR
418                         && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
419                 return;
420         vmanip.m_data[vmanip.m_area.index(p1)] = tree_definition.trunknode;
421 }
422
423 void tree_leaves_placement(ManualMapVoxelManipulator &vmanip, v3f p0,
424                 PseudoRandom ps ,TreeDef &tree_definition)
425 {
426         MapNode leavesnode=tree_definition.leavesnode;
427         if (ps.range(1,100) > 100-tree_definition.leaves2_chance)
428                 leavesnode=tree_definition.leaves2node;
429         v3s16 p1 = v3s16(myround(p0.X),myround(p0.Y),myround(p0.Z));
430         if(vmanip.m_area.contains(p1) == false)
431                 return;
432         u32 vi = vmanip.m_area.index(p1);
433         if(vmanip.m_data[vi].getContent() != CONTENT_AIR
434                         && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
435                 return; 
436         if (tree_definition.fruit_chance>0)
437         {
438                 if (ps.range(1,100) > 100-tree_definition.fruit_chance)
439                         vmanip.m_data[vmanip.m_area.index(p1)] = tree_definition.fruitnode;
440                 else
441                         vmanip.m_data[vmanip.m_area.index(p1)] = leavesnode;
442         }
443         else if (ps.range(1,100) > 20)
444                 vmanip.m_data[vmanip.m_area.index(p1)] = leavesnode;
445 }
446
447 void tree_single_leaves_placement(ManualMapVoxelManipulator &vmanip, v3f p0,
448                 PseudoRandom ps, TreeDef &tree_definition)
449 {
450         MapNode leavesnode=tree_definition.leavesnode;
451         if (ps.range(1,100) > 100-tree_definition.leaves2_chance)
452                 leavesnode=tree_definition.leaves2node;
453         v3s16 p1 = v3s16(myround(p0.X),myround(p0.Y),myround(p0.Z));
454         if(vmanip.m_area.contains(p1) == false)
455                 return;
456         u32 vi = vmanip.m_area.index(p1);
457         if(vmanip.m_data[vi].getContent() != CONTENT_AIR
458                 && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
459                 return;
460         vmanip.m_data[vmanip.m_area.index(p1)] = leavesnode;
461 }
462
463 void tree_fruit_placement(ManualMapVoxelManipulator &vmanip, v3f p0,
464                 TreeDef &tree_definition)
465 {
466         v3s16 p1 = v3s16(myround(p0.X),myround(p0.Y),myround(p0.Z));
467         if(vmanip.m_area.contains(p1) == false)
468                 return;
469         u32 vi = vmanip.m_area.index(p1);
470         if(vmanip.m_data[vi].getContent() != CONTENT_AIR
471                 && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
472                 return;
473         vmanip.m_data[vmanip.m_area.index(p1)] = tree_definition.fruitnode;
474 }
475
476 irr::core::matrix4 setRotationAxisRadians(irr::core::matrix4 M, double angle, v3f axis)
477 {
478         double c = cos(angle);
479         double s = sin(angle);
480         double t = 1.0 - c;
481
482         double tx  = t * axis.X;
483         double ty  = t * axis.Y;
484         double tz  = t * axis.Z;
485         double sx  = s * axis.X;
486         double sy  = s * axis.Y;
487         double sz  = s * axis.Z;
488
489         M[0] = tx * axis.X + c;
490         M[1] = tx * axis.Y + sz;
491         M[2] = tx * axis.Z - sy;
492
493         M[4] = ty * axis.X - sz;
494         M[5] = ty * axis.Y + c;
495         M[6] = ty * axis.Z + sx;
496
497         M[8]  = tz * axis.X + sy;
498         M[9]  = tz * axis.Y - sx;
499         M[10] = tz * axis.Z + c;
500         return M;
501 }
502
503 v3f transposeMatrix(irr::core::matrix4 M, v3f v)
504 {
505         v3f translated;
506         double x = M[0] * v.X + M[4] * v.Y + M[8]  * v.Z +M[12];
507         double y = M[1] * v.X + M[5] * v.Y + M[9]  * v.Z +M[13];
508         double z = M[2] * v.X + M[6] * v.Y + M[10] * v.Z +M[14];
509         translated.X=x;
510         translated.Y=y;
511         translated.Z=z;
512         return translated;
513 }
514
515 #if 0
516 static void make_jungletree(VoxelManipulator &vmanip, v3s16 p0,
517                 INodeDefManager *ndef)
518 {
519         MapNode treenode(ndef->getId("mapgen_jungletree"));
520         MapNode leavesnode(ndef->getId("mapgen_leaves"));
521
522         for(s16 x=-1; x<=1; x++)
523         for(s16 z=-1; z<=1; z++)
524         {
525                 if(myrand_range(0, 2) == 0)
526                         continue;
527                 v3s16 p1 = p0 + v3s16(x,0,z);
528                 v3s16 p2 = p0 + v3s16(x,-1,z);
529                 if(vmanip.m_area.contains(p2)
530                                 && vmanip.m_data[vmanip.m_area.index(p2)] == CONTENT_AIR)
531                         vmanip.m_data[vmanip.m_area.index(p2)] = treenode;
532                 else if(vmanip.m_area.contains(p1))
533                         vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
534         }
535
536         s16 trunk_h = myrand_range(8, 12);
537         v3s16 p1 = p0;
538         for(s16 ii=0; ii<trunk_h; ii++)
539         {
540                 if(vmanip.m_area.contains(p1))
541                         vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
542                 p1.Y++;
543         }
544
545         // p1 is now the last piece of the trunk
546         p1.Y -= 1;
547
548         VoxelArea leaves_a(v3s16(-3,-2,-3), v3s16(3,2,3));
549         //SharedPtr<u8> leaves_d(new u8[leaves_a.getVolume()]);
550         Buffer<u8> leaves_d(leaves_a.getVolume());
551         for(s32 i=0; i<leaves_a.getVolume(); i++)
552                 leaves_d[i] = 0;
553
554         // Force leaves at near the end of the trunk
555         {
556                 s16 d = 1;
557                 for(s16 z=-d; z<=d; z++)
558                 for(s16 y=-d; y<=d; y++)
559                 for(s16 x=-d; x<=d; x++)
560                 {
561                         leaves_d[leaves_a.index(v3s16(x,y,z))] = 1;
562                 }
563         }
564
565         // Add leaves randomly
566         for(u32 iii=0; iii<30; iii++)
567         {
568                 s16 d = 1;
569
570                 v3s16 p(
571                         myrand_range(leaves_a.MinEdge.X, leaves_a.MaxEdge.X-d),
572                         myrand_range(leaves_a.MinEdge.Y, leaves_a.MaxEdge.Y-d),
573                         myrand_range(leaves_a.MinEdge.Z, leaves_a.MaxEdge.Z-d)
574                 );
575
576                 for(s16 z=0; z<=d; z++)
577                 for(s16 y=0; y<=d; y++)
578                 for(s16 x=0; x<=d; x++)
579                 {
580                         leaves_d[leaves_a.index(p+v3s16(x,y,z))] = 1;
581                 }
582         }
583
584         // Blit leaves to vmanip
585         for(s16 z=leaves_a.MinEdge.Z; z<=leaves_a.MaxEdge.Z; z++)
586         for(s16 y=leaves_a.MinEdge.Y; y<=leaves_a.MaxEdge.Y; y++)
587         for(s16 x=leaves_a.MinEdge.X; x<=leaves_a.MaxEdge.X; x++)
588         {
589                 v3s16 p(x,y,z);
590                 p += p1;
591                 if(vmanip.m_area.contains(p) == false)
592                         continue;
593                 u32 vi = vmanip.m_area.index(p);
594                 if(vmanip.m_data[vi].getContent() != CONTENT_AIR
595                                 && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
596                         continue;
597                 u32 i = leaves_a.index(x,y,z);
598                 if(leaves_d[i] == 1)
599                         vmanip.m_data[vi] = leavesnode;
600         }
601 }
602 #endif
603
604 }; // namespace treegen