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


aom_stone = {}


minetest.register_node("aom_stone:stone", {
    description = S("Stone"),
    _tt_long_desc = S("What type of stone? Yes."),
    groups = {
        full_solid = 1, nature = 1, stone = 1, oddly_breakable_by_hand = 3,
        solid = 1, suffocates = 2, cracky = 1, silk_touchable = 1, natural_stone = 1 },
    tiles = { "aom_stone_stone.png" },
    drop = "aom_stone:cobble",
    sounds = aom_sounds.default_stone(),
    _scrap = {
        ["aom_scrap:stone"] = 8,
    },
    -- light_source = 10,
}) aom_util.register_all_shapes("aom_stone:stone")

minetest.register_node("aom_stone:sandstone", {
    description = S("Sandstone"),
    _tt_long_desc = S(""),
    groups = {
        full_solid = 1, nature = 1, sandstone = 1, oddly_breakable_by_hand = 3,
        solid = 1, suffocates = 2, cracky = 1, stone = 1, natural_stone = 1 },
    tiles = { "aom_stone_sandstone.png" },
    sounds = aom_sounds.default_stone(),
}) aom_util.register_all_shapes("aom_stone:sandstone", {}, {remove_groups={"natural_stone"}})

minetest.register_node("aom_stone:granite", {
    description = S("Granite"),
    groups = {
        full_solid = 1, nature = 1, granite = 1, oddly_breakable_by_hand = 3,
        solid = 1, suffocates = 2, cracky = 1, stone = 1, natural_stone = 1 },
    tiles = { "aom_stone_granite.png" },
    sounds = aom_sounds.default_stone(),
}) aom_util.register_all_shapes("aom_stone:granite", {}, {remove_groups={"natural_stone"}})

minetest.register_node("aom_stone:limestone", {
    description = S("Limestone"),
    groups = {
        full_solid = 1, nature = 1, limestone = 1, oddly_breakable_by_hand = 3,
        solid = 1, suffocates = 2, cracky = 1, stone = 1, natural_stone = 1 },
    tiles = { "aom_stone_limestone.png" },
    sounds = aom_sounds.default_stone(),
}) aom_util.register_all_shapes("aom_stone:limestone", {}, {remove_groups={"natural_stone"}})

minetest.register_node("aom_stone:limestone_cobble", {
    description = S("Cobbled Limestone"),
    groups = {
        full_solid = 1, nature = 1, limestone_cobble = 1, oddly_breakable_by_hand = 3,
        solid = 1, suffocates = 2, cracky = 1, stone = 1 },
    tiles = { "aom_stone_limestone_cobble.png" },
    sounds = aom_sounds.default_stone(),
}) aom_util.register_all_shapes("aom_stone:limestone_cobble")
minetest.register_craft({
        output = "aom_stone:limestone_cobble",
        recipe = {{"aom_stone:limestone"}},
})

minetest.register_node("aom_stone:cobble", {
    description = S("Cobblestone"),
    groups = {
        full_solid = 1, nature = 1, cobble = 1, oddly_breakable_by_hand = 3,
        solid = 1, suffocates = 2, cracky = 1, stone = 1, natural_stone = 1},
    tiles = { "aom_stone_cobble.png" },
    sounds = aom_sounds.default_stone(),
    _scrap = {
        ["aom_scrap:stone"] = 8,
    },
}) aom_util.register_all_shapes("aom_stone:cobble", {}, {remove_groups={"cobble"}})

aom_tcraft.register_craft({
    output = "aom_stone:stone 10",
    items = {
        ["aom_stone:cobble"] = 10,
    }
})

minetest.register_node("aom_stone:cobble_moss_1", {
    description = S("Slightly Mossy Cobble"),
    groups = {
        full_solid = 1, nature = 1, cobble_moss_1 = 1, oddly_breakable_by_hand = 3,
        solid = 1, suffocates = 2, cracky = 1, stone = 1, mossy_stone = 1, },
    tiles = { "aom_stone_cobble.png^aom_stone_cobble_moss_1.png" },
    drop = "aom_stone:cobble_moss_1",
    sounds = aom_sounds.default_stone(),
    _scrap = {
        ["aom_scrap:stone"] = 8,
    },
}) aom_util.register_all_shapes("aom_stone:cobble_moss_1")

