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

init()
{
    setupfx();
    setuphelirange();
    level.remote_uav = [];
    level.killstreakfuncs["vanguard"] = ::tryusevanguard;
    level.vanguard_lastdialogtime = 0;
    level.vanguardfiremisslefunc = ::vanguard_firemissile;
    level.lasedstrikeglow = loadfx( "fx/misc/laser_glow" );
}

setupfx()
{
    level.vanguard_fx["hit"] = loadfx( "fx/impacts/large_metal_painted_hit" );
    level.vanguard_fx["smoke"] = loadfx( "fx/smoke/remote_heli_damage_smoke_runner" );
    level.vanguard_fx["explode"] = loadfx( "vfx/gameplay/explosions/vehicle/vang/vfx_exp_vanguard" );
    level.vanguard_fx["target_marker_circle"] = loadfx( "vfx/gameplay/mp/core/vfx_marker_gryphon_orange" );
}

setuphelirange()
{
    level.vanguardrangetriggers = getentarray( "remote_heli_range", "targetname" );
    level.vanguardmaxheightent = getent( "airstrikeheight", "targetname" );

    if ( isdefined( level.vanguardmaxheightent ) )
    {
        level.vanguardmaxheight = level.vanguardmaxheightent.origin[2];
        level.vanguradmaxdistancesq = 163840000;
    }

    level.is_mp_descent = 0;

    if ( maps\mp\_utility::getmapname() == "mp_descent" || maps\mp\_utility::getmapname() == "mp_descent_new" )
    {
        level.vanguardmaxheight = level.vanguardrangetriggers[0].origin[2] + 360;
        level.is_mp_descent = 1;
    }
}

tryusevanguard( var_0, var_1 )
{
    return usevanguard( var_0, var_1 );
}

usevanguard( var_0, var_1 )
{
    if ( maps\mp\_utility::isusingremote() || self isusingturret() )
        return 0;

    if ( isdefined( self.underwater ) && self.underwater )
        return 0;

    if ( exceededmaxvanguards( self.team ) || level.littlebirds.size >= 4 )
    {
        self iprintlnbold( &"KILLSTREAKS_AIR_SPACE_TOO_CROWDED" );
        return 0;
    }
    else if ( maps\mp\_utility::currentactivevehiclecount() >= maps\mp\_utility::maxvehiclesallowed() || level.fauxvehiclecount + 1 >= maps\mp\_utility::maxvehiclesallowed() )
    {
        self iprintlnbold( &"KILLSTREAKS_TOO_MANY_VEHICLES" );
        return 0;
    }
    else if ( isdefined( self.drones_disabled ) )
    {
        self iprintlnbold( &"KILLSTREAKS_UNAVAILABLE" );
        return 0;
    }

    maps\mp\_utility::incrementfauxvehiclecount();
    var_2 = givecarryvanguard( var_0, var_1 );

    if ( !isdefined( var_2 ) )
    {
        maps\mp\_utility::decrementfauxvehiclecount();
        return 0;
    }

    if ( !maps\mp\_utility::is_aliens() )
        maps\mp\_matchdata::logkillstreakevent( var_1, self.origin );

    return startvanguard( var_2, var_1, var_0 );
}

exceededmaxvanguards( var_0 )
{
    if ( level.teambased )
        return isdefined( level.remote_uav[var_0] );
    else
        return isdefined( level.remote_uav[var_0] ) || isdefined( level.remote_uav[level.otherteam[var_0]] );
}

findvalidvanguardspawnpoint( var_0, var_1 )
{
    var_2 = anglestoforward( self.angles );
    var_3 = anglestoright( self.angles );
    var_4 = self geteye();
    var_5 = var_4 + ( 0, 0, var_1 );
    var_6 = var_5 + var_0 * var_2;

    if ( checkvanguardspawnpoint( var_4, var_6 ) )
        return var_6;

    var_6 = var_5 - var_0 * var_2;

    if ( checkvanguardspawnpoint( var_4, var_6 ) )
        return var_6;

    var_6 = var_6 + var_0 * var_3;

    if ( checkvanguardspawnpoint( var_4, var_6 ) )
        return var_6;

    var_6 = var_5 - var_0 * var_3;

    if ( checkvanguardspawnpoint( var_4, var_6 ) )
        return var_6;

    var_6 = var_5;

    if ( checkvanguardspawnpoint( var_4, var_6 ) )
        return var_6;

    common_scripts\utility::waitframe();
    var_6 = var_5 + 0.707 * var_0 * ( var_2 + var_3 );

    if ( checkvanguardspawnpoint( var_4, var_6 ) )
        return var_6;

    var_6 = var_5 + 0.707 * var_0 * ( var_2 - var_3 );

    if ( checkvanguardspawnpoint( var_4, var_6 ) )
        return var_6;

    var_6 = var_5 + 0.707 * var_0 * ( var_3 - var_2 );

    if ( checkvanguardspawnpoint( var_4, var_6 ) )
        return var_6;

    var_6 = var_5 + 0.707 * var_0 * ( -1 * var_2 - var_3 );

    if ( checkvanguardspawnpoint( var_4, var_6 ) )
        return var_6;

    return undefined;
}

checkvanguardspawnpoint( var_0, var_1 )
{
    var_2 = 0;

    if ( capsuletracepassed( var_1, 20, 40.01, undefined, 1, 1 ) )
        var_2 = bullettracepassed( var_0, var_1, 0, undefined );

    return var_2;
}

givecarryvanguard( var_0, var_1, var_2 )
{
    var_3 = maps\mp\gametypes\_spawnscoring::finddronepathnode( self, 90, 20, 192 );

    if ( !isdefined( var_3 ) )
    {
        var_3 = maps\mp\gametypes\_spawnscoring::finddronepathnode( self, 0, 20, 192 );

        if ( !isdefined( var_3 ) )
        {
            var_3 = findvalidvanguardspawnpoint( 80, 35 );

            if ( !isdefined( var_3 ) )
                var_3 = findvalidvanguardspawnpoint( 80, 0 );
        }
    }

    if ( isdefined( var_3 ) )
    {
        var_4 = self.angles;
        var_5 = createvanguard( var_0, self, var_1, var_3, var_4, var_2 );

        if ( !isdefined( var_5 ) )
            self iprintlnbold( &"KILLSTREAKS_AIR_SPACE_TOO_CROWDED" );

        return var_5;
    }
    else
    {
        self iprintlnbold( &"KILLSTREAKS_VANGUARD_NO_SPAWN_POINT" );
        return undefined;
    }
}

startvanguard( var_0, var_1, var_2 )
{
    maps\mp\_utility::setusingremote( var_1 );
    maps\mp\_utility::freezecontrolswrapper( 1 );
    self.restoreangles = self.angles;

    if ( getdvarint( "camera_thirdPerson" ) )
        maps\mp\_utility::setthirdpersondof( 0 );

    thread watchintrocleared( var_0 );
    var_3 = maps\mp\killstreaks\_killstreaks::initridekillstreak( "vanguard" );

    if ( var_3 != "success" )
    {
        var_0 notify( "death" );
        return 0;
    }
    else if ( !isdefined( var_0 ) )
        return 0;

    maps\mp\_utility::freezecontrolswrapper( 0 );
    var_0.playerlinked = 1;
    self cameralinkto( var_0, "tag_origin" );
    self remotecontrolvehicle( var_0 );
    var_0.ammocount = 100;
    self.remote_uav_ridelifeid = var_2;
    self.remoteuav = var_0;
    thread maps\mp\_utility::teamplayercardsplash( "used_vanguard", self );
    return 1;
}

vanguard_moving_platform_death( var_0 )
{
    if ( !isdefined( var_0.lasttouchedplatform.destroydroneoncollision ) || var_0.lasttouchedplatform.destroydroneoncollision || !isdefined( self.spawngraceperiod ) || gettime() > self.spawngraceperiod )
        thread handledeathdamage( undefined, undefined, undefined, undefined );
    else
    {
        wait 1.0;
        thread maps\mp\_movers::handle_moving_platform_touch( var_0 );
    }
}

createvanguard( var_0, var_1, var_2, var_3, var_4, var_5 )
{
    var_6 = spawnhelicopter( var_1, var_3, var_4, "remote_uav_mp", "vehicle_drone_vanguard" );

    if ( !isdefined( var_6 ) )
        return undefined;

    var_6 maps\mp\killstreaks\_helicopter::addtolittlebirdlist();
    var_6 thread maps\mp\killstreaks\_helicopter::removefromlittlebirdlistondeath();
    var_6 makevehiclesolidcapsule( 20, -5, 10 );
    var_6.attackarrow = spawn( "script_model", ( 0, 0, 0 ) );
    var_6.attackarrow setmodel( "tag_origin" );
    var_6.attackarrow.angles = ( -90, 0, 0 );
    var_6.attackarrow.offset = 4;
    var_7 = spawnturret( "misc_turret", var_6.origin, "ball_drone_gun_mp", 0 );
    var_7 linkto( var_6, "tag_turret_attach", ( 0, 0, 0 ), ( 0, 0, 0 ) );
    var_7 setmodel( "vehicle_drone_vanguard_gun" );
    var_7 maketurretinoperable();
    var_6.turret = var_7;
    var_7 makeunusable();
    var_6.lifeid = var_0;
    var_6.team = var_1.team;
    var_6.pers["team"] = var_1.team;
    var_6.owner = var_1;
    var_6 common_scripts\utility::make_entity_sentient_mp( var_1.team );

    if ( issentient( var_6 ) )
        var_6 setthreatbiasgroup( "DogsDontAttack" );

    var_6.health = 999999;
    var_6.maxhealth = 750;
    var_6.damagetaken = 0;
    var_6.smoking = 0;
    var_6.inheliproximity = 0;
    var_6.helitype = "remote_uav";
    var_7.owner = var_1;
    var_7 setentityowner( var_6 );
    var_7 thread maps\mp\gametypes\_weapons::doblinkinglight( "tag_fx1" );
    var_7.parent = var_6;
    var_7.health = 999999;
    var_7.maxhealth = 250;
    var_7.damagetaken = 0;
    level thread vanguard_monitorkillstreakdisowned( var_6 );
    level thread vanguard_monitortimeout( var_6, var_5 );
    level thread vanguard_monitordeath( var_6 );
    level thread vanguard_monitorobjectivecam( var_6 );
    var_6 thread vanguard_watch_distance();
    var_6 thread vanguard_watchheliproximity();
    var_6 thread vanguard_handledamage();
    var_6.turret thread vanguard_turret_handledamage();
    var_6 thread watchempdamage();
    var_8 = spawn( "script_model", var_6.origin );
    var_8 setscriptmoverkillcam( "explosive" );
    var_8 linkto( var_6, "tag_player", ( -10, 0, 20 ), ( 0, 0, 0 ) );
    var_6.killcament = var_8;
    var_6.spawngraceperiod = gettime() + 2000;
    var_9 = spawnstruct();
    var_9.validateaccuratetouching = 1;
    var_9.deathoverridecallback = ::vanguard_moving_platform_death;
    var_6 thread maps\mp\_movers::handle_moving_platforms( var_9 );
    level.remote_uav[var_6.team] = var_6;
    return var_6;
}

watchhostmigrationfinishedinit( var_0 )
{
    self endon( "disconnect" );
    self endon( "joined_team" );
    self endon( "joined_spectators" );
    level endon( "game_ended" );
    var_0 endon( "death" );

    for (;;)
    {
        level waittill( "host_migration_end" );
        initvanguardhud();
        var_0 thread vanguard_reticlestart();
    }
}

watchintrocleared( var_0 )
{
    self endon( "disconnect" );
    self endon( "joined_team" );
    self endon( "joined_spectators" );
    level endon( "game_ended" );
    var_0 endon( "death" );
    self waittill( "intro_cleared" );
    initvanguardhud();
    var_0 enableaimassist();
    thread vanguard_think( var_0 );
    thread vanguard_monitorfire( var_0 );
    thread vanguard_monitormanualplayerexit( var_0 );
    thread vanguard_turrettarget( var_0 );
    var_0 thread vanguard_reticlestart();

    if ( !level.hardcoremode )
        var_0 thread vanguard_reticlewaitforjoinedteam();

    thread watchhostmigrationfinishedinit( var_0 );
    maps\mp\_utility::freezecontrolswrapper( 0 );
}

initvanguardhud()
{
    self thermalvisionfofoverlayon();
    self thermalvisionon();
    self setclientomnvar( "ui_vanguard", 1 );
}

vanguard_monitormanualplayerexit( var_0 )
{
    level endon( "game_ended" );
    self endon( "disconnect" );
    var_0 endon( "death" );
    var_0 endon( "end_remote" );
    var_0 thread maps\mp\killstreaks\_killstreaks::allowridekillstreakplayerexit();
    var_0 waittill( "killstreakExit" );

    if ( isdefined( var_0.owner ) )
        var_0.owner maps\mp\_utility::leaderdialogonplayer( "gryphon_gone" );

    var_0 notify( "death" );
}

vanguard_turrettarget( var_0 )
{
    level endon( "game_ended" );
    self endon( "disconnect" );
    var_0 endon( "death" );
    var_0 endon( "end_remote" );

    while ( !isdefined( var_0.attackarrow ) )
        wait 0.05;

    var_0 setotherent( var_0.attackarrow );
    var_0 setturrettargetent( var_0.attackarrow );
}

vanguard_think( var_0 )
{
    level endon( "game_ended" );
    self endon( "disconnect" );
    var_0 endon( "death" );
    var_0 endon( "end_remote" );

    for (;;)
    {
        if ( var_0 maps\mp\_utility::touchingbadtrigger( "gryphon" ) )
            var_0 notify( "damage", 1019, self, self.angles, self.origin, "MOD_EXPLOSIVE", undefined, undefined, undefined, undefined, "c4_mp" );

        self.lockedlocation = var_0.attackarrow.origin;
        common_scripts\utility::waitframe();
    }
}

vanguard_reticlestart()
{
    playfxontagforclients( level.vanguard_fx["target_marker_circle"], self.attackarrow, "tag_origin", self.owner );
    thread vanguard_showreticletoenemies();
}

vanguard_reticlewaitforjoinedteam()
{
    self endon( "death" );
    self endon( "end_remote" );

    for (;;)
    {
        level waittill( "joined_team", var_0 );
        stopfxontag( level.vanguard_fx["target_marker_circle"], self.attackarrow, "tag_origin" );
        common_scripts\utility::waitframe();
        vanguard_reticlestart();
    }
}

vanguard_showreticletoenemies()
{
    self endon( "death" );
    self endon( "end_remote" );

    if ( !level.hardcoremode )
    {
        foreach ( var_1 in level.players )
        {
            if ( self.owner maps\mp\_utility::isenemy( var_1 ) )
            {
                common_scripts\utility::waitframe();
                playfxontagforclients( level.vanguard_fx["target_marker_circle"], self.attackarrow, "tag_origin", var_1 );
            }
        }
    }
}

vanguard_selecttarget( var_0 )
{
    var_1 = gettargetpoint( var_0.owner, var_0 );

    if ( isdefined( var_1 ) )
    {
        var_0.attackarrow.origin = var_1[0] + ( 0, 0, 4 );
        return var_1[0];
    }

    return undefined;
}

