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

callback_playerlaststandhorde( var_0, var_1, var_2, var_3, var_4, var_5, var_6, var_7, var_8 )
{
    registerlaststandparameter( var_0, var_1, var_2, var_3, var_4, var_5, var_6 );

    if ( gameshouldend( self ) )
    {
        self.uselaststandparams = 1;
        maps\mp\_utility::_suicide();
        hordeendgame();
        return;
    }

    if ( !maydolaststandhorde( self ) )
    {
        self.uselaststandparams = 1;
        maps\mp\_utility::_suicide();
        return;
    }

    self.inlaststand = 1;
    self.laststand = 1;
    self.ignoreme = 1;
    self.health = 1;
    self hudoutlineenable( 1, 0 );
    common_scripts\utility::_disableusability();
    thread laststandrevivehorde();
}

gameshouldend( var_0 )
{
    var_1 = 0;

    foreach ( var_3 in level.participants )
    {
        if ( var_0 == var_3 && !maps\mp\gametypes\_horde_util::hasagentsquadmember( var_0 ) )
            continue;

        if ( !maps\mp\gametypes\_horde_util::isonhumanteam( var_3 ) )
            continue;

        if ( maps\mp\gametypes\_horde_util::isplayerinlaststand( var_3 ) && !maps\mp\gametypes\_horde_util::hasagentsquadmember( var_3 ) )
            continue;

        if ( !isdefined( var_3.sessionstate ) || var_3.sessionstate != "playing" )
            continue;

        var_1 = 1;
        break;
    }

    return !var_1;
}

hordeendgame()
{
    level.finalkillcam_winner = level.enemyteam;
    level thread maps\mp\gametypes\_gamelogic::endgame( level.enemyteam, game["end_reason"][level.playerteam + "_eliminated"] );
}

maydolaststandhorde( var_0 )
{
    if ( var_0 maps\mp\_utility::touchingbadtrigger() )
        return 0;

    return 1;
}

registerlaststandparameter( var_0, var_1, var_2, var_3, var_4, var_5, var_6 )
{
    var_7 = spawnstruct();
    var_7.einflictor = var_0;
    var_7.attacker = var_1;
    var_7.idamage = var_2;
    var_7.attackerposition = var_1.origin;
    var_7.smeansofdeath = var_3;
    var_7.sweapon = var_4;
    var_7.vdir = var_5;
    var_7.shitloc = var_6;
    var_7.laststandstarttime = gettime();

    if ( isdefined( var_1 ) && isplayer( var_1 ) && var_1 getcurrentprimaryweapon() != "none" )
        var_7.sprimaryweapon = var_1 getcurrentprimaryweapon();
    else
        var_7.sprimaryweapon = undefined;

    self.laststandparams = var_7;
}

