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

set_game_state( var_0 )
{
    var_1 = [ "pregame", "prehive", "progressing", "escaping", "ended" ];
    var_2 = common_scripts\utility::array_contains( var_1, var_0 );
    self setcoopplayerdata( "alienSession", "game_state", var_0 );
}

get_selected_perk_0()
{
    if ( maps\mp\alien\_utility::is_chaos_mode() )
        return "perk_none";

    return self getcoopplayerdata( "alienPlayerLoadout", "perks", 0 );
}

get_selected_perk_0_secondary()
{
    var_0 = "perk_none";

    if ( is_upgrade_enabled( "multi_class" ) && !maps\mp\alien\_utility::is_chaos_mode() )
    {
        var_1 = self getcoopplayerdatareservedint( "secondary_class" );
        var_2 = getarraykeys( level.alien_perks["perk_0"] );

        foreach ( var_4 in var_2 )
        {
            if ( level.alien_perks["perk_0"][var_4].baseidx == var_1 )
                var_0 = var_4;
        }
    }

    return var_0;
}

get_selected_perk_1()
{
    return self getcoopplayerdata( "alienPlayerLoadout", "perks", 1 );
}

get_perk_0_level()
{
    return self getcoopplayerdata( "alienSession", "perk_0_level" );
}

get_perk_1_level()
{
    return self getcoopplayerdata( "alienSession", "perk_1_level" );
}

set_perk_0_level( var_0 )
{
    self setcoopplayerdata( "alienSession", "perk_0_level", var_0 );
}

set_perk_1_level( var_0 )
{
    self setcoopplayerdata( "alienSession", "perk_1_level", var_0 );
}

set_perk( var_0 )
{
    self [[ level.alien_perk_callbacks[var_0].set ]]();
}

unset_perk( var_0 )
{
    self [[ level.alien_perk_callbacks[var_0].unset ]]();
}

init_perk_level()
{
    set_perk_0_level( 0 );
    set_perk_1_level( 0 );
}

give_initial_perks()
{
    set_perk( get_selected_perk_0() );
    set_perk( get_selected_perk_1() );

    if ( is_upgrade_enabled( "multi_class" ) )
        set_perk( get_selected_perk_0_secondary() );
}

get_selected_dpad_up()
{
    return self getcoopplayerdata( "alienPlayerLoadout", "munition" );
}

get_selected_dpad_down()
{
    return self getcoopplayerdata( "alienPlayerLoadout", "support" );
}

get_selected_dpad_left()
{
    var_0 = self getcoopplayerdata( "alienPlayerLoadout", "defense" );

    if ( var_0 == "dpad_placeholder_def_1" && maps\mp\alien\_utility::is_chaos_mode() )
        return "dpad_minigun_turret";
    else
        return var_0;
}

get_selected_dpad_right()
{
    var_0 = self getcoopplayerdata( "alienPlayerLoadout", "offense" );

    if ( var_0 == "dpad_placeholder_off_1" && maps\mp\alien\_utility::is_chaos_mode() )
        return "dpad_ims";
    else
        return var_0;
}

get_dpad_up_level()
{
    return self getcoopplayerdata( "alienSession", "munition_level" );
}

get_dpad_down_level()
{
    return self getcoopplayerdata( "alienSession", "support_level" );
}

get_dpad_left_level()
{
    return self getcoopplayerdata( "alienSession", "defense_level" );
}

get_dpad_right_level()
{
    return self getcoopplayerdata( "alienSession", "offense_level" );
}

get_upgrade_level( var_0 )
{
    return self getcoopplayerdata( "alienSession", var_0 + "_level" );
}

set_upgrade_level( var_0, var_1 )
{
    self setcoopplayerdata( "alienSession", var_0 + "_level", var_1 );
    var_2 = "LoadoutPlayer" + self getentitynumber();
    setcoopplayerdata_for_everyone( var_2, var_0 + "_level", var_1 );
}

set_dpad_up_level( var_0 )
{
    self setcoopplayerdata( "alienSession", "munition_level", var_0 );
}

set_dpad_down_level( var_0 )
{
    self setcoopplayerdata( "alienSession", "support_level", var_0 );
}

set_dpad_left_level( var_0 )
{
    self setcoopplayerdata( "alienSession", "defense_level", var_0 );
}

set_dpad_right_level( var_0 )
{
    self setcoopplayerdata( "alienSession", "offense_level", var_0 );
}

init_combat_resource_level()
{
    set_dpad_up_level( 0 );
    set_dpad_down_level( 0 );
    set_dpad_left_level( 0 );
    set_dpad_right_level( 0 );
}

get_player_currency()
{
    return self getcoopplayerdata( "alienSession", "currency" );
}

get_player_max_currency()
{
    return self.maxcurrency;
}

wait_to_set_player_currency( var_0 )
{
    self endon( "disconnect" );
    level endon( "game_ended" );
    wait 0.5;

    if ( is_upgrade_enabled( "more_cash_upgrade" ) )
        set_player_currency( 1500 );
    else
        set_player_currency( var_0 );
}

set_player_currency( var_0 )
{
    self setcoopplayerdata( "alienSession", "currency", int( var_0 ) );
    eog_player_update_stat( "currency", int( var_0 ), 1 );
}

get_player_points()
{
    return self getcoopplayerdata( "alienSession", "skill_points" );
}

set_player_points( var_0 )
{
    self setcoopplayerdata( "alienSession", "skill_points", int( var_0 ) );
}

set_player_max_currency( var_0 )
{
    var_0 = int( var_0 );
    self setclientomnvar( "ui_alien_max_currency", var_0 );
    self.maxcurrency = var_0;
}

