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

init()
{
    maps\mp\gametypes\_class::init();

    if ( !maps\mp\_utility::is_aliens() )
    {
        level.persistentdatainfo = [];
        maps\mp\gametypes\_missions::init();
        maps\mp\gametypes\_rank::init();
        level thread updatebufferedstats();
        level thread uploadglobalstatcounters();
        level thread writekdhistorystats();
    }

    maps\mp\gametypes\_playercards::init();
}

initbufferedstats()
{
    self.bufferedstats = [];
    self.squadmemberbufferedstats = [];

    if ( maps\mp\_utility::rankingenabled() )
    {
        self.bufferedstats["totalShots"] = self getrankedplayerdata( "totalShots" );
        self.bufferedstats["accuracy"] = self getrankedplayerdata( "accuracy" );
        self.bufferedstats["misses"] = self getrankedplayerdata( "misses" );
        self.bufferedstats["hits"] = self getrankedplayerdata( "hits" );
        self.bufferedstats["timePlayedAllies"] = self getrankedplayerdata( "timePlayedAllies" );
        self.bufferedstats["timePlayedOpfor"] = self getrankedplayerdata( "timePlayedOpfor" );
        self.bufferedstats["timePlayedOther"] = self getrankedplayerdata( "timePlayedOther" );
        self.bufferedstats["timePlayedTotal"] = self getrankedplayerdata( "timePlayedTotal" );
        var_0 = self getrankedplayerdata( "activeSquadMember" );
        self.squadmemberbufferedstats["experienceToPrestige"] = self getrankedplayerdata( "squadMembers", var_0, "experienceToPrestige" );
    }

    self.bufferedchildstats = [];
    self.bufferedchildstats["round"] = [];
    self.bufferedchildstats["round"]["timePlayed"] = self getcommonplayerdata( "round", "timePlayed" );

    if ( maps\mp\_utility::rankingenabled() )
    {
        self.bufferedchildstats["xpMultiplierTimePlayed"] = [];
        self.bufferedchildstats["xpMultiplierTimePlayed"][0] = self getrankedplayerdata( "xpMultiplierTimePlayed", 0 );
        self.bufferedchildstats["xpMultiplierTimePlayed"][1] = self getrankedplayerdata( "xpMultiplierTimePlayed", 1 );
        self.bufferedchildstats["xpMultiplierTimePlayed"][2] = self getrankedplayerdata( "xpMultiplierTimePlayed", 2 );
        self.bufferedchildstatsmax["xpMaxMultiplierTimePlayed"] = [];
        self.bufferedchildstatsmax["xpMaxMultiplierTimePlayed"][0] = self getrankedplayerdata( "xpMaxMultiplierTimePlayed", 0 );
        self.bufferedchildstatsmax["xpMaxMultiplierTimePlayed"][1] = self getrankedplayerdata( "xpMaxMultiplierTimePlayed", 1 );
        self.bufferedchildstatsmax["xpMaxMultiplierTimePlayed"][2] = self getrankedplayerdata( "xpMaxMultiplierTimePlayed", 2 );
        self.bufferedchildstats["challengeXPMultiplierTimePlayed"] = [];
        self.bufferedchildstats["challengeXPMultiplierTimePlayed"][0] = self getrankedplayerdata( "challengeXPMultiplierTimePlayed", 0 );
        self.bufferedchildstatsmax["challengeXPMaxMultiplierTimePlayed"] = [];
        self.bufferedchildstatsmax["challengeXPMaxMultiplierTimePlayed"][0] = self getrankedplayerdata( "challengeXPMaxMultiplierTimePlayed", 0 );
        self.bufferedchildstats["weaponXPMultiplierTimePlayed"] = [];
        self.bufferedchildstats["weaponXPMultiplierTimePlayed"][0] = self getrankedplayerdata( "weaponXPMultiplierTimePlayed", 0 );
        self.bufferedchildstatsmax["weaponXPMaxMultiplierTimePlayed"] = [];
        self.bufferedchildstatsmax["weaponXPMaxMultiplierTimePlayed"][0] = self getrankedplayerdata( "weaponXPMaxMultiplierTimePlayed", 0 );

        if ( issquadsmode() )
        {
            self.bufferedstats["prestigeDoubleXp"] = self getrankedplayerdata( "prestigeDoubleXp" );
            self.bufferedstats["prestigeDoubleXpTimePlayed"] = self getrankedplayerdata( "prestigeDoubleXpTimePlayed" );
            self.bufferedstatsmax["prestigeDoubleXpMaxTimePlayed"] = self getrankedplayerdata( "prestigeDoubleXpMaxTimePlayed" );
        }

        self.bufferedstats["prestigeDoubleWeaponXp"] = self getrankedplayerdata( "prestigeDoubleWeaponXp" );
        self.bufferedstats["prestigeDoubleWeaponXpTimePlayed"] = self getrankedplayerdata( "prestigeDoubleWeaponXpTimePlayed" );
        self.bufferedstatsmax["prestigeDoubleWeaponXpMaxTimePlayed"] = self getrankedplayerdata( "prestigeDoubleWeaponXpMaxTimePlayed" );
    }
}

