summaryrefslogtreecommitdiff
path: root/polebean.lua
blob: afe8fb11bf6e15ad3772894a1dfaa1a8e00b5081 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

--[[

Copyright (C) 2015 - Auke Kok <sofar@foo-projects.org>

"crops" is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1
of the license, or (at your option) any later version.

--]]

-- Intllib
local S = crops.intllib

minetest.register_craft({
	output = "crops:beanpoles",
	recipe = {
		{'', '', ''},
		{'default:stick', '', 'default:stick'},
		{'default:stick', '', 'default:stick'},
	}
})

minetest.register_craftitem("crops:green_bean", {
	description = S("Green Bean"),
	inventory_image = "crops_green_bean.png",
	on_use = minetest.item_eat(1)
})

minetest.register_craft({
	type = "shapeless",
	output = "crops:green_bean_seed",
	recipe = { "crops:green_bean" }
})

local function crops_beanpole_on_dig(pos, node, digger)
	local bottom
	local bottom_n
	local top
	local top_n
	local drops = {}

	if node.name == "crops:beanpole_base" or
	   node.name == "crops:beanpole_plant_base_1" or
	   node.name == "crops:beanpole_plant_base_2" or
	   node.name == "crops:beanpole_plant_base_3" or --grown tall enough for top section
	   node.name == "crops:beanpole_plant_base_4" or --flowering
	   node.name == "crops:beanpole_plant_base_5" or --ripe
	   node.name == "crops:beanpole_plant_base_6"    --harvested
	   then
		bottom = pos
		bottom_n = node
		top = { x = pos.x, y = pos.y + 1, z = pos.z }
		top_n = minetest.get_node(top)
	elseif node.name == "crops:beanpole_top" or
	       node.name == "crops:beanpole_plant_top_1" or
	       node.name == "crops:beanpole_plant_top_2" or --flowering
	       node.name == "crops:beanpole_plant_top_3" or --ripe
	       node.name == "crops:beanpole_plant_top_4"    --harvested
	       then
		top = pos
		top_n = node
		bottom = { x = pos.x, y = pos.y - 1, z = pos.z }
		bottom_n = minetest.get_node(bottom)
	else
		-- ouch, this shouldn't happen
		print("beanpole on_dig falsely attached to: " .. pos.x .. "," .. pos.y .. "," .. pos.z)
		return
	end

	if bottom_n.name == "crops:beanpole_base" and top_n.name == "crops:beanpole_top" then
		-- bare beanpole
		table.insert(drops, "crops:beanpoles")
		minetest.remove_node(bottom)
		minetest.remove_node(top)
	elseif (
		bottom_n.name == "crops:beanpole_plant_base_1" or
		bottom_n.name == "crops:beanpole_plant_base_2" or
		bottom_n.name == "crops:beanpole_plant_base_3" or
		bottom_n.name == "crops:beanpole_plant_base_4"
		) and (
		top_n.name == "crops:beanpole_top" or
		top_n.name == "crops:beanpole_plant_top_1" or
		top_n.name == "crops:beanpole_plant_top_2"
		) then
		-- non-ripe
		for i = 1,4 do
			table.insert(drops, "default:stick")
		end
		minetest.set_node(bottom, { name = "crops:beanpole_base"})
		minetest.set_node(top, { name = "crops:beanpole_top"})
	elseif bottom_n.name == "crops:beanpole_plant_base_5" and top_n.name == "crops:beanpole_plant_top_3" then
		-- ripe beanpole
		local meta = minetest.get_meta(bottom)
		local damage = meta:get_int("crops_damage")
		--   0 - 3-7
		--  50 - 2-4
		-- 100 - 1-1
		for i = 1,math.random(3 - (2 * (damage / 100)),7 - (6 * (damage / 100))) do
			table.insert(drops, "crops:green_bean")
		end
		crops.die(bottom)
	elseif bottom_n.name == "crops:beanpole_plant_base_6" and top_n.name == "crops:beanpole_plant_top_4" then
		-- harvested beans
		for i = 1,math.random(3,4) do
			table.insert(drops, "default:stick")
		end
		minetest.remove_node(bottom)
		minetest.remove_node(top)
	else
		-- ouch, this shouldn't happen
		print("beanpole on_dig can't handle blocks at to: " ..
				bottom.x .. "," .. bottom.y .. "," .. bottom.z ..
				" and " .. top.x .. "," .. top.y .. "," .. top.z)
		print("removing a " .. node.name .. " at " ..
				pos.x .. "," .. pos.y .. "," .. pos.z)
		minetest.remove_node(pos)
		return
	end

	core.handle_node_drops(pos, drops, digger)
