@name The Sniper
@persist Walk Jump Dead Owned AllowMore Spawned Diff MaxDist MinDist Taunt #Numbers
@persist O:vector #Vecotrs
@persist Target:entity
@persist [Sound FootSteps]:array #Arrays
@model models/props_borealis/bluebarrel001.mdl
if(first()){    
 FootSteps[1,string]="/mvm/player/footsteps/robostep_01.wav"
 FootSteps[2,string]="/mvm/player/footsteps/robostep_02.wav"
 FootSteps[3,string]="/mvm/player/footsteps/robostep_03.wav"
 FootSteps[4,string]="/mvm/player/footsteps/robostep_04.wav"
 FootSteps[5,string]="/mvm/player/footsteps/robostep_05.wav"
 FootSteps[6,string]="/mvm/player/footsteps/robostep_06.wav"
 FootSteps[7,string]="/mvm/player/footsteps/robostep_07.wav"
 FootSteps[8,string]="/mvm/player/footsteps/robostep_08.wav"
 FootSteps[9,string]="/mvm/player/footsteps/robostep_09.wav"
 FootSteps[10,string]="/mvm/player/footsteps/robostep_10.wav"
 FootSteps[11,string]="/mvm/player/footsteps/robostep_11.wav"
 FootSteps[12,string]="/mvm/player/footsteps/robostep_12.wav"
 FootSteps[13,string]="/mvm/player/footsteps/robostep_13.wav"
 FootSteps[14,string]="/mvm/player/footsteps/robostep_14.wav"
 FootSteps[15,string]="/mvm/player/footsteps/robostep_15.wav"
 FootSteps[16,string]="/mvm/player/footsteps/robostep_16.wav"
 FootSteps[17,string]="/mvm/player/footsteps/robostep_17.wav"
 FootSteps[18,string]="/mvm/player/footsteps/robostep_18.wav"
    
 if(hostname():find("TylerB")){
 AllowMore = 1 
}else{AllowMore=0}
if(!AllowMore){
 setName("hue")
 findByClass(entity():type())
 E2s = findToArray()
 for(I=1,E2s:count()){
 E2 = E2s[I,entity]
 if(E2:getName()=="@sniper_spawned"&E2:owner()==owner()){
 print("[SniperBot]: You already have a soldier spawned")
 selfDestruct()
}   
}
}
 MaxDist = 1500
 MinDist = 1450
 runOnTick(1)
 Diff = 52
 entity():propFreeze(0)
 entity():setColor(0,0,0,0)
 holoCreate(1)
 holoModel(1,"models/bots/sniper/bot_sniper.mdl")
 holoPos(1,entity():toWorld(vec(0,0,-Diff)))
 holoParent(1,entity())
 holoAnim(1,"Stand_MELEE")
 timer("spawned",250)
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())   
}
}

#Timers
if(clk("spawned")){
 stoptimer("spawned")
 Spawned = 1   
}

if(Spawned){
    if(!Owned){
     findByClass("player")
     findSortByDistance(entity():pos())
     Target = find()
     print("[SniperBot]: Choosing player, "+Target:name())
     Owned = 1
    }
    if(!Dead){
        
    #Death detecting
    if(entity():isOnFire()|entity():angles():pitch()>85||entity():angles():pitch()<-85||entity():angles():roll()>85||entity():angles():roll()<-85|entity():isUnderWater()){
     Dead = 1   
    }
    
    #Rangers & Angling
    Ranger = rangerOffset(Diff,entity():pos(),vec(0,0,-1))
    if(Ranger:hit()&!Jump){
     V = Ranger:pos()+vec(0,0,Diff)+O
    if(Owned){
     if(Walk==0|Walk==1){
        Ang = ang(0,(Target:pos()-entity():pos()):toAngle():yaw(),0)
    }elseif(Walk == 2){
        Ang = ang(0,-(Target:pos()-entity():pos()):toAngle():yaw(),0)
    }
    }else{Ang=ang(0,entity():angles():yaw(),0)}
    entity():goto(V)
    entity():turnTo(Ang)
    }
    
    #Moving
    if(Owned){
    if(!Taunt){
    if(Walk>0){
    holoSetPose(1,"move_scale",entity():vel():length()/250)
    }
    
    if(entity():pos():distance(Target:pos())>MaxDist){
     Walk = 1   
    }elseif(entity():pos():distance(Target:pos())<MinDist){
     Walk = 2
    }elseif(entity():pos():distance(Target:pos())<MaxDist&entity():pos():distance(Target:pos())>MinDist){
     Walk = 0
    }
    
    if(Walk>0){   
    O=entity():forward()*10
    if(clk("step1")){
    stoptimer("step1")
    timer("step2",300)
    if(Ranger:hit()){
    soundPlay(10,0,FootSteps[round(random(1,FootSteps:count())),string]) 
    }  
    }
    if(clk("step2")){
    stoptimer("step2")
    timer("step3",300)
    if(Ranger:hit()){
    soundPlay(11,0,FootSteps[round(random(1,FootSteps:count())),string])
    }
    }
    if(clk("step3")){
    stoptimer("step3")
    timer("step1",300)
    if(Ranger:hit()){
    soundPlay(12,0,FootSteps[round(random(1,FootSteps:count())),string])
    }
    }
    }else{
    O=entity():forward()*0
    }
    }
    
    if(changed(Walk)){
     if(Walk>0){
     holoAnim(1,"Run_MELEE")  
     timer("step1",300) 
    }
     if(!Walk){
     holoAnim(1,"Stand_MELEE") 
     stoptimer("step1")  
     stoptimer("step2")
     stoptimer("step3")
    }
    }
    }
    
    #Jumping
    FRang = rangerOffset(100,Ranger:pos()+vec(0,0,20),entity():forward())
    if(FRang:hit()){
     Jump = 1   
    }else{Jump = 0}
    
    if(changed(Jump)){
     if(Jump){
        applyForce(entity():up()*35000)
        soundPlay(2,0,"/mvm/sentrybuster/mvm_sentrybuster_step_0"+round(random(1,4))+".wav")   
    }   
    }
    
    #Taunting
    if(Target:aimEntity()==entity()&!Walk&Taunt==0){
     Taunt = 1
     holoAnim(1,round(random(1,3)):toString():replace("1","taunt01"):replace("2","taunt03"):replace("3","taunt04"))
     timer("taunt",holoAnimLength(1)*1000)   
    }
    if(clk("taunt")){
     stoptimer("taunt")
     Taunt = 0   
     holoAnim(1,"Stand_MELEE")
    }
}
#Death handling
if(changed(Dead)){
 if(Dead){
    setName("@sniper_dead")
    stoptimer("snd")
    soundStop(1)
    soundStop(2)
    soundStop(3)
    soundPlay(4,0,"/vo/mvm/norm/sniper_mvm_paincrticialdeath0"+round(random(1,4))+".mp3")
    holoSetPose(1,"mode_scale",0)
    holoAnim(1,"dieviolent",0,0.5)
    timer("delete",3000)
    holoCreate(3)
    holoModel(3,"models/buildables/sapper_dispenser.mdl")
    holoAlpha(3,0)
    holoParent(3,entity())
    holoCreate(4)
    holoModel(4,"models/buildables/sapper_sentry3.mdl")
    holoPos(4,holoEntity(3):toWorld(vec(0,0,-10)))
    holoAlpha(4,0)
    holoParent(4,entity())  
}   
}
if(Dead){
 if(clk("delete")){
 B=propSpawn("models/props_phx/ww2bomb.mdl",entity():pos(),1)
 B:propBreak()
 selfDestruct()   
}   
}
}