gettargetpoint( var_0, var_1 )
{
    var_2 = var_1.turret gettagorigin( "tag_flash" );
    var_3 = var_0 getplayerangles();
    var_4 = anglestoforward( var_3 );
    var_5 = var_2 + var_4 * 15000;
    var_6 = bullettrace( var_2, var_5, 0, var_1 );

    if ( var_6["surfacetype"] == "none" )
        return undefined;

    if ( var_6["surfacetype"] == "default" )
        return undefined;

    var_7 = var_6["entity"];
    var_8 = [];
    var_8[0] = var_6["position"];
    var_8[1] = var_6["normal"];
    return var_8;
}

vanguard_monitorfire( var_0 )
{
    self endon( "disconnect" );
    level endon( "game_ended" );
    var_0 endon( "death" );
    var_0 endon( "end_remote" );
    self notifyonplayercommand( "vanguard_fire", "+attack" );
    self notifyonplayercommand( "vanguard_fire", "+attack_akimbo_accessible" );
    var_0.firereadytime = gettime();

    for (;;)
    {
        self waittill( "vanguard_fire" );
        maps\mp\gametypes\_hostmigration::waittillhostmigrationdone();

        if ( isdefined( level.hostmigrationtimer ) )
            continue;

        if ( isdefined( self.lockedlocation ) && gettime() >= var_0.firereadytime )
            self thread [[ level.vanguardfiremisslefunc ]]( var_0, self.lockedlocation );
    }
}

vanguard_rumble( var_0, var_1, var_2 )
{
    self endon( "disconnect" );
    level endon( "game_ended" );
    var_0 endon( "death" );
    var_0 endon( "end_remote" );
    var_0 notify( "end_rumble" );
    var_0 endon( "end_rumble" );

    for ( var_3 = 0; var_3 < var_2; var_3++ )
    {
        self playrumbleonentity( var_1 );
        common_scripts\utility::waitframe();
    }
}

looptriggeredeffect( var_0, var_1 )
{
    var_1 endon( "death" );
    level endon( "game_ended" );
    self endon( "death" );

    for (;;)
    {
        triggerfx( var_0 );
        wait 0.25;
    }
}

vanguard_firemissile( var_0, var_1 )
{
    level endon( "game_ended" );

    if ( var_0.ammocount <= 0 )
        return;

    var_2 = var_0.turret gettagorigin( "tag_fire" );
    var_2 = var_2 + ( 0, 0, -25 );

    if ( distancesquared( var_2, var_1 ) < 10000 )
    {
        var_0 playsoundtoplayer( "weap_vanguard_fire_deny", self );
        return;
    }

    var_0.ammocount--;
    self playlocalsound( "weap_gryphon_fire_plr" );
    maps\mp\_utility::playsoundinspace( "weap_gryphon_fire_npc", var_0.origin );
    thread vanguard_rumble( var_0, "shotgun_fire", 1 );
    earthquake( 0.3, 0.25, var_0.origin, 60 );
    var_3 = magicbullet( "remote_tank_projectile_mp", var_2, var_1, self );
    var_3.vehicle_fired_from = var_0;
    var_4 = 1500;
    var_0.firereadytime = gettime() + var_4;
    thread updateweaponui( var_0, var_4 * 0.001 );
    var_3 maps\mp\gametypes\_hostmigration::waittill_notify_or_timeout_hostmigration_pause( "death", 4 );
    earthquake( 0.3, 0.75, var_1, 128 );

    if ( isdefined( var_0 ) )
    {
        earthquake( 0.25, 0.75, var_0.origin, 60 );
        thread vanguard_rumble( var_0, "damage_heavy", 3 );

        if ( var_0.ammocount == 0 )
        {
            wait 0.75;
            var_0 notify( "death" );
        }
    }
}

updateweaponui( var_0, var_1 )
{
    level endon( "game_ended" );
    self endon( "disconnect" );
    var_0 endon( "death" );
    var_0 endon( "end_remote" );
    self setclientomnvar( "ui_vanguard_ammo", -1 );
    wait( var_1 );
    self setclientomnvar( "ui_vanguard_ammo", var_0.ammocount );
}

