If a tree node is removed I trigger all leave nodes in a radius around the tree node to search for a tree node. If there is no tree node near the leave I start a node timer with a random time
The advantage is, that I can only trigger the matching leaves to the tree, so I don't remove other leaves. Works pretty well, except that apples don't drop until now. And we save a lot of computation time I think because this only cost time if a tree node is removed
Here is a demonstration video
https://youtu.be/VaK0cyDRaO8
This code is just a proof of concept, but works as expected for default trees
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
minetest.register_node(":default:tree", {
description = "Tree",
tiles = {"default_tree_top.png", "default_tree_top.png", "default_tree.png"},
paramtype2 = "facedir",
is_ground_content = false,
groups = {tree = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2},
sounds = default.node_sound_wood_defaults(),
on_place = minetest.rotate_node,
after_destruct = function(pos, oldnode)
local radius = 3
local lradius = 2
local minp = vector.subtract(pos, radius)
local maxp = vector.add(pos, radius)
local leaves_near, leaves_count = minetest.find_nodes_in_area(minp, maxp, "default:leaves")
for i = 1, leaves_count["default:leaves"] do
local leaves_at_pos = leaves_near[i]
local trunks_near = minetest.find_node_near(leaves_at_pos, lradius, "default:tree")
if not trunks_near then
minetest.get_node_timer(leaves_at_pos):start(math.random() * 10.0)
end
end
end,
})
minetest.register_node(":default:leaves", {
description = "Leaves",
drawtype = "allfaces_optional",
waving = 1,
tiles = {"default_leaves.png"},
special_tiles = {"default_leaves_simple.png"},
paramtype = "light",
is_ground_content = false,
groups = {snappy = 3, leafdecay = 3, flammable = 2, leaves = 1},
drop = {
max_items = 1,
items = {
{
-- player will get sapling with 1/20 chance
items = {'default:sapling'},
rarity = 20,
},
{
-- player will get leaves only if he get no saplings,
-- this is because max_items is 1
items = {'default:leaves'},
}
}
},
sounds = default.node_sound_leaves_defaults(),
on_timer = function(pos, elapsed)
-- Drop stuff
local itemstacks = minetest.get_node_drops("default:leaves")
for _, itemname in ipairs(itemstacks) do
if itemname ~= "default:leaves" or
minetest.get_item_group("default:leaves", "leafdecay_drop") ~= 0 then
local p_drop = {
x = pos.x - 0.5 + math.random(),
y = pos.y - 0.5 + math.random(),
z = pos.z - 0.5 + math.random(),
}
minetest.add_item(p_drop, itemname)
end
end
-- Remove node
minetest.remove_node(pos)
minetest.check_for_falling(pos)
end,
after_place_node = tree.after_place_leaves,
})