-- Biomes definition
minetest.register_biome({
		name = "deciduous_cold",
		node_top = "default:dirt_with_grass",
		depth_top = 1,
		node_filler = "default:dirt",
		node_riverbed = "default:sand",
		depth_riverbed = 2,
		node_dungeon = "default:cobble",
		node_dungeon_alt = "default:mossycobble",
		node_dungeon_stair = "default:stair_cobble",
		y_min = 3,
		y_max = 200,
		heat_point = 55,
		humidity_point = 69,
})

minetest.register_biome({
		name = "deciduous_cold_humid",
		node_top = "default:dirt_with_grass",
		depth_top = 1,
		node_filler = "default:dirt",
		node_riverbed = "default:sand",
		depth_riverbed = 2,
		node_dungeon = "default:cobble",
		node_dungeon_alt = "default:mossycobble",
		node_dungeon_stair = "default:stair_cobble",
		y_min = 3,
		y_max = 200,
		heat_point = 55,
		humidity_point = 111,
})

-- fonctions et variables de bruits
local densités = {
	{ offset = 0.000001,
	  scale = 0.00001,
	},
	{ offset = 0.00001,
	  scale = 0.0001,
	},
	{ offset = 0.0001,
	  scale = 0.001,
	},
	{ offset = 0.001,
	  scale = 0.01,
	},
}

local function noise_param(seed, spread, densité, octaves, persist)
	return {
		offset = densité.offset or 0.001,
		scale = densité.scale or 0.01,
		spread = {x = spread or 100, y = spread or 100, z = spread or 100},
		seed = seed or math.random(1,999),
		octaves = octaves or 1,
		persist = persist or 0.5,
	}
end

-- Décorations --

-- Génération des arbres de moretrees en fonction des biomes et non plus par biome_lib.
-- Pour que cette génération soit active, il faut désactiver la génération de moretrees
-- en mettant à false les arbres souhaités dans le fichier default_settings.txt du mod
-- moretrees.
if not moretrees.enable_apple_tree then
	minetest.register_decoration({
			name = "moretrees:apple_tree",
			deco_type = "simple",
			place_on = {"default:dirt_with_grass", "default:dirt_with_coniferous_litter"},
			sidelen = 16,
			noise_params = noise_param(750, 200, densités[2]),
			biomes = {"mixed_mushroom_deciduous", "deciduous_forest", "coniferous_forest", "mixed_mushroom_cold", "grassland", "deciduous_cold", "deciduous_cold_humid"},
			y_min = 0,
			y_max = 31000,
			decoration = "moretrees:apple_tree_sapling_ongen",
	})
end

if not moretrees.enable_oak then
	minetest.register_decoration({
			name = "moretrees:oak",
			deco_type = "simple",
			place_on = {"default:dirt_with_grass", "default:dirt_with_coniferous_litter", "nalc_mediterranean:dirt_with_mediterranean_grass"},
			sidelen = 16,
			noise_params = noise_param(751, 200, densités[2]),
			biomes = {"grassland", "deciduous_forest", "mixed_mushroom_deciduous", "mixed_mushroom_cold", "coniferous_forest", "mediterranean"},
			y_min = 0,
			y_max = 31000,
			decoration = "moretrees:oak_sapling_ongen",
	})
end

if not moretrees.enable_sequoia then
	minetest.register_decoration({
			name = "moretrees:sequoia",
			deco_type = "simple",
			place_on = {"default:dirt_with_grass", "default:dirt_with_coniferous_litter", "default:dirt_with_snow"},
			sidelen = 16,
			noise_params = noise_param(752, 150, densités[4], 3),
			biomes = {"grassland", "pine_forest", "coniferous_forest", "mixed_mushroom_cold", "deciduous_forest", "mixed_mushroom_deciduous", "taiga", "snow_biome_alpine"},
			y_min = 100,
			y_max = 210,
			decoration = "moretrees:sequoia_sapling_ongen",
	})	
end

if not moretrees.enable_palm then
	minetest.register_decoration({
			name = "moretrees:palm",
			deco_type = "simple",
			place_on = {"default:dirt", "default:sand", "default:desert_sand", "default:dirt_with_rainforest_litter"},
			sidelen = 16,
			noise_params = {
				offset = 0.003,
				scale = 0.01,
				spread = {x = 30, y = 30, z = 30},
				seed = 753,
				octaves = 3,
				persist = 0.5,
			},			
			biomes = {"desert", "desert_ocean", "rainforest_swamp"},
			y_min = -2,
			y_max = 3,
			decoration = "moretrees:palm_sapling_ongen",
	})		
end

if not moretrees.enable_date_palm then
	minetest.register_decoration({
			name = "moretrees:date_palm",
			deco_type = "simple",
			place_on = {"default:sand", "default:desert_sand", "default:dry_dirt_with_dry_grass"},
			sidelen = 16,
			noise_params = {
				offset = -0.003,
				scale = 0.01,
				spread = {x = 30, y = 30, z = 30},
				seed = 753,
				octaves = 3,
				persist = 0.5,
			},
			biomes = {"desert", "desert_ocean", "sandstone_desert", "sandstone_desert_ocean", "savanna_shore", "mediterranean_shore"},
			y_min = 0,
			y_max = 3,
			decoration = "moretrees:date_palm_sapling_ongen",
	})
