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

main()
{
    level.bot_tag_obj_radius = 200;
    setup_callbacks();
}

setup_callbacks()
{
    level.bot_funcs["gametype_think"] = ::bot_mugger_think;
    level.bot_funcs["gametype_loadout_modify"] = ::bot_mugger_loadout_modify;
}

bot_mugger_think()
{
    self notify( "bot_mugger_think" );
    self endon( "bot_mugger_think" );
    self endon( "death" );
    self endon( "disconnect" );
    level endon( "game_ended" );
    self.last_killtag_tactical_goal_pos = ( 0, 0, 0 );
    self.tag_getting = undefined;
    self.heading_for_tag_pile = 0;
    self.hiding_until_bank = 0;
    self.default_meleechargedist = self botgetdifficultysetting( "meleeChargeDist" );
    childthread tag_watcher();

    if ( self botgetdifficultysetting( "strategyLevel" ) > 0 )
        childthread tag_pile_watcher();

    if ( self botgetdifficultysetting( "strategyLevel" ) > 0 )
        childthread enemy_watcher();

    for (;;)
    {
        if ( self botgetdifficultysetting( "strategyLevel" ) > 1 )
        {
            if ( isdefined( self.tags_carried ) && level.mugger_bank_limit <= self.tags_carried )
            {
                if ( !self.hiding_until_bank )
                {
                    var_0 = getnodesinradius( self.origin, 1000, 0, 500, "node_hide" );
                    var_1 = self botnodepick( var_0, 3, "node_hide" );

                    if ( isdefined( var_1 ) )
                    {
                        self botsetscriptgoalnode( var_1, "critical" );
                        self.hiding_until_bank = 1;
                    }
                }
            }
            else if ( self.hiding_until_bank )
            {
                self botclearscriptgoal();
                self.hiding_until_bank = 0;
            }
        }

        if ( !self.hiding_until_bank )
        {
            if ( !isdefined( self.tag_getting ) && !self.heading_for_tag_pile )
                self [[ self.personality_update_function ]]();
        }

        wait 0.05;
    }
}

enemy_watcher()
{
    for (;;)
    {
        if ( self botgetdifficultysetting( "strategyLevel" ) < 2 )
            wait 0.5;
        else
            wait 0.2;

        if ( isdefined( self.enemy ) && isplayer( self.enemy ) && isdefined( self.enemy.tags_carried ) && self.enemy.tags_carried >= 3 && self botcanseeentity( self.enemy ) && distance( self.origin, self.enemy.origin ) <= 500 )
        {
            self botsetdifficultysetting( "meleeChargeDist", 500 );
            self botsetflag( "prefer_melee", 1 );
            self botsetflag( "throw_knife_melee", level.mugger_throwing_knife_mug_frac > 0 );
            continue;
        }

        self botsetdifficultysetting( "meleeChargeDist", self.default_meleechargedist );
        self botsetflag( "prefer_melee", 0 );
        self botsetflag( "throw_knife_melee", 0 );
    }
}

tag_pile_watcher()
{
    for (;;)
    {
        level waittill( "mugger_tag_pile", var_0 );

        if ( self.health <= 0 )
            continue;

        if ( self.hiding_until_bank )
            continue;

        if ( !isdefined( self.last_tag_pile_time ) || gettime() - self.last_tag_pile_time > 7500 )
        {
            self.last_tag_pile_time = undefined;
            self.last_tag_pile_location = undefined;
            self.heading_for_tag_pile = 0;
        }

        if ( !isdefined( self.last_tag_pile_location ) || distancesquared( self.origin, self.last_tag_pile_location ) > distancesquared( self.origin, var_0 ) )
        {
            self.last_tag_pile_time = gettime();
            self.last_tag_pile_location = var_0;
        }
    }
}

bot_find_closest_tag()
{
    var_0 = self getnearestnode();
    var_1 = undefined;

    if ( isdefined( var_0 ) )
    {
        var_2 = 1000000;
        var_3 = common_scripts\utility::array_combine( level.dogtags, level.mugger_extra_tags );

        foreach ( var_5 in var_3 )
        {
            if ( var_5 maps\mp\gametypes\_gameobjects::caninteractwith( self.team ) )
            {
                var_6 = distancesquared( self.origin, var_5.curorigin );

                if ( !isdefined( var_1 ) || var_6 < var_2 )
                {
                    if ( self botgetdifficultysetting( "strategyLevel" ) > 0 && var_6 < 122500 || var_6 < 1000000 && maps\mp\bots\_bots_gametype_conf::bot_is_tag_visible( var_5, var_0, self botgetfovdot() ) )
                    {
                        var_2 = var_6;
                        var_1 = var_5;
                    }
                }
            }
        }
    }

    return var_1;
}

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

    if ( isdefined( var_0 ) )
    {
        var_3 = common_scripts\utility::array_combine( level.dogtags, level.mugger_extra_tags );

        foreach ( var_5 in var_3 )
        {
            if ( var_5 maps\mp\gametypes\_gameobjects::caninteractwith( self.team ) )
            {
                if ( isplayer( self ) || distancesquared( self.origin, var_5.curorigin ) < 1000000 )
                {
                    if ( maps\mp\bots\_bots_gametype_conf::bot_is_tag_visible( var_5, var_0, var_1 ) )
                    {
                        var_6 = spawnstruct();
                        var_6.origin = var_5.curorigin;
                        var_6.tag = var_5;
                        var_2[var_2.size] = var_6;
                    }
                }
            }
        }
    }

    return var_2;
}