give_player_currency( var_0, var_1, var_2, var_3 )
{
    if ( !maps\mp\alien\_utility::is_true( var_3 ) )
    {
        var_0 = int( var_0 * maps\mp\alien\_prestige::prestige_getmoneyearnedscalar() );
        var_0 = maps\mp\alien\_gamescore::round_up_to_nearest( var_0, 5 );
    }

    if ( maps\mp\alien\_utility::is_chaos_mode() && maps\mp\alien\_utility::is_true( self.chaosinthemoney ) )
    {
        var_0 = int( var_0 * 1.5 );
        var_0 = maps\mp\alien\_gamescore::round_up_to_nearest( var_0, 5 );
    }

    var_4 = get_player_currency();
    var_5 = get_player_max_currency();
    var_6 = var_4 + var_0;
    var_6 = min( var_6, var_5 );

    if ( !isdefined( self.total_currency_earned ) )
        self.total_currency_earned = var_0;
    else
        self.total_currency_earned = self.total_currency_earned + ( var_6 - var_4 );

    eog_player_update_stat( "currencytotal", int( self.total_currency_earned ), 1 );
    self notify( "loot_pickup" );
    set_player_currency( var_6 );
    var_7 = 30000;
    var_8 = gettime();

    if ( var_4 == var_5 )
        self setclientomnvar( "ui_alien_cash_overflow", 1 );

    if ( var_6 >= var_5 )
    {
        if ( !isdefined( self.next_maxmoney_hint_time ) )
            self.next_maxmoney_hint_time = var_8 + var_7;
        else if ( var_8 < self.next_maxmoney_hint_time )
            return;

        if ( !level.gameended )
        {
            maps\mp\_utility::setlowermessage( "maxmoney", &"ALIEN_COLLECTIBLES_MONEY_MAX", 4 );
            self.next_maxmoney_hint_time = var_8 + var_7;
        }
    }
}

take_player_currency( var_0, var_1, var_2, var_3 )
{
    var_4 = get_player_currency();
    var_5 = max( 0, var_4 - var_0 );
    var_6 = int( var_4 - var_5 );
    maps\mp\alien\_chaos::update_spending_currency_event( self, var_2, var_3 );
    self notify( "loot_removed" );
    set_player_currency( var_5 );

    if ( isdefined( var_2 ) )
        maps\mp\alien\_alien_matchdata::update_spending_type( var_6, var_2 );

    if ( isdefined( var_1 ) && var_1 )
        return;

    eog_player_update_stat( "currencyspent", var_6 );

    if ( var_6 < 1 )
        return;

    maps\mp\alien\_challenge::update_challenge( "spend_10k", var_0 );
    maps\mp\alien\_challenge::update_challenge( "spend_20k", var_0 );
    maps\mp\alien\_challenge::update_challenge( "spend_no_money" );
}

take_all_currency()
{
    set_player_currency( 0 );
}

player_has_enough_currency( var_0 )
{
    var_1 = get_player_currency();
    return var_1 >= var_0;
}

try_take_player_currency( var_0 )
{
    if ( player_has_enough_currency( var_0 ) )
    {
        take_player_currency( var_0 );
        return 1;
    }
    else
        return 0;
}

give_player_points( var_0 )
{
    var_1 = get_player_points();
    var_2 = var_1 + var_0;
    var_2 = min( var_2, 255 );
    set_player_points( var_2 );
}

take_player_points( var_0 )
{
    var_1 = get_player_points();
    var_2 = max( 0, var_1 - var_0 );
    self notify( "points_removed" );
    set_player_points( var_2 );
}

player_has_enough_points( var_0 )
{
    var_1 = get_player_points();
    return var_1 >= var_0;
}

try_take_player_points( var_0 )
{
    if ( player_has_enough_points( var_0 ) )
    {
        take_player_points( var_0 );
        return 1;
    }
    else
        return 0;
}

is_unlocked( var_0 )
{
    var_1 = undefined;
    var_1 = strtok( var_0, "_" )[0];
    var_2 = level.combat_resource[var_0].unlock;
    var_3 = get_player_rank();
    return var_3 >= var_2;
}

player_persistence_init()
{
    level.alien_xp = 1;
    init_combat_resource_level();
    init_perk_level();
    maps\mp\alien\_perk_utility::init_each_perk();
    give_initial_perks();
    var_0 = get_starting_currency();
    thread wait_to_set_player_currency( var_0 );
    set_player_max_currency( 6000 * maps\mp\alien\_prestige::prestige_getwalletsizescalar() );
    var_1 = get_starting_skill_point();
    set_player_points( var_1 );
    set_player_session_xp( 0 );
    set_player_session_rankup( 0 );
    self setrank( get_player_rank(), get_player_prestige() );
    session_stats_init();
    var_2 = maps\mp\alien\_prestige::get_num_nerf_selected();
    lb_player_update_stat( "prestigenerfs", var_2, 1 );
    thread update_loadout_for_everyone();
}

get_starting_currency()
{
    return 500;
}

get_starting_skill_point()
{
    if ( maps\mp\alien\_utility::is_chaos_mode() )
        return 50;

    if ( is_upgrade_enabled( "free_skill_points_upgrade" ) )
        return 1;
    else
        return 0;
}

