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

main()
{
    maps\mp\alien\_perks::init_perks();
    maps\mp\alien\_combat_resources::init_combat_resources();
}

player_setup()
{
    self setactionslot( 1, "" );
    self setactionslot( 2, "" );
    self setactionslot( 3, "" );
    self setactionslot( 4, "" );
    self notifyonplayercommand( "action_slot_1", "+actionslot 1" );
    self notifyonplayercommand( "action_slot_2", "+actionslot 2" );
    self notifyonplayercommand( "action_slot_3", "+actionslot 3" );
    self notifyonplayercommand( "action_slot_4", "+actionslot 4" );
    self notifyonplayercommand( "action_slot_1", "+actionslot 5" );
    self notifyonplayercommand( "action_slot_2", "+actionslot 6" );
    self notifyonplayercommand( "action_slot_3", "+actionslot 7" );
    self notifyonplayercommand( "action_use", "+attack" );
    self notifyonplayercommand( "action_use", "+attack_akimbo_accessible" );
    self notifyonplayercommand( "change_weapon", "weapnext" );
    thread player_watcher();
}

player_watcher()
{
    if ( can_use_munition() )
    {
        thread player_action_slot( level.alien_combat_resources["munition"][maps\mp\alien\_persistence::get_selected_dpad_up()], maps\mp\alien\_persistence::get_dpad_up_level, "action_slot_1" );
        thread player_action_slot( level.alien_combat_resources["support"][maps\mp\alien\_persistence::get_selected_dpad_down()], maps\mp\alien\_persistence::get_dpad_down_level, "action_slot_2" );
    }

    if ( can_use_ability() )
    {
        thread player_action_slot( level.alien_combat_resources["defense"][maps\mp\alien\_persistence::get_selected_dpad_left()], maps\mp\alien\_persistence::get_dpad_left_level, "action_slot_3" );
        thread player_action_slot( level.alien_combat_resources["offense"][maps\mp\alien\_persistence::get_selected_dpad_right()], maps\mp\alien\_persistence::get_dpad_right_level, "action_slot_4" );
    }

    thread player_watch_upgrade();

    if ( !maps\mp\alien\_utility::is_chaos_mode() )
        thread player_watch_currency_transfer();
}

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

    if ( maps\mp\alien\_prestige::prestige_getnodeployables() == 1.0 )
        return 0;

    return 1;
}

can_use_ability()
{
    if ( maps\mp\alien\_prestige::prestige_getnoabilities() == 1.0 )
        return 0;

    return 1;
}

player_watch_upgrade()
{
    thread player_watch_dpad_upgrade( level.alien_combat_resources["munition"][maps\mp\alien\_persistence::get_selected_dpad_up()] );
    thread player_watch_dpad_upgrade( level.alien_combat_resources["support"][maps\mp\alien\_persistence::get_selected_dpad_down()] );
    thread player_watch_dpad_upgrade( level.alien_combat_resources["defense"][maps\mp\alien\_persistence::get_selected_dpad_left()] );
    thread player_watch_dpad_upgrade( level.alien_combat_resources["offense"][maps\mp\alien\_persistence::get_selected_dpad_right()] );
    thread player_watch_perk_upgrade( level.alien_perks["perk_0"][maps\mp\alien\_persistence::get_selected_perk_0()], "perk_0" );
    thread player_watch_perk_upgrade( level.alien_perks["perk_1"][maps\mp\alien\_persistence::get_selected_perk_1()], "perk_1" );
}

player_watch_dpad_upgrade( var_0 )
{
    player_watch_upgrade_internal( var_0, var_0.type );
}

player_watch_perk_upgrade( var_0, var_1 )
{
    thread player_watch_upgrade_internal( var_0, var_1 );
    thread player_handle_perk_upgrades( var_0, var_1 );
}

player_watch_upgrade_internal( var_0, var_1 )
{
    self endon( "disconnect" );
    level endon( "game_ended" );
    var_2 = var_1 + "_try_upgrade";

    for (;;)
    {
        self waittillmatch( "luinotifyserver", var_2 );
        var_3 = maps\mp\alien\_persistence::get_upgrade_level( var_1 );

        if ( var_3 + 1 < var_0.upgrades.size && maps\mp\alien\_persistence::try_take_player_points( var_0.upgrades[var_3 + 1].point_cost ) )
        {
            var_3 = var_3 + 1;
            maps\mp\alien\_persistence::set_upgrade_level( var_1, var_3 );
            self notify( "upgrade_" + var_1 );
            thread maps\mp\alien\_persistence::update_resource_stats( "upgrade", var_0.ref, 1 );
            maps\mp\alien\_alien_matchdata::record_perk_upgrade( var_0.ref );
            var_4 = -1;

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

            var_5 = "unknown";

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

            var_6 = "unknown";

            if ( isdefined( level.current_hive_name ) )
                var_6 = level.current_hive_name;

            bbprint( "aliencombatresourceupgraded", "cyclenum %i hivename %s resource %s resourcelevel %s ownername %s ", var_4, var_6, var_0.ref, var_3, var_5 );
        }
    }
}

