@name Physgun Defense
@inputs 
@outputs Fire T:array S On
@persist T:array S On Fire
@trigger 
interval(20)
Self=owner()
findInSphere(Self:pos(),10000)
findIncludeClass("prop_combine_ball")
findIncludeClass("npc_grenade_frag")
findIncludeClass("rpg_missile")
findIncludeClass("grenade_ar2")
findIncludeClass("crossbow_bolt")
findSortByDistance(Self:pos())
T=findToArray()
Target=T[1,entity]
Offset=vec()
runOnChat(1)
K=chatClk()
Chat=Self:lastSaid()
if(K){
    if(Chat=="PD on"){On=1 print("Physgun Defense On")}
    if(Chat=="PD off"){On=0 print("Physgun Defense Off")}
    if(Chat=="PD die"){selfDestruct() print("Physgun Defense Self Destructing")}
}

Object = Self:pos() + Self:forward()
#Object=Self:shootPos()
Aim = Self:forward()
Ranger = rangerOffset(500000,Object,Aim)
Tar = Ranger:entity():id()
NME = Target:id()
if (Tar==NME & NME!=0) {Fire = 1 print("fire")} else {Fire = 0}





Ang=(Target:pos()-Self:shootPos()+Offset):toAngle()

if(On&T:count()&!Target:isPlayerHolding()){Self:setEyeAngle(Ang)

Target=entity():owner():aimEntity()
if(Self:isAlive()){
if(Target:isAlive()&!S) {S=1 concmd("+attack")}}}
if(Target:isAlive()&S){S=0 concmd("-attack")}

#Pitch goes from 90 to -90 Z+ and 90 Z- is down
#Yaw goes from 0 to 360 where 0 is east or +x and 90 is North or +y

#Associated weapon entities
#primary weapon entity
#special effect entities

#Combine Ball
#1=Entity [110][prop_combine_ball]
#2=Entity [112][env_spritetrail]

#Grenade
#3=Entity [110][npc_grenade_frag]
#1=Entity [113][env_spritetrail]
#2=Entity [112][env_sprite]

#RPG Entities
#1=Entity [110][env_laserdot]
#1=Entity [112][rpg_missile]
#2=Entity [113][env_rockettrail]

#Crossbow bolts hiitting
#1=Entity [113][crossbow_bolt]
#2=Entity [114][env_sprite]

#Dropped SMG grenade
#1=Entity [110][grenade_ar2]
#2=Entity [112][env_smoketrail]
