--
-- Aliases for map generators
--

-- All mapgens

minetest.register_alias("mapgen_stone", "default:stone")
minetest.register_alias("mapgen_water_source", "default:water_source")
minetest.register_alias("mapgen_river_water_source", "default:river_water_source")

-- smaller biomes

minetest.set_noiseparams("heat", {
    offset = 5,
    scale = 5,
    spread = {x = 10, y = 10, z = 10},
    octaves = 3,
    persistence = 0.5,
})

minetest.set_noiseparams("humidity", {
    offset = 5,
    scale = 5,
    spread = {x = 10, y = 10, z = 10},
    octaves = 3,
    persistence = 0.5,
})

function default.register_ores()
	-- Sandstone

	minetest.register_ore({
		ore_type = "stratum",
		ore = "default:sandstone",
		wherein = { "default:desert_stone" },
		clust_scarcity = 1,
		y_max = 39,
		y_min = 3,
		noise_params = {
			offset = 21,
			scale = 16,
			spread = { x = 128, y = 128, z = 128 },
			seed = 90122,
			octaves = 1,
		},
		stratum_thickness = 2,
		biomes = { "desert" },
	})

	-- Blob ore.
	-- These before scatter ores to avoid other ores in blobs.

	-- Clay

	minetest.register_ore({
		ore_type = "blob",
		ore = "default:clay",
		wherein = { "default:sand" },
		clust_scarcity = 16 * 16 * 16,
		clust_size = 5,
		y_max = 0,
		y_min = -15,
		noise_threshold = 0.0,
		noise_params = {
			offset = 0.5,
			scale = 0.2,
			spread = { x = 5, y = 5, z = 5 },
			seed = -316,
			octaves = 1,
			persist = 0.0,
		},
	})

	-- Dirt

	minetest.register_ore({
		ore_type = "blob",
		ore = "default:dirt",
		wherein = { "default:stone" },
		clust_scarcity = 16 * 16 * 16,
		clust_size = 5,
		y_max = 31000,
		y_min = -31,
		noise_threshold = 0.0,
		noise_params = {
			offset = 0.5,
			scale = 0.2,
			spread = { x = 5, y = 5, z = 5 },
			seed = 17676,
			octaves = 1,
			persist = 0.0,
		},
		-- Only where default:dirt is present as surface material
		biomes = {
			"forest",
			"plains",
		},
	})

	-- Gravel

	minetest.register_ore({
		ore_type = "blob",
		ore = "default:gravel",
		wherein = { "default:stone" },
		clust_scarcity = 16 * 16 * 16,
		clust_size = 5,
		y_max = 31000,
		y_min = -31000,
		noise_threshold = 0.0,
		noise_params = {
			offset = 0.5,
			scale = 0.2,
			spread = { x = 5, y = 5, z = 5 },
			seed = 766,
			octaves = 1,
			persist = 0.0,
		},
	})
end

--
-- Register biomes
--

function default.register_biomes()
	-- Forest
	minetest.register_biome({
		name = "forest",
		node_top = "default:dirt_with_grass",
		depth_top = 1,
		node_filler = "default:dirt",
		depth_filler = 1,
		node_riverbed = "default:sand",
		depth_riverbed = 2,
		y_max = 31000,
		y_min = 6,
		heat_point = 60,
		humidity_point = 50,
	})
	-- Plains

	minetest.register_biome({
		name = "plains",
		node_top = "default:dirt_with_grass",
		depth_top = 1,
		node_filler = "default:dirt",
		depth_filler = 1,
		node_riverbed = "default:sand",
		depth_riverbed = 2,
		y_max = 31000,
		y_min = 6,
		heat_point = 50,
		humidity_point = 35,
	})

	minetest.register_biome({
		name = "plains_dunes",
		node_top = "default:sand",
		depth_top = 1,
		node_filler = "default:sand",
		depth_filler = 2,
		node_riverbed = "default:sand",
		depth_riverbed = 2,
		vertical_blend = 1,
		y_max = 5,
		y_min = 4,
		heat_point = 50,
		humidity_point = 35,
	})

	minetest.register_biome({
		name = "plains_ocean",
		node_top = "default:sand",
		depth_top = 1,
		node_filler = "default:sand",
		depth_filler = 3,
		node_riverbed = "default:sand",
		depth_riverbed = 2,
		node_cave_liquid = "default:water_source",
		y_max = 3,
		y_min = -255,
		heat_point = 50,
		humidity_point = 35,
	})

	minetest.register_biome({
		name = "plains_under",
		node_cave_liquid = { "default:water_source", "default:lava_source" },
		y_max = -256,
		y_min = -31000,
		heat_point = 50,
		humidity_point = 35,
	})

	-- Desert

	minetest.register_biome({
		name = "desert",
		node_top = "default:desert_sand",
		depth_top = 1,
		node_filler = "default:desert_sand",
		depth_filler = 1,
		node_stone = "default:desert_stone",
		node_riverbed = "default:sand",
		depth_riverbed = 2,
		y_max = 31000,
		y_min = 4,
		heat_point = 92,
		humidity_point = 16,
	})

	minetest.register_biome({
		name = "desert_ocean",
		node_top = "default:sand",
		depth_top = 1,
		node_filler = "default:sand",
		depth_filler = 3,
		node_stone = "default:desert_stone",
		node_riverbed = "default:sand",
		depth_riverbed = 2,
		node_cave_liquid = "default:water_source",
		vertical_blend = 1,
		y_max = 3,
		y_min = -255,
		heat_point = 92,
		humidity_point = 16,
	})

	minetest.register_biome({
		name = "desert_under",
		node_cave_liquid = { "default:water_source", "default:lava_source" },
		y_max = -256,
		y_min = -31000,
		heat_point = 92,
		humidity_point = 16,
	})
