From f9b90f74f927b531f0ef225e07da8e95e7774478 Mon Sep 17 00:00:00 2001 From: crazyginger72 Date: Sat, 23 Aug 2014 18:08:15 -0400 Subject: [PATCH] cleaned up treegen code cleaned up treegen --- mods/default/functions.lua | 4 +- mods/default/trees.lua | 173 +++++++++++-------------------------- 2 files changed, 52 insertions(+), 125 deletions(-) diff --git a/mods/default/functions.lua b/mods/default/functions.lua index 6840c5d2..b21210c9 100644 --- a/mods/default/functions.lua +++ b/mods/default/functions.lua @@ -141,7 +141,7 @@ minetest.register_abm({ if is_soil == 0 then return end - + minetest.remove_node(pos) minetest.log("action", "A sapling grows into a tree at "..minetest.pos_to_string(pos)) 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}) @@ -164,7 +164,7 @@ minetest.register_abm({ if is_soil == 0 then return end - + minetest.remove_node(pos) minetest.log("action", "A jungle sapling grows into a tree at "..minetest.pos_to_string(pos)) 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}) diff --git a/mods/default/trees.lua b/mods/default/trees.lua index e68c0554..41b0c0f3 100644 --- a/mods/default/trees.lua +++ b/mods/default/trees.lua @@ -10,64 +10,29 @@ function default.grow_tree(data, a, pos, is_apple_tree, seed) and in games that have saplings; both are deprecated but not replaced yet ]]-- - local pr = PseudoRandom(seed) - local th = pr:next(4, 5) - local x, y, z = pos.x, pos.y, pos.z - for yy = y, y+th-1 do - local vi = a:index(x, yy, z) - if a:contains(x, yy, z) and (data[vi] == c_air or yy == y) then - data[vi] = c_tree + local hight = math.random(4, 5) + for x_area = -2, 2 do + for y_area = -1, 2 do + for z_area = -2, 2 do + if math.random(1,30) < 23 then --randomize leaves + local area_l = a:index(pos.x+x_area, pos.y+hight+y_area-1, pos.z+z_area) --sets area for leaves + if data[area_l] == c_air or data[area_l] == c_ignore then --sets if not air or ignore + if is_apple_tree == true and math.random(1, 100) <= 10 then --randomize apples + data[area_l] = c_apple --add apples now + else + data[area_l] = c_leaves --add leaves now 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 - 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 - data[vi] = c_leaves - end - end - end - end - end - end - end + end end local c_jungletree = minetest.get_content_id("default:jungletree") @@ -79,72 +44,34 @@ function default.grow_jungletree(data, a, pos, seed) and in games that have saplings; both are deprecated but not replaced yet ]]-- - local pr = PseudoRandom(seed) - local x, y, z = pos.x, pos.y, pos.z - for xi = -1, 1 do - for zi = -1, 1 do - if pr:next(1, 3) >= 2 then - local vi1 = a:index(x+xi, y, z+zi) - local vi2 = a:index(x+xi, y-1, z+zi) - if a:contains(x+xi, y-1, z+zi) and data[vi2] == c_air then - data[vi2] = c_jungletree - elseif a:contains(x+xi, y, z+zi) and data[vi1] == c_air then - data[vi1] = c_jungletree - end - end + local hight = math.random(8, 12) + for x_area = -3, 3 do + for y_area = -2, 2 do + for z_area = -3, 3 do + if math.random(1,30) < 23 then --randomize leaves + local area_l = a:index(pos.x+x_area, pos.y+hight+y_area-1, pos.z+z_area) --sets area for leaves + if data[area_l] == c_air or data[area_l] == c_ignore then --sets if not air or ignore + data[area_l] = c_jungleleaves --add leaves now + 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_jungleleaves then --sets if air + data[area_t] = c_jungletree --add tree now 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 - - local th = pr:next(8, 12) - for yy = y, y+th-1 do - local vi = a:index(x, yy, z) - if a:contains(x, yy, z) and (data[vi] == c_air or yy == y) then - 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 - end - end -end + end + end +end \ No newline at end of file