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

main()
{
    setup_callbacks();
    setup_bot_blitz();
}

setup_callbacks()
{
    level.bot_funcs["gametype_think"] = ::bot_blitz_think;
}

setup_bot_blitz()
{
    maps\mp\bots\_bots_util::bot_waittill_bots_enabled( 1 );
    level.protect_radius = 600;
    thread bot_blitz_ai_director_update();
    level.bot_gametype_precaching_done = 1;
}

bot_blitz_think()
{
    self notify( "bot_blitz_think" );
    self endon( "bot_blitz_think" );
    self endon( "death" );
    self endon( "disconnect" );
    level endon( "game_ended" );

    while ( !isdefined( level.bot_gametype_precaching_done ) )
        wait 0.05;

    self botsetflag( "separation", 0 );

    for (;;)
    {
        wait 0.05;

        if ( !isdefined( self.role ) )
            initialize_blitz_role();

        if ( maps\mp\bots\_bots_strategy::bot_has_tactical_goal() )
            continue;

        if ( self.role == "attacker" )
        {
            var_0 = level.portallist[common_scripts\utility::get_enemy_team( self.team )];

            if ( var_0.open )
            {
                if ( maps\mp\bots\_bots_util::bot_is_defending() )
                    maps\mp\bots\_bots_strategy::bot_defend_stop();

                if ( !self bothasscriptgoal() )
                    self botsetscriptgoal( var_0.origin, 0, "objective" );
            }
            else if ( !maps\mp\bots\_bots_util::bot_is_defending() )
            {
                self botclearscriptgoal();
                maps\mp\bots\_bots_strategy::bot_protect_point( var_0.origin, level.protect_radius );
            }

            continue;
        }

        if ( self.role == "defender" )
        {
            var_0 = level.portallist[self.team];

            if ( !maps\mp\bots\_bots_util::bot_is_defending_point( var_0.origin ) )
            {
                var_1["min_goal_time"] = 20;
                var_1["max_goal_time"] = 30;
                var_1["score_flags"] = "strict_los";
                maps\mp\bots\_bots_strategy::bot_protect_point( var_0.origin, level.protect_radius, var_1 );
            }
        }
    }
}

initialize_blitz_role()
{
    var_0 = get_allied_attackers_for_team( self.team );
    var_1 = get_allied_defenders_for_team( self.team );
    var_2 = blitz_bot_attacker_limit_for_team( self.team );
    var_3 = blitz_bot_defender_limit_for_team( self.team );
    var_4 = level.bot_personality_type[self.personality];

    if ( var_4 == "active" )
    {
        if ( var_0.size >= var_2 )
        {
            var_5 = 0;

            foreach ( var_7 in var_0 )
            {
                if ( isai( var_7 ) && level.bot_personality_type[var_7.personality] == "stationary" )
                {
                    var_7.role = undefined;
                    var_5 = 1;
                    break;
                }
            }

            if ( var_5 )
            {
                blitz_set_role( "attacker" );
                return;
            }

            blitz_set_role( "defender" );
            return;
        }
        else
            blitz_set_role( "attacker" );
    }
    else if ( var_4 == "stationary" )
    {
        if ( var_1.size >= var_3 )
        {
            var_5 = 0;

            foreach ( var_10 in var_1 )
            {
                if ( isai( var_10 ) && level.bot_personality_type[var_10.personality] == "active" )
                {
                    var_10.role = undefined;
                    var_5 = 1;
                    break;
                }
            }

            if ( var_5 )
            {
                blitz_set_role( "defender" );
                return;
            }

            blitz_set_role( "attacker" );
            return;
        }
        else
            blitz_set_role( "defender" );
    }
}

