93 lines
2.4 KiB
Lua
93 lines
2.4 KiB
Lua
local modname = minetest.get_current_modname()
|
|
local api = blockgame.tree
|
|
local vec = blockgame.vector
|
|
|
|
-- NODE NAMES
|
|
|
|
local leaves = modname .. ":leaves"
|
|
local leaves_decomposing = modname .. ":leaves_decomposing"
|
|
|
|
-- END OF NODE NAMES
|
|
|
|
blockgame.register_increasing_abm({
|
|
id = modname .. ":begin_decompose",
|
|
label = "decompose leaves",
|
|
nodenames = {leaves},
|
|
neighbors = {"group:dirty", leaves_decomposing},
|
|
interval = 20,
|
|
chance = 4,
|
|
rate = function (pos, node, data)
|
|
return data.value + math.random(4, 6)
|
|
end,
|
|
check = function (pos, node, data)
|
|
return data.value >= 80
|
|
end,
|
|
action = function (pos, node, data)
|
|
-- NOTE: might wanna access the 4 from somewhere, instead of directly knowing it.
|
|
-- that way this won't have to change if decomposing leaves max levels change.
|
|
local level = math.random(1, 4)
|
|
minetest.set_node(pos, {name = leaves_decomposing .. "_" .. level})
|
|
end,
|
|
})
|
|
|
|
local decompose_scores = {
|
|
dirty = 50,
|
|
leaves_decomposing = 30,
|
|
}
|
|
|
|
local decompose_cost = 30000
|
|
local average_leaves_per_dirt = 4
|
|
|
|
blockgame.register_increasing_abm({
|
|
id = modname .. ":decompose",
|
|
label = "decompose leaves",
|
|
nodenames = {"group:leaves_decomposing"},
|
|
-- neighbors = {"group:dirty", "group:leaves_decomposing"},
|
|
interval = 30,
|
|
chance = 5,
|
|
rate = function (pos, node, data)
|
|
local score = 20
|
|
|
|
local source_pos = pos
|
|
blockgame.flood_fill(pos, function (pos, distance)
|
|
if pos == source_pos then return true end
|
|
|
|
local name = minetest.get_node(pos).name
|
|
|
|
local gain = 0
|
|
for group, value in pairs(decompose_scores) do
|
|
local group = minetest.get_item_group(name, group)
|
|
if group > 0 then
|
|
gain = math.max(gain, value * group)
|
|
end
|
|
end
|
|
|
|
gain = math.floor(gain / (distance ^ 3))
|
|
|
|
if gain > 0 then
|
|
score = score + gain
|
|
return true
|
|
end
|
|
|
|
return false
|
|
end, 4)
|
|
|
|
return data.value + score
|
|
end,
|
|
check = function (pos, node, data)
|
|
return data.value >= decompose_cost
|
|
end,
|
|
action = function (pos, node, data)
|
|
-- currently, larger leaf piles have a greater chance of turning into dirt instead of disappearing.
|
|
-- might wanna make it so that instead, dirt is layered as well?
|
|
local def = minetest.registered_nodes[node.name]
|
|
local dirt_chance = def.level_max - def.level + 1
|
|
|
|
if blockgame.chance(dirt_chance * average_leaves_per_dirt) then
|
|
minetest.set_node(pos, {name = "bg_terrain:dirt"})
|
|
else
|
|
minetest.remove_node(pos)
|
|
end
|
|
end,
|
|
})
|