-- -------------------------------------------------------------- --
-- BIOMES
-- -------------------------------------------------------------- --

-- Helper function to create noise parameters
local function create_noise_params(offset, scale, spread, seed, octaves, persist)
    return {
        offset = offset,
        scale = scale,
        spread = spread,
        seed = seed,
        octaves = octaves,
        persist = persist
    }
end

-- Grassland
minetest.register_biome({
    name = "grassland",
    node_top = "vox_core:dirt_with_grass",
    depth_top = 1,
    node_filler = "vox_core:dirt",
    depth_filler = 3,
    node_stone = "vox_core:stone",
    node_river_water = "vox_core:river_water_source",
    node_riverbed = "vox_core:sand",
    depth_riverbed = 2,
    node_dungeon = "vox_core:cobble",
    node_dungeon_alt = "vox_core:mossycobble",
    node_dungeon_stair = "vox_core:stonebrick",
    vertical_blend = 5,  -- Smooth transition between biomes
    horizontal_blend = 6,
    y_max = 31000,
    y_min = 3,  -- Ensure grassland spawns above beach level
    heat_point = 50,
    humidity_point = 35,
    noise_params = create_noise_params(0, 1, {x = 100, y = 100, z = 100}, 12345, 3, 0.5)
})

-- Forest
minetest.register_biome({
    name = "forest",
    node_top = "vox_core:dirt_with_grass",
    depth_top = 1,
    node_filler = "vox_core:dirt",
    depth_filler = 3,
    node_stone = "vox_core:stone",
    node_river_water = "vox_core:river_water_source",
    node_riverbed = "vox_core:sand",
    depth_riverbed = 2,
    node_dungeon = "vox_core:cobble",
    node_dungeon_alt = "vox_core:mossycobble",
    node_dungeon_stair = "vox_core:stonebrick",
    vertical_blend = 4,  -- Smooth transition between biomes
    horizontal_blend = 6,
    y_max = 31000,
    y_min = 6,  -- Ensure forest spawns above beach level
    heat_point = 45,
    humidity_point = 70,
    noise_params = create_noise_params(0, 1, {x = 100, y = 100, z = 100}, 67890, 3, 0.5)
})

-- Desert
minetest.register_biome({
    name = "desert",
    node_top = "vox_core:desert_sand",
    depth_top = 1,
    node_filler = "vox_core:desert_sand",
    depth_filler = 1,
    node_stone = "vox_core:desert_stone",
    node_river_water = "vox_core:river_water_source",
    node_riverbed = "vox_core:sand",
    depth_riverbed = 2,
    node_dungeon = "vox_core:desert_stonebrick",
    node_dungeon_alt = "vox_core:desert_stone",
    node_dungeon_stair = "vox_core:desert_stonebrick",
    vertical_blend = 5,  -- Smooth transition between biomes
    horizontal_blend = 6,
    y_max = 31000,
    y_min = 1,  -- Ensure desert spawns above beach level
    heat_point = 92,
    humidity_point = 16
})

-- Tundra
minetest.register_biome({
    name = "tundra",
    node_top = "vox_core:dirt_with_snow",
    depth_top = 1,
    node_filler = "vox_core:dirt",
    depth_filler = 3,
    node_stone = "vox_core:stone",
    node_water_top = "vox_core:ice",
    depth_water_top = 10,
    node_river_water = "vox_core:river_water_source",
    node_riverbed = "vox_core:gravel",
    depth_riverbed = 2,
    node_dungeon = "vox_core:cobble",
    node_dungeon_alt = "vox_core:mossycobble",
    node_dungeon_stair = "vox_core:stonebrick",
    vertical_blend = 5,
    horizontal_blend = 6,
    y_max = 31000,
    y_min = 1,  -- Ensure tundra spawns above ocean level
    heat_point = 0,
    humidity_point = 40
})

-- Icesheet
minetest.register_biome({
    name = "icesheet",
    node_dust = "vox_core:snowblock",
    node_top = "vox_core:snowblock",
    depth_top = 1,
    node_filler = "vox_core:snowblock",
    depth_filler = 3,
    node_stone = "vox_core:cave_ice",
    node_water_top = "vox_core:ice",
    depth_water_top = 10,
    node_river_water = "vox_core:ice",
    node_riverbed = "vox_core:gravel",
    depth_riverbed = 2,
    node_dungeon = "vox_core:ice",
    node_dungeon_stair = "vox_core:stonebrick",
    y_max = 31000,
    y_min = 1,
    heat_point = 0,
    humidity_point = 73
})

