@name Force Turret
#@inputs Numpad:wirelink 
@inputs Gun:wirelink 
@outputs [Laser Target]:entity T:array
@persist 
@trigger 
@model models/props_junk/PopCan01a.mdl

runOnTick(1)

Fly=1
Length=500 #What range does the turret have? 

Gun["Length",number]=Length
O=owner()
findInSphere(entity():pos(),Length)
findSortByDistance(entity():pos())
findIncludeClass("player")
findIncludeClass("prop")
findExcludeEntity(O)
#findExcludePlayerProps(O) 
T=findToArray()
G=Gun:entity()
for(C=1,T:count()){
    rangerFilter(array(entity(),G))
    Laser=rangerOffset(G:pos(),T[C,entity]:pos()):entity()
    
    if(Laser==T[C,entity]){
        Target=T[C,entity] 
        break
    }
}

#G=entity():isConstrainedTo()

#if(!G){selfDestruct()}
#G=Gun:entity()
Dir = G:pos()-((Target:pos()+vec(0,0,Target:height()/2))+Target:vel()/5)
#Q = quat(Dir:toAngle())*qRotation(vec(0,0,1),180) #X
Q = quat(Dir:toAngle())*qRotation(vec(0,1,0),-90) #Z
#Q = quat(Dir:toAngle())*qRotation(vec(0,0,1),90) #Y
G:applyTorque((150*G:toLocal(rotationVector(Q/quat(G))+G:pos()) - 12*G:angVelVector())*G:inertia())

Fire="Velocity"

if(Target){
    rangerFilter(array(entity(),G))
    Laser=rangerOffset(Length,G:pos(),G:up()):entity()

    if((Laser==Target)){Gun[Fire,number]=20000}else{Gun[Fire,number]=0}
}
if(Fly){
    XYZ=entity():pos()+vec(0,0,50)
    Speed=100*(G:mass()/4)
    G:applyForce(Speed*(XYZ-G:pos())-(G:vel()/2)*G:mass())
}
