@name The Soldier
@persist Walk Jump AllowMore Diff Spawned Dead Owned FootStepRate Shooting #Numbers
@persist [O]:vector #Vectors
@persist [Target Ba]:entity #Entities
@persist [FootSteps Sounds]: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"

 Sounds[1,string]="/vo/mvm/norm/soldier_mvm_robot01.mp3"
 Sounds[2,string]="/vo/mvm/norm/soldier_mvm_robot02.mp3"
 Sounds[3,string]="/vo/mvm/norm/soldier_mvm_robot04.mp3"
 Sounds[4,string]="/vo/mvm/norm/soldier_mvm_robot04.mp3"
 Sounds[5,string]="/vo/mvm/norm/soldier_mvm_robot05.mp3"
 Sounds[6,string]="/vo/mvm/norm/soldier_mvm_robot06.mp3"
 Sounds[7,string]="/vo/mvm/norm/soldier_mvm_robot07.mp3"
 Sounds[8,string]="/vo/mvm/norm/soldier_mvm_robot08.mp3"
 Sounds[9,string]="/vo/mvm/norm/soldier_mvm_robot09.mp3"
 Sounds[10,string]="/vo/mvm/norm/soldier_mvm_robot10.mp3"
 Sounds[11,string]="/vo/mvm/norm/soldier_mvm_robot11.mp3"
 Sounds[12,string]="/vo/mvm/norm/soldier_mvm_robot12.mp3"
 Sounds[13,string]="/vo/mvm/norm/soldier_mvm_robot13.mp3"
 Sounds[14,string]="/vo/mvm/norm/soldier_mvm_robot14.mp3"
 Sounds[15,string]="/vo/mvm/norm/soldier_mvm_robot15.mp3"
 Sounds[16,string]="/vo/mvm/norm/soldier_mvm_robot16.mp3"
 Sounds[17,string]="/vo/mvm/norm/soldier_mvm_robot17.mp3"
 Sounds[18,string]="/vo/mvm/norm/soldier_mvm_robot18.mp3"
 Sounds[19,string]="/vo/mvm/norm/soldier_mvm_robot19.mp3"
 Sounds[20,string]="/vo/mvm/norm/soldier_mvm_robot20.mp3"
 Sounds[21,string]="/vo/mvm/norm/soldier_mvm_robot21.mp3"
 Sounds[22,string]="/vo/mvm/norm/soldier_mvm_robot22.mp3"
 Sounds[23,string]="/vo/mvm/norm/soldier_mvm_robot23.mp3"
 Sounds[24,string]="/vo/mvm/norm/soldier_mvm_robot24.mp3"
 Sounds[25,string]="/vo/mvm/norm/soldier_mvm_robot25.mp3"
 Sounds[26,string]="/vo/mvm/norm/soldier_mvm_robot26.mp3"
 Sounds[27,string]="/vo/mvm/norm/soldier_mvm_robot27.mp3"
 Sounds[28,string]="/vo/mvm/norm/soldier_mvm_robot28.mp3"
 Sounds[29,string]="/vo/mvm/norm/soldier_mvm_robot29.mp3"
     
if(hostname():find("TylerB")|isSinglePlayer()){
 AllowMore = 1 
}else{AllowMore=0}
if(!AllowMore){
 setName("@soldier_spawned")
 findByClass(entity():type())
 E2s = findToArray()
 for(I=1,E2s:count()){
 E2 = E2s[I,entity]
 if(E2:getName()=="@soldier_spawned"&E2:owner()==owner()){
 print("[SoldierBot #"+entity():id()+"]: You already have a soldier spawned")
 selfDestruct()
}   
}
}
    
    runOnTick(1)
    soundPlay(2,0,"/mvm/giant_soldier/giant_soldier_loop.wav")
    Diff = 52
    entity():setColor(0,0,0,0)
    entity():propFreeze(0)
    holoCreate(1)
    holoModel(1,"models/bots/soldier/bot_soldier.mdl")
    holoPos(1,entity():toWorld(vec(0,0,-Diff)))
    holoSkin(1,round(random(0,1)))
    holoParent(1,entity())
    holoAnim(1,"Stand_PRIMARY")
    holoCreate(2)
    holoModel(2,"models/weapons/w_models/w_rocketlauncher.mdl")
    holoPos(2,holoEntity(1):attachmentPos("weapon_bone"))
    holoAng(2,holoEntity(1):attachmentAng("weapon_bone")-ang(0,90,90))
    holoParentAttachment(2,holoEntity(1),"weapon_bone")
    holoCreate(50)
    holoModel(50,"hq_sphere")
    holoColor(50,vec(255,100,0))
    holoAlpha(50,0)
    timer("startup",250)
    timer("fr",random(5000,8000))
    timer("snd",random(6000,7500))
    FootStepRate = 375
    
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("startup")){
 stoptimer("startup")
 Spawned = 1   
}