-- Icesheet Ocean
minetest.register_biome({
    name = "icesheet_ocean",
    node_dust = "vox_core:snowblock",
    node_top = "vox_core:sand",
    depth_top = 1,
    node_filler = "vox_core:sand",
    depth_filler = 3,
    node_water_top = "vox_core:water_source",
    depth_water_top = 10,
    node_cave_liquid = "vox_core:water_source",
    node_riverbed = "vox_core:gravel",
    depth_riverbed = 2,
    y_max = 0,
    y_min = -255,
    heat_point = 0,
    humidity_point = 73
})

-- Tundra Under
minetest.register_biome({
    name = "tundra_under",
    node_cave_liquid = {"vox_core:water_source", "vox_core:lava_source"},
    node_dungeon = "vox_core:cobble",
    node_dungeon_alt = "vox_core:mossycobble",
    node_dungeon_stair = "vox_core:stonebrick",
    y_max = -1,  -- Ensure tundra_under spawns below ocean level
    y_min = -31000,
    heat_point = 0,
    humidity_point = 40
})

-- Tundra Highland
minetest.register_biome({
    name = "tundra_highland",
    node_dust = "vox_core:snow",
    node_riverbed = "vox_core:gravel",
    depth_riverbed = 2,
    node_dungeon = "vox_core:cobble",
    node_dungeon_alt = "vox_core:mossycobble",
    node_dungeon_stair = "vox_core:stonebrick",
    vertical_blend = 5,
    horizontal_blend = 6,
    y_max = 31000,
    y_min = 47,
    heat_point = 0,
    humidity_point = 40
})

-- Tundra Beach
minetest.register_biome({
    name = "tundra_beach",
    node_top = "vox_core:gravel",
    depth_top = 1,
    node_filler = "vox_core:gravel",
    depth_filler = 2,
    node_riverbed = "vox_core:gravel",
    depth_riverbed = 2,
    node_dungeon = "vox_core:cobble",
    node_dungeon_alt = "vox_core:mossycobble",
    node_dungeon_stair = "vox_core:stonebrick",
    vertical_blend = 5,
    horizontal_blend = 6,
    y_max = 4,
    y_min = -3,
    heat_point = 0,
    humidity_point = 40
})

-- Tundra Ocean
minetest.register_biome({
    name = "tundra_ocean",
    node_top = "vox_core:sand",
    depth_top = 1,
    node_filler = "vox_core:sand",
    depth_filler = 3,
    node_riverbed = "vox_core:gravel",
    depth_riverbed = 2,
    node_cave_liquid = "vox_core:water_source",
    node_dungeon = "vox_core:cobble",
    node_dungeon_alt = "vox_core:mossycobble",
    node_dungeon_stair = "vox_core:stonebrick",
    vertical_blend = 1,
    horizontal_blend = 6,
    y_max = 0,
    y_min = -255,
    heat_point = 0,
    humidity_point = 40
})

-- Ocean
minetest.register_biome({
    name = "ocean",
    node_top = "vox_core:sand",
    depth_top = 1,
    node_filler = "vox_core:sand",
    depth_filler = 3,
    node_water_top = "vox_core:water_source",
    depth_water_top = 10,
    node_river_water = "vox_core:river_water_source",
    node_riverbed = "vox_core:gravel",
    depth_riverbed = 4,
    y_min = -255,
    y_max = 0,  -- Ocean level at height 0
    heat_point = 50,
    humidity_point = 70
})

-- River
minetest.register_biome({
    name = "river",
    node_top = "vox_core:dirt_with_grass",
    depth_top = 1,
    node_filler = "vox_core:dirt",
    depth_filler = 3,
    node_river_water = "vox_core:river_water_source",
    node_riverbed = "vox_core:sand",
    depth_riverbed = 3,
    y_min = -2,
    y_max = 0,  -- River level at height 0
    heat_point = 50,
    humidity_point = 70
})

