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

init()
{
    level.radarviewtime = 23;
    level.uavblocktime = 23;
    level.killstreakfuncs["uav_3dping"] = ::tryuse3dping;
    level.uavsettings = [];
    level.uavsettings["uav_3dping"] = spawnstruct();
    level.uavsettings["uav_3dping"].timeout = 63;
    level.uavsettings["uav_3dping"].streakname = "uav_3dping";
    level.uavsettings["uav_3dping"].highlightfadetime = 1.5;
    level.uavsettings["uav_3dping"].pingtime = 10.0;
    level.uavsettings["uav_3dping"].fxid_ping = loadfx( "vfx/gameplay/mp/killstreaks/vfx_3d_world_ping" );
    level.uavsettings["uav_3dping"].sound_ping_plr = "oracle_radar_pulse_plr";
    level.uavsettings["uav_3dping"].sound_ping_npc = "oracle_radar_pulse_npc";
    level.uavsettings["uav_3dping"].votimeout = "oracle_gone";
    level.uavsettings["uav_3dping"].teamsplash = "used_uav_3dping";
    var_0 = getentarray( "minimap_corner", "targetname" );

    if ( var_0.size )
        var_1 = maps\mp\gametypes\_spawnlogic::findboxcenter( var_0[0].origin, var_0[1].origin );
    else
        var_1 = ( 0, 0, 0 );

    level.uavrig = spawn( "script_model", var_1 );
    level.uavrig.angles = ( 0, 115, 0 );
    level.uavrig hide();
    level.uavrig.targetname = "uavrig_script_model";
    level.uavrig thread rotateuavrig();

    if ( level.multiteambased )
    {
        for ( var_2 = 0; var_2 < level.teamnamelist.size; var_2++ )
        {
            level.radarmode[level.teamnamelist[var_2]] = "normal_radar";
            level.activeuavs[level.teamnamelist[var_2]] = 0;
            level.activecounteruavs[level.teamnamelist[var_2]] = 0;
            level.uavmodels[level.teamnamelist[var_2]] = [];
        }
    }
    else if ( level.teambased )
    {
        level.radarmode["allies"] = "normal_radar";
        level.radarmode["axis"] = "normal_radar";
        level.activeuavs["allies"] = 0;
        level.activeuavs["axis"] = 0;
        level.activecounteruavs["allies"] = 0;
        level.activecounteruavs["axis"] = 0;
        level.uavmodels["allies"] = [];
        level.uavmodels["axis"] = [];
    }
    else
    {
        level.radarmode = [];
        level.activeuavs = [];
        level.activecounteruavs = [];
        level.uavmodels = [];
        level thread onplayerconnect();
    }

    level thread uavtracker();
}

onplayerconnect()
{
    for (;;)
    {
        level waittill( "connected", var_0 );
        level.activeuavs[var_0.guid] = 0;
        level.activeuavs[var_0.guid + "_radarStrength"] = 0;
        level.activecounteruavs[var_0.guid] = 0;
        level.radarmode[var_0.guid] = "normal_radar";
    }
}

rotateuavrig( var_0, var_1 )
{
    if ( isdefined( var_1 ) )
        self endon( var_1 );

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

    for (;;)
    {
        self rotateyaw( -360, var_0 );
        wait( var_0 );
    }
}

tryuseuav( var_0, var_1 )
{
    return useuav( var_1 );
}

tryuse3dping( var_0, var_1 )
{
    var_2 = "uav_3dping";
    thread watch3dping( var_2 );
    thread watch3dpingtimeout( var_2 );
    level thread maps\mp\_utility::teamplayercardsplash( level.uavsettings[var_2].teamsplash, self );
    return 1;
}