statget( var_0 )
{
    return self getrankedplayerdata( var_0 );
}

statset( var_0, var_1 )
{
    if ( !maps\mp\_utility::rankingenabled() )
        return;

    self setrankedplayerdata( var_0, var_1 );
}

statadd( var_0, var_1, var_2 )
{
    if ( !maps\mp\_utility::rankingenabled() )
        return;

    if ( isdefined( var_2 ) )
    {
        var_3 = self getrankedplayerdata( var_0, var_2 );
        self setrankedplayerdata( var_0, var_2, var_1 + var_3 );
    }
    else
    {
        var_3 = self getrankedplayerdata( var_0 );
        self setrankedplayerdata( var_0, var_1 + var_3 );
    }
}

statgetchild( var_0, var_1 )
{
    if ( var_0 == "round" )
        return self getcommonplayerdata( var_0, var_1 );
    else
        return self getrankedplayerdata( var_0, var_1 );
}

statsetchild( var_0, var_1, var_2 )
{
    if ( isagent( self ) )
        return;

    if ( !maps\mp\_utility::rankingenabled() )
        return;

    if ( var_0 == "round" )
        self setcommonplayerdata( var_0, var_1, var_2 );
    else
        self setrankedplayerdata( var_0, var_1, var_2 );
}

stataddchild( var_0, var_1, var_2 )
{
    if ( !maps\mp\_utility::rankingenabled() )
        return;

    var_3 = self getrankedplayerdata( var_0, var_1 );
    self setrankedplayerdata( var_0, var_1, var_3 + var_2 );
}

statgetchildbuffered( var_0, var_1 )
{
    if ( !maps\mp\_utility::rankingenabled() )
        return 0;

    return self.bufferedchildstats[var_0][var_1];
}

statsetchildbuffered( var_0, var_1, var_2 )
{
    if ( !maps\mp\_utility::rankingenabled() )
        return;

    self.bufferedchildstats[var_0][var_1] = var_2;
}

stataddchildbuffered( var_0, var_1, var_2 )
{
    if ( !maps\mp\_utility::rankingenabled() )
        return;

    var_3 = statgetchildbuffered( var_0, var_1 );
    statsetchildbuffered( var_0, var_1, var_3 + var_2 );
}

stataddbufferedwithmax( var_0, var_1, var_2 )
{
    if ( !maps\mp\_utility::rankingenabled() )
        return;

    var_3 = statgetbuffered( var_0 ) + var_1;

    if ( var_3 > var_2 )
        var_3 = var_2;

    if ( var_3 < statgetbuffered( var_0 ) )
        var_3 = var_2;

    statsetbuffered( var_0, var_3 );
}

