print("Thanks for downloading my mod! Now, let's ROK THIS OUT!!!!")

local MT_NOTE = freeslot("MT_NOTE")
local sfx_NOTEXP = freeslot("sfx_NOTEXP")

----------------------------------------------------
-- 1. COOLDOWN
----------------------------------------------------
addHook("AbilitySpecial", function(player)
    if not (player and player.mo and player.mo.valid) then
        return false
    end

    if string.lower(player.mo.skin) ~= "janet" then
        return false
    end

    if player.janet_cooldown == nil then
        player.janet_cooldown = 0
    end

    if player.janet_cooldown > 0 then
        return false
    end

    player.janet_cooldown = 18

    local mo = player.mo

    mo.momz = 5 * FRACUNIT + (FRACUNIT / 2)
    
    mo.eflags = $ & ~MFE_ONGROUND

    local note = P_SpawnMobj(mo.x, mo.y, mo.z - (12 * FRACUNIT), MT_NOTE)
    if note and note.valid then
        note.target = mo
        note.momz = -15 * FRACUNIT
        note.momx = mo.momx / 2
        note.momy = mo.momy / 2

        S_StartSound(note, sfx_NOTEXP)
    end

    return true
end)

----------------------------------------------------
-- 2. PLAYERTHINK: PHYSICS AND COOLDOWN
----------------------------------------------------
addHook("PlayerThink", function(player)
    if not (player and player.mo and player.mo.valid) then
        return
    end

    if string.lower(player.mo.skin) ~= "janet" then
        return
    end

    if player.janet_cooldown == nil then
        player.janet_cooldown = 0
    end

    if player.janet_cooldown > 0 then
        player.janet_cooldown = player.janet_cooldown - 1
    end

    -- Keep air momentum nice and easy
    local isAirborne = not ((player.mo.eflags & MFE_ONGROUND) or P_IsObjectOnGround(player.mo))
    if isAirborne then
        player.pflags = $ | PF_GLIDING
        player.pflags = $ & ~PF_JUMPED
    else
        player.janet_cooldown = 0
    end
end)