2023-10-16 17:18:29 +00:00
|
|
|
local modname = minetest.get_current_modname()
|
|
|
|
|
|
|
|
-- NODE NAMES
|
|
|
|
|
|
|
|
local log_alive = modname .. ":log_alive"
|
|
|
|
local leaves = modname .. ":leaves"
|
2023-10-17 08:23:53 +00:00
|
|
|
local leaves_growing = modname .. ":leaves_growing"
|
2023-10-16 17:18:29 +00:00
|
|
|
local leaves_alive = modname .. ":leaves_alive"
|
|
|
|
|
2023-10-19 13:23:07 +00:00
|
|
|
|
2023-10-16 17:18:29 +00:00
|
|
|
|
|
|
|
local max_grow_distance = 2
|
2023-10-19 13:23:07 +00:00
|
|
|
local leaves_grow_cost = 250
|
2023-10-16 17:18:29 +00:00
|
|
|
|
2023-10-19 13:23:07 +00:00
|
|
|
blockgame.register_increasing_abm({
|
|
|
|
id = modname .. ":grow_leaves",
|
2023-10-16 17:18:29 +00:00
|
|
|
label = "grow leaves",
|
2023-10-17 08:23:53 +00:00
|
|
|
nodenames = {leaves_growing},
|
|
|
|
neighbors = {log_alive, leaves_growing, leaves_alive},
|
2023-10-16 17:18:29 +00:00
|
|
|
interval = 15,
|
|
|
|
chance = 8,
|
2023-10-19 13:23:07 +00:00
|
|
|
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)
|
2023-10-16 17:18:29 +00:00
|
|
|
-- TODO: give leaves energy over time, that they spend when creating new leaves.
|
2023-10-19 13:23:07 +00:00
|
|
|
-- this requires the API to add support for increasing ABM `action`:s to modify `data`.
|
2023-10-16 17:18:29 +00:00
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local distance = meta:get_int("leaf_distance") or 1
|
2023-10-19 13:39:04 +00:00
|
|
|
if distance >= max_grow_distance then
|
|
|
|
minetest.set_node(pos, {name = leaves_alive})
|
|
|
|
return
|
|
|
|
end
|
2023-10-16 17:18:29 +00:00
|
|
|
|
2023-10-19 13:12:36 +00:00
|
|
|
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
|
2023-10-17 08:23:53 +00:00
|
|
|
blockgame.attempt_place(target, {name = leaves_growing})
|
2023-10-19 13:23:07 +00:00
|
|
|
-- TODO: find lowest possible leaf distance by checking all 4 sides?
|
2023-10-16 17:18:29 +00:00
|
|
|
minetest.get_meta(target):set_int("leaf_distance", distance + 1)
|
|
|
|
end
|
|
|
|
end
|
2023-10-17 08:23:53 +00:00
|
|
|
minetest.set_node(pos, {name = leaves_alive})
|
2023-10-16 17:18:29 +00:00
|
|
|
end,
|
|
|
|
})
|