end

minetest.register_node("crops:beanpole_base", {
	description = "",
	drawtype = "plantlike",
	tiles = { "crops_beanpole_base.png" },
	use_texture_alpha = true,
	walkable = true,
	sunlight_propagates = true,
	paramtype = "light",
	groups = { snappy=3,flammable=3,flora=1,attached_node=1,not_in_creative_inventory=1 },
	drop = {},
	sounds = default.node_sound_leaves_defaults(),
	on_dig = crops_beanpole_on_dig,
})

minetest.register_node("crops:beanpole_top", {
	description = "",
	drawtype = "plantlike",
	tiles = { "crops_beanpole_top.png" },
	use_texture_alpha = true,
	walkable = true,
	sunlight_propagates = true,
	paramtype = "light",
	groups = { snappy=3,flammable=3,flora=1,not_in_creative_inventory=1 },
	drop = {},
	sounds = default.node_sound_leaves_defaults(),
	on_dig = crops_beanpole_on_dig,
})

minetest.register_node("crops:beanpoles", {
	description = S("Beanpoles"),
	inventory_image = "crops_beanpole_top.png",
	wield_image = "crops_beanpole_top.png",
	tiles = { "crops_beanpole_base.png" },
	drawtype = "plantlike",
	sunlight_propagates = true,
	use_texture_alpha = true,
	paramtype = "light",
	groups = { snappy=3,flammable=3,flora=1,attached_node=1 },
	drop = {},
	sounds = default.node_sound_leaves_defaults(),
	node_placement_prediction = "crops:beanpole_base",

	on_place = function(itemstack, placer, pointed_thing)
                local under = minetest.get_node(pointed_thing.under)
		if minetest.get_item_group(under.name, "soil") <= 1 then
			return
		end
		local top = { x = pointed_thing.above.x, y = pointed_thing.above.y + 1, z = pointed_thing.above.z }
		if not minetest.get_node(top).name == "air" then
			return
		end
		minetest.set_node(pointed_thing.above, {name="crops:beanpole_base"})
		minetest.set_node(top, {name="crops:beanpole_top"})
		if not minetest.settings:get_bool("creative_mode") then
			itemstack:take_item()
		end
		return itemstack
	end
})

minetest.register_craftitem("crops:green_bean_seed", {
	description = S("Green bean seed"),
	inventory_image = "crops_green_bean_seed.png",
	wield_image = "crops_green_bean_seed.png",
	node_placement_prediction = "", -- disabled, prediction assumes pointed_think.above!

	on_place = function(itemstack, placer, pointed_thing)
		local under = minetest.get_node(pointed_thing.under)
		if under.name == "crops:beanpole_base" then
			crops.plant(pointed_thing.under, {name="crops:beanpole_plant_base_1"})
			local above = { x = pointed_thing.under.x, y = pointed_thing.under.y + 1, z = pointed_thing.under.z}
			local meta = minetest.get_meta(above)
			meta:set_int("crops_top_half", 1)
		elseif under.name == "crops:beanpole_top" then
			local below = { x = pointed_thing.under.x, y = pointed_thing.under.y - 1, z = pointed_thing.under.z }
			if minetest.get_node(below).name == "crops:beanpole_base" then
				crops.plant(below, {name="crops:beanpole_plant_base_1"})
				local meta = minetest.get_meta(pointed_thing.under)
				meta:set_int("crops_top_half", 1)
			else
				return
			end
		else
			return
		end
		if not minetest.settings:get_bool("creative_mode") then
			itemstack:take_item()
		end
		return itemstack
	end
})

