mirror of
https://github.com/luanti-org/minetest_game.git
synced 2025-05-21 14:53:16 -04:00
cleaned up treegen code
cleaned up treegen
This commit is contained in:
parent
11c04e984d
commit
f9b90f74f9
2 changed files with 52 additions and 125 deletions
|
@ -141,7 +141,7 @@ minetest.register_abm({
|
||||||
if is_soil == 0 then
|
if is_soil == 0 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
minetest.remove_node(pos)
|
||||||
minetest.log("action", "A sapling grows into a tree at "..minetest.pos_to_string(pos))
|
minetest.log("action", "A sapling grows into a tree at "..minetest.pos_to_string(pos))
|
||||||
local vm = minetest.get_voxel_manip()
|
local vm = minetest.get_voxel_manip()
|
||||||
local minp, maxp = vm:read_from_map({x=pos.x-16, y=pos.y, z=pos.z-16}, {x=pos.x+16, y=pos.y+16, z=pos.z+16})
|
local minp, maxp = vm:read_from_map({x=pos.x-16, y=pos.y, z=pos.z-16}, {x=pos.x+16, y=pos.y+16, z=pos.z+16})
|
||||||
|
@ -164,7 +164,7 @@ minetest.register_abm({
|
||||||
if is_soil == 0 then
|
if is_soil == 0 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
minetest.remove_node(pos)
|
||||||
minetest.log("action", "A jungle sapling grows into a tree at "..minetest.pos_to_string(pos))
|
minetest.log("action", "A jungle sapling grows into a tree at "..minetest.pos_to_string(pos))
|
||||||
local vm = minetest.get_voxel_manip()
|
local vm = minetest.get_voxel_manip()
|
||||||
local minp, maxp = vm:read_from_map({x=pos.x-16, y=pos.y-1, z=pos.z-16}, {x=pos.x+16, y=pos.y+16, z=pos.z+16})
|
local minp, maxp = vm:read_from_map({x=pos.x-16, y=pos.y-1, z=pos.z-16}, {x=pos.x+16, y=pos.y+16, z=pos.z+16})
|
||||||
|
|
|
@ -10,63 +10,28 @@ function default.grow_tree(data, a, pos, is_apple_tree, seed)
|
||||||
and in games that have saplings; both are deprecated but not
|
and in games that have saplings; both are deprecated but not
|
||||||
replaced yet
|
replaced yet
|
||||||
]]--
|
]]--
|
||||||
local pr = PseudoRandom(seed)
|
local hight = math.random(4, 5)
|
||||||
local th = pr:next(4, 5)
|
for x_area = -2, 2 do
|
||||||
local x, y, z = pos.x, pos.y, pos.z
|
for y_area = -1, 2 do
|
||||||
for yy = y, y+th-1 do
|
for z_area = -2, 2 do
|
||||||
local vi = a:index(x, yy, z)
|
if math.random(1,30) < 23 then --randomize leaves
|
||||||
if a:contains(x, yy, z) and (data[vi] == c_air or yy == y) then
|
local area_l = a:index(pos.x+x_area, pos.y+hight+y_area-1, pos.z+z_area) --sets area for leaves
|
||||||
data[vi] = c_tree
|
if data[area_l] == c_air or data[area_l] == c_ignore then --sets if not air or ignore
|
||||||
end
|
if is_apple_tree == true and math.random(1, 100) <= 10 then --randomize apples
|
||||||
end
|
data[area_l] = c_apple --add apples now
|
||||||
y = y+th-1 -- (x, y, z) is now last piece of trunk
|
|
||||||
local leaves_a = VoxelArea:new{MinEdge={x=-2, y=-1, z=-2}, MaxEdge={x=2, y=2, z=2}}
|
|
||||||
local leaves_buffer = {}
|
|
||||||
|
|
||||||
-- Force leaves near the trunk
|
|
||||||
local d = 1
|
|
||||||
for xi = -d, d do
|
|
||||||
for yi = -d, d do
|
|
||||||
for zi = -d, d do
|
|
||||||
leaves_buffer[leaves_a:index(xi, yi, zi)] = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Add leaves randomly
|
|
||||||
for iii = 1, 8 do
|
|
||||||
local d = 1
|
|
||||||
local xx = pr:next(leaves_a.MinEdge.x, leaves_a.MaxEdge.x - d)
|
|
||||||
local yy = pr:next(leaves_a.MinEdge.y, leaves_a.MaxEdge.y - d)
|
|
||||||
local zz = pr:next(leaves_a.MinEdge.z, leaves_a.MaxEdge.z - d)
|
|
||||||
|
|
||||||
for xi = 0, d do
|
|
||||||
for yi = 0, d do
|
|
||||||
for zi = 0, d do
|
|
||||||
leaves_buffer[leaves_a:index(xx+xi, yy+yi, zz+zi)] = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Add the leaves
|
|
||||||
for xi = leaves_a.MinEdge.x, leaves_a.MaxEdge.x do
|
|
||||||
for yi = leaves_a.MinEdge.y, leaves_a.MaxEdge.y do
|
|
||||||
for zi = leaves_a.MinEdge.z, leaves_a.MaxEdge.z do
|
|
||||||
if a:contains(x+xi, y+yi, z+zi) then
|
|
||||||
local vi = a:index(x+xi, y+yi, z+zi)
|
|
||||||
if data[vi] == c_air or data[vi] == c_ignore then
|
|
||||||
if leaves_buffer[leaves_a:index(xi, yi, zi)] then
|
|
||||||
if is_apple_tree and pr:next(1, 100) <= 10 then
|
|
||||||
data[vi] = c_apple
|
|
||||||
else
|
else
|
||||||
data[vi] = c_leaves
|
data[area_l] = c_leaves --add leaves now
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
for tree_h = 0, hight-1 do -- add the trunk
|
||||||
|
local area_t = a:index(pos.x, pos.y+tree_h, pos.z) --set area for tree
|
||||||
|
if data[area_t] == c_air or data[area_t] == c_leaves or data[area_t] == c_apple then --sets if air
|
||||||
|
data[area_t] = c_tree --add tree now
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -79,70 +44,32 @@ function default.grow_jungletree(data, a, pos, seed)
|
||||||
and in games that have saplings; both are deprecated but not
|
and in games that have saplings; both are deprecated but not
|
||||||
replaced yet
|
replaced yet
|
||||||
]]--
|
]]--
|
||||||
local pr = PseudoRandom(seed)
|
local hight = math.random(8, 12)
|
||||||
local x, y, z = pos.x, pos.y, pos.z
|
for x_area = -3, 3 do
|
||||||
for xi = -1, 1 do
|
for y_area = -2, 2 do
|
||||||
for zi = -1, 1 do
|
for z_area = -3, 3 do
|
||||||
if pr:next(1, 3) >= 2 then
|
if math.random(1,30) < 23 then --randomize leaves
|
||||||
local vi1 = a:index(x+xi, y, z+zi)
|
local area_l = a:index(pos.x+x_area, pos.y+hight+y_area-1, pos.z+z_area) --sets area for leaves
|
||||||
local vi2 = a:index(x+xi, y-1, z+zi)
|
if data[area_l] == c_air or data[area_l] == c_ignore then --sets if not air or ignore
|
||||||
if a:contains(x+xi, y-1, z+zi) and data[vi2] == c_air then
|
data[area_l] = c_jungleleaves --add leaves now
|
||||||
data[vi2] = c_jungletree
|
|
||||||
elseif a:contains(x+xi, y, z+zi) and data[vi1] == c_air then
|
|
||||||
data[vi1] = c_jungletree
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
local th = pr:next(8, 12)
|
for tree_h = 0, hight-1 do -- add the trunk
|
||||||
for yy = y, y+th-1 do
|
local area_t = a:index(pos.x, pos.y+tree_h, pos.z) --set area for tree
|
||||||
local vi = a:index(x, yy, z)
|
if data[area_t] == c_air or data[area_t] == c_jungleleaves then --sets if air
|
||||||
if a:contains(x, yy, z) and (data[vi] == c_air or yy == y) then
|
data[area_t] = c_jungletree --add tree now
|
||||||
data[vi] = c_jungletree
|
|
||||||
end
|
|
||||||
end
|
|
||||||
y = y+th-1 -- (x, y, z) is now last piece of trunk
|
|
||||||
local leaves_a = VoxelArea:new{MinEdge={x=-3, y=-2, z=-3}, MaxEdge={x=3, y=2, z=3}}
|
|
||||||
local leaves_buffer = {}
|
|
||||||
|
|
||||||
-- Force leaves near the trunk
|
|
||||||
local d = 1
|
|
||||||
for xi = -d, d do
|
|
||||||
for yi = -d, d do
|
|
||||||
for zi = -d, d do
|
|
||||||
leaves_buffer[leaves_a:index(xi, yi, zi)] = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Add leaves randomly
|
|
||||||
for iii = 1, 30 do
|
|
||||||
local d = 1
|
|
||||||
local xx = pr:next(leaves_a.MinEdge.x, leaves_a.MaxEdge.x - d)
|
|
||||||
local yy = pr:next(leaves_a.MinEdge.y, leaves_a.MaxEdge.y - d)
|
|
||||||
local zz = pr:next(leaves_a.MinEdge.z, leaves_a.MaxEdge.z - d)
|
|
||||||
|
|
||||||
for xi = 0, d do
|
|
||||||
for yi = 0, d do
|
|
||||||
for zi = 0, d do
|
|
||||||
leaves_buffer[leaves_a:index(xx+xi, yy+yi, zz+zi)] = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Add the leaves
|
|
||||||
for xi = leaves_a.MinEdge.x, leaves_a.MaxEdge.x do
|
|
||||||
for yi = leaves_a.MinEdge.y, leaves_a.MaxEdge.y do
|
|
||||||
for zi = leaves_a.MinEdge.z, leaves_a.MaxEdge.z do
|
|
||||||
if a:contains(x+xi, y+yi, z+zi) then
|
|
||||||
local vi = a:index(x+xi, y+yi, z+zi)
|
|
||||||
if data[vi] == c_air or data[vi] == c_ignore then
|
|
||||||
if leaves_buffer[leaves_a:index(xi, yi, zi)] then
|
|
||||||
data[vi] = c_jungleleaves
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
for roots_x = -1, 1 do
|
||||||
|
for roots_z = -1, 1 do
|
||||||
|
if math.random(1, 3) >= 2 then --randomize roots
|
||||||
|
if a:contains(pos.x+roots_x, pos.y-1, pos.z+roots_z) and data[a:index(pos.x+roots_x, pos.y-1, pos.z+roots_z)] == c_air then
|
||||||
|
data[a:index(pos.x+roots_x, pos.y-1, pos.z+roots_z)] = c_jungletree
|
||||||
|
elseif a:contains(pos.x+roots_x, pos.y, pos.z+roots_z) and data[a:index(pos.x+roots_x, pos.y, pos.z+roots_z)] == c_air then
|
||||||
|
data[a:index(pos.x+roots_x, pos.y, pos.z+roots_z)] = c_jungletree
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue