@name Turret
@inputs Plus W:wirelink W2:wirelink W3:wirelink
@outputs 
@persist Pos:vector Target:entity
@persist  Ang:angle E:entity E2:entity E3:entity
@persist Target2:entity Target3:entity
@persist Ang2:angle Ang3:angle T
@trigger 

runOnTick(1)
if(first())
{
    E = W:entity()
    E2 = W2:entity()
    E3 = W3:entity()
}
if(!Target:isAlive())
{
findExcludePlayer(owner())
    findByClass("npc*")
    findSortByDistance(E:pos())
    Target = findResult(1)
}

if(!Target2:isAlive()| Target2 == Target)
{
findExcludePlayer(owner())
    findByClass("npc*")
    findSortByDistance(E:pos())
    Target2 = findResult(2)
}

if(!Target2:isAlive()| Target3 == Target2 | Target3 == Target)
{
findExcludePlayer(owner())
    findByClass("npc*")
    findSortByDistance(E:pos())
    Target3 = findResult(3)
}

Ang = ang(Target:elevation(E:pos() - vec(0,0,40)),atan(Target:pos():y() - E:pos():y(), Target:pos():x() - E:pos():x()),0) - E:angles()
E:setPos(owner():pos() + vec(sin(T)*50, cos(T)*50, 80))
E:applyAngForce((Ang*20-E:angVel())*E:mass())

Ang2 = ang(Target2:elevation(E2:pos() - vec(0,0,40)),atan(Target2:pos():y() - E2:pos():y(), Target2:pos():x() - E2:pos():x()),0) - E2:angles()
E2:setPos(owner():pos() + vec(sin(T+ 120)*50, cos(T + 120)*50, 80))
E2:applyAngForce((Ang2*20-E2:angVel())*E2:mass())

Ang3 = ang(Target3:elevation(E3:pos() - vec(0,0,40)),atan(Target3:pos():y() - E3:pos():y(), Target3:pos():x() - E3:pos():x()),0) - E3:angles()
E3:setPos(owner():pos() + vec(sin(T + 240)*100, cos(T + 240)*50, 80))
E3:applyAngForce((Ang3*20-E3:angVel())*E3:mass())

T += 1
