@name The Engineer
@persist Diff Walk Jump BlockWalk Owned Dead Spawned AllowMore FootStepRate HiFive #Numbers
@persist [O]:vector #Vectors
@persist [FootSteps Sound Taunts]:array #Arrays
@persist Target:entity #Entities
@persist Taunt:string #Strings
@model models/props_borealis/bluebarrel001.mdl
if(first()){
 #Array handling
 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" 
    
 Taunts[1,string]="taunt01"    
 Taunts[2,string]="taunt03"
 Taunts[3,string]="taunt06"
 Taunts[4,string]="taunt09"
 Taunts[5,string]="taunt10"
 Taunts[6,string]="taunt_laugh"
 Taunts[7,string]="taunt_replay"
 Taunts[8,string]="taunt_highFiveStart"

 runOnTick(1)
 Diff = 45
 
 entity():setColor(0,0,0,0)
 entity():propFreeze(0)
 
 holoCreate(1)
 holoModel(1,"models/player/engineer.mdl")
 holoPos(1,entity():toWorld(vec(0,0,-Diff)))
 holoParent(1,entity())
 holoAnim(1,"Stand_LOSER")

 timer("spawn",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())   
}
}

if(clk("spawn")){
 stoptimer("spawn")
 Spawned = 1   
}

if(Spawned){
 if(!Dead){
    #Timers
    
    
    #Entity detection
    if(!Owned){
     if(findCanQuery()){
     findByClass("player")
     findSortByDistance(entity():pos())
     if(find():pos():distance(entity():pos())<500){
        Target = find()
        print("[Engineer]: Fonud target, "+Target:name())
        Owned = 1
    }   
    }   
    }
    
    #Rangers
    Ranger = rangerOffset(Diff,entity():pos(),vec(0,0,-1))
    
    if(Ranger:hit()&!Jump){
    V = Ranger:pos()+vec(0,0,Diff)+O
    
     if(Owned){
        if(!BlockWalk){
        Ang = ang(0,(Target:pos()-entity():pos()):toAngle():yaw(),0)
        }else{
        Ang = ang(0,entity():angles():yaw(),0)
        }
    }else{ Ang = ang(0,entity():angles():yaw(),0) }
    
    entity():goto(V)
    entity():turnTo(Ang)
    }
    
    if(changed(Ranger:hit())){
     if(Ranger:hit()){
    BlockWalk = 0
    stoptimer("finishtaunt")
    if(Walk){
     holoAnim(1,"Run_LOSER")
    }else{
     holoAnim(1,"Stand_LOSER")
    }
    }
     if(!Ranger:hit()){
    holoAnim(1,"Airwalk_LOSER")
    }   
    }
    
    #Jumping
    rangerFilter(players())
    FRang = rangerOffset(100,entity():pos(),entity():forward())
    if(FRang:hit()){
     Jump = 1   
    }else{ Jump = 0 }
    
    if(changed(Jump)){
     if(Jump){
     applyForce(entity():up()*15000)   
    }   
    }
    
    #Walking
    if(Owned){
     
    if(entity():pos():distance(Target:pos())>75){
        Walk = 1
    }else{Walk = 0}
    
    if(changed(Walk)&!BlockWalk){
        if(Walk){
        holoAnim(1,"Run_LOSER")
        stoptimer("taunt")
        }
        
        if(!Walk){
        holoAnim(1,"Stand_LOSER")   
        timer("taunt",random(2000,3000))
        }
    }
    
    if(Walk&!BlockWalk){
        holoSetPose(1,"move_x",entity():vel():length()/250)
        O = entity():forward()*10
    }else{ O = entity():forward()*0 }
       
    }
    
    #Taunting
    if(!Walk){
     if(clk("taunt")){
     stoptimer("taunt")
     BlockWalk = 1   
     Taunt = Taunts[round(random(1,Taunts:count())),string]
     holoAnim(1,Taunt)
     timer("finishtaunt",holoAnimLength(1)*1000)
    }
    }
    
    if(clk("finishtaunt")){
     stoptimer("finishtaunt")
     if(Taunt!="taunt_highFiveStart"){
     BlockWalk = 0
     if(Walk){
     holoAnim(1,"Run_LOSER")
    }else{
    holoAnim(1,"Stand_LOSER")
    timer("taunt",random(2000,3000))
    }
    }else{
    if(!HiFive){
    holoAnim(1,"taunt_highFiveStart_idle")
    }
    timer("hftimeout",4000)
    }
    }   
    
    if(BlockWalk){
    if(clk("hftimeout")){
     stoptimer("hftimeout")
     timer("taunt",random(2000,3000))
     BlockWalk = 0
    if(Walk){
     holoAnim(1,"Run_LOSER")
    }else{
     holoAnim(1,"Stand_LOSER")
    }
    }else{
     if(Taunt=="taunt_highFiveStart"){
     if(Target:aimEntity()==entity()&changed(Target:keyUse())&Target:keyUse()&!HiFive){
     stoptimer("hftimeout")
     HiFive = 1
     holoAnim(1,"taunt_hifiveSuccessFull")
     timer("finishhf",holoAnimLength(1)*1000)
    }
    }
    }
    
    if(clk("finishhf")){
     stoptimer("finishhf")
     BlockWalk = 0
    if(Walk){
     holoAnim(1,"Run_LOSER")
    }else{
     holoAnim(1,"Stand_LOSER")
     timer("taunt",random(2000,3000))
    }
    }
    
    }
}


   
}