for stage = 1,6 do
minetest.register_node("crops:beanpole_plant_base_" .. stage, {
	description = S("Green Bean plant"),
	tiles = { "crops_beanpole_plant_base_" .. stage .. ".png" },
	drawtype = "plantlike",
	sunlight_propagates = true,
	use_texture_alpha = true,
	paramtype = "light",
	walkable = false,
	groups = { snappy=3,flammable=3,flora=1,attached_node=1,not_in_creative_inventory=1 },
	drop = {},
	sounds = default.node_sound_leaves_defaults(),
	on_dig = crops_beanpole_on_dig
})
end

for stage = 1,4 do
minetest.register_node("crops:beanpole_plant_top_" .. stage, {
	description = S("Green Bean plant"),
	tiles = { "crops_beanpole_plant_top_" .. stage .. ".png" },
	drawtype = "plantlike",
	sunlight_propagates = true,
	use_texture_alpha = true,
	paramtype = "light",
	walkable = true,
	groups = { snappy=3,flammable=3,flora=1,not_in_creative_inventory=1 },
	drop = {},
	sounds = default.node_sound_leaves_defaults(),
	on_dig = crops_beanpole_on_dig
})
end

minetest.register_abm({
	nodenames = {
		"crops:beanpole_plant_base_1",
		"crops:beanpole_plant_base_2",
		"crops:beanpole_plant_base_3",
		"crops:beanpole_plant_base_4"
	},
	interval = crops.settings.interval,
	chance = crops.settings.chance,
	neighbors = { "group:soil" },
	action = function(pos, node, active_object_count, active_object_count_wider)
		if not crops.can_grow(pos) then
			return
		end
		if node.name == "crops:beanpole_plant_base_1" then
			minetest.swap_node(pos, { name = "crops:beanpole_plant_base_2"})
		elseif node.name == "crops:beanpole_plant_base_2" then
			minetest.swap_node(pos, { name = "crops:beanpole_plant_base_3"})
		elseif node.name == "crops:beanpole_plant_base_3" then
			local apos = {x = pos.x, y = pos.y + 1, z = pos.z}
			local above = minetest.get_node(apos)
			if above.name == "crops:beanpole_top" then
				minetest.set_node(apos, { name = "crops:beanpole_plant_top_1" })
				local meta = minetest.get_meta(apos)
				meta:set_int("crops_top_half", 1)
			elseif above.name == "crops:beanpole_plant_top_1" then
				minetest.swap_node(pos, { name = "crops:beanpole_plant_base_4" })
				minetest.swap_node(apos, { name = "crops:beanpole_plant_top_2" })
			end
		elseif node.name == "crops:beanpole_plant_base_4" then
			local apos = {x = pos.x, y = pos.y + 1, z = pos.z}
			minetest.swap_node(pos, { name = "crops:beanpole_plant_base_5" })
			minetest.swap_node(apos, { name = "crops:beanpole_plant_top_3" })
		end
	end
})

crops.beanpole_die = function(pos)
	minetest.set_node(pos, { name = "crops:beanpole_plant_base_6" })
	local above = {x = pos.x, y = pos.y + 1, z = pos.z}
	minetest.set_node(above, { name = "crops:beanpole_plant_top_4" })
end

local properties = {
	die = crops.beanpole_die,
	waterstart = 30,
	wateruse = 1,
	night = 5,
	soak = 60,
	soak_damage = 75,
	wither = 25,
	wither_damage = 15,
	doublesize = true,
}

crops.register({ name = "crops:beanpole_plant_base_1", properties = properties })
crops.register({ name = "crops:beanpole_plant_base_2", properties = properties })
crops.register({ name = "crops:beanpole_plant_base_3", properties = properties })
crops.register({ name = "crops:beanpole_plant_base_4", properties = properties })
crops.register({ name = "crops:beanpole_plant_base_5", properties = properties })