aom_tcraft.register_craft({
    output = "aom_stone:cobble_moss_1 10",
    extra_items = {
        "aom_items:wooden_cup",
    },
    items = {
        ["aom_items:wooden_cup_water"] = 1,
        ["aom_stone:cobble"] = 10,
    },
})

minetest.register_node("aom_stone:cobble_moss_2", {
    description = S("Very Mossy Cobble"),
    groups = {
        full_solid = 1, nature = 1, cobble_moss_2 = 1, oddly_breakable_by_hand = 3,
        solid = 1, suffocates = 2, cracky = 1,    stone = 1, mossy_stone = 1, },
    tiles = { "aom_stone_cobble.png^aom_stone_cobble_moss_2.png" },
    drop = "aom_stone:cobble_moss_2",
    sounds = aom_sounds.default_stone(),
    _scrap = {
        ["aom_scrap:stone"] = 8,
    },
}) aom_util.register_all_shapes("aom_stone:cobble_moss_2")

aom_tcraft.register_craft({
    output = "aom_stone:cobble_moss_2 10",
    extra_items = {
        "aom_items:wooden_cup",
    },
    items = {
        ["aom_items:wooden_cup_water"] = 1,
        ["aom_stone:cobble_moss_1"] = 10,
    },
})

minetest.register_node("aom_stone:obsidian", {
    description = S("Obsidian"),
    groups = {
        full_solid = 1, nature = 1, obsidian = 1, oddly_breakable_by_hand = 3,
        cracky = 4, solid = 1, suffocates = 2, },
    tiles = {"aom_obsidian.png"},
    sounds = aom_sounds.default_stone(),
}) aom_util.register_all_shapes("aom_stone:obsidian", {}, {remove_groups={"obsidian"}})

aom_tcraft.register_craft({
    output = "aom_stone:obsidian 80",
    extra_items = {
        "aom_elementals:elemental_core_stone",
    },
    items = {
        ["aom_elementals:elemental_core_stone"] = 1,
        ["aom_items:iron_nugget"] = 1,
        ["aom_stone:cobble"] = 80,
    },
})

-- copy an existing node and make it an ore
function aom_stone.register_ore_node(in_node, oredef, flags)
    local name = in_node .. "_" .. oredef.ore_id .. "_ore"
    if flags and flags.blacklist and flags.blacklist[in_node] then return end
    if minetest.registered_nodes[name] then
        return
    end
    local def = table.copy(minetest.registered_nodes[in_node])
    def.groups = table.copy(def.groups)
    if not def then error(in_node.." is not a real node! Cannot make an ore from it!") end

    flags = flags or {}
    def.groups.natural_stone = nil
    def.description = (oredef.name .. " ore")
    def.drop = oredef.drop
    def.groups["item_"..oredef.ore_id .. "_ore"] = 1
    def.groups.ore = 1
    def.light_source = math.max(def.glow or 0, oredef.glow or 0)
    if def.light_source > 0 and not def.paramtype then
        def.paramtype = "light"
    end

    for group, val in pairs(oredef.groups or {}) do
        def.groups[group] = val
    end
    for group, val in pairs(flags.groups or {}) do
        def.groups[group] = val
    end

    if type(def.tiles[1]) == "table" then
        error(in_node.." does not have simple tiles format.")
    else
        for i, texture in ipairs(def.tiles) do
            def.tiles[i] = texture .. "^" .. oredef.tiles_overlay
        end
    end

    minetest.register_node(name, def)
end

aom_stone.already_registered_ore_nodes = {}
aom_stone.registered_ore_types = {}
-- adds an ore type (e.g. "iron") which will have its ore added to all nodes that want ores placed in them
-- also registers nodes for "host nodes" that have already been defined, so that you don't get a race condition
function aom_stone.register_ore_type(ore_id, oredef)
    oredef.ore_id = ore_id
    aom_stone.registered_ore_types[ore_id] = oredef
    for node_name, flags in pairs(aom_stone.registered_ore_host_nodes) do
        aom_stone.register_ores_for_node(node_name, flags, true)
    end
