@name The Demoman
@persist Diff Walk Jump Dead Owned Spawned AllowMore #Numbers
@persist O:vector #Vectors
@persist [Sound FootSteps]:array #Arrays
@persist [Grenade B Target Nade NadeB]:entity #Entities
@model models/props_borealis/bluebarrel001.mdl
if(first()){
 #Array
 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"

 Sound[1,string]="/vo/mvm/norm/demoman_mvm_gibberish01.mp3"
 Sound[2,string]="/vo/mvm/norm/demoman_mvm_gibberish02.mp3"
 Sound[3,string]="/vo/mvm/norm/demoman_mvm_gibberish03.mp3"
 Sound[4,string]="/vo/mvm/norm/demoman_mvm_gibberish04.mp3"
 Sound[5,string]="/vo/mvm/norm/demoman_mvm_gibberish05.mp3"
 Sound[6,string]="/vo/mvm/norm/demoman_mvm_gibberish06.mp3"
 Sound[7,string]="/vo/mvm/norm/demoman_mvm_gibberish07.mp3"
 Sound[8,string]="/vo/mvm/norm/demoman_mvm_gibberish08.mp3"
 Sound[9,string]="/vo/mvm/norm/demoman_mvm_gibberish09.mp3"
 Sound[10,string]="/vo/mvm/norm/demoman_mvm_gibberish10.mp3"
 Sound[11,string]="/vo/mvm/norm/demoman_mvm_gibberish11.mp3"
 Sound[12,string]="/vo/mvm/norm/demoman_mvm_gibberish12.mp3"
 Sound[13,string]="/vo/mvm/norm/demoman_mvm_gibberish13.mp3"
 soundPlay(1,0,Sound[round(random(1,Sound:count())),string])
 timer("sound",random(7000,10000))
 #Checking to see if another demo is spawned
 if(hostname():find("TylerB")){
 AllowMore = 0  
}else{AllowMore=0}
if(AllowMore){
 setName("@demo_spawned")
 findByClass(entity():type())
 E2s = findToArray()
 for(I=1,E2s:count()){
 E2 = E2s[I,entity]
 if(E2:getName()=="@demo_spawned"&E2:owner()==owner()){
 print("[DemoMan]: You already have a demoman spawned")
 selfDestruct()
}   
}
}
 #Creating holos, etc
 timer("spawn",500)
 timer("nade",random(10000,20000))
 soundPlay(2,0,"/mvm/giant_demoman/giant_demoman_loop.wav")
 soundVolume(2,0.75)
 entity():propFreeze(0)
 entity():setColor(0,0,0,0)
 runOnTick(1)
 Diff = 52
 holoCreate(1)
 holoModel(1,"models/bots/demo/bot_demo.mdl")
 holoPos(1,entity():toWorld(vec(0,0,-Diff)))
 holoParent(1,entity())
 holoAnim(1,"Stand_MELEE")
 holoCreate(2)
 holoModel(2,"models/weapons/w_models/w_bottle.mdl")
 holoPos(2,holoEntity(1):attachmentPos("eye_1"))
 holoPos(2,holoEntity(2):toWorld(vec(-1,0,-5)))
 holoAng(2,holoEntity(1):attachmentAng("eye_1")+ang(-20,90,0))
 holoParentAttachment(2,holoEntity(1),"eye_1")
 #Functions
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("nade")){
 stoptimer("nade")
 timer("nade",random(10000,20000))
 Nade = propSpawn("models/weapons/w_models/w_grenade_grenadelauncher.mdl",entity():toWorld(vec(-20,5,30)),ang(90,0,0),0)
 Nade:applyForce((entity():up()*2000)-(entity():forward()*1000))
 Nade:applyAngForce(ang(random(1,360),random(1,360),random(1,360)))
 Nade:setTrails(10,1,0.5,"trails/smoke",vec(255,0,0),255)
 timer("expl",5000)
 soundPlay(4,0,"/weapons/grenade_launcher_shoot.wav")
}

if(clk("expl")){
 stoptimer("expl")
 NadeB = propSpawn("models/props_phx/misc/potato_launcher_explosive.mdl",Nade:pos(),1)
 NadeB:propBreak()
 Nade:propDelete()   
}

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

if(clk("sound")){
 stoptimer("sound")
 timer("sound",random(7000,10000))
 soundPlay(1,0,Sound[round(random(1,Sound:count())),string])
}

#Finding a target
if(Spawned){
if(!Owned){
 if(findCanQuery()){
    findByClass("player")
    findSortByDistance(entity():pos())
    if(find():pos():distance(entity():pos())<375){
        Owned = 1
        Target = find()
        print("[DemoBot]: Found target, "+Target:name())
}
}   
}
}

if(!Dead){
#Death control
if(Spawned){
if(entity():isOnFire()|entity():angles():pitch()>85||entity():angles():pitch()<-85||entity():angles():roll()>85||entity():angles():roll()<-85|entity():isUnderWater()){
 Dead = 1   
}
}

#Ranger and functions
if(Spawned){
Ranger = rangerOffset(Diff,entity():pos(),vec(0,0,-1))
if(Ranger:hit()&!Jump){
 V = Ranger:pos()+vec(0,0,Diff)+O
 entity():goto(V)   
 if(Owned){
 Ang = ang(0,(Target:pos()-entity():pos()):toAngle():yaw(),0)
 entity():turnTo(Ang)
}else{entity():turnTo(ang())}
}
}

#Moving
if(Owned){
if(entity():pos():distance(Target:pos())>100){
 Walk = 1   
}else{Walk = 0}

if(Walk){
    holoSetPose(1,"move_x",entity():vel():length()/250)
    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){
 holoAnim(1,"Run_MELEE")  
 timer("step1",300) 
}   
 if(!Walk){
 holoAnim(1,"Stand_MELEE")   
 stoptimer("step1")
 stoptimer("step2")
 stoptimer("step3")
}
}
}

#Jumping
rangerFilter(Target)
FRang = rangerOffset(100,entity():pos(),entity():forward())
if(FRang:hit()){
 Jump = 1   
}else{
Jump = 0
}
if(changed(Jump)){
 if(Jump){
 entity():applyForce(entity():up()*35000)
 soundPlay(3,0,"/mvm/sentrybuster/mvm_sentrybuster_step_0"+round(random(1,4))+".wav")   
}   
}
}


if(changed(Dead)){
 if(Dead){
 setName("@demo_dead")
 stoptimer("sound")
 stoptimer("nade")
 soundStop(1)
 soundStop(2)
 soundPlay(3,0,"/vo/mvm/norm/demoman_mvm_paincrticialdeath0"+round(random(1,5))+".mp3")
 holoSetPose(1,"mode_scale",0)
 holoAnim(1,"Airwalk_LOSER",0,0)
 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()
 Nade:propDelete()
 selfDestruct()
}
}
