electric/mods/ebiometrees/sapling.lua

88 lines
2.3 KiB
Lua
Raw Normal View History

ebiometrees = {}
2023-03-19 21:47:19 +00:00
ebiometrees.directions = {
up = vector.new( 0, 1, 0),
down = vector.new( 0, -1, 0),
north = vector.new( 0, 0, 1),
east = vector.new( 1, 0, 0),
south = vector.new( 0, 0, -1),
west = vector.new(-1, 0, 0)
}
function ebiometrees.grow_closure(nextstage)
local function growtree(pos)
minetest.set_node(pos, {name=nextstage})
end
return growtree
end
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)
if node and node.name == leaves_name
then
return candidate
end
end
end
return where_to_grow_trunk
end
function ebiometrees.grow_trunk_closure(trunk_terminal_name, grow_preferences, leaves_name)
local function grow_trunk(pos)
local where = ebiometrees.where_to_grow_trunk_closure(
grow_preferences, leaves_name
)
local there = where(pos)
if there
then
minetest.set_node(there, {name=trunk_terminal_name})
end
end
return grow_trunk
end
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
function ebiometrees.tree_grower(leaves_name, grow_preferences, terminal_name, nextstage, age_max)
local grow_trunk = ebiometrees.grow_trunk_closure(terminal_name, grow_preferences, leaves_name)
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
function ebiometrees.root_sapling(pos)
minetest.set_node(pos, {name="ebiome:oak_sprout"})
return false
end