end

if not moretrees.enable_cedar then
	minetest.register_decoration({
			name = "moretrees:cedar_rare_alt50",
			deco_type = "simple",
			place_on = {"default:dirt_with_coniferous_litter"},
			sidelen = 16,
			noise_params = {
				offset = 0.00015,
				scale = 0.0015,
				spread = {x = 130, y = 130, z = 130},
				seed = 754,
				octaves = 1,
				persist = 0.6,
			},
			biomes = {"coniferous_forest", "mixed_mushroom_cold"},
			y_min = 50,
			y_max = 31000,
			decoration = "moretrees:cedar_sapling_ongen",
	})

	minetest.register_decoration({
			name = "moretrees:cedar_rare_alt100",
			deco_type = "simple",
			place_on = {"default:dirt_with_grass", "nalc_mediterranean:dirt_with_mediterranean_grass"},
			sidelen = 16,
			noise_params = {
				offset = 0.00015,
				scale = 0.0008,
				spread = {x = 130, y = 130, z = 130},
				seed = 754,
				octaves = 1,
				persist = 0.6,
			},			
			biomes = {"deciduous_forest", "mixed_mushroom_deciduous", "mediterranean"},
			y_min = 100,
			y_max = 31000,
			decoration = "moretrees:cedar_sapling_ongen",
	})

	minetest.register_decoration({
			name = "moretrees:cedar",
			deco_type = "simple",
			place_on = {"default:dirt_with_grass", "default:dirt_with_snow"},
			sidelen = 16,
			noise_params = {
				offset = 0.00015,
				scale = 0.0015,
				spread = {x = 130, y = 130, z = 130},
				seed = 754,
				octaves = 1,
				persist = 0.6,
			},
			biomes = {"pine_forest"},
			y_min = 0,
			y_max = 31000,
			decoration = "moretrees:cedar_sapling_ongen",
	})
end

if not moretrees.enable_rubber_tree then
	minetest.register_decoration({
			name = "moretrees:rubber_tree",
			deco_type = "simple",
			place_on = {"default:dirt_with_rainforest_litter", "default:dry_dirt_with_dry_grass", "default:dirt", "default:dry_dirt"},
			sidelen = 16,
			noise_params = noise_param(754, 130, densités[2]),
			biomes = {"rainforest", "rainforest_swamp", "mixed_mushroom_hot", "savanna", "savanna_shore"},
			y_min = -1,
			y_max = 31000,
			decoration = "moretrees:rubber_tree_sapling_ongen",
	})
end

if not moretrees.enable_willow then
	minetest.register_decoration({
			name = "moretrees:willow_rare",
			deco_type = "simple",
			place_on = {"default:dirt_with_grass"},
			sidelen = 16,
			noise_params = noise_param(755, 130, densités[2]),
			biomes = {"deciduous_forest", "mixed_mushroom_deciduous"},
			y_min = 0,
			y_max = 31000,
			decoration = "moretrees:willow_sapling_ongen",
	})	

	minetest.register_decoration({
			name = "moretrees:willow",
			deco_type = "simple",
			place_on = {"default:dirt_with_grass"},
			sidelen = 16,
			noise_params = noise_param(755, 100, densités[3]),
			biomes = {"deciduous_cold", "deciduous_cold_humid"},
			y_min = 0,
			y_max = 31000,
			decoration = "moretrees:willow_sapling_ongen",
	})	
end

if not moretrees.enable_birch then
	minetest.register_decoration({
			name = "moretrees:birch",
			deco_type = "simple",
			place_on = {"default:dirt_with_grass", "default:dirt_with_coniferous_litter"},
			sidelen = 16,
			noise_params = noise_param(756, 150, densités[3]),
			biomes = {"coniferous_forest", "mixed_mushroom_cold", "grassland", "deciduous_forest", "mixed_mushroom_deciduous"},
			y_min = 0,
			y_max = 31000,
			decoration = "moretrees:birch_sapling_ongen",
	})	
end

if not moretrees.enable_spruce then
	minetest.register_decoration({
			name = "moretrees:spruce_rare_alt40-200",
			deco_type = "simple",
			place_on = {"default:dirt_with_grass"},
			sidelen = 16,
			noise_params = noise_param(757, 250, densités[2], 2),
			biomes = {"deciduous_forest", "mixed_mushroom_deciduous"},
			y_min = 40,
			y_max = 200,
			decoration = "moretrees:spruce_sapling_ongen",
	})

	minetest.register_decoration({
			name = "moretrees:spruce",
			deco_type = "simple",
			place_on = {"default:dirt_with_snow", "default:dirt_with_grass"},
			sidelen = 16,
			noise_params = noise_param(757, 250, densités[3], 2),
			biomes = {"coniferous_forest", "mixed_mushroom_cold", "taiga", "snow_biome_forest", "snow_biome_lush", "pine_forest"},
			y_min = 0,
			y_max = 31000,
			decoration = "moretrees:spruce_sapling_ongen",
	})
