local S = core.get_translator(core.get_current_modname());
local dp = natural_habitat.dupes;

local y_min, y_max = -31000, 31000;
local nether_y_min, nether_y_max = -31000, -16000;

if core.get_modpath("mcl_vars") then
    y_min = mcl_vars.mg_overworld_min;
    y_max = mcl_vars.mg_overworld_max;
    
    nether_y_min = mcl_vars.mg_nether_min;
    nether_y_max = mcl_vars.mg_nether_max;
end;

local stones = {
    stone = {
        blocks = { dp.cobble, dp.stone },
        biomes = {
            dp.biomes.grassland,    dp.biomes.mountains,         dp.biomes.jungle, 
            dp.biomes.jungle_edge,  dp.biomes.jungle_bamboo,     dp.biomes.meadow,
            dp.biomes.forest,       dp.biomes.forest_coniferous, dp.biomes.forest_deciduous,
            dp.biomes.forest_birch, dp.biomes.rainforest,
        },
    },
    deepstone = {
        blocks = { dp.cobble_deep, dp.stone_deep },
    },
    mossystone = {
        blocks = { dp.mossycobble, dp.mossystone },
    },
    sandstone = {
        blocks = { dp.sandstone },
    },
    netherrack = {
        blocks = { dp.netherrack },
        y_min = nether_y_min,
        y_max = nether_y_max,
        place_on = {
            "mcl_nether:netherrack", "mcl_crimson:warped_nylium", "mcl_crimson:crimson_nylium",
            "mcl_nether:soul_sand", "mcl_blackstone:soul_soil", "mcl_nether:magma",
            "mcl_blackstone:blackstone", "mcl_nether:ancient_debris", 
        },
    },
}

local place_on = {
    dp.soil, dp.soil_with_grass, dp.stone, dp.cobble, dp.mossycobble,
};

for stone, def in pairs(stones) do
    for i, block in ipairs(def.blocks or {}) do
        local next_node = core.registered_items[def.blocks[i+1]];
        local node = core.registered_items[block];
        local tiles = node.tiles;
        
        if next_node then tiles = next_node.tiles; end;
        
        if node then
            local rubble_name = m7a.register_node("natural_habitat:"..stone.."_rubble", {
                description = S(m7a.to_name(stone).." Rubble"),
                drawtype = "mesh",
                mesh = "nbt_rubble.obj",
                tiles = tiles,
                color = "#CCCCCC",
                paramtype = "light",
                paramtype2 = "facedir",
                walkable = false,
                sunlight_propagates = true,
                groups = { dig_immediate=3, attached_node=1, rubble=1, rock=1, falling_node=1 },
                node_box = {type = "fixed", fixed = {{-0.4, -0.5, -0.4, 0.4, -0.2, 0.4}}},
                selection_box = {type = "fixed", fixed = {{-0.4, -0.5, -0.4, 0.4, -0.2, 0.4}}},
                --on_place = natural_habitat.place_item,
                sounds = natural_habitat.sound_stone(),
                
                _mcl_hardness = 0;
            });
            
            -- CRAFT RUBBLE
            for i, stone in pairs(def.blocks) do
                if core.registered_nodes[stone] then
                    core.register_craft({
                        output = rubble_name.." 36",
                        recipe = { 
                            { stone, stone },
                            { stone, stone },
                        },
                    })
                end;
            end;
            
            -- CRAFT BLOCK
            core.register_craft({
                output = block,
                recipe = {
                    {rubble_name, rubble_name, rubble_name},
                    {rubble_name, rubble_name, rubble_name},
                    {rubble_name, rubble_name, rubble_name},
                },
            });
            
            -- REGISTER DECORATION
            for i, rubble in pairs({
                {
                    biomes={}, fill_ratio=0.01,  
                    place_on=def.blocks, spawn_by="air",
                },
                {
                    biomes={}, fill_ratio=0.002, 
                    place_on=def.place_on or place_on, spawn_by=def.blocks,
                },
                {
                    biomes=def.biomes, fill_ratio=0.005,
                    place_on=def.place_on or place_on, spawn_by="air",
                },
            }) do
                if rubble.biomes then
                    core.register_decoration({
                        deco_type = "simple",
                        decoration = "natural_habitat:"..stone.."_rubble",
                        place_on = rubble.place_on,
                        fill_ratio = rubble.fill_ratio,
                        biomes = rubble.biomes,
                        spawn_by = rubble.spawn_by,
                        num_spawn_by = 1,
                        flags = "all_floors",
                        y_min = def.y_min or y_min,
                        y_max = def.y_max or y_max,
                    })
                end;
            end;
            
            break;
        end;
    end;
end;