update_loadout_for_everyone()
{
    level notify( "updating_player_loadout_data" );
    level endon( "updating_player_loadout_data" );

    if ( !isdefined( level.players ) || level.players.size == 0 )
        wait 0.5;

    wait 1;

    foreach ( var_1 in level.players )
    {
        var_2 = "LoadoutPlayer" + var_1 getentitynumber();
        var_3 = var_1 get_selected_perk_0();
        var_4 = var_1 get_selected_perk_1();
        var_5 = var_1 get_selected_dpad_up();
        var_6 = var_1 get_selected_dpad_down();
        var_7 = var_1 get_selected_dpad_left();
        var_8 = var_1 get_selected_dpad_right();
        var_9 = var_1 get_perk_0_level();
        var_10 = var_1 get_perk_1_level();
        var_11 = var_1 get_dpad_up_level();
        var_12 = var_1 get_dpad_down_level();
        var_13 = var_1 get_dpad_left_level();
        var_14 = var_1 get_dpad_right_level();

        foreach ( var_16 in level.players )
        {
            var_16 setcoopplayerdata( var_2, "perks", 0, var_3 );
            var_16 setcoopplayerdata( var_2, "perks", 1, var_4 );
            var_16 setcoopplayerdata( var_2, "munition", var_5 );
            var_16 setcoopplayerdata( var_2, "support", var_6 );
            var_16 setcoopplayerdata( var_2, "defense", var_7 );
            var_16 setcoopplayerdata( var_2, "offense", var_8 );
            var_16 setcoopplayerdata( var_2, "perk_0_level", var_9 );
            var_16 setcoopplayerdata( var_2, "perk_1_level", var_10 );
            var_16 setcoopplayerdata( var_2, "munition_level", var_11 );
            var_16 setcoopplayerdata( var_2, "support_level", var_12 );
            var_16 setcoopplayerdata( var_2, "defense_level", var_13 );
            var_16 setcoopplayerdata( var_2, "offense_level", var_14 );
        }

        if ( !isdefined( var_1.bbprint_loadout ) )
        {
            var_1.bbprint_loadout = 1;
            var_18 = "";

            if ( isdefined( self.name ) )
                var_18 = self.name;

            var_19 = get_player_rank();
            var_20 = get_player_xp();
            var_21 = get_player_prestige();
            var_22 = get_player_escaped();
            var_23 = gettime();
            var_24 = 0;

            if ( isdefined( level.current_cycle_num ) )
                var_24 = level.current_cycle_num;

            bbprint( "alienplayerloadout", "playername %s playerrank %i playerxp %i playerprestige %i playerescaped %i connecttime %i cyclenum %i perk0 %s perk1 %s dpadup %s dpaddown %s dpadleft %s dpadright %s ", var_18, var_19, var_20, var_21, var_22, var_23, var_24, var_3, var_4, var_5, var_6, var_7, var_8 );
        }
    }
}

setcoopplayerdata_for_everyone( var_0, var_1, var_2, var_3, var_4 )
{
    foreach ( var_6 in level.players )
    {
        if ( isdefined( var_0 ) && isdefined( var_1 ) && isdefined( var_2 ) && isdefined( var_3 ) && isdefined( var_4 ) )
        {
            var_6 setcoopplayerdata( var_0, var_1, var_2, var_3, var_4 );
            continue;
        }

        if ( isdefined( var_0 ) && isdefined( var_1 ) && isdefined( var_2 ) && isdefined( var_3 ) && !isdefined( var_4 ) )
        {
            var_6 setcoopplayerdata( var_0, var_1, var_2, var_3 );
            continue;
        }

        if ( isdefined( var_0 ) && isdefined( var_1 ) && isdefined( var_2 ) && !isdefined( var_3 ) && !isdefined( var_4 ) )
        {
            var_6 setcoopplayerdata( var_0, var_1, var_2 );
            continue;
        }

        if ( isdefined( var_0 ) && isdefined( var_1 ) && !isdefined( var_2 ) && !isdefined( var_3 ) && !isdefined( var_4 ) )
        {
            var_6 setcoopplayerdata( var_0, var_1 );
            continue;
        }
    }
}

get_token_bonus_flag( var_0 )
{
    var_1 = 4;

    if ( var_0 < 28 )
        var_1 = var_1 + var_0;

    return var_1;
}

is_valid_bonus_flag( var_0 )
{
    return var_0 >= 4 && var_0 < 32;
}

wait_then_try_give_one_shot_token_bonus( var_0, var_1 )
{
    self endon( "disconnect" );
    level endon( "game_ended" );

    if ( !is_valid_bonus_flag( var_0 ) )
        return;

    level waittill( "regular_hive_destroyed" );
    try_give_one_shot_token_bonus( var_0, var_1 );
}

try_give_one_shot_token_bonus( var_0, var_1 )
{
    if ( !is_using_extinction_tokens() )
        return;

    if ( !level.onlinegame )
        return;

    if ( !is_valid_bonus_flag( var_0 ) )
        return;

    var_2 = self getcoopplayerdatareservedint( "eggstra_award_flags" );
    var_3 = var_2 >> var_0 & 1;

    if ( var_3 == 1 )
        return;

    var_2 = var_2 | 1 << var_0;
    self setcoopplayerdatareservedint( "eggstra_award_flags", var_2 );
    give_player_tokens( var_1, 1 );
}

is_using_extinction_tokens()
{
    if ( getdvarint( "extinction_tokens_enabled" ) > 0 )
        return 1;

    return 0;
}

give_player_tokens( var_0, var_1 )
{
    if ( !is_using_extinction_tokens() )
        return;

    if ( maps\mp\alien\_utility::is_true( var_1 ) )
        self setclientomnvar( "ui_alien_award_token", var_0 );

    var_2 = self getcoopplayerdatareservedint( "extinction_tokens" );
    self setcoopplayerdatareservedint( "extinction_tokens", var_2 + var_0 );
    give_player_session_tokens( var_0 );
}

try_award_bonus_pool_token()
{
    if ( !is_using_extinction_tokens() )
        return;

    if ( !level.onlinegame )
        return;

    if ( isdefined( self.pers["hasEarnedBonusToken"] ) )
        return;

    self.pers["hasEarnedBonusToken"] = 1;
    var_0 = self getcoopplayerdatareservedint( "bonus_pool_size" );

    if ( var_0 > 0 )
    {
        self setcoopplayerdatareservedint( "bonus_pool_size", var_0 - 1 );
        give_player_tokens( 1, 1 );
    }
}

award_rank_up_tokens( var_0 )
{
    var_1 = get_player_prestige();

    if ( var_0 < level.alien_ranks.size && var_1 == 0 )
    {
        var_2 = level.alien_ranks[var_0].tokenreward;

        if ( var_2 > 0 )
            give_player_tokens( var_2, 1 );
    }
}

