2023-10-16 18:41:09 +00:00
|
|
|
local modname = minetest.get_current_modname()
|
|
|
|
local api = blockgame.tree
|
|
|
|
|
|
|
|
-- NODE NAMES
|
|
|
|
|
|
|
|
local leaves = modname .. ":leaves"
|
|
|
|
local leaves_decomposing = modname .. ":leaves_decomposing"
|
|
|
|
|
2023-10-19 14:18:24 +00:00
|
|
|
|
|
|
|
|
2023-10-19 14:49:36 +00:00
|
|
|
local decompose_time_speed = 1
|
|
|
|
-- TODO: add setting for this as well.
|
|
|
|
minetest.register_chatcommand(modname .. ":set_decompose_time_speed", {
|
|
|
|
params = "<value>",
|
|
|
|
description = "set the speed at which decomposition happens.",
|
|
|
|
privs = {
|
|
|
|
control_time = true,
|
|
|
|
},
|
|
|
|
func = function (player_name, speed)
|
|
|
|
speed = tonumber(speed)
|
|
|
|
if speed == nil then
|
|
|
|
minetest.chat_send_player(player_name, "time speed must be a number.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if speed < 0 or speed > 256 then
|
|
|
|
minetest.chat_send_player(player_name, "time speed must be between 0 and 256 (inclusive).")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
decompose_time_speed = speed
|
|
|
|
minetest.chat_send_all(player_name .. " set the leaves decomposition time speed to " .. speed .. ".")
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-10-19 14:18:24 +00:00
|
|
|
local decompose_scores = {
|
|
|
|
dirty = 50,
|
|
|
|
leaves_decomposing = 30,
|
|
|
|
}
|
|
|
|
|
|
|
|
local start_decompose_cost = 4000
|
2023-10-16 18:41:09 +00:00
|
|
|
|
2023-10-16 19:30:57 +00:00
|
|
|
blockgame.register_increasing_abm({
|
2023-10-17 07:48:48 +00:00
|
|
|
id = modname .. ":begin_decompose",
|
2023-10-16 18:41:09 +00:00
|
|
|
label = "decompose leaves",
|
|
|
|
nodenames = {leaves},
|
2023-10-17 08:15:13 +00:00
|
|
|
neighbors = {"group:dirty", leaves_decomposing},
|
2023-10-19 14:51:14 +00:00
|
|
|
interval = 15,
|
|
|
|
chance = 8,
|
2023-10-16 19:30:57 +00:00
|
|
|
rate = function (pos, node, data)
|
2023-10-19 14:18:24 +00:00
|
|
|
local score = blockgame.score_nearby_nodes(pos, 2, decompose_scores, function (gain, distance)
|
|
|
|
return math.floor(gain / (distance ^ 3))
|
|
|
|
end)
|
|
|
|
|
2023-10-19 14:49:36 +00:00
|
|
|
return data.value + score * decompose_time_speed
|
2023-10-16 19:30:57 +00:00
|
|
|
end,
|
|
|
|
check = function (pos, node, data)
|
2023-10-19 14:18:24 +00:00
|
|
|
return data.value >= start_decompose_cost
|
2023-10-16 19:30:57 +00:00
|
|
|
end,
|
|
|
|
action = function (pos, node, data)
|
2023-10-19 13:59:11 +00:00
|
|
|
-- 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})
|
2023-10-16 18:41:09 +00:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2023-10-19 13:32:24 +00:00
|
|
|
local decompose_cost = 30000
|
2023-10-19 14:00:43 +00:00
|
|
|
local average_leaves_per_dirt = 4
|
2023-10-17 11:41:30 +00:00
|
|
|
|
2023-10-16 19:30:57 +00:00
|
|
|
blockgame.register_increasing_abm({
|
2023-10-17 07:48:48 +00:00
|
|
|
id = modname .. ":decompose",
|
2023-10-16 18:41:09 +00:00
|
|
|
label = "decompose leaves",
|
2023-10-19 10:54:09 +00:00
|
|
|
nodenames = {"group:leaves_decomposing"},
|
2023-10-19 11:11:41 +00:00
|
|
|
-- neighbors = {"group:dirty", "group:leaves_decomposing"},
|
2023-10-19 14:51:14 +00:00
|
|
|
interval = 15,
|
|
|
|
chance = 10,
|
2023-10-16 19:30:57 +00:00
|
|
|
rate = function (pos, node, data)
|
2023-10-19 13:17:05 +00:00
|
|
|
local score = 20
|
2023-10-17 11:41:30 +00:00
|
|
|
|
2023-10-19 14:09:09 +00:00
|
|
|
local bonus = blockgame.score_nearby_nodes(pos, 4, decompose_scores, function (gain, distance)
|
|
|
|
return math.floor(gain / (distance ^ 3))
|
|
|
|
end)
|
2023-10-17 11:41:30 +00:00
|
|
|
|
2023-10-19 14:09:09 +00:00
|
|
|
score = score + bonus
|
2023-10-19 14:49:36 +00:00
|
|
|
return data.value + score * decompose_time_speed
|
2023-10-16 19:30:57 +00:00
|
|
|
end,
|
|
|
|
check = function (pos, node, data)
|
2023-10-17 11:41:30 +00:00
|
|
|
return data.value >= decompose_cost
|
2023-10-16 19:30:57 +00:00
|
|
|
end,
|
|
|
|
action = function (pos, node, data)
|
2023-10-19 10:58:31 +00:00
|
|
|
-- 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
|
|
|
|
|
2023-10-19 14:00:43 +00:00
|
|
|
if blockgame.chance(dirt_chance * average_leaves_per_dirt) then
|
2023-10-17 08:27:41 +00:00
|
|
|
minetest.set_node(pos, {name = "bg_terrain:dirt"})
|
|
|
|
else
|
|
|
|
minetest.remove_node(pos)
|
2023-10-19 15:05:49 +00:00
|
|
|
minetest.check_for_falling(pos)
|
2023-10-17 08:27:41 +00:00
|
|
|
end
|
2023-10-16 18:41:09 +00:00
|
|
|
end,
|
|
|
|
})
|