-- Freeslot.
freeslot("MT_EMERALDDISPLAY", "S_EMERALDDISPLAY", "S_EMERALDDISPLAY_HIDE", "S_EMERALDDISPLAY_TOKEN",
        "MT_EMERALDSPARKLE","S_EMERALDSPARKLE1", "S_EMERALDSPARKLE2", "SPR_ESPK")

-- Key thing
local display = 0

addHook("KeyDown", function(keyevent)
    if keyevent.repeated then return end
    local aa, ab = input.gameControlToKeyNum(GC_SCORES)
    local ba, bb = input.gameControl2ToKeyNum(GC_SCORES)
    if keyevent.num == aa or keyevent.num == ab then
        display = $|1
    end
    if keyevent.num == ba or keyevent.num == bb then
        display = $|2
    end
end)

addHook("KeyUp", function(keyevent)
    if keyevent.repeated then return end
    local aa, ab = input.gameControlToKeyNum(GC_SCORES)
    local ba, bb = input.gameControl2ToKeyNum(GC_SCORES)
    if keyevent.num == aa or keyevent.num == ab then
        display = $ & ~1
    end
    if keyevent.num == ba or keyevent.num == bb then
        display = $ & ~2
    end
end)

-- The hooks
addHook("PlayerThink", function(player)
    if player == consoleplayer and display&1
    or player == secondarydisplayplayer and display&2 then
        player.gamestatus = true
    else player.gamestatus = false end
    
    if not player.mo or not player.mo.valid then return end
    if player.gamestatus then
        hud.disable("coopemeralds")
        
        if not player.displayemeralds or not player.displayemeralds[1] or not player.displayemeralds[1].valid then
            player.displayemeralds = {}
            for i = 1, 7 do
                local a = P_SpawnMobjFromMobj(player.mo, 0,0,player.height/2, MT_EMERALDDISPLAY)
                a.angle = FixedAngle(360*FRACUNIT/7 * i)
                player.displayemeralds[i] = a
                a.scale = player.mo.scale
                a.target = player.mo
                a.emeralddisplay = i
            end
        end
    end
end)

local coltable = {
    SKINCOLOR_EMERALD,
    SKINCOLOR_MAGENTA,
    SKINCOLOR_BLUE,
    SKINCOLOR_SKY,
    SKINCOLOR_YELLOW,
    SKINCOLOR_RED,
    SKINCOLOR_GREY
}

local function firstNotTaken(e)
    for i = 0, 6 do
        if not (e&(2^i)) then return i+1 end
    end
    return 0
end

addHook("MobjThinker", function(mo)
    if not mo.target or not mo.target.valid or not mo.target.player or not mo.target.player.gamestatus then -- kys
        mo.target.player.displayemeralds[mo.emeralddisplay] = nil
        P_RemoveMobj(mo)
        return
    end
    
    -- Update position and scale
    local a = ANG1+4900000
    if mo.target.player.powers[pw_super] then a = $*4
    elseif P_SuperReady(mo.target.player) then a = $*2 end
    mo.angle = $+a
    mo.scale = mo.target.scale
    P_TeleportMove(mo, mo.target.x+FixedMul(32*mo.scale, cos(mo.angle)),
            mo.target.y+FixedMul(32*mo.scale, sin(mo.angle)),
            mo.target.z+(mo.target.height/2)+FixedMul(10*mo.scale, cos(mo.angle-mo.target.angle+ANG60)))
    
    -- Sprite
    local e = emeralds
    if mo.target.player.powers[pw_emeralds] then e = mo.target.player.powers[pw_emeralds] end
    
    if e & 2^(mo.emeralddisplay-1) then mo.state = S_EMERALDDISPLAY; mo.frame = FF_FULLBRIGHT|(mo.emeralddisplay-1)
    else mo.state = S_EMERALDDISPLAY_HIDE end
    
    if token and mo.emeralddisplay == firstNotTaken(e) then mo.state = S_EMERALDDISPLAY_TOKEN end
    
    -- Sparkle
    if mo.state != S_EMERALDDISPLAY_HIDE and leveltime&1 then
        local g = P_SpawnMobjFromMobj(mo, 0,0,8*FRACUNIT, MT_EMERALDSPARKLE)
        g.color = coltable[mo.emeralddisplay]
    end
end, MT_EMERALDDISPLAY)