get_perk_ref_at_upgrade_level( var_0, var_1, var_2 )
{
    return level.alien_perks[var_0][var_1].upgrades[var_2].ref;
}

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

    for (;;)
    {
        self waittill( "upgrade_" + var_1 );
        var_2 = maps\mp\alien\_persistence::get_upgrade_level( var_1 );
        maps\mp\alien\_persistence::unset_perk( get_perk_ref_at_upgrade_level( var_1, var_0.ref, var_2 - 1 ) );
        maps\mp\alien\_persistence::set_perk( get_perk_ref_at_upgrade_level( var_1, var_0.ref, var_2 ) );
        var_3 = maps\mp\alien\_persistence::get_selected_perk_0_secondary();

        if ( var_1 == "perk_0" && var_3 != "perk_none" )
        {
            var_4 = level.alien_perks["perk_0"][var_3];
            maps\mp\alien\_persistence::unset_perk( get_perk_ref_at_upgrade_level( var_1, var_4.ref, var_2 - 1 ) );
            maps\mp\alien\_persistence::set_perk( get_perk_ref_at_upgrade_level( var_1, var_4.ref, var_2 ) );
        }
    }
}

restore_all_perks()
{
    var_0 = maps\mp\alien\_persistence::get_selected_perk_0();
    var_1 = maps\mp\alien\_persistence::get_perk_0_level();
    maps\mp\alien\_persistence::set_perk( get_perk_ref_at_upgrade_level( "perk_0", var_0, var_1 ) );
    var_2 = maps\mp\alien\_persistence::get_selected_perk_0_secondary();

    if ( var_2 != "perk_none" )
    {
        var_3 = level.alien_perks["perk_0"][var_2];
        maps\mp\alien\_persistence::set_perk( get_perk_ref_at_upgrade_level( "perk_0", var_3.ref, var_1 ) );
    }
}

player_cancel()
{
    player_cancel_internal();
    self enableweapons();
    self.alien_used_resource = undefined;
}

player_cancel_internal()
{
    self endon( "player_action_slot_restart" );
    self endon( "fired_ability_gun" );
    common_scripts\utility::waittill_any( "change_weapon", "action_slot_1", "action_slot_2", "action_slot_3", "action_slot_4", "last_stand", "dpad_cancel" );

    if ( maps\mp\alien\_utility::is_holding_deployable() && !self.is_holding_crate_marker )
    {
        if ( !isdefined( self.playerlinkedtochopper ) || !self.playerlinkedtochopper )
        {
            self.deployable = 0;
            self notify( "cancel_deployable_via_marker" );

            if ( !self hasweapon( "mortar_detonator_mp" ) && !self hasweapon( "switchblade_laptop_mp" ) )
                self notify( "player_action_slot_restart" );
        }
    }

    var_0 = self.alien_used_resource;
    var_1 = self.alien_used_resource_rank;
    self [[ var_0.callbacks.canceluse ]]( var_0, var_1 );
    self notify( "player_action_slot_restart" );
}

player_watch_use()
{
    self endon( "cancel_watch" );
    self waittill( "action_use" );
    return 1;
}

player_watch_riotshield_use( var_0, var_1 )
{
    self endon( "cancel_watch" );
    common_scripts\utility::_disableusability();
    thread reenable_usability( 3.1 );
    common_scripts\utility::waittill_any_timeout( 3, "action_use", "riotshield_block", "riotshield_melee" );

    if ( maps\mp\alien\_persistence::player_has_enough_currency( ceil( var_0.upgrades[var_1].cost ) ) )
        return 1;
    else
    {
        self [[ var_0.callbacks.canceluse ]]( var_0, var_1 );
        self notify( "action_finish_used" );
        self notify( "player_action_slot_restart" );
        return 0;
    }
}

player_watch_equalizer_use( var_0, var_1 )
{
    self endon( "cancel_watch" );
    common_scripts\utility::waittill_any_timeout( 3, "action_use" );

    if ( maps\mp\alien\_persistence::player_has_enough_currency( ceil( var_0.upgrades[var_1].cost ) ) )
        return 1;
    else
    {
        self [[ var_0.callbacks.canceluse ]]( var_0, var_1 );
        self notify( "action_finish_used" );
        self notify( "player_action_slot_restart" );
        return 0;
    }
}

