@name Spooky Skelly Turret
@persist VM RN PA
@persist HR:ranger
@persist AP:vector
@persist [E B P M]:entity
@persist [WL WP]:array
if(first())
{
    runOnTick(1)
    runOnChat(1)
    
    E = entity()
    B = E:isConstrainedTo()
    
    VM = B:isVehicle()
    soundPlay("vm",1,VM ? "hl1/fvox/bell.wav" : "hl1/fvox/beep.wav")
    
    holoCreate(1,E:toWorld(vec(0,0,17.5)),vec(3),E:toWorld(ang(0,0,-15)))
    holoModel(1,"models/Gibs/HGIBS_spine.mdl")
    holoParent(1,E)
    
    holoCreate(2,E:toWorld(vec(0,0,41.25)),vec(2))
    holoModel(2,"models/Gibs/HGIBS.mdl")
    holoParent(2,E)
    
    holoCreate(3,holoEntity(2):toWorld(vec(-5,0,0)),vec(0.75),E:toWorld(ang(-90,0,0)))
    holoModel(3,"models/Items/combine_rifle_ammo01.mdl")
    holoMaterial(3,"models/antlion/antlion_innards")
    holoColor(3,vec(25))
    holoParent(3,2)
    
    rangerFilter(B)
    rangerPersist(1)
    
    WL = array(
    "STEAM_0:0:32759932",
    "STEAM_0:0:33397564",
    "STEAM_0:0:33994222",
    "STEAM_0:0:38933156",
    "STEAM_0:1:22885263",
    "STEAM_0:1:43508393",
    "STEAM_0:1:63782001",
    "STEAM_0:0:75800655"
    )
    
    function update()
    {
        WP:clear()
        foreach(I, Ply:entity = players())
        {
            foreach(J, Str:string = WL)
            {
                if(Ply:steamID() == Str)
                {
                    WP:pushEntity(Ply)
                    print(2,"Pushing "+Ply:name()+" to array.")
                    break
                }
            }
        }
    }
}

if(chatClk(owner()))
{
    local LS = lastSaid():lower():explode(" ")
    if(LS[1,string][1]=="-")
    {
        hideChat(1)
        switch(LS[1,string])
        {
            case "-g",
            local TE = findPlayerByName(LS[2,string])
            if(TE:isValid())
            {
                print(TE:name() + ": " + TE:steamID())
            }else
            {
                print("Player not found.")
            }
            break
            
            case "-r",
            reset()
            break
        }
    }
}

if(changed(numPlayers()))
{
    update()
}

RN -= 0.125
RN = max(RN,3)
if(changed(RN))
{
    holoColor(3,RN <= 5 ? vec(180,255,180) : vec(255,200,200))
}

holoAng(3,holoEntity(3):toWorld(ang(0,RN,0)))

####

if(VM)
{
    P = B:driver()
    if(changed(P))
    {
        PA = P:team() == owner():team()
    }
    if(P:isValid())
    {
        HR = P:eyeTrace()
        
        AP = HR:pos()
    }
}else
{
    if(findCanQuery())
    {
        findByClass("player")
        findIncludeEntities(WP)
        findSortByDistance(E:pos())
        P = find()
        
        PA = P:pos():distance(E:pos())<100
        
        AP = P:aimPos()
    }
}

####

if(changed(PA))
{
    soundPlay("pa",1,PA ? "buttons/button18.wav" : "buttons/button19.wav")
    
    holoColor(2,PA ? vec(255) : vec(255,200,200))
}
if(PA)
{
    holoAng(2,(AP-holoEntity(2):pos()):toAngle())
}

####

if(M:isValid())
{
    local R = rangerOffset(75,M:pos(),M:forward())
    if(R:hit() | P:keyAttack2())
    {
        M:propBreak()
    }else
    {
        M:setPos(M:toWorld(vec(50,0,0)))
        local R = 0.125
        M:setAng(M:toWorld(ang(random(-R,R),random(-R,R),random(-R,R))))
    }
}else
{
    if(PA & P:keyAttack1() & RN<=3 & P:aimEntity()!=E & P:isAlive())
    {
        M = propSpawn("models/props_phx/amraam.mdl",holoEntity(2):toWorld(vec(150,0,0)),(AP-holoEntity(2):pos()):toAngle(),1)
        M:setColor(0,0,0,0)
        M:setTrails(25,0,3,"trails/laser",vec(200,100,100),255)
        M:soundPlay("rckt",0,"weapons/rpg/rocket1.wav")
        
        holoCreate(M:id(),M:pos(),vec(1),M:angles())
        holoModel(M:id(),"models/Gibs/HGIBS.mdl")
        holoParent(M:id(),M)
        
        soundPlay("sh",1.792,"weapons/sentry_rocket.wav")
        
        RN = 20
    }
}
