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

'look for our initial turrets and spokes
If ListIsEmpty(s.controller.targetList)
	Local shield = 0
	Local turret = 1
	For Local other:Ship = EachIn shipList
		Select other.behavior
		Case "boss_angelturret"
			other.controller.vars.set("angle", turret)
			turret:+ 2
			s.controller.targetList.addLast(other)
		Case "boss_angelshield"
			other.controller.vars.set("angle", shield)
			shield:+ 2
			s.controller.targetList.addLast(other)
		EndSelect
	Next
EndIf

'count the number of peons
If timerTick(s.controller.timerList, "census", 3000)
	'make the ship tangible by default
	s.ignoreCollisions = False

	Local shield = 0
	Local turret = 0
	Local spokenum = 0
	For Local other:Ship = EachIn shipList
		Select other.behavior
		Case "boss_angelturret"
			turret:+ 1
		Case "boss_angelshield"
			shield:+ 1
			s.ignoreCollisions = True'don't collide while there's still a shield
			s.addDamageBuffer(30)
		Case "boss_angelspoke"
			spokenum:+ 1
		EndSelect
	Next
	s.controller.vars.set("spokenum", spokenum)
	s.controller.vars.set("turretnum", turret)
	s.controller.vars.set("shieldnum", shield)
EndIf


'lose dead targets
Local checkList:TList = s.controller.targetList.Copy()
For Local peon:Gertrude = EachIn checkList
	If peon.dead Then ListRemove(s.controller.targetList, peon)
Next

'constantly set the position of our initial turrets and spokes
For Local peon:Ship = EachIn s.controller.targetList
	Local theta# = s.rot + peon.controller.vars.get("angle") * 45
	Local rad#
	Select peon.behavior
	Case "boss_angelturret"
		rad = 140
	Case "boss_angelshield"
		rad = 80
	Default
		rad = 100
	EndSelect
	peon.x = s.x + Cos(theta) * rad
	peon.y = s.y + Sin(theta) * rad
	peon.rot = theta
	'If peon.dead Then ListRemove(s.controller.targetList, peon)
Next

Local peonnum = s.controller.vars.get("spokenum") + s.controller.vars.get("turretnum") + s.controller.vars.get("shieldnum")

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

s.controller.vars.init("stage", 1)
If target <> Null

	'if we've taken some damage (start with 14 peons)
	If peonnum <= 8
		AI.fireAIWeapons(s, 0)'fire at predicted target coords
	EndIf
	
	'spawn mooks as stage goes down
	If peonnum > 0
		Select s.controller.vars.get("stage")
		Case 1
			If peonnum <= 12
				s.controller.vars.set("stage", 2)
				AI.fireAIWeapons(s, 1)'summon the first set of mooks
				Print "stage 2"
			EndIf
		Case 2
			If peonnum <= 9
				s.controller.vars.set("stage", 3)
				AI.fireAIWeapons(s, 1)'summon the second set of mooks
				Print "stage 3"
			EndIf
		Case 3
			If peonnum <= 5
				s.controller.vars.set("stage", 4)
				AI.fireAIWeapons(s, 1)'summon the third set of mooks
				Print "stage 4"
			EndIf
		Case 4
			If peonnum <= 3
				s.controller.vars.set("stage", 5)
				AI.fireAIWeapons(s, 1)'summon the forth set of mooks
				Print "stage 5"
			EndIf
		EndSelect
	EndIf
EndIf


'orbit the gate (go faster the more hurt we are)
Local throttle# = .2
If peonnum <= 10 Then throttle = .6
If peonnum <= 6 Then throttle = 1
s.controller.goalx = 0
s.controller.goaly = 0
s.controller.vars.set("holdrad", 900)

'If vars.get("stage") >= 3
	AI.holdingPattern(s)
	AI.face(s, "go")
	AI.go(s, Max(.25, throttle))
'EndIf

'slo-mo death
'If s.dead Or s.armour <= 0 Then timeRate = 0

If s.dead Or s.armour <= 0

	For Local nemesis:Ship = EachIn shipList
		If nemesis.faction = s.faction And nemesis <> s And Lower(nemesis.name) <> "angelpod"
			nemesis.noItemDrop()
			nemesis.death()
		EndIf
	Next


	timeRate = .25
	
	'how long to leave timerate unchanged
	maintainTimeRate = 750

	new_shakytext("WHAT HAVE YOU DONE", 2500)
	'shakyText("FABULOUS", 2500, False, 8)
	For Local i = 0 To 10
		Local e:Emitter = new_emitter("shipexplode")
		e.y = s.y + Rand(-150,150)
		e.x = s.x + Rand(-150,150)
		e.num = 30
		e.upload()
		
		Local es:Emitter = new_emitter("shipexplode_slow")
		es.x = e.x
		es.y = e.y
		es.num = 15
		es.upload()
	Next
EndIf

