summaryrefslogtreecommitdiff
path: root/biome_lib/growth.lua
blob: 56ba4ef7c61884fb6d7f1e6b6d29b1d0a05f98dd (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
local time_scale = ...

-- The growing ABM

function biome_lib:grow_plants(opts)

	local options = opts

	options.height_limit = options.height_limit or 5
	options.ground_nodes = options.ground_nodes or { "default:dirt_with_grass" }
	options.grow_nodes = options.grow_nodes or { "default:dirt_with_grass" }
	options.seed_diff = options.seed_diff or 0

	local n

	if type(options.grow_plant) == "table" then
		n = "multi: "..options.grow_plant[1]..", ..."
	else
		n = options.grow_plant
	end

	options.label = options.label or "biome_lib grow_plants(): "..n

	if options.grow_delay*time_scale >= 1 then
		options.interval = options.grow_delay*time_scale
	else
		options.interval = 1
	end

	minetest.register_abm({
		nodenames = { options.grow_plant },
		interval = options.interval,
		chance = options.grow_chance,
		label = options.label,
		action = function(pos, node, active_object_count, active_object_count_wider)
			local p_top = {x=pos.x, y=pos.y+1, z=pos.z}
			local p_bot = {x=pos.x, y=pos.y-1, z=pos.z}
			local n_top = minetest.get_node(p_top)
			local n_bot = minetest.get_node(p_bot)
			local root_node = minetest.get_node({x=pos.x, y=pos.y-options.height_limit, z=pos.z})
			local walldir = nil
			if options.need_wall and options.verticals_list then
				walldir = biome_lib:find_adjacent_wall(p_top, options.verticals_list, options.choose_random_wall)
			end
			if (n_top.name == "air" or n_top.name == "default:snow")
			  and (not options.need_wall or (options.need_wall and walldir)) then
				-- corner case for changing short junglegrass
				-- to dry shrub in desert
				if n_bot.name == options.dry_early_node and options.grow_plant == "junglegrass:short" then
					minetest.swap_node(pos, { name = "default:dry_shrub" })

				elseif options.grow_vertically and walldir then
					if biome_lib:search_downward(pos, options.height_limit, options.ground_nodes) then
						minetest.swap_node(p_top, { name = options.grow_plant, param2 = walldir})
					end

				elseif not options.grow_result and not options.grow_function then
					minetest.swap_node(pos, biome_lib.air)

				else
					biome_lib:replace_object(pos, options.grow_result, options.grow_function, options.facedir, options.seed_diff)
				end
			end
		end
	})
end


-- spawn_tree() on generate is routed through here so that other mods can hook
-- into it.

function biome_lib:generate_tree(pos, nodes_or_function_or_model)
	minetest.spawn_tree(pos, nodes_or_function_or_model)
end

-- and this one's for the call used in the growing code

function biome_lib:grow_tree(pos, nodes_or_function_or_model)
	minetest.spawn_tree(pos, nodes_or_function_or_model)
end