blockgame/mods/bg_tree/grow_leaves.lua
trans_soup 717cbf91e2 fix leaves growth error.
make growing leaves turn into alive leaves if they should do so.
previously, this didn't happen for leaves that exceeded their max
distance from supporting node.
2023-10-19 15:39:04 +02:00

49 lines
1.5 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"
local max_grow_distance = 2
local leaves_grow_cost = 250
blockgame.register_increasing_abm({
id = modname .. ":grow_leaves",
label = "grow leaves",
nodenames = {leaves_growing},
neighbors = {log_alive, leaves_growing, leaves_alive},
interval = 15,
chance = 8,
rate = function (pos, node, data)
-- TODO: account for nearby nodes? e.g. grow slower if there are more leaves nearby.
return data.value + math.random(1, 99)
end,
check = function (pos, node, data)
return data.value >= leaves_grow_cost
end,
action = function (pos, node, data)
-- TODO: give leaves energy over time, that they spend when creating new leaves.
-- this requires the API to add support for increasing ABM `action`:s to modify `data`.
local meta = minetest.get_meta(pos)
local distance = meta:get_int("leaf_distance") or 1
if distance >= max_grow_distance then
minetest.set_node(pos, {name = leaves_alive})
return
end
local sides = blockgame.vector.get_sides_of(pos)
for _, target in pairs(sides) do
if blockgame.chance(2) then
blockgame.attempt_place(target, {name = leaves_growing})
-- TODO: find lowest possible leaf distance by checking all 4 sides?
minetest.get_meta(target):set_int("leaf_distance", distance + 1)
end
end
minetest.set_node(pos, {name = leaves_alive})
end,
})