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

init()
{
    level thread populateintelchallenges();
    level.intelactive = 0;

    if ( !isdefined( level.supportintel ) || level.supportintel )
        level thread onplayerconnect();

    if ( isdefined( level.enableteamintel ) && isdefined( level.enableteamintel ) )
        level thread runteamintel();
}

populateintelchallenges()
{
    level endon( "game_ended" );
    wait 0.05;
    level.intelchallengearray = [];
    var_0 = level.gametype == "sd" || level.gametype == "sr";
    var_1 = 0;

    for (;;)
    {
        var_2 = tablelookupbyrow( "mp/intelChallenges.csv", var_1, 0 );
        var_3 = int( tablelookupbyrow( "mp/intelChallenges.csv", var_1, 2 ) );
        var_4 = int( tablelookupbyrow( "mp/intelChallenges.csv", var_1, 3 ) );
        var_5 = int( tablelookupbyrow( "mp/intelChallenges.csv", var_1, 4 ) ) == 1;
        var_6 = int( tablelookupbyrow( "mp/intelChallenges.csv", var_1, 5 ) ) == 1;
        var_7 = int( tablelookupbyrow( "mp/intelChallenges.csv", var_1, 6 ) ) == 1;
        var_1++;

        if ( var_2 == "" )
            break;

        if ( var_0 && !var_5 )
            continue;

        level.intelchallengearray[var_2] = spawnstruct();
        level.intelchallengearray[var_2].challengename = var_2;
        level.intelchallengearray[var_2].challengereward = var_3;
        level.intelchallengearray[var_2].challengetarget = var_4;
        level.intelchallengearray[var_2].teamchallenge = var_6;
        level.intelchallengearray[var_2].juggchallenge = var_7;
    }
}

runteamintel()
{
    level endon( "game_ended" );
    level.nummeleekillsintel = 0;
    level.numheadshotsintel = 0;
    level.numkillstreakkillsintel = 0;
    level.numequipmentkillsintel = 0;

    for (;;)
    {
        level waittill( "giveTeamIntel", var_0 );
        var_1 = [];

        foreach ( var_3 in level.intelchallengearray )
        {
            if ( !var_3.teamchallenge )
                continue;

            var_1[var_1.size] = var_3;
        }

        var_5 = var_1[randomint( var_1.size )];
        var_6 = var_5.challengename;
        level maps\mp\gametypes\_intelchallenges::giveteamchallenge( var_6, var_0 );
    }
}

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

inteldeathwatcher( var_0 )
{
    level endon( "game_ended" );
    self endon( "disconnect" );

    for (;;)
    {
        self waittill( "death", var_1 );

        if ( isdefined( self.hasintel ) && self.hasintel )
        {
            if ( isdefined( self.onhelisniper ) && self.onhelisniper || self.sessionstate == "spectator" )
            {
                self.hasintel = 0;
                level thread randassignintel();
            }
            else
                ondropintel();

            continue;
        }

        if ( !level.intelactive )
        {
            if ( level.teambased && isdefined( var_1 ) && isdefined( var_1.team ) && var_1.team == self.team )
                continue;

            level.intelactive = 1;
            ondropintel( 1 );
        }
    }
}