laststandrevivehorde()
{
    self endon( "death" );
    self endon( "disconnect" );
    self endon( "revive" );
    level endon( "game_ended" );
    level notify( "player_last_stand" );
    self notify( "force_cancel_placement" );
    level thread maps\mp\gametypes\_horde_util::playsoundtoallplayers( "mp_safe_team_last_stand" );
    level thread maps\mp\_utility::leaderdialog( "ally_down", level.playerteam, "status" );
    level thread perkpenalty( self );
    thread laststandwaittilldeathhorde();
    thread laststandammowacher();
    thread laststandkeepoverlayhorde();
    var_0 = spawn( "script_model", self.origin );
    var_0 setmodel( "tag_origin" );
    var_0 setcursorhint( "HINT_NOICON" );
    var_0 sethintstring( &"PLATFORM_REVIVE" );
    var_0 makeusable();
    var_0.inuse = 0;
    var_0.curprogress = 0;
    var_0.usetime = level.laststandusetime;
    var_0.userate = 1;
    var_0.id = "last_stand";
    var_0.targetname = "revive_trigger";
    var_0.owner = self;
    var_0 linkto( self, "tag_origin", ( 0, 0, 20 ), ( 0, 0, 0 ) );
    var_0 thread maps\mp\gametypes\_damage::deleteonreviveordeathordisconnect();
    var_1 = newteamhudelem( self.team );
    var_1 setshader( "waypoint_revive", 8, 8 );
    var_1 setwaypoint( 1, 1 );
    var_1 settargetent( self );
    var_1.color = ( 0.33, 0.75, 0.24 );
    var_1 thread maps\mp\gametypes\_damage::destroyonreviveentdeath( var_0 );
    thread laststandupdatereviveiconcolorhorde( var_0, var_1, 25 );
    thread laststandtimerhorde( 25, var_0 );
    var_0 thread revivetriggerthinkhorde();
    var_0 thread laststandwaittillliferecived();
    var_0 endon( "death" );
    wait 25;

    while ( isdefined( var_0.inuse ) && var_0.inuse )
        common_scripts\utility::waitframe();

    level thread maps\mp\_utility::leaderdialog( "ally_dead", level.playerteam, "status" );
    self hudoutlinedisable();
    maps\mp\_utility::_suicide();
}

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

    for (;;)
    {
        self makeusable();
        self waittill( "trigger", var_0 );
        self makeunusable();
        self.curprogress = 0;
        self.inuse = 1;
        self.owner.beingrevived = 1;
        var_0 freezecontrols( 1 );
        var_0 common_scripts\utility::_disableweapon();
        var_0.isreviving = 1;
        var_1 = maps\mp\gametypes\_damage::useholdthinkloop( var_0 );
        self.inuse = 0;

        if ( isdefined( self.owner ) )
            self.owner.beingrevived = 0;

        if ( isdefined( var_0 ) && maps\mp\_utility::isreallyalive( var_0 ) )
        {
            var_0 freezecontrols( 0 );
            var_0 common_scripts\utility::_enableweapon();
            var_0.isreviving = 0;

            if ( isdefined( var_1 ) && var_1 )
            {
                var_0 thread maps\mp\gametypes\_hud_message::splashnotifydelayed( "horde_reviver" );
                var_0 thread maps\mp\perks\_perkfunctions::setlightarmor( 850 );

                if ( isplayer( var_0 ) )
                    maps\mp\gametypes\_horde_util::awardhorderevive( var_0 );
                else if ( isdefined( var_0.owner ) && isplayer( var_0.owner ) && var_0.owner != self.owner )
                    maps\mp\gametypes\_horde_util::awardhorderevive( var_0.owner );
            }

            if ( !isdefined( var_1 ) )
                var_0 maps\mp\gametypes\_gameobjects::updateuiprogress( self, 0 );
        }

        if ( isdefined( var_1 ) && var_1 )
        {
            self.owner notify( "revive_trigger", var_0 );
            break;
        }
    }
}

laststandwaittillliferecived()
{
    self endon( "death" );
    self endon( "game_ended" );
    var_0 = self.owner;
    var_0 waittill( "revive_trigger", var_1 );

    if ( isdefined( var_1 ) && isplayer( var_1 ) && var_1 != var_0 )
        var_0 thread maps\mp\gametypes\_hud_message::playercardsplashnotify( "revived", var_1 );

    var_0 laststandrespawnplayerhorde( self );
}

laststandrespawnplayerhorde( var_0 )
{
    self notify( "revive" );
    self.laststand = undefined;
    self.inlaststand = 0;
    self.headicon = "";
    self.health = self.maxhealth;
    self.movespeedscaler = 1;
    self.ignoreme = 0;
    self.beingrevived = 0;

    if ( maps\mp\_utility::_hasperk( "specialty_lightweight" ) )
        self.movespeedscaler = maps\mp\_utility::lightweightscalar();

    self hudoutlinedisable();
    self laststandrevive();
    self setstance( "crouch" );
    common_scripts\utility::_enableusability();
    maps\mp\gametypes\_weapons::updatemovespeedscale();
    maps\mp\_utility::clearlowermessage( "last_stand" );
    maps\mp\_utility::giveperk( "specialty_pistoldeath", 0 );

    if ( !canspawn( self.origin ) )
        maps\mp\_movers::unresolved_collision_nearest_node( self, 0 );

    var_0 delete();
}

laststandwaittilldeathhorde()
{
    self endon( "disconnect" );
    self endon( "revive" );
    level endon( "game_ended" );
    self waittill( "death" );
    self.laststand = undefined;
    self.inlaststand = 0;
    self.ignoreme = 0;
}