tag_watcher()
{
    wait( randomfloatrange( 0, 0.5 ) );

    for (;;)
    {
        if ( self botgetdifficultysetting( "strategyLevel" ) == 0 )
            wait 3.0;
        else if ( self botgetdifficultysetting( "strategyLevel" ) == 1 )
            wait 1.5;
        else
            wait 0.5;

        if ( self.health <= 0 )
            continue;

        if ( self.hiding_until_bank )
            continue;

        if ( isdefined( self.enemy ) && isplayer( self.enemy ) && self botcanseeentity( self.enemy ) )
            continue;

        var_0 = bot_find_closest_tag();

        if ( isdefined( var_0 ) )
        {
            mugger_pick_up_tag( var_0 );
            continue;
        }

        if ( !self.heading_for_tag_pile )
        {
            if ( isdefined( self.last_tag_pile_location ) && isdefined( self.last_tag_pile_time ) && gettime() - self.last_tag_pile_time <= 7500 )
                thread mugger_go_to_tag_pile( self.last_tag_pile_location );
        }
    }
}

mugger_go_to_tag_pile( var_0 )
{
    self endon( "disconnect" );
    level endon( "game_ended" );
    self.heading_for_tag_pile = 1;
    var_1 = spawnstruct();
    var_1.script_goal_type = "objective";
    var_1.objective_radius = level.bot_tag_obj_radius;
    maps\mp\bots\_bots_strategy::bot_new_tactical_goal( "kill_tag_pile", var_0, 25, var_1 );
    var_2 = common_scripts\utility::waittill_any_return( "death", "tag_spotted" );
    self botclearscriptgoal();
    self.heading_for_tag_pile = 0;
    maps\mp\bots\_bots_strategy::bot_abort_tactical_goal( "kill_tag_pile" );
}

mugger_pick_up_tag( var_0 )
{
    self endon( "disconnect" );
    level endon( "game_ended" );
    self.tag_getting = var_0;
    self notify( "tag_spotted" );
    childthread notify_when_tag_picked_up( var_0, "tag_picked_up" );
    maps\mp\bots\_bots_strategy::bot_abort_tactical_goal( "kill_tag" );
    var_1 = var_0.curorigin;

    if ( maps\mp\bots\_bots_util::bot_vectors_are_equal( self.last_killtag_tactical_goal_pos, var_0.curorigin ) )
    {
        var_2 = var_0.nearest_node;

        if ( isdefined( var_2 ) )
        {
            var_3 = var_2.origin - var_1;
            var_1 = var_1 + vectornormalize( var_3 ) * length( var_3 ) * 0.5;
        }
    }

    self.last_killtag_tactical_goal_pos = var_0.curorigin;
    var_4 = spawnstruct();
    var_4.script_goal_type = "objective";
    var_4.objective_radius = level.bot_tag_obj_radius;
    maps\mp\bots\_bots_strategy::bot_new_tactical_goal( "kill_tag", var_1, 25, var_4 );
    thread notify_when_tag_aborted( "tag_aborted" );
    var_5 = common_scripts\utility::waittill_any_return( "death", "tag_picked_up" );
    self notify( "tag_watch_stop" );
    self.tag_getting = undefined;
    self botclearscriptgoal();
    maps\mp\bots\_bots_strategy::bot_abort_tactical_goal( "kill_tag" );
}

notify_when_tag_aborted( var_0 )
{
    self endon( "disconnect" );
    level endon( "game_ended" );
    self endon( "tag_watch_stop" );

    while ( maps\mp\bots\_bots_strategy::bot_has_tactical_goal( "kill_tag" ) )
        wait 0.05;

    self notify( var_0 );
}

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

    while ( var_0 maps\mp\gametypes\_gameobjects::caninteractwith( self.team ) )
        wait 0.05;

    self notify( var_1 );
}