ondropintel( var_0 )
{
    self.hasintel = 0;

    if ( !isdefined( var_0 ) )
        var_0 = 0;

    var_1 = self.origin;

    if ( isdefined( self.inteldeathposition ) )
    {
        var_1 = self.inteldeathposition;
        self.inteldeathposition = undefined;
    }

    var_2 = getgroundposition( var_1, 42, 1000, 72 );
    var_3 = var_1 + ( 0, 0, 32 );

    if ( var_0 )
    {
        var_4 = spawn( "script_model", var_3 );
        var_4.angles = ( 0, 0, 0 );
        var_4 setmodel( "com_metal_briefcase_intel" );
        var_5 = spawn( "trigger_radius", var_1, 0, 96, 60 );
        var_5.id = "intel";
        var_6 = [];
        var_6["visuals"] = var_4;
        var_6["trigger"] = var_5;
        var_6["owner"] = "none";
        var_6["isActive"] = 1;
        var_6["firstTriggerPlayer"] = undefined;
        var_6["useRate"] = 1;
        var_6["useTime"] = 0.5;
        var_6["useProgress"] = 0;
        var_6["dropped_time"] = gettime();
        level.intelent = var_6;
        level.intelent["trigger"] enablelinkto();
    }
    else
    {
        if ( isdefined( level.intelent["visuals"] getlinkedparent() ) )
        {
            level.intelent["visuals"] unlink();
            level.intelent["trigger"] unlink();
        }

        level.intelent["visuals"] hide();
        level.intelent["visuals"].origin = var_3;
        level.intelent["trigger"].origin = var_2;
    }

    if ( level.intelent["visuals"] maps\mp\_utility::touchingbadtrigger() )
    {
        level.intelent["isActive"] = 0;
        level.intelent["visuals"] hide();
        level thread randassignintel();
        return;
    }

    level.intelent["owner"] = "none";
    level.intelent["isActive"] = 1;
    level.intelent["dropped_time"] = gettime();
    level.intelent["visuals"] scriptmodelplayanim( "mp_briefcase_spin" );
    thread inteltriggerwatcher();
    level.intelent thread intelemergencyrespawntimer();
}

inteldeathoverride( var_0 )
{
    level.intelent["isActive"] = 0;
    level.intelent["visuals"] hide();
    level thread randassignintel();
}

inteltriggerwatcher()
{
    level notify( "intelTriggerWatcher" );
    level endon( "intelTriggerWatcher" );
    level endon( "game_ended" );
    level.intelent["visuals"] endon( "pickedUp" );
    var_0 = level.intelent["trigger"];
    var_1 = spawnstruct();
    var_1.linkparent = self getmovingplatformparent();
    var_1.endonstring = "intelTriggerWatcher";
    var_1.deathoverridecallback = ::inteldeathoverride;
    level.intelent["visuals"] thread maps\mp\_movers::handle_moving_platforms( var_1 );
    level.intelent["trigger"] linkto( level.intelent["visuals"] );
    wait 0.05;
    level.intelent["visuals"] show();

    for (;;)
    {
        var_0 waittill( "trigger", var_2 );

        if ( !isplayer( var_2 ) )
            continue;

        if ( isai( var_2 ) )
            continue;

        if ( !isalive( var_2 ) || isdefined( var_2.fauxdead ) && var_2.fauxdead )
            continue;

        if ( level.intelent["isActive"] )
        {
            if ( isdefined( var_2.hasintel ) && var_2.hasintel )
                continue;

            var_3 = var_0 proximitythink( var_2 );

            if ( var_3 )
                var_2 onpickupintel();
        }
    }
}

intelemergencyrespawntimer()
{
    level.intelent["visuals"] endon( "pickedUp" );

    for (;;)
    {
        if ( gettime() > level.intelent["dropped_time"] + 60000 )
            break;

        wait 1;
    }

    level.intelent["isActive"] = 0;
    level.intelent["visuals"] hide();
    level thread randassignintel();
}