player_watch_use_manned_turret()
{
    self endon( "cancel_watch" );

    for (;;)
    {
        self waittill( "action_use" );

        if ( isdefined( self.carriedsentry ) )
        {
            if ( maps\mp\alien\_utility::is_true( self.carriedsentry.canbeplaced ) )
                return 1;
        }
    }
}

player_watch_use_ims()
{
    self endon( "disconnect" );
    self endon( "cancel_watch" );
    self waittill( "IMS_placed" );
    return 1;
}

player_watch_use_sentry()
{
    self endon( "disconnect" );
    self endon( "cancel_watch" );

    for (;;)
    {
        self waittill( "action_use" );

        if ( isdefined( self.current_sentry ) )
        {
            var_0 = maps\mp\alien\_autosentry_alien::can_place_sentry( self.current_sentry );

            if ( var_0 )
                return 1;
        }

        wait 0.05;
    }
}

player_use( var_0, var_1 )
{
    self endon( "player_action_slot_restart" );
    self endon( "disconnect" );
    level endon( "game_ended" );
    thread player_cancel();
    self [[ var_0.callbacks.tryuse ]]( var_0, var_1 );

    if ( self [[ var_0.callbacks.canuse ]]( var_0 ) )
    {
        if ( !show_alternate_spend_hint( var_0 ) )
            thread maps\mp\alien\_hud::createspendhinthud( var_0, var_1 );
        else
            thread maps\mp\alien\_hud::createspendhinthud( var_0, var_1, &"ALIENS_PATCH_CANCEL_USE" );

        var_2 = wait_for_use( var_0, var_1 );

        if ( !isdefined( var_2 ) || !var_2 )
            return;

        if ( !maps\mp\alien\_persistence::player_has_enough_currency( ceil( var_0.upgrades[var_1].cost ) ) )
        {
            self notify( "dpad_cancel" );
            return;
        }

        self playlocalsound( "alien_killstreak_equip" );
        self thread [[ var_0.callbacks.use ]]( var_0, var_1 );
        thread maps\mp\alien\_persistence::update_resource_stats( "purchase", var_0.ref, 1 );
        maps\mp\alien\_persistence::take_player_currency( ceil( var_0.upgrades[var_1].cost ), 0, "ability" );
        self.alien_used_resource = undefined;
        var_3 = -1;

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

        var_4 = "unknown";

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

        var_5 = "unknown";

        if ( isdefined( level.current_hive_name ) )
            var_5 = level.current_hive_name;

        bbprint( "aliencombatresourceused", "cyclenum %i hivename %s resource %s resourcelevel %s ownername %s ", var_3, var_5, var_0.ref, var_1, var_4 );
        self notify( "action_finish_used" );
        self notify( "player_action_slot_restart" );
    }
}

show_alternate_spend_hint( var_0 )
{
    if ( maps\mp\alien\_utility::level_uses_maaws() && issubstr( var_0.ref, "predator" ) )
        return 1;

    return issubstr( var_0.ref, "turret" ) || issubstr( var_0.ref, "_ims" );
}

wait_for_use( var_0, var_1 )
{
    switch ( var_0.ref )
    {
        case "dpad_sentry":
            return player_watch_use_sentry();
        case "dpad_gl_sentry":
        case "dpad_minigun_turret":
            return player_watch_use_manned_turret();
        case "dpad_ims":
            return player_watch_use_ims();
        case "dpad_placeholder_ammo_2":
        case "dpad_team_ammo_ap":
        case "dpad_team_ammo_stun":
        case "dpad_team_ammo_explo":
        case "dpad_team_ammo_in":
        case "dpad_team_boost":
        case "dpad_team_adrenaline":
        case "dpad_team_randombox":
        case "dpad_team_ammo_reg":
        case "dpad_team_armor":
        case "dpad_team_explosives":
            return player_watch_box_thrown();
        case "dpad_riotshield":
            return player_watch_riotshield_use( var_0, var_1 );
        case "dpad_death_machine":
        case "dpad_war_machine":
            return player_watch_equalizer_use( var_0, var_1 );
        case "dpad_predator":
            if ( maps\mp\alien\_utility::level_uses_maaws() )
                return player_watch_equalizer_use( var_0, var_1 );
        default:
            return player_watch_use();
    }
}

player_watch_box_thrown()
{
    self endon( "cancel_watch" );

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

        if ( var_1 == "aliendeployable_crate_marker_mp" )
            return 1;
    }
}

