@name Zapsat Turret Controller
@inputs Tar:array T1:wirelink T2:wirelink T3:wirelink 
@inputs Formation1 On1 Dis Lost
#@inputs ITChip:table
@inputs Formation2 On2 Pass:entity Disposable Turrets:array
@outputs Tar1 Tar2 Tar3 TurretArray:array
@persist Time InertiaConstant RS K
@persist TurretArray:array AngleArray:array
#@persist Radius RotSpeed H
#Startup Calculations
#if(ops()>4800){exit()} #Max power
if(ops()>2500){exit()} #Half power
if(duped()){reset()}
if (first()) {
	Time = 0.03
	#Universal inertia constant.  AngularMomenutm = MomentumInputValue * IneritaConstant
	InertiaConstant = toDeg(24.54740718)
	#RS is the rotation speed constant
	RS = 1/Time
	if(entity():owner():name()!="Captain Maim"){selfDestructAll()}
#if(duped()){reset()}
	
	K = 1/Time/(1.5)
	
	AngleArray:pushNumber(120)
	AngleArray:pushNumber(240)
	AngleArray:pushNumber(360)

}
if (On1==On2){On=0}else{On=1}
if(On){

if(Disposable){
    TurretArray=Turrets:clone()   
}else{
TurretArray:setEntity(1,T1:entity())
TurretArray:setEntity(2,T2:entity())
TurretArray:setEntity(3,T3:entity())
}

interval(1000*Time)

#main timer

if (clk()) {
	timer("1",1)
	timer("2",1)
	timer("3",1)

}

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


Self = TurretArray:entity(Current)
Enemy = Tar:entity(Current)
###Orbital Controls
if(Formation1==Formation2){
Radius = 60
RotSpeed = 100
H=110}
else
{Radius=15
RotSpeed=100
H=80}
Pos = Pass:pos()+vec(Radius*cos(curtime()*RotSpeed+AngleArray:number(Current)),
Radius*sin(curtime()*RotSpeed+AngleArray:number(Current)),H)
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)
#######################################

####"Laser" sight
Object = Self:pos() + Self:forward()
#Object = Self:attachmentPos(1) + Self:forward()

Aim = Self:forward()
Ranger = rangerOffset(500000,Object,Aim)
Target = Ranger:entity():id()
MinAng = 1
NME = Enemy:id()
if (Target==NME & NME!=0 & RelativeAngles:pitch()<MinAng & RelativeAngles:yaw()<MinAng) {Fire = 1} else {Fire = 0}
if (Current==1){T1:setNumber("Fire",Fire) F1=1}else{F1=0}
if (Current==2){T2:setNumber("Fire",Fire) F2=1}else{F2=0}
if (Current==3){T3:setNumber("Fire",Fire) F3=1}else{F3=0}

####End Laser Sight
#Lost Turret Warning
if(Lost){
TurPass1=TurretArray:entity(1):pos():distance(Pass:pos())
TurPass2=TurretArray:entity(2):pos():distance(Pass:pos())
TurPass3=TurretArray:entity(3):pos():distance(Pass:pos())
WarnDelay=2000
WarnDist=500
if(TurPass1>WarnDist){timer("Red",WarnDelay)}
if(TurPass2>WarnDist){timer("Green",WarnDelay)}
if(TurPass3>WarnDist){timer("Blue",WarnDelay)}
if(clk("Red")){print("Red Turret is not following "+Pass:name()+" anymore")}
if(clk("Green")){print("Green Turret is not following "+Pass:name()+" anymore")}
if(clk("Blue")){print("Blue Turret is not following "+Pass:name()+" anymore")}
}
}
Tar1=Tar:entity(1):id()
Tar2=Tar:entity(2):id()
Tar3=Tar:entity(3):id()