onpickupintel()
{
    self.hasintel = 1;
    level.intelent["isActive"] = 0;
    level.intelent["visuals"] hide();
    level.intelent["owner"] = self;
    var_0 = [];
    thread maps\mp\gametypes\_rank::giverankxp( "challenge", 100 );

    if ( maps\mp\_utility::isjuggernaut() )
    {
        foreach ( var_2 in level.intelchallengearray )
        {
            var_3 = var_2.challengename;

            if ( !var_2.juggchallenge )
                continue;

            if ( isdefined( self.isjuggernautmaniac ) && self.isjuggernautmaniac == 1 )
            {
                if ( !issubstr( var_3, "maniac" ) )
                    continue;
            }
            else if ( isdefined( self.isjuggernautrecon ) && self.isjuggernautrecon == 1 )
            {
                if ( !issubstr( var_3, "recon" ) )
                    continue;
            }
            else if ( !issubstr( var_3, "assault" ) )
                continue;

            var_0[var_0.size] = var_2;
        }
    }
    else
    {
        var_5 = maps\mp\_utility::getotherteam( self.team );
        var_6 = level.teamcount[var_5];
        var_7 = level.alivecount[var_5];
        var_8 = undefined;
        var_8 = getdvarint( "scr_player_lives" ) == 0;
        var_9 = self getweaponslistprimaries();

        foreach ( var_2 in level.intelchallengearray )
        {
            var_3 = var_2.challengename;
            var_11 = var_2.challengetarget;

            if ( !var_8 )
            {
                if ( var_3 == "ch_intel_tbag" || var_11 > var_7 )
                    continue;
            }

            if ( var_3 == "ch_intel_secondarykills" && intelcancomplete_secondarykills( self, var_9 ) == 0 )
                continue;

            if ( var_3 == "ch_intel_explosivekill" && intelcancomplete_explosivekill( self, var_9 ) == 0 )
                continue;

            if ( var_2.teamchallenge )
                continue;

            if ( var_2.juggchallenge )
                continue;

            var_0[var_0.size] = var_2;
        }
    }

    var_13 = var_0[randomint( var_0.size )];
    var_14 = var_13.challengename;

    if ( maps\mp\_utility::isjuggernaut() )
        maps\mp\gametypes\_intelchallenges::givejuggernautchallenge( var_14 );
    else
    {
        maps\mp\gametypes\_intelchallenges::givechallenge( var_14 );
        thread watchforjuggernaut();
    }

    thread watchforplayerdisconnect();
    thread watchforremotekillstreak();
    level.intelent["visuals"] notify( "pickedUp" );
}

intelcancomplete_secondarykills( var_0, var_1 )
{
    var_2 = 0;

    foreach ( var_4 in var_1 )
    {
        if ( maps\mp\_utility::iscacsecondaryweapon( var_4 ) )
        {
            if ( var_0 getammocount( var_4 ) > 0 )
            {
                var_2 = 1;
                break;
            }
        }
    }

    return var_2;
}

intelcancomplete_explosivekill( var_0, var_1 )
{
    var_2 = 0;

    if ( self.loadoutperkequipment != "specialty_null" && !issubstr( self.loadoutperkequipment, "throwingknife" ) )
    {
        var_3 = var_0 getammocount( self.loadoutperkequipment );

        if ( var_3 > 0 )
            var_2 = 1;
    }

    if ( var_2 == 0 )
    {
        foreach ( var_5 in var_1 )
        {
            var_6 = weaponclass( var_5 );

            if ( ( var_6 == "rocketlauncher" || var_6 == "grenade" ) && var_0 getammocount( var_5 ) > 0 )
            {
                var_2 = 1;
                break;
            }
            else
            {
                var_7 = weaponaltweaponname( var_5 );

                if ( isdefined( var_7 ) && weaponclass( var_7 ) == "grenade" && var_0 getammocount( var_7 ) > 0 )
                {
                    var_2 = 1;
                    break;
                }
            }
        }
    }

    return var_2;
}

watchforjuggernaut()
{
    self endon( "death" );
    self endon( "intel_cleanup" );

    for (;;)
    {
        level waittill( "juggernaut_equipped" );
        waittillframeend;

        if ( maps\mp\_utility::isjuggernaut() )
            thread updatejuggintel();
    }
}

updatejuggintel()
{
    self notify( "intel_cleanup" );
    self setclientomnvar( "ui_intel_active_index", -1 );
    onpickupintel();
}

watchforplayerdisconnect()
{
    self endon( "death" );
    self endon( "intel_cleanup" );
    self waittill( "disconnect" );
    level thread randassignintel();
}

