summaryrefslogtreecommitdiff
path: root/bushes_classic/nodes.lua
blob: e45c7e75aaa763b8727bf33215f5a27e33a914d8 (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
local S
if minetest.get_modpath("intllib") then
	S = intllib.Getter()
else
	S = function(s) return s end
end

plantlife_bushes = {}

-- TODO: add support for nodebreakers? those dig like mese picks
plantlife_bushes.after_dig_node = function(pos, oldnode, oldmetadata, digger)
	if not (digger and pos and oldnode) then
		return
	end

	-- find out which bush type we are dealing with
	local bush_name   = ""
	local can_harvest = false
	
	if oldnode.name == "bushes:fruitless_bush" then
		-- this bush has not grown fruits yet (but will eventually)
		bush_name = oldmetadata.fields.bush_type
		-- no fruits to be found, so can_harvest stays false
	else
		local name_parts = oldnode.name:split(":")
		if #name_parts >= 2 and name_parts[2] ~= nil then

			name_parts = name_parts[2]:split("_")

			if #name_parts >= 2 and name_parts[1] ~= nil then
				bush_name = name_parts[1]
				-- this bush really carries fruits
				can_harvest = true
			end
		end
	end

	-- find out which tool the digger was wielding (if any)
	local toolstack = digger:get_wielded_item()
	local capabilities = toolstack:get_tool_capabilities()

	-- what the player will get
	local harvested

	-- failure to find out what the tool can do: destroy the bush and return nothing
	local groupcaps = capabilities.groupcaps
	if not groupcaps then
		return

	-- digging with the hand or something like that
	elseif groupcaps.snappy then

		-- plant a new bush without fruits
		minetest.set_node(pos, {type = "node", name = "bushes:fruitless_bush"})
		local meta = minetest.get_meta(pos)
		meta:set_string('bush_type', bush_name)

		-- construct the stack of fruits the player will get
		-- only bushes that have grown fruits can actually give fruits
		if can_harvest then
			local amount = "4"
			harvested = "bushes:" .. bush_name .. " " .. amount
		end

	-- something like a shovel
	elseif groupcaps.crumbly then

		-- with a chance of 1/3, return 2 bushes
		local amount
		if math.random(1,3) == 1 then
			amount = "2"
		else
			amount = "1"
		end
		-- return the bush itself
		harvested = "bushes:" .. bush_name .. "_bush "..amount

	-- something like an axe
	elseif groupcaps.choppy then

		-- the amount of sticks may vary
		local amount = math.random(4, 20)
		-- return some sticks
		harvested = "default:stick " .. amount

	-- nothing known - destroy the plant
	else
		return
	end

	-- give the harvested result to the player
	if harvested then
		--minetest.chat_send_player("singleplayer","you would now get "..tostring( harvested ) );
		local itemstack = ItemStack(harvested)
		local inventory = digger:get_inventory()
		if inventory:room_for_item("main", itemstack) then
			inventory:add_item("main", itemstack)
		else
			minetest.item_drop(itemstack, digger, pos)
		end
	end
end

plantlife_bushes.after_place_node = function(pos, placer, itemstack)

	if not (itemstack and pos) then
		return
	end

	local name_parts = itemstack:get_name():split(":")
	if #name_parts < 2 or name_parts[2] == nil then
		return
	end

	name_parts = name_parts[2]:split("_")

	if #name_parts < 2 or name_parts[1] == nil then
		return
	end

	minetest.set_node(pos, {name = "bushes:fruitless_bush"})
	local meta = minetest.get_meta(pos)
	meta:set_string("bush_type", name_parts[1])
end

-- regrow berries (uses a base abm instead of biome_lib because of the use of metadata).

minetest.register_abm({
	nodenames = {"bushes:fruitless_bush"},
	neighbors = {"group:soil", "group:potting_soil"},
	interval = 500,
	chance = 5,
	action = function(pos, node, active_object_count, active_object_count_wider)

		local meta = minetest.get_meta(pos)
		local bush_name = meta:get_string("bush_type")

		if bush_name and bush_name ~= "" then
			local dirtpos = {x = pos.x, y = pos.y-1, z = pos.z}
			local dirt = minetest.get_node(dirtpos)
			local is_soil = minetest.get_item_group(dirt.name, "soil") or minetest.get_item_group(dirt.name, "potting_soil")

			if is_soil and (dirt.name == "farming:soil_wet" or math.random(1,3) == 1) then
				minetest.set_node( pos, {name = "bushes:" .. bush_name .. "_bush"})
			end
		end
	end
})

-- Define the basket and bush nodes

for i, bush_name in ipairs(bushes_classic.bushes) do

	local desc = bushes_classic.bushes_descriptions[i]

	minetest.register_node(":bushes:basket_"..bush_name, {
		description = S("Basket with "..desc.." Pies"),
		drawtype = "mesh",
		mesh = "bushes_basket_full.obj",
		tiles = {
			"bushes_basket_pie_"..bush_name..".png",
			"bushes_basket.png"
		},
		paramtype = "light",
		paramtype2 = "facedir",
		on_use = minetest.item_eat(18),
		groups = { dig_immediate = 3 },
	})

	local texture_top, texture_bottom

	local groups = {snappy = 3, bush = 1, flammable = 2, attached_node=1}
	if bush_name == "mixed_berry" then
		bush_name = "fruitless";
		desc      = S("currently fruitless");
		texture_top = "bushes_fruitless_bush_top.png"
		texture_bottom = "bushes_fruitless_bush_bottom.png"
		groups.not_in_creative_inventory = 1
	else
		texture_top = "bushes_bush_top.png"
		texture_bottom = "bushes_bush_bottom.png"
	end

	minetest.register_node(":bushes:" .. bush_name .. "_bush", {
		description = S(desc.." Bush"),
		drawtype = "mesh",
		mesh = "bushes_bush.obj",
		tiles = {"bushes_bush_"..bush_name..".png"},
		paramtype = "light",
		sunlight_propagates = true,
		walkable = false,
		groups = groups,
		sounds = default.node_sound_leaves_defaults(),
		drop = "",
		after_dig_node = function( pos, oldnode, oldmetadata, digger )
			return plantlife_bushes.after_dig_node(pos, oldnode, oldmetadata, digger);
		end,
		after_place_node = function( pos, placer, itemstack )
			return plantlife_bushes.after_place_node(pos, placer, itemstack);
		end,
	})

	-- do not spawn fruitless bushes
	if bush_name ~= "fruitless" then
		table.insert(bushes_classic.spawn_list, "bushes:"..bush_name.."_bush")
	end
end

minetest.register_node(":bushes:basket_empty", {
    description = S("Basket"),
	drawtype = "mesh",
	mesh = "bushes_basket_empty.obj",
	tiles = { "bushes_basket.png" },
	paramtype = "light",
	paramtype2 = "facedir",
    groups = { dig_immediate = 3 },
})