laststandkeepoverlayhorde()
{
    self endon( "death" );
    self endon( "disconnect" );
    self endon( "revive" );
    level endon( "game_ended" );

    for (;;)
    {
        self.health = 2;
        common_scripts\utility::waitframe();
        self.health = 1;
        common_scripts\utility::waitframe();
    }
}

laststandupdatereviveiconcolorhorde( var_0, var_1, var_2 )
{
    self endon( "death" );
    self endon( "disconnect" );
    self endon( "revive" );
    level endon( "game_ended" );
    var_0 endon( "death" );
    maps\mp\_utility::playdeathsound();
    wait( var_2 / 3 );
    var_1.color = ( 1, 0.64, 0 );

    while ( var_0.inuse )
        wait 0.05;

    maps\mp\_utility::playdeathsound();
    wait( var_2 / 3 );
    var_1.color = ( 1, 0, 0 );

    while ( var_0.inuse )
        wait 0.05;

    maps\mp\_utility::playdeathsound();
}

laststandtimerhorde( var_0, var_1 )
{
    self endon( "disconnect" );
    var_2 = 90;

    if ( !issplitscreen() )
        var_2 = 135;

    var_3 = maps\mp\gametypes\_hud_util::createtimer( "hudsmall", 1.0 );
    var_3 maps\mp\gametypes\_hud_util::setpoint( "CENTER", undefined, 0, var_2 );
    var_3.label = &"MP_HORDE_BLEED_OUT";
    var_3.color = ( 0.804, 0.804, 0.035 );
    var_3.archived = 0;
    var_3.showinkillcam = 0;
    var_3 settimer( var_0 - 1 );
    var_1 waittill( "death" );

    if ( !isdefined( var_3 ) )
        return;

    var_3 notify( "destroying" );
    var_3 maps\mp\gametypes\_hud_util::destroyelem();
}

laststandammowacher()
{
    self endon( "death" );
    self endon( "disconnect" );
    self endon( "revive" );
    level endon( "game_ended" );

    for (;;)
    {
        wait 1.5;
        var_0 = self getcurrentprimaryweapon();

        if ( !maps\mp\gametypes\_weapons::isprimaryweapon( var_0 ) )
            continue;

        var_1 = self getweaponammostock( var_0 );
        var_2 = weaponclipsize( var_0 );

        if ( var_1 < var_2 )
            self setweaponammostock( var_0, var_2 );
    }
}

perkpenalty( var_0 )
{
    var_0 endon( "disconnect" );
    level endon( "game_ended" );

    if ( !isplayer( var_0 ) )
        return;

    while ( maps\mp\gametypes\_horde_util::isplayerinlaststand( var_0 ) && var_0.horde_perks.size > 0 )
    {
        var_1 = var_0.horde_perks[var_0.horde_perks.size - 1]["name"];
        var_2 = var_0.horde_perks[var_0.horde_perks.size - 1]["index"];
        var_0 thread flashperk( var_2 );
        var_3 = var_0 common_scripts\utility::waittill_any_return_no_endon_death( "remove_perk", "death", "revive" );

        if ( var_3 == "death" )
            return;

        if ( var_3 == "revive" )
        {
            if ( isdefined( var_0.flashingperkindex ) )
            {
                if ( var_0.flashingperkindex == var_2 )
                {
                    var_0 setclientomnvar( "ui_horde_update_perk", var_2 );
                    var_0.flashingperkindex = undefined;
                }
            }

            return;
        }

        if ( var_3 == "remove_perk" )
        {
            var_0 setclientomnvar( "ui_horde_update_perk", var_2 * -1 );
            var_0 maps\mp\_utility::_unsetperk( var_1 );
            var_0.horde_perks = array_remove_perk( var_0.horde_perks, var_1 );
        }
    }
}

array_remove_perk( var_0, var_1 )
{
    var_2 = [];

    foreach ( var_4 in var_0 )
    {
        if ( var_4["name"] != var_1 )
            var_2[var_2.size] = var_4;
    }

    return var_2;
}

flashperk( var_0 )
{
    self endon( "death" );
    self endon( "revive" );
    wait 0.5;
    self setclientomnvar( "ui_horde_update_perk", var_0 );
    self.flashingperkindex = var_0;
    wait 8;
    self notify( "remove_perk" );
}
