Page 1 of 1

Leave decay without ABM

PostPosted: Sun Feb 05, 2017 20:24
by burli
I don't know if this was tried before. I disabled the leave decay ABM and tried this.

If a tree node is removed I trigger all leave nodes in a radius around the tree node to search for a tree node. If there is no tree node near the leave I start a node timer with a random time

The advantage is, that I can only trigger the matching leaves to the tree, so I don't remove other leaves. Works pretty well, except that apples don't drop until now. And we save a lot of computation time I think because this only cost time if a tree node is removed

Here is a demonstration video
https://youtu.be/VaK0cyDRaO8

This code is just a proof of concept, but works as expected for default trees

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_node(":default:tree", {
   description = "Tree",
   tiles = {"default_tree_top.png", "default_tree_top.png", "default_tree.png"},
   paramtype2 = "facedir",
   is_ground_content = false,
   groups = {tree = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2},
   sounds = default.node_sound_wood_defaults(),

   on_place = minetest.rotate_node,

   after_destruct = function(pos, oldnode)
      local radius = 3
      local lradius = 2
      local minp = vector.subtract(pos, radius)
      local maxp = vector.add(pos, radius)
      local leaves_near, leaves_count = minetest.find_nodes_in_area(minp, maxp, "default:leaves")
      for i = 1, leaves_count["default:leaves"] do
         local leaves_at_pos = leaves_near[i]
         local trunks_near = minetest.find_node_near(leaves_at_pos, lradius, "default:tree")         
         if not trunks_near then
            minetest.get_node_timer(leaves_at_pos):start(math.random() * 10.0)
         end
      end
   end,
})


minetest.register_node(":default:leaves", {
   description = "Leaves",
   drawtype = "allfaces_optional",
   waving = 1,
   tiles = {"default_leaves.png"},
   special_tiles = {"default_leaves_simple.png"},
   paramtype = "light",
   is_ground_content = false,
   groups = {snappy = 3, leafdecay = 3, flammable = 2, leaves = 1},
   drop = {
      max_items = 1,
      items = {
         {
            -- player will get sapling with 1/20 chance
            items = {'default:sapling'},
            rarity = 20,
         },
         {
            -- player will get leaves only if he get no saplings,
            -- this is because max_items is 1
            items = {'default:leaves'},
         }
      }
   },
   sounds = default.node_sound_leaves_defaults(),
   on_timer = function(pos, elapsed)
      -- Drop stuff
      local itemstacks = minetest.get_node_drops("default:leaves")
      for _, itemname in ipairs(itemstacks) do
         if itemname ~= "default:leaves" or
               minetest.get_item_group("default:leaves", "leafdecay_drop") ~= 0 then
            local p_drop = {
               x = pos.x - 0.5 + math.random(),
               y = pos.y - 0.5 + math.random(),
               z = pos.z - 0.5 + math.random(),
            }
            minetest.add_item(p_drop, itemname)
         end
      end
      -- Remove node
      minetest.remove_node(pos)
      minetest.check_for_falling(pos)
   end,
   after_place_node = tree.after_place_leaves,
})

Re: Leave decay without ABM

PostPosted: Sun Feb 05, 2017 22:09
by Milan*
Oh i like this idea.

Re: Leave decay without ABM

PostPosted: Sun Feb 05, 2017 22:56
by juhdanad
You should check if a tree node was placed nearly in 'on_timer' too.
But great idea indeed!

Re: Leave decay without ABM

PostPosted: Mon Feb 06, 2017 06:35
by burli
juhdanad wrote:You should check if a tree node was placed nearly in 'on_timer' too.

Maybe, yes

Does anyone know why the apples don't drop?

Re: Leave decay without ABM

PostPosted: Mon Feb 06, 2017 08:06
by burli
I add the new leave decay to my mod base experiment. Works now for all default trees and the experimental and still buggy palm tree.

Just download it and use it like a subgame (don't use v6 mapgen. It is not supported yet)
https://github.com/MarkuBu/minetest_game/tree/mod_base

pros of this methode:
- no ABM
- decay speed can be adjusted down to immediate
- radius for leave search can be adjusted per tree. Huge trees are possible
- only leaves from the tree are decaying
- leaves even decay if another (different) tree is standing close

cons:
- mods have to be changed to the new system

bugs:
- fruits don't drop

Re: Leave decay without ABM

PostPosted: Mon Feb 06, 2017 12:49
by TumeniNodes
Well, this is very cool : )
Strange bit about the fruit though... I never paid attention to the code for the apples... gonna look now.

Re: Leave decay without ABM

PostPosted: Mon Feb 06, 2017 13:46
by burli

Re: Leave decay without ABM

PostPosted: Tue Feb 07, 2017 10:34
by Milan*
I like the activity around the pull req. :)

Re: Leave decay without ABM

PostPosted: Fri Feb 10, 2017 04:55
by ErrorNull
i like this idea. the current leafdecay abm seems to be so heavy..

Re: Leave decay without ABM

PostPosted: Fri Feb 10, 2017 09:48
by burli
I tried to replace lava cooling with a similar approach, but only works with lava source, not with lava flowing. Can't get the node timer to run