summaryrefslogtreecommitdiff
path: root/leafdecay.lua
blob: f77f8ddc4f40af2029ad1a691e4a50a0bc1b4a45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
-- leaf decay

function clone_node(name)
	node2={}
	node=minetest.registered_nodes[name]
	for k,v in pairs(node) do
		node2[k]=v
	end
	return node2
end

-- this function is based on the default leafdecay code
local process_drops = function(pos, name)
	local drops = minetest.get_node_drops(name)
	for _,dropitem in ipairs(drops) do
		if dropitem ~= name then
			local newpos = {
						x=pos.x + math.random() - 0.5,
						y=pos.y + math.random() - 0.5,
						z=pos.z + math.random() - 0.5
					}
			minetest.add_item(newpos, dropitem)
		end
	end
end

if moretrees.enable_leafdecay then
	for i in ipairs(moretrees.treelist) do
		local treename = moretrees.treelist[i][1]
		if treename ~= "jungletree" and treename ~= "fir" then
			minetest.register_abm({
				nodenames = "moretrees:"..treename.."_leaves",
				interval = moretrees.leafdecay_delay,
				chance = moretrees.leafdecay_chance,
				action = function(pos, node, active_object_count, active_object_count_wider)
					if minetest.find_node_near(pos, moretrees.leafdecay_radius, "moretrees:"..treename.."_trunk") then return end
					if minetest.find_node_near(pos, moretrees.leafdecay_radius, "ignore") then return end
					process_drops(pos, node.name)
					minetest.remove_node(pos)
					nodeupdate(pos)
				end
			})
		end
	end

	minetest.register_abm({
		nodenames = {"moretrees:jungletree_leaves_red","moretrees:jungletree_leaves_green","moretrees:jungletree_leaves_yellow"},
		interval = moretrees.leafdecay_delay,
		chance = moretrees.leafdecay_chance,
		action = function(pos, node, active_object_count, active_object_count_wider)
			if minetest.find_node_near(pos, moretrees.leafdecay_radius, {"default:jungletree", "moretrees:jungletree_trunk"}) then return end
			if minetest.find_node_near(pos, moretrees.leafdecay_radius, "ignore") then return end
			process_drops(pos, node.name)
			minetest.remove_node(pos)
			nodeupdate(pos)
		end
	})

	minetest.register_abm({
		nodenames = {"moretrees:fir_leaves", "moretrees:fir_leaves_bright"},
		interval = moretrees.leafdecay_delay,
		chance = moretrees.leafdecay_chance,
		action = function(pos, node, active_object_count, active_object_count_wider)
			if minetest.find_node_near(pos, moretrees.leafdecay_radius, "moretrees:fir_trunk") then return end
			if minetest.find_node_near(pos, moretrees.leafdecay_radius, "ignore") then return end
				process_drops(pos, node.name)
				minetest.remove_node(pos)
				nodeupdate(pos)
		end
	})

	minetest.register_abm({
		nodenames = "moretrees:palm_leaves",
		interval = moretrees.leafdecay_delay,
		chance = moretrees.leafdecay_chance,
		action = function(pos, node, active_object_count, active_object_count_wider)
			if minetest.find_node_near(pos, moretrees.palm_leafdecay_radius, "moretrees:palm_trunk") then return end
			if minetest.find_node_near(pos, moretrees.palm_leafdecay_radius, "ignore") then return end
				process_drops(pos, node.name)
				minetest.remove_node(pos)
				nodeupdate(pos)
		end
	})
end

if moretrees.enable_default_leafdecay then

	new_default_leaves = clone_node("default:leaves")
	new_default_leaves.groups = {snappy=3, flammable=2, leaves=1}
	minetest.register_node(":default:leaves", new_default_leaves)

	minetest.register_abm({
		nodenames = "default:leaves",
		interval = moretrees.default_leafdecay_delay,
		chance = moretrees.default_leafdecay_chance,
		action = function(pos, node, active_object_count, active_object_count_wider)
			if minetest.find_node_near(pos, moretrees.default_leafdecay_radius, "default:tree") then return end
			if minetest.find_node_near(pos, moretrees.default_leafdecay_radius, "ignore") then return end
			process_drops(pos, node.name)
			minetest.remove_node(pos)
			nodeupdate(pos)
		end
	})	
end

if moretrees.enable_default_jungle_leafdecay then

	new_default_jungle_leaves = clone_node("default:jungleleaves")
	new_default_jungle_leaves.groups = {snappy=3, flammable=2, leaves=1}
	minetest.register_node(":default:jungleleaves", new_default_jungle_leaves)

	minetest.register_abm({
		nodenames = "default:jungleleaves",
		interval = moretrees.default_jungle_leafdecay_delay,
		chance = moretrees.default_jungle_leafdecay_chance,
		action = function(pos, node, active_object_count, active_object_count_wider)
			if minetest.find_node_near(pos, moretrees.default_jungle_leafdecay_radius, "default:jungletree") then return end
			if minetest.find_node_near(pos, moretrees.default_jungle_leafdecay_radius, "ignore") then return end
			process_drops(pos, node.name)
			minetest.remove_node(pos)
			nodeupdate(pos)
		end
	})	
end