useuav( var_0 )
{
    maps\mp\_matchdata::logkillstreakevent( var_0, self.origin );
    var_1 = self.pers["team"];
    var_2 = level.uavsettings[var_0].timeout;
    level thread launchuav( self, var_2, var_0 );

    switch ( var_0 )
    {
        case "counter_uav":
            self notify( "used_counter_uav" );
            break;
        case "directional_uav":
            self.radarshowenemydirection = 1;

            if ( level.teambased )
            {
                foreach ( var_4 in level.players )
                {
                    if ( var_4.pers["team"] == var_1 )
                        var_4.radarshowenemydirection = 1;
                }
            }

            level thread maps\mp\_utility::teamplayercardsplash( level.uavsettings[var_0].teamsplash, self, var_1 );
            self notify( "used_directional_uav" );
            break;
        default:
            self notify( "used_uav" );
            break;
    }

    return 1;
}

launchuav( var_0, var_1, var_2 )
{
    var_3 = var_0.team;
    var_4 = spawn( "script_model", level.uavrig gettagorigin( "tag_origin" ) );
    var_4 setmodel( level.uavsettings[var_2].modelbase );
    var_4.team = var_3;
    var_4.owner = var_0;
    var_4.timetoadd = 0;
    var_4.uavtype = var_2;
    var_4.health = level.uavsettings[var_2].health;
    var_4.maxhealth = level.uavsettings[var_2].maxhealth;

    if ( var_2 == "directional_uav" )
        var_4 thread spawnfxdelay( level.uavsettings[var_2].fxid_contrail, level.uavsettings[var_2].fx_contrail_tag );

    var_4 adduavmodel();
    var_4 thread damagetracker();
    var_4 thread handleincomingstinger();
    var_4 thread removeuavmodelondeath();
    var_5 = randomintrange( 3000, 5000 );

    if ( isdefined( level.spawnpoints ) )
        var_6 = level.spawnpoints;
    else
        var_6 = level.startspawnpoints;

    var_7 = var_6[0];

    foreach ( var_9 in var_6 )
    {
        if ( var_9.origin[2] < var_7.origin[2] )
            var_7 = var_9;
    }

    var_11 = var_7.origin[2];
    var_12 = level.uavrig.origin[2];

    if ( var_11 < 0 )
    {
        var_12 = var_12 + var_11 * -1;
        var_11 = 0;
    }

    var_13 = var_12 - var_11;

    if ( var_13 + var_5 > 8100.0 )
        var_5 = var_5 - ( var_13 + var_5 - 8100.0 );

    var_14 = randomint( 360 );
    var_15 = randomint( 2000 ) + 5000;
    var_16 = cos( var_14 ) * var_15;
    var_17 = sin( var_14 ) * var_15;
    var_18 = vectornormalize( ( var_16, var_17, var_5 ) );
    var_18 = var_18 * randomintrange( 6000, 7000 );
    var_4 linkto( level.uavrig, "tag_origin", var_18, ( 0, var_14 - 90, 0 ) );
    var_4 thread updateuavmodelvisibility();
    var_4 [[ level.uavsettings[var_2].addfunc ]]();

    if ( isdefined( level.activeuavs[var_3] ) )
    {
        foreach ( var_20 in level.uavmodels[var_3] )
        {
            if ( var_20 == var_4 )
                continue;

            var_20.timetoadd = var_20.timetoadd + 5;
        }
    }

    level notify( "uav_update" );
    var_4 maps\mp\gametypes\_hostmigration::waittill_notify_or_timeout_hostmigration_pause( "death", var_1 );

    if ( var_4.damagetaken < var_4.maxhealth )
    {
        var_4 unlink();
        var_22 = var_4.origin + anglestoforward( var_4.angles ) * 20000;
        var_4 moveto( var_22, 60 );

        if ( isdefined( level.uavsettings[var_2].fxid_leave ) && isdefined( level.uavsettings[var_2].fx_leave_tag ) )
            playfxontag( level.uavsettings[var_2].fxid_leave, var_4, level.uavsettings[var_2].fx_leave_tag );

        var_4 maps\mp\gametypes\_hostmigration::waittill_notify_or_timeout_hostmigration_pause( "death", 3 );

        if ( var_4.damagetaken < var_4.maxhealth )
        {
            var_4 notify( "leaving" );
            var_4.isleaving = 1;
            var_4 moveto( var_22, 4, 4, 0.0 );
        }

        var_4 maps\mp\gametypes\_hostmigration::waittill_notify_or_timeout_hostmigration_pause( "death", 4 + var_4.timetoadd );
    }

    var_4 [[ level.uavsettings[var_2].removefunc ]]();
    var_4 delete();
    var_4 removeuavmodel();

    if ( var_2 == "directional_uav" )
    {
        var_0.radarshowenemydirection = 0;

        if ( level.teambased )
        {
            foreach ( var_24 in level.players )
            {
                if ( var_24.pers["team"] == var_3 )
                    var_24.radarshowenemydirection = 0;
            }
        }
    }

    level notify( "uav_update" );
}

