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

init_gamescore()
{
    register_scoring_mode();
}

init_eog_score_components( var_0 )
{
    level.eog_score_components = [];

    foreach ( var_2 in var_0 )
    {
        switch ( var_2 )
        {
            case "hive":
                register_eog_score_component( "hive", 8 );
                break;
            case "escape":
                register_eog_score_component( "escape", 9 );
                break;
            case "relics":
                register_eog_score_component( "relics", 5 );
                break;
            default:
        }
    }
}

init_encounter_score_components( var_0 )
{
    level.encounter_score_components = [];

    foreach ( var_2 in var_0 )
    {
        switch ( var_2 )
        {
            case "challenge":
                init_challenge_score_component();
                break;
            case "drill":
                init_drill_score_component();
                break;
            case "team":
                init_teamwork_score_component();
                break;
            case "team_blocker":
                init_blocker_hive_teamwork_score_component();
                break;
            case "personal":
                init_personal_score_component();
                break;
            case "personal_blocker":
                init_blocker_hive_personal_score_component();
                break;
            case "escape":
                init_escape_score_component();
                break;
            default:
        }
    }
}

init_player_score()
{
    if ( is_scoring_disabled() )
        return;

    self.encounter_performance = [];
    self.end_game_score = [];
    component_specific_init( self );
    reset_player_encounter_performance( self );
    reset_end_game_score();
}

reset_encounter_performance()
{
    foreach ( var_2, var_1 in level.encounter_score_components )
    {
        if ( isdefined( var_1.reset_team_performance_func ) )
            [[ var_1.reset_team_performance_func ]]( var_1 );
    }

    reset_players_encounter_performance_and_lua();
}

reset_players_encounter_performance_and_lua()
{
    foreach ( var_1 in level.players )
    {
        reset_player_encounter_performance( var_1 );
        maps\mp\alien\_hud::reset_player_encounter_lua_omnvars( var_1 );
    }
}

component_specific_init( var_0 )
{
    foreach ( var_3, var_2 in level.encounter_score_components )
    {
        if ( isdefined( var_2.player_init_func ) )
            [[ var_2.player_init_func ]]( var_0 );
    }
}

reset_player_encounter_performance( var_0 )
{
    foreach ( var_3, var_2 in level.encounter_score_components )
    {
        if ( isdefined( var_2.reset_player_performance_func ) )
            [[ var_2.reset_player_performance_func ]]( var_0 );
    }
}

reset_end_game_score()
{
    foreach ( var_2, var_1 in level.eog_score_components )
        self.end_game_score[var_2] = 0;
}

calculate_total_end_game_score( var_0 )
{
    var_1 = 1;
    var_2 = 0;

    foreach ( var_6, var_4 in level.eog_score_components )
    {
        var_5 = var_0.end_game_score[var_6];
        maps\mp\alien\_hud::set_lua_eog_score_row( var_0, var_1, var_4.lua_string_index, var_5 );
        var_1++;
        var_2 = var_2 + var_5;
    }

    maps\mp\alien\_hud::set_lua_eog_score_row( var_0, var_1, 10, var_2 );
}

calculate_players_total_end_game_score()
{
    if ( is_scoring_disabled() )
        return;

    if ( common_scripts\utility::flag_exist( "drill_drilling" ) && common_scripts\utility::flag( "drill_drilling" ) )
        calculate_encounter_scores( level.players, get_partial_hive_score_component_list() );

    foreach ( var_1 in level.players )
        calculate_total_end_game_score( var_1 );
}

get_partial_hive_score_component_list()
{
    if ( isdefined( level.partial_hive_score_component_list_func ) )
        return [[ level.partial_hive_score_component_list_func ]]();

    return [ "challenge", "team" ];
}

update_players_encounter_performance( var_0, var_1, var_2 )
{
    foreach ( var_4 in level.players )
        var_4 update_personal_encounter_performance( var_0, var_1, var_2 );
}

calculate_and_show_encounter_scores( var_0, var_1 )
{
    calculate_encounter_scores( var_0, var_1 );
    maps\mp\alien\_hud::show_encounter_scores();
}

calculate_encounter_scores( var_0, var_1 )
{
    foreach ( var_3 in var_0 )
        calculate_player_encounter_scores( var_3, var_1 );
}

