'== included in the AI scripts file =='


'look for our initial turrets and spokes
If ListIsEmpty(s.controller.targetList)
	Local turret1 = -1
	Local turret2 = -1
	For Local other:Ship = EachIn shipList
		Select Lower(other.name)
		Case "stationturret1"
			other.controller.vars.set("side", turret1)
			other.controller.vars.set("dist", 1)
			turret1:+ 2
			s.controller.targetList.addLast(other)
		Case "stationturret2"
			other.controller.vars.set("side", turret2)
			other.controller.vars.set("dist", 1)
			turret2:+ 2
			s.controller.targetList.addLast(other)
		EndSelect
	Next
EndIf

'constantly set the position of our turrets
For Local peon:Ship = EachIn s.controller.targetList
	Local side = peon.controller.vars.get("side")	'-1 is left, 1 is right
	Local dist# = peon.controller.vars.get("dist")	'0-1, how far this turret is currently extended
	
	'figure out how far away this turret SHOULD be (do AI for it!)
	Local movedist = dist
	If peon.controller.target <> Null
		Local tardist = approxDist(peon.controller.target.x - s.x, peon.controller.target.y - s.y)
		movedist = constrain(tardist / 500, 0, 1)
	EndIf
	
	'move the turret to the target move dist
	If Abs(movedist - dist) < .05 Then dist = movedist Else dist:+ .05*Sgn(movedist-dist)
	
	'update the stored distance
	peon.controller.vars.set("dist", dist)
	
	'offset from the center of the station in x and y
	Local offx#, offy#
	
	'adjusted for rotation
	Local realx, realy, theta#
	
	Select Lower(peon.name)
	Case "stationturret1"'off on the two long wings
		offx = (57 + 54*dist) * side
		offy = 0
		
		'find the current relative rotation of the turret
		theta = s.rot + ATan2(offy, offx)		
		realx = (-offx*Sin(theta) + offy*Cos(-theta))*SHIPSCALE*side
		realy = (-offy*Sin(-theta) + offx*Cos(theta))*SHIPSCALE*side
		
	Case "stationturret2"
		offx = 36 * side
		offy = (42 + 32*dist)
		
		'find the current relative rotation of the turret
		theta = s.rot + 90 + 90*side
		realx = -(-offx*Sin(theta) + offy*Cos(-theta))*SHIPSCALE*side
		realy = -(-offy*Sin(-theta) + offx*Cos(theta))*SHIPSCALE*side
	EndSelect

	'find the current relative position of the turret	
	peon.x = s.x + realx
	peon.y = s.y + realy
	peon.rot = theta

	'lose it if it died
	If peon.dead Then ListRemove(s.controller.targetList, peon)
Next

'self-destruct
If s.armour <= 6
	s.armour = 0
	For Local peon:Ship = EachIn s.controller.targetList
		peon.armour = 0
	Next
EndIf

'orbit the gate
s.controller.goalx = 0
s.controller.goaly = 0
s.controller.vars.set("holdrad", 900)
AI.holdingPattern(s)
AI.face(s, "go")
AI.go(s, 1.0)


If s.armour <= 0 And ListIsEmpty(shakytextList) Then new_shakytext("STATION LOST")