Fix tree growing and jungle sapling growth rate
authorBlockMen <nmuelll@web.de>
Wed, 6 Nov 2013 22:41:49 +0000 (23:41 +0100)
committerBlockMen <nmuelll@web.de>
Wed, 6 Nov 2013 23:13:51 +0000 (00:13 +0100)
mods/default/functions.lua
mods/default/init.lua
mods/default/nodes.lua
mods/default/trees.lua [new file with mode: 0644]

index e62b3d6bacfff27c39b839d10f85f6522ef676ea..7f4492cbc58e2f14999a460bc322de1c7282fe67 100644 (file)
@@ -126,6 +126,49 @@ function on_punchnode(p, node)
 end
 minetest.register_on_punchnode(on_punchnode)
 
+
+--
+-- Grow trees
+--
+
+minetest.register_abm({
+        nodenames = {"default:sapling"},
+        interval = 10,
+        chance = 50,
+        action = function(pos, node)
+                local is_soil = minetest.registered_nodes[minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name].groups.soil
+                if is_soil == nil or is_soil == 0 then return end
+                print("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})
+                local a = VoxelArea:new{MinEdge=minp, MaxEdge=maxp}
+                local data = vm:get_data()
+                default.grow_tree(data, a, pos, math.random(1, 4) == 1, math.random(1,100000))
+                vm:set_data(data)
+                vm:write_to_map(data)
+                vm:update_map()
+        end
+})
+
+minetest.register_abm({
+        nodenames = {"default:junglesapling"},
+        interval = 10,
+        chance = 50,
+        action = function(pos, node)
+                local is_soil = minetest.registered_nodes[minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name].groups.soil
+                if is_soil == nil or is_soil == 0 then return end
+                print("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})
+                local a = VoxelArea:new{MinEdge=minp, MaxEdge=maxp}
+                local data = vm:get_data()
+                default.grow_jungletree(data, a, pos, math.random(1,100000))
+                vm:set_data(data)
+                vm:write_to_map(data)
+                vm:update_map()
+        end
+})
+
 --
 -- Lavacooling
 --
index 61d5e1f332284ed78d126b59d9ee903f18cca8a8..968cacd4c0b783cd27723286d1c94a7050feb504 100644 (file)
@@ -19,3 +19,4 @@ dofile(minetest.get_modpath("default").."/craftitems.lua")
 dofile(minetest.get_modpath("default").."/crafting.lua")
 dofile(minetest.get_modpath("default").."/mapgen.lua")
 dofile(minetest.get_modpath("default").."/player.lua")
+dofile(minetest.get_modpath("default").."/trees.lua")
index 4698c877954ee0a7ff0408dfcc2367598cf1c827..7ec97b73d999c44c1c2518b68970be447d0fa377 100644 (file)
@@ -287,193 +287,6 @@ minetest.register_node("default:junglesapling", {
        sounds = default.node_sound_leaves_defaults(),
 })
 
-local c_air = minetest.get_content_id("air")
-local c_ignore = minetest.get_content_id("ignore")
-local c_tree = minetest.get_content_id("default:tree")
-local c_leaves = minetest.get_content_id("default:leaves")
-local c_apple = minetest.get_content_id("default:apple")
-function default.grow_tree(data, a, pos, is_apple_tree, seed)
-       --[[
-               NOTE: Tree-placing code is currently duplicated in the engine
-               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
-               end
-       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
-
-minetest.register_abm({
-       nodenames = {"default:sapling"},
-       interval = 10,
-       chance = 50,
-       action = function(pos, node)
-               local is_soil = minetest.registered_nodes[minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name].groups.soil
-               if is_soil == nil or is_soil == 0 then return end
-               print("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})
-               local a = VoxelArea:new{MinEdge=minp, MaxEdge=maxp}
-               local data = vm:get_data()
-               default.grow_tree(data, a, pos, math.random(1, 4) == 1, math.random(1,100000))
-               vm:set_data(data)
-               vm:write_to_map(data)
-               vm:update_map()
-       end
-})
-
-local c_jungletree = minetest.get_content_id("default:jungletree")
-local c_jungleleaves = minetest.get_content_id("default:jungleleaves")
-function default.grow_jungletree(data, a, pos, seed)
-       --[[
-               NOTE: Tree-placing code is currently duplicated in the engine
-               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
-       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
-
-minetest.register_abm({
-       nodenames = {"default:junglesapling"},
-       interval = 1,
-       chance = 1,
-       action = function(pos, node)
-               local is_soil = minetest.registered_nodes[minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name].groups.soil
-               if is_soil == nil or is_soil == 0 then return end
-               print("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})
-               local a = VoxelArea:new{MinEdge=minp, MaxEdge=maxp}
-               local data = vm:get_data()
-               default.grow_jungletree(data, a, pos, math.random(1,100000))
-               vm:set_data(data)
-               vm:write_to_map(data)
-               vm:update_map()
-       end
-})
-
 minetest.register_node("default:junglegrass", {
        description = "Jungle Grass",
        drawtype = "plantlike",
diff --git a/mods/default/trees.lua b/mods/default/trees.lua
new file mode 100644 (file)
index 0000000..e68c055
--- /dev/null
@@ -0,0 +1,150 @@
+local c_air = minetest.get_content_id("air")
+local c_ignore = minetest.get_content_id("ignore")
+local c_tree = minetest.get_content_id("default:tree")
+local c_leaves = minetest.get_content_id("default:leaves")
+local c_apple = minetest.get_content_id("default:apple")
+
+function default.grow_tree(data, a, pos, is_apple_tree, seed)
+        --[[
+                NOTE: Tree-placing code is currently duplicated in the engine
+                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
+                end
+        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
+
+local c_jungletree = minetest.get_content_id("default:jungletree")
+local c_jungleleaves = minetest.get_content_id("default:jungleleaves")
+
+function default.grow_jungletree(data, a, pos, seed)
+        --[[
+                NOTE: Tree-placing code is currently duplicated in the engine
+                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
+        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