blockgame/mods/bg_tree/node_leaves.lua
2023-10-19 15:14:02 +02:00

129 lines
3.2 KiB
Lua

local modname = minetest.get_current_modname()
local function reg_leaves (name, def)
local texture = def.texture or name
def = blockgame.underride(def, {
description = name,
texture = texture,
tiles = {modname .. "_" .. texture .. ".png"},
drawtype = "allfaces_optional",
paramtype = "light",
sunlight_propagates = true,
})
def.groups = blockgame.underride(def.groups, {
planty = 1,
extends_leaves_support = 1,
air_flowable = 1,
falling_node = 1,
})
blockgame.register_node(name, def)
end
local nutty_drops = {
max_items = 3,
items = {
{
rarity = 20,
items = {modname .. ":nut 2"},
},
{
rarity = 16,
items = {modname .. ":nut"},
},
{
rarity = 1,
items = {modname .. ":leaves"},
},
},
}
reg_leaves("leaves", {
description = "Leaves",
walkable = false,
supports_falling = true,
})
reg_leaves("leaves_growing", {
description = "Growing Leaves",
texture = "leaves_alive",
drop = nutty_drops,
groups = {
falling_node = 0,
},
})
reg_leaves("leaves_alive", {
description = "Leaves",
drop = nutty_drops,
groups = {
falling_node = 0,
},
})
-- TODO: generalize stacking node registration and put into API.
local decomposing_leaves_layers = 4
local after_place_stack = function (pos)
local node = minetest.get_node(pos)
if not blockgame.starts_with(node.name, modname .. ":leaves_decomposing") then return end
local below = pos + blockgame.vector.dirs.down
local below_node = minetest.get_node(below)
if not blockgame.starts_with(below_node.name, modname .. ":leaves_decomposing") then return end
local def = minetest.registered_nodes[node.name]
local below_def = minetest.registered_nodes[below_node.name]
if below_def.level == decomposing_leaves_layers then return end
local max_diff = decomposing_leaves_layers - below_def.level
local diff = math.min(max_diff, def.level)
minetest.swap_node(below, {name = modname .. ":leaves_decomposing_" .. (below_def.level + diff)})
if diff == def.level then
minetest.remove_node(pos)
else
minetest.swap_node(pos, {name = modname .. ":leaves_decomposing_" .. (def.level - diff)})
end
end
local function reg_decomposing (level)
local height = level / decomposing_leaves_layers
local description = "Decomposing Leaves (" .. level .. "/" .. decomposing_leaves_layers .. ")"
if level == decomposing_leaves_layers then description = "Decomposing Leaves" end
local drawtype = "nodebox"
local node_box = {
["type"] = "fixed",
fixed = { -0.5, -0.5, -0.5, 0.5, -0.5 + height, 0.5 },
}
if level == decomposing_leaves_layers then
drawtype = "allfaces_optional"
node_box = nil
end
reg_leaves("leaves_decomposing_" .. level, {
description = description,
level = level,
level_max = decomposing_leaves_layers,
texture = "leaves_decomposing",
drawtype = drawtype,
node_box = node_box,
after_place_node = after_place_stack,
after_land = after_place_stack,
drop = modname .. ":leaves_decomposing_1 " .. level,
groups = {
leaves_decomposing = level,
},
walkable = false,
supports_falling = true,
})
end
for level=1, decomposing_leaves_layers do
reg_decomposing(level)
end
minetest.register_alias(modname .. ":leaves_decomposing", modname .. ":leaves_decomposing_" .. decomposing_leaves_layers)