bot_blitz_ai_director_update()
{
    level notify( "bot_blitz_ai_director_update" );
    level endon( "bot_blitz_ai_director_update" );
    level endon( "game_ended" );
    var_0[0] = "allies";
    var_0[1] = "axis";

    for (;;)
    {
        foreach ( var_2 in var_0 )
        {
            var_3 = blitz_bot_attacker_limit_for_team( var_2 );
            var_4 = blitz_bot_defender_limit_for_team( var_2 );
            var_5 = get_allied_attackers_for_team( var_2 );
            var_6 = get_allied_defenders_for_team( var_2 );

            if ( var_5.size > var_3 )
            {
                var_7 = [];
                var_8 = 0;

                foreach ( var_10 in var_5 )
                {
                    if ( isai( var_10 ) )
                    {
                        if ( level.bot_personality_type[var_10.personality] == "stationary" )
                        {
                            var_10 blitz_set_role( "defender" );
                            var_8 = 1;
                            break;
                        }
                        else
                            var_7 = common_scripts\utility::array_add( var_7, var_10 );
                    }
                }

                if ( !var_8 && var_7.size > 0 )
                    common_scripts\utility::random( var_7 ) blitz_set_role( "defender" );
            }

            if ( var_6.size > var_4 )
            {
                var_12 = [];
                var_13 = 0;

                foreach ( var_15 in var_6 )
                {
                    if ( isai( var_15 ) )
                    {
                        if ( level.bot_personality_type[var_15.personality] == "active" )
                        {
                            var_15 blitz_set_role( "attacker" );
                            var_13 = 1;
                            break;
                        }
                        else
                            var_12 = common_scripts\utility::array_add( var_12, var_15 );
                    }
                }

                if ( !var_13 && var_12.size > 0 )
                    common_scripts\utility::random( var_12 ) blitz_set_role( "attacker" );
            }
        }

        wait 1.0;
    }
}

blitz_bot_attacker_limit_for_team( var_0 )
{
    var_1 = blitz_get_num_players_on_team( var_0 );
    return int( int( var_1 ) / 2 ) + 1 + int( var_1 ) % 2;
}

blitz_bot_defender_limit_for_team( var_0 )
{
    var_1 = blitz_get_num_players_on_team( var_0 );
    return max( int( int( var_1 ) / 2 ) - 1, 0 );
}

blitz_get_num_players_on_team( var_0 )
{
    var_1 = 0;

    foreach ( var_3 in level.participants )
    {
        if ( maps\mp\_utility::isteamparticipant( var_3 ) && isdefined( var_3.team ) && var_3.team == var_0 )
            var_1++;
    }

    return var_1;
}

get_allied_attackers_for_team( var_0 )
{
    var_1 = get_players_by_role( "attacker", var_0 );

    foreach ( var_3 in level.players )
    {
        if ( !isai( var_3 ) && isdefined( var_3.team ) && var_3.team == var_0 )
        {
            if ( distancesquared( level.portallist[var_0].origin, var_3.origin ) > level.protect_radius * level.protect_radius )
                var_1 = common_scripts\utility::array_add( var_1, var_3 );
        }
    }

    return var_1;
}

get_allied_defenders_for_team( var_0 )
{
    var_1 = get_players_by_role( "defender", var_0 );

    foreach ( var_3 in level.players )
    {
        if ( !isai( var_3 ) && isdefined( var_3.team ) && var_3.team == var_0 )
        {
            if ( distancesquared( level.portallist[var_0].origin, var_3.origin ) <= level.protect_radius * level.protect_radius )
                var_1 = common_scripts\utility::array_add( var_1, var_3 );
        }
    }

    return var_1;
}

blitz_set_role( var_0 )
{
    self.role = var_0;
    self botclearscriptgoal();
    maps\mp\bots\_bots_strategy::bot_defend_stop();
}

get_players_by_role( var_0, var_1 )
{
    var_2 = [];

    foreach ( var_4 in level.participants )
    {
        if ( !isdefined( var_4.team ) )
            continue;

        if ( isalive( var_4 ) && maps\mp\_utility::isteamparticipant( var_4 ) && var_4.team == var_1 && isdefined( var_4.role ) && var_4.role == var_0 )
            var_2[var_2.size] = var_4;
    }

    return var_2;
}