if(Spawned){
 if(!Dead){
    #Timers
    if(clk("snd")){
     stoptimer("snd")
     timer("snd",random(6000,7500))
     soundPlay(1,0,Sounds[round(random(1,Sounds:count())),string])
    }
    
    #Entity Targeting
    if(!Owned){
     if(findCanQuery()){
     findByClass("player")
     findSortByDistance(entity():pos())
     if(find():pos():distance(entity():pos())<1000){
        Owned = 1
        Target = find()
        print("[SoldierBot #"+entity():id()+"]: found target, "+Target:name())
    }   
    }   
    }
    
    #Death Handling
    if(entity():isOnFire()|entity():angles():pitch()>85||entity():angles():pitch()<-85||entity():angles():roll()>85||entity():angles():roll()<-85|entity():isUnderWater()){
     Dead = 1   
    }
    
    #Ranger, AppleFork, and Angling
    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)
    }
    
    if(changed(Ranger:hit())){
     if(Ranger:hit()){
     soundPlay(6,0,FootSteps[round(random(1,FootSteps:count())),string])
    }
    }
    
    #Jumping
    rangerFilter(players())
    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(3,0,"/mvm/sentrybuster/mvm_sentrybuster_step_0"+round(random(1,4))+".wav")
    }   
    }
    
    #Walking & holoSetPose
    if(Owned){
    holoSetPose(1,"body_pitch",-(Target:attachmentPos("eyes")-holoEntity(1):attachmentPos("eye_1")):toAngle():pitch())
    if(entity():pos():distance(Target:pos())>750){
     Walk = 1
    if(clk("step1")){
    stoptimer("step1")
    timer("step2",FootStepRate)
    if(Ranger:hit()){
    soundPlay(10,0,FootSteps[round(random(1,FootSteps:count())),string]) 
    }  
    }
    if(clk("step2")){
    stoptimer("step2")
    timer("step3",FootStepRate)
    if(Ranger:hit()){
    soundPlay(11,0,FootSteps[round(random(1,FootSteps:count())),string])
    }
    }
    if(clk("step3")){
    stoptimer("step3")
    timer("step1",FootStepRate)
    if(Ranger:hit()){
    soundPlay(12,0,FootSteps[round(random(1,FootSteps:count())),string])
    }
    }
    }else{Walk=0}
    
    if(Walk){
     O = entity():forward()*10
     holoSetPose(1,"move_scale",entity():vel():length()/250)
    
    
    }else{
    O=entity():forward()*0
    }
    
    if(changed(Walk)){
     if(Walk){
     holoAnim(1,"Run_PRIMARY")   
     timer("step1",FootStepRate)
    }   
     if(!Walk){
     holoAnim(1,"Stand_PRIMARY")   
     stoptimer("step1")
     stoptimer("step2")
     stoptimer("step3")
    }
    }
    }
    
    #Shooting
    rangerFilter(entity())
    holoAng(50,ang(holoEntity(2):angles():pitch(),entity():angles():yaw(),0))
    AimRang = rangerOffset(1000000000000,holoEntity(1):pos()+vec(0,0,60),holoEntity(50):forward())
    if(clk("fr")){
     stoptimer("fr")
     timer("fr",random(5000,8000))
     if(Target:isAlive()&!Shooting&Owned&!Walk&AimRang:hit()&AimRang:entity():owner()==Target){
     Shooting = 1
     holoCreate(3)
     holoModel(3,"models/weapons/w_models/w_rocket.mdl")
     holoPos(3,holoEntity(2):toWorld(vec(0,0,0)))
     holoAng(3,(Target:attachmentPos("eyes")-holoEntity(2):pos()):toAngle())
     holoEntity(3):setTrails(10,10,1,"trails/smoke",vec(255),255)
     holoEntity(2):soundPlay(7,0,"/weapons/rocket_blackbox_shoot.wav")
     Pos = holoEntity(3):pos()
     Ba=propSpawn("models/props_phx/ww2bomb.mdl",Pos,1)
     Ba:setColor(0,0,0,0)
    }
    }
    holoPos(50,AimRang:pos())
    if(Shooting){
    Ba:setPos(holoEntity(3):pos())
    rangerFilter(Ba)
    MisRang = rangerOffset(20,holoEntity(3):pos(),holoEntity(2):forward())
    holoPos(3,holoEntity(3):pos()+holoEntity(3):forward()*15)
    if(MisRang:hit()|!Ba:isValid()){
     Ba:propBreak()
     holoDelete(3)
     Shooting = 0   
    }    
    }
    
}
if(changed(Dead)){
 if(Dead){
    setName("@soldier_dead")
    stoptimer("snd")
    soundStop(1)
    soundStop(2)
    soundStop(3)
    soundPlay(5,0,"/vo/mvm/norm/soldier_mvm_paincrticialdeath0"+round(random(1,4))+".mp3")
    holoSetPose(1,"mode_scale",0)
    holoAnim(1,"dieviolent",0,0.5)
    timer("delete",3000)
    holoDelete(2)
    holoCreate(3)
    holoEntity(3):removeTrails()
    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()  
}   
}
}