getstartposition( var_0, var_1 )
{
    var_2 = ( 3000, 3000, 3000 );
    var_3 = vectornormalize( var_0.origin - ( var_1 + ( 0, 0, -400 ) ) );
    var_4 = rotatevector( var_3, ( 0, 25, 0 ) );
    var_5 = var_1 + var_4 * var_2;

    if ( isvalidstartpoint( var_5, var_1 ) )
        return var_5;

    var_4 = rotatevector( var_3, ( 0, -25, 0 ) );
    var_5 = var_1 + var_4 * var_2;

    if ( isvalidstartpoint( var_5, var_1 ) )
        return var_5;

    var_5 = var_1 + var_3 * var_2;

    if ( isvalidstartpoint( var_5, var_1 ) )
        return var_5;

    return var_1 + ( 0, 0, 3000 );
}

isvalidstartpoint( var_0, var_1 )
{
    var_2 = bullettrace( var_0, var_1, 0 );

    if ( var_2["fraction"] > 0.99 )
        return 1;

    return 0;
}

vanguard_watch_distance()
{
    self endon( "death" );
    var_0 = self.origin;
    self.rangecountdownactive = 0;

    for (;;)
    {
        if ( !isdefined( self ) )
            return;

        if ( !isdefined( self.owner ) )
            return;

        if ( !vanguard_in_range() )
        {
            while ( !vanguard_in_range() )
            {
                if ( !isdefined( self ) )
                    return;

                if ( !isdefined( self.owner ) )
                    return;

                if ( !self.rangecountdownactive )
                {
                    self.rangecountdownactive = 1;
                    thread vanguard_rangecountdown();
                }

                if ( isdefined( self.heliinproximity ) )
                    var_1 = distance( self.origin, self.heliinproximity.origin );
                else if ( isdefined( level.disablevanguardsinair ) )
                    var_1 = 467.5;
                else
                    var_1 = distance( self.origin, var_0 );

                var_2 = getsignalstrengthalpha( var_1 );
                self.owner setclientomnvar( "ui_vanguard", var_2 );
                wait 0.1;
            }

            self notify( "in_range" );
            self.rangecountdownactive = 0;
            self.owner setclientomnvar( "ui_vanguard", 1 );
        }

        var_3 = int( angleclamp( self.angles[1] ) );
        self.owner setclientomnvar( "ui_vanguard_heading", var_3 );
        var_4 = self.origin[2] * 0.0254;
        var_4 = int( clamp( var_4, -250, 250 ) );
        self.owner setclientomnvar( "ui_vanguard_altitude", var_4 );
        var_5 = distance2d( self.origin, self.attackarrow.origin ) * 0.0254;
        var_5 = int( clamp( var_5, 0, 256 ) );
        self.owner setclientomnvar( "ui_vanguard_range", var_5 );
        var_0 = self.origin;
        wait 0.1;
    }
}

getsignalstrengthalpha( var_0 )
{
    var_0 = clamp( var_0, 50, 550 );
    return 2 + int( 8 * ( var_0 - 50 ) / 500 );
}

vanguard_in_range()
{
    if ( isdefined( self.inheliproximity ) && self.inheliproximity )
        return 0;

    if ( isdefined( level.disablevanguardsinair ) )
        return 0;

    if ( isdefined( level.vanguardrangetriggers[0] ) )
    {
        foreach ( var_1 in level.vanguardrangetriggers )
        {
            if ( self istouching( var_1 ) )
                return 0;
        }

        if ( level.is_mp_descent )
            return self.origin[2] < level.vanguardmaxheight;
        else
            return 1;
    }
    else if ( distance2dsquared( self.origin, level.mapcenter ) < level.vanguradmaxdistancesq && self.origin[2] < level.vanguardmaxheight )
        return 1;

    return 0;
}

vanguard_rangecountdown()
{
    self endon( "death" );
    self endon( "in_range" );

    if ( isdefined( self.heliinproximity ) )
        var_0 = 3;
    else
        var_0 = 6;

    maps\mp\gametypes\_hostmigration::waitlongdurationwithhostmigrationpause( var_0 );
    self notify( "death", "range_death" );
}

