summaryrefslogtreecommitdiff
path: root/init.lua
blob: ccb0900e0b4bea869148b2468ca891282fe6aab4 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
woodnt = {}

minetest.register_node("woodnt:treent",{
	description = "Treen't",
	tiles = {
		"default_tree.png^[invert:rgb",
	},
	groups = {
		cracky = 3,
	},
})

minetest.register_node("woodnt:woodnt",{
	description = "Woodn't Planks",
	tiles = {
		"default_wood.png^[invert:rgb",
	},
	groups = {
		cracky = 3,
	},
})

minetest.register_node("woodnt:leavesnt",{
	description = "Leavesn't",
	tiles = {
		"default_leaves.png^[invert:rgb",
	},
	groups = {
		snappy = 3,
	},
	drawtype = "glasslike",
	paramtype = "light",
	light_source = 8,
	drop = {
		max_items = 1,
		items = {
			{
				rarity = 10,
				items = {"woodnt:leavesnt"},
			},
			{
				rarity = 1,
				items = {"woodnt:saplingnt"},
			},

		}
	},
})

local random = math.random

function woodnt.can_grow(pos)
	local node_under = minetest.get_node_or_nil({x = pos.x, y = pos.y + 1, z = pos.z})
	if not node_under then
		return false
	end
	if minetest.get_item_group(node_under.name, "soil") > 0 then
		return false
	end
	local light_level = minetest.get_node_light(pos)
	if not light_level or light_level > 10 then
		return false
	end
	return true
end

local function add_trunk_and_leaves(data, a, pos, tree_cid, leaves_cid,
		height, size, iters, is_apple_tree)
	local x, y, z = pos.x, pos.y, pos.z
	local c_air = minetest.get_content_id("air")
	local c_ignore = minetest.get_content_id("ignore")
	local c_apple = minetest.get_content_id("default:apple")

	-- Trunk
	data[a:index(x, y, z)] = tree_cid -- Force-place lowest trunk node to replace sapling
	for yy = y-height-1,y - 1 do
		local vi = a:index(x, yy, z)
		local node_id = data[vi]
		if node_id == c_air or node_id == c_ignore or node_id == leaves_cid then
			data[vi] = tree_cid
		end
	end

	-- Force leaves near the trunk
	for z_dist = -1, 1 do
	for y_dist = -size, 1 do
		local vi = a:index(x - 1, y - height - y_dist, z + z_dist)
		for x_dist = -1, 1 do
			if data[vi] == c_air or data[vi] == c_ignore then
				if is_apple_tree and random(1, 8) == 1 then
					data[vi] = c_apple
				else
					data[vi] = leaves_cid
				end
			end
			vi = vi + 1
		end
	end
	end

	-- Randomly add leaves in 2x2x2 clusters.
	for i = 1, iters do
		local clust_x = x + random(-size, size - 1)
		local clust_y = y - height + random(-size, 0)
		local clust_z = z + random(-size, size - 1)

		for xi = 0, 1 do
		for yi = 0, 1 do
		for zi = 0, 1 do
			local vi = a:index(clust_x + xi, clust_y + yi, clust_z + zi)
			if data[vi] == c_air or data[vi] == c_ignore then
				if is_apple_tree and random(1, 8) == 1 then
					data[vi] = c_apple
				else
					data[vi] = leaves_cid
				end
			end
		end
		end
		end
	end
end

function woodnt.grow_tree(pos, is_apple_tree, bad)
	--[[
		NOTE: Tree-placing code is currently duplicated in the engine
		and in games that have saplings; both are deprecated but not
		replaced yet
	--]]
	if bad then
		error("Deprecated use of default.grow_tree")
	end

	local x, y, z = pos.x, pos.y, pos.z
	local height = random(4, 5)
	local c_tree = minetest.get_content_id("woodnt:treent")
	local c_leaves = minetest.get_content_id("woodnt:leavesnt")

	local vm = minetest.get_voxel_manip()
	local minp, maxp = vm:read_from_map(
		{x = x - 2, y = y, z = z - 2},
		{x = x + 2, y = y - height - 1, z = z + 2}
	)
	local a = VoxelArea:new({MinEdge = minp, MaxEdge = maxp})
	local data = vm:get_data()

	add_trunk_and_leaves(data, a, pos, c_tree, c_leaves, height, 2, 8, is_apple_tree)

	vm:set_data(data)
	vm:write_to_map()
	vm:update_map()
end

function woodnt.grow_sapling(pos)
	if not woodnt.can_grow(pos) then
		minetest.get_node_timer(pos):start(30)
		return
	end
	woodnt.grow_tree(pos)
end

minetest.register_node("woodnt:saplingnt",{
	description = "Saplingn't",
	tiles = {
		"default_sapling.png^[invert:rgb^[transformFY",
	},
	groups = {
		snappy = 1,
	},
	drawtype = "plantlike",
	paramtype = "light",
	on_timer = woodnt.grow_sapling,
	on_construct = function(pos)
		minetest.get_node_timer(pos):start(math.random(30, 150))
	end,
})

minetest.register_craft({
	output = "woodnt:woodnt 4",
	recipe = {{"woodnt:treent"}},
})