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

pmb_map_base = {}


pmb_biomes = {}
pmb_biomes.registered = {}

function pmb_biomes.register_biome(biome, groups)
    -- pmb_biomes[biome.name] = groups
    pmb_biomes.registered[biome.name] = 1
    minetest.register_biome(biome)
    for _, group in pairs(groups) do
        if not pmb_biomes[group] then
            pmb_biomes[group] = {biome.name}
        else
            pmb_biomes[group][#pmb_biomes[group]+1] = biome.name
        end
    end

    if biome.y_min_ocean and (not biome.__has_reg) then
        biome.__has_reg = true
        biome.y_max = biome.y_min - 1
        biome.y_min = biome.y_min_ocean
        biome.name = biome.name.."_OCEAN"
        biome.node_top = biome.node_riverbed
        biome.vertical_blend = biome.ocean_vertical_blend or 0
        pmb_biomes.register_biome(biome, groups)
    end
end


dofile(mod_path .. DIR_DELIM .. "structure_spawning" .. DIR_DELIM .. "cabins.lua")
dofile(mod_path .. DIR_DELIM .. "structure_spawning" .. DIR_DELIM .. "ruins.lua")
dofile(mod_path .. DIR_DELIM .. "structure_spawning" .. DIR_DELIM .. "boulders.lua")

dofile(mod_path .. DIR_DELIM .. "aliases.lua")
dofile(mod_path .. DIR_DELIM .. "biomes.lua")
dofile(mod_path .. DIR_DELIM .. "ores" .. DIR_DELIM .. "soil_types.lua")
dofile(mod_path .. DIR_DELIM .. "ores.lua")


minetest.register_chatcommand("biome", {
        params = "",
        description = S("Tells the biome"),
        privs = {},
        func = function(name, param)
                local player = minetest.get_player_by_name(name)
                local p = player:get_pos()
                local b = minetest.get_biome_data(p)
                if not b then return false, S("No biome") end
                return true, S("Biome: "..minetest.get_biome_name(b.biome).."    Heat: "..b.heat.."    Humid: "..b.humidity)
        end
})

local function is_ocean(name)
    local l = string.len(name)
    if string.sub(name, l-5, l) == "_OCEAN" then
        return true
    end
end

minetest.register_chatcommand("biomelistoutput", {
    params = "",
    description = S("Tells the biome"),
    privs = {give=true},
    func = function(name, param)
        local str = "["
        local len = 0
        for bname, def in pairs(minetest.registered_biomes) do
            if not is_ocean(bname) then
                len = len +1
            end
        end
        local i = 0
        for bname, def in pairs(minetest.registered_biomes) do
            if not is_ocean(bname) then
                i = i + 1
                local t = (
                    '{"name": "'..bname..'",'..
                    '"heat_point": '..def.heat_point..','..
                    '"humidity_point": '..def.humidity_point..
                    ',"y_min": '..def.y_min..''..
                    ',"y_max": '..(def.y_max+100)..''..
                    '}'
                )
                if i<len then t = t .. ',' end
                str = str..t
            end
        end
        str = str .. "]"
        local filepath = minetest.get_worldpath().."/debugoutput.txt"
        local file = io.open(filepath, "w")
        if file then
            file:write(str)
        end
    end,
})

