@name Crow
@persist Model:string
@persist Found Turn Walk
@persist O:vector
@model models/hunter/plates/plate.mdl
if(first()){
 entity():setColor(0,0,0,0)
 entity():propFreeze(0)
 entity():setMass(25)
 runOnTick(1)
 holoCreate(1)
 holoModel(1,"models/crow.mdl")
 holoAnim(1,"Idle01")
 holoPos(1,entity():toWorld(vec(0,0,-3)))
 holoParent(1,entity())
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-entity():vel())*entity():mass())   
}
Model = "models/props/cs_italy/orange.mdl"
Turn = random(1,360)
timer("a",random(5000,7500))
timer("snd",random(3000,5000))
}

if(clk("snd")){
 stoptimer("snd")
 timer("snd",random(3000,5000))
 soundPlay(1,0,"/npc/crow/alert"+round(random(2,3))+".wav")   
}

if(clk("a")){
 stoptimer("a")
 timer("a",random(5000,7500))
 Turn = random(1,360)
}

if(findCanQuery()){
 findByModel(Model)   
 findSortByDistance(entity():pos())
 if(find():isValid()){
 Found = 1   
}else{ Found = 0 }
}

if(Found){
 Ang = ang(0,(find():pos()-entity():pos()):toAngle():yaw(),0)
 if(find():pos():distance(entity():pos())>5){
 Walk = 1
}else{Walk = 0}
}else{
 Ang = ang(0,Turn,0)
 Walk = 1
}

O = entity():forward()*Walk/2

if(changed(Walk)){
 if(Walk){
 holoAnim(1,"Walk")
 }
 if(!Walk){
 holoAnim(1,"Eat_A")  
}
}

RD = rangerOffset(5,entity():pos(),vec(0,0,-1))
if(RD:hit()){
 entity():goto((RD:pos()+vec(0,0,5))+O)
}

TA = HA = (holoEntity(1):angles())
    
GroundAng = angnorm(RD:hitNormal():toAngle() + ang(90, 0, 0))
P = GroundAng:pitch(), Y = GroundAng:yaw() - HA:yaw()
TA = ang(cos(Y) * P, 0, -sin(Y) * P)

entity():turnTo(Ang+TA)
