local S = core.get_translator(core.get_current_modname());
local modpath = core.get_modpath(core.get_current_modname());
local schempath = modpath.."/schems/data";
local dp = natural_habitat.dupes;

-- COCONUT PALMTREE LOG
core.register_node("natural_habitat:coconut_palmtree_log", {
    description = S("Coconut Palmtree Log"),
    drawtype = "mesh",
    mesh = "nbt_palmtree_log.obj",
    tiles = {
        "nbt_coconut_palm_log_side.png", 
        "nbt_coconut_palm_log_top.png",
        "nbt_coconut_palm_log_bottom.png",
    },
    paramtype = "light",
    paramtype2 = "facedir",
    selection_box = { type="fixed", fixed={-0.4, -0.5, -0.4, 0.4, 0.5, 0.4} },
    collision_box = { type="fixed", fixed={-0.4, -0.5, -0.4, 0.4, 0.5, 0.4} },
    groups = m7a.mod_impact(
        { tree=1, choppy=2, oddly_breakable_by_hand=1, flammable=2, }, {
            ["mcl_player"] = {
                handy=1, axey=1, material_wood=1, 
                flammable=3, fire_encouragement=5, fire_flammability=20, 
            },
        }
    ),
    sunlight_propagates = true,
    sounds = natural_habitat.sound_leaves(),
    
    _mcl_hardness = 3;
});

-- COCONUT PALMTREE LEAVES
core.register_node("natural_habitat:coconut_palmtree_leaves", {
    description = S("Coconut Palmtree Leaves"),
    drawtype = "mesh",
    mesh = "nbt_palmtree_leaves.obj",
    tiles = {"nbt_coconut_palmtree_leaves.png"},
    paramtype = "light",
    paramtype2 = "facedir",
    groups = m7a.mod_impact(
        { snappy=1, flammable=1 }, {
            ["mcl_player"] = { handy=1, food=2, deco_block=1, compostability=65, },
        }
    ),
    use_texture_alpha = "clip",
    sunlight_propagates = true,
    walkable = false,
    drop = "",
    selection_box = { type = "fixed", fixed={-0.5, -0.5, -1.5, 0.5, 0.4, 0.5} },
    sounds = natural_habitat.sound_wood(),
    
    _mcl_hardness = 1;
});

-- COCONUT
m7a.register_multinode("natural_habitat:coconut", {
    shared = {
        drawtype = "mesh",
        inventory_image = "nbt_coconut.png",
        tiles = { "nbt_coconut_texture.png", },
        paramtype = "light",
        paramtype2 = "facedir",
        sounds = natural_habitat.sound_wood(),
    },
    nodes = {
        [""] = {
            description = S("Coconut"),
            mesh = "nbt_coconut.obj";
            groups = m7a.mod_impact(
                { snappy=1, flammable=1, falling_node=1, }, {
                    ["mcl_player"] = {
                        handy=1, deco_block=1, compostability=65, falling_node=1,
                    },
                }
            ),
            selection_box = { type="fixed", fixed={-0.3, -0.5, -0.3, 0.3, 0.2, 0.3} },
            collision_box = { type="fixed", fixed={-0.3, -0.5, -0.3, 0.3, 0.2, 0.3} },
            _mcl_hardness = 0,
        },
        ["_hanging"] = {
            description = S("Coconut Hanging"),
            mesh = "nbt_coconut_hanging.obj";
            drop = "natural_habitat:coconut",
            groups = m7a.mod_impact(
                { choppy=2, oddly_breakable_by_hand=1, flammable=2, }, {
                    ["mcl_player"] = {
                        handy=1, axey=1, material_wood=1, 
                        flammable=3, fire_encouragement=5, fire_flammability=10, 
                    },
                }
            ),
            selection_box = { type="fixed", fixed={-0.3, -0.35, 0, 0.3, 0.3, 0.6} },
            collision_box = { type="fixed", fixed={-0.3, -0.35, 0, 0.3, 0.3, 0.6} },
            _mcl_hardness = 1,
        },
    },
});

-- ABM - SPAWN COCONUT
core.register_abm({
    label = "Spawn Coconut",
    nodenames = { "natural_habitat:coconut_palmtree_leaves" },
    neighbors = { "natural_habitat:coconut_palmtree_log" },
    interval = 10.0,
    chance = 70,
    action = function(pos, node, active_object_count, active_object_count_wider)
        core.set_node(
            vector.offset(pos, 0, -1, 0),
            {name="natural_habitat:coconut_hanging", param2=node.param2}
        );
    end,
});

-- ABM - DESPAWN PALMTREE LEAVES
core.register_abm({
    label = "Despawn Palmtree Leaves",
    nodenames = { "natural_habitat:coconut_palmtree_leaves" },
    interval = 0.5,
    chance = 1,
    action = function(pos, node, active_object_count, active_object_count_wider)
        local p1 = vector.offset(pos, -1, -1, -1);
        local p2 = vector.offset(pos, 1, 1, 1);
        local find_nodes = core.find_nodes_in_area(p1, p2, {"natural_habitat:coconut_palmtree_log"});
        if (#find_nodes < 1) then
            core.set_node(pos, {name="air"} );
        end;
    end,
});

-- ABM - COCONUT HANGING
core.register_abm({
    label = "Coconut Hanging",
    nodenames = { "natural_habitat:coconut_hanging" },
    interval = 0.3,
    chance = 1,
    action = function(pos, node, active_object_count, active_object_count_wider)
        local node = core.get_node(vector.offset(pos, 0, 1, 0));
        if node and (node.name ~= "natural_habitat:coconut_palmtree_leaves") then
            core.set_node(pos, {name="natural_habitat:coconut"} );
            core.check_for_falling(pos);
        end;
    end,
});

-- REGISTER DECORATION
m7a.register_multideco("natural_habitat:deco_coconut_palmtree", {
    smart_place = true,
    deco_type="schematic",
    sidelen = 1,
    fill_ratio = 0.005,
    spawn_by = { "air" },
    num_spawn_by = 1,
    decos = {
        { schematic=schempath.."/nbt_coconut_palmtree_1.mts", },
        { schematic=schempath.."/nbt_coconut_palmtree_2.mts", },
        { schematic=schempath.."/nbt_coconut_palmtree_3.mts", },
    },
    avoid_nodes = { "natural_habitat:coconut_palmtree_log" },
    avoid_radius = 3,
    
    pursue_nodes = { dp.water_source },
    pursue_radius = 5,
    
    biomes = {
        dp.biomes.meadow, dp.biomes.meadow.."_shore", dp.biomes.meadow.."_ocean",
        dp.biomes.desert, dp.biomes.desert.."_shore", dp.biomes.desert.."_ocean",
        dp.biomes.rainforest, --dp.biomes.rainforest.."_shore", dp.biomes.rainforest.."_ocean",
    },
    
    place_on = { dp.soil_with_grass, dp.sand },
    
    y_min = 2,
    y_max = 50,
});