vanguard_monitorkillstreakdisowned( var_0 )
{
    var_0 endon( "death" );
    var_0.owner common_scripts\utility::waittill_any( "killstreak_disowned" );
    var_0 notify( "death" );
}

vanguard_monitortimeout( var_0, var_1 )
{
    var_0 endon( "death" );
    var_2 = 60;

    if ( !maps\mp\_utility::is_aliens() )
    {

    }
    else
        var_2 = var_1;

    maps\mp\gametypes\_hostmigration::waitlongdurationwithhostmigrationpause( var_2 );

    if ( isdefined( var_0.owner ) )
        var_0.owner maps\mp\_utility::leaderdialogonplayer( "gryphon_gone" );

    var_0 notify( "death" );
}

vanguard_monitordeath( var_0 )
{
    level endon( "game_ended" );
    level endon( "objective_cam" );
    var_1 = var_0.turret;
    var_0 waittill( "death" );
    var_0 maps\mp\gametypes\_weapons::stopblinkinglight();
    stopfxontag( level.vanguard_fx["target_marker_circle"], var_0.attackarrow, "tag_origin" );
    playfx( level.vanguard_fx["explode"], var_0.origin );
    var_0 playsound( "ball_drone_explode" );
    var_1 delete();

    if ( isdefined( var_0.targeteffect ) )
        var_0.targeteffect delete();

    vanguard_endride( var_0.owner, var_0 );
}

vanguard_monitorobjectivecam( var_0 )
{
    var_0 endon( "death" );
    level common_scripts\utility::waittill_any( "objective_cam", "game_ended" );
    playfx( level.vanguard_fx["explode"], var_0.origin );
    var_0 playsound( "ball_drone_explode" );
    vanguard_endride( var_0.owner, var_0 );
}

vanguard_endride( var_0, var_1 )
{
    var_1 notify( "end_remote" );
    var_1.playerlinked = 0;
    var_1 setotherent( undefined );
    vanguard_removeplayer( var_0, var_1 );
    stopfxontag( level.vanguard_fx["smoke"], var_1, "tag_origin" );
    level.remote_uav[var_1.team] = undefined;
    maps\mp\_utility::decrementfauxvehiclecount();

    if ( isdefined( var_1.killcament ) )
        var_1.killcament delete();

    var_1.attackarrow delete();
    var_1 delete();
}

restorevisionset()
{
    self visionsetnakedforplayer( "", 1 );
    maps\mp\_utility::set_visionset_for_watching_players( "", 1 );
}

vanguard_removeplayer( var_0, var_1 )
{
    if ( !isdefined( var_0 ) )
        return;

    var_0 maps\mp\_utility::clearusingremote();
    var_0 restorevisionset();
    var_0 setclientomnvar( "ui_vanguard", 0 );

    if ( getdvarint( "camera_thirdPerson" ) )
        var_0 maps\mp\_utility::setthirdpersondof( 1 );

    var_0 cameraunlink( var_1 );
    var_0 remotecontrolvehicleoff( var_1 );
    var_0 thermalvisionoff();
    var_0 thermalvisionfofoverlayoff();
    var_0 setplayerangles( var_0.restoreangles );
    var_0.remoteuav = undefined;

    if ( var_0.team == "spectator" )
        return;

    level thread vanguard_freezecontrolsbuffer( var_0 );
}

vanguard_freezecontrolsbuffer( var_0 )
{
    var_0 endon( "disconnect" );
    var_0 endon( "death" );
    level endon( "game_ended" );
    var_0 maps\mp\_utility::freezecontrolswrapper( 1 );
    wait 0.5;
    var_0 maps\mp\_utility::freezecontrolswrapper( 0 );
}

