'== included in the AI scripts file =='
'If s.spin = 0 Then s.spin = 0.8 * Rand(-1,1)
s.spin = 0.4

'look for our initial lifeforms
If ListIsEmpty(s.controller.targetList)
	For Local other:Ship = EachIn shipList
		If Left(other.behavior, 10) = "boss_reef_"
			other.controller.vars.set("angle", CountList(s.controller.targetList) * 32)'Rand(0,359))
			other.controller.vars.set("dist", Rand(50,150))
			s.controller.targetList.addLast(other)
			
			'scramble the timing of their openings/closings
			timerBump(other.controller.timerList, "close", Rand(0, 3000))
		EndIf
		
		If other.behavior = "anemone"
			Local dist = approxDist(s.x-other.x, s.y-other.y)
			If dist < 500 Then other.remove()
		EndIf
		
		'mindcontrol all bio stuff
		If other.base.bio = True Then other.faction = s.faction
	Next
	
	'attach an aura to ourselves
	'Local aura:Emitter = new_emitter("nebulaeaura")
	'aura.attach(s)
	'aura.upload()
EndIf

s.orbitx = 0
s.orbity = 700

Local peonnum = CountList(s.controller.targetList)

vars.init("stage", 0)

'if we've taken some damage (start with 12 peons)
'If peonnum <= 4
'	fireAIWeapons(s, 0)'fire at predicted target coords
'EndIf

'spawn mooks as stage goes down
If peonnum > 0

	Local boundx = (stage.width-400)
	Local boundy = (stage.height-400)
	
	Local blink = False

	Select s.controller.vars.get("stage")
	Case 0
		vars.set("stage", 1)
		For Local i = 1 To 2
			AI.fireAIWeapons(s, 1)'summon mooks
		Next
		's.activateAbility()
		Print "stage 2"
	Case 1
		If peonnum <= 10
			vars.set("stage", 2)
			For Local i = 1 To 2
				AI.fireAIWeapons(s, 1)'summon mooks
			Next
			blink = True
			's.activateAbility()
			Print "stage 2"
		EndIf
	Case 2
		If peonnum <= 8
			vars.set("stage", 3)
			For Local i = 1 To 3
				AI.fireAIWeapons(s, 1)'summon mooks
			Next
			Print "stage 3"
		EndIf
	Case 3
		If peonnum <= 5
			vars.set("stage", 4)
			For Local i = 1 To 3
				AI.fireAIWeapons(s, 1)'summon mooks
			Next
			blink = True
			's.activateAbility()
			Print "stage 4"
		EndIf
	Case 4
		If peonnum <= 2
			vars.set("stage", 5)
			For Local i = 1 To 5
				AI.fireAIWeapons(s, 1)'summon mooks
			Next
			's.activateAbility()
			Print "stage 5"
		EndIf
	EndSelect
	
	If blink
		'blip!
		Local blip:Emitter = new_emitter("blink")
		blip.clearGFX()
		blip.addGFX("trailmissile.png")
		blip.x = s.x
		blip.y = s.y
		blip.animated = 0
		blip.upload()
	
		s.x = Rand(400, (stage.width-200)) * binsgn(Rand(0,1))
		s.y = Rand(400, (stage.height-200)) * binsgn(Rand(0,1))
		
		'blip!
		blip = new_emitter("blink")
		blip.clearGFX()
		blip.addGFX("trailmissile.png")
		blip.x = s.x
		blip.y = s.y
		blip.animated = 0
		blip.upload()
	EndIf
	

EndIf




'constantly set the position of the lifeforms
For Local peon:Ship = EachIn s.controller.targetList
	Local theta# = s.rot + peon.controller.vars.get("angle")
	peon.x = s.x + Cos(theta) * peon.controller.vars.get("dist")
	peon.y = s.y + Sin(theta) * peon.controller.vars.get("dist")
	peon.rot = theta - peon.controller.vars.get("angle")*2.5'some random number to break up the radial symmetry

	'only activate the other forms after we've lost a few
	If vars.get("stage") < 2
		If peon.config = "puker" Then timerBump(peon.controller.timerList, "close", 2000)
	EndIf
	
	If vars.get("stage") < 3
		If peon.config = "urchin2" Then timerBump(peon.controller.timerList, "close", 2000)
	EndIf

	'whenever one dies, the rest close up
	If peon.dead
		ListRemove(s.controller.targetList, peon)
		For Local spooked:Ship = EachIn s.controller.targetList
			timerBump(spooked.controller.timerList, "close", Rand(1000, 5000))
		Next
	EndIf
Next


'if all life dies, we die
If ListIsEmpty(s.controller.targetList) Then s.armour = 0

'if we die, all life dies (whoa...)
If s.dead Or s.armour <= 0
	For Local life:Ship = EachIn shipList
		If life.faction = s.faction And life.base.bio And life <> s Then life.death()
	Next
	
	timeRate = .25
	
	'how long to leave timerate unchanged
	maintainTimeRate = 750

	
	new_shakytext("ECOSYSTEM VANQUISHED", 2500)
	'slo-mo death
	'timeRate = 0
	For Local i = 0 To 10
		Local e:Emitter = new_emitter("gib")
		e.y = s.y + Rand(-150,150)
		e.x = s.x + Rand(-150,150)
		e.num = 30
		e.upload()
		
		Local es:Emitter = new_emitter("gib_slow")
		es.x = e.x
		es.y = e.y
		es.num = 15
		es.upload()
	Next
EndIf

