// IW6 GSC SOURCE
// Generated by https://github.com/xensik/gsc-tool

init()
{
    thread onplayerconnect();
}

onplayerconnect()
{
    for (;;)
    {
        level waittill( "connected", var_0 );
        var_0 thread onplayerspawned();
    }
}

onplayerspawned()
{
    self endon( "disconnect" );

    for (;;)
    {
        self waittill( "spawned_player" );
        thread monitorempgrenade();
    }
}

monitorempgrenade()
{
    self endon( "death" );
    self endon( "disconnect" );
    self endon( "faux_spawn" );
    self.empendtime = 0;

    for (;;)
    {
        self waittill( "emp_damage", var_0, var_1 );

        if ( !isalive( self ) || isdefined( self.usingremote ) || maps\mp\_utility::_hasperk( "specialty_empimmune" ) || !isdefined( var_0 ) )
            continue;

        var_2 = 1;
        var_3 = 0;

        if ( level.teambased && var_0 != self && isdefined( var_0.pers["team"] ) && var_0.pers["team"] == self.pers["team"] )
        {
            if ( level.friendlyfire == 0 )
                continue;
            else if ( level.friendlyfire == 1 )
            {
                var_3 = 0;
                var_2 = 1;
            }
            else if ( level.friendlyfire == 2 )
            {
                var_2 = 0;
                var_3 = 1;
            }
            else if ( level.friendlyfire == 3 )
            {
                var_3 = 1;
                var_2 = 1;
            }
        }
        else
        {
            var_0 notify( "emp_hit" );

            if ( var_0 != self )
                var_0 maps\mp\gametypes\_missions::processchallenge( "ch_onthepulse" );
        }

        if ( var_2 && isdefined( self ) )
            thread applyemp( var_1 );

        if ( var_3 )
            var_0 thread applyemp( var_1 );
    }
}

applyemp( var_0 )
{
    self notify( "applyEmp" );
    self endon( "applyEmp" );
    self endon( "disconnect" );
    self endon( "death" );
    wait 0.05;
    self.empgrenaded = 1;
    self shellshock( "flashbang_mp", 1 );
    self.empendtime = gettime() + var_0 * 1000;
    maps\mp\killstreaks\_emp_common::applyperplayerempeffects_ondetonate();
    maps\mp\killstreaks\_emp_common::applyperplayerempeffects();
    thread emprumbleloop( 0.75 );
    thread empgrenadedeathwaiter();
    wait( var_0 );
    self notify( "empGrenadeTimedOut" );
    checktoturnoffemp();
}

empgrenadedeathwaiter()
{
    self notify( "empGrenadeDeathWaiter" );
    self endon( "empGrenadeDeathWaiter" );
    self endon( "empGrenadeTimedOut" );
    self waittill( "death" );
    checktoturnoffemp();
}

checktoturnoffemp()
{
    self.empgrenaded = 0;

    if ( !maps\mp\killstreaks\_emp_common::shouldplayerbeaffectedbyemp() )
        maps\mp\killstreaks\_emp_common::removeperplayerempeffects();
}

emprumbleloop( var_0 )
{
    self endon( "emp_rumble_loop" );
    self notify( "emp_rumble_loop" );
    var_1 = gettime() + var_0 * 1000;

    while ( gettime() < var_1 )
    {
        self playrumbleonentity( "damage_heavy" );
        wait 0.05;
    }
}

isempgrenaded()
{
    return isdefined( self.empendtime ) && gettime() < self.empendtime;
}