calculate_player_encounter_scores( var_0, var_1 )
{
    var_2 = 1;
    var_3 = 0;

    foreach ( var_5 in var_1 )
    {
        var_6 = level.encounter_score_components[var_5];
        var_7 = [[ var_6.calculate_func ]]( var_0, var_6 );
        var_7 = var_7 * level.cycle_score_scalar;
        var_7 = int( var_7 );
        var_0.end_game_score[var_6.end_game_score_component_ref] = var_0.end_game_score[var_6.end_game_score_component_ref] + var_7;
        maps\mp\alien\_hud::set_lua_encounter_score_row( var_0, var_2, var_6.lua_string_index, var_7 );
        var_3 = var_3 + var_7;
        var_2++;
    }

    var_9 = var_0 maps\mp\alien\_prestige::get_num_nerf_selected();
    var_10 = int( var_3 * var_9 * 0.2 );
    var_0.end_game_score["relics"] = var_0.end_game_score["relics"] + var_10;
    maps\mp\alien\_hud::set_lua_encounter_score_row( var_0, var_2, 5, var_10 );
    var_2++;
    var_3 = var_3 + var_10;
    var_0 maps\mp\alien\_persistence::eog_player_update_stat( "score", var_3 );
    maps\mp\alien\_hud::set_lua_encounter_score_row( var_0, var_2, 6, var_3 );
    var_2++;
    var_11 = var_0 maps\mp\alien\_perk_utility::perk_getcurrencyscaleperhive();
    var_12 = var_0 maps\mp\alien\_prestige::prestige_getmoneyearnedscalar();
    var_13 = int( var_3 * var_11 * var_12 * 0.1 / level.cycle_score_scalar );
    var_13 = round_up_to_nearest( var_13, 10 );
    maps\mp\alien\_hud::set_lua_encounter_score_row( var_0, var_2, 7, var_13 );
    var_0.encounter_score_earned = var_3;
    var_0.encounter_cash_earned = var_13;
}

init_challenge_score_component()
{
    register_encounter_score_component( "challenge", ::init_challenge_score, undefined, ::reset_player_challenge_performance, ::calculate_challenge_score, 4, "hive" );
}

init_challenge_score( var_0 )
{
    if ( maps\mp\alien\_utility::isplayingsolo() )
        var_0.max_score = 1500;
    else
        var_0.max_score = 1000;

    return var_0;
}

reset_player_challenge_performance( var_0 )
{
    var_0.encounter_performance["challenge_complete"] = 0;
}

calculate_challenge_score( var_0, var_1 )
{
    return int( var_0.encounter_performance["challenge_complete"] * var_1.max_score );
}

get_challenge_score_component_name()
{
    return common_scripts\utility::ter_op( isdefined( level.challenge_score_component_name ), level.challenge_score_component_name, "challenge" );
}

set_challenge_score_component_name( var_0 )
{
    level.challenge_score_component_name = var_0;
}

init_drill_score_component()
{
    register_encounter_score_component( "drill", ::init_drill_score, ::reset_team_drill_performance, undefined, ::calculate_drill_protection_score, 1, "hive" );
}

init_drill_score( var_0 )
{
    if ( maps\mp\alien\_utility::isplayingsolo() )
    {
        var_0.max_score_damage_damage = 4500;
        var_0.max_drill_damage_limit = 1200;
    }
    else
    {
        var_0.max_score_damage_damage = 3500;
        var_0.max_drill_damage_limit = 750;
    }

    return var_0;
}

reset_team_drill_performance( var_0 )
{
    var_0.team_encounter_performance["drill_damage_taken"] = 0;
    return var_0;
}

calculate_drill_protection_score( var_0, var_1 )
{
    var_2 = get_team_encounter_performance( var_1, "drill_damage_taken" ) / var_1.max_drill_damage_limit;
    var_3 = max( 0, 1 - var_2 );
    var_4 = var_1.max_score_damage_damage;
    var_5 = var_4 * var_3;
    return int( var_5 );
}

get_drill_score_component_name()
{
    return common_scripts\utility::ter_op( isdefined( level.drill_score_component_name ), level.drill_score_component_name, "drill" );
}

set_drill_score_component_name( var_0 )
{
    level.drill_score_component_name = var_0;
}

init_teamwork_score_component()
{
    register_encounter_score_component( "team", ::init_teamwork_score, ::reset_team_score_performance, ::reset_player_teamwork_score_performance, ::calculate_teamwork_score, 2, "hive" );
}

