Leave decay without ABM

User avatar
burli
Member
 
Posts: 1313
Joined: Fri Apr 10, 2015 13:18

Leave decay without ABM

by burli » Sun Feb 05, 2017 20:24

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,
})
 

User avatar
Milan*
Member
 
Posts: 203
Joined: Thu May 28, 2015 06:45
GitHub: tchncs
IRC: Passant
In-game: Milan Passant

Re: Leave decay without ABM

by Milan* » Sun Feb 05, 2017 22:09

Oh i like this idea.
 

juhdanad
New member
 
Posts: 2
Joined: Tue Jan 03, 2017 21:42
GitHub: juhdanad
IRC: juhdanad
In-game: juhdanad

Re: Leave decay without ABM

by juhdanad » Sun Feb 05, 2017 22:56

You should check if a tree node was placed nearly in 'on_timer' too.
But great idea indeed!
 

User avatar
burli
Member
 
Posts: 1313
Joined: Fri Apr 10, 2015 13:18

Re: Leave decay without ABM

by burli » Mon Feb 06, 2017 06:35

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?
 

User avatar
burli
Member
 
Posts: 1313
Joined: Fri Apr 10, 2015 13:18

Re: Leave decay without ABM

by burli » Mon Feb 06, 2017 08:06

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
 

User avatar
TumeniNodes
Member
 
Posts: 1335
Joined: Fri Feb 26, 2016 19:49
GitHub: TumeniNodes

Re: Leave decay without ABM

by TumeniNodes » Mon Feb 06, 2017 12:49

Well, this is very cool : )
Strange bit about the fruit though... I never paid attention to the code for the apples... gonna look now.
Flick?... Flick who?
 

User avatar
burli
Member
 
Posts: 1313
Joined: Fri Apr 10, 2015 13:18

Re: Leave decay without ABM

by burli » Mon Feb 06, 2017 13:46

 

User avatar
Milan*
Member
 
Posts: 203
Joined: Thu May 28, 2015 06:45
GitHub: tchncs
IRC: Passant
In-game: Milan Passant

Re: Leave decay without ABM

by Milan* » Tue Feb 07, 2017 10:34

I like the activity around the pull req. :)
 

User avatar
ErrorNull
Member
 
Posts: 94
Joined: Thu Mar 03, 2016 00:43

Re: Leave decay without ABM

by ErrorNull » Fri Feb 10, 2017 04:55

i like this idea. the current leafdecay abm seems to be so heavy..
 

User avatar
burli
Member
 
Posts: 1313
Joined: Fri Apr 10, 2015 13:18

Re: Leave decay without ABM

by burli » Fri Feb 10, 2017 09:48

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
 


Return to Minetest Features

Who is online

Users browsing this forum: No registered users and 44 guests

cron