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

start_game_type()
{
    init();
    set_is_private_match();
    override_gametype();
    register_upgrade_types();
    level thread wait_set_initial_player_count();
}

set_is_private_match()
{
    setmatchdata( "aliensIsPrivateMatch", getdvarint( "xblive_privatematch" ) );
}

override_gametype()
{
    setmatchdata( "gametype", get_alien_game_type() );
}

get_alien_game_type()
{
    var_0 = "aliens ch";
    var_1 = "aliens hc";
    var_2 = "aliens ca";
    var_3 = "aliens";

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

    if ( maps\mp\alien\_utility::is_hardcore_mode() )
        return var_1;
    else if ( maps\mp\alien\_utility::is_casual_mode() )
        return var_2;
    else
        return var_3;
}

init()
{
    var_0 = spawnstruct();
    var_1 = [];
    var_1["aliensTotalDrillDamage"] = get_single_value_struct( 0, "short" );
    var_0.single_value_stats = var_1;
    var_2 = [];
    var_0.challenge_results = var_2;
    level.alien_matchdata = var_0;
}

wait_set_initial_player_count()
{
    level endon( "gameEnded" );
    level waittill( "prematch_over" );
    setmatchdata( "aliensInitialPlayerCount", validate_byte( level.players.size ) );
}

on_player_connect()
{
    player_init();
    set_max_player_count();
    set_split_screen();
    set_alien_loadout();
    set_join_in_progress();
    set_relics_selected();
    set_upgrades_purchased();
    set_upgrades_enabled();
}

player_init()
{
    var_0 = spawnstruct();
    var_1 = [];
    var_1["aliensCashSpentOnWeapon"] = get_single_value_struct( 0, "int" );
    var_1["aliensCashSpentOnAbility"] = get_single_value_struct( 0, "int" );
    var_1["aliensCashSpentOnTrap"] = get_single_value_struct( 0, "int" );
    var_0.single_value_stats = var_1;
    var_2 = [];
    var_0.perk_upgraded = var_2;
    var_3 = [];
    var_3["aliensTimesDowned"] = [];
    var_3["aliensTimesRevived"] = [];
    var_3["aliensTimesBledOut"] = [];
    var_0.laststand_record = var_3;
    self.alien_matchdata = var_0;
}

set_max_player_count()
{
    if ( !isdefined( level.max_player_count ) )
        level.max_player_count = 0;

    if ( level.players.size + 1 > level.max_player_count )
    {
        level.max_player_count++;
        setmatchdata( "aliensMaxPlayerCount", validate_byte( level.max_player_count ) );
    }
}

set_split_screen()
{
    setmatchdata( "players", self.clientid, "isSplitscreen", self issplitscreenplayer() );
}

set_join_in_progress()
{
    if ( prematch_over() )
        setmatchdata( "players", self.clientid, "aliensJIP", 1 );
}

prematch_over()
{
    if ( isdefined( level.starttime ) )
        return 1;

    return 0;
}

set_alien_loadout()
{
    setmatchdata( "players", self.clientid, "aliensLoadOut", 0, maps\mp\alien\_persistence::get_selected_perk_0() );
    setmatchdata( "players", self.clientid, "aliensLoadOut", 1, maps\mp\alien\_persistence::get_selected_perk_1() );
    setmatchdata( "players", self.clientid, "aliensLoadOut", 2, maps\mp\alien\_persistence::get_selected_dpad_up() );
    setmatchdata( "players", self.clientid, "aliensLoadOut", 3, maps\mp\alien\_persistence::get_selected_dpad_down() );
    setmatchdata( "players", self.clientid, "aliensLoadOut", 4, maps\mp\alien\_persistence::get_selected_dpad_left() );
    setmatchdata( "players", self.clientid, "aliensLoadOut", 5, maps\mp\alien\_persistence::get_selected_dpad_right() );
}

set_relics_selected()
{
    var_0 = 0;

    foreach ( var_2 in level.nerf_list )
    {
        if ( self _meth_842C( var_2 ) )
        {
            setmatchdata( "players", self.clientid, "aliensRelics", var_0, var_2 );
            var_0++;
        }
    }

    for ( var_4 = var_0; var_4 < level.nerf_list.size; var_4++ )
        setmatchdata( "players", self.clientid, "aliensRelics", var_4, "none" );
}

set_upgrades_purchased()
{
    var_0 = 0;

    foreach ( var_2 in level.alien_upgrades )
    {
        if ( maps\mp\alien\_persistence::is_upgrade_purchased( var_2 ) )
        {
            setmatchdata( "players", self.clientid, "aliensUpgradePurchased", var_0, var_2 );
            var_0++;
        }
    }

    for ( var_4 = var_0; var_4 < 32; var_4++ )
        setmatchdata( "players", self.clientid, "aliensUpgradePurchased", var_4, "none" );
}

set_upgrades_enabled()
{
    var_0 = 0;

    foreach ( var_2 in level.alien_upgrades )
    {
        if ( maps\mp\alien\_persistence::is_upgrade_enabled( var_2 ) )
        {
            setmatchdata( "players", self.clientid, "aliensUpgradeEnabled", var_0, var_2 );
            var_0++;
        }
    }

    for ( var_4 = var_0; var_4 < 32; var_4++ )
        setmatchdata( "players", self.clientid, "aliensUpgradeEnabled", var_4, "none" );
}

inc_drill_heli_damages( var_0 )
{
    level.alien_matchdata.single_value_stats["aliensTotalDrillDamage"].value = level.alien_matchdata.single_value_stats["aliensTotalDrillDamage"].value + var_0;
}

set_escape_time_remaining( var_0 )
{
    setmatchdata( "aliensEscapeTimeRemaining", validate_int( var_0 ) );
}

