Import source (dev state between 1.2.0 and 1.3.0)
[minetest_tutorial_subgame.git] / mods / default / trees.lua
blobe68c05548008317ccc46b9167a0bced1517fa48c
1 local c_air = minetest.get_content_id("air")
2 local c_ignore = minetest.get_content_id("ignore")
3 local c_tree = minetest.get_content_id("default:tree")
4 local c_leaves = minetest.get_content_id("default:leaves")
5 local c_apple = minetest.get_content_id("default:apple")
7 function default.grow_tree(data, a, pos, is_apple_tree, seed)
8 --[[
9 NOTE: Tree-placing code is currently duplicated in the engine
10 and in games that have saplings; both are deprecated but not
11 replaced yet
12 ]]--
13 local pr = PseudoRandom(seed)
14 local th = pr:next(4, 5)
15 local x, y, z = pos.x, pos.y, pos.z
16 for yy = y, y+th-1 do
17 local vi = a:index(x, yy, z)
18 if a:contains(x, yy, z) and (data[vi] == c_air or yy == y) then
19 data[vi] = c_tree
20 end
21 end
22 y = y+th-1 -- (x, y, z) is now last piece of trunk
23 local leaves_a = VoxelArea:new{MinEdge={x=-2, y=-1, z=-2}, MaxEdge={x=2, y=2, z=2}}
24 local leaves_buffer = {}
26 -- Force leaves near the trunk
27 local d = 1
28 for xi = -d, d do
29 for yi = -d, d do
30 for zi = -d, d do
31 leaves_buffer[leaves_a:index(xi, yi, zi)] = true
32 end
33 end
34 end
36 -- Add leaves randomly
37 for iii = 1, 8 do
38 local d = 1
39 local xx = pr:next(leaves_a.MinEdge.x, leaves_a.MaxEdge.x - d)
40 local yy = pr:next(leaves_a.MinEdge.y, leaves_a.MaxEdge.y - d)
41 local zz = pr:next(leaves_a.MinEdge.z, leaves_a.MaxEdge.z - d)
43 for xi = 0, d do
44 for yi = 0, d do
45 for zi = 0, d do
46 leaves_buffer[leaves_a:index(xx+xi, yy+yi, zz+zi)] = true
47 end
48 end
49 end
50 end
52 -- Add the leaves
53 for xi = leaves_a.MinEdge.x, leaves_a.MaxEdge.x do
54 for yi = leaves_a.MinEdge.y, leaves_a.MaxEdge.y do
55 for zi = leaves_a.MinEdge.z, leaves_a.MaxEdge.z do
56 if a:contains(x+xi, y+yi, z+zi) then
57 local vi = a:index(x+xi, y+yi, z+zi)
58 if data[vi] == c_air or data[vi] == c_ignore then
59 if leaves_buffer[leaves_a:index(xi, yi, zi)] then
60 if is_apple_tree and pr:next(1, 100) <= 10 then
61 data[vi] = c_apple
62 else
63 data[vi] = c_leaves
64 end
65 end
66 end
67 end
68 end
69 end
70 end
71 end
73 local c_jungletree = minetest.get_content_id("default:jungletree")
74 local c_jungleleaves = minetest.get_content_id("default:jungleleaves")
76 function default.grow_jungletree(data, a, pos, seed)
77 --[[
78 NOTE: Tree-placing code is currently duplicated in the engine
79 and in games that have saplings; both are deprecated but not
80 replaced yet
81 ]]--
82 local pr = PseudoRandom(seed)
83 local x, y, z = pos.x, pos.y, pos.z
84 for xi = -1, 1 do
85 for zi = -1, 1 do
86 if pr:next(1, 3) >= 2 then
87 local vi1 = a:index(x+xi, y, z+zi)
88 local vi2 = a:index(x+xi, y-1, z+zi)
89 if a:contains(x+xi, y-1, z+zi) and data[vi2] == c_air then
90 data[vi2] = c_jungletree
91 elseif a:contains(x+xi, y, z+zi) and data[vi1] == c_air then
92 data[vi1] = c_jungletree
93 end
94 end
95 end
96 end
98 local th = pr:next(8, 12)
99 for yy = y, y+th-1 do
100 local vi = a:index(x, yy, z)
101 if a:contains(x, yy, z) and (data[vi] == c_air or yy == y) then
102 data[vi] = c_jungletree
105 y = y+th-1 -- (x, y, z) is now last piece of trunk
106 local leaves_a = VoxelArea:new{MinEdge={x=-3, y=-2, z=-3}, MaxEdge={x=3, y=2, z=3}}
107 local leaves_buffer = {}
109 -- Force leaves near the trunk
110 local d = 1
111 for xi = -d, d do
112 for yi = -d, d do
113 for zi = -d, d do
114 leaves_buffer[leaves_a:index(xi, yi, zi)] = true
119 -- Add leaves randomly
120 for iii = 1, 30 do
121 local d = 1
122 local xx = pr:next(leaves_a.MinEdge.x, leaves_a.MaxEdge.x - d)
123 local yy = pr:next(leaves_a.MinEdge.y, leaves_a.MaxEdge.y - d)
124 local zz = pr:next(leaves_a.MinEdge.z, leaves_a.MaxEdge.z - d)
126 for xi = 0, d do
127 for yi = 0, d do
128 for zi = 0, d do
129 leaves_buffer[leaves_a:index(xx+xi, yy+yi, zz+zi)] = true
135 -- Add the leaves
136 for xi = leaves_a.MinEdge.x, leaves_a.MaxEdge.x do
137 for yi = leaves_a.MinEdge.y, leaves_a.MaxEdge.y do
138 for zi = leaves_a.MinEdge.z, leaves_a.MaxEdge.z do
139 if a:contains(x+xi, y+yi, z+zi) then
140 local vi = a:index(x+xi, y+yi, z+zi)
141 if data[vi] == c_air or data[vi] == c_ignore then
142 if leaves_buffer[leaves_a:index(xi, yi, zi)] then
143 data[vi] = c_jungleleaves