blockgame/mods/bg_tree/grow_leaves.lua
trans_soup 5d98c3dadc add growing_leaves node.
create node representing growing leaves, and use it where previously
living leaves was used. this allows for a distinction between leaves
that will try to create more leaves, leaves that can provide nutrients
to other leaves, and leaves that are dead, whereas previously the first
2 of those couldn't be distinguished.
2023-10-17 10:26:15 +02:00

36 lines
1.1 KiB
Lua

local modname = minetest.get_current_modname()
-- NODE NAMES
local log_alive = modname .. ":log_alive"
local leaves = modname .. ":leaves"
local leaves_growing = modname .. ":leaves_growing"
local leaves_alive = modname .. ":leaves_alive"
-- END OF NODE NAMES
local max_grow_distance = 2
-- TODO: turn this into an increasing ABM.
blockgame.register_abm({
label = "grow leaves",
nodenames = {leaves_growing},
neighbors = {log_alive, leaves_growing, leaves_alive},
interval = 15,
chance = 8,
action = function (pos, node)
-- TODO: give leaves energy over time, that they spend when creating new leaves.
local meta = minetest.get_meta(pos)
local distance = meta:get_int("leaf_distance") or 1
if distance >= max_grow_distance then return end
local neighbors = blockgame.get_neighbors(pos)
for _, target in pairs(neighbors) do
if blockgame.chance(2) then
blockgame.attempt_place(target, {name = leaves_growing})
minetest.get_meta(target):set_int("leaf_distance", distance + 1)
end
end
minetest.set_node(pos, {name = leaves_alive})
end,
})