@name Multi Gun
@inputs [Gun1 Gun2 Gun3]:wirelink
@outputs
@persist Guns:array
@trigger 
@model models/expression 2/cpu_interface_nano.mdl
runOnTick(1)
#findInSphere(entity():pos(),100)
#findSortByDistance(entity():pos())
#findIncludeClass("player")
#P=findToArray()
#P=players()
#if(P[1,entity]:pos():distance(entity():pos())<50){O=P[1,entity]}else{O=noentity()}

#O=owner()

if(first()){
    Guns=array()
    Guns:pushEntity(Gun1:entity())
    Guns:pushEntity(Gun2:entity())
    Guns:pushEntity(Gun3:entity())
}
P=players()

for(C=1,3){
    G1=array()
    
    for(D=1,P:count()){
        G1:pushNumber(Guns[C,entity]:pos():distance(P[D,entity]:pos()))    
    }
    if(G1:min()<50){O=P[G1:minIndex(),entity]}
    G=Guns[C,entity]    
    rangerFilter(G)
    Range=1000
    Aim=rangerOffset(Range,G:pos(),O:eye()):pos()
    #G=entity():isConstrainedTo()
    if(O:keyUse()){
        if(C==1){Gun1["Fire",number]=1}
        if(C==2){Gun2["Fire",number]=1}
        if(C==3){Gun3["Fire",number]=1}
    }else{
        
        if(C==1){Gun1["Fire",number]=0}
        if(C==2){Gun2["Fire",number]=0}
        if(C==3){Gun3["Fire",number]=0}
    }
    Dir = G:pos()-(Aim)
    Q = quat(Dir:toAngle())*qRotation(vec(0,0,1),180)
    G:applyTorque((150*G:toLocal(rotationVector(Q/quat(G))+G:pos()) - 12*G:angVelVector())*G:inertia())
}