stataddchildbufferedwithmax( var_0, var_1, var_2, var_3 )
{
    if ( !maps\mp\_utility::rankingenabled() )
        return;

    var_4 = statgetchildbuffered( var_0, var_1 ) + var_2;

    if ( var_4 > var_3 )
        var_4 = var_3;

    if ( var_4 < statgetchildbuffered( var_0, var_1 ) )
        var_4 = var_3;

    statsetchildbuffered( var_0, var_1, var_4 );
}

statgetbuffered( var_0 )
{
    if ( !maps\mp\_utility::rankingenabled() )
        return 0;

    return self.bufferedstats[var_0];
}

statgetsquadbuffered( var_0 )
{
    if ( !maps\mp\_utility::rankingenabled() )
        return 0;

    return self.squadmemberbufferedstats[var_0];
}

statsetbuffered( var_0, var_1 )
{
    if ( !maps\mp\_utility::rankingenabled() )
        return;

    self.bufferedstats[var_0] = var_1;
}

statsetsquadbuffered( var_0, var_1 )
{
    if ( !maps\mp\_utility::rankingenabled() )
        return;

    self.squadmemberbufferedstats[var_0] = var_1;
}

stataddbuffered( var_0, var_1 )
{
    if ( !maps\mp\_utility::rankingenabled() )
        return;

    var_2 = statgetbuffered( var_0 );
    statsetbuffered( var_0, var_2 + var_1 );
}

stataddsquadbuffered( var_0, var_1 )
{
    if ( !maps\mp\_utility::rankingenabled() )
        return;

    var_2 = statgetsquadbuffered( var_0 );
    statsetsquadbuffered( var_0, var_2 + var_1 );
}

updatebufferedstats()
{
    wait 0.15;
    var_0 = 0;

    while ( !level.gameended )
    {
        maps\mp\gametypes\_hostmigration::waittillhostmigrationdone();
        var_0++;

        if ( var_0 >= level.players.size )
            var_0 = 0;

        if ( isdefined( level.players[var_0] ) )
        {
            level.players[var_0] writebufferedstats();
            level.players[var_0] updateweaponbufferedstats();
        }

        wait 2.0;
    }

    foreach ( var_2 in level.players )
    {
        var_2 writebufferedstats();
        var_2 updateweaponbufferedstats();
    }
}

writebufferedstats()
{
    var_0 = maps\mp\_utility::rankingenabled();

    if ( var_0 )
    {
        foreach ( var_3, var_2 in self.bufferedstats )
            self setrankedplayerdata( var_3, var_2 );

        if ( !isai( self ) )
        {
            foreach ( var_3, var_2 in self.squadmemberbufferedstats )
                self setrankedplayerdata( "squadMembers", self.pers["activeSquadMember"], var_3, var_2 );
        }
    }

    foreach ( var_3, var_2 in self.bufferedchildstats )
    {
        foreach ( var_8, var_7 in var_2 )
        {
            if ( var_3 == "round" )
            {
                self setcommonplayerdata( var_3, var_8, var_7 );
                continue;
            }

            if ( var_0 )
                self setrankedplayerdata( var_3, var_8, var_7 );
        }
    }
}

writekdhistorystats()
{
    if ( !maps\mp\_utility::matchmakinggame() )
        return;

    if ( issquadsmode() )
        return;

    level waittill( "game_ended" );
    wait 0.1;

    if ( maps\mp\_utility::waslastround() || !maps\mp\_utility::isroundbased() && maps\mp\_utility::hittimelimit() )
    {
        foreach ( var_1 in level.players )
            var_1 incrementrankedreservedhistory( var_1.kills, var_1.deaths );
    }
}