init_teamwork_score( var_0 )
{
    var_0.max_score_deploy = 1000;
    var_0.max_score_revive = 1000;
    var_0.max_score_damage = 1000;
    reset_team_score_performance( var_0 );
    return var_0;
}

reset_team_score_performance( var_0 )
{
    var_0.team_encounter_performance["damage_done_on_alien"] = 0;
    var_0.team_encounter_performance["num_players_enter_laststand"] = 0;
    var_0.team_encounter_performance["num_players_bleed_out"] = 0;
    return var_0;
}

reset_player_teamwork_score_performance( var_0 )
{
    var_0.encounter_performance["damage_done_on_alien"] = 0;
    var_0.encounter_performance["team_support_deploy"] = 0;
}

calculate_teamwork_score( var_0, var_1 )
{
    var_2 = var_1.max_score_deploy;
    var_3 = min( var_2, get_player_encounter_performance( var_0, "team_support_deploy" ) * 100 );

    if ( get_team_encounter_performance( var_1, "num_players_bleed_out" ) )
        var_4 = 0;
    else
    {
        var_5 = var_1.max_score_revive;
        var_6 = get_team_encounter_performance( var_1, "num_players_enter_laststand" ) * 200;
        var_4 = max( 0, var_5 - var_6 );
    }

    var_7 = get_team_encounter_performance( var_1, "damage_done_on_alien" );

    if ( var_7 == 0 )
        var_7 = 1;

    var_8 = get_player_encounter_performance( var_0, "damage_done_on_alien" ) / var_7;
    var_9 = var_1.max_score_damage;
    var_10 = var_9 * level.players.size;
    var_11 = min( var_9, var_8 * var_10 );
    return int( var_3 + var_4 + var_11 );
}

get_team_score_component_name()
{
    return common_scripts\utility::ter_op( isdefined( level.team_score_component_name ), level.team_score_component_name, "team" );
}

set_team_score_component_name( var_0 )
{
    level.team_score_component_name = var_0;
}

init_blocker_hive_teamwork_score_component()
{
    register_encounter_score_component( "team_blocker", ::init_blocker_teamwork_score, ::reset_team_score_performance, ::reset_player_teamwork_score_performance, ::calculate_teamwork_score, 2, "hive" );
}

init_blocker_teamwork_score( var_0 )
{
    var_0.max_score_deploy = 2000;
    var_0.max_score_revive = 1000;
    var_0.max_score_damage = 2500;
    reset_team_score_performance( var_0 );
    return var_0;
}

init_personal_score_component()
{
    register_encounter_score_component( "personal", ::init_personal_score, undefined, ::reset_player_personal_score_performance, ::calculate_personal_skill_score, 3, "hive" );
}

init_personal_score( var_0 )
{
    if ( maps\mp\alien\_utility::isplayingsolo() )
    {
        var_0.max_score_damage_taken = 2500;
        var_0.max_score_accuracy = 1500;
    }
    else
    {
        var_0.max_score_damage_taken = 1500;
        var_0.max_score_accuracy = 1000;
    }

    return var_0;
}

reset_player_personal_score_performance( var_0 )
{
    var_0.encounter_performance["damage_taken"] = 0;
    var_0.encounter_performance["shots_hit"] = 0;
    var_0.encounter_performance["shots_fired"] = 0;
}

calculate_personal_skill_score( var_0, var_1 )
{
    var_2 = get_player_encounter_performance( var_0, "damage_taken" ) / 500;
    var_3 = max( 0, 1 - var_2 );
    var_4 = var_1.max_score_damage_taken;
    var_5 = var_4 * var_3;

    if ( get_player_encounter_performance( var_0, "shots_fired" ) == 0 )
        var_6 = 1.0;
    else
        var_6 = get_player_encounter_performance( var_0, "shots_hit" ) / get_player_encounter_performance( var_0, "shots_fired" );

    var_6 = min( 1.0, var_6 );
    var_7 = var_1.max_score_accuracy;
    var_8 = var_7 * var_6;
    return int( var_5 + var_8 );
}

get_personal_score_component_name()
{
    return common_scripts\utility::ter_op( isdefined( level.personal_score_component_name ), level.personal_score_component_name, "personal" );
}

set_personal_score_component_name( var_0 )
{
    level.personal_score_component_name = var_0;
}

