@name Portal Turret
@inputs 
@outputs E:entity [Fri Pla]:array
@persist T:entity Grabbed AngDetect
@trigger 
@model models/props_combine/breenbust_Chunk04.mdl


E=entity():isConstrainedTo()
#if(!E){
#    print("AIE!!! I cannot compell you! Your mind is closed to me!")
#    selfDestruct()
#}
entity():setAlpha(10)
Rad=50
Loc=E:pos()+vec(0,0,36)+E:forward()*300+E:right()*3
if(first()|dupefinished()){
    holoCreate(1,Loc,vec(0.1,0.1,-Rad),E:angles()+ang(90,0,0))
    holoModel(1,"hq_cylinder")
    holoColor(1,vec(1,0,0)*255,255)
    holoMaterial(1,"models/props_combine/portalball001_sheet")
}
interval(50)
holoPos(1,Loc)
holoAng(1,E:angles()+ang(90,0,0))

findInCone(E:pos(),E:up(),1000,30)
findIncludeClass("player")
findIncludeClass("npc_")
findSortByDistance(E:pos())
Tar=findToArray()
T=Tar[1,entity]
Grabbed=E:isPlayerHolding()
Ang=E:angles()
AngDetect=(Ang:pitch()>abs(45))|(Ang:roll()>abs(45))


Search=changed(T)&!T
Active=changed(T)&T
Deploy=0
Pickuped=Grabbed&changed(Grabbed)
Autosearch=0
Retire=0
Disabled=AngDetect&changed(AngDetect)

Pla=players()
Fri=owner():friends()
Fri:pushEntity(owner())

for(C=1,Pla:count()){
    for(D=1,Fri:count()){
        if(Fri[D,entity]==Pla[C,entity]){
            Pla:remove(C)
        }
    }
}
if(Pla:count()){
    for(C=1,Pla:count()){
        
        E:npcRelationship(Pla[C,entity],"hate",99)
    }
}

for(C=1,Fri:count()){E:npcRelationship(Fri[C,entity],"like",99)}

if(Active){entity():soundPlay(1,0,"npc/turret_floor/turret_active_"+randint(1,8)+".wav")}
if(Autosearch){entity():soundPlay(1,0,"npc/turret_floor/turret_autosearch_"+randint(1,6)+".wav")}
if(Deploy){entity():soundPlay(1,0,"npc/turret_floor/turret_deploy_"+randint(1,6)+".wav")}
if(Pickuped){entity():soundPlay(1,0,"npc/turret_floor/turret_pickup_"+randint(1,10)+".wav")}
if(Retire){entity():soundPlay(1,0,"npc/turret_floor/turret_retire_"+randint(1,7)+".wav")}
if(Search){entity():soundPlay(1,0,"npc/turret_floor/turret_search_"+randint(1,4)+".wav")}
if(Disabled){entity():soundPlay(1,0,"npc/turret_floor/turret_disabled_"+randint(1,8)+".wav")}