update_challenges_status( var_0, var_1 )
{
    if ( level.alien_matchdata.challenge_results.size > 25 )
        return;

    var_2 = spawnstruct();
    var_2.challenge_name = var_0;
    var_2.result = var_1;
    level.alien_matchdata.challenge_results[level.alien_matchdata.challenge_results.size] = var_2;
}

record_perk_upgrade( var_0 )
{
    if ( self.alien_matchdata.perk_upgraded.size > 50 )
        return;

    self.alien_matchdata.perk_upgraded[self.alien_matchdata.perk_upgraded.size] = var_0;
}

inc_downed_counts()
{
    inc_laststand_record( "aliensTimesDowned" );
}

inc_revived_counts()
{
    inc_laststand_record( "aliensTimesRevived" );
}

inc_bleedout_counts()
{
    inc_laststand_record( "aliensTimesBledOut" );
}

inc_laststand_record( var_0 )
{
    if ( !isdefined( self.alien_matchdata.laststand_record[var_0][level.num_hive_destroyed] ) )
        self.alien_matchdata.laststand_record[var_0][level.num_hive_destroyed] = 0;

    self.alien_matchdata.laststand_record[var_0][level.num_hive_destroyed]++;
}

update_spending_type( var_0, var_1 )
{
    switch ( var_1 )
    {
        case "weapon":
            self.alien_matchdata.single_value_stats["aliensCashSpentOnWeapon"].value = self.alien_matchdata.single_value_stats["aliensCashSpentOnWeapon"].value + var_0;
            break;
        case "ability":
            self.alien_matchdata.single_value_stats["aliensCashSpentOnAbility"].value = self.alien_matchdata.single_value_stats["aliensCashSpentOnAbility"].value + var_0;
            break;
        case "trap":
            self.alien_matchdata.single_value_stats["aliensCashSpentOnTrap"].value = self.alien_matchdata.single_value_stats["aliensCashSpentOnTrap"].value + var_0;
            break;
        default:
            break;
    }
}

endgame( var_0, var_1 )
{
    set_game_data( var_0, var_1 );

    foreach ( var_3 in level.players )
        var_3 set_player_game_data();

    sendmatchdata();
}

set_game_data( var_0, var_1 )
{
    var_2 = "aliensChallengesCompleted";
    setmatchdata( "aliensFinalPlayerCount", validate_byte( level.players.size ) );
    setmatchdata( "aliensHivesDestroyed", validate_byte( level.num_hive_destroyed ) );
    setmatchdata( "aliensGameOverCondition", var_0 );
    setmatchdata( "aliensTotalTimeElapsed", validate_int( var_1 ) );
    var_3 = level.alien_matchdata;

    foreach ( var_7, var_5 in var_3.single_value_stats )
    {
        var_6 = validate_value( var_5.value, var_5.value_type );
        setmatchdata( var_7, var_6 );
    }

    foreach ( var_10, var_9 in var_3.challenge_results )
    {
        setmatchdata( var_2, var_10, "challengeId", var_9.challenge_name );
        setmatchdata( var_2, var_10, "success", var_9.result );
    }
}

set_player_game_data()
{
    copy_from_playerdata();
    set_perk_upgraded();
    set_laststand_stats();
    set_single_value_stats();
}

copy_from_playerdata()
{
    setmatchdata( "players", self.clientid, "aliensFinalScore", validate_int( self getcoopplayerdata( "alienSession", "score" ) ) );
    setmatchdata( "players", self.clientid, "aliensDrillRepairs", validate_byte( self getcoopplayerdata( "alienSession", "repairs" ) ) );
    setmatchdata( "players", self.clientid, "aliensXpEarned", validate_int( self getcoopplayerdata( "alienSession", "experience" ) ) );
}

set_perk_upgraded()
{
    foreach ( var_2, var_1 in self.alien_matchdata.perk_upgraded )
        setmatchdata( "players", self.clientid, "aliensPerksBought", var_2, var_1 );
}

set_laststand_stats()
{
    foreach ( var_5, var_1 in self.alien_matchdata.laststand_record )
    {
        foreach ( var_4, var_3 in var_1 )
            setmatchdata( "players", self.clientid, var_5, var_4, validate_byte( var_3 ) );
    }
}

set_single_value_stats()
{
    foreach ( var_3, var_1 in self.alien_matchdata.single_value_stats )
    {
        var_2 = validate_value( var_1.value, var_1.value_type );
        setmatchdata( "players", self.clientid, var_3, var_2 );
    }
}

validate_value( var_0, var_1 )
{
    switch ( var_1 )
    {
        case "byte":
            return validate_byte( var_0 );
        case "short":
            return validate_short( var_0 );
        case "int":
            return validate_int( var_0 );
        default:
    }
}

validate_byte( var_0 )
{
    return int( min( var_0, 127 ) );
}

validate_short( var_0 )
{
    return int( min( var_0, 32767 ) );
}

validate_int( var_0 )
{
    return int( min( var_0, 2147483647 ) );
}

get_single_value_struct( var_0, var_1 )
{
    var_2 = spawnstruct();
    var_2.value = var_0;
    var_2.value_type = var_1;
    return var_2;
}

register_upgrade_types()
{
    var_0 = "mp/alien/alien_purchasable_items.csv";
    var_1 = 0;
    var_2 = 1;
    var_3 = [];

    for ( var_4 = 0; var_4 < 32; var_4++ )
    {
        var_5 = tablelookup( var_0, var_1, var_4, var_2 );

        if ( maps\mp\agents\alien\_alien_agents::is_empty_string( var_5 ) )
            break;

        var_3[var_3.size] = var_5;
    }

    level.alien_upgrades = var_3;
}