init_blocker_hive_personal_score_component()
{
    register_encounter_score_component( "personal_blocker", ::init_blocker_personal_score, undefined, ::reset_player_personal_score_performance, ::calculate_personal_skill_score, 3, "hive" );
}

init_blocker_personal_score( var_0 )
{
    if ( maps\mp\alien\_utility::isplayingsolo() )
    {
        var_0.max_score_damage_taken = 5500;
        var_0.max_score_accuracy = 4500;
    }
    else
    {
        var_0.max_score_damage_taken = 2500;
        var_0.max_score_accuracy = 2000;
    }

    return var_0;
}

init_escape_score_component()
{
    register_encounter_score_component( "escape", ::init_escape_score, undefined, undefined, ::calculate_escape_score, 9, "escape" );
}

init_escape_score( var_0 )
{
    var_0.team_encounter_performance["time_remain_ms"] = 0;
    var_0.team_encounter_performance["escape_player_ratio"] = 0;
    return var_0;
}

calculate_escape_score( var_0, var_1 )
{
    var_2 = get_team_encounter_performance( var_1, "time_remain_ms" ) / 240000;
    var_3 = 1 + ( var_0 maps\mp\alien\_prestige::get_num_nerf_selected() + 1 ) * 0.2;
    var_4 = int( 15000 + 15000 * var_2 * get_team_encounter_performance( var_1, "escape_player_ratio" ) * var_3 );
    return var_4;
}

process_end_game_score_escaped( var_0, var_1 )
{
    var_2 = var_1.size / level.players.size;
    update_team_encounter_performance( "escape", "time_remain_ms", var_0 );
    update_team_encounter_performance( "escape", "escape_player_ratio", var_2 );
    calculate_encounter_scores( var_1, [ "escape" ] );
}

update_personal_encounter_performance( var_0, var_1, var_2 )
{
    if ( !has_encounter_score_component( var_0 ) )
        return;

    if ( !isplayer( self ) )
        return;

    self.encounter_performance = update_encounter_performance_internal( self.encounter_performance, var_1, var_2 );
}

update_team_encounter_performance( var_0, var_1, var_2 )
{
    if ( !has_encounter_score_component( var_0 ) )
        return;

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

    level.encounter_score_components[var_0].team_encounter_performance[var_1] = level.encounter_score_components[var_0].team_encounter_performance[var_1] + var_2;
}

update_encounter_performance_internal( var_0, var_1, var_2 )
{
    if ( !isdefined( var_2 ) )
        var_2 = 1;

    var_0[var_1] = var_0[var_1] + var_2;
    return var_0;
}

register_scoring_mode()
{
    if ( maps\mp\alien\_utility::isplayingsolo() )
        setomnvar( "ui_alien_is_solo", 1 );
    else
        setomnvar( "ui_alien_is_solo", 0 );
}

get_team_encounter_performance( var_0, var_1 )
{
    return var_0.team_encounter_performance[var_1];
}

get_player_encounter_performance( var_0, var_1 )
{
    return var_0.encounter_performance[var_1];
}

has_encounter_score_component( var_0 )
{
    return has_score_component_internal( level.encounter_score_components, var_0 );
}

has_eog_score_component( var_0 )
{
    return has_score_component_internal( level.eog_score_components, var_0 );
}

has_score_component_internal( var_0, var_1 )
{
    if ( is_scoring_disabled() )
        return 0;

    return isdefined( var_0[var_1] );
}

register_eog_score_component( var_0, var_1 )
{
    var_2 = spawnstruct();
    var_2.lua_string_index = var_1;
    level.eog_score_components[var_0] = var_2;
}

register_encounter_score_component( var_0, var_1, var_2, var_3, var_4, var_5, var_6, var_7 )
{
    var_8 = spawnstruct();
    var_8 = [[ var_1 ]]( var_8 );
    var_8.reset_team_performance_func = var_2;
    var_8.reset_player_performance_func = var_3;
    var_8.calculate_func = var_4;
    var_8.lua_string_index = var_5;
    var_8.end_game_score_component_ref = var_6;

    if ( isdefined( var_7 ) )
        var_8.player_init_func = var_7;

    level.encounter_score_components[var_0] = var_8;
}