award_completion_tokens()
{
    if ( maps\mp\alien\_utility::is_casual_mode() )
        return;

    var_0 = get_scaled_xp( 5 );

    if ( maps\mp\alien\_utility::is_hardcore_mode() )
        var_0 = var_0 + 3;

    if ( level.all_challenge_completed )
        var_0 = var_0 + 4;

    var_1 = maps\mp\alien\_prestige::get_num_nerf_selected();

    if ( var_1 > 0 && !maps\mp\alien\_utility::is_true( self.pers["hotjoined"] ) )
    {
        if ( var_1 == 1 )
            var_0 = var_0 + 1;
        else if ( var_1 == 2 )
            var_0 = var_0 + 2;
        else if ( var_1 >= 3 )
            var_0 = var_0 + 3;
    }

    if ( 1 < get_player_escaped() )
    {
        foreach ( var_3 in level.players )
        {
            if ( var_3 != self && 1 == var_3 get_player_escaped() )
                var_0++;
        }
    }

    give_player_tokens( var_0 );
}

is_upgrade_enabled( var_0 )
{
    if ( maps\mp\alien\_utility::is_chaos_mode() )
        return 0;

    if ( !is_using_extinction_tokens() )
        return 0;

    if ( self _meth_842B( var_0 ) )
        return 1;
    else
        return 0;
}

is_upgrade_purchased( var_0 )
{
    if ( !is_using_extinction_tokens() )
        return 0;

    if ( self alienscheckisitempurchased( var_0 ) )
        return 1;
    else
        return 0;
}

session_stats_init()
{
    self setcoopplayerdata( "alienSession", "team_shots", 0 );
    self setcoopplayerdata( "alienSession", "team_kills", 0 );
    self setcoopplayerdata( "alienSession", "team_hives", 0 );
    self setcoopplayerdata( "alienSession", "downed", 0 );
    self setcoopplayerdata( "alienSession", "hivesDestroyed", 0 );
    self setcoopplayerdata( "alienSession", "prestigenerfs", 0 );
    self setcoopplayerdata( "alienSession", "repairs", 0 );
    self setcoopplayerdata( "alienSession", "drillPlants", 0 );
    self setcoopplayerdata( "alienSession", "deployables", 0 );
    self setcoopplayerdata( "alienSession", "challengesCompleted", 0 );
    self setcoopplayerdata( "alienSession", "challengesAttempted", 0 );
    self setcoopplayerdata( "alienSession", "trapKills", 0 );
    self setcoopplayerdata( "alienSession", "currencyTotal", 0 );
    self setcoopplayerdata( "alienSession", "currencySpent", 0 );
    self setcoopplayerdata( "alienSession", "escapedRank0", 0 );
    self setcoopplayerdata( "alienSession", "escapedRank1", 0 );
    self setcoopplayerdata( "alienSession", "escapedRank2", 0 );
    self setcoopplayerdata( "alienSession", "escapedRank3", 0 );
    self setcoopplayerdata( "alienSession", "kills", 0 );
    self setcoopplayerdata( "alienSession", "revives", 0 );
    self setcoopplayerdata( "alienSession", "time", 0 );
    self setcoopplayerdata( "alienSession", "score", 0 );
    self setcoopplayerdata( "alienSession", "shots", 0 );
    self setcoopplayerdata( "alienSession", "last_stand_count", 0 );
    self setcoopplayerdata( "alienSession", "deaths", 0 );
    self setcoopplayerdata( "alienSession", "headShots", 0 );
    self setcoopplayerdata( "alienSession", "hits", 0 );
    thread weapons_tracking_init();
    thread resource_tracking_init();
    thread eog_player_tracking_init();
}

eog_player_tracking_init()
{
    wait 0.5;
    var_0 = get_player_ref();

    foreach ( var_2 in level.players )
    {
        var_2 reset_eog_stats( var_0 );
        var_3 = "unknownPlayer";

        if ( isdefined( self.name ) )
            var_3 = self.name;

        if ( !level.console )
            var_3 = getsubstr( var_3, 0, 19 );
        else if ( have_clan_tag( var_3 ) )
            var_3 = remove_clan_tag( var_3 );

        var_2 setcoopplayerdata( var_0, "name", var_3 );
    }

    var_5 = [ 0, 0, 0, 0 ];

    foreach ( var_7 in level.players )
    {
        var_5[int( var_7 getentitynumber() )] = 1;

        if ( var_7 == self )
            continue;

        var_0 = var_7 get_player_ref();
        var_8 = var_7 getcoopplayerdata( var_0, "name" );
        var_9 = var_7 getcoopplayerdata( var_0, "kills" );
        var_10 = var_7 getcoopplayerdata( var_0, "score" );
        var_11 = var_7 getcoopplayerdata( var_0, "assists" );
        var_12 = var_7 getcoopplayerdata( var_0, "revives" );
        var_13 = var_7 getcoopplayerdata( var_0, "drillrestarts" );
        var_14 = var_7 getcoopplayerdata( var_0, "drillplants" );
        var_15 = var_7 getcoopplayerdata( var_0, "downs" );
        var_16 = var_7 getcoopplayerdata( var_0, "deaths" );
        var_17 = var_7 getcoopplayerdata( var_0, "hivesdestroyed" );
        var_18 = var_7 getcoopplayerdata( var_0, "currency" );
        var_19 = var_7 getcoopplayerdata( var_0, "currencyspent" );
        var_20 = var_7 getcoopplayerdata( var_0, "currencytotal" );
        var_21 = var_7 getcoopplayerdata( var_0, "traps" );
        var_22 = var_7 getcoopplayerdata( var_0, "deployables" );
        var_23 = var_7 getcoopplayerdata( var_0, "deployablesused" );
        self setcoopplayerdata( var_0, "name", var_8 );
        self setcoopplayerdata( var_0, "kills", var_9 );
        self setcoopplayerdata( var_0, "score", var_10 );
        self setcoopplayerdata( var_0, "assists", var_11 );
        self setcoopplayerdata( var_0, "revives", var_12 );
        self setcoopplayerdata( var_0, "drillrestarts", var_13 );
        self setcoopplayerdata( var_0, "drillplants", var_14 );
        self setcoopplayerdata( var_0, "downs", var_15 );
        self setcoopplayerdata( var_0, "deaths", var_16 );
        self setcoopplayerdata( var_0, "hivesdestroyed", var_17 );
        self setcoopplayerdata( var_0, "currency", var_18 );
        self setcoopplayerdata( var_0, "currencyspent", var_19 );
        self setcoopplayerdata( var_0, "currencytotal", var_20 );
        self setcoopplayerdata( var_0, "traps", var_21 );
        self setcoopplayerdata( var_0, "deployables", var_22 );
        self setcoopplayerdata( var_0, "deployablesused", var_23 );
    }

    foreach ( var_27, var_26 in var_5 )
    {
        if ( !var_26 )
            reset_eog_stats( "EoGPlayer" + var_27 );
    }
}

