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

init()
{
    level.killstreakfuncs["mine_level_killstreak"] = ::tryusewolfpack;
    level.killstreakweildweapons["killstreak_wolfpack_mp"] = "mine_level_killstreak";
}

setup_callbacks()
{
    level.agent_funcs["wolf"] = level.agent_funcs["dog"];
    level.agent_funcs["wolf"]["spawn"] = ::spawn_dog;
    level.agent_funcs["wolf"]["on_killed"] = ::on_agent_dog_killed;
    level.agent_funcs["wolf"]["on_damaged"] = maps\mp\agents\_agents::on_agent_generic_damaged;
    level.agent_funcs["wolf"]["on_damaged_finished"] = maps\mp\killstreaks\_dog_killstreak::on_damaged_finished;
    level.agent_funcs["wolf"]["think"] = ::think_init;
}

tryusewolfpack( var_0, var_1 )
{
    setup_callbacks();
    return usedog();
}

usedog()
{
    if ( maps\mp\agents\_agent_utility::getnumownedactiveagents( self ) >= 2 )
    {
        self iprintlnbold( &"KILLSTREAKS_AGENT_MAX" );
        return 0;
    }

    var_0 = getmaxagents();

    if ( maps\mp\agents\_agent_utility::getnumactiveagents() >= var_0 )
    {
        self iprintlnbold( &"KILLSTREAKS_UNAVAILABLE" );
        return 0;
    }

    if ( !maps\mp\_utility::isreallyalive( self ) )
        return 0;

    var_1 = spawnwolf( 1 );

    if ( var_1 )
    {
        self playsound( "mp_mine_wolf_spawn" );
        thread maps\mp\_utility::teamplayercardsplash( "used_mine_level_killstreak", self );
        thread spawnwolfpack();
    }

    return var_1;
}

spawnwolf( var_0 )
{
    var_1 = maps\mp\agents\_agent_common::connectnewagent( "wolf", self.team );

    if ( !isdefined( var_1 ) )
        return 0;

    var_1 maps\mp\agents\_agent_utility::set_agent_team( self.team, self );
    var_2 = "wolf_spawn_0" + var_0;
    var_3 = common_scripts\utility::getstruct( var_2, "targetname" );
    var_4 = var_3.origin;
    var_5 = self.angles;
    var_1.wolfid = var_0;
    var_1.pathnodearray = common_scripts\utility::getstructarray( "wolf_path_0" + var_0, "script_noteworthy" );
    var_1 thread [[ var_1 maps\mp\agents\_agent_utility::agentfunc( "spawn" ) ]]( var_4, var_5, self );
    var_1 maps\mp\_utility::_setnameplatematerial( "player_name_bg_green_dog", "player_name_bg_red_dog" );
    return 1;
}

spawnwolfpack()
{
    self endon( "death" );
    self endon( "disconnect" );
    level endon( "game_ended" );

    for ( var_0 = 2; var_0 <= 3; var_0++ )
    {
        wait 0.75;
        spawnwolf( var_0 );
    }

    var_1 = 3;

    for (;;)
    {
        level waittill( "wolf_killed", var_2 );

        if ( var_1 > 0 )
        {
            wait 0.75;
            spawnwolf( var_2 );
            var_1--;
            continue;
        }

        break;
    }
}

on_agent_dog_killed( var_0, var_1, var_2, var_3, var_4, var_5, var_6, var_7, var_8 )
{
    self.isactive = 0;
    self.hasdied = 0;
    var_1.lastkilldogtime = gettime();

    if ( isdefined( self.animcbs.onexit[self.aistate] ) )
        self [[ self.animcbs.onexit[self.aistate] ]]();

    if ( isplayer( var_1 ) && isdefined( self.owner ) && var_1 != self.owner )
        maps\mp\gametypes\_damage::onkillstreakkilled( var_1, var_4, var_3, var_2, "destroyed_ks_wolf" );

    self setanimstate( "death" );
    var_9 = self getanimentry();
    var_10 = getanimlength( var_9 );
    var_8 = int( var_10 * 1000 );
    self.body = self cloneagent( var_8 );
    self playsound( "anml_wolf_shot_death" );
    level notify( "wolf_killed", self.wolfid );
    maps\mp\agents\_agent_utility::deactivateagent();
    self notify( "killanimscript" );
}

