
artifact.registered_nodes = {}

function artifact.register_node(name, def)
    artifact.registered_nodes[name] = def
    def._name = name
    if not name:find ":" then
        name = "artifact:"..name
    end
    if artifact.debug then
        if not def.groups then def.groups = {} end
        def.groups.dig_immediate = 3
    end
    if def._variants then
        for _, x in ipairs(def._variants) do
            if x == "slab" then
                artifact.register_slab(def)
            elseif x == "stair" then
                artifact.register_stair(def)
            end
        end
    end
    minetest.register_node(":"..name, def)
    if name ~= def._name then
        minetest.register_alias(def._name, name)
    end
end

function artifact.register_craftitem(name, def)
    def._name = name
    if not name:find ":" then
        name = "artifact:"..name
    end 
    minetest.register_craftitem(":"..name, def)
    if name ~= def._name then
        minetest.register_alias(def._name, name)
    end
end

function artifact.register_slab(def)
    def = table.copy(def)
    def._variants = nil
    
    artifact.register_node(def._name.."_slab", extend(def, {
        drawtype = "nodebox",
        node_box = {
            type = "fixed",
            fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5}
        },
        paramtype = "light",
        paramtype2 = "facedir"
    }))
end

function artifact.register_stair(def)
    def = table.copy(def)
    def._variants = nil
    
    def.groups[def._name.."_stair"] = 1
    
    artifact.register_node(def._name.."_stair", extend(table.copy(def), {
        drawtype = "nodebox",
        node_box = {
            type = "fixed",
            fixed = {{-0.5, -0.5, -0.5, 0.5, 0, 0.5}, {-0.5, -0.5, 0, 0.5, 0.5, 0.5}}
        },
        paramtype = "light",
        paramtype2 = "facedir"
    }))

    artifact.register_node(def._name.."_stair_inner", extend(table.copy(def), {
        drawtype = "nodebox",
        node_box = {
            type = "fixed",
            fixed = {{-0.5, -0.5, -0.5, 0.5, 0, 0.5}, {-0.5, -0.5, 0, 0.5, 0.5, 0.5}, {-0.5, -0.5, -0.5, 0, 0.5, 0.5}}
        },
        paramtype = "light",
        paramtype2 = "facedir"
    }))

    artifact.register_node(def._name.."_stair_outer", extend(def, {
        drawtype = "nodebox",
        node_box = {
            type = "fixed",
            fixed = {{-0.5, -0.5, -0.5, 0.5, 0, 0.5}, {-0.5, 0, 0.5, 0, 0.5, 0}}
        },
        paramtype = "light",
        paramtype2 = "facedir"
    }))
end

local function rep(tx, size)
    local out = "[combine:"..(size *16).."x"..(size *16)
    for x = 0, size -1 do
        for y = 0, size -1 do
            out = out..":"..(x *16)..","..(y *16).."="..tx
        end
    end
    return out
end

artifact.register_node("stone", {
    tiles = {{name = "artifact_stone.png", align_style = "world"}},
    _variants = {"stair", "slab"}
})
artifact.register_node("stone_mossy", {
    tiles = {{name = rep("artifact_stone.png", 4).."^artifact_moss.png", align_style = "world", scale = 4}},
    _variants = {"stair", "slab"},
})

artifact.register_node("stone_bricks", {
    tiles = {{name = "artifact_stone_bricks.png", align_style = "world"}},
    _variants = {"stair", "slab"},
})
artifact.register_node("stone_bricks_mossy", {
    tiles = {{name = rep("artifact_stone_bricks.png", 4).."^artifact_moss_bricks.png", align_style = "world", scale = 4}},
    _variants = {"stair", "slab"},
})

artifact.register_node("stone_bricks_small", {
    tiles = {{name = "artifact_stone_bricks_small.png", align_style = "world"}},
    _variants = {"stair", "slab"},
})

artifact.register_node("stone_tile", {
    tiles = {{name = "artifact_stone_tile.png", align_style = "world"}},
    _variants = {"stair", "slab"},
})

artifact.register_node("stone_tile_small", {
    tiles = {{name = "artifact_stone_tile_small.png", align_style = "world"}},
    _variants = {"stair", "slab"},
})


artifact.register_node("vines", {
    drawtype = "nodebox",
    node_box = {
        type = "fixed",
        fixed = {
            -0.5, -0.5, 0.48,
             0.5,  0.5, 0.48
        }
    },
    walkable = false,
    selection_box = {
        type = "fixed",
        fixed = {
            -0.5, -0.5, 0.5,
             0.5,  0.5, 0.45
        }
    },
    paramtype = "light",
    paramtype2 = "facedir",
    tiles = {"artifact_vines.png"},
    use_texture_alpha = "clip"
})
artifact.register_node("vines_dry", {
    drawtype = "nodebox",
    node_box = {
        type = "fixed",
        fixed = {
            -0.5, -0.5, 0.48,
             0.5,  0.5, 0.48
        }
    },
    walkable = false,
    selection_box = {
        type = "fixed",
        fixed = {
            -0.5, -0.5, 0.5,
             0.5,  0.5, 0.45
        }
    },
    paramtype = "light",
    paramtype2 = "facedir",
    tiles = {"artifact_vines_dry.png"},
    use_texture_alpha = "clip"
})

artifact.register_node("leaves", {
    drawtype = "allfaces",
--    paramtype = "light",
    tiles = {"artifact_leaves.png"},
    use_texture_alpha = "clip"
})
artifact.register_node("leaves_dry", {
    drawtype = "allfaces",
--    paramtype = "light",
    tiles = {"artifact_leaves_dry.png"},
    use_texture_alpha = "clip"
})


artifact.register_node("wood_planks", {
    tiles = {{name = "artifact_wood_planks.png", align_style = "world"}},
    _variants = {"stair", "slab"},
})

artifact.register_node("ladder_wood", {
    drawtype = "mesh",
    mesh = "artifact_ladder.obj",
    paramtype = "light",
    paramtype2 = "facedir",
    tiles = {"artifact_ladder_wood.png"},
    walkable = false,
    climbable = true
})


artifact.register_node("glass", {
    drawtype = "glasslike",
    use_texture_alpha = "clip",
    tiles = {"artifact_glass.png"},
    _variants = {"stair", "slab"},
})


local function register_lamp(color, brightness)
    artifact.register_node("lamp_"..color, {
        drawtype = "mesh",
        mesh = "artifact_lamp.obj",
        tiles = {"artifact_lamp_"..color..".png"},
        light_source = brightness,
        paramtype = "light",
        sunlight_propagates = true
    })
    artifact.register_node("lamp_"..color.."_hanging", {
        drawtype = "mesh",
        mesh = "artifact_lamp_hanging.obj",
        tiles = {"artifact_lamp_"..color..".png"},
        light_source = brightness,
        paramtype = "light",
        sunlight_propagates = true
    })
end

register_lamp("red", 6)
register_lamp("gold", 8)
register_lamp("blue", 10)


artifact.register_node("light", {
    tiles = {"artifact_light.png"},
    paramtype = "light",
    light_source = 14
})

-- Make darkness the default.
minetest.override_item("air", {
    sunlight_propagates = false,
    light_source = 2
})

if artifact.debug then

    minetest.register_mapgen_script(minetest.get_modpath(minetest.get_current_modname()).."/mapgen.lua")
    
    minetest.register_decoration {
        deco_type = "simple",
        decoration = "lamp_blue",
        place_on = "stone",
        fill_ratio = 0.02,
    }

end
