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

minetest.register_node(modname .. ":smallgrass", {
	description = S("Grass"),
	tiles = {"eg_foliage_smallgrass.png"},
	drawtype = "plantlike",
	groups = {dig_immediate = 2, attached_node = 1},
	walkable = false,
	paramtype = "light",
	paramtype2 = "degrotate",
	floodable = true,
	node_placement_prediction = "",
	on_place = function(itemstack, placer, pointed_thing)
		--can only place on top of a node
		if pointed_thing.type == "node" then
			local under = vector.new(pointed_thing.above)
			under.y = under.y - 1
			local node_under = minetest.get_node(under)
			if minetest.get_item_group(node_under.name, "crumbly") > 0 then
				local param2 = math.random(90) - 1
				return minetest.item_place_node(itemstack, placer, pointed_thing, param2)
			end
		end
	end,
	after_place_node = function(pos, placer, itemstack)
		minetest.sound_play("plant_place", {pos = pos})
	end,
	sounds = {
		dig = "plant_dig",
		dug = "plant_dug",
		footstep = "plant_footstep",
		place = "plant_place"
	},
	selection_box = {
		type = "fixed",
		fixed = {-0.25, -0.5, -0.25, 0.25, 0.25, 0.25},
	},
})

minetest.register_node(modname .. ":tallgrass", {
	description = S("Grass"),
	tiles = {"eg_foliage_tallgrass.png"},
	drawtype = "plantlike",
	groups = {dig_immediate = 2},
	walkable = false,
	paramtype = "light",
	floodable = true,
	node_placement_prediction = "",
	on_place = function(itemstack, placer, pointed_thing)
		--can only place on top of a node
		if pointed_thing.type == "node" then
			local under = vector.new(pointed_thing.above)
			under.y = under.y - 1
			local node_under = minetest.get_node(under)
			if node_under.name == modname .. ":tallgrass" or
				minetest.get_item_group(node_under.name, "crumbly") > 0 then
				return minetest.item_place_node(itemstack, placer, pointed_thing)
			end
		end
	end,
	after_place_node = function(pos, placer, itemstack)
		minetest.sound_play("plant_place", {pos = pos})
	end,
	sounds = {
		dig = "plant_dig",
		dug = "plant_dug",
		footstep = "plant_footstep",
		place = "plant_place"
	},
	selection_box = {
		type = "fixed",
		fixed = {-0.25, -0.5, -0.25, 0.25, 0.25, 0.25},
	},
})

if minetest.get_modpath("eg_mapgen") then
	--register decorations
	minetest.register_decoration({
		decoration = modname .. ":smallgrass",
		deco_type = "simple",
		place_on = {"eg_mapgen:grass", "eg_mapgen:sand"},
		sidelen = 8,
		noise_params = {
			offset = 0,
			scale = 0.2,
			spread = {x = 500, y = 500, z = 500},
			seed = 405829485,
			octaves = 3,
			persist = 0.7,
			lacunarity = 16.0,
		},
		y_min = -31000,
		y_max = 31000,
		param2 = 0,
		param2_max = 89,
	})
	minetest.register_decoration({
		decoration = modname .. ":tallgrass",
		deco_type = "simple",
		place_on = {"eg_mapgen:grass", "eg_mapgen:sand"},
		sidelen = 8,
		noise_params = {
			offset = -0.2,
			scale = 0.2,
			spread = {x = 500, y = 500, z = 500},
			seed = 39184930,
			octaves = 3,
			persist = 0.7,
			lacunarity = 16.0,
		},
		y_min = -31000,
		y_max = 31000,
		height = 2,
		height_max = 5,
	})
	minetest.register_decoration({
		decoration = modname .. ":tallgrass",
		deco_type = "simple",
		place_on = {"eg_mapgen:grass", "eg_mapgen:sand"},
		spawn_by = {"mapgen_water_source", "mapgen_river_water_source"},
		num_spawn_by = 1,
		sidelen = 8,
		noise_params = {
			offset = -1,
			scale = 3,
			spread = {x = 100, y = 100, z = 100},
			seed = 94829482,
			octaves = 3,
			persist = 0.7,
			lacunarity = 16.0,
		},
		y_min = -31000,
		y_max = 31000,
		height = 3,
		height_max = 5,
	})
end
