@name The Sentry Buster
@persist Walk Diff Explode Dead Found Activated
@persist V:vector
@persist Ang:angle
@persist E:entity
@model models/props_borealis/bluebarrel001.mdl
if(first())
{
    runOnTick(1)
    
    Diff = 52
    
    function entity:angTo(A:angle)
    {
        This:applyAngForce((angnorm(A-This:angles())*5-This:angVel())*This:mass())
    }
    function entity:goto(Vector:vector)
    {
        This:applyForce(((Vector-This:pos())*25-This:vel())*This:mass())   
    }
    
    holoCreate(1)
    holoModel(1,"models/bots/demo/bot_sentry_buster.mdl")
    holoPos(1,entity():toWorld(vec(0,0,-Diff)))
    holoParent(1,entity())
    holoAnim(1,"SECONDARY_stun_middle")
    
    entity():setColor(0,0,0,0)
    entity():propFreeze(0)
}

if(!Dead)
{
    if(!Found)
    {
        findByClass("player")
        findSortByDistance(entity():pos())
        if(find():pos():distance(entity():pos())<500)
        {
            E = find()
            print("Sentry Buster #"+entity():id()+" has found "+E:name())
            Found = 1
            soundPlay("intro",1,"/mvm/sentrybuster/mvm_sentrybuster_intro.wav")
            timer("act",450)
        }
    }
    if(clk("act"))
    {
        stoptimer("act")
        Activated = 1
        soundPlay("loop",0,"/mvm/sentrybuster/mvm_sentrybuster_loop.wav")
    }
    
    if(Activated)
    {
        Walk = (entity():pos():distance(E:pos())>150)*!Explode
    }
    
    JR = rangerOffset(60,entity():pos()-vec(0,0,25),vec(entity():forward():x(),entity():forward():y(),0))
    
    Jump = changed(JR:hit()) & JR:hit() & !Explode
    if(Jump)
    {
        applyForce(vec(0,0,40000))
        soundPlay("jump",3,"/mvm/giant_common/giant_common_step_0"+round(random(1,8))+".wav")
    }
    
    R = rangerOffset(Diff+5,entity():pos(),vec(0,0,-1))
    if(R:hit() & !Jump)
    {
        entity():goto((R:pos()+vec(0,0,Diff))+V)
    }
    
    Ang = Activated & !Explode ? (E:pos()-entity():pos()):toAngle():setPitch(0) : entity():angles():setRoll(0):setPitch(0)
    if(R:hit())
    {
        entity():angTo(Ang)
    }
    
    if(changed(R:hit()))
    {
        if(!R:hit())
        {
            holoAnim(1,"Airwalk_ITEM1")
        }elseif(!Explode)
        {
            holoAnim(1,"Run_MELEE")
            
            soundPlay("hitground",0.5,"/mvm/player/footsteps/robostep_"+round(random(1,18))+".wav")
        }
    }
    
    V = entity():forward()*10*Walk
    if(Found)
    {
        if(changed(Walk))
        {
            if(Walk)
            {
                holoAnim(1,"Run_MELEE")
                holoSetPose(1,"move_x",1)
            }else
            {
                holoAnim(1,"Stand_MELEE")
            }
        }
    }
    
    Ded = !inrange(entity():angles(),-ang(90,360,90),ang(90,360,90)) | entity():isUnderWater() | entity():isOnFire()
    if(Ded & Activated)
    {
        Dead = 1
        timer("expl",600)
        soundPlay("ded",3,"/mvm/mvm_deploy_small.wav")
    }
    
    if(Activated & !Explode)
    {
        if(entity():pos():distance(E:pos())<150)
        {
            Explode = 1
            
            holoAnim(1,"sentry_buster_preExplode")
            
            soundStop("loop")
            soundPlay("epxpl",5,"/mvm/sentrybuster/mvm_sentrybuster_spin.wav")
            
            timer("expl",holoAnimLength(1)*1000)
        }
    }
}

if(clk("expl"))
{
    propSpawn("models/props_phx/amraam.mdl",entity():pos(),1):propBreak()
    
    selfDestruct()
}