reset_eog_stats( var_0 )
{
    self setcoopplayerdata( var_0, "name", "" );
    self setcoopplayerdata( var_0, "kills", 0 );
    self setcoopplayerdata( var_0, "score", 0 );
    self setcoopplayerdata( var_0, "assists", 0 );
    self setcoopplayerdata( var_0, "revives", 0 );
    self setcoopplayerdata( var_0, "drillrestarts", 0 );
    self setcoopplayerdata( var_0, "drillplants", 0 );
    self setcoopplayerdata( var_0, "downs", 0 );
    self setcoopplayerdata( var_0, "deaths", 0 );
    self setcoopplayerdata( var_0, "hivesdestroyed", 0 );
    self setcoopplayerdata( var_0, "currency", 0 );
    self setcoopplayerdata( var_0, "currencyspent", 0 );
    self setcoopplayerdata( var_0, "currencytotal", 0 );
    self setcoopplayerdata( var_0, "traps", 0 );
    self setcoopplayerdata( var_0, "deployables", 0 );
    self setcoopplayerdata( var_0, "deployablesused", 0 );
}

eog_player_update_stat( var_0, var_1, var_2 )
{
    var_3 = get_player_ref();
    var_4 = var_1;

    if ( !isdefined( var_2 ) || !var_2 )
    {
        var_5 = self getcoopplayerdata( var_3, var_0 );
        var_4 = int( var_5 ) + int( var_1 );
    }

    try_update_lb_playerdata( var_0, var_4, 1 );
    setcoopplayerdata_for_everyone( var_3, var_0, var_4 );
}

try_update_lb_playerdata( var_0, var_1, var_2 )
{
    var_3 = get_mapped_lb_ref_from_eog_ref( var_0 );

    if ( !isdefined( var_3 ) )
        return;

    lb_player_update_stat( var_3, var_1, var_2 );
}

lb_player_update_stat( var_0, var_1, var_2 )
{
    if ( maps\mp\alien\_utility::is_true( var_2 ) )
        var_3 = var_1;
    else
    {
        var_4 = self getcoopplayerdata( "alienSession", var_0 );
        var_3 = var_4 + var_1;
    }

    self setcoopplayerdata( "alienSession", var_0, var_3 );
}

weapons_tracking_init()
{
    self.persistence_weaponstats = [];

    foreach ( var_3, var_1 in level.collectibles )
    {
        if ( strtok( var_3, "_" )[0] == "weapon" )
        {
            var_2 = get_base_weapon_name( var_3 );
            self.persistence_weaponstats[var_2] = 1;
        }
    }

    thread player_weaponstats_track_shots();
}

get_base_weapon_name( var_0 )
{
    var_1 = "";
    var_2 = strtok( var_0, "_" );

    for ( var_3 = 0; var_3 < var_2.size; var_3++ )
    {
        var_4 = var_2[var_3];

        if ( var_4 == "weapon" && var_3 == 0 )
            continue;

        if ( var_4 == "mp" )
        {
            var_1 = var_1 + "mp";
            break;
        }

        if ( var_3 < var_2.size - 1 )
        {
            var_1 = var_1 + ( var_4 + "_" );
            continue;
        }

        var_1 = var_1 + var_4;
        break;
    }

    if ( var_1 == "" )
        return "none";

    return var_1;
}

weaponstats_reset( var_0, var_1 )
{
    self setcoopplayerdata( var_0, var_1, "hits", 0 );
    self setcoopplayerdata( var_0, var_1, "shots", 0 );
    self setcoopplayerdata( var_0, var_1, "kills", 0 );
}

update_weaponstats_hits( var_0, var_1, var_2 )
{
    if ( !is_valid_weapon_hit( var_0, var_2 ) )
        return;

    update_weaponstats( "weaponStats", var_0, "hits", var_1 );
    maps\mp\alien\_gamescore::update_personal_encounter_performance( maps\mp\alien\_gamescore::get_personal_score_component_name(), "shots_hit", var_1 );
}

is_valid_weapon_hit( var_0, var_1 )
{
    if ( var_0 == "none" )
        return 0;

    if ( var_1 == "MOD_MELEE" )
        return 0;

    if ( no_weapon_fired_notify( var_0 ) )
        return 0;

    return 1;
}