updateuavmodelvisibility()
{
    self endon( "death" );

    for (;;)
    {
        level common_scripts\utility::waittill_either( "joined_team", "uav_update" );
        self hide();

        foreach ( var_1 in level.players )
        {
            if ( level.teambased )
            {
                if ( var_1.team != self.team )
                    self showtoplayer( var_1 );

                continue;
            }

            if ( isdefined( self.owner ) && var_1 == self.owner )
                continue;

            self showtoplayer( var_1 );
        }
    }
}

damagetracker()
{
    level endon( "game_ended" );
    self setcandamage( 1 );
    self.damagetaken = 0;

    for (;;)
    {
        self waittill( "damage", var_0, var_1, var_2, var_3, var_4, var_5, var_6, var_7, var_8, var_9 );

        if ( !isplayer( var_1 ) )
        {
            if ( !isdefined( self ) )
                return;
        }
        else
        {
            if ( isdefined( var_8 ) && var_8 & level.idflags_penetration )
                self.wasdamagedfrombulletpenetration = 1;

            self.wasdamaged = 1;
            var_10 = var_0;

            if ( isplayer( var_1 ) )
            {
                var_1 maps\mp\gametypes\_damagefeedback::updatedamagefeedback( "" );

                if ( var_4 == "MOD_RIFLE_BULLET" || var_4 == "MOD_PISTOL_BULLET" )
                {
                    if ( var_1 maps\mp\_utility::_hasperk( "specialty_armorpiercing" ) )
                        var_10 = var_10 + var_0 * level.armorpiercingmod;
                }
            }

            if ( isdefined( var_9 ) )
            {
                switch ( var_9 )
                {
                    case "javelin_mp":
                    case "stinger_mp":
                        self.largeprojectiledamage = 1;
                        var_10 = self.maxhealth + 1;
                        break;
                    case "sam_projectile_mp":
                        self.largeprojectiledamage = 1;
                        var_11 = 0.25;

                        if ( self.uavtype == "directional_uav" )
                            var_11 = 0.15;

                        var_10 = self.maxhealth * var_11;
                        break;
                }

                maps\mp\killstreaks\_killstreaks::killstreakhit( var_1, var_9, self );
            }

            self.damagetaken = self.damagetaken + var_10;

            if ( self.damagetaken >= self.maxhealth )
            {
                if ( isplayer( var_1 ) && ( !isdefined( self.owner ) || var_1 != self.owner ) )
                {
                    thread maps\mp\_utility::teamplayercardsplash( level.uavsettings[self.uavtype].calloutdestroyed, var_1 );
                    thread maps\mp\gametypes\_missions::vehiclekilled( self.owner, self, undefined, var_1, var_0, var_4, var_9 );
                    var_1 thread maps\mp\gametypes\_rank::giverankxp( "kill", 50, var_9, var_4 );
                    var_1 notify( "destroyed_killstreak" );

                    if ( isdefined( self.uavremotemarkedby ) && self.uavremotemarkedby != var_1 )
                        self.uavremotemarkedby thread maps\mp\killstreaks\_remoteuav::remoteuav_processtaggedassist();
                }

                self hide();
                var_12 = anglestoright( self.angles ) * 200;
                playfx( level.uavsettings[self.uavtype].fxid_explode, self.origin, var_12 );
                self notify( "death" );
                return;
            }
        }
    }
}

