Add cactus generation, desert_sand and desert_stone
[oweals/minetest_game.git] / mods / default / leafdecay.lua
1 -- minetest/default/leafdecay.lua
2
3 -- To enable leaf decay for a node, add it to the "leafdecay" group.
4 --
5 -- The rating of the group determines how far from a node in the group "tree"
6 -- the node can be without decaying.
7 --
8 -- If param2 of the node is ~= 0, the node will always be preserved. Thus, if
9 -- the player places a node of that kind, you will want to set param2=1 or so.
10
11 default.leafdecay_trunk_cache = {}
12 default.leafdecay_enable_cache = true
13 -- Spread the load of finding trunks
14 default.leafdecay_trunk_find_allow_accumulator = 0
15
16 minetest.register_globalstep(function(dtime)
17         local finds_per_second = 5000
18         default.leafdecay_trunk_find_allow_accumulator =
19                         math.floor(dtime * finds_per_second)
20 end)
21
22 minetest.register_abm({
23         nodenames = {"group:leafdecay"},
24         neighbors = {"air", "group:liquid"},
25         -- A low interval and a high inverse chance spreads the load
26         interval = 2,
27         chance = 5,
28
29         action = function(p0, node, _, _)
30                 --print("leafdecay ABM at "..p0.x..", "..p0.y..", "..p0.z..")")
31                 local do_preserve = false
32                 local d = minetest.registered_nodes[node.name].groups.leafdecay
33                 if not d or d == 0 then
34                         --print("not groups.leafdecay")
35                         return
36                 end
37                 local n0 = minetest.env:get_node(p0)
38                 if n0.param2 ~= 0 then
39                         --print("param2 ~= 0")
40                         return
41                 end
42                 local p0_hash = nil
43                 if default.leafdecay_enable_cache then
44                         p0_hash = minetest.hash_node_position(p0)
45                         local trunkp = default.leafdecay_trunk_cache[p0_hash]
46                         if trunkp then
47                                 local n = minetest.env:get_node(trunkp)
48                                 local reg = minetest.registered_nodes[n.name]
49                                 -- Assume ignore is a trunk, to make the thing work at the border of the active area
50                                 if n.name == "ignore" or (reg.groups.tree and reg.groups.tree ~= 0) then
51                                         --print("cached trunk still exists")
52                                         return
53                                 end
54                                 --print("cached trunk is invalid")
55                                 -- Cache is invalid
56                                 table.remove(default.leafdecay_trunk_cache, p0_hash)
57                         end
58                 end
59                 if default.leafdecay_trunk_find_allow_accumulator <= 0 then
60                         return
61                 end
62                 default.leafdecay_trunk_find_allow_accumulator =
63                                 default.leafdecay_trunk_find_allow_accumulator - 1
64                 -- Assume ignore is a trunk, to make the thing work at the border of the active area
65                 local p1 = minetest.env:find_node_near(p0, d, {"ignore", "group:tree"})
66                 if p1 then
67                         do_preserve = true
68                         if default.leafdecay_enable_cache then
69                                 --print("caching trunk")
70                                 -- Cache the trunk
71                                 default.leafdecay_trunk_cache[p0_hash] = p1
72                         end
73                 end
74                 if not do_preserve then
75                         -- Drop stuff other than the node itself
76                         itemstacks = minetest.get_node_drops(n0.name)
77                         for _, itemname in ipairs(itemstacks) do
78                                 if itemname ~= n0.name then
79                                         local p_drop = {
80                                                 x = p0.x - 0.5 + math.random(),
81                                                 y = p0.y - 0.5 + math.random(),
82                                                 z = p0.z - 0.5 + math.random(),
83                                         }
84                                         minetest.env:add_item(p_drop, itemname)
85                                 end
86                         end
87                         -- Remove node
88                         minetest.env:remove_node(p0)
89                 end
90         end
91 })
92