bot_mugger_loadout_modify( var_0 )
{
    var_1 = 0;
    var_2 = self botgetdifficulty();

    if ( var_2 == "recruit" )
        var_1 = 0.1;
    else if ( var_2 == "regular" )
        var_1 = 0.25;
    else if ( var_2 == "hardened" )
        var_1 = 0.6;
    else if ( var_2 == "veteran" )
        var_1 = 0.9;

    var_3 = var_0["loadoutEquipment"] == "throwingknife_mp";

    if ( !var_3 )
    {
        if ( var_1 >= randomfloat( 1 ) )
        {
            var_0["loadoutEquipment"] = "throwingknife_mp";
            var_3 = 1;
        }
    }

    if ( var_1 >= randomfloat( 1 ) )
    {
        if ( var_0["loadoutOffhand"] != "concussion_grenade_mp" )
            var_0["loadoutOffhand"] = "concussion_grenade_mp";
    }

    if ( var_1 >= randomfloat( 1 ) )
    {
        if ( var_0["loadoutPrimaryAttachment"] != "tactical" && var_0["loadoutPrimaryAttachment2"] != "tactical" )
        {
            var_4 = maps\mp\bots\_bots_loadout::bot_validate_weapon( var_0["loadoutPrimary"], var_0["loadoutPrimaryAttachment"], "tactical" );

            if ( var_4 )
                var_0["loadoutPrimaryAttachment2"] = "tactical";
            else
            {
                var_4 = maps\mp\bots\_bots_loadout::bot_validate_weapon( var_0["loadoutPrimary"], "tactical", var_0["loadoutPrimaryAttachment2"] );

                if ( var_4 )
                    var_0["loadoutPrimaryAttachment"] = "tactical";
            }
        }
    }

    if ( var_1 >= randomfloat( 1 ) )
    {
        if ( var_0["loadoutSecondaryAttachment"] != "tactical" && var_0["loadoutSecondaryAttachment2"] != "tactical" )
        {
            var_4 = maps\mp\bots\_bots_loadout::bot_validate_weapon( var_0["loadoutSecondary"], var_0["loadoutSecondaryAttachment"], "tactical" );

            if ( var_4 )
                var_0["loadoutSecondaryAttachment2"] = "tactical";
            else
            {
                var_4 = maps\mp\bots\_bots_loadout::bot_validate_weapon( var_0["loadoutSecondary"], "tactical", var_0["loadoutSecondaryAttachment2"] );

                if ( var_4 )
                    var_0["loadoutSecondaryAttachment"] = "tactical";
            }
        }
    }

    var_5 = [];
    var_6 = [];
    var_7 = [];
    var_8 = [];

    if ( var_3 )
        var_8[var_8.size] = "specialty_extra_deadly";

    var_8[var_8.size] = "specialty_lightweight";
    var_8[var_8.size] = "specialty_marathon";
    var_8[var_8.size] = "specialty_fastsprintrecovery";
    var_8[var_8.size] = "specialty_stun_resistance";

    for ( var_9 = 1; var_9 < 9; var_9++ )
    {
        if ( isdefined( var_0["loadoutPerk" + var_9] ) )
        {
            if ( var_0["loadoutPerk" + var_9] != "none" )
            {
                var_5[var_5.size] = var_0["loadoutPerk" + var_9];
                var_6[var_6.size] = var_9;
                continue;
            }

            var_7[var_7.size] = var_9;
        }
    }

    foreach ( var_11 in var_8 )
    {
        if ( var_1 >= randomfloat( 1 ) )
        {
            if ( !common_scripts\utility::array_contains( var_5, var_11 ) )
            {
                var_12 = -1;

                if ( var_7.size )
                {
                    var_12 = var_7[0];
                    var_7 = common_scripts\utility::array_remove( var_7, var_12 );
                }
                else if ( var_6.size )
                {
                    var_12 = common_scripts\utility::random( var_6 );
                    var_6 = common_scripts\utility::array_remove( var_6, var_12 );
                }

                if ( var_12 != -1 )
                    var_0["loadoutPerk" + var_12] = var_11;
            }
        }
    }

    if ( var_1 >= randomfloat( 1 ) )
    {
        if ( var_0["loadoutStreakType"] == "streaktype_assault" && var_0["loadoutStreak1"] != "airdrop_juggernaut_maniac" && var_0["loadoutStreak2"] != "airdrop_juggernaut_maniac" && var_0["loadoutStreak3"] != "airdrop_juggernaut_maniac" )
            var_0["loadoutStreak3"] = "airdrop_juggernaut_maniac";
    }

    return var_0;
}