no_weapon_fired_notify( var_0 )
{
    switch ( var_0 )
    {
        case "alientank_rigger_turret_mp":
        case "alientank_turret_mp":
        case "turret_minigun_alien_grenade":
        case "turret_minigun_alien_railgun":
        case "alienvulture_mp":
        case "alien_ball_drone_gun4_mp":
        case "alien_ball_drone_gun3_mp":
        case "alien_ball_drone_gun2_mp":
        case "alien_ball_drone_gun1_mp":
        case "alien_ball_drone_gun_mp":
        case "alienims_projectile_mp":
        case "aliensoflam_missle_mp":
        case "alien_sentry_minigun_4_mp":
        case "alien_sentry_minigun_3_mp":
        case "alien_sentry_minigun_2_mp":
        case "alien_sentry_minigun_1_mp":
        case "alienmortar_strike_mp":
        case "switchblade_rocket_mp":
        case "alien_manned_minigun_turret4_mp":
        case "alien_manned_minigun_turret3_mp":
        case "alien_manned_minigun_turret2_mp":
        case "alien_manned_minigun_turret1_mp":
        case "alien_manned_minigun_turret_mp":
        case "alien_manned_gl_turret4_mp":
        case "alien_manned_gl_turret3_mp":
        case "alien_manned_gl_turret2_mp":
        case "alien_manned_gl_turret1_mp":
        case "alien_manned_gl_turret_mp":
        case "turret_minigun_alien":
        case "alienmortar_shell_mp":
        case "alienclaymore_mp":
        case "aliensemtex_mp":
        case "alienbetty_mp":
        case "alienthrowingknife_mp":
            return 1;
        default:
            return 0;
    }
}

update_weaponstats_shots( var_0, var_1 )
{
    if ( !self.should_track_weapon_fired )
        return;

    level.alienbbdata["bullets_shot"] = level.alienbbdata["bullets_shot"] + var_1;
    update_weaponstats( "weaponStats", var_0, "shots", var_1 );
    maps\mp\alien\_gamescore::update_personal_encounter_performance( maps\mp\alien\_gamescore::get_personal_score_component_name(), "shots_fired", var_1 );
}

update_weaponstats_kills( var_0, var_1 )
{
    update_weaponstats( "weaponStats", var_0, "kills", var_1 );
}

update_weaponstats( var_0, var_1, var_2, var_3 )
{
    if ( !isplayer( self ) )
        return;

    var_4 = get_base_weapon_name( var_1 );

    if ( !isdefined( var_4 ) || !isdefined( self.persistence_weaponstats[var_4] ) )
        return;

    if ( isdefined( level.weapon_stats_override_name_func ) )
        var_4 = [[ level.weapon_stats_override_name_func ]]( var_4 );

    if ( issubstr( var_4, "dlc" ) )
    {
        var_5 = strtok( var_4, "d" );
        var_4 = var_5[0] + "DLC";
        var_5 = strtok( var_5[1], "c" );
        var_4 = var_4 + var_5[1];
    }

    var_6 = int( self getcoopplayerdata( var_0, var_4, var_2 ) );
    var_7 = var_6 + int( var_3 );
    self setcoopplayerdata( var_0, var_4, var_2, var_7 );
}

player_weaponstats_track_shots()
{
    self endon( "disconnect" );
    self notify( "weaponstats_track_shots" );
    self endon( "weaponstats_track_shots" );

    for (;;)
    {
        self waittill( "weapon_fired", var_0 );

        if ( !isdefined( var_0 ) )
            continue;

        var_1 = 1;
        update_weaponstats_shots( var_0, var_1 );
    }
}

resource_tracking_init()
{
    foreach ( var_1 in level.alien_combat_resources )
    {
        foreach ( var_4, var_3 in var_1 )
        {
            self setcoopplayerdata( "resourceStatsSession", var_4, "purchase", 0 );
            self setcoopplayerdata( "resourceStatsSession", var_4, "upgrade", 0 );
            self setcoopplayerdata( "resourceStatsSession", var_4, "used", 0 );
        }
    }

    foreach ( var_7 in level.alien_perks["perk_0"] )
    {
        self setcoopplayerdata( "resourceStatsSession", var_7.ref, "purchase", 0 );
        self setcoopplayerdata( "resourceStatsSession", var_7.ref, "upgrade", 0 );
    }

    foreach ( var_7 in level.alien_perks["perk_1"] )
    {
        self setcoopplayerdata( "resourceStatsSession", var_7.ref, "purchase", 0 );
        self setcoopplayerdata( "resourceStatsSession", var_7.ref, "upgrade", 0 );
    }
}

update_resource_stats( var_0, var_1, var_2 )
{
    if ( !isplayer( self ) )
        return;

    thread update_resource_stats_raw( "resourceStats", var_0, var_1, var_2 );
    thread update_resource_stats_raw( "resourceStatsSession", var_0, var_1, var_2 );
}

update_resource_stats_raw( var_0, var_1, var_2, var_3 )
{
    var_4 = int( self getcoopplayerdata( var_0, var_2, var_1 ) );
    var_5 = var_4 + int( var_3 );
    self setcoopplayerdata( var_0, var_2, var_1, var_5 );
}

rank_init()
{
    if ( !isdefined( level.alien_ranks_table ) )
        level.alien_ranks_table = "mp/alien/rankTable.csv";

    level.alien_ranks = [];
    level.alien_max_rank = int( tablelookup( level.alien_ranks_table, 0, "maxrank", 1 ) );

    for ( var_0 = 0; var_0 <= level.alien_max_rank; var_0++ )
    {
        var_1 = get_ref_by_id( var_0 );

        if ( var_1 == "" )
            break;

        if ( !isdefined( level.alien_ranks[var_0] ) )
        {
            var_2 = spawnstruct();
            var_2.id = var_0;
            var_2.ref = var_1;
            var_2.lvl = get_level_by_id( var_0 );
            var_2.icon = get_icon_by_id( var_0 );
            var_2.tokenreward = get_token_reward_by_id( var_0 );
            var_2.xp = [];
            var_2.xp["min"] = get_minxp_by_id( var_0 );
            var_2.xp["next"] = get_nextxp_by_id( var_0 );
            var_2.xp["max"] = get_maxxp_by_id( var_0 );
            var_2.name = [];
            var_2.name["short"] = get_shortrank_by_id( var_0 );
            var_2.name["full"] = get_fullrank_by_id( var_0 );
            var_2.name["ingame"] = get_ingamerank_by_id( var_0 );
            level.alien_ranks[var_0] = var_2;
        }
    }
}

