blockgame/mods/bg_tree/grow_leaves.lua

50 lines
1.5 KiB
Lua
Raw Normal View History

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
2023-10-17 08:19:27 +00:00
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,
})