update_performance_alien_damage( var_0, var_1, var_2 )
{
    if ( !isdefined( var_0 ) )
        return;

    if ( isdefined( var_0.classname ) && var_0.classname == "script_vehicle" )
        return;

    if ( var_2 == "MOD_TRIGGER_HURT" )
        return;

    update_team_encounter_performance( get_team_score_component_name(), "damage_done_on_alien", var_1 );
    var_3 = get_personal_score_component_name();

    if ( isplayer( var_0 ) )
        var_0 update_personal_encounter_performance( var_3, "damage_done_on_alien", var_1 );
    else if ( isdefined( var_0.owner ) )
        var_0.owner update_personal_encounter_performance( var_3, "damage_done_on_alien", var_1 );
}

give_attacker_kill_rewards( var_0, var_1 )
{
    if ( self.agentteam == "allies" )
        return;

    if ( maps\mp\alien\_utility::get_alien_type() == "elite" || maps\mp\alien\_utility::get_alien_type() == "mammoth" )
    {
        var_2 = get_reward_point_for_kill();

        foreach ( var_4 in level.players )
            givekillreward( var_4, var_2, "large" );

        return;
    }

    if ( isdefined( self.attacker_damage ) )
    {
        var_6 = 0.1;
        var_7 = self.max_health * var_6;
        var_8 = getassistbonusamount();

        foreach ( var_10 in self.attacker_damage )
        {
            if ( var_10.player == var_0 || isdefined( var_0.owner ) && var_10.player == var_0.owner )
                continue;

            if ( var_10.damage >= var_7 )
            {
                if ( isdefined( var_10.player ) && var_10.player != var_0 )
                {
                    var_10.player maps\mp\alien\_persistence::eog_player_update_stat( "assists", 1 );
                    givekillreward( var_10.player, var_8 );
                }
            }
        }
    }

    if ( !isdefined( var_0 ) )
        return;

    if ( !isplayer( var_0 ) && ( !isdefined( var_0.owner ) || !isplayer( var_0.owner ) ) )
        return;

    var_12 = 0;

    if ( isdefined( var_0.owner ) )
    {
        var_0 = var_0.owner;
        var_12 = 1;
    }

    var_2 = get_reward_point_for_kill();

    if ( isdefined( var_1 ) && var_1 == "soft" && !var_12 )
        var_2 = int( var_2 * 1.5 );

    givekillreward( var_0, var_2, "large", var_1 );
}

givekillreward( var_0, var_1, var_2, var_3 )
{
    var_4 = var_1 * level.cycle_reward_scalar;
    var_0 maps\mp\alien\_persistence::give_player_currency( var_4, var_2, var_3 );

    if ( isdefined( level.alien_xp ) )
        var_0 maps\mp\alien\_persistence::give_player_xp( int( var_4 ) );

    if ( common_scripts\utility::flag_exist( "cortex_started" ) && common_scripts\utility::flag( "cortex_started" ) )
    {
        if ( isdefined( level.add_cortex_charge_func ) )
            [[ level.add_cortex_charge_func ]]( var_1 );
    }
}

giveassistbonus( var_0, var_1 )
{
    if ( !isdefined( var_0 ) )
        return;

    if ( !isplayer( var_0 ) && ( !isdefined( var_0.owner ) || !isplayer( var_0.owner ) ) )
        return;

    if ( isdefined( var_0.owner ) )
        var_0 = var_0.owner;

    if ( !isdefined( self.attacker_damage ) )
        self.attacker_damage = [];

    foreach ( var_3 in self.attacker_damage )
    {
        if ( var_3.player == var_0 )
        {
            var_3.damage = var_3.damage + var_1;
            return;
        }
    }

    var_5 = spawnstruct();
    var_5.player = var_0;
    var_5.damage = var_1;
    self.attacker_damage[self.attacker_damage.size] = var_5;
}

getassistbonusamount()
{
    return level.alien_types[maps\mp\alien\_utility::get_alien_type()].attributes["reward"] * 0.5;
}

get_reward_point_for_kill()
{
    return level.alien_types[maps\mp\alien\_utility::get_alien_type()].attributes["reward"];
}

round_up_to_nearest( var_0, var_1 )
{
    var_2 = var_0 / var_1;
    var_2 = ceil( var_2 );
    return int( var_2 * var_1 );
}

is_scoring_disabled()
{
    if ( maps\mp\alien\_utility::is_chaos_mode() )
        return 1;

    return 0;
}

calculate_under_max_score( var_0, var_1, var_2 )
{
    var_3 = clamp( var_1 - var_0, 0, var_1 );
    return int( var_3 / var_1 * var_2 );
}
