@name Killgore APC Turret
@inputs [Pod Cam]:wirelink
@persist [MA SA]:array
@persist Lock Welcome View Rint FLIR
if(first())
{
    entity():setColor(0,0,0,0)
    
    runOnTick(1)
    runOnChat(1)
    
    holoCreate(1)
    holoCreate(2)
    holoCreate(3)
    holoCreate(4)
    holoCreate(5)
    
    holoModel(1,"models/props_combine/combine_mortar01b.mdl")
    holoModel(2,"hq_sphere")
    holoModel(3,"hq_tube")
    holoModel(4,"models/Items/combine_rifle_ammo01.mdl")
    holoModel(5,"hq_sphere")
    
    holoPos(2,entity():toWorld(vec(0,0,54)))
    holoPos(3,holoEntity(2):toWorld(vec(0,0,-15)))
    holoPos(4,holoEntity(2):pos())
    
    holoScale(3,vec(0.75,0.75,3))
    
    holoParent(1,entity())
    holoParent(2,1)
    holoParent(3,2)
    holoParent(4,2)
    
    holoColor(2,vec(50))
    holoColor(3,vec(50))
    holoColor(4,vec(30))
    holoColor(5,teamColor(owner():team()),75)
    
    holoMaterial(2,"models/shiny")
    holoMaterial(3,"models/shiny")
    holoMaterial(4,"models/shiny")
    
    holoVisible(5,players(),0)
    
    Rint = 0.05
    
    function beep(S:string)
    {
        if(S:find("[STR-SPLIT]"))
        {
            Str = S:explode("[STR-SPLIT]")
            for(I=1,Str:count())
            {
                FTime = clamp(Str[I,string]:length()/3,2,15) + clamp(Str[I-1,string]:length()/3,2,15)
                Pod["Entity",entity]:hintDriver(Str[I,string],FTime)
            }
        }else
        {
            Time = clamp(S:length()/3,2,15)
            Pod["Entity",entity]:hintDriver(S,Time)
        }
        soundPlay("hint",0.5,"UI/beepclear.wav")
    }
    function fire(A:angle)
    {
        propSpawnEffect(0)
        TE = propSpawn("models/props_phx/amraam.mdl",holoEntity(2):toWorld(vec(-7,0,0)),A+ang(90,0,0),1)
        TE:setTrails(0,100,2,"trails/smoke",vec(255),255)
        MA:pushEntity(TE)
        Lock = 1
        timer("unlock",500)
        soundPlay("fire",0.3,"weapons/boxing_gloves_hit"+round(random(1,4))+".wav")
    }
}
holoAng(4,holoEntity(4):toWorld(ang(0,4,0)))

Seat = Pod["Entity",entity]
Active = Pod["Active",number]
Alt = Pod["Alt",number]
Mwu = Pod["PrevWeapon",number]
Mwd = Pod["NextWeapon",number]
Driver = Seat:driver()

if(changed(Active))
{
    Pod["Crosshairs",number] = Active
    Cam["Activated",number] = Active
    if(Active)
    {
        holoVisible(5,players(),0)
        Welcome = Driver:team()==owner():team()
        if(Welcome)
        {
            beep("Welcome!")
            holoVisible(5,Driver,1)
        }else
        {
            beep("YOU DON'T BELOOOOONG HERE")
            Seat:killPod()
        }
    }else
    {
        View = 0
        FLIR = 0
        if(!first())
        {
            Seat:soundPlay("bai",2,"vo/halloween_merasmus/sf12_leaving0"+round(random(2,3))+".wav")
        }
    }
}
if(Active)
{
    if(changed(Alt)&Alt)
    {
        View = !View
        beep("Third-Person View has been toggled to "+View)
    }
    if(!View)
    {
        Cam["Position",vector] = holoEntity(2):toWorld(vec(10,0,0))
        Cam["Angle",angle] = holoEntity(2):toWorld(ang(90,0,0))
        #Cam["Parent",entity] = holoEntity(2)
    }else
    {
        Cam["Position",vector] = Seat:toWorld(-Driver:eye()*750)
        Cam["Angle",angle] = (Seat:pos()-Seat:toWorld(-Driver:eye()*750)):toAngle()
        #Cam["Parent",entity] = Seat
    }
    
    if(!View)
    {
        HR = rangerOffset(10000000000,holoEntity(2):pos(),Driver:eye())
        holoPos(5,HR:pos())
        Ang = Driver:eyeAngles()-ang(90,0,0)
        holoAng(2,Ang)
        
        if(Driver:keyAttack1()&!Lock)
        {
            fire(Ang)
        }
    }
}

if(clk("unlock"))
{
    stoptimer("unlock")
    Lock = 0
}

if(MA:count()>0)
{
    for(I=1,MA:count())
    {
        TM = MA[I,entity]
        rangerFilter(TM)
        TR = rangerOffset(100,TM:pos(),TM:forward())
        TM:setPos(TM:toWorld(vec(50,0,0)))
        TM:setAng(TM:toWorld(ang(random(-Rint,Rint),random(-Rint,Rint),random(-Rint,Rint))))
        if(TR:hit()||Driver:keyAttack2())
        {
            TM:propBreak()
            MA:remove(I)
        }
    }
}

if(changed(owner():team()))
{
    holoColor(5,teamColor(owner():team()),75)
}

if(chatClk(owner()))
{
    Str = owner():lastSaid():explode(" ")
    if(Str[1,string]=="!cmd")
    {
        hideChat(1)
        beep("You've been command by the owner of the chip. Your command is below this hint.[STR-SPLIT]"+Str:concat(" ",2))
    }
}

if(duped()||dupefinished())
{
    reset()
}
