@name Zapsat Turret Controller
#@inputs Pitch Yaw Roll 
@inputs Tar:array T1:wirelink T2:wirelink T3:wirelink 
@inputs Formation1 On1 ITChip:table
@inputs Formation2 On2 Pass:entity Dis
@outputs Tar1 Tar2 Tar3
@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
	if(entity():owner():name()!="Captain Maim"){selfDestructAll()}
if(duped()){reset()}
	
	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()
TurretArray:setEntity(1,T1:entity())
TurretArray:setEntity(2,T2:entity())
TurretArray:setEntity(3,T3:entity())
#}
#Formation2=ITChip:number("Formation")
#On2=ITChip:number("On")
#Pass=ITChip:entity("Pass")
#Dis=ITChip:number("Disposable")
#timer("T",8000)
#if(clk("T")){
#print("power: "+On1+":"+On2)}
if (On1==On2){On=0}else{On=1}
interval(1000*Time)
if(On){
#main timer

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 = 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()
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
###Disposable Sats
if(Dis){
findInSphere(entity():pos(),200)
findSortByDistance(entity():pos())
findClipToClass("turret")
C1 = findResult(1):getColor()
C2 = findResult(2):getColor()
C3 = findResult(3):getColor()
if 
(C1:x()==255) {C1a = 1}
if
(C1:y()==255) {C1a = 2} 
if
(C1:z()==255) {C1a = 3}

if 
(C2:x()==255) {C2a = 1} 
if
(C2:y()==255) {C2a = 2} 
if
(C2:z()==255) {C2a = 3}

if 
(C3:x()==255) {C3a = 1} 
if
(C3:y()==255) {C3a = 2} 
if
(C3:z()==255) {C3a = 3}
entity():setColor(255,255,0)
Dis2=1
}
if(Dis2){
if (C1a==1 & On) {T1F = F1} elseif
   (C1a==2 & On) {T1F = F2} elseif
   (C1a==3 & On) {T1F = F3} else {T1F = 0}

if (C2a==1 & On) {T2F = F1} elseif
   (C2a==2 & On) {T2F = F2} elseif
   (C2a==3 & On) {T2F = F3} else {T2F = 0}

if (C3a==1 & On) {T3F = F1} elseif
   (C3a==2 & On) {T3F = F2} elseif
   (C3a==3 & On) {T3F = F3} else {T3F = 0}

N9p = "+gm_special 9;" N9m = "-gm_special 9;"
N7p = "+gm_special 7;" N7m = "-gm_special 7;"
N3p = "+gm_special 3;" N3m = "-gm_special 3;"
#concmd(
}

}
Tar1=Tar:entity(1):id()
Tar2=Tar:entity(2):id()
Tar3=Tar:entity(3):id()