vanguard_watchheliproximity()
{
    level endon( "game_ended" );
    self endon( "death" );
    self endon( "end_remote" );

    for (;;)
    {
        var_0 = 0;

        foreach ( var_2 in level.helis )
        {
            if ( distance( var_2.origin, self.origin ) < 300 )
            {
                var_0 = 1;
                self.heliinproximity = var_2;
            }
        }

        foreach ( var_5 in level.littlebirds )
        {
            if ( var_5 != self && ( !isdefined( var_5.helitype ) || var_5.helitype != "remote_uav" ) && distance( var_5.origin, self.origin ) < 300 )
            {
                var_0 = 1;
                self.heliinproximity = var_5;
            }
        }

        if ( !self.inheliproximity && var_0 )
            self.inheliproximity = 1;
        else if ( self.inheliproximity && !var_0 )
        {
            self.inheliproximity = 0;
            self.heliinproximity = undefined;
        }

        wait 0.05;
    }
}

vanguard_handledamage()
{
    self endon( "death" );
    level endon( "game_ended" );
    self setcandamage( 1 );

    for (;;)
    {
        self waittill( "damage", var_0, var_1, var_2, var_3, var_4, var_5, var_6, var_7, var_8, var_9 );
        maps\mp\gametypes\_damage::monitordamageoneshot( var_0, var_1, var_2, var_3, var_4, var_5, var_6, var_7, var_8, var_9, "remote_uav", ::handledeathdamage, ::modifydamage, 1 );
    }
}

vanguard_turret_handledamage()
{
    self endon( "death" );
    level endon( "game_ended" );
    self maketurretsolid();
    self setcandamage( 1 );

    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 ( isdefined( self.parent ) )
            self.parent maps\mp\gametypes\_damage::monitordamageoneshot( var_0, var_1, var_2, var_3, var_4, var_5, var_6, var_7, var_8, var_9, "remote_uav", ::handledeathdamage, ::modifydamage, 1 );
    }
}

modifydamage( var_0, var_1, var_2, var_3 )
{
    var_4 = var_3;
    var_4 = maps\mp\gametypes\_damage::handleempdamage( var_1, var_2, var_4 );
    var_4 = maps\mp\gametypes\_damage::handlemissiledamage( var_1, var_2, var_4 );
    var_4 = maps\mp\gametypes\_damage::handlegrenadedamage( var_1, var_2, var_4 );
    var_4 = maps\mp\gametypes\_damage::handleapdamage( var_1, var_2, var_4, var_0 );

    if ( var_2 == "MOD_MELEE" )
        var_4 = self.maxhealth * 0.34;

    playfxontagforclients( level.vanguard_fx["hit"], self, "tag_origin", self.owner );

    if ( self.smoking == 0 && self.damagetaken >= self.maxhealth / 2 )
    {
        self.smoking = 1;
        playfxontag( level.vanguard_fx["smoke"], self, "tag_origin" );
    }

    return var_4;
}

handledeathdamage( var_0, var_1, var_2, var_3 )
{
    if ( isdefined( self.owner ) )
        self.owner maps\mp\_utility::leaderdialogonplayer( "gryphon_destroyed" );

    maps\mp\gametypes\_damage::onkillstreakkilled( var_0, var_1, var_2, var_3, "destroyed_vanguard", undefined, "callout_destroyed_vanguard" );

    if ( isdefined( var_0 ) )
    {
        var_0 maps\mp\gametypes\_missions::processchallenge( "ch_gryphondown" );
        maps\mp\gametypes\_missions::checkaachallenges( var_0, self, var_1 );
    }
}

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

    for (;;)
    {
        self waittill( "emp_damage", var_0, var_1 );
        stopfxontag( level.vanguard_fx["target_marker_circle"], self.attackarrow, "tag_origin" );
        common_scripts\utility::waitframe();
        thread vanguard_showreticletoenemies();
        playfxontag( common_scripts\utility::getfx( "emp_stun" ), self, "tag_origin" );
        wait( var_1 );
        stopfxontag( level.vanguard_fx["target_marker_circle"], self.attackarrow, "tag_origin" );
        common_scripts\utility::waitframe();
        thread vanguard_reticlestart();
    }
}
