local modname = minetest.get_current_modname()
local S = minetest.get_translator(modname)

minetest.register_node(modname .. ":bamboo",
	{
		tiles = {modname .. "_stalk.png"},
		description = S("Bamboo"),
		drawtype = "nodebox",
		groups = {choppy = 3},
		climbable = true,
		sunlight_propagates = true,
		walkable = true,
		is_ground_content = false,
		paramtype = "light",
		sounds =
		{
			dig = "wood_high_dig",
			dug = "wood_high_dug",
			footstep = "wood_high_footstep",
			place = "wood_high_place",
			
		},
		node_box =
		{
			type = "fixed",
			fixed =
			{
				{-2 / 16, -0.5, -2 / 16, 2 / 16, 0.5, 2 / 16},
            },
		}
	})

minetest.register_abm(
	{
		label = "Bamboo growing",
		nodenames = {modname .. ":bamboo"},
		interval = 5.0,
		chance = 60,
		catch_up = true,
		action = function(pos)
			local above = {x = pos.x, y = pos.y + 1, z = pos.z}
			local above_node = minetest.get_node(above)
			if above_node.name == "air"
			then
				--pos is at top node of bamboo stalk
				local y = pos.y - 1
				local stalk
				local height = 0
				repeat
					stalk = minetest.get_node({x = pos.x, y = y, z = pos.z})
					y = y - 1
					height = height + 1
				until stalk.name ~= modname .. ":bamboo" or height > 10
				if height <= 10
				then
					minetest.after(0.1, minetest.set_node, above, {name = modname .. ":bamboo"})
				end
			end
		end,
	})

if minetest.get_modpath("eg_mapgen")
then
	minetest.register_decoration(
		{
			deco_type = "simple",
			place_on = "eg_mapgen:grass",
			sidelen = 8,
			noise_params =
			{
				offset = -0.6,
				scale = -0.5,
				seed = 490249204,
				spread = {x = 100, y = 100, z = 100},
				octaves = 3,
				persistence = 0.7,
				lacunarity = 2.0,
			},
			decoration = modname .. ":bamboo",
			height = 1,
			height_max = 11,
		})
end