spawn_dog( var_0, var_1, var_2 )
{
    var_3 = 1;
    var_4 = "mp_fullbody_wolf_c";
    var_5 = 1;

    if ( self.wolfid == 1 )
    {
        var_4 = "mp_fullbody_wolf_b";
        var_5 = 0;
    }

    if ( ishairrunning() )
        var_4 = var_4 + "_fur";

    self setmodel( var_4 );
    self.species = "dog";
    self.onenteranimstate = maps\mp\agents\dog\_dog_think::onenteranimstate;

    if ( isdefined( var_0 ) && isdefined( var_1 ) )
    {
        var_6 = var_0;
        var_7 = var_1;
    }
    else
    {
        var_8 = self [[ level.getspawnpoint ]]();
        var_6 = var_8.origin;
        var_7 = var_8.angles;
    }

    maps\mp\agents\_agent_utility::activateagent();
    self.spawntime = gettime();
    self.lastspawntime = gettime();
    self.biswolf = 1;
    var_9 = "wolf_animclass";
    maps\mp\agents\dog\_dog_think::init();
    self.watchattackstatefunc = ::watchattackstate;
    self spawnagent( var_6, var_7, var_9, 15, 40, var_2 );
    level notify( "spawned_agent", self );
    maps\mp\agents\_agent_common::set_agent_health( 200 );

    if ( isdefined( var_2 ) )
        maps\mp\agents\_agent_utility::set_agent_team( var_2.team, var_2 );

    self setthreatbiasgroup( "Dogs" );
    self takeallweapons();

    if ( isdefined( self.owner ) )
    {
        self hide();
        wait 1.0;

        if ( !isalive( self ) )
            return;

        self show();
    }

    self thread [[ maps\mp\agents\_agent_utility::agentfunc( "think" ) ]]();
    wait 0.1;

    if ( ishairrunning() )
    {
        var_10 = level.wolffurfx[var_5];
        playfxontag( var_10, self, "tag_origin" );
    }
}

think_init()
{
    maps\mp\agents\dog\_dog_think::setupdogstate();
    self.wolfgoalpos = get_closest( self.origin, self.pathnodearray );
    thread think();
    thread maps\mp\agents\dog\_dog_think::watchownerdeath();
    thread maps\mp\agents\dog\_dog_think::watchownerteamchange();
    thread maps\mp\agents\dog\_dog_think::waitforbadpath();
    thread maps\mp\agents\dog\_dog_think::waitforpathset();
}

think()
{
    self endon( "death" );
    level endon( "game_ended" );

    if ( isdefined( self.owner ) )
    {
        self endon( "owner_disconnect" );
        thread maps\mp\agents\dog\_dog_think::destroyonownerdisconnect( self.owner );
    }

    self thread [[ self.watchattackstatefunc ]]();
    thread maps\mp\agents\dog\_dog_think::monitorflash();

    for (;;)
    {
        if ( self.aistate != "melee" && !self.statelocked && maps\mp\agents\dog\_dog_think::readytomeleetarget() && !maps\mp\agents\dog\_dog_think::didpastmeleefail() )
            self scragentbeginmelee( self.curmeleetarget );

        switch ( self.aistate )
        {
            case "idle":
                updatemove();
                break;
            case "move":
                updatemove();
                break;
            case "melee":
                maps\mp\agents\dog\_dog_think::updatemelee();
                break;
        }

        wait 0.05;
    }
}

updatemove()
{
    updatemovestate();
}

updatemovestate()
{
    if ( self.blockgoalpos )
        return;

    self.prevmovestate = self.movestate;
    var_0 = undefined;
    var_1 = 0;
    var_2 = 0;
    var_3 = 500;

    if ( self.bhasbadpath && gettime() - self.lastbadpathtime < var_3 )
    {
        self.movestate = "follow";
        var_1 = 1;
    }
    else
        self.movestate = maps\mp\agents\dog\_dog_think::getmovestate();

    if ( self.movestate == "pursuit" )
    {
        var_0 = maps\mp\agents\dog\_dog_think::getattackpoint( self.enemy );
        var_4 = 0;

        if ( isdefined( self.lastbadpathtime ) && gettime() - self.lastbadpathtime < 3000 )
        {
            if ( distance2dsquared( var_0, self.lastbadpathgoal ) < 16 )
                var_4 = 1;
            else if ( isdefined( self.lastbadpathmovestate ) && self.lastbadpathmovestate == "pursuit" && distance2dsquared( self.lastbadpathultimategoal, self.enemy.origin ) < 16 )
                var_4 = 1;
        }

        if ( !maps\mp\_utility::isreallyalive( self.enemy ) || var_4 || maps\mp\agents\dog\_dog_think::wanttoattacktargetbutcant( 1 ) || maps\mp\agents\dog\_dog_think::didpastpursuitfail( self.enemy ) )
        {
            self.movestate = "follow";
            var_2 = 1;
        }
    }

    maps\mp\agents\dog\_dog_think::setpastpursuitfailed( var_2 );

    if ( self.movestate == "follow" )
    {
        self.curmeleetarget = undefined;
        self.movemode = maps\mp\agents\dog\_dog_think::getfollowmovemode( self.movemode );
        self.barrivalsenabled = 1;
        var_5 = self getpathgoalpos();

        if ( !isdefined( var_5 ) )
            var_5 = self.origin;

        if ( gettime() - self.timeoflastdamage < 5000 )
            var_1 = 1;

        var_6 = distance2dsquared( self.origin, self.wolfgoalpos.origin );

        if ( var_6 < 800 )
            picknewlocation();

        self scragentsetgoalpos( self.wolfgoalpos.origin );

        if ( var_1 == 1 )
        {
            self scragentsetgoalpos( self.origin );
            return;
        }
    }
    else if ( self.movestate == "pursuit" )
    {
        self.curmeleetarget = self.enemy;
        self.movemode = "sprint";
        self.barrivalsenabled = 0;
        self scragentsetgoalpos( var_0 );
    }
}