player_action_slot_internal( var_0, var_1, var_2 )
{
    self endon( "player_action_slot_block" );
    self endon( "disconnect" );
    level endon( "game_ended" );

    for (;;)
    {
        self waittill( var_2 );

        if ( maps\mp\alien\_utility::is_true( self.player_action_disabled ) )
            continue;

        if ( self usebuttonpressed() )
            continue;

        if ( self isonladder() )
            continue;

        if ( maps\mp\alien\_utility::has_special_weapon() )
        {
            maps\mp\_utility::setlowermessage( "cant_buy", &"ALIEN_COLLECTIBLES_PLAYER_HAS_SPECIALWEAPON", 3 );
            continue;
        }

        if ( maps\mp\alien\_utility::is_true( self.picking_up_item ) )
            continue;

        if ( self isusingturret() )
            continue;

        if ( maps\mp\alien\_utility::is_true( self.hasprogressbar ) )
            continue;

        if ( maps\mp\alien\_utility::is_true( self.iscarrying ) )
            continue;

        if ( isdefined( self.throwinggrenade ) )
            continue;

        if ( common_scripts\utility::flag_exist( "escape_conditions_met" ) && common_scripts\utility::flag( "escape_conditions_met" ) )
            continue;

        var_3 = self [[ var_1 ]]();

        if ( !maps\mp\alien\_persistence::player_has_enough_currency( ceil( var_0.upgrades[var_3].cost ) ) )
            continue;

        common_scripts\utility::_disableusability();
        thread reenable_usability();

        if ( !isdefined( self.alien_used_resource ) )
        {
            if ( self [[ var_0.callbacks.canpurchase ]]( var_0, var_3 ) )
            {
                self.alien_used_resource = var_0;
                self.alien_used_resource_rank = var_3;
                thread player_use( var_0, var_3 );
                self notify( "player_action_slot_block" );
            }
        }
    }
}

reenable_usability( var_0 )
{
    self endon( "disconnect" );

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

    wait( var_0 );
    common_scripts\utility::_enableusability();
}

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

    for (;;)
    {
        thread player_action_slot_internal( var_0, var_1, var_2 );
        self waittill( "player_action_slot_restart" );
        wait 0.2;
    }
}

player_watch_currency_transfer()
{
    self endon( "disconnect" );
    level endon( "game_ended" );
    wait 0.05;

    if ( maps\mp\alien\_utility::isplayingsolo() )
        return;

    var_0 = 0;

    for (;;)
    {
        if ( is_drop_button_pressed() )
        {
            for ( var_1 = 2; is_drop_button_pressed() && var_1 >= 0; var_1 = var_1 - 0.05 )
                wait 0.05;

            if ( var_1 > 0 )
            {
                wait 0.05;
                continue;
            }

            if ( maps\mp\alien\_persistence::player_has_enough_currency( level.deployable_currency_ranks[var_0] ) )
            {
                deploy_currency( var_0 );
                level notify( "currency_dropped", self );
            }

            while ( is_drop_button_pressed() )
                wait 0.05;
        }

        wait 0.5;
    }
}

is_drop_button_pressed()
{
    if ( !isdefined( self ) || !isalive( self ) )
        return 0;

    if ( isdefined( self.laststand ) && self.laststand )
        return 0;

    var_0 = self adsbuttonpressed();
    var_1 = self jumpbuttonpressed();
    return var_0 && var_1;
}

deploy_currency( var_0 )
{
    self endon( "disconnect" );
    self notify( "deploying_currency" );
    maps\mp\alien\_persistence::take_player_currency( level.deployable_currency_ranks[var_0], 1 );
    var_1 = "deployable_currency";
    self.team_currency_rank = var_0;
    var_2 = level.boxsettings[var_1].maxuses;
    level.boxsettings[var_1].maxuses = 1;
    var_3 = maps\mp\alien\_deployablebox::createboxforplayer( var_1, self.origin, self );
    level.boxsettings[var_1].maxuses = var_2;
    var_3.upgrade_rank = var_0;
    var_3 playsoundtoplayer( level.boxsettings[var_1].deployedsfx, self );
    thread currency_box_think( var_3 );
}

currency_box_think( var_0 )
{
    self endon( "disconnect" );
    var_0 endon( "death" );
    wait 0.05;
    var_0 thread maps\mp\alien\_deployablebox::box_setactive( 1 );
    self waittill( "deploying_currency" );

    if ( isdefined( var_0 ) )
        var_0 maps\mp\alien\_deployablebox::box_leave();
}
