54cee0fefba098f20f2b87dd04f4bbd881dd0515
[oweals/minetest.git] / src / treegen.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2012 celeron55, Perttu Ahola <celeron55@gmail.com>,
4                                    2012 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 "noise.h"
25 #include "map.h"
26 #include "environment.h"
27 #include "nodedef.h"
28 #include "treegen.h"
29
30 namespace treegen
31 {
32
33 void make_tree(ManualMapVoxelManipulator &vmanip, v3s16 p0,
34                 bool is_apple_tree, INodeDefManager *ndef)
35 {
36         MapNode treenode(ndef->getId("mapgen_tree"));
37         MapNode leavesnode(ndef->getId("mapgen_leaves"));
38         MapNode applenode(ndef->getId("mapgen_apple"));
39
40         s16 trunk_h = myrand_range(4, 5);
41         v3s16 p1 = p0;
42         for(s16 ii=0; ii<trunk_h; ii++)
43         {
44                 if(vmanip.m_area.contains(p1))
45                         if(ii == 0 || vmanip.getNodeNoExNoEmerge(p1).getContent() == CONTENT_AIR)
46                                 vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
47                 p1.Y++;
48         }
49
50         // p1 is now the last piece of the trunk
51         p1.Y -= 1;
52
53         VoxelArea leaves_a(v3s16(-2,-1,-2), v3s16(2,2,2));
54         //SharedPtr<u8> leaves_d(new u8[leaves_a.getVolume()]);
55         Buffer<u8> leaves_d(leaves_a.getVolume());
56         for(s32 i=0; i<leaves_a.getVolume(); i++)
57                 leaves_d[i] = 0;
58
59         // Force leaves at near the end of the trunk
60         {
61                 s16 d = 1;
62                 for(s16 z=-d; z<=d; z++)
63                 for(s16 y=-d; y<=d; y++)
64                 for(s16 x=-d; x<=d; x++)
65                 {
66                         leaves_d[leaves_a.index(v3s16(x,y,z))] = 1;
67                 }
68         }
69
70         // Add leaves randomly
71         for(u32 iii=0; iii<7; iii++)
72         {
73                 s16 d = 1;
74
75                 v3s16 p(
76                         myrand_range(leaves_a.MinEdge.X, leaves_a.MaxEdge.X-d),
77                         myrand_range(leaves_a.MinEdge.Y, leaves_a.MaxEdge.Y-d),
78                         myrand_range(leaves_a.MinEdge.Z, leaves_a.MaxEdge.Z-d)
79                 );
80
81                 for(s16 z=0; z<=d; z++)
82                 for(s16 y=0; y<=d; y++)
83                 for(s16 x=0; x<=d; x++)
84                 {
85                         leaves_d[leaves_a.index(p+v3s16(x,y,z))] = 1;
86                 }
87         }
88
89         // Blit leaves to vmanip
90         for(s16 z=leaves_a.MinEdge.Z; z<=leaves_a.MaxEdge.Z; z++)
91         for(s16 y=leaves_a.MinEdge.Y; y<=leaves_a.MaxEdge.Y; y++)
92         for(s16 x=leaves_a.MinEdge.X; x<=leaves_a.MaxEdge.X; x++)
93         {
94                 v3s16 p(x,y,z);
95                 p += p1;
96                 if(vmanip.m_area.contains(p) == false)
97                         continue;
98                 u32 vi = vmanip.m_area.index(p);
99                 if(vmanip.m_data[vi].getContent() != CONTENT_AIR
100                                 && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
101                         continue;
102                 u32 i = leaves_a.index(x,y,z);
103                 if(leaves_d[i] == 1) {
104                         bool is_apple = myrand_range(0,99) < 10;
105                         if(is_apple_tree && is_apple) {
106                                 vmanip.m_data[vi] = applenode;
107                         } else {
108                                 vmanip.m_data[vi] = leavesnode;
109                         }
110                 }
111         }
112 }
113
114 // L-System tree LUA spawner
115 void spawn_ltree (ServerEnvironment *env, v3s16 p0, INodeDefManager *ndef, TreeDef tree_definition)
116 {
117         ServerMap *map = &env->getServerMap();
118         core::map<v3s16, MapBlock*> modified_blocks;
119         ManualMapVoxelManipulator vmanip(map);
120         v3s16 tree_blockp = getNodeBlockPos(p0);
121         vmanip.initialEmerge(tree_blockp - v3s16(1,1,1), tree_blockp + v3s16(1,1,1));
122         make_ltree (vmanip, p0, ndef, tree_definition);
123         vmanip.blitBackAll(&modified_blocks);
124
125         // update lighting
126         core::map<v3s16, MapBlock*> lighting_modified_blocks;
127         for(core::map<v3s16, MapBlock*>::Iterator
128                 i = modified_blocks.getIterator();
129                 i.atEnd() == false; i++)
130         {
131                 lighting_modified_blocks.insert(i.getNode()->getKey(), i.getNode()->getValue());
132         }
133         map->updateLighting(lighting_modified_blocks, modified_blocks);
134         // Send a MEET_OTHER event
135         MapEditEvent event;
136         event.type = MEET_OTHER;
137         for(core::map<v3s16, MapBlock*>::Iterator
138                 i = modified_blocks.getIterator();
139                 i.atEnd() == false; i++)
140         {
141                 v3s16 p = i.getNode()->getKey();
142                 event.modified_blocks.insert(p, true);
143         }
144         map->dispatchEvent(&event);
145 }
146
147 //L-System tree generator
148 void make_ltree(ManualMapVoxelManipulator &vmanip, v3s16 p0, INodeDefManager *ndef,
149                 TreeDef tree_definition)
150 {
151         MapNode dirtnode(ndef->getId("mapgen_dirt"));
152
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 -= myrand_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)(myrand_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 = v3f(0,0,0);
174         std::stack <core::matrix4> stack_orientation;
175         std::stack <v3f> stack_position;
176
177         //generate axiom
178         std::string axiom = tree_definition.initial_axiom;
179         for(s16 i=0; i<iterations; i++)
180         {
181                 std::string temp = "";
182                 for(s16 j=0; j<(s16)axiom.size(); j++)
183                 {
184                         char axiom_char = axiom.at(j);
185                         switch (axiom_char)
186                         {
187                         case 'A':
188                                 temp+=tree_definition.rules_a;
189                                 break;
190                         case 'B':
191                                 temp+=tree_definition.rules_b;
192                                 break;
193                         case 'C':
194                                 temp+=tree_definition.rules_c;
195                                 break;
196                         case 'D':
197                                 temp+=tree_definition.rules_d;
198                                 break;
199                         case 'a':
200                                 if (prop_a >= myrand_range(1,10))
201                                         temp+=tree_definition.rules_a;
202                                 break;
203                         case 'b':
204                                 if (prop_b >= myrand_range(1,10))
205                                         temp+=tree_definition.rules_b;
206                                 break;
207                         case 'c':
208                                 if (prop_c >= myrand_range(1,10))
209                                         temp+=tree_definition.rules_c;
210                                 break;
211                         case 'd':
212                                 if (prop_d >= myrand_range(1,10))
213                                         temp+=tree_definition.rules_d;
214                                 break;
215                         default:
216                                 temp+=axiom_char;
217                                 break;
218                         }
219                 }
220                 axiom=temp;
221         }
222
223         //make sure tree is not floating in the air
224         if (tree_definition.thin_trunks == false)
225         {
226                 make_tree_node_placement(vmanip,v3f(p0.X+position.X+1,p0.Y+position.Y-1,p0.Z+position.Z),dirtnode);
227                 make_tree_node_placement(vmanip,v3f(p0.X+position.X-1,p0.Y+position.Y-1,p0.Z+position.Z),dirtnode);
228                 make_tree_node_placement(vmanip,v3f(p0.X+position.X,p0.Y+position.Y-1,p0.Z+position.Z+1),dirtnode);
229                 make_tree_node_placement(vmanip,v3f(p0.X+position.X,p0.Y+position.Y-1,p0.Z+position.Z-1),dirtnode);
230         }
231
232         /* build tree out of generated axiom
233
234         Key for Special L-System Symbols used in Axioms
235
236     G  - move forward one unit with the pin down
237     F  - move forward one unit with the pin up
238     A  - replace with rules set A
239     B  - replace with rules set B
240     C  - replace with rules set C
241     D  - replace with rules set D
242     a  - replace with rules set A, chance 90%
243     b  - replace with rules set B, chance 80%
244     c  - replace with rules set C, chance 70%
245     d  - replace with rules set D, chance 60%
246     +  - yaw the turtle right by angle degrees
247     -  - yaw the turtle left by angle degrees
248     &  - pitch the turtle down by angle degrees
249     ^  - pitch the turtle up by angle degrees
250     /  - roll the turtle to the right by angle degrees
251     *  - roll the turtle to the left by angle degrees
252     [  - save in stack current state info
253     ]  - recover from stack state info
254
255     */
256
257         s16 x,y,z;
258         for(s16 i=0; i<(s16)axiom.size(); i++)
259         {
260                 char axiom_char = axiom.at(i);
261                 core::matrix4 temp_rotation;
262                 temp_rotation.makeIdentity();
263                 v3f dir;
264                 switch (axiom_char)
265                 {
266                 case 'G':
267                         dir = v3f(-1,0,0);
268                         dir = transposeMatrix(rotation,dir);
269                         position+=dir;
270                         break;
271                 case 'F':
272                         make_tree_trunk_placement(vmanip,v3f(p0.X+position.X,p0.Y+position.Y,p0.Z+position.Z),tree_definition);
273                         if (tree_definition.thin_trunks == false)
274                         {
275                                 make_tree_trunk_placement(vmanip,v3f(p0.X+position.X+1,p0.Y+position.Y,p0.Z+position.Z),tree_definition);
276                                 make_tree_trunk_placement(vmanip,v3f(p0.X+position.X-1,p0.Y+position.Y,p0.Z+position.Z),tree_definition);
277                                 make_tree_trunk_placement(vmanip,v3f(p0.X+position.X,p0.Y+position.Y,p0.Z+position.Z+1),tree_definition);
278                                 make_tree_trunk_placement(vmanip,v3f(p0.X+position.X,p0.Y+position.Y,p0.Z+position.Z-1),tree_definition);
279                         }
280                         if (stack_orientation.empty() == false)
281                         {
282                                 s16 size = 1;
283                                 for(x=-size; x<size+1; x++)
284                                         for(y=-size; y<size+1; y++)
285                                                 for(z=-size; z<size+1; z++)
286                                                         if (abs(x) == size && abs(y) == size && abs(z) == size)
287                                                         {
288                                                                 make_tree_leaves_placement(vmanip,v3f(p0.X+position.X+x+1,p0.Y+position.Y+y,p0.Z+position.Z+z),tree_definition);
289                                                                 make_tree_leaves_placement(vmanip,v3f(p0.X+position.X+x-1,p0.Y+position.Y+y,p0.Z+position.Z+z),tree_definition);
290                                                                 make_tree_leaves_placement(vmanip,v3f(p0.X+position.X+x,p0.Y+position.Y+y,p0.Z+position.Z+z+1),tree_definition);
291                                                                 make_tree_leaves_placement(vmanip,v3f(p0.X+position.X+x,p0.Y+position.Y+y,p0.Z+position.Z+z-1),tree_definition);
292                                                         }
293                         }
294                         dir = v3f(1,0,0);
295                         dir = transposeMatrix(rotation,dir);
296                         position+=dir;
297                         break;
298                 // turtle commands
299                 case '[':
300                         stack_orientation.push(rotation);
301                         stack_position.push(position);
302                         break;
303                 case ']':
304                         rotation=stack_orientation.top();
305                         stack_orientation.pop();
306                         position=stack_position.top();
307                         stack_position.pop();
308                         break;
309                 case '+':
310                         temp_rotation.makeIdentity();
311                         temp_rotation=setRotationAxisRadians(temp_rotation, angle_in_radians+angleOffset_in_radians,v3f(0,0,1));
312                         rotation*=temp_rotation;
313                         break;
314                 case '-':
315                         temp_rotation.makeIdentity();
316                         temp_rotation=setRotationAxisRadians(temp_rotation, angle_in_radians+angleOffset_in_radians,v3f(0,0,-1));
317                         rotation*=temp_rotation;
318                         break;
319                 case '&':
320                         temp_rotation.makeIdentity();
321                         temp_rotation=setRotationAxisRadians(temp_rotation, angle_in_radians+angleOffset_in_radians,v3f(0,1,0));
322                         rotation*=temp_rotation;
323                         break;
324                 case '^':
325                         temp_rotation.makeIdentity();
326                         temp_rotation=setRotationAxisRadians(temp_rotation, angle_in_radians+angleOffset_in_radians,v3f(0,-1,0));
327                         rotation*=temp_rotation;
328                         break;
329                 case '*':
330                         temp_rotation.makeIdentity();
331                         temp_rotation=setRotationAxisRadians(temp_rotation, angle_in_radians,v3f(1,0,0));
332                         rotation*=temp_rotation;
333                         break;
334                 case '/':
335                         temp_rotation.makeIdentity();
336                         temp_rotation=setRotationAxisRadians(temp_rotation, angle_in_radians,v3f(-1,0,0));
337                         rotation*=temp_rotation;
338                         break;
339                 default:
340                         break;
341                 }
342         }
343 }
344
345 void make_tree_node_placement(ManualMapVoxelManipulator &vmanip, v3f p0,
346                 MapNode node)
347 {
348         v3s16 p1 = v3s16(myround(p0.X),myround(p0.Y),myround(p0.Z));
349         if(vmanip.m_area.contains(p1) == false)
350                 return;
351         u32 vi = vmanip.m_area.index(p1);
352         if(vmanip.m_data[vi].getContent() != CONTENT_AIR
353                         && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
354                 return;
355         vmanip.m_data[vmanip.m_area.index(p1)] = node;
356 }
357
358 void make_tree_trunk_placement(ManualMapVoxelManipulator &vmanip, v3f p0,
359                 TreeDef &tree_definition)
360 {
361         v3s16 p1 = v3s16(myround(p0.X),myround(p0.Y),myround(p0.Z));
362         if(vmanip.m_area.contains(p1) == false)
363                 return;
364         u32 vi = vmanip.m_area.index(p1);
365         if(vmanip.m_data[vi].getContent() != CONTENT_AIR
366                         && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
367                 return;
368         vmanip.m_data[vmanip.m_area.index(p1)] = tree_definition.trunknode;
369 }
370
371 void make_tree_leaves_placement(ManualMapVoxelManipulator &vmanip, v3f p0,
372                 TreeDef &tree_definition)
373 {
374         v3s16 p1 = v3s16(myround(p0.X),myround(p0.Y),myround(p0.Z));
375         if(vmanip.m_area.contains(p1) == false)
376                 return;
377         u32 vi = vmanip.m_area.index(p1);
378         if(vmanip.m_data[vi].getContent() != CONTENT_AIR
379                         && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
380                 return;
381         if (tree_definition.fruit_tree)
382         {
383                 if (myrand_range(1,100) > 90+tree_definition.iterations)
384                         vmanip.m_data[vmanip.m_area.index(p1)] = tree_definition.fruitnode;
385                 else
386                         vmanip.m_data[vmanip.m_area.index(p1)] = tree_definition.leavesnode;
387         }
388         else if (myrand_range(1,100) > 20)
389                 vmanip.m_data[vmanip.m_area.index(p1)] = tree_definition.leavesnode;
390 }
391
392 irr::core::matrix4 setRotationAxisRadians(irr::core::matrix4 M, double angle, v3f axis)
393 {
394         double c = cos(angle);
395         double s = sin(angle);
396         double t = 1.0 - c;
397
398         double tx  = t * axis.X;
399         double ty  = t * axis.Y;
400         double tz  = t * axis.Z;
401         double sx  = s * axis.X;
402         double sy  = s * axis.Y;
403         double sz  = s * axis.Z;
404
405         M[0] = tx * axis.X + c;
406         M[1] = tx * axis.Y + sz;
407         M[2] = tx * axis.Z - sy;
408
409         M[4] = ty * axis.X - sz;
410         M[5] = ty * axis.Y + c;
411         M[6] = ty * axis.Z + sx;
412
413         M[8]  = tz * axis.X + sy;
414         M[9]  = tz * axis.Y - sx;
415         M[10] = tz * axis.Z + c;
416         return M;
417 }
418
419 v3f transposeMatrix(irr::core::matrix4 M, v3f v)
420 {
421         v3f translated;
422         double x = M[0] * v.X + M[4] * v.Y + M[8]  * v.Z +M[12];
423         double y = M[1] * v.X + M[5] * v.Y + M[9]  * v.Z +M[13];
424         double z = M[2] * v.X + M[6] * v.Y + M[10] * v.Z +M[14];
425         translated.X=x;
426         translated.Y=y;
427         translated.Z=z;
428         return translated;
429 }
430
431 #if 0
432 static void make_jungletree(VoxelManipulator &vmanip, v3s16 p0,
433                 INodeDefManager *ndef)
434 {
435         MapNode treenode(ndef->getId("mapgen_jungletree"));
436         MapNode leavesnode(ndef->getId("mapgen_leaves"));
437
438         for(s16 x=-1; x<=1; x++)
439         for(s16 z=-1; z<=1; z++)
440         {
441                 if(myrand_range(0, 2) == 0)
442                         continue;
443                 v3s16 p1 = p0 + v3s16(x,0,z);
444                 v3s16 p2 = p0 + v3s16(x,-1,z);
445                 if(vmanip.m_area.contains(p2)
446                                 && vmanip.m_data[vmanip.m_area.index(p2)] == CONTENT_AIR)
447                         vmanip.m_data[vmanip.m_area.index(p2)] = treenode;
448                 else if(vmanip.m_area.contains(p1))
449                         vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
450         }
451
452         s16 trunk_h = myrand_range(8, 12);
453         v3s16 p1 = p0;
454         for(s16 ii=0; ii<trunk_h; ii++)
455         {
456                 if(vmanip.m_area.contains(p1))
457                         vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
458                 p1.Y++;
459         }
460
461         // p1 is now the last piece of the trunk
462         p1.Y -= 1;
463
464         VoxelArea leaves_a(v3s16(-3,-2,-3), v3s16(3,2,3));
465         //SharedPtr<u8> leaves_d(new u8[leaves_a.getVolume()]);
466         Buffer<u8> leaves_d(leaves_a.getVolume());
467         for(s32 i=0; i<leaves_a.getVolume(); i++)
468                 leaves_d[i] = 0;
469
470         // Force leaves at near the end of the trunk
471         {
472                 s16 d = 1;
473                 for(s16 z=-d; z<=d; z++)
474                 for(s16 y=-d; y<=d; y++)
475                 for(s16 x=-d; x<=d; x++)
476                 {
477                         leaves_d[leaves_a.index(v3s16(x,y,z))] = 1;
478                 }
479         }
480
481         // Add leaves randomly
482         for(u32 iii=0; iii<30; iii++)
483         {
484                 s16 d = 1;
485
486                 v3s16 p(
487                         myrand_range(leaves_a.MinEdge.X, leaves_a.MaxEdge.X-d),
488                         myrand_range(leaves_a.MinEdge.Y, leaves_a.MaxEdge.Y-d),
489                         myrand_range(leaves_a.MinEdge.Z, leaves_a.MaxEdge.Z-d)
490                 );
491
492                 for(s16 z=0; z<=d; z++)
493                 for(s16 y=0; y<=d; y++)
494                 for(s16 x=0; x<=d; x++)
495                 {
496                         leaves_d[leaves_a.index(p+v3s16(x,y,z))] = 1;
497                 }
498         }
499
500         // Blit leaves to vmanip
501         for(s16 z=leaves_a.MinEdge.Z; z<=leaves_a.MaxEdge.Z; z++)
502         for(s16 y=leaves_a.MinEdge.Y; y<=leaves_a.MaxEdge.Y; y++)
503         for(s16 x=leaves_a.MinEdge.X; x<=leaves_a.MaxEdge.X; x++)
504         {
505                 v3s16 p(x,y,z);
506                 p += p1;
507                 if(vmanip.m_area.contains(p) == false)
508                         continue;
509                 u32 vi = vmanip.m_area.index(p);
510                 if(vmanip.m_data[vi].getContent() != CONTENT_AIR
511                                 && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
512                         continue;
513                 u32 i = leaves_a.index(x,y,z);
514                 if(leaves_d[i] == 1)
515                         vmanip.m_data[vi] = leavesnode;
516         }
517 }
518 #endif
519
520 }; // namespace treegen