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

init()
{
    if ( level.teambased && !isdefined( level.nobuddyspawns ) )
    {
        if ( !isdefined( level.battlebuddywaitlist ) )
            level.battlebuddywaitlist = [];

        level thread onplayerspawned();
        level thread onplayerconnect();
    }
}

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

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

    for (;;)
    {
        level waittill( "player_spawned", var_0 );

        if ( !isai( var_0 ) )
        {
            if ( isdefined( var_0.isspawningonbattlebuddy ) )
            {
                var_0.isspawningonbattlebuddy = undefined;

                if ( isdefined( var_0.battlebuddy ) && isalive( var_0.battlebuddy ) )
                {
                    if ( var_0.battlebuddy getstance() != "stand" )
                        var_0 setstance( "crouch" );
                }
            }

            if ( var_0 wantsbattlebuddy() )
            {
                if ( !var_0 hasbattlebuddy() )
                {
                    var_0.firstspawn = 0;
                    var_0 findbattlebuddy();
                }

                continue;
            }

            var_0 leavebattlebuddysystem();
        }
    }
}

onbattlebuddymenuselection()
{
    self endon( "disconnect" );
    level endon( "game_ended" );

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

        if ( var_0 == "battlebuddy_update" )
        {
            var_2 = !wantsbattlebuddy();
            self setcommonplayerdata( "enableBattleBuddy", var_2 );

            if ( var_2 )
                findbattlebuddy();
            else
                leavebattlebuddysystem();

            continue;
        }

        if ( var_0 == "team_select" && self.hasspawned )
        {
            var_3 = wantsbattlebuddy();
            leavebattlebuddysystem();
            self setcommonplayerdata( "enableBattleBuddy", var_3 );
        }
    }
}

ondisconnect()
{
    self waittill( "disconnect" );
    leavebattlebuddysystemdisconnect();
}

waitforplayerrespawnchoice()
{
    maps\mp\_utility::updatesessionstate( "spectator" );
    self.forcespectatorclient = self.battlebuddy getentitynumber();
    self forcethirdpersonwhenfollowing();
    self setclientomnvar( "cam_scene_name", "over_shoulder" );
    self setclientomnvar( "cam_scene_lead", self.battlebuddy getentitynumber() );
    waitforbuddyspawntimer();
}

watchforrandomspawnbutton()
{
    self endon( "disconnect" );
    self endon( "abort_battlebuddy_spawn" );
    self endon( "teamSpawnPressed" );
    level endon( "game_ended" );
    self setclientomnvar( "ui_battlebuddy_showButtonPrompt", 1 );
    self notifyonplayercommand( "respawn_random", "+usereload" );
    self notifyonplayercommand( "respawn_random", "+activate" );
    wait 0.5;
    self waittill( "respawn_random" );
    self setclientomnvar( "ui_battlebuddy_timer_ms", 0 );
    self setclientomnvar( "ui_battlebuddy_showButtonPrompt", 0 );
    setupforrandomspawn();
}

setupforrandomspawn()
{
    clearbuddymessage();
    self.isspawningonbattlebuddy = undefined;
    self notify( "randomSpawnPressed" );
    cleanupbuddyspawn();
}

waitforbuddyspawntimer()
{
    self endon( "randomSpawnPressed" );
    level endon( "game_ended" );
    self.isspawningonbattlebuddy = undefined;
    thread watchforrandomspawnbutton();

    if ( isdefined( self.battlebuddyrespawntimestamp ) )
    {
        var_0 = 4000 - ( gettime() - self.battlebuddyrespawntimestamp );

        if ( var_0 < 2000 )
            var_0 = 2000;
    }
    else
        var_0 = 4000;

    var_1 = checkbuddyspawn();

    if ( var_1.status == 0 )
        self.battlebuddy setclientomnvar( "ui_battlebuddy_status", "incoming" );
    else if ( var_1.status == -1 || var_1.status == -3 )
        self.battlebuddy setclientomnvar( "ui_battlebuddy_status", "err_combat" );
    else
        self.battlebuddy setclientomnvar( "ui_battlebuddy_status", "err_pos" );

    updatetimer( var_0 );

    for ( var_1 = checkbuddyspawn(); var_1.status != 0; var_1 = checkbuddyspawn() )
    {
        if ( var_1.status == -1 || var_1.status == -3 )
        {
            self setclientomnvar( "ui_battlebuddy_status", "wait_combat" );
            self.battlebuddy setclientomnvar( "ui_battlebuddy_status", "err_combat" );
        }
        else if ( var_1.status == -2 )
        {
            self setclientomnvar( "ui_battlebuddy_status", "wait_pos" );
            self.battlebuddy setclientomnvar( "ui_battlebuddy_status", "err_pos" );
        }
        else if ( var_1.status == -4 )
        {
            cleanupbuddyspawn();
            return;
        }

        wait 0.5;
    }

    self.isspawningonbattlebuddy = 1;
    thread displaybuddyspawnsuccessful();
    self playlocalsound( "copycat_steal_class" );
    self notify( "teamSpawnPressed" );
}

clearbuddymessage()
{
    self setclientomnvar( "ui_battlebuddy_status", "none" );
    self setclientomnvar( "ui_battlebuddy_showButtonPrompt", 0 );

    if ( isdefined( self.battlebuddy ) )
        self.battlebuddy setclientomnvar( "ui_battlebuddy_status", "none" );
}

displaybuddystatusmessage( var_0 )
{
    maps\mp\_utility::setlowermessage( "waiting_info", var_0, undefined, undefined, undefined, undefined, undefined, undefined, 1 );
}

