redwood/sapling.lua

113 lines
2.6 KiB
Lua

local S = redwood.intllib
-- Register Saplings
redwood.register_sapling = function(name, desc, texture, height)
minetest.register_node(name .. "_sapling", {
description = S(desc .. " Tree Sapling"),
drawtype = "plantlike",
visual_scale = 1.0,
tiles = {texture .. ".png"},
inventory_image = texture .. ".png",
wield_image = texture .. ".png",
paramtype = "light",
sunlight_propagates = true,
is_ground_content = false,
walkable = false,
selection_box = {
type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5}
},
groups = {
snappy = 2, dig_immediate = 3, flammable = 2,
redwood_sapling = 1, sapling = 1, attached_node = 1
},
sounds = default.node_sound_defaults(),
grown_height = height,
})
end
redwood.register_sapling("redwood:redwood", "Redwood", "redwood_sapling", 31)
redwood.add_tree = function (pos, ofx, ofy, ofz, schem)
-- check for schematic
if not schem then
print (S("Schematic not found"))
return
end
-- remove sapling and place schematic
minetest.swap_node(pos, {name = "air"})
minetest.place_schematic(
{x = pos.x - ofx, y = pos.y - ofy, z = pos.z - ofz},
schem, 0, nil, false)
end
local path = minetest.get_modpath("redwood").."/schematics/"
-- grow tree functions
function redwood.grow_redwood_tree(pos)
if math.random(1, 2) == 1 then
redwood.add_tree(pos, 9, 3, 9, path .. "redwood.mts") -- shinji
else
redwood.add_tree(pos, 8, 6, 8, path .. "redwood_tree.mts") -- iska
end
end
-- check if sapling has enough height room to grow
local function enough_height(pos, height)
local nod = minetest.line_of_sight(
{x = pos.x, y = pos.y + 1, z = pos.z},
{x = pos.x, y = pos.y + height, z = pos.z})
if not nod then
return false -- obstructed
else
return true -- can grow
end
end
redwood.grow_sapling = function (pos, node)
local under = minetest.get_node({
x = pos.x,
y = pos.y - 1,
z = pos.z
}).name
if not minetest.registered_nodes[node.name] then
return
end
local height = minetest.registered_nodes[node.name].grown_height
-- do we have enough height to grow sapling into tree?
if not height or not enough_height(pos, height) then
return
end
-- Check if Ethereal Sapling is growing on correct substrate
if node.name == "redwood:redwood_sapling" then
redwood.grow_redwood_tree(pos)
end
end
-- Grow saplings
minetest.register_abm({
label = "Redwood grow sapling",
nodenames = {"group:redwood_sapling"},
interval = 10,
chance = 50,
catch_up = false,
action = function(pos, node)
local light_level = minetest.get_node_light(pos)
if not light_level or light_level < 13 then
return
end
redwood.grow_sapling(pos, node)
end,
})