-- Beach
minetest.register_biome({
    name = "beach",
    node_top = "vox_core:sand",
    depth_top = 1,
    node_filler = "vox_core:sand",
    depth_filler = 3,
    y_min = 0,
    y_max = 5,  -- Allow beach to vary in height
    vertical_blend = 3,  -- Smooth transition between biomes
    horizontal_blend = 6,
    heat_point = 50,
    humidity_point = 70
})

-- Savanna
minetest.register_biome({
    name = "savanna",
    node_top = "vox_core:dry_dirt_with_dry_grass",
    depth_top = 1,
    node_filler = "vox_core:dry_dirt",
    depth_filler = 3,
    node_stone = "vox_core:desert_stone",
    node_river_water = "vox_core:river_water_source",
    node_riverbed = "vox_core:sand",
    depth_riverbed = 2,
    node_dungeon = "vox_core:cobble",
    node_dungeon_alt = "vox_core:mossycobble",
    node_dungeon_stair = "vox_core:stonebrick",
    vertical_blend = 4,
    horizontal_blend = 6,
    y_max = 31000,
    y_min = 4,
    heat_point = 85,
    humidity_point = 20
})

-- Volcanic
minetest.register_biome({
    name = "volcanic",
    node_top = "vox_core:ash_block",
    depth_top = 1,
    node_filler = "vox_core:volcanic_rock",
    depth_filler = 6,
    node_stone = "vox_core:stone",
    node_river_water = "vox_core:lava_source",
    node_riverbed = "vox_core:gravel",
    depth_riverbed = 2,
    node_dungeon = "vox_core:cobble",
    node_dungeon_alt = "vox_core:mossycobble",
    node_dungeon_stair = "vox_core:stonebrick",
    vertical_blend = 4,
    horizontal_blend = 6,
    y_max = 31000,
    y_min = 7,
    heat_point = 100,
    humidity_point = 10
})

-- Mountain
minetest.register_biome({
    name = "mountain",
    node_top = "vox_core:granite",
    depth_top = 1,
    node_filler = "vox_core:diorite",
    depth_filler = 3,
    node_stone = "vox_core:andesite",
    node_river_water = "vox_core:river_water_source",
    node_riverbed = "vox_core:gravel",
    depth_riverbed = 2,
    node_dungeon = "vox_core:cobble",
    node_dungeon_alt = "vox_core:mossycobble",
    node_dungeon_stair = "vox_core:stonebrick",
    horizontal_blend = 6,
    vertical_blend = 10,
    y_max = 31000,
    y_min = 40,
    heat_point = 30,
    humidity_point = 40
})

-- Painted Mesa
minetest.register_biome({
    name = "painted_mesa",
    node_top = "vox_core:hardened_clay",
    depth_top = 1,
    node_filler = "vox_core:hardened_clay",
    depth_filler = 3,
    node_stone = "vox_core:desert_stone",
    node_river_water = "vox_core:river_water_source",
    node_riverbed = "vox_core:sand",
    depth_riverbed = 2,
    node_dungeon = "vox_core:cobble",
    node_dungeon_alt = "vox_core:mossycobble",
    node_dungeon_stair = "vox_core:stonebrick",
    vertical_blend = 3,
    horizontal_blend = 6,
    y_max = 31000,
    y_min = 3,
    heat_point = 75,
    humidity_point = 25,
    noise_params = create_noise_params(0, 1, {x = 100, y = 100, z = 100}, 54321, 3, 0.5)
})

-- Register terracotta layers for Painted Mesa
local colors = {"red", "orange", "yellow", "brown", "white", "light_blue", "lime", "pink"}
for _, color in ipairs(colors) do
    minetest.register_biome({
        name = "painted_mesa_" .. color,
        node_top = "vox_core:teracotta_" .. color,
        depth_top = 1,
        node_filler = "vox_core:teracotta_" .. color,
        depth_filler = 3,
        node_stone = "vox_core:desert_stone",
        node_river_water = "vox_core:river_water_source",
        node_riverbed = "vox_core:sand",
        depth_riverbed = 2,
        node_dungeon = "vox_core:cobble",
        node_dungeon_alt = "vox_core:mossycobble",
        node_dungeon_stair = "vox_core:stonebrick",
        vertical_blend = 3,
        y_max = 31000,
        y_min = 6,
        heat_point = 75,
        humidity_point = 25,
        noise_params = create_noise_params(0, 1, {x = 100, y = 100, z = 100}, 98765, 3, 0.5)
    })
end