end

aom_stone.registered_ore_host_nodes = {}
-- makes all nodes combinations for every ore currently defined
function aom_stone.register_ores_for_node(node_name, flags, skipdef)
    if not skipdef then
        aom_stone.registered_ore_host_nodes[node_name] = flags
    end

    for ore_id, oredef in pairs(aom_stone.registered_ore_types) do
        aom_stone.register_ore_node(node_name, oredef, flags)
    end
end


-- list each ore and its drops so you can batch register them later
aom_stone.register_ore_type("iron", {
    name = "Iron",
    tiles_overlay = "aom_iron_overlay.png",
    drop = {
        max_items = 1,
        items = {
            {
                items = {"aom_items:iron_nugget"},
            },
        },
    },
})
aom_stone.register_ore_type("tin", {
    name = "Tin",
    tiles_overlay = "aom_tin_overlay.png",
    drop = {
        max_items = 1,
        items = {
            {
                items = {"aom_items:tin_nugget"},
            },
        },
    },
})
aom_stone.register_ore_type("copper", {
    name = "Copper",
    tiles_overlay = "aom_copper_overlay.png",
    drop = {
        max_items = 1,
        items = {
            {
                items = {"aom_items:copper_nugget"},
            },
        },
    },
})
aom_stone.register_ore_type("lapis", {
    name = "Lapis",
    tiles_overlay = "aom_lapis_overlay.png",
    drop = {
        max_items = 7,
        items = {
            {
                items = {"aom_items:lapis_lazuli 2"},
            },
            {
                items = {"aom_items:lapis_lazuli 2"},
                rarity = 2
            },
            {
                items = {"aom_items:lapis_lazuli 3"},
                rarity = 6
            },
        },
    },
})
aom_stone.register_ore_type("diamond", {
    name = "Diamond",
    tiles_overlay = "aom_diamond_overlay.png",
    drop = {
        max_items = 1,
        items = {
            {
                items = {"aom_items:diamond"},
            },
        },
    },
})
aom_stone.register_ore_type("coal", {
    name = "Coal",
    tiles_overlay = "aom_coal_overlay.png",
    drop = {
        max_items = 1,
        items = {
            {
                items = {"aom_items:coal"},
            },
        },
    },
})
aom_stone.register_ore_type("gold", {
    name = "Gold",
    tiles_overlay = "aom_gold_overlay.png",
    drop = {
        max_items = 1,
        items = {
            {
                items = {"aom_items:gold_nugget"},
            },
        },
    },
})
aom_stone.register_ore_type("ignis", {
    name = "Ignis",
    tiles_overlay = "aom_ignis_overlay.png",
    drop = {
        max_items = 5,
        items = {
            {
                items = {"aom_items:ignis 2"},
            },
            {
                items = {"aom_items:ignis"},
                rarity = 2,
            },
            {
                items = {"aom_items:ignis 2"},
                rarity = 2,
            },
        },
    },
})

-- make all the ore nodes for these base nodes
aom_stone.register_ores_for_node("aom_stone:stone")
aom_stone.register_ores_for_node("aom_stone:limestone")
aom_stone.register_ores_for_node("aom_stone:sandstone")
aom_stone.register_ores_for_node("aom_stone:granite")




minetest.register_node("aom_stone:bedrock", {
    description = S("Bedrock"),
    groups = {
        full_solid = 1, nature = 1, solid = 1, suffocates = 2, unobtainable = 1 },
    tiles = { "aom_bedrock.png" },
    drop = "",
    sounds = aom_sounds.default_stone(),
    sunlight_propagates = true,
    diggable = false,
    on_dig = function(pos, node, digger)
        local name = (digger:is_player() and digger:get_player_name()) or dump(digger)
        minetest.log("\n\n[!!!] Bedrock was dug at " .. tostring(pos) .. " by " .. name .. "\n\n")
    end
})