picknewlocation()
{
    self.wolfgoalpos = common_scripts\utility::getstruct( self.wolfgoalpos.target, "targetname" );
}

get_closest( var_0, var_1, var_2 )
{
    var_3 = var_1[0];
    var_4 = distance( var_0, var_3.origin );

    for ( var_5 = 0; var_5 < var_1.size; var_5++ )
    {
        var_6 = distance( var_0, var_1[var_5].origin );

        if ( var_6 >= var_4 )
            continue;

        var_4 = var_6;
        var_3 = var_1[var_5];
    }

    if ( !isdefined( var_2 ) || var_4 <= var_2 )
        return var_3;

    return undefined;
}

watchattackstate()
{
    self endon( "death" );
    level endon( "game_ended" );

    for (;;)
    {
        if ( self.aistate == "melee" )
        {
            if ( self.attackstate != "melee" )
            {
                self.attackstate = "melee";
                setsoundstate( undefined );
            }
        }
        else if ( self.movestate == "pursuit" )
        {
            if ( self.attackstate != "attacking" )
            {
                self.attackstate = "attacking";
                setsoundstate( "bark", "attacking" );
            }
        }
        else if ( self.attackstate != "warning" )
        {
            if ( maps\mp\agents\dog\_dog_think::wantstogrowlattarget() )
            {
                self.attackstate = "warning";
                setsoundstate( "growl", "warning" );
            }
            else
            {
                self.attackstate = self.aistate;
                setsoundstate( "pant" );
            }
        }
        else if ( !maps\mp\agents\dog\_dog_think::wantstogrowlattarget() )
        {
            self.attackstate = self.aistate;
            setsoundstate( "pant" );
        }

        wait 0.05;
    }
}

setsoundstate( var_0, var_1 )
{
    if ( !isdefined( var_0 ) )
    {
        self notify( "end_dog_sound" );
        self.soundstate = undefined;
        return;
    }

    if ( !isdefined( self.soundstate ) || self.soundstate != var_0 )
    {
        self notify( "end_dog_sound" );
        self.soundstate = var_0;

        if ( var_0 == "bark" )
            thread playbark( var_1 );
        else if ( var_0 == "growl" )
            thread playgrowl( var_1 );
        else if ( var_0 == "pant" )
            thread maps\mp\agents\dog\_dog_think::playpanting();
        else
        {

        }
    }
}

playbark( var_0 )
{
    self endon( "death" );
    level endon( "game_ended" );
    self endon( "end_dog_sound" );

    if ( !isdefined( self.barking_sound ) )
    {
        self playsoundonmovingent( "mine_wolf_bark" );
        self.barking_sound = 1;
        watchbarking();
    }
}

watchbarking()
{
    self endon( "death" );
    level endon( "game_ended" );
    self endon( "end_dog_sound" );
    wait( randomintrange( 4, 6 ) );
    self.barking_sound = undefined;
}

playgrowl( var_0 )
{
    self endon( "death" );
    level endon( "game_ended" );
    self endon( "end_dog_sound" );

    if ( isdefined( self.lastgrowlplayedtime ) && gettime() - self.lastgrowlplayedtime < 3000 )
        wait 3;

    for (;;)
    {
        self.lastgrowlplayedtime = gettime();
        self playsoundonmovingent( "mine_wolf_growl" );
        wait( randomintrange( 3, 6 ) );
    }
}