incrementrankedreservedhistory( var_0, var_1 )
{
    if ( !maps\mp\_utility::rankingenabled() )
        return;

    for ( var_2 = 0; var_2 < 4; var_2++ )
    {
        var_3 = self getrankedplayerdatareservedint( "kdHistoryK" + ( var_2 + 1 ) );
        self setrankedplayerdatareservedint( "kdHistoryK" + var_2, var_3 );
        var_3 = self getrankedplayerdatareservedint( "kdHistoryD" + ( var_2 + 1 ) );
        self setrankedplayerdatareservedint( "kdHistoryD" + var_2, var_3 );
    }

    self setrankedplayerdatareservedint( "kdHistoryK4", int( clamp( var_0, 0, 255 ) ) );
    self setrankedplayerdatareservedint( "kdHistoryD4", int( clamp( var_1, 0, 255 ) ) );
}

incrementweaponstat( var_0, var_1, var_2 )
{
    if ( var_0 == "iw6_pdwauto" )
        var_0 = "iw6_pdw";
    else if ( var_0 == "iw6_knifeonlyfast" )
        var_0 = "iw6_knifeonly";

    if ( maps\mp\_utility::iskillstreakweapon( var_0 ) )
        return;

    if ( isdefined( level.disableweaponstats ) )
        return;

    if ( maps\mp\_utility::rankingenabled() )
    {
        var_3 = self getrankedplayerdata( "weaponStats", var_0, var_1 );
        self setrankedplayerdata( "weaponStats", var_0, var_1, var_3 + var_2 );
    }
}

incrementattachmentstat( var_0, var_1, var_2 )
{
    if ( isdefined( level.disableweaponstats ) )
        return;

    if ( maps\mp\_utility::rankingenabled() )
    {
        var_3 = self getrankedplayerdata( "attachmentsStats", var_0, var_1 );
        self setrankedplayerdata( "attachmentsStats", var_0, var_1, var_3 + var_2 );
    }
}

updateweaponbufferedstats()
{
    if ( !isdefined( self.trackingweaponname ) )
        return;

    if ( self.trackingweaponname == "" || self.trackingweaponname == "none" )
        return;

    if ( maps\mp\_utility::iskillstreakweapon( self.trackingweaponname ) || maps\mp\_utility::isenvironmentweapon( self.trackingweaponname ) )
        return;

    var_0 = self.trackingweaponname;
    var_1 = undefined;
    var_2 = getsubstr( var_0, 0, 4 );

    if ( var_2 == "alt_" )
    {
        var_3 = maps\mp\_utility::getweaponattachmentsbasenames( var_0 );

        foreach ( var_5 in var_3 )
        {
            if ( var_5 == "shotgun" || var_5 == "gl" )
            {
                var_1 = var_5;
                break;
            }
        }

        if ( !isdefined( var_1 ) )
        {
            var_7 = strtok( var_0, "_" );
            var_1 = var_7[1] + "_" + var_7[2];
        }
    }
    else if ( var_2 == "iw5_" || var_2 == "iw6_" )
    {
        var_7 = strtok( var_0, "_" );
        var_1 = var_7[0] + "_" + var_7[1];
    }

    if ( var_1 == "gl" || var_1 == "shotgun" )
    {
        perslog_attachmentstats( var_1 );
        persclear_stats();
        return;
    }

    if ( !maps\mp\_utility::iscacprimaryweapon( var_1 ) && !maps\mp\_utility::iscacsecondaryweapon( var_1 ) )
        return;

    perslog_weaponstats( var_1 );
    var_3 = getweaponattachments( var_0 );

    foreach ( var_5 in var_3 )
    {
        var_9 = maps\mp\_utility::attachmentmap_tobase( var_5 );

        switch ( var_9 )
        {
            case "scope":
            case "gl":
            case "shotgun":
                continue;
        }

        perslog_attachmentstats( var_9 );
    }

    persclear_stats();
}

persclear_stats()
{
    self.trackingweaponname = "none";
    self.trackingweaponshots = 0;
    self.trackingweaponkills = 0;
    self.trackingweaponhits = 0;
    self.trackingweaponheadshots = 0;
    self.trackingweapondeaths = 0;
}