get_ref_by_id( var_0 )
{
    return tablelookup( level.alien_ranks_table, 0, var_0, 1 );
}

get_minxp_by_id( var_0 )
{
    return int( tablelookup( level.alien_ranks_table, 0, var_0, 2 ) );
}

get_maxxp_by_id( var_0 )
{
    return int( tablelookup( level.alien_ranks_table, 0, var_0, 7 ) );
}

get_nextxp_by_id( var_0 )
{
    return int( tablelookup( level.alien_ranks_table, 0, var_0, 3 ) );
}

get_level_by_id( var_0 )
{
    return int( tablelookup( level.alien_ranks_table, 0, var_0, 13 ) );
}

get_shortrank_by_id( var_0 )
{
    return tablelookup( level.alien_ranks_table, 0, var_0, 4 );
}

get_fullrank_by_id( var_0 )
{
    return tablelookup( level.alien_ranks_table, 0, var_0, 5 );
}

get_ingamerank_by_id( var_0 )
{
    return tablelookup( level.alien_ranks_table, 0, var_0, 16 );
}

get_icon_by_id( var_0 )
{
    return tablelookup( level.alien_ranks_table, 0, var_0, 6 );
}

get_token_reward_by_id( var_0 )
{
    return int( tablelookup( level.alien_ranks_table, 0, var_0, 18 ) );
}

get_player_rank()
{
    return self getcoopplayerdata( "alienPlayerStats", "rank" );
}

get_player_xp()
{
    return self getcoopplayerdata( "alienPlayerStats", "experience" );
}

get_player_prestige()
{
    return self getcoopplayerdata( "alienPlayerStats", "prestige" );
}

get_player_escaped()
{
    return self getcoopplayerdata( "alienPlayerStats", "escaped" );
}

get_player_kills()
{
    return self getcoopplayerdata( "alienPlayerStats", "kills" );
}

get_player_revives()
{
    return self getcoopplayerdata( "alienPlayerStats", "revives" );
}

get_player_highest_nerf_escape_count()
{
    return self getcoopplayerdata( "alienPlayerStats", "headShots" );
}

get_player_session_xp()
{
    return self getcoopplayerdata( "alienSession", "experience" );
}

set_player_session_xp( var_0 )
{
    self setcoopplayerdata( "alienSession", "experience", var_0 );
}

give_player_session_xp( var_0 )
{
    var_1 = get_player_session_xp();
    var_2 = var_0 + var_1;
    set_player_session_xp( var_2 );
}

get_player_session_tokens()
{
    return self getcoopplayerdata( "alienSession", "shots" );
}

set_player_session_tokens( var_0 )
{
    self setcoopplayerdata( "alienSession", "shots", var_0 );
}

give_player_session_tokens( var_0 )
{
    var_1 = get_player_session_tokens();
    var_2 = var_0 + var_1;
    set_player_session_tokens( var_2 );
}

set_player_session_rankup( var_0 )
{
    self setcoopplayerdata( "alienSession", "ranked_up", int( var_0 ) );
}

get_player_session_rankup()
{
    return self getcoopplayerdata( "alienSession", "ranked_up" );
}

update_player_session_rankup( var_0 )
{
    if ( !isdefined( var_0 ) )
        var_0 = 1;

    var_1 = get_player_session_rankup();
    var_2 = var_0 + var_1;
    set_player_session_rankup( var_2 );
}

set_player_rank( var_0 )
{
    self setcoopplayerdata( "alienPlayerStats", "rank", var_0 );
}

set_player_xp( var_0 )
{
    self setcoopplayerdata( "alienPlayerStats", "experience", var_0 );
}

set_player_kills()
{
    var_0 = get_player_kills();
    var_1 = var_0 + 1;
    self setcoopplayerdata( "alienPlayerStats", "kills", var_1 );
}

set_player_revives()
{
    var_0 = get_player_revives();
    var_1 = var_0 + 1;
    self setcoopplayerdata( "alienPlayerStats", "revives", var_1 );
}

set_player_escaped()
{
    var_0 = get_player_escaped();
    var_1 = var_0 + 1;
    self setcoopplayerdata( "alienPlayerStats", "escaped", var_1 );
}

set_player_prestige( var_0 )
{
    self setcoopplayerdata( "alienPlayerStats", "prestige", var_0 );
    self setcoopplayerdata( "alienPlayerStats", "experience", 0 );
    self setcoopplayerdata( "alienPlayerStats", "rank", 0 );
}

get_rank_by_xp( var_0 )
{
    var_1 = 0;

    for ( var_2 = 0; var_2 < level.alien_ranks.size; var_2++ )
    {
        if ( var_0 >= level.alien_ranks[var_2].xp["min"] )
        {
            if ( var_0 < level.alien_ranks[var_2].xp["max"] )
            {
                var_1 = level.alien_ranks[var_2].id;
                break;
            }
        }
    }

    return var_1;
}

get_scaled_xp( var_0 )
{
    if ( maps\mp\alien\_utility::is_chaos_mode() )
        var_0 = int( 0.66 * var_0 );

    return int( var_0 * level.xpscale );
}

wait_and_give_player_xp( var_0, var_1 )
{
    self endon( "disconnect" );
    level endon( "game_ended" );
    wait( var_1 );
    give_player_xp( var_0 );
}