displaybuddyspawnsuccessful()
{
    clearbuddymessage();

    if ( isdefined( self.battlebuddy ) )
    {
        self.battlebuddy setclientomnvar( "ui_battlebuddy_status", "on_you" );
        wait 1.5;
        self.battlebuddy setclientomnvar( "ui_battlebuddy_status", "none" );
    }
}

checkbuddyspawn()
{
    var_0 = spawnstruct();

    if ( !isdefined( self.battlebuddy ) || !isalive( self.battlebuddy ) )
    {
        var_0.status = -4;
        return var_0;
    }

    if ( maps\mp\gametypes\_spawnscoring::isplayerincombat( self.battlebuddy, 1 ) )
        var_0.status = -1;
    else
    {
        var_1 = maps\mp\gametypes\_spawnscoring::findspawnlocationnearplayer( self.battlebuddy );

        if ( isdefined( var_1 ) )
        {
            var_2 = spawnstruct();
            var_2.maxtracecount = 18;
            var_2.currenttracecount = 0;

            if ( !maps\mp\gametypes\_spawnscoring::issafetospawnon( self.battlebuddy, var_1, var_2 ) )
                var_0.status = -3;
            else
            {
                var_0.status = 0;
                var_0.origin = var_1;
                var_3 = self.battlebuddy.origin - var_1;
                var_0.angles = ( 0, self.battlebuddy.angles[1], 0 );
            }
        }
        else
            var_0.status = -2;
    }

    return var_0;
}

cleanupbuddyspawn()
{
    thread maps\mp\gametypes\_spectating::setspectatepermissions();
    self.forcespectatorclient = -1;
    maps\mp\_utility::updatesessionstate( "dead" );
    self disableforcethirdpersonwhenfollowing();
    self setclientomnvar( "cam_scene_name", "unknown" );
    clearbuddymessage();
    self notify( "abort_battlebuddy_spawn" );
}

updatetimer( var_0 )
{
    self endon( "disconnect" );
    self endon( "abort_battlebuddy_spawn" );
    self endon( "teamSpawnPressed" );
    var_1 = var_0 * 0.001;
    self setclientomnvar( "ui_battlebuddy_timer_ms", var_0 + gettime() );
    wait( var_1 );
    self setclientomnvar( "ui_battlebuddy_timer_ms", 0 );
}

wantsbattlebuddy()
{
    return self getcommonplayerdata( "enableBattleBuddy" );
}

hasbattlebuddy()
{
    return isdefined( self.battlebuddy );
}

needsbattlebuddy()
{
    return wantsbattlebuddy() && !hasbattlebuddy();
}

isvalidbattlebuddy( var_0 )
{
    return self != var_0 && self.team == var_0.team && var_0 needsbattlebuddy();
}

canbuddyspawn()
{
    return hasbattlebuddy() && maps\mp\_utility::isreallyalive( self.battlebuddy );
}

pairbattlebuddy( var_0 )
{
    removefrombattlebuddywaitlist( var_0 );
    self.battlebuddy = var_0;
    var_0.battlebuddy = self;
    self setclientomnvar( "ui_battlebuddy_idx", var_0 getentitynumber() );
    var_0 setclientomnvar( "ui_battlebuddy_idx", self getentitynumber() );
}

getwaitingbattlebuddy()
{
    return level.battlebuddywaitlist[self.team];
}

addtobattlebuddywaitlist( var_0 )
{
    if ( !isdefined( level.battlebuddywaitlist[var_0.team] ) )
        level.battlebuddywaitlist[var_0.team] = var_0;
    else if ( level.battlebuddywaitlist[var_0.team] != var_0 )
        return;
}

removefrombattlebuddywaitlist( var_0 )
{
    if ( isdefined( var_0.team ) && isdefined( level.battlebuddywaitlist[var_0.team] ) && var_0 == level.battlebuddywaitlist[var_0.team] )
        level.battlebuddywaitlist[var_0.team] = undefined;
}

findbattlebuddy()
{
    if ( level.onlinegame )
    {
        self.fireteammembers = self getfireteammembers();

        if ( self.fireteammembers.size >= 1 )
        {
            foreach ( var_1 in self.fireteammembers )
            {
                if ( isvalidbattlebuddy( var_1 ) )
                    pairbattlebuddy( var_1 );
            }
        }
    }

    if ( !hasbattlebuddy() )
    {
        var_1 = getwaitingbattlebuddy();

        if ( isdefined( var_1 ) && isvalidbattlebuddy( var_1 ) )
            pairbattlebuddy( var_1 );
        else
        {
            addtobattlebuddywaitlist( self );
            self setclientomnvar( "ui_battlebuddy_idx", -1 );
        }
    }
}

clearbattlebuddy()
{
    if ( !isalive( self ) )
        setupforrandomspawn();

    self setclientomnvar( "ui_battlebuddy_idx", -1 );
    self.battlebuddy = undefined;
}

leavebattlebuddysystem()
{
    if ( hasbattlebuddy() )
    {
        var_0 = self.battlebuddy;
        clearbattlebuddy();
        self setcommonplayerdata( "enableBattleBuddy", 0 );
        var_0 clearbattlebuddy();
        var_0 findbattlebuddy();
    }
    else
    {
        removefrombattlebuddywaitlist( self );
        self setclientomnvar( "ui_battlebuddy_idx", -1 );
    }
}

leavebattlebuddysystemdisconnect()
{
    if ( hasbattlebuddy() )
    {
        var_0 = self.battlebuddy;
        var_0 clearbattlebuddy();
        var_0 findbattlebuddy();
        var_0 clearbuddymessage();
    }
    else
    {
        foreach ( var_3, var_2 in level.battlebuddywaitlist )
        {
            if ( var_2 == self )
            {
                level.battlebuddywaitlist[var_3] = undefined;
                break;
            }
        }
    }
}
