From a4ef367708b909c82cbbedd408710b9e936a67b3 Mon Sep 17 00:00:00 2001 From: kabou Date: Sun, 11 Dec 2022 20:54:29 +0100 Subject: [PATCH 1/6] Add "orphaned" leaves nodes. * Add "orphaned" leaves nodes, these are copies of the regular leaves nodes whose sole purpose is to replace leaves nodes when no more tree trunks are present nearby. The orphaned nodes are swapped in by a callback on destruction of tree trunk nodes and will be decayed by a dedicated abm that processes all nodes in `group:leaves_orphan`. * Add `mcl_core.update_leaves()` function, a `after_destruct` callback handler for tree trunk nodes. The function finds leaves nodes nearby destroyed tree trunk nodes and swaps these for orphaned leaves nodes if no other tree trunk nodes remain near them. * Add `after_destruct` callbacks to tree trunk node registrations in mcl_core/nodes_trees.lua and mcl_mangrove/init.lua to update orphaned leaves. * Add entry for group `leaves_orphan` to GROUPS.md * Also add entry for group `leaves` to GROUPS.md (it was missing). --- GROUPS.md | 2 ++ mods/ITEMS/mcl_core/nodes_trees.lua | 51 +++++++++++++++++++++++++++-- mods/ITEMS/mcl_mangrove/init.lua | 17 ++++++++-- 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/GROUPS.md b/GROUPS.md index 3c954111f..e6d878990 100644 --- a/GROUPS.md +++ b/GROUPS.md @@ -74,6 +74,8 @@ Please read to learn how digging times * `coral_species=X`: Specifies the species of a coral; equal X means equal species * `set_on_fire=X`: Sets any (not fire-resistant) mob or player on fire for X seconds when touching * `compostability=X`: Item can be used on a composter block; X (1-100) is the % chance of adding a level of compost +* `leaves=X`: Node will spotaneously decay if no tree trunk nodes remain within 6 blocks distance. +* `leaves_orphan`: See above, these nodes are in the process of decayed. #### Footnotes diff --git a/mods/ITEMS/mcl_core/nodes_trees.lua b/mods/ITEMS/mcl_core/nodes_trees.lua index 54a43d6bf..6e47da52c 100644 --- a/mods/ITEMS/mcl_core/nodes_trees.lua +++ b/mods/ITEMS/mcl_core/nodes_trees.lua @@ -8,6 +8,40 @@ if mod_screwdriver then on_rotate = screwdriver.rotate_3way end +-- Check dug/destroyed tree trunks for orphaned leaves. +-- +-- This function is meant to be called by the `after_destruct` handler of +-- treetrunk nodes. +-- +-- Whenever a trunk node is removed, all `group:leaves` nodes in a sphere +-- with radius 6 are checked. Every such node that does not have a trunk +-- node within a distance of 6 blocks is converted into a orphan leaf node. +-- An ABM will gradually decay these nodes. +-- +-- If param2 of the node is set to a nonzero value, the node will always +-- be preserved. This is set automatically when leaves are placed manually. +-- +-- @param pos the position of the removed trunk node. +-- @param oldnode the node table of the removed trunk node. +function mcl_core.update_leaves(pos, oldnode) + local pos1, pos2 = vector.offset(pos, -6, -6, -6), vector.offset(pos, 6, 6, 6) + local lnode + local leaves = minetest.find_nodes_in_area(pos1, pos2, "group:leaves") + for _, lpos in pairs(leaves) do + lnode = minetest.get_node(lpos) + -- skip already decaying leaf nodes + if minetest.get_item_group(lnode.name, "orphan_leaves") ~= 1 then + if not minetest.find_node_near(lpos, 6, "group:tree") then + -- manually placed leaf nodes have param2 + -- set and will never decay automatically + if lnode.param2 == 0 then + minetest.swap_node(lpos, {name = lnode.name .. "_orphan"}) + end + end + end + end +end + -- Register tree trunk (wood) and bark local function register_tree_trunk(subname, description_trunk, description_bark, longdesc, tile_inner, tile_bark, stripped_variant) minetest.register_node("mcl_core:"..subname, { @@ -17,6 +51,7 @@ local function register_tree_trunk(subname, description_trunk, description_bark, tiles = {tile_inner, tile_inner, tile_bark}, paramtype2 = "facedir", on_place = mcl_util.rotate_axis, + after_destruct = mcl_core.update_leaves, stack_max = 64, groups = {handy=1,axey=1, tree=1, flammable=2, building_block=1, material_wood=1, fire_encouragement=5, fire_flammability=5}, sounds = mcl_sounds.node_sound_wood_defaults(), @@ -141,7 +176,7 @@ local function register_leaves(subname, description, longdesc, tiles, sapling, d return drop end - minetest.register_node("mcl_core:"..subname, { + local l_def = { description = description, _doc_items_longdesc = longdesc, _doc_items_hidden = false, @@ -164,7 +199,19 @@ local function register_leaves(subname, description, longdesc, tiles, sapling, d _mcl_hardness = 0.2, _mcl_silk_touch_drop = true, _mcl_fortune_drop = { get_drops(1), get_drops(2), get_drops(3), get_drops(4) }, - }) + } + + minetest.register_node("mcl_core:" .. subname, l_def) + + local o_def = table.copy(l_def) + o_def._doc_items_create_entry = false + o_def.place_param2 = nil + o_def.groups.not_in_creative_inventory = 1 + o_def.groups.orphan_leaves = 1 + o_def._mcl_shears_drop = {"mcl_core:" .. subname} + o_def._mcl_silk_touch_drop = {"mcl_core:" .. subname} + + minetest.register_node("mcl_core:" .. subname .. "_orphan", o_def) end local function register_sapling(subname, description, longdesc, tt_help, texture, selbox) diff --git a/mods/ITEMS/mcl_mangrove/init.lua b/mods/ITEMS/mcl_mangrove/init.lua index 666b471de..e228e9f37 100644 --- a/mods/ITEMS/mcl_mangrove/init.lua +++ b/mods/ITEMS/mcl_mangrove/init.lua @@ -52,6 +52,7 @@ minetest.register_node("mcl_mangrove:mangrove_tree", { tiles = {"mcl_mangrove_log_top.png", "mcl_mangrove_log_top.png", "mcl_mangrove_log.png"}, paramtype2 = "facedir", on_place = mcl_util.rotate_axis, + after_destruct = mcl_core.update_leaves, groups = {handy=1,axey=1, tree=1, flammable=2, building_block=1, material_wood=1, fire_encouragement=5, fire_flammability=5}, sounds = mcl_sounds.node_sound_wood_defaults(), on_place = mcl_util.rotate_axis, @@ -86,7 +87,7 @@ minetest.register_node("mcl_mangrove:mangrove_wood", { _mcl_hardness = 2, }) -minetest.register_node("mcl_mangrove:mangroveleaves", { +local l_def = { description = S("Mangrove Leaves"), _doc_items_longdesc = S("mangrove leaves are grown from mangrove trees."), _doc_items_hidden = false, @@ -103,7 +104,19 @@ minetest.register_node("mcl_mangrove:mangroveleaves", { _mcl_hardness = 0.2, _mcl_silk_touch_drop = true, _mcl_fortune_drop = { get_drops(1), get_drops(2), get_drops(3), get_drops(4) }, -}) +} + +minetest.register_node("mcl_mangrove:mangroveleaves", l_def) + +local o_def = table.copy(l_def) +o_def._doc_items_create_entry = false +o_def.place_param2 = nil +o_def.groups.not_in_creative_inventory = 1 +o_def.groups.orphan_leaves = 1 +o_def._mcl_shears_drop = {"mcl_mangrove:mangroveleaves"} +o_def._mcl_silk_touch_drop = {"mcl_mangrove:mangroveleaves"} + +minetest.register_node("mcl_mangrove:mangroveleaves_orphan", o_def) minetest.register_node("mcl_mangrove:mangrove_stripped_trunk", { description = S("Stripped Mangrove Wood"), From adcf03e0c63cac1a6804e4a556e51fdb6e403927 Mon Sep 17 00:00:00 2001 From: kabou Date: Sun, 11 Dec 2022 21:30:05 +0100 Subject: [PATCH 2/6] Refactor leaf decay ABM. * The leaf decay ABM no longer scans all leaves nodes. Now only "orphaned" leaves are decayed. These nodes are placed by callbacks in tree trunk nodes upon their destruction. * This commit also removes the tree trunk node caching system and an abm throttling mechanism and its associated globalstep function. --- mods/ITEMS/mcl_core/functions.lua | 132 ++++++++---------------------- 1 file changed, 35 insertions(+), 97 deletions(-) diff --git a/mods/ITEMS/mcl_core/functions.lua b/mods/ITEMS/mcl_core/functions.lua index 5a01cd8c1..c26f31a54 100644 --- a/mods/ITEMS/mcl_core/functions.lua +++ b/mods/ITEMS/mcl_core/functions.lua @@ -1367,108 +1367,46 @@ function mcl_core.supports_vines(nodename) end -- Leaf Decay - --- To enable leaf decay for a node, add it to the "leafdecay" group. -- --- The rating of the group determines how far from a node in the group "tree" --- the node can be without decaying. +-- Whenever a tree trunk node is removed, all `group:leaves` nodes in a radius +-- of 6 blocks are checked from the trunk node's `after_destruct` handler. +-- Any such nodes within that radius that has no trunk node present within a +-- distance of 6 blocks is replaced with a `group:orphan_leaves` node. -- --- If param2 of the node is ~= 0, the node will always be preserved. Thus, if --- the player places a node of that kind, you will want to set param2=1 or so. --- - -mcl_core.leafdecay_trunk_cache = {} -mcl_core.leafdecay_enable_cache = true --- Spread the load of finding trunks -mcl_core.leafdecay_trunk_find_allow_accumulator = 0 - -minetest.register_globalstep(function(dtime) - --local finds_per_second = 5000 - mcl_core.leafdecay_trunk_find_allow_accumulator = math.floor(dtime * 5000) -end) - +-- The `group:orphan_leaves` nodes are gradually decayed in this ABM. minetest.register_abm({ label = "Leaf decay", - nodenames = {"group:leafdecay"}, - neighbors = {"air", "group:liquid"}, - -- A low interval and a high inverse chance spreads the load - interval = 2, - chance = 5, + nodenames = {"group:orphan_leaves"}, + interval = 5, + chance = 10, + action = function(pos, node) + -- Spawn item entities for any of the leaf's drops + local itemstacks = minetest.get_node_drops(node.name) + for _, itemname in pairs(itemstacks) do + local p_drop = vector.offset(pos, math.random() - 0.5, math.random() - 0.5, math.random() - 0.5) + minetest.add_item(p_drop, itemname) + end + -- Remove the decayed node + minetest.remove_node(pos) + leafdecay_particles(pos, node) + minetest.check_for_falling(pos) - action = function(p0, node, _, _) - local do_preserve = false - local d = minetest.registered_nodes[node.name].groups.leafdecay - if not d or d == 0 then - return - end - local n0 = minetest.get_node(p0) - if n0.param2 ~= 0 then - -- Prevent leafdecay for player-placed leaves. - -- param2 is set to 1 after it was placed by the player - return - end - local p0_hash = nil - if mcl_core.leafdecay_enable_cache then - p0_hash = minetest.hash_node_position(p0) - local trunkp = mcl_core.leafdecay_trunk_cache[p0_hash] - if trunkp then - local n = minetest.get_node(trunkp) - local reg = minetest.registered_nodes[n.name] - -- Assume ignore is a trunk, to make the thing work at the border of the active area - if n.name == "ignore" or (reg and reg.groups.tree and reg.groups.tree ~= 0) then - return - end - -- Cache is invalid - table.remove(mcl_core.leafdecay_trunk_cache, p0_hash) - end - end - if mcl_core.leafdecay_trunk_find_allow_accumulator <= 0 then - return - end - mcl_core.leafdecay_trunk_find_allow_accumulator = - mcl_core.leafdecay_trunk_find_allow_accumulator - 1 - -- Assume ignore is a trunk, to make the thing work at the border of the active area - local p1 = minetest.find_node_near(p0, d, {"ignore", "group:tree"}) - if p1 then - do_preserve = true - if mcl_core.leafdecay_enable_cache then - -- Cache the trunk - mcl_core.leafdecay_trunk_cache[p0_hash] = p1 - end - end - if not do_preserve then - -- Drop stuff other than the node itself - local itemstacks = minetest.get_node_drops(n0.name) - for _, itemname in pairs(itemstacks) do - local p_drop = { - x = p0.x - 0.5 + math.random(), - y = p0.y - 0.5 + math.random(), - z = p0.z - 0.5 + math.random(), - } - minetest.add_item(p_drop, itemname) - end - -- Remove node - minetest.remove_node(p0) - leafdecay_particles(p0, n0) - minetest.check_for_falling(p0) - - -- Kill depending vines immediately to skip the vines decay delay - local surround = { - { x = 0, y = 0, z = -1 }, - { x = 0, y = 0, z = 1 }, - { x = -1, y = 0, z = 0 }, - { x = 1, y = 0, z = 0 }, - { x = 0, y = -1, z = -1 }, - } - for s=1, #surround do - local spos = vector.add(p0, surround[s]) - local maybe_vine = minetest.get_node(spos) - --local surround_inverse = vector.multiply(surround[s], -1) - if maybe_vine.name == "mcl_core:vine" and (not mcl_core.check_vines_supported(spos, maybe_vine)) then - minetest.remove_node(spos) - vinedecay_particles(spos, maybe_vine) - minetest.check_for_falling(spos) - end + -- Kill depending vines immediately to skip the vines decay delay + local surround = { + { x = 0, y = 0, z = -1 }, + { x = 0, y = 0, z = 1 }, + { x = -1, y = 0, z = 0 }, + { x = 1, y = 0, z = 0 }, + { x = 0, y = -1, z = -1 }, + } + for s=1, #surround do + local spos = vector.add(pos, surround[s]) + local maybe_vine = minetest.get_node(spos) + --local surround_inverse = vector.multiply(surround[s], -1) + if maybe_vine.name == "mcl_core:vine" and (not mcl_core.check_vines_supported(spos, maybe_vine)) then + minetest.remove_node(spos) + vinedecay_particles(spos, maybe_vine) + minetest.check_for_falling(spos) end end end From cec5b1668ab5eb93d8f569c0851d0385eb72342f Mon Sep 17 00:00:00 2001 From: kabou Date: Sun, 11 Dec 2022 21:42:38 +0100 Subject: [PATCH 3/6] Remove unused `leafdecay` group. * This group is no longer used and is removed from leaves node groups. * Since it was never added to GROUPS.md, nothing needs changed there. * Remove the `leafdecay_distance` parameter from node registrations. --- mods/ITEMS/mcl_core/nodes_trees.lua | 8 ++------ mods/ITEMS/mcl_mangrove/init.lua | 6 +++++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/mods/ITEMS/mcl_core/nodes_trees.lua b/mods/ITEMS/mcl_core/nodes_trees.lua index 6e47da52c..787faa6d7 100644 --- a/mods/ITEMS/mcl_core/nodes_trees.lua +++ b/mods/ITEMS/mcl_core/nodes_trees.lua @@ -142,10 +142,7 @@ local function register_wooden_planks(subname, description, tiles) }) end -local function register_leaves(subname, description, longdesc, tiles, sapling, drop_apples, sapling_chances, leafdecay_distance) - if leafdecay_distance == nil then - leafdecay_distance = 4 - end +local function register_leaves(subname, description, longdesc, tiles, sapling, drop_apples, sapling_chances) local apple_chances = {200, 180, 160, 120, 40} local stick_chances = {50, 45, 30, 35, 10} @@ -188,9 +185,8 @@ local function register_leaves(subname, description, longdesc, tiles, sapling, d stack_max = 64, groups = { handy = 1, hoey = 1, shearsy = 1, swordy = 1, dig_by_piston = 1, - leaves = 1, leafdecay = leafdecay_distance, deco_block = 1, flammable = 2, fire_encouragement = 30, fire_flammability = 60, - compostability = 30 + leaves = 1, deco_block = 1, compostability = 30 }, drop = get_drops(0), _mcl_shears_drop = true, diff --git a/mods/ITEMS/mcl_mangrove/init.lua b/mods/ITEMS/mcl_mangrove/init.lua index e228e9f37..c31c9abb2 100644 --- a/mods/ITEMS/mcl_mangrove/init.lua +++ b/mods/ITEMS/mcl_mangrove/init.lua @@ -96,7 +96,11 @@ local l_def = { place_param2 = 1, -- Prevent leafdecay for placed nodes tiles = {"mcl_mangrove_leaves.png"}, paramtype = "light", - groups = {handy=1,shearsy=1,swordy=1, leafdecay=10, flammable=2, leaves=1, deco_block=1, dig_by_piston=1, fire_encouragement=30, fire_flammability=60}, + groups = { + handy = 1, hoey = 1, shearsy = 1, swordy = 1, dig_by_piston = 1, + flammable = 2, fire_encouragement = 30, fire_flammability = 60, + leaves = 1, deco_block = 1 + }, drop = get_drops(0), _mcl_shears_drop = true, sounds = mcl_sounds.node_sound_leaves_defaults(), From c33d7d01068afe4fb6b53cd5ce9029413c5a7652 Mon Sep 17 00:00:00 2001 From: kabou Date: Sun, 11 Dec 2022 22:05:16 +0100 Subject: [PATCH 4/6] Remove duplicate callback registration. * `on_place` was registered twice in mangrove tree trunk node registration. Remove one. --- mods/ITEMS/mcl_mangrove/init.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/mods/ITEMS/mcl_mangrove/init.lua b/mods/ITEMS/mcl_mangrove/init.lua index c31c9abb2..803f1a47b 100644 --- a/mods/ITEMS/mcl_mangrove/init.lua +++ b/mods/ITEMS/mcl_mangrove/init.lua @@ -55,7 +55,6 @@ minetest.register_node("mcl_mangrove:mangrove_tree", { after_destruct = mcl_core.update_leaves, groups = {handy=1,axey=1, tree=1, flammable=2, building_block=1, material_wood=1, fire_encouragement=5, fire_flammability=5}, sounds = mcl_sounds.node_sound_wood_defaults(), - on_place = mcl_util.rotate_axis, _mcl_blast_resistance = 2, _mcl_hardness = 2, _mcl_stripped_variant = "mcl_mangrove:mangrove_stripped_trunk", From 1b0b2b8b04f9b25e6b30415f08c3221183c64106 Mon Sep 17 00:00:00 2001 From: kabou Date: Sun, 11 Dec 2022 22:09:18 +0100 Subject: [PATCH 5/6] Add `compostability` group to mangrove leaves. --- mods/ITEMS/mcl_mangrove/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mods/ITEMS/mcl_mangrove/init.lua b/mods/ITEMS/mcl_mangrove/init.lua index 803f1a47b..767edf966 100644 --- a/mods/ITEMS/mcl_mangrove/init.lua +++ b/mods/ITEMS/mcl_mangrove/init.lua @@ -98,7 +98,7 @@ local l_def = { groups = { handy = 1, hoey = 1, shearsy = 1, swordy = 1, dig_by_piston = 1, flammable = 2, fire_encouragement = 30, fire_flammability = 60, - leaves = 1, deco_block = 1 + leaves = 1, deco_block = 1, compostability = 30 }, drop = get_drops(0), _mcl_shears_drop = true, From 4e282df905450d3e7517923d9b38a8ca2ddf7b48 Mon Sep 17 00:00:00 2001 From: kabou Date: Sun, 11 Dec 2022 22:12:30 +0100 Subject: [PATCH 6/6] Mangrove roots are not leaves and some trivia. * Remove `leaves` group from mangrove roots node registration. Mangrove roots should not decay like leaves do. * Fix some formatting weirdness. * Add a "fixme" comment to some duplicated code that causes luacheck to complain. --- mods/ITEMS/mcl_mangrove/init.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mods/ITEMS/mcl_mangrove/init.lua b/mods/ITEMS/mcl_mangrove/init.lua index 767edf966..85f96dfb0 100644 --- a/mods/ITEMS/mcl_mangrove/init.lua +++ b/mods/ITEMS/mcl_mangrove/init.lua @@ -163,11 +163,13 @@ minetest.register_node("mcl_mangrove:mangrove_roots", { drawtype = "allfaces_optional", groups = { handy = 1, hoey = 1, shearsy = 1, axey = 1, swordy = 1, dig_by_piston = 0, - leaves = 1, deco_block = 1,flammable = 10, fire_encouragement = 30, fire_flammability = 60, compostability = 30 + flammable = 10, fire_encouragement = 30, fire_flammability = 60, + deco_block = 1, compostability = 30 }, drop = "mcl_mangrove:mangrove_roots", _mcl_shears_drop = true, - sounds = mcl_sounds.node_sound_leaves_defaults(), _mcl_blast_resistance = 0.7, + sounds = mcl_sounds.node_sound_leaves_defaults(), + _mcl_blast_resistance = 0.7, _mcl_hardness = 0.7, _mcl_silk_touch_drop = true, _mcl_fortune_drop = { "mcl_mangrove:mangrove_roots 1", "mcl_mangrove:mangrove_roots 2", "mcl_mangrove:mangrove_roots 3", "mcl_mangrove:mangrove_roots 4" }, @@ -345,6 +347,9 @@ local wlroots = { end, } local rwlroots = table.copy(wlroots) +-- FIXME luacheck complains that this is a repeated definition of water_tex. +-- Maybe the tiles definition below should be replaced with the animated tile +-- definition as per above? water_tex = "default_river_water_source_animated.png^[verticalframe:16:0" rwlroots.tiles = { "("..water_tex..")^mcl_mangrove_roots_top.png",