@name 
@inputs Pitch Yaw Roll Enemy:entity 
@outputs 
@persist Time InertiaConstant RS K
@persist TurretArray:array AngleArray:array
#Startup Calculations
if (first()) {
	Time = 0.03
	#Universal inertia constant.  AngularMomenutm = MomentumInputValue * IneritaConstant
	InertiaConstant = toDeg(24.54740718)
	#RS is the rotation speed constant
	RS = 1/Time
	

	
	K = 1/Time/(1.5)
	
	AngleArray:pushNumber(60)
	AngleArray:pushNumber(120)
	AngleArray:pushNumber(180)
	AngleArray:pushNumber(240)
	AngleArray:pushNumber(300)
	AngleArray:pushNumber(360)


timer("begin",100)

}
if (clk("begin")) {
findByClass("gmod_turret")
findSortByDistance(entity():pos())
TurretArray = findToArray()
}

#main timer
interval(1000*Time)
if (clk()) {
	timer("1",1)
	timer("2",1)
	timer("3",1)
	timer("4",1)
	timer("5",1)
	timer("6",1)
}

#finds the current execution
if (clk("1")){Current = 1}
if (clk("2")){Current = 2}
if (clk("3")){Current = 3}
if (clk("4")){Current = 4}
if (clk("5")){Current = 5}
if (clk("6")){Current = 6}

Self = TurretArray:entity(Current)
Enemy = owner()

Radius = 100
RotSpeed = 100
Pos = entity():pos()+vec(Radius*cos(curtime()*RotSpeed+AngleArray:number(Current)),
Radius*sin(curtime()*RotSpeed+AngleArray:number(Current)),100)
Self:applyForce(Self:mass()*(K*(Pos-Self:pos()) - Self:vel()))



#needs to keep getting inertia incase weight changes
Inertia = ang(Self:inertia():y(),Self:inertia():z(),Self:inertia():x())

#######################################
#rotates to local
RelativePosition = Self:toLocal(Enemy:pos()+
vec(0,0,Enemy:height()/2)*(Enemy:isPlayer()|Enemy:isNPC())-#adjusts height for players and npcs
(Self:massCenter() - Self:pos())+ #this line adjusts for center of mass
Enemy:vel()*Time-Self:vel()*Time+#this line adjusts for player and self velocity
Enemy:isPlayer()*Enemy:vel()*0.03)#lead for players only

#converts to angle and formats
RelativeAngles = RelativePosition:toAngle()
RelativeAngles:setRoll( -Self:angles():roll())
RelativeAngles = angnorm(RelativeAngles)
#finds the correct torque and applies it
Torque = Inertia*InertiaConstant*pi()/180*(RelativeAngles*RS-Self:angVel())
Self:applyAngForce(Torque)
#######################################