end

--
-- Register decorations
--

function default.register_decorations()
	-- Bush
	minetest.register_decoration({
		name = "default:tree",
		deco_type = "schematic",
		place_on = { "default:dirt_with_grass" },
		sidelen = 64,
		noise_params = {
			offset = 0.001,
			scale = 0.00001,
			spread = { x = 1000, y = 1000, z = 1000 },
			seed = 2,
			octaves = 3,
			persist = 0.96,
		},
		biomes = { "forest" },
		y_max = 31000,
		y_min = 1,
		schematic = minetest.get_modpath("mapgen") .. "/schematics/bush.mts",
		flags = "place_center_x, place_center_z",
		rotation = "random",
	})

	-- Apple tree and log

	minetest.register_decoration({
		name = "default:tree",
		deco_type = "schematic",
		place_on = { "default:dirt_with_grass" },
		sidelen = 64,
		noise_params = {
			offset = 0.001,
			scale = 0.00001,
			spread = { x = 1000, y = 1000, z = 1000 },
			seed = 2,
			octaves = 3,
			persist = 0.96,
		},
		biomes = { "forest" },
		y_max = 31000,
		y_min = 1,
		schematic = minetest.get_modpath("cl_default") .. "/schematics/tree.mts",
		flags = "place_center_x, place_center_z",
		rotation = "random",
	})
	minetest.register_decoration({
		name = "default:tree1",
		deco_type = "schematic",
		place_on = { "default:dirt_with_grass" },
		sidelen = 64,
		fill_ratio = 0.0001,
		noise_params = {
			offset = 0.001,
			scale = 0.00001,
			spread = { x = 1000, y = 1000, z = 1000 },
			seed = 2,
			octaves = 3,
			persist = 0.96,
		},
		biomes = { "forest" },
		y_max = 31000,
		y_min = 1,
		schematic = minetest.get_modpath("cl_default") .. "/schematics/tree1.mts",
		flags = "place_center_x, place_center_z",
		rotation = "random",
	})
	minetest.register_decoration({
		name = "default:tree2",
		deco_type = "schematic",
		place_on = { "default:dirt_with_grass" },
		sidelen = 64,
		fill_ratio = 0.0001,
		noise_params = {
			offset = 0.001,
			scale = 0.00001,
			spread = { x = 1000, y = 1000, z = 1000 },
			seed = 2,
			octaves = 3,
			persist = 0.96,
		},
		biomes = { "forest" },
		y_max = 31000,
		y_min = 1,
		schematic = minetest.get_modpath("cl_default") .. "/schematics/tree2.mts",
		flags = "place_center_x, place_center_z",
		rotation = "random",
	})

	-- Sugar cane

	minetest.register_decoration({
		name = "farming:sugar_cane",
		deco_type = "simple",
		place_on = { "default:dirt", "default:dirt_with_grass" },
		sidelen = 16,
		noise_params = {
			offset = -0.0003,
			scale = 0.0009,
			spread = { x = 200, y = 200, z = 200 },
			seed = 230,
			octaves = 3,
			persist = 0.6,
		},
		biomes = { "plains" },
		y_max = 31000,
		y_min = 4,
		decoration = "farming:sugar_cane",
		spawn_by = "default:water",
		height = 2,
		height_max = 5,
	})

	-- Bush

	minetest.register_decoration({
		name = "farming:bush",
		deco_type = "simple",
		place_on = { "default:dirt", "default:dirt_with_grass" },
		sidelen = 16,
		noise_params = {
			offset = -0.00003,
			scale = 0.09,
			spread = { x = 1, y = 1, z = 1 },
			seed = 230,
			octaves = 3,
			persist = 0.6,
		},
		biomes = { "plains", "forest" },
		y_max = 31000,
		y_min = 4,
		decoration = "farming:bush",
		height = 1,
		height_max = 1,
	})

	-- Cactus

	minetest.register_decoration({
		name = "farming:cactus",
		deco_type = "simple",
		place_on = { "default:desert_sand" },
		sidelen = 16,
		noise_params = {
			offset = -0.0003,
			scale = 0.0009,
			spread = { x = 200, y = 200, z = 200 },
			seed = 230,
			octaves = 3,
			persist = 0.6,
		},
		biomes = { "desert" },
		y_max = 31000,
		y_min = 4,
		decoration = "farming:cactus",
		height = 2,
		height_max = 5,
	})
end

default.register_biomes()
default.register_ores()
default.register_decorations()