watchforremotekillstreak()
{
    self endon( "death" );
    self endon( "intel_cleanup" );
    self endon( "stopped_using_remote" );
    self waittill( "using_remote" );
    self.inteldeathposition = self.origin;
    thread updateinteldeathposition();
}

updateinteldeathposition()
{
    self endon( "death" );
    self endon( "intel_cleanup" );
    self waittill( "stopped_using_remote" );
    self.inteldeathposition = undefined;
    thread watchforremotekillstreak();
}

randassignintel()
{
    level notify( "randAssignIntel" );
    level endon( "randAssignIntel" );
    level endon( "game_ended" );

    for (;;)
    {
        wait 1;
        var_0 = maps\mp\_utility::getrandomplayingplayer();

        if ( !isdefined( var_0 ) )
            continue;

        var_0.hasintel = 1;
        level.intelent["owner"] = var_0;
        var_0 thread watchforplayerdisconnect();
        break;
    }
}

awardplayerchallengecomplete( var_0 )
{
    self endon( "disconnect" );
    replenishammo();
    self.hasintel = 0;
    var_1 = level.intelchallengearray[var_0];
    var_2 = var_1.challengename;
    var_3 = int( var_1.challengereward );
    self setclientomnvar( "ui_intel_active_index", -1 );
    thread maps\mp\killstreaks\_killstreaks::givekillstreak( "airdrop_assault", 0, 0, self );
    thread maps\mp\gametypes\_hud_message::splashnotifydelayed( var_2, var_3 );
    thread maps\mp\gametypes\_rank::giverankxp( "intel", var_3 );
    level thread randassignintel();
    self thread [[ level.leaderdialogonplayer_func ]]( "achieve_carepackage", undefined, undefined, self.origin );
    maps\mp\gametypes\_missions::processchallenge( "ch_intelligence" );
    self notify( "intel_cleanup" );
}

replenishammo()
{
    var_0 = self getweaponslistall();

    foreach ( var_2 in var_0 )
    {
        if ( maps\mp\_utility::iskillstreakweapon( var_2 ) )
            continue;
        else
        {
            if ( weaponinventorytype( var_2 ) == "offhand" )
            {
                if ( var_2 == self.loadoutperkequipment && maps\mp\_utility::_hasperk( "specialty_extra_deadly" ) || var_2 == self.loadoutperkoffhand && maps\mp\_utility::_hasperk( "specialty_extra_equipment" ) )
                    self setweaponammoclip( var_2, 2 );
                else
                    self setweaponammoclip( var_2, 1 );

                continue;
            }

            self givemaxammo( var_2 );
        }
    }
}

proximitythink( var_0 )
{
    if ( !isdefined( self ) )
        return 0;

    self.inuse = 1;
    var_1 = proximitythinkloop( var_0 );

    if ( !isdefined( self ) )
        return 0;

    level.intelent["useProgress"] = 0;
    return var_1;
}

proximitythinkloop( var_0 )
{
    self.userate = level.intelent["useRate"];
    self.usetime = level.intelent["useTime"];
    self.curprogress = 0;

    while ( !level.gameended && isdefined( self ) && maps\mp\_utility::isreallyalive( var_0 ) )
    {
        if ( distance2dsquared( self.origin, var_0.origin ) > 36864 )
            break;

        level.intelent["useProgress"] = level.intelent["useProgress"] + 0.05 * self.userate;
        self.curprogress = level.intelent["useProgress"];
        var_0 maps\mp\gametypes\_gameobjects::updateuiprogress( self, 1 );

        if ( level.intelent["useProgress"] >= self.usetime )
        {
            var_0 maps\mp\gametypes\_gameobjects::updateuiprogress( self, 0 );
            return maps\mp\_utility::isreallyalive( var_0 );
        }

        wait 0.05;
    }

    var_0 maps\mp\gametypes\_gameobjects::updateuiprogress( self, 0 );
    return 0;
}
