@name The Medic
@persist Diff Dead Jump Walk Spawned Owned AllowMore #Numbers
@persist O:vector #Vectors
@persist [Sound ]:array #Arrays
@persist Target:entity #Entities
@model models/props_borealis/bluebarrel001.mdl
if(first()){
 Sound[1,string]="/vo/mvm/norm/medic_mvm_medic01.mp3"
 Sound[2,string]="/vo/mvm/norm/medic_mvm_medic02.mp3"
 Sound[3,string]="/vo/mvm/norm/medic_mvm_medic03.mp3"
 Sound[4,string]="/vo/mvm/norm/demoman_mvm_medic01.mp3"
 Sound[5,string]="/vo/mvm/norm/demoman_mvm_medic02.mp3"
 Sound[6,string]="/vo/mvm/norm/demoman_mvm_medic03.mp3"
 Sound[7,string]="/vo/mvm/norm/heavy_mvm_medic01.mp3"
 Sound[8,string]="/vo/mvm/norm/heavy_mvm_medic02.mp3"
 Sound[9,string]="/vo/mvm/norm/heavy_mvm_medic03.mp3"
 Sound[10,string]="/vo/mvm/norm/pyro_mvm_medic01.mp3"
 Sound[11,string]="/vo/mvm/norm/scout_mvm_medic01.mp3"
 Sound[12,string]="/vo/mvm/norm/scout_mvm_medic02.mp3"
 Sound[13,string]="/vo/mvm/norm/scout_mvm_medic03.mp3"
 Sound[14,string]="/vo/mvm/norm/sniper_mvm_medic01.mp3"
 Sound[15,string]="/vo/mvm/norm/sniper_mvm_medic02.mp3"
 Sound[16,string]="/vo/mvm/norm/soldier_mvm_medic01.mp3"
 Sound[17,string]="/vo/mvm/norm/soldier_mvm_medic02.mp3"
 Sound[18,string]="/vo/mvm/norm/soldier_mvm_medic03.mp3"
 Sound[19,string]="/vo/mvm/norm/spy_mvm_medic01.mp3"
 Sound[20,string]="/vo/mvm/norm/spy_mvm_medic02.mp3"
 Sound[21,string]="/vo/mvm/norm/spy_mvm_medic03.mp3"

 if(hostname():find("TylerB")){
 AllowMore = 1 
}else{AllowMore=0}
if(!AllowMore){
 setName("@medic_spawned")
 findByClass(entity():type())
 E2s = findToArray()
 for(I=1,E2s:count()){
 E2 = E2s[I,entity]
 if(E2:getName()=="@medic_spawned"&E2:owner()==owner()){
 print("[Medic]: You already have a medic spawned")
 selfDestruct()
}   
}
}
 Diff = 52
 runOnTick(1)   
 holoCreate(1)
 entity():setColor(0,0,0,0)
 entity():propFreeze(0)
 holoPos(1,entity():toWorld(vec(0,0,-Diff)))
 holoModel(1,"models/bots/medic/bot_medic.mdl")
 holoAnim(1,"Stand_MELEE")
 holoParent(1,entity())
 timer("start",250)
 timer("snd",random(5000,7500))
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("snd")){
 stoptimer("snd")
 timer("snd",random(5000,7500))
 soundPlay(1,0,Sound[round(random(1,Sound:count())),string])
}

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

if(Spawned){
if(!Dead){
    #Check if dead
    if(entity():isOnFire()|entity():angles():pitch()>85||entity():angles():pitch()<-85||entity():angles():roll()>85||entity():angles():roll()<-85|entity():isUnderWater()){    
        Dead = 1
    }
    
    #Target finding
    if(!Owned){
     findByClass("player")
     findSortByDistance(entity():pos())
     if(find():pos():distance(entity():pos())<375){
     Owned = 1
     Target = find()
     print("[Medic]: found target, "+Target:name())   
    }
    }
    
    #Ranger
    Ranger = rangerOffset(Diff,entity():pos(),vec(0,0,-1))
    if(Ranger:hit()&!Jump){
        V = Ranger:pos()+vec(0,0,Diff)+O
        if(Owned){
        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(entity():pos():distance(Target:pos())>75){
     Walk = 1   
    }else{
     Walk = 0
    }
    if(Walk){
     O=entity():forward()*clamp(entity():pos():distance(Target:pos())/40,0,50)
     holoSetPose(1,"move_scale",entity():vel():length()/250)   
     holoSetPose(1,"move_x",entity():vel():length()/250)
    }else{
     O=entity():forward()*0
    }
    }
    
    #Jumping
    rangerFilter(Target)
    FRang = rangerOffset(100,Ranger:pos()+vec(0,0,30),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")   
    }   
    }
    
    #Anim controlling sounds
    soundPitch(3,entity():vel():length())
    if(changed(Walk)){
     if(Walk){
     soundPlay(3,0,"/vehicles/diesel_loop2.wav")
     holoAnim(1,"Run_MELEE")   
    }   
     if(!Walk){
     holoAnim(1,"Stand_MELEE")   
    }
    }
    
}
    #Death handling
    if(changed(Dead)){
    if(Dead){
    setName("@medic_dead")
    stoptimer("snd")
    soundStop(1)
    soundStop(2)
    soundStop(3)
    soundPlay(4,0,"/vo/mvm/norm/medic_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()   
    }
    }
}
