@name 
@inputs Pitch Yaw Roll 
@outputs
@persist Time Self:entity InertiaConstant Inertia:angle RS Enemy:entity 
if (tickClk()) {

if (Enemy:isPlayer()|Enemy:isNPC()) {
    EnemyPosition = Enemy:pos()
    EnemyPosition += vec(0,0,Enemy:height()/2)
} else {
    EnemyPosition = Enemy:massCenter()
}
EnemyVelocity = Enemy:vel()

#######################################

#rotates to local
RelativePosition = Self:toLocal(EnemyPosition-
(Self:massCenter() - Self:pos())+ #this line adjusts for center of mass
EnemyVelocity*Time-Self:vel()*Time+#this line adjusts for player and self velocity
Enemy:isPlayer()*EnemyVelocity*Time)#lead for players only

#inertia needs to be recalculated incase weight change
Inertia = ang(Self:inertia():y(),Self:inertia():z(),Self:inertia():x())

#Takes a vector representing distance from target relative to turret
RelativeAngles = angnorm(RelativePosition:toAngle():setRoll( -Self:angles():roll()))
#finds the correct impulse and applies it
AngularImpulse = Inertia*InertiaConstant*(RelativeAngles*RS-Self:angVel())
Self:applyAngForce(AngularImpulse)#this function applies impulse, not force
#######################################

} elseif (first()) {

    Time = 0.015 #time for a game tick
    InertiaConstant = 27.053079273838 #Universal inertia constant
    RS = 1/Time #Rotational speed #Theoritically optimal speed (1 tick to aim)
    
    timer("startup1",300)
    Enemy = entity(147)
    
} elseif (clk("startup1")) {

    if (findCanQuery()) {
        findByClass("gmod_turret")
        Self = find()
        runOnTick(1)
    } else {
        timer("startup1",300)
    }
}
