summaryrefslogtreecommitdiff
path: root/sapling.lua
blob: 6ace2f2c22c76dbc11e1128b3c420ad4ee566d2e (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

local S = ethereal.intllib

-- Bamboo Sprout
minetest.register_node("ethereal:bamboo_sprout", {
	description = S("Bamboo Sprout"),
	drawtype = "plantlike",
	tiles = {"bamboo_sprout.png"},
	inventory_image = "bamboo_sprout.png",
	wield_image = "bamboo_sprout.png",
	paramtype = "light",
	sunlight_propagates = true,
	walkable = false,
	groups = {
		food_bamboo_sprout = 1, snappy = 3, attached_node = 1, flammable = 2,
		dig_immediate = 3, ethereal_sapling = 1, sapling = 1,
	},
	sounds = default.node_sound_defaults(),
	selection_box = {
		type = "fixed",
		fixed = {-4 / 16, -0.5, -4 / 16, 4 / 16, 0, 4 / 16}
	},
	on_use = minetest.item_eat(2),
	grown_height = 11,
})

-- Register Saplings
ethereal.register_sapling = function(name, desc, texture, height)

	minetest.register_node(name .. "_sapling", {
		description = S(desc .. " Tree Sapling"),
		drawtype = "plantlike",
		tiles = {texture .. ".png"},
		inventory_image = texture .. ".png",
		wield_image = texture .. ".png",
		paramtype = "light",
		sunlight_propagates = true,
		is_ground_content = false,
		walkable = false,
		selection_box = {
			type = "fixed",
			fixed = {-4 / 16, -0.5, -4 / 16, 4 / 16, 7 / 16, 4 / 16}
		},
		groups = {
			snappy = 2, dig_immediate = 3, flammable = 2,
			ethereal_sapling = 1, attached_node = 1, sapling = 1
		},
		sounds = default.node_sound_leaves_defaults(),
		grown_height = height,
	})
end

ethereal.register_sapling("ethereal:willow", "Willow", "willow_sapling", 14)
ethereal.register_sapling("ethereal:yellow_tree", "Healing", "yellow_tree_sapling", 19)
ethereal.register_sapling("ethereal:big_tree", "Big", "ethereal_big_tree_sapling", 7)
ethereal.register_sapling("ethereal:banana_tree", "Banana", "banana_tree_sapling", 8)
ethereal.register_sapling("ethereal:frost_tree", "Frost", "ethereal_frost_tree_sapling", 19)
ethereal.register_sapling("ethereal:mushroom", "Mushroom", "ethereal_mushroom_sapling", 11)
ethereal.register_sapling("ethereal:palm", "Palm", "moretrees_palm_sapling", 9)
ethereal.register_sapling("ethereal:redwood", "Redwood", "redwood_sapling", 31)
ethereal.register_sapling("ethereal:orange_tree", "Orange", "orange_tree_sapling", 6)
ethereal.register_sapling("ethereal:birch", "Birch", "moretrees_birch_sapling", 7)

ethereal.add_tree = function (pos, ofx, ofy, ofz, schem)
	-- check for schematic
	if not schem then
		print (S("Schematic not found"))
		return
	end
	-- remove sapling and place schematic
	minetest.swap_node(pos, {name = "air"})
	minetest.place_schematic(
		{x = pos.x - ofx, y = pos.y - ofy, z = pos.z - ofz},
		schem, 0, nil, false)
end

local path = minetest.get_modpath("ethereal").."/schematics/"

-- grow tree functions
function ethereal.grow_yellow_tree(pos)
	ethereal.add_tree(pos, 4, 0, 4, path .. "yellowtree.mts")
end

function ethereal.grow_big_tree(pos)
	ethereal.add_tree(pos, 4, 0, 4, path .. "bigtree.mts")
end

function ethereal.grow_banana_tree(pos)
	ethereal.add_tree(pos, 3, 0, 3, ethereal.bananatree)
end

function ethereal.grow_frost_tree(pos)
	ethereal.add_tree(pos, 4, 0, 4, path .. "frosttrees.mts")
end

function ethereal.grow_mushroom_tree(pos)
	ethereal.add_tree(pos, 4, 0, 4, path .. "mushroomone.mts")
end

function ethereal.grow_palm_tree(pos)
	ethereal.add_tree(pos, 4, 0, 4, path .. "palmtree.mts")
end

function ethereal.grow_willow_tree(pos)
	ethereal.add_tree(pos, 5, 0, 5, path .. "willow.mts")
end

function ethereal.grow_redwood_tree(pos)
	if math.random(1, 2) == 1 then
		ethereal.add_tree(pos, 9, 3, 9, path .. "redwood.mts") -- shinji
	else
		ethereal.add_tree(pos, 8, 6, 8, path .. "redwood_tree.mts") -- iska
	end
end

function ethereal.grow_orange_tree(pos)
	ethereal.add_tree(pos, 1, 0, 1, ethereal.orangetree)
end

function ethereal.grow_bamboo_tree(pos)
	ethereal.add_tree(pos, 1, 0, 1, ethereal.bambootree)
end

function ethereal.grow_birch_tree(pos)
	ethereal.add_tree(pos, 2, 0, 2, ethereal.birchtree)
end

-- check if sapling has enough height room to grow
local function enough_height(pos, height)

	local nod = minetest.line_of_sight(
		{x = pos.x, y = pos.y + 1, z = pos.z},
		{x = pos.x, y = pos.y + height, z = pos.z})

	if not nod then
		return false -- obstructed
	else
		return true -- can grow
	end
end

ethereal.grow_sapling = function (pos, node)

	local under =  minetest.get_node({
		x = pos.x,
		y = pos.y - 1,
		z = pos.z
	}).name

	if not minetest.registered_nodes[node.name] then
		return
	end

	local height = minetest.registered_nodes[node.name].grown_height

	-- do we have enough height to grow sapling into tree?
	if not height or not enough_height(pos, height) then
		return
	end

	-- Check if Ethereal Sapling is growing on correct substrate
	if node.name == "ethereal:yellow_tree_sapling"
--	and under == "default:dirt_with_snow" then
	and minetest.get_item_group(under, "soil") > 0 then
		ethereal.grow_yellow_tree(pos)

	elseif node.name == "ethereal:big_tree_sapling"
	and under == "default:dirt_with_grass" then
		ethereal.grow_big_tree(pos)

	elseif node.name == "ethereal:banana_tree_sapling"
	and under == "ethereal:grove_dirt" then
		ethereal.grow_banana_tree(pos)

	elseif node.name == "ethereal:frost_tree_sapling"
	and under == "ethereal:crystal_dirt" then
		ethereal.grow_frost_tree(pos)

	elseif node.name == "ethereal:mushroom_sapling"
	and under == "ethereal:mushroom_dirt" then
		ethereal.grow_mushroom_tree(pos)

	elseif node.name == "ethereal:palm_sapling"
	and under == "default:sand" then
		ethereal.grow_palm_tree(pos)

	elseif node.name == "ethereal:willow_sapling"
	and under == "ethereal:gray_dirt" then
		ethereal.grow_willow_tree(pos)

	elseif node.name == "ethereal:redwood_sapling"
	--and under == "bakedclay:red" then
	and under == "default:dirt_with_dry_grass" then
		ethereal.grow_redwood_tree(pos)

	elseif node.name == "ethereal:orange_tree_sapling"
	and under == "ethereal:prairie_dirt" then
		ethereal.grow_orange_tree(pos)

	elseif node.name == "ethereal:bamboo_sprout"
	and under == "ethereal:bamboo_dirt" then
		ethereal.grow_bamboo_tree(pos)

	elseif node.name == "ethereal:birch_sapling"
	and under == "default:dirt_with_grass" then
		ethereal.grow_birch_tree(pos)
	end

end

-- Grow saplings
minetest.register_abm({
	label = "Ethereal grow sapling",
	nodenames = {"group:ethereal_sapling"},
	interval = 10,
	chance = 50,
	catch_up = false,
	action = function(pos, node)

		local light_level = minetest.get_node_light(pos)

		if not light_level or light_level < 13 then
			return
		end

		ethereal.grow_sapling(pos, node)
	end,
})

--[[ burn saplings
minetest.register_craft({
	type = "fuel",
	recipe = "group:ethereal_sapling",
	burntime = 10,
})
]]