freeslot(
    "MT_PLAYERSHOCK",
    "S_PLAYERSHOCK_INTRO_A",
    "S_PLAYERSHOCK_INTRO_B",
    "S_PLAYERSHOCK_LOOP_C",
    "S_PLAYERSHOCK_LOOP_D",
    "S_PLAYERSHOCK_OUTRO_B",
    "S_PLAYERSHOCK_OUTRO_A"
)

states[S_PLAYERSHOCK_INTRO_A] = {SPR_WHAT, 0|FF_FULLBRIGHT, 3, nil, 0, 0, S_PLAYERSHOCK_INTRO_B}
states[S_PLAYERSHOCK_INTRO_B] = {SPR_WHAT, 1|FF_FULLBRIGHT, 3, nil, 0, 0, S_PLAYERSHOCK_LOOP_C}
states[S_PLAYERSHOCK_LOOP_C]  = {SPR_WHAT, 2|FF_FULLBRIGHT, 4, nil, 0, 0, S_PLAYERSHOCK_LOOP_D}
states[S_PLAYERSHOCK_LOOP_D]  = {SPR_WHAT, 3|FF_FULLBRIGHT, 4, nil, 0, 0, S_PLAYERSHOCK_LOOP_C}
states[S_PLAYERSHOCK_OUTRO_B] = {SPR_WHAT, 1|FF_FULLBRIGHT, 3, nil, 0, 0, S_PLAYERSHOCK_OUTRO_A}
states[S_PLAYERSHOCK_OUTRO_A] = {SPR_WHAT, 0|FF_FULLBRIGHT, 3, nil, 0, 0, S_NULL}

mobjinfo[MT_PLAYERSHOCK] = {
    doomednum = -1,
    spawnstate = S_PLAYERSHOCK_INTRO_A,
    spawnhealth = 1000,
    radius = 8*FRACUNIT,
    height = 8*FRACUNIT,
    mass = 100,
    flags = MF_NOBLOCKMAP|MF_NOGRAVITY|MF_SCENERY
}

addHook("PlayerThink", function(p)
    if not (p and p.mo and p.mo.valid) then return end

    if (p.cmd.buttons & BT_FIRENORMAL) and not p.last_firenormal and P_IsObjectOnGround(p.mo) and p.mo.state == S_PLAY_STND then
        if p.mo.player_alert_obj and p.mo.player_alert_obj.valid then
            P_RemoveMobj(p.mo.player_alert_obj)
        end

        local what = P_SpawnMobj(p.mo.x, p.mo.y, p.mo.z + 64*FRACUNIT, MT_PLAYERSHOCK)
        if what and what.valid then
            what.target = p.mo
            p.mo.player_alert_obj = what
            p.player_alert_timer = 3 * TICRATE
            p.mo.state = S_PLAY_DEAD
            S_StartSound(p.mo, sfx_alart)
            p.powers[pw_nocontrol] = 999
        end
    end
    p.last_firenormal = (p.cmd.buttons & BT_FIRENORMAL)

    if p.player_alert_timer and p.player_alert_timer > 0 then
        p.player_alert_timer = $ - 1
        
        if p.mo.player_alert_obj and p.mo.player_alert_obj.valid then
            P_SetOrigin(p.mo.player_alert_obj, p.mo.x, p.mo.y, p.mo.z + 64*FRACUNIT)
        end

        if p.player_alert_timer == 6 then
            if p.mo.player_alert_obj and p.mo.player_alert_obj.valid then
                p.mo.player_alert_obj.state = S_PLAYERSHOCK_OUTRO_B
            end
        end

        if p.player_alert_timer == 0 then
            p.mo.state = S_PLAY_STND
            p.powers[pw_nocontrol] = 1
        end
    end
end)