perslog_weaponstats( var_0 )
{
    if ( self.trackingweaponshots > 0 )
    {
        incrementweaponstat( var_0, "shots", self.trackingweaponshots );
        maps\mp\_matchdata::logweaponstat( var_0, "shots", self.trackingweaponshots );
    }

    if ( self.trackingweaponkills > 0 )
    {
        incrementweaponstat( var_0, "kills", self.trackingweaponkills );
        maps\mp\_matchdata::logweaponstat( var_0, "kills", self.trackingweaponkills );
    }

    if ( self.trackingweaponhits > 0 )
    {
        incrementweaponstat( var_0, "hits", self.trackingweaponhits );
        maps\mp\_matchdata::logweaponstat( var_0, "hits", self.trackingweaponhits );
    }

    if ( self.trackingweaponheadshots > 0 )
    {
        incrementweaponstat( var_0, "headShots", self.trackingweaponheadshots );
        maps\mp\_matchdata::logweaponstat( var_0, "headShots", self.trackingweaponheadshots );
    }

    if ( self.trackingweapondeaths > 0 )
    {
        incrementweaponstat( var_0, "deaths", self.trackingweapondeaths );
        maps\mp\_matchdata::logweaponstat( var_0, "deaths", self.trackingweapondeaths );
    }
}

perslog_attachmentstats( var_0 )
{
    if ( self.trackingweaponshots > 0 && var_0 != "tactical" )
    {
        incrementattachmentstat( var_0, "shots", self.trackingweaponshots );
        maps\mp\_matchdata::logattachmentstat( var_0, "shots", self.trackingweaponshots );
    }

    if ( self.trackingweaponkills > 0 && var_0 != "tactical" )
    {
        incrementattachmentstat( var_0, "kills", self.trackingweaponkills );
        maps\mp\_matchdata::logattachmentstat( var_0, "kills", self.trackingweaponkills );
    }

    if ( self.trackingweaponhits > 0 && var_0 != "tactical" )
    {
        incrementattachmentstat( var_0, "hits", self.trackingweaponhits );
        maps\mp\_matchdata::logattachmentstat( var_0, "hits", self.trackingweaponhits );
    }

    if ( self.trackingweaponheadshots > 0 && var_0 != "tactical" )
    {
        incrementattachmentstat( var_0, "headShots", self.trackingweaponheadshots );
        maps\mp\_matchdata::logattachmentstat( var_0, "headShots", self.trackingweaponheadshots );
    }

    if ( self.trackingweapondeaths > 0 )
    {
        incrementattachmentstat( var_0, "deaths", self.trackingweapondeaths );
        maps\mp\_matchdata::logattachmentstat( var_0, "deaths", self.trackingweapondeaths );
    }
}

uploadglobalstatcounters()
{
    level waittill( "game_ended" );

    if ( !maps\mp\_utility::matchmakinggame() )
        return;

    var_0 = 0;
    var_1 = 0;
    var_2 = 0;
    var_3 = 0;
    var_4 = 0;
    var_5 = 0;

    foreach ( var_7 in level.players )
        var_5 = var_5 + var_7.timeplayed["total"];

    incrementcounter( "global_minutes", int( var_5 / 60 ) );

    if ( maps\mp\_utility::isroundbased() && !maps\mp\_utility::waslastround() )
        return;

    wait 0.05;

    foreach ( var_7 in level.players )
    {
        var_0 = var_0 + var_7.kills;
        var_1 = var_1 + var_7.deaths;
        var_2 = var_2 + var_7.assists;
        var_3 = var_3 + var_7.headshots;
        var_4 = var_4 + var_7.suicides;
    }

    incrementcounter( "global_headshots", var_3 );
    incrementcounter( "global_suicides", var_4 );
    incrementcounter( "global_games", 1 );

    if ( !isdefined( level.assists_disabled ) )
        incrementcounter( "global_assists", var_2 );

    if ( !isdefined( level.ishorde ) )
        incrementcounter( "global_kills", var_0 );

    if ( !isdefined( level.ishorde ) )
        incrementcounter( "global_deaths", var_1 );
}
