local mod_name = minetest.get_current_modname()
local mod_path = minetest.get_modpath(mod_name)
local S = minetest.get_translator(mod_name)

br_core.level = {}
br_core.level_for_index = {}

local function sch(name)
    return (mod_path .. DIR_DELIM .. "schematics" .. DIR_DELIM .. name .. ".mts")
end

function br_core.register_level(p)
    if p.level > 180 then error("CANNOT REGISTER LEVEL "..p.level.." AS IT WOULD EXCEED HEIGHT LIMIT") end
    p.index = #br_core.level_for_index+1
    br_core.level[p.level] = p
    br_core.level_for_index[#br_core.level_for_index+1] = p.level
end
function br_core.register_biome(p)
    p.vert_schems = {}
    p.segheight = p.segheight or 80
    for i, schem in pairs(p.schems) do
        local all_layers = false
        if not schem.vertical_segments then
            schem.vertical_segments = {}
            all_layers = true
        end
        for k=0, math.floor(br_core.chunk_width / p.segheight) + 1 do
            if not p.vert_schems[k] then p.vert_schems[k] = {} end
            if all_layers or schem.vertical_segments[k] == 1 then
                for l=1, schem.prevalence or 1 do
                    p.vert_schems[k][#p.vert_schems[k]+1] = p.schems[i]
                end
            end
        end
    end
    if not br_core.level[p.level].biome then br_core.level[p.level].biome = {} end
    for i=1, p.prevalence or 1 do
        br_core.level[p.level].biome[#br_core.level[p.level].biome+1] = p
    end
end
function br_core.index_to_level(index)
    return br_core.level_for_index[index]
end

function br_core.sort_levels()
    table.sort(br_core.level_for_index, function(a, b) return a < b end)
    for i, level in pairs(br_core.level_for_index) do
        br_core.level[level].index = i
    end
end

br_core.register_level({
    level = 0,
    desc = "Level 0",
    danger = 1,
    grace_dist = 5,
    biome = {},
    segsize = 20,
    base_height = 28,
    sky = {
        clouds = false,
        base_color = "#001",
        type = "plain"
    },
})

br_core.register_biome({
    level = 0,
    desc = "Level 0 columns",
    danger = 1,
    on_generate = nil,
    segheight = 20,
    can_generate = function(pos) -- must be deterministic
        if vector.distance(pos, vector.new(0, pos.y, 0)) > 10 then
            return false
        else
            return true
        end
    end,
    schems = {
        {name=sch("0_20_1_columns_0"), vertical_segments={1,0}},
        {name=sch("0_20_2_columns_0"), vertical_segments={0,1}, rotation="0"},
    }
})
br_core.register_biome({
    level = 0,
    desc = "Level 0 main",
    danger = 1,
    on_generate = nil,
    segheight = 20,
    prevalence = 2,
    schems = {
        {name=sch("0_20_1_columns_0"), vertical_segments={1,0}},
        --
        {name=sch("0_20_2_columns_0"), vertical_segments={0,1}, rotation="0"},
        {name=sch("0_20_2_walls_0"), vertical_segments={0,1}},
        {name=sch("0_20_2_walls_1"), vertical_segments={0,1}},
        {name=sch("0_20_2_walls_2"), vertical_segments={0,1}},
        {name=sch("0_20_2_walls_3"), vertical_segments={0,1}},
    }
})
br_core.register_biome({
    level = 0,
    desc = "Level 0 main with doors",
    danger = 1,
    on_generate = nil,
    segheight = 20,
    prevalence = 1,
    schems = {
        {name=sch("0_20_1_columns_0"), vertical_segments={1,0}},
        --
        {name=sch("0_20_2_columns_0"), vertical_segments={0,1}, rotation="0", prevalence=12},
        {name=sch("0_20_2_walls_0"), vertical_segments={0,1}, prevalence=12},
        {name=sch("0_20_2_walls_1"), vertical_segments={0,1}, prevalence=12},
        {name=sch("0_20_2_walls_2"), vertical_segments={0,1}, prevalence=12},
        {name=sch("0_20_2_walls_3"), vertical_segments={0,1}, prevalence=12},
        {name=sch("0_20_2_doors_0"), vertical_segments={0,1}, prevalence=2},
        {name=sch("0_20_2_doors_1"), vertical_segments={0,1}, prevalence=2},
        {name=sch("0_20_2_doors_0_portal"), vertical_segments={0,1}, prevalence=1},
        {name=sch("0_20_2_doors_1_portal"), vertical_segments={0,1}, prevalence=1},
        {name=sch("0_20_2_stairs_0_portal"), vertical_segments={0,1}, prevalence=1},
    }
})
br_core.register_biome({
    level = 0,
    desc = "Level 0 concrete",
    danger = 1,
    on_generate = nil,
    segheight = 20,
    prevalence = 1,
    schems = {
        {name=sch("0_20_1_columns_0"), vertical_segments={1,0}},
        --
        {name=sch("0_20_2_walls_concrete_0"), vertical_segments={0,1}, prevalence=9},
        {name=sch("0_20_2_walls_concrete_1"), vertical_segments={0,1}, prevalence=5},
        {name=sch("0_20_2_walls_concrete_1_portal"), vertical_segments={0,1}, prevalence=1},
    }
})
br_core.register_biome({
    level = 0,
    desc = "Level 0 concrete",
    danger = 1,
    on_generate = nil,
    segheight = 20,
    prevalence = 1,
    schems = {
        {name=sch("0_20_1_columns_0"), vertical_segments={1,0}},
        --
        {name=sch("0_20_2_nolight_walls_0"), vertical_segments={0,1}, prevalence=3},
        {name=sch("0_20_2_nolight_walls_1"), vertical_segments={0,1}, prevalence=3},
        {name=sch("0_20_2_nolight_walls_2"), vertical_segments={0,1}, prevalence=3},
        {name=sch("0_20_2_nolight_columns_0"), vertical_segments={0,1}, prevalence=2},
    }
})
br_core.register_biome({
    level = 0,
    desc = "Level 0 dark",
    danger = 1,
    on_generate = nil,
    segheight = 20,
    prevalence = 1,
    can_generate = function(pos) -- must be deterministic
        if vector.distance(pos, vector.new(0, pos.y, 0)) > 15 then
            return true
        else
            return false
        end
    end,
    schems = {
        {name=sch("0_20_1_columns_0"), vertical_segments={1,0}},
        --
        {name=sch("0_20_2_dark_0"), vertical_segments={0,1}, prevalence=8},
        {name=sch("0_20_2_dark_walls_0"), vertical_segments={0,1}, prevalence=8},
        {name=sch("0_20_2_dark_walls_1"), vertical_segments={0,1}, prevalence=8},
        --
        {name=sch("0_20_2_dark_walls_2"), vertical_segments={0,1}, prevalence=4},
        {name=sch("0_20_2_dark_walls_2_portal"), vertical_segments={0,1}},
    }
})




--[[
====LAYOUT====
br_core.level
    LEVELNAME
        desc
        danger
        biome
            BIOMENAME
                schems
                on_generate
                danger
                desc
                level
]]



br_core.register_level({
    level = 1,
    desc = "Level 1",
    danger = 1,
    grace_dist = 5,
    biome = {},
    segsize = 20,
    base_height = 20,
    sun = {
        visible = false,
        texture = "blank.png",
        sunrise_visible = false,
    },
    moon = {
        visible = false,
    },
    sky = {
        clouds = false,
        base_color = "#001",
        type = "plain"
    },
})

br_core.register_biome({
    level = 1,
    desc = "Level 1 columns",
    danger = 1,
    on_generate = nil,
    can_generate = function(pos) -- must be deterministic
        if vector.distance(pos, vector.new(0, pos.y, 0)) > 10 then
            return false
        else
            return true
        end
    end,
    schems = {
        {name=sch("1_open_0"), no_rotation=true},
    }
})
br_core.register_biome({
    level = 1,
    desc = "Level 1 main",
    danger = 1,
    on_generate = nil,
    prevalence = 2,
    schems = {
        {name=sch("1_open_0"), no_rotation=true},
        {name=sch("1_open_1"), no_rotation=true},
        {name=sch("1_open_0"), no_rotation=true},
        {name=sch("1_open_1"), no_rotation=true},
        {name=sch("1_open_0"), no_rotation=true},
        {name=sch("1_open_2")},
        {name=sch("1_open_2")},
        {name=sch("1_building_0")}, --PORTAL
        {name=sch("1_building_1")},
        {name=sch("1_building_2")},
        {name=sch("1_stairs_0")},
        {name=sch("1_ramp_0")},
    }
})
br_core.register_biome({
    level = 1,
    desc = "Level 1 main",
    danger = 1,
    on_generate = nil,
    prevalence = 1,
    schems = {
        {name=sch("1_stairs_0")},
        {name=sch("1_building_0")}, --PORTAL
        {name=sch("1_building_1")},
        {name=sch("1_building_2")},
        {name=sch("1_building_2")},
        {name=sch("1_building_2")},
        {name=sch("1_open_0"), no_rotation=true},
    }
})


-- pipes
br_core.register_level({
    level = 2,
    desc = "Level 2",
    danger = 3,
    grace_dist = 5,
    biome = {},
    segsize = 20,
    base_height = 20,
    sun = {
        visible = false,
        texture = "blank.png",
        sunrise_visible = false,
    },
    moon = {
        visible = false,
    },
    sky = {
        clouds = false,
        base_color = "#001",
        type = "plain"
    },
})

br_core.register_biome({
    level = 2,
    desc = "Level 2 corridors",
    danger = 3,
    on_generate = nil,
    can_generate = function(pos) -- must be deterministic
        if vector.distance(pos, vector.new(0, pos.y, 0)) > 10 then
            return false
        else
            return true
        end
    end,
    schems = {
        {name=sch("2_crossroad_0"), no_rotation=true},
        {name=sch("2_blue_0")},
    }
})

br_core.register_biome({
    level = 2,
    desc = "Level 0 main",
    danger = 1,
    on_generate = nil,
    prevalence = 1,
    schems = {
        {name=sch("2_maze_0")},
        {name=sch("2_crossroad_0"), no_rotation=true},
        {name=sch("2_crossroad_0"), no_rotation=true},
        {name=sch("2_crossroad_0"), no_rotation=true},
        {name=sch("2_crossroad_0")},
        {name=sch("2_open_0")},
        {name=sch("2_water_0")},
    }
})

br_core.register_biome({
    level = 2,
    desc = "Level 0 main",
    danger = 1,
    on_generate = nil,
    prevalence = 1,
    schems = {
        {name=sch("2_blue_1"), no_rotation=true},
        {name=sch("2_blue_1"), no_rotation=true},
        {name=sch("2_blue_1"), no_rotation=true},
        {name=sch("2_blue_1")},
        {name=sch("2_blue_1")},
        {name=sch("2_blue_1_portal"), no_rotation=true},
        {name=sch("2_blue_0")},
        {name=sch("2_blue_0")},
        {name=sch("2_blue_0")},
        {name=sch("2_blue_0_portal")},
    }
})

br_core.register_biome({
    level = 2,
    desc = "Level 0 main",
    danger = 1,
    on_generate = nil,
    prevalence = 1,
    schems = {
        {name=sch("2_green_0"), no_rotation=true},
        {name=sch("2_green_0"), no_rotation=true},
        {name=sch("2_green_1"), no_rotation=true},
        {name=sch("2_green_1"), no_rotation=true},
        {name=sch("2_green_1_portal")},
        {name=sch("2_green_0")},
        {name=sch("2_green_1")},
        {name=sch("2_green_2")},
        {name=sch("2_green_2")},
        {name=sch("2_green_0_nolight")},
        {name=sch("2_green_1_nolight")},
    }
})




-- hotel
br_core.register_level({
    level = 5,
    desc = "Level 5",
    danger = 3,
    grace_dist = 5,
    biome = {},
    segsize = 20,
    base_height = 12,
    sun = {
        visible = false,
        texture = "blank.png",
        sunrise_visible = false,
    },
    moon = {
        visible = false,
    },
    sky = {
        clouds = false,
        base_color = "#001",
        type = "plain"
    },
})

br_core.register_biome({
    level = 5,
    desc = "Level 5 rust",
    danger = 1,
    on_generate = nil,
    can_generate = function(pos)
        return true
    end,
    segheight = 10,
    schems = {
        {name=sch("5_10h_rust_1_portal"), vertical_segments={1,1}},
        {name=sch("5_10h_rust_0"), rotation="0", vertical_segments={1,1}, prevalence=1},
        {name=sch("5_10h_rust_1"), rotation="0", vertical_segments={1,1}, prevalence=1},
        {name=sch("5_10h_rust_2"), rotation="0", vertical_segments={1,1}, prevalence=1},
    }
})

br_core.register_biome({
    level = 5,
    desc = "Level 0 orange",
    danger = 1,
    on_generate = nil,
    prevalence = 1,
    segheight = 10,
    schems = {
        {name=sch("5_10h_orange_2_portal"), vertical_segments={1,1}},
        {name=sch("5_10h_orange_0"), rotation="0", vertical_segments={1,1}, prevalence=1},
        {name=sch("5_10h_orange_1"), rotation="0", vertical_segments={1,1}, prevalence=1},
        {name=sch("5_10h_orange_2"), rotation="0", vertical_segments={1,1}, prevalence=1},
    }
})

br_core.register_biome({
    level = 5,
    desc = "Level 0 black",
    danger = 1,
    on_generate = nil,
    prevalence = 1,
    segheight = 10,
    schems = {
        {name=sch("5_10h_black_0"), rotation="0", vertical_segments={1,1}, prevalence=1},
        {name=sch("5_20h_black_stairs_0"), vertical_segments={1,0}, prevalence=1, skip_above=1},
        {name=sch("5_10h_black_1"), rotation="0", vertical_segments={1,1}, prevalence=1},
        {name=sch("5_10h_black_2"), rotation="0", vertical_segments={1,1}, prevalence=1},
        {name=sch("5_10h_black_3"), rotation="0", vertical_segments={1,1}, prevalence=1},
        {name=sch("5_10h_black_0"), rotation="90", vertical_segments={1,1}, prevalence=3},
        {name=sch("5_10h_black_1"), rotation="90", vertical_segments={1,1}, prevalence=6},
        {name=sch("5_10h_black_2"), rotation="90", vertical_segments={1,1}, prevalence=6},
        {name=sch("5_10h_black_3"), rotation="90", vertical_segments={1,1}, prevalence=6},
        {name=sch("5_10h_black_portal_0"), rotation="90", vertical_segments={1,1}, prevalence=5},
    }
})

-- hub
br_core.register_level({
    level = 12,
    desc = "Level 12 hub",
    danger = 3,
    grace_dist = 5,
    biome = {},
    segsize = 40,
    base_height = 24,
    sun = {
        visible = true,
        texture = "blank.png",
        sunrise_visible = false,
    },
    moon = {
        visible = false,
    },
    sky = {
        clouds = false,
        base_color = "#001",
        type = "plain"
    },
})

local l12pattern = {
    1,2,2,2,2,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0
}
br_core.register_biome({
    level = 12,
    desc = "Level 12 tunnel",
    danger = 1,
    on_generate = nil,
    can_generate = function(pos)
        return ((pos.z % 3 == 0) and l12pattern[pos.x%30+1] <= 1)
    end,
    schems = {
        {name=sch("12_tunnel_portal_0"), rotation="90", prevalence=3},
        {name=sch("12_tunnel_random_0"), rotation="90", prevalence=1},
        {name=sch("12_tunnel_0"), rotation="90", prevalence=10},
    }
})
br_core.register_biome({
    level = 12,
    desc = "Level 12 block",
    danger = 1,
    on_generate = nil,
    can_generate = function(pos)
        return ((pos.z % 3 ~= 0) and l12pattern[pos.x%30+1] == 0)
    end,
    schems = {
        {name=sch("12_block_0")},
    }
})
br_core.register_biome({
    level = 12,
    desc = "Level 12 empty",
    danger = 1,
    on_generate = nil,
    can_generate = function(pos)
        return ((pos.z % 3 ~= 0) and l12pattern[pos.x%30+1] == 1)
    end,
    schems = {
        {name=sch("12_block_1")},
    }
})
br_core.register_biome({
    level = 12,
    desc = "Level 12 bridge",
    danger = 1,
    on_generate = nil,
    can_generate = function(pos)
        return ((pos.z % 3 == 0) and l12pattern[pos.x%30+1] == 2)
    end,
    schems = {
        {name=sch("12_bridge_0"), rotation="90"},
    }
})
br_core.register_biome({
    level = 12,
    desc = "Level 12 water",
    danger = 1,
    on_generate = nil,
    can_generate = function(pos)
        return ((pos.z % 3 ~= 0) and l12pattern[pos.x%30+1] == 2)
    end,
    schems = {
        {name=sch("12_water_0"), rotation="90"},
    }
})
br_core.register_on_changed_level(function(player, from_level, to_level)
    if to_level == 12 then
        player:override_day_night_ratio(0.2)
    elseif from_level == 12 then
        player:override_day_night_ratio(1)
    end
end)





-- hub
br_core.register_level({
    level = 17,
    desc = "Level 17",
    danger = 3,
    grace_dist = 5,
    biome = {},
    segsize = 20,
    base_height = 6,
    sun = {
        visible = true,
        texture = "blank.png",
        sunrise_visible = false,
    },
    moon = {
        visible = false,
    },
    sky = {
        clouds = false,
        base_color = "#001",
        type = "plain"
    },
})

br_core.register_biome({
    level = 17,
    desc = "Level 17 canals",
    danger = 1,
    on_generate = nil,
    segheight = 20,
    can_generate = function(pos)
        return true
    end,
    schems = {
        {name=sch("17_fill_0"), rotation="0", vertical_segments={0,1,1,1}},
        {name=sch("17_canal_0"), rotation="0", prevalence=3, vertical_segments={1,0,0,0}},
        {name=sch("17_canal_1"), rotation="0", prevalence=3, vertical_segments={1,0,0,0}},
        {name=sch("17_canal_1"), rotation="180", prevalence=3, vertical_segments={1,0,0,0}},
        {name=sch("17_canal_2"), rotation="0", prevalence=3, vertical_segments={1,0,0,0}},
        {name=sch("17_canal_2"), rotation="180", prevalence=3, vertical_segments={1,0,0,0}},
        {name=sch("17_canal_2_portal"), rotation="180", prevalence=1, vertical_segments={1,0,0,0}},
        -- {name=sch("17_canal_0"), rotation="180", prevalence=1, vertical_segments={1,0,0}},
    }
})
br_core.register_biome({
    level = 17,
    desc = "Level 17 storm drains",
    danger = 1,
    on_generate = nil,
    segheight = 20,
    can_generate = function(pos)
        return true
    end,
    schems = {
        {name=sch("17_fill_0"), rotation="0", vertical_segments={0,1,1,1}},
        {name=sch("17_storm_drains_0"), rotation="0", prevalence=1, vertical_segments={1,0,0,0}, skip_above=3},
    }
})




-- ???
br_core.register_level({
    level = 31,
    desc = "Level 31",
    danger = 3,
    grace_dist = 5,
    biome = {},
    segsize = 20,
    base_height = 42,
    biome_roughness = 91.798,
    sun = {
        visible = false,
        texture = "blank.png",
        sunrise_visible = false,
    },
    moon = {
        visible = false,
    },
    sky = {
        clouds = false,
        base_color = "#001",
        type = "plain"
    },
})
br_core.register_biome({
    level = 31,
    desc = "Level 31",
    danger = 3,
    on_generate = nil,
    segheight = 10,
    can_generate = function(pos) -- must be deterministic
        return (pos.z%5 ~= 0 and (pos.x%8 < 4 and pos.z%7 < 4))
    end,
    schems = {
        {name=sch("31_10h_balcony_base"),  vertical_segments={1}, prevalence=3},
        {name=sch("31_10h_balcony_roof"),  vertical_segments={0,0,0,0,0,0,1,1}},
        {name=sch("31_10h_balcony_0"),     vertical_segments={0,1,1,1,1,1,1,0}},
        {name=sch("31_10h_balcony_1"),     vertical_segments={0,1,1,1,1,1,1,0}},
        {name=sch("31_10h_balcony_2"),     vertical_segments={0,1,1,1,1,1,1,0}},
        {name=sch("31_10h_balcony_3"),     vertical_segments={0,1,1,1,1,1,1,0}},
        {name=sch("31_10h_balcony_4"),     vertical_segments={0,1,1,1,1,1,1,0}},
        {name=sch("31_10h_balcony_5"),     vertical_segments={0,1,1,1,1,1,1,0}},
        {name=sch("31_10h_balcony_6"),     vertical_segments={0,1,1,1,1,1,1,0}},
    }
})
br_core.register_biome({
    level = 31,
    desc = "Level 31",
    danger = 3,
    on_generate = nil,
    segheight = 10,
    can_generate = function(pos) -- must be deterministic
        return ((pos.z%5 ~= 0) or (pos.x%3 == 0))
    end,
    schems = {
        {name=sch("31_10h_corridor_base"),  vertical_segments={1}, prevalence=3, rotation="0"},
        {name=sch("31_10h_corridor_roof"),  vertical_segments={0,0,0,0,0,0,1,1}, rotation="0", prevalence=7},
        {name=sch("31_10h_corridor_0"),     vertical_segments={0,1,1,1,1,1,1,0}, rotation="0", prevalence=7},
        {name=sch("31_10h_corridor_1"),     vertical_segments={0,1,1,1,1,1,1,0}, rotation="0", prevalence=7},
        {name=sch("31_10h_corridor_2"),     vertical_segments={0,1,1,1,1,1,1,0}, rotation="0", prevalence=7},
        {name=sch("31_10h_corridor_3"),     vertical_segments={0,1,1,1,1,1,1,0}, rotation="0", prevalence=7},
        {name=sch("31_10h_corridor_4"),     vertical_segments={0,1,1,1,1,1,1,0}, rotation="0", prevalence=7},
        {name=sch("31_10h_corridor_5"),     vertical_segments={0,1,1,1,1,1,1,0}, rotation="0", prevalence=7},
        {name=sch("31_20h_stairs_0"), vertical_segments={0,1,1,1,1,1,0,0}, skip_above=1, prevalence=3},
        {name=sch("31_20h_stairs_1"), vertical_segments={0,1,1,1,1,1,0,0}, skip_above=1, prevalence=3},
        {name=sch("31_20h_stairs_0_portal"), vertical_segments={0,1,1,1,1,1,0,0}, skip_above=1},
    }
})

br_core.register_biome({
    level = 31,
    desc = "Level 31 SHORT",
    danger = 3,
    on_generate = nil,
    segheight = 10,
    prevalence = 1,
    can_generate = function(pos) -- must be deterministic
        return pos.z%5 == 0
    end,
    schems = {
        {name=sch("31_10h_corridor_short_base"),  vertical_segments={1}, prevalence=3, rotation="0"},
        {name=sch("31_10h_corridor_short_roof"),  vertical_segments={0,0,0,0,0,0,1,1}, rotation="0"},
        {name=sch("31_10h_corridor_short_0"),     vertical_segments={0,1,1,1,1,1,1,0}, rotation="0"},
        {name=sch("31_10h_corridor_short_1"),     vertical_segments={0,1,1,1,1,1,1,0}, rotation="0"},
        {name=sch("31_10h_corridor_short_2"),     vertical_segments={0,1,1,1,1,1,1,0}, rotation="0"},
        {name=sch("31_10h_corridor_short_3"),     vertical_segments={0,1,1,1,1,1,1,0}, rotation="0"},
        {name=sch("31_10h_corridor_short_4"),     vertical_segments={0,1,1,1,1,1,1,0}, rotation="0"},
        {name=sch("31_10h_corridor_short_5"),     vertical_segments={0,1,1,1,1,1,1,0}, rotation="0"},
    }
})




-- poolrooms
br_core.register_level({
    level = 37,
    desc = "Level 37",
    danger = 1,
    grace_dist = 5,
    biome = {},
    segsize = 20,
    base_height = 40,
    sun = {
        visible = true,
        texture = "blank.png",
        sunrise_visible = false,
    },
    moon = {
        visible = false,
    },
    sky = {
        clouds = false,
        base_color = "#def",
        type = "plain"
    },
})

br_core.register_biome({
    level = 37,
    desc = "Level 37 columns",
    danger = 1,
    on_generate = nil,
    can_generate = function(pos) -- must be deterministic
        if vector.distance(pos, vector.new(0, pos.y, 0)) > 10 then
            return false
        else
            return true
        end
    end,
    schems = {
        {name=sch("37_channel_0")},
    }
})
br_core.register_biome({
    level = 37,
    desc = "Level 0 main",
    danger = 1,
    on_generate = nil,
    prevalence = 1,
    schems = {
        {name=sch("37_channel_0"), prevalence=3},
        {name=sch("37_channel_1"), prevalence=3},
        {name=sch("37_channel_2"), prevalence=3},
        {name=sch("37_channel_3"), prevalence=3},
        {name=sch("37_pool_0"), prevalence=2},
        {name=sch("37_stair_0"), prevalence=1},
    }
})
br_core.register_biome({
    level = 37,
    desc = "Level 0 closed",
    danger = 1,
    on_generate = nil,
    prevalence = 1,
    schems = {
        {name=sch("37_channel_0"), prevalence=1},
        {name=sch("37_channel_3"), prevalence=1},
        {name=sch("37_closed_0"), prevalence=5},
        {name=sch("37_closed_1"), prevalence=4},
        {name=sch("37_closed_1_portal"), prevalence=1},
        {name=sch("37_pool_0"), prevalence=2},
    }
})




br_core.register_level({
    level = 45,
    desc = "Level 45",
    danger = 0,
    grace_dist = 5,
    biome = {},
    segsize = 20,
    base_height = 41,
    sun = {
        texture = "blank.png",
        sunrise_visible = false,
    },
    moon = {
        visible = false,
    },
    sky = {
		base_color = "#ffffff",
		type = "regular",
        clouds = false,
		sky_color = {
			day_sky = "#92b9f2" ,
			day_horizon = "#010c22",
			dawn_sky = "#92b9f2" ,
			dawn_horizon = "#010c22",
			night_sky = "#006bff" ,
			night_horizon = "#010c22",
			indoors = "#010c22",
			fog_sun_tint = "#010c22",
			fog_moon_tint = "#010c22",
			fog_tint_type = "custom",
		}
	},
})

br_core.register_biome({
    level = 45,
    desc = "Level 45 doors",
    danger = 0,
    on_generate = nil,
    can_generate = function(pos) -- must be deterministic
        if vector.distance(pos, vector.new(0, pos.y, 0)) > 10 then
            return false
        else
            return true
        end
    end,
    schems = {
        {name=sch("45_doors_0"), no_rotation=true},
        {name=sch("45_sparse_0"), no_rotation=true},
        {name=sch("45_sparse_0"), no_rotation=true},
    }
})

br_core.register_biome({
    level = 45,
    desc = "Level 45 doors with portal",
    danger = 0,
    on_generate = nil,
    prevalence = 1,
    schems = {
        {name=sch("45_sparse_0"), no_rotation=true},
        {name=sch("45_sparse_0"), no_rotation=true},
        {name=sch("45_sparse_0"), no_rotation=true},
        {name=sch("45_sparse_0_portal"), no_rotation=true},
        {name=sch("45_doors_0"), no_rotation=true},
        {name=sch("45_doors_0"), no_rotation=true},
        {name=sch("45_doors_0"), no_rotation=true},
    }
})




-- ???
br_core.register_level({
    level = 51,
    desc = "Level 51 area",
    danger = 1,
    grace_dist = 5,
    biome = {},
    segsize = 20,
    base_height = 2,
    biome_roughness = 8.654,
    sun = {
        visible = false,
        texture = "blank.png",
        sunrise_visible = false,
    },
    moon = {
        visible = false,
    },
    sky = {
        clouds = false,
        base_color = "#001",
        type = "plain"
    },
})
br_core.register_biome({
    level = 51,
    desc = "Level 51",
    danger = 1,
    on_generate = nil,
    segheight = 10,
    can_generate = function(pos) -- must be deterministic
        return true
    end,
    schems = {
        {name=sch("51_shelves_0"),  vertical_segments={1}, prevalence=1, skip_above=3},
        {name=sch("51_shelves_1"),  vertical_segments={1}, prevalence=1, skip_above=3},
        {name=sch("51_shelves_0_portal"),  vertical_segments={1}, prevalence=1, skip_above=3, can_generate = function(pos)
            return ((pos.x % 2 == 0) and (pos.z % 2 == 0))
        end},
        {name=sch("51_shelves_0"),  vertical_segments={1}, prevalence=3, skip_above=3, rotation="0"},
        {name=sch("51_shelves_1"),  vertical_segments={1}, prevalence=3, skip_above=3, rotation="0"},
    }
})
br_core.register_biome({
    level = 51,
    desc = "Level 51",
    danger = 1,
    on_generate = nil,
    segheight = 10,
    schems = {
        {name=sch("51_shelves_0"),  vertical_segments={1}, prevalence=1, skip_above=3},
        {name=sch("51_shelves_1"),  vertical_segments={1}, prevalence=1, skip_above=3},
        {name=sch("51_piles_0"),  vertical_segments={1}, prevalence=9, skip_above=3},
        {name=sch("51_piles_0_portal"),  vertical_segments={1}, prevalence=1, skip_above=3},
        {name=sch("51_car_0"),  vertical_segments={1}, prevalence=1, skip_above=3, can_generate = function(pos)
            return ((pos.x % 3 == 0) and (pos.z % 2 == 0))
        end},
    }
})




-- hospital
br_core.register_level({
    level = 62,
    desc = "Level 62 hospital",
    danger = 3,
    grace_dist = 5,
    biome = {},
    segsize = 40,
    base_height = 2,
    sun = {
        visible = false,
        texture = "blank.png",
        sunrise_visible = false,
    },
    moon = {
        visible = false,
    },
    sky = {
        clouds = false,
        base_color = "#eee",
        type = "plain"
    },
})
br_core.register_biome({
    level = 62,
    desc = "Level 62",
    danger = 1,
    on_generate = nil,
    schems = {
        {name=sch("62_corridors_0"), rotation="0"},
        {name=sch("62_corridors_0"), rotation="90"},
        {name=sch("62_corridors_1"), rotation="0"},
        {name=sch("62_corridors_1"), rotation="90"},
        {name=sch("62_corridors_closed_0"), rotation="0", prevalence=3},
        {name=sch("62_corridors_closed_0"), rotation="90", prevalence=3},
    }
})







br_core.register_level({
    level = 180,
    desc = "Level 180 \'the end\'",
    danger = 0,
    grace_dist = 5,
    biome = {},
    segsize = 20,
    base_height = 42,
    biome_roughness = 2981.7138976,
    sun = {
        visible = true,
        sunrise_visible = false,
    },
    moon = {
        visible = false,
    },
    sky = {
		base_color = "#ffffff",
		type = "regular",
		clouds = true,
		sky_color = {
			day_sky = "#3383a6" ,
			day_horizon = "#3383a6",
			dawn_sky = "#000" ,
			dawn_horizon = "#000",
			night_sky = "#263965" ,
			night_horizon = "#263965",
			indoors = "#263965",
			fog_sun_tint = "#263965",
			fog_moon_tint = "#263965",
			fog_tint_type = "custom",
		}
	},
})

br_core.register_biome({
    level = 180,
    desc = "Level 180 house",
    danger = 0,
    on_generate = nil,
    prevalence = 1,
    can_generate = function(seg_pos) -- must be deterministic
        if (seg_pos.x%17 == 0) and (seg_pos.z%87 == 0) then
            return true
        else
            return false
        end
    end,
    schems = {
        {name=sch("180_house_0")},
    }
})

br_core.register_biome({
    level = 180,
    desc = "Level 180",
    danger = 0,
    on_generate = nil,
    can_generate = function(pos) -- must be deterministic
        return true
    end,
    prevalence = 20,
    schems = {
        {name=sch("180_open_0")},
    }
})

br_core.register_biome({
    level = 180,
    desc = "Level 180",
    danger = 0,
    on_generate = nil,
    prevalence = 1,
    schems = {
        {name=sch("180_open_0")},
        {name=sch("180_open_0")},
        {name=sch("180_open_0")},
        {name=sch("180_open_0")},
        {name=sch("180_open_1")},
        {name=sch("180_open_2")},
        {name=sch("180_open_3")},
        {name=sch("180_open_4")},
        {name=sch("180_arch_0")},
        {name=sch("180_path_0")},
    }
})

br_core.register_biome({
    level = 180,
    desc = "Level 180",
    danger = 0,
    on_generate = nil,
    prevalence = 1,
    schems = {
        {name=sch("180_arch_0")},
        {name=sch("180_path_0")},
        {name=sch("180_path_0")},
        {name=sch("180_path_0")},
        {name=sch("180_path_1")},
        {name=sch("180_path_1")},
    }
})
br_core.register_on_changed_level(function(player, from_level, to_level)
    if to_level == 180 and from_level ~= -1 then
        player:set_pos(vector.new(50,br_spawn.get_level_y(180),50))
    end
end)

-- sort levels after all mods loaded
minetest.register_on_mods_loaded(function()
    br_core.sort_levels()
end)
