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

#using_animtree("generic_human");

movecqb()
{
    animscripts\run::standrun_checkchangeweapon();

    if ( self.a.pose != "stand" )
    {
        self clearanim( %root, 0.2 );

        if ( self.a.pose == "prone" )
            animscripts\utility::exitpronewrapper( 1 );

        self.a.pose = "stand";
    }

    self.a.movement = self.movemode;
    cqbtracking();

    if ( animscripts\run::move_checkstairstransition() )
        return;

    self clearanim( %stair_transitions, 0.2 );

    if ( animscripts\run::standrun_checkreload() )
        return;

    if ( isdefined( self.timeofmaincqbupdate ) )
        var_0 = self.timeofmaincqbupdate;
    else
        var_0 = 0;

    self.timeofmaincqbupdate = gettime();
    var_1 = determinecqbanim();

    if ( self.stairsstate == "none" )
        var_2 = 0.3;
    else
        var_2 = 0.1;

    var_3 = 0.2;
    var_4 = %walk_and_run_loops;

    if ( self.timeofmaincqbupdate - var_0 > var_3 * 1000 )
        var_4 = %stand_and_crouch;

    self setflaggedanimknoball( "runanim", var_1, var_4, 1, var_2, self.moveplaybackrate, 1 );
    cqb_playfacialanim( var_1 );
    animscripts\run::setmovenonforwardanims( animscripts\utility::lookupanim( "cqb", "move_b" ), animscripts\utility::lookupanim( "cqb", "move_l" ), animscripts\utility::lookupanim( "cqb", "move_r" ) );
    thread animscripts\run::setcombatstandmoveanimweights( "cqb" );
    animscripts\notetracks::donotetracksfortime( var_3, "runanim" );
}

determinecqbanim()
{
    if ( isdefined( self.custommoveanimset ) && isdefined( self.custommoveanimset["cqb"] ) )
        return animscripts\run::getrunanim();

    if ( self.stairsstate == "up" )
        return animscripts\utility::lookupanim( "cqb", "stairs_up" );

    if ( self.stairsstate == "down" )
        return animscripts\utility::lookupanim( "cqb", "stairs_down" );

    if ( self.movemode == "walk" )
        return animscripts\utility::lookupanim( "cqb", "move_f" );

    if ( isdefined( self.a.bdisablemovetwitch ) && self.a.bdisablemovetwitch )
        return animscripts\utility::lookupanim( "cqb", "straight" );

    var_0 = animscripts\utility::lookupanim( "cqb", "straight_twitch" );

    if ( !isdefined( var_0 ) || var_0.size == 0 )
        return animscripts\utility::lookupanim( "cqb", "straight" );

    var_1 = animscripts\utility::getrandomintfromseed( self.a.runloopcount, 4 );

    if ( var_1 == 0 )
    {
        var_2 = animscripts\utility::getrandomintfromseed( self.a.runloopcount, var_0.size );
        return var_0[var_2];
    }

    return animscripts\utility::lookupanim( "cqb", "straight" );
}

cqb_reloadinternal()
{
    self endon( "movemode" );
    self orientmode( "face motion" );
    var_0 = "reload_" + animscripts\combat_utility::getuniqueflagnameindex();
    var_1 = animscripts\utility::lookupanim( "cqb", "reload" );

    if ( isarray( var_1 ) )
        var_1 = var_1[randomint( var_1.size )];

    self setflaggedanimknoballrestart( var_0, var_1, %body, 1, 0.25 );
    cqb_playfacialanim( var_1 );
    animscripts\run::setmovenonforwardanims( animscripts\utility::lookupanim( "cqb", "move_b" ), animscripts\utility::lookupanim( "cqb", "move_l" ), animscripts\utility::lookupanim( "cqb", "move_r" ) );
    thread animscripts\run::setcombatstandmoveanimweights( "cqb" );
    animscripts\shared::donotetracks( var_0 );
}

cqbtracking()
{
    var_0 = self.stairsstate != "none";
    var_1 = !var_0 && animscripts\move::mayshootwhilemoving();
    animscripts\run::setshootwhilemoving( var_1 );

    if ( var_0 )
        animscripts\run::endfaceenemyaimtracking();
    else
        thread animscripts\run::faceenemyaimtracking();
}

setupcqbpointsofinterest()
{
    level.cqbpointsofinterest = [];
    var_0 = getentarray( "cqb_point_of_interest", "targetname" );

    for ( var_1 = 0; var_1 < var_0.size; var_1++ )
    {
        level.cqbpointsofinterest[var_1] = var_0[var_1].origin;
        var_0[var_1] delete();
    }
}

findcqbpointsofinterest()
{
    if ( isdefined( anim.findingcqbpointsofinterest ) )
        return;

    anim.findingcqbpointsofinterest = 1;

    if ( !level.cqbpointsofinterest.size )
        return;

    for (;;)
    {
        var_0 = getaiarray();
        var_1 = 0;

        foreach ( var_3 in var_0 )
        {
            if ( isalive( var_3 ) && var_3 animscripts\utility::iscqbwalking() && !isdefined( var_3.disable_cqb_points_of_interest ) )
            {
                var_4 = var_3.a.movement != "stop";
                var_5 = ( var_3.origin[0], var_3.origin[1], var_3 getshootatpos()[2] );
                var_6 = var_5;
                var_7 = anglestoforward( var_3.angles );

                if ( var_4 )
                {
                    var_8 = bullettrace( var_6, var_6 + var_7 * 128, 0, undefined );
                    var_6 = var_8["position"];
                }

                var_9 = -1;
                var_10 = 1048576;

                for ( var_11 = 0; var_11 < level.cqbpointsofinterest.size; var_11++ )
                {
                    var_12 = level.cqbpointsofinterest[var_11];
                    var_13 = distancesquared( var_12, var_6 );

                    if ( var_13 < var_10 )
                    {
                        if ( var_4 )
                        {
                            if ( distancesquared( var_12, var_5 ) < 4096 )
                                continue;

                            var_14 = vectordot( vectornormalize( var_12 - var_5 ), var_7 );

                            if ( var_14 < 0.643 || var_14 > 0.966 )
                                continue;
                        }
                        else if ( var_13 < 2500 )
                            continue;

                        if ( !sighttracepassed( var_6, var_12, 0, undefined ) )
                            continue;

                        var_10 = var_13;
                        var_9 = var_11;
                    }
                }

                if ( var_9 < 0 )
                    var_3.cqb_point_of_interest = undefined;
                else
                    var_3.cqb_point_of_interest = level.cqbpointsofinterest[var_9];

                wait 0.05;
                var_1 = 1;
            }
        }

        if ( !var_1 )
            wait 0.25;
    }
}

cqb_playfacialanim( var_0 )
{
    self.facialidx = animscripts\face::playfacialanim( var_0, "run", self.facialidx );
}

cqb_clearfacialanim()
{
    self.facialidx = undefined;
    self clearanim( %head, 0.2 );
}