end

if not moretrees.enable_fir then
	minetest.register_decoration({
			name = "moretrees:fir_rare_alt40-200",
			deco_type = "simple",
			place_on = {"default:dirt_with_grass"},
			sidelen = 16,
			noise_params = noise_param(758, 250, densités[2], 2),
			biomes = {"deciduous_forest", "mixed_mushroom_deciduous"},
			y_min = 40,
			y_max = 200,
			decoration = "moretrees:fir_sapling_ongen",
	})

	minetest.register_decoration({
			name = "moretrees:fir",
			deco_type = "simple",
			place_on = {"default:dirt_with_snow", "default:dirt_with_grass", "default:dirt_with_coniferous_litter"},
			sidelen = 16,
			noise_params = noise_param(758, 250, densités[3], 2),
			biomes = {"snow_biome_forest", "taiga", "snow_biome_lush", "pine_forest", "coniferous_forest", "mixed_mushroom_cold"},
			y_min = 0,
			y_max = 31000,
			decoration = "moretrees:fir_sapling_ongen",
	})
end

if not moretrees.enable_poplar then
	minetest.register_decoration({
			name = "moretrees:poplar_small_rare",
			deco_type = "simple",
			place_on = {"default:dirt_with_grass"},
			sidelen = 16,
			noise_params = noise_param(759, 250, densités[2], 3),
			biomes = {"deciduous_forest", "mixed_mushroom_deciduous", "deciduous_cold"},
			y_min = 0,
			y_max = 31000,
			decoration = "moretrees:poplar_small_sapling_ongen",
	})	

	minetest.register_decoration({
			name = "moretrees:poplar_small_dense",
			deco_type = "simple",
			place_on = {"default:dirt_with_grass"},
			sidelen = 16,
			noise_params = noise_param(759, 100, densités[4], 3),
			biomes = {"deciduous_cold_humid"},
			y_min = 0,
			y_max = 31000,
			decoration = "moretrees:poplar_small_sapling_ongen",
	})	

	minetest.register_decoration({
			name = "moretrees:poplar",
			deco_type = "simple",
			place_on = {"default:dirt_with_grass"},
			sidelen = 16,
			noise_params = noise_param(760, 50, densités[3], 2),
			biomes = {"deciduous_cold", "deciduous_cold_humid"},
			y_min = 0,
			y_max = 31000,
			decoration = "moretrees:poplar_sapling_ongen",
	})	
end

-- /!\ La génération des Jungletrees n'est pas redéfinie ici mais est laissée à moretrees.
-- Mettez à true (ou false) dans default_settings.txt de moretrees les jungletrees si vous
-- voulez (ou non) que moretrees génère des Jungletrees supplémentaires.

-- Grass decorations
local function register_grass_decoration(offset, scale, length)
	minetest.register_decoration({
		name = "nalc_moretrees:grass_" .. length,
		deco_type = "simple",
		place_on = {"default:dirt_with_grass"},
		sidelen = 16,
		noise_params = {
			offset = offset,
			scale = scale,
			spread = {x = 200, y = 200, z = 200},
			seed = 329,
			octaves = 3,
			persist = 0.6
		},
		biomes = {"deciduous_cold", "deciduous_cold_humid"},
		y_max = 31000,
		y_min = 1,
		decoration = "default:grass_" .. length,
	})
end

register_grass_decoration(-0.03,  0.09,  5)
register_grass_decoration(-0.015, 0.075, 4)
register_grass_decoration(0,      0.06,  3)
register_grass_decoration(0.015,  0.045, 2)
register_grass_decoration(0.03,   0.03,  1)

-- Redefinir les feuilles avec l'apparence plantlike si configuré dans les paramètres
-- (Améliore les performances des clients)
if minetest.settings:get_bool("nalc_moretrees.plantlike_leaves") or moretrees.plantlike_leaves then
	local function o_leaves(name, texture)
		minetest.override_item(name, {
			inventory_image = minetest.inventorycube(texture),
			drawtype = "plantlike",
			visual_scale = math.sqrt(2)
		})
	end

	o_leaves("default:blueberry_bush_leaves", "default_blueberry_bush_leaves.png")
	o_leaves("default:blueberry_bush_leaves_with_berries", "default_blueberry_bush_leaves.png^default_blueberry_overlay.png")
	o_leaves("default:bush_leaves", "default_leaves.png")
	o_leaves("default:aspen_leaves", "default_aspen_leaves.png")
	o_leaves("default:pine_needles", "default_pine_needles.png")
	o_leaves("default:pine_bush_needles", "default_pine_needles.png")
	o_leaves("default:acacia_leaves", "default_acacia_leaves.png")
	o_leaves("default:acacia_bush_leaves", "default_acacia_leaves.png")
	o_leaves("nalc_mediterranean:needles", "default_pine_needles.png")
	o_leaves("nalc_mediterranean:olive_leaves", "feuilles_olivier.png")
	o_leaves("snow:needles", "default_pine_needles.png")
end

minetest.register_alias("moretrees:acacia_sapling_ongen", "default:acacia_sapling")

minetest.log("action", "[nalc_moretrees] loaded.")

