@name The Spy
@persist Diff Dead Found CE Walk Jump Ready
@persist [A]:angle
@persist [V O]:vector
@persist Data:table
@persist Models:array
@persist [Prop T]:entity
@model models/props_borealis/bluebarrel001.mdl
if(first())
{
    Data["Stand",string]="ACT_HL2MP_IDLE_MELEE"
    Data["Walk",string]="ACT_HL2MP_RUN_MELEE"
    Data["SWalk",string]="RUN_MELEE"
    Diff = 52
    
    timer("red",500)
    
    Models = array("models/player/Kleiner.mdl","models/player/Phoenix.mdl","models/player/arctic.mdl","models/player/barney.mdl","models/player/breen.mdl","models/player/charple.mdl","models/player/combine_soldier.mdl","models/player/combine_super_soldier.mdl","models/player/police.mdl")
    
    runOnTick(1)
    
    holoCreate(1)
    holoModel(1,"models/bots/spy/bot_spy.mdl")
    holoPos(1,entity():toWorld(vec(0,0,-Diff)))
    holoParent(1,entity())
    holoAnim(1,"Stand_MELEE")
    holoSkin(1,round(random(0,1)))
    
    #holoCreate(2)
    holoPos(2,holoEntity(1):bone(16):pos())
    #holoParent(2,holoEntity(1):bone(16))
    
    entity():setColor(0,0,0,0)
    entity():propFreeze(0)
    
    function entity:turnTo(Tarang:angle)
    {
        V = This:toLocal(rotationVector(quat(Tarang)/quat(This))+This:pos())
        This:applyTorque((100*V - 30*This:angVelVector())*This:inertia()/3)
    }
    function entity:goto(Vector:vector)
    {
        This:applyForce(((Vector-This:pos())*25-This:vel())*This:mass())   
    }
}

if(clk("red"))
{
    stoptimer("red")
    Ready = 1
}

if(!Dead)
{
    R = rangerOffset(Diff+5,entity():pos(),vec(0,0,-1))
    if(R:hit()&!Jump)
    {
        O = entity():forward()*(10-(CE*2.75))*Walk
        V = R:pos()+vec(0,0,Diff)+O
        entity():goto(V)
    }
    
    rangerFilter(T)
    JR = rangerOffset(100,entity():pos()-vec(0,0,25),entity():forward():setZ(0))
    Jump = changed(JR:hit())&JR:hit()
    if(Jump)
    {
        applyForce(entity():up()*12000)
    }
    
    if(!Found)
    {
        T = players()[randint(numPlayers()),entity]
        print("[Spy Bot] has chosen "+T:name())
        Found = 1
    }
    
    if(Found)
    {
        if(R:hit())
        {
            A = ang(0,(T:pos()-entity():pos()):toAngle():yaw(),0)
            entity():turnTo(A)
        }
    }
    
    Walk = entity():pos():distance(T:pos())>200
    if(changed(Walk))
    {
        if(Walk)
        {
            if(CE)
            {
                holoAnim(1,Data["Walk",string])
            }else
            {
                holoAnim(1,Data["SWalk",string])
            }
            stoptimer("wait")
        }else
        {
            if(CE)
            {
                holoAnim(1,Data["Stand",string])
            }
            timer("wait",500)
        }
    }
    holoSetPose(1,"move_scale",entity():vel():length()/250)
    holoSetPose(1,"move_x",1)
    
    if(clk("wait"))
    {
        stoptimer("wait")
        Prop = propSpawn("models/props_phx/ww2bomb.mdl",entity():pos() + vec(0,0,50) + (100 * (T:pos() - entity():pos()):normalized()),0)
        Prop:propBreak()
        Prop:setMass(10^5)
        Prop:setColor(0,0,0,0)
        Prop:goto(T:pos() + vec(0, 0, 50) + (100 * (T:pos() - entity():pos()):normalized()))
        print("[Spy Bot] has attempted to assassinate "+T:name()+". Choosing another target...")
        Found = 0
        timer("del",300)
    }
    if(clk("del"))
    {
        stoptimer("del")
        Prop:propDelete()
        if(changed(T:isAlive())&!T:isAlive())
        {
            soundPlay("kill",5,"/vo/mvm/norm/spy_mvm_laughevil0"+round(random(1,2))+".mp3")
        }
    }
    
    CE = entity():pos():distance(T:pos())<500
    if(changed(CE))
    {
        if(CE)
        {
            MI = round(random(1,Models:count()))
            holoModel(1,Models[MI,string])
            holoAnim(1,Data["Walk",string])
            soundPlay("disguise",4,"/player/spy_disguise.wav")
        }else
        {
            holoModel(1,"models/bots/spy/bot_spy.mdl")
            holoAnim(1,Data["SWalk",string])
            soundPlay("undisguise",4,"/player/spy_uncloak.wav")
        }
    }
    
    if(Ready)
    {
        Dead = !inrange(entity():angles():pitch(),-85,85)||!inrange(entity():angles():roll(),-85,95)||entity():isUnderWater()
    }
}else
{
    if(changed(Dead))
    {
        timer("delete",3000)
        holoModel(1,"models/bots/spy/bot_spy.mdl")
        holoAnim(1,"dieviolent",0,0.3)
        soundPlay("ded",1.5,"/vo/mvm/norm/spy_mvm_painsevere0"+round(random(1,5))+".mp3")
    }
    
    if(clk("delete"))
    {
        TE = propSpawn("models/props_phx/ww2bomb.mdl",entity():pos(),1)
        TE:propBreak()
        selfDestruct()
    }
}
#[
if(entity():isPlayerHolding())
{
    TE = propSpawn("models/props_phx/ww2bomb.mdl",entity():pos(),1)
    TE:propBreak()
    selfDestruct()
}
]#