give_player_xp( var_0 )
{
    var_0 = get_scaled_xp( var_0 );

    if ( var_0 > 90000 )
        var_0 = 90000;

    thread give_player_session_xp( var_0 );
    var_1 = 0;
    var_2 = get_player_rank();
    var_3 = get_player_xp();
    var_4 = self getcoopplayerdata( "alienPlayerStats", "prestige" );

    if ( ( var_4 == 5 || var_4 == 10 || var_4 == 15 || var_4 == 20 ) && var_3 >= level.alien_ranks[29].xp["max"] )
    {
        var_3 = level.alien_ranks[29].xp["max"] - 1;
        var_2 = 29;
    }

    var_5 = var_3 + var_0;
    var_6 = 0;
    set_player_xp( var_5 );
    var_7 = get_rank_by_xp( var_5 );

    if ( var_7 > var_2 )
    {
        if ( var_7 == level.alien_max_rank )
        {
            var_8 = self getcoopplayerdata( "alienPlayerStats", "prestige" );
            var_6 = var_8;

            if ( var_8 < 25 )
            {
                set_player_prestige( var_8 + 1 );
                var_6 = var_8 + 1;
                give_player_tokens( 1, 1 );
            }
            else
            {
                var_1 = 1;
                set_player_rank( var_7 );
            }
        }
        else
            set_player_rank( var_7 );

        if ( var_1 == 0 )
        {
            if ( var_7 == 30 )
                var_9 = 1;
            else
                var_9 = var_7 + 1;

            self setclientomnvar( "ui_alien_rankup", var_9 );
            self notify( "ranked_up", var_7 );
            update_player_session_rankup();
            award_rank_up_tokens( var_2 );
        }

        self setrank( get_player_rank(), get_player_prestige() );
    }
}

inc_stat( var_0, var_1, var_2 )
{
    var_3 = self getcoopplayerdata( var_0, var_1 );
    var_4 = var_3 + var_2;
    self setcoopplayerdata( var_0, var_1, var_4 );
}

inc_session_stat( var_0, var_1 )
{
    inc_stat( "alienSession", var_0, var_1 );
}

get_hives_destroyed_stat()
{
    return get_aliensession_stat( "hivesDestroyed" );
}

get_aliensession_stat( var_0 )
{
    return self getcoopplayerdata( "alienSession", var_0 );
}

set_aliensession_stat( var_0, var_1 )
{
    self setcoopplayerdata( "alienSession", var_0, var_1 );
}

bbdata_init()
{
    level.alienbbdata = [];
    level.alienbbdata["times_downed"] = 0;
    level.alienbbdata["times_died"] = 0;
    level.alienbbdata["times_drill_stuck"] = 0;
    level.alienbbdata["aliens_killed"] = 0;
    level.alienbbdata["team_item_deployed"] = 0;
    level.alienbbdata["team_item_used"] = 0;
    level.alienbbdata["bullets_shot"] = 0;
    level.alienbbdata["damage_taken"] = 0;
    level.alienbbdata["damage_done"] = 0;
    level.alienbbdata["traps_used"] = 0;
    level notify( "BBData_initialized" );
}

deployablebox_used_track( var_0 )
{
    var_1 = var_0.boxtype;

    if ( isdefined( var_0.dpadname ) )
        var_1 = var_0.dpadname;

    thread update_resource_stats( "used", var_1, 1 );
    maps\mp\alien\_gamescore::update_personal_encounter_performance( maps\mp\alien\_gamescore::get_team_score_component_name(), "team_support_deploy" );
    level.alienbbdata["team_item_used"]++;
    eog_player_update_stat( "deployablesused", 1 );
    var_2 = "";

    if ( isdefined( self.name ) )
        var_2 = self.name;

    bbprint( "aliendeployableused", "itemname %s itemlevel %i itemx %f itemy %f itemz %f username %s ", var_0.boxtype, var_0.upgrade_rank, var_0.origin[0], var_0.origin[1], var_0.origin[2], var_2 );
}

get_player_ref()
{
    return "EoGPlayer" + self getentitynumber();
}

update_lb_aliensession_challenge( var_0 )
{
    foreach ( var_2 in level.players )
    {
        var_2 lb_player_update_stat( "challengesAttempted", 1 );

        if ( var_0 )
            var_2 lb_player_update_stat( "challengesCompleted", 1 );
    }
}

update_lb_aliensession_escape( var_0, var_1 )
{
    var_2 = get_lb_escape_rank( var_1 );

    foreach ( var_4 in var_0 )
    {
        var_4 lb_player_update_stat( "escapedRank" + var_2, 1, 1 );
        var_4 lb_player_update_stat( "hits", 1, 1 );
    }
}

update_alien_kill_sessionstats( var_0, var_1 )
{
    if ( !isdefined( var_1 ) || !isplayer( var_1 ) )
        return;

    if ( maps\mp\alien\_utility::is_trap( var_0 ) )
        var_1 lb_player_update_stat( "trapKills", 1 );
}

register_lb_escape_rank( var_0 )
{
    level.escape_rank_array = var_0;
}

get_lb_escape_rank( var_0 )
{
    for ( var_1 = 0; var_1 < level.escape_rank_array.size - 1; var_1++ )
    {
        if ( var_0 >= level.escape_rank_array[var_1] && var_0 < level.escape_rank_array[var_1 + 1] )
            return var_1;
    }
}

have_clan_tag( var_0 )
{
    return issubstr( var_0, "[" ) && issubstr( var_0, "]" );
}

remove_clan_tag( var_0 )
{
    var_1 = strtok( var_0, "]" );
    return var_1[1];
}

register_eog_to_lb_playerdata_mapping()
{
    var_0 = [];
    var_1["kills"] = "kills";
    var_1["deployables"] = "deployables";
    var_1["drillplants"] = "drillPlants";
    var_1["revives"] = "revives";
    var_1["downs"] = "downed";
    var_1["drillrestarts"] = "repairs";
    var_1["score"] = "score";
    var_1["currencyspent"] = "currencySpent";
    var_1["currencytotal"] = "currencyTotal";
    var_1["hivesdestroyed"] = "hivesDestroyed";
    level.eog_to_lb_playerdata_mapping = var_1;
}

get_mapped_lb_ref_from_eog_ref( var_0 )
{
    return level.eog_to_lb_playerdata_mapping[var_0];
}

play_time_monitor()
{
    self endon( "disconnect" );

    for (;;)
    {
        wait 1;
        lb_player_update_stat( "time", 1 );
    }
}