uavtracker()
{
    level endon( "game_ended" );

    for (;;)
    {
        level waittill( "uav_update" );

        if ( level.multiteambased )
        {
            for ( var_0 = 0; var_0 < level.teamnamelist.size; var_0++ )
                updateteamuavstatus( level.teamnamelist[var_0] );

            continue;
        }

        if ( level.teambased )
        {
            updateteamuavstatus( "allies" );
            updateteamuavstatus( "axis" );
            continue;
        }

        updateplayersuavstatus();
    }
}

_getradarstrength( var_0 )
{
    var_1 = 0;
    var_2 = 0;

    foreach ( var_4 in level.uavmodels[var_0] )
    {
        if ( var_4.uavtype == "counter_uav" )
            continue;

        if ( var_4.uavtype == "remote_mortar" )
            continue;

        var_1++;
    }

    if ( level.multiteambased )
    {
        foreach ( var_7 in level.teamnamelist )
        {
            foreach ( var_4 in level.uavmodels[var_7] )
            {
                if ( var_7 == var_0 )
                    continue;

                if ( var_4.uavtype != "counter_uav" )
                    continue;

                var_2++;
            }
        }
    }
    else
    {
        foreach ( var_4 in level.uavmodels[level.otherteam[var_0]] )
        {
            if ( var_4.uavtype != "counter_uav" )
                continue;

            var_2++;
        }
    }

    if ( var_2 > 0 )
        var_13 = -3;
    else
        var_13 = var_1;

    var_14 = getuavstrengthmin();
    var_15 = getuavstrengthmax();

    if ( var_13 <= var_14 )
        var_13 = var_14;
    else if ( var_13 >= var_15 )
        var_13 = var_15;

    return var_13;
}

updateteamuavstatus( var_0 )
{
    var_1 = _getradarstrength( var_0 );
    setteamradarstrength( var_0, var_1 );

    if ( var_1 >= getuavstrengthlevelneutral() )
        unblockteamradar( var_0 );
    else
        blockteamradar( var_0 );

    if ( var_1 <= getuavstrengthlevelneutral() )
    {
        setteamradarwrapper( var_0, 0 );
        updateteamuavtype( var_0 );
        return;
    }

    if ( var_1 >= getuavstrengthlevelshowenemyfastsweep() )
        level.radarmode[var_0] = "fast_radar";
    else
        level.radarmode[var_0] = "normal_radar";

    updateteamuavtype( var_0 );
    setteamradarwrapper( var_0, 1 );
}

updateplayersuavstatus()
{
    var_0 = getuavstrengthmin();
    var_1 = getuavstrengthmax();
    var_2 = getuavstrengthlevelshowenemydirectional();

    foreach ( var_4 in level.players )
    {
        var_5 = level.activeuavs[var_4.guid + "_radarStrength"];

        foreach ( var_7 in level.players )
        {
            if ( var_7 == var_4 )
                continue;

            var_8 = level.activecounteruavs[var_7.guid];

            if ( var_8 > 0 )
            {
                var_5 = -3;
                break;
            }
        }

        if ( var_5 <= var_0 )
            var_5 = var_0;
        else if ( var_5 >= var_1 )
            var_5 = var_1;

        var_4.radarstrength = var_5;

        if ( var_5 >= getuavstrengthlevelneutral() )
            var_4.isradarblocked = 0;
        else
            var_4.isradarblocked = 1;

        if ( var_5 <= getuavstrengthlevelneutral() )
        {
            var_4.hasradar = 0;
            var_4.radarshowenemydirection = 0;
            continue;
        }

        if ( var_5 >= getuavstrengthlevelshowenemyfastsweep() )
            var_4.radarmode = "fast_radar";
        else
            var_4.radarmode = "normal_radar";

        var_4.radarshowenemydirection = var_5 >= var_2;
        var_4.hasradar = 1;
    }
}

