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

aom_mapgen = {}

aom_mapgen.biome_groups = {
    ocean = {},
    mobs = {},
}
aom_mapgen.biome_groups.registered = {}

function aom_mapgen.register_biome(biome, groups)
    -- aom_mapgen.biome_groups[biome.name] = groups
    table.insert(aom_mapgen.biome_groups.registered, biome.name)
    core.register_biome(biome)
    for _, group in pairs(groups) do
        if not aom_mapgen.biome_groups[group] then
            aom_mapgen.biome_groups[group] = {biome.name}
        else
            aom_mapgen.biome_groups[group][#aom_mapgen.biome_groups[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
        aom_mapgen.biome_groups["ocean"][#aom_mapgen.biome_groups["ocean"]+1] = biome.name
        aom_mapgen.register_biome(biome, groups)
    end
end

local decors_by_layer = {}
local core_regdecor = core.register_decoration
--[[

    Layer number guide:
     1-9  = -- large terrain modifying passes, base layers and really important first passes
        -- (force_placement)
    10-19 = -- terrain modifying passes (pools/lakes)
        -- (force_placement, 15 = pools, 10 = mesa and cliffs and chasms, 16 = boulders)
    20-29 = -- things that might prevent or get in the way of trees and other passes
        -- (25 = houses)
    30-39 = -- trees and other "delicate" things that need to connect to the ground reliably
        -- (tallest first, 35 = medium oak, 30 = giant redwood, 39 = bushes)
    40-49 = -- anything else completely unimportant, small details etc
        -- (important first, 45 = tall grasses, 49 = tiny specks of detail)
--]]
core.register_decoration = function(def)
    if not def._layer then return core_regdecor(def) end
    local diff = def._layer - #decors_by_layer
    if diff > 0 then
        for i = 0, diff do
            table.insert(decors_by_layer, {})
        end
    end
    local decor_list = decors_by_layer[def._layer]
    table.insert(decor_list, def)
end

core.register_on_mods_loaded(function()
    for layer_num, decor_list in pairs(decors_by_layer) do
        for i, decor_def in ipairs(decor_list) do
            core_regdecor(decor_def)
        end
    end
    decors_by_layer = nil
end)

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

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


core.register_chatcommand("biome", {
        params = "",
        description = S("Tells the biome"),
        privs = {},
        func = function(name, param)
                local player = core.get_player_by_name(name)
                local p = player:get_pos()
                local b = core.get_biome_data(p)
                if not b then return false, S("No biome") end
                return true, S("Biome: "..core.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

core.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(core.registered_biomes) do
            if not is_ocean(bname) then
                len = len +1
            end
        end
        local i = 0
        for bname, def in pairs(core.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 = core.get_worldpath().."/debugoutput.txt"
        local file = io.open(filepath, "w")
        if file then
            file:write(str)
        end
    end,
})

