@name AP Defense Turret
@inputs Cam:wirelink Pod:entity
@persist Num NoMount Owned Player:entity Missile:entity Vel AllowShoot CurPos:vector Seat:entity
if(first()){
 soundPlay(426,0,"/common/warning.wav")
 entity():setColor(0,0,0,0)
 runOnTick(1)
 runOnChat(1)
 holoCreate(1)
 holoModel(1,"models/props_combine/combine_mortar01b.mdl")   
 holoCreate(3)
 holoPos(3,entity():pos()+vec(0,0,55))
 holoModel(3,"hq_sphere")
 holoCreate(2)
 holoModel(2,"hq_tube")
 holoScale(2,vec(0.75,0.75,3))
 holoPos(2,entity():pos()+vec(0,0,50))
 holoPos(2,holoEntity(3):toWorld(vec(0,0,-15)))
 holoParent(2,3)
 holoCreate(4)
 holoModel(4,"models/Items/combine_rifle_ammo01.mdl")
 #holoColor(1,vec(50,50,50))
 holoColor(2,vec(50,50,50))
 holoColor(3,vec(50,50,50))
 holoColor(4,vec(50,50,50))
 #holoMaterial(1,"models/shiny")
 holoMaterial(2,"models/shiny")
 holoMaterial(3,"models/shiny")
 holoMaterial(4,"models/shiny")
 if(entity():isWeldedTo()!=noentity()&entity():isWeldedTo():isVehicle()){
    holoCreate(5)
    holoModel(5,"hq_sphere")
    holoScale(5,vec(2,2,2))
    holoColor(5,vec(255,100,0),100)
    NoMount = 0
    print("Vehicle detected, mounting...")
    Seat = entity():isWeldedTo()
} else { NoMount = 1 }
 Vel = 100
 AllowShoot = 1
}
if(!NoMount){
 if(chatClk(owner())){
 if(owner():lastSaid()=="/kill"){
 hideChat(1)
 Seat:killPod()   
}   
}
 holoVisible(5,players(),0)
 holoVisible(5,Player,1)
 holoPos(5,Player:aimPos())
 Player = Seat:driver()   
}
#Missile shit below
if(NoMount){
if(findCanQuery()){
 findByClass("player")
 Potato = findClosest(entity():pos()):team()==owner():team()||findClosest(entity():pos()):steamID()=="STEAM_0:1:19680709"
 if(findClosest(entity():pos()):pos():distance(entity():pos())<75&Potato&findClosest(entity():pos()):getMaterial()!="models/props_combine/com_shield001a"){
    Player = findClosest(entity():pos())
    Owned = 1
} else {
Player = noentity()
#Num = 0 
Owned = 0
}
if(changed(Owned)&Owned){
    soundPlay(1,0,"/buttons/blip2.wav")
    timer("InitSound",500)   
}
}
#End Mount Shit Here
}
Pos = Player:aimPos()
#Shoot = Player:keyAttack1()
if(changed(Player:keyAttack1())&Player:keyAttack1()&AllowShoot&Player:team()==owner():team()&Player:isAlive()){
    CurPos = Player:aimPos()
    Missile = propSpawn("models/props_phx/amraam.mdl",holoEntity(2):toWorld(vec(-5,0,-50)),(CurPos-holoEntity(2):pos()):toAngle(),1)
    Vel = CurPos:distance(Missile:pos())/85
    Vel = clamp(Vel,100,1000000)
    soundPlay(23,0,"/weapons/doom_rocket_launcher.wav")
    Missile:setTrails(7.5,7.5,7.5,"trails/smoke",vec(75,75,75),255)
    timer("Shoot",750)
    AllowShoot = 0
}
if(clk("Shoot")){
 stoptimer("Shoot")
 soundStop(23)   
}
if(Missile!=noentity()&Missile:pos()!=vec()){
    Missile:setPos(Missile:pos()+Missile:forward()*Vel)
    if(Missile:pos():distance(CurPos)<250|Player:keyAttack2()){
     findByClass("player")
     findSortByDistance(Missile:pos())
     timer("break",20)
     soundPlay(32,0,"/buttons/button19.wav")
     timer("AlwSh",1000)
    }
} else { AllowShoot = 1 }
if(clk("AlwSh")){
 stoptimer("AlwSh")
 soundStop(32)   
}
if(clk("InitSound")){
 stoptimer("InitSound")
 soundStop(1)   
}
Num = Num+3
holoPos(1,entity():pos())
holoAng(1,ang(0,(Pos-holoEntity(1):pos()):toAngle():yaw()-180,0))
holoPos(3,entity():pos()+vec(0,0,55))
#holoPos(2,holoEntity(3):toWorld(vec(0,0,-10)))
holoAng(3,(Pos-holoEntity(2):pos()):toAngle()-ang(90,0,0))
holoPos(4,holoEntity(3):toWorld(vec(0,0,0)))
holoAng(4,holoEntity(3):toWorld(ang(0,Num,0)))

if(dupefinished()){
 reset()   
}

if(clk("break")){
 stoptimer("break")
 Missile:propBreak()
}