blockplayeruav()
{
    self endon( "disconnect" );
    self notify( "blockPlayerUAV" );
    self endon( "blockPlayerUAV" );
    self.isradarblocked = 1;
    wait( level.uavblocktime );
    self.isradarblocked = 0;
}

updateteamuavtype( var_0 )
{
    var_1 = _getradarstrength( var_0 ) >= getuavstrengthlevelshowenemydirectional();

    foreach ( var_3 in level.players )
    {
        if ( var_3.team == "spectator" )
            continue;

        var_3.radarmode = level.radarmode[var_3.team];

        if ( var_3.team == var_0 )
            var_3.radarshowenemydirection = var_1;
    }
}

useplayeruav( var_0, var_1 )
{
    level endon( "game_ended" );
    self endon( "disconnect" );
    self notify( "usePlayerUAV" );
    self endon( "usePlayerUAV" );

    if ( var_0 )
        self.radarmode = "fast_radar";
    else
        self.radarmode = "normal_radar";

    self.hasradar = 1;
    wait( var_1 );
    self.hasradar = 0;
}

setteamradarwrapper( var_0, var_1 )
{
    setteamradar( var_0, var_1 );
    level notify( "radar_status_change", var_0 );
}

handleincomingstinger()
{
    level endon( "game_ended" );
    self endon( "death" );

    for (;;)
    {
        level waittill( "stinger_fired", var_0, var_1, var_2 );

        if ( !isdefined( var_2 ) || var_2 != self )
            continue;

        var_1 thread stingerproximitydetonate( var_2, var_0 );
    }
}

stingerproximitydetonate( var_0, var_1 )
{
    self endon( "death" );
    var_2 = distance( self.origin, var_0 getpointinbounds( 0, 0, 0 ) );
    var_3 = var_0 getpointinbounds( 0, 0, 0 );

    for (;;)
    {
        if ( !isdefined( var_0 ) )
            var_4 = var_3;
        else
            var_4 = var_0 getpointinbounds( 0, 0, 0 );

        var_3 = var_4;
        var_5 = distance( self.origin, var_4 );

        if ( var_5 < var_2 )
            var_2 = var_5;

        if ( var_5 > var_2 )
        {
            if ( var_5 > 1536 )
                return;

            radiusdamage( self.origin, 1536, 600, 600, var_1, "MOD_EXPLOSIVE", "stinger_mp" );
            playfx( level.stingerfxid, self.origin );
            self hide();
            self notify( "deleted" );
            wait 0.05;
            self delete();
            var_1 notify( "killstreak_destroyed" );
        }

        wait 0.05;
    }
}

adduavmodel()
{
    if ( level.teambased )
        level.uavmodels[self.team][level.uavmodels[self.team].size] = self;
    else
        level.uavmodels[self.owner.guid + "_" + gettime()] = self;
}

removeuavmodelondeath()
{
    self waittill( "death" );

    if ( isdefined( self.uavrig ) )
        self.uavrig delete();

    if ( isdefined( self ) )
        self delete();

    removeuavmodel();
}

removeuavmodel()
{
    var_0 = [];

    if ( level.teambased )
    {
        var_1 = self.team;

        foreach ( var_3 in level.uavmodels[var_1] )
        {
            if ( !isdefined( var_3 ) )
                continue;

            var_0[var_0.size] = var_3;
        }

        level.uavmodels[var_1] = var_0;
    }
    else
    {
        foreach ( var_3 in level.uavmodels )
        {
            if ( !isdefined( var_3 ) )
                continue;

            var_0[var_0.size] = var_3;
        }

        level.uavmodels = var_0;
    }
}

addactiveuav()
{
    if ( level.teambased )
        level.activeuavs[self.team]++;
    else
    {
        level.activeuavs[self.owner.guid]++;
        level.activeuavs[self.owner.guid + "_radarStrength"]++;
    }
}

addactivecounteruav()
{
    if ( level.teambased )
        level.activecounteruavs[self.team]++;
    else
        level.activecounteruavs[self.owner.guid]++;
}

