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

'start out happy with where we are
If s.controller.goalx = 0 Then s.controller.goalx = s.x
If s.controller.goaly = 0 Then s.controller.goaly = s.y

If s.controller.vars.get("phase") = 0
	'set blink ability values
	s.abilitySetTimer("Blink", "cooldown", 1000)
	s.abilitySetVar("Blink", "cost", 0)
	
	'set cloak ability values
	s.abilitySetTimer("Cloak", "duration", 30000)
	s.abilitySetTimer("Cloak", "cooldown", 0)
	s.abilitySetVar("Cloak", "cost", 0)
	
	s.controller.vars.set("phase", 1)
EndIf

'try to avoid collisions: sets gox & goy to point away from nearby ss
AI.setProximityAvoid(s)

'look for a s.target
AI.getTarget(s, 4000)

If target = Null

	'fly in circles around the goal
	AI.holdingPattern(s)

	AI.face(s, "go")
	AI.go(s, 1)
		
	Local throttle = 1
	s.add_thrust(0, throttle, 0)
	
Else

	'set the goal coords to the target (to circle them)
	s.controller.goalx = target.x
	s.controller.goaly = target.y
	
	'face the target
	AI.face(s, "target")

	'PHASE I : CLOAKED, HOMING MISSILES
	If (s.armour / s.stat.armour) >= .6 And Not s.abilityHave("Illusory")

		'fly in circles around the target
		AI.holdingPattern(s)

		'go in the direction we decided	
		AI.go(s, 1)
		
		s.activateAbility(0,0,"Cloak")

	'PHASE II : BLINKING TURRET MIRROR IMAGES
	Else
		'turn off cloak
		If s.controller.vars.get("phase") = 1
			s.abilitySetTimer("Cloak", "duration", 20)
			s.controller.vars.set("phase", 2)
		EndIf
		
		'avoid collisions, but no other movement
		If Abs(s.controller.gox) > 20 Or Abs(s.controller.goy) > 20 
			AI.go(s, 1)
		Else
			'slow down
			If s.speed > 1 Then s.speed = s.speed - 1 * (timePass / 20)
		EndIf
	EndIf
	
	'BLINKING ON DAMAGE
	If s.recent_damage >= 6 And Not s.abilityHave("Illusory") And (s.armour / s.stat.armour) >= .15
	
		'try to activate our ability (AKA blink) -- if we do, then also fire our alternate weapon (AKA mirror image)
		Local xtar = target.x + binsgn(Rand(0,1)) * Rand(1000, 2000)
		Local ytar = target.y + binsgn(Rand(0,1)) * Rand(1000, 2000)
		'keep it on the stage
		If Abs(xtar) > (stage.width - 300) Then xtar = (stage.width - 300)*Sgn(xtar)
		If Abs(ytar) > (stage.height - 300) Then ytar = (stage.height - 300)*Sgn(ytar)
	
		If s.activateAbility(xtar, ytar, "Blink")
			s.speed = 0
			'phase II spawns mirror images
			If s.controller.vars.get("phase") = 2 Then AI.fireAIWeapons(s, 1, s.x,  s.y)
		EndIf
	EndIf
	
	
	'shoot
	'fireAIWeapons(s, 0, target.x,  target.y)'fire at current target coords
	AI.fireAIWeapons(s, 0)'fire at predicted target coords

EndIf

'illusions kill themselves on ending
If s.abilityHave("Illusory")
	If ListIsEmpty(s.controller.targetList)
		For Local other:Ship = EachIn shipList
			If Lower(other.name) = "wraith" Then s.controller.targetList.addLast(other.placeholder)
		Next
	Else
		For Local other:Gertrude = EachIn s.controller.targetList
			If other.dead Then s.armour = 0
		Next
	EndIf
EndIf


