-- "I'll stop calling it Minetest when it stops being one."
minetest = core

artifact = {
    debug = true
}

-- For brevity.
function include(file)
    dofile(minetest.get_modpath(minetest.get_current_modname()).."/"..file)
end

function enum(cases)
    local out = {}
    local i = 0
    for _, x in ipairs(cases) do
        out[x] = i
        i = i +1
    end
    return out
end

say = minetest.chat_send_all

function extend(dst, src)
    for k, v in pairs(src) do
        dst[k] = v
    end
    return dst
end

-- Some kind of promise API. (Forget about async-await, though.)
function Promise(fn)
    local p = {resolved = false}
    p.resolve = function(...)
        if p.resolved then return end
        p.resolved = true
        if p.after then p.after(...) end
    end
    fn(p.resolve)
    return {
        after = function(fn)
            p.after = fn
        end
    }
end

-- HACK: Lookup table for getting a rotation from a
-- facedir (because Minetest doesn't have any way
-- to get this)
local facedir_rotations = {
    -- +Y
    [0] = vector.new(0,0,0),
    [1] = vector.new(0, math.pi *1.5, 0),
    [2] = vector.new(0, math.pi, 0),
    [3] = vector.new(0, math.pi *0.5, 0),
    
    -- +Z
    [4] = vector.new(math.pi *1.5, 0, 0),
    [5] = vector.new(math.pi *1.5, math.pi *1.5, 0),
    [6] = vector.new(math.pi *1.5, math.pi, 0),
    [7] = vector.new(math.pi *1.5, math.pi *0.5, 0),
}
function artifact.facedir_to_rotation(facedir)
    return facedir_rotations[facedir] or minetest.facedir_to_dir(facedir):dir_to_rotation()
end


minetest.register_lbm{
    name = ":artifact:on_load",
    nodenames = {"group:call_on_load"},
    action = function(pos, node)
        minetest.registered_nodes[node.name].on_load(pos)
    end
}

if artifact.debug then
    minetest.register_chatcommand("chest", {
        func = function(name, args)
            minetest.registered_chatcommands.giveme.func(name, "chest_with_everything:chest")
        end
    })
end

