electric/mods/ebiometrees/sapling.lua

87 lines
2.6 KiB
Lua
Raw Normal View History

function ebiometrees.grow_closure(nextstage)
local function growtree(pos)
minetest.set_node(pos, {name=nextstage})
end
return growtree
end
2023-05-10 23:39:08 +00:00
2023-03-19 21:47:19 +00:00
function ebiometrees.where_to_grow_trunk_closure(grow_preferences, leaves_name)
local function where_to_grow_trunk(pos)
for _, directions in ipairs(grow_preferences)
do
table.shuffle(directions)
local candidate_dir = directions[1]
local candidate = vector.copy(pos):add(candidate_dir)
local node = minetest.get_node_or_nil(candidate)
2023-05-10 23:39:08 +00:00
corner1 = pos:add({x=-1, y=-1, z=-1}):add(candidate_dir:multiply(2))
corner2 = pos:add({x= 1, y= 1, z= 1}):add(candidate_dir:multiply(2))
local spacious, _ = minetest.find_nodes_in_area(
corner1, corner2, "group:treetrunk"
)
minetest.chat_send_all(dump2(spacious))
minetest.chat_send_all(#spacious)
if node and node.name == leaves_name and #spacious == 0
2023-03-19 21:47:19 +00:00
then
return candidate
end
end
end
return where_to_grow_trunk
end
2023-05-10 23:39:08 +00:00
function ebiometrees.grow_trunk_closure(trunk_terminal_name, grow_preferences, leaves_name, grow_limit)
2023-03-19 21:47:19 +00:00
local function grow_trunk(pos)
2023-05-10 23:39:08 +00:00
local meta = minetest.get_meta(pos)
if meta:get_int("ebiometrees:distance") > grow_limit
then
return
end
local where = ebiometrees.where_to_grow_trunk_closure(grow_preferences, leaves_name)
2023-03-19 21:47:19 +00:00
local there = where(pos)
if there
then
minetest.set_node(there, {name=trunk_terminal_name})
2023-05-10 23:39:08 +00:00
local newmeta = minetest.get_meta(there)
newmeta:set_int("ebiometrees:distance", meta:get_int("ebiometrees:distance")+1)
2023-03-19 21:47:19 +00:00
end
end
return grow_trunk
end
2023-05-10 23:39:08 +00:00
2023-03-19 21:47:19 +00:00
function ebiometrees.grow_leaves_closure(leaves_name)
local function grow_leaves(pos)
local free_node = minetest.find_node_near(pos, 1, "air")
if free_node
then
minetest.set_node(free_node, {name=leaves_name})
end
return true
end
return grow_leaves
end
2023-03-19 21:47:19 +00:00
2023-05-10 23:39:08 +00:00
function ebiometrees.tree_grower(leaves_name, grow_preferences, terminal_name, nextstage, age_max, grow_limit)
local grow_trunk = ebiometrees.grow_trunk_closure(terminal_name, grow_preferences, leaves_name, grow_limit)
2023-03-19 21:47:19 +00:00
local grow_leaves = ebiometrees.grow_leaves_closure(leaves_name)
local function grow_tree(pos)
local meta = minetest.get_meta(pos)
meta:set_int("ebiometrees:age", meta:get_int("ebiometrees:age")+1)
grow_trunk(pos)
grow_leaves(pos)
if nextstage
then
if meta:get_int("ebiometrees:age") > age_max
then
minetest.set_node(pos, {name=nextstage})
end
end
return true
end
return grow_tree
end