removeactiveuav()
{
    if ( level.teambased )
        level.activeuavs[self.team]--;
    else if ( isdefined( self.owner ) )
    {
        level.activeuavs[self.owner.guid]--;
        level.activeuavs[self.owner.guid + "_radarStrength"]--;
    }
}

removeactivecounteruav()
{
    if ( level.teambased )
        level.activecounteruavs[self.team]--;
    else if ( isdefined( self.owner ) )
        level.activecounteruavs[self.owner.guid]--;
}

spawnfxdelay( var_0, var_1 )
{
    self endon( "death" );
    level endon( "game_ended" );
    wait 0.5;
    playfxontag( var_0, self, var_1 );
}

watch3dping( var_0, var_1 )
{
    if ( isdefined( var_1 ) )
        var_1 endon( "death" );

    self endon( "leave" );
    self endon( "killstreak_disowned" );
    level endon( "game_ended" );
    var_2 = level.uavsettings[var_0];
    var_3 = var_2.pingtime;

    if ( level.teambased )
        level.activeuavs[self.team]++;
    else
        level.activeuavs[self.guid]++;

    for (;;)
    {
        playfx( var_2.fxid_ping, self.origin );
        self playlocalsound( var_2.sound_ping_plr );
        playsoundatpos( self.origin + ( 0, 0, 5 ), var_2.sound_ping_npc );

        foreach ( var_5 in level.participants )
        {
            if ( !maps\mp\_utility::isreallyalive( var_5 ) )
                continue;

            if ( !maps\mp\_utility::isenemy( var_5 ) )
                continue;

            if ( var_5 maps\mp\_utility::_hasperk( "specialty_noplayertarget" ) )
                continue;

            var_5 maps\mp\gametypes\_damagefeedback::hudicontype( "oracle" );

            foreach ( var_7 in level.participants )
            {
                if ( !maps\mp\_utility::isreallyalive( var_7 ) )
                    continue;

                if ( maps\mp\_utility::isenemy( var_7 ) )
                    continue;

                if ( isai( var_7 ) )
                {
                    var_7 common_scripts\utility::ai_3d_sighting_model( var_5 );
                    continue;
                }

                var_8 = maps\mp\_utility::outlineenableforplayer( var_5, "orange", var_7, 0, "killstreak" );
                var_9 = var_2.highlightfadetime;
                var_7 thread watchhighlightfadetime( var_8, var_5, var_9, var_1 );
            }
        }

        maps\mp\gametypes\_hostmigration::waitlongdurationwithhostmigrationpause( var_3 );
    }
}

watch3dpingtimeout( var_0 )
{
    self endon( "killstreak_disowned" );
    level endon( "game_ended" );
    var_1 = level.uavsettings[var_0];
    var_2 = var_1.timeout;
    var_3 = self.guid;

    if ( level.teambased )
        var_3 = self.team;

    thread watch_3dping_killstreakdisowned( var_3 );
    maps\mp\gametypes\_hostmigration::waitlongdurationwithhostmigrationpause( var_2 );
    maps\mp\_utility::leaderdialogonplayer( var_1.votimeout );
    self notify( "leave" );
    cleanup3dping( var_3 );
}

watch_3dping_killstreakdisowned( var_0 )
{
    self endon( "leave" );
    self waittill( "killstreak_disowned" );
    cleanup3dping( var_0 );
}

cleanup3dping( var_0 )
{
    level.activeuavs[var_0]--;

    if ( level.activeuavs[var_0] < 0 )
        level.activeuavs[var_0] = 0;
}

watchhighlightfadetime( var_0, var_1, var_2, var_3 )
{
    if ( isdefined( var_3 ) )
        var_3 endon( "death" );

    self endon( "disconnect" );
    level endon( "game_ended" );
    common_scripts\utility::waittill_any_timeout_no_endon_death( var_2, "leave" );

    if ( isdefined( var_1 ) )
        maps\mp\_utility::outlinedisable( var_0, var_1 );
}
