'== 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

'increase the range of the pulsar
If s.controller.vars.get("init") = 0
	For Local a:Ability = EachIn s.abilityList
		If a.name = "Pulsar" Then a.vars.set("range", 800)
	Next
	s.controller.vars.set("init", 1)
EndIf


Local old_peonnum = vars.get("peonnum")
'look for our nodes
If timerTick(timerList, "nodecheck", 5000)
	ClearList(s.controller.targetList)
	For Local other:Ship = EachIn shipList
		If other.behavior = "boss_aspoke"
			s.controller.targetList.addLast(other.placeholder)
		EndIf
	Next	
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
vars.set("peonnum", CountList(s.controller.targetList))

'if we lost anybody or take too much damage, activate the pulsar ability
If (vars.get("peonnum") < old_peonnum) Or s.recent_damage >= 50
	'Print "       lost a good soul today"
	s.activateAbility()
	
	'reset the guns
	For Local g:Gun = EachIn s.gunGroup[0]
		timerSet(g.timerList, "fireDelay")
		g.burst = 0
	Next
EndIf


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

s.controller.vars.init("holdrad", Rand(100, 200))
Local circledist = Min(rangeGroup(s, 0) - 25, s.controller.vars.get("holdrad"))
s.controller.vars.set("holdrad", circledist)

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

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

If target = Null
		
	AI.face(s, "go")
	AI.go(s, 1)
	
	'face the direction we've decided
	's.turn(ATan2(s.controller.goy, s.controller.gox), 1)
	
	'--go towards wherever we decided we wanted to go (faster if heavily weighted)
	'throttle = math.Max(script.getDist(gox, goy) / 200, .5)
	's.add_thrust(math.deg(math.ATan2(goy, gox)), throttle, 1)
	
	Local throttle = 1
	s.add_thrust(0, throttle, 0)
	
Else

	'set the goal coords to the target
	AI.setGoalTo(s, "target")
		
	'shoot
	AI.fireAIWeapons(s, 0)'fire at predicted target coords
	
	Local aimx = s.controller.vars.get("aimx")
	Local aimy = s.controller.vars.get("aimy")
	
	If AI.fireWeapons(s, 1, aimx, aimy)'fire at current aiming coords
		'zero aim in on the target
		Local dx = (s.controller.goalx - aimx)
		Local dy = (s.controller.goaly - aimy)
		
		aimx:+ 2.5 * Sgn(dx)
		aimy:+ 2.5 * Sgn(dy)
		
		'but keep it some distance away from the axel to keep it from spinning around fast
		'Local sx = (aimx - s.x)
		'Local sy = (aimy - s.y)
		'Local aimdist# = approxDist(sx, sy)
		'Local aimtheta# = ATan2(sy, sx)
		
		'Local mindist# = 120
		'If aimdist < mindist
		'	aimx = mindist * Cos(aimtheta)
		'	aimy = mindist * Sin(aimtheta)
		'EndIf
		
		s.controller.vars.set("aimx", aimx)
		s.controller.vars.set("aimy", aimy)
		'Print "ZEROING"
		
	Else'reset them to self
	'	Print "RESETTING"
		s.controller.vars.set("aimx", s.controller.goalx)
		s.controller.vars.set("aimy", s.controller.goaly)
	EndIf

	AI.face(s, "goal")	'face the target 
	AI.go(s, 1)		'& go in the direction we decided	

EndIf

'spawn mooks as stage goes down
vars.init("stage", 1)
Local peonnum = vars.get("peonnum")
If peonnum > 0
	'enable the spawner
	For Local g:Gun = EachIn s.gunGroup[1]
		If Lower(g.name) = "spawner axel" Then g.enabled = True
	Next

	Select vars.get("stage")
	Case 1
		If peonnum <= 5
			vars.set("stage", 2)
			fireShipWeapons(s,1)
			Print "stage 2"
		EndIf
	Case 2
		If peonnum <= 2
			vars.set("stage", 3)
			fireShipWeapons(s,1)
			fireShipWeapons(s,1)
			Print "stage 3"
		EndIf
	Rem
	Case 3
		If peonnum <= 2
			vars.set("stage", 4)
			fireShipWeapons(s,1)
			fireShipWeapons(s,1)
			fireShipWeapons(s,1)
			Print "stage 4"
		EndIf
	EndRem
	EndSelect
	
	'disable the spawner
	For Local g:Gun = EachIn s.gunGroup[1]
		If Lower(g.name) = "spawner axel" Then g.enabled = False
	Next

'if we've lost everybody who ever protected us
ElseIf vars.get("stage") > 1
	'lash out
	For Local g:GunBeam = EachIn s.gunGroup[1]
		If Lower(g.name) = "beam_axel"
			g.heat = 0'CONTINUOUS
			g.burst = 100
		EndIf
	Next
EndIf


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


If s.dead Or s.armour <= 0
	Rem
	Local label$
	Select Rand(1,7)
	Case 1
		label = "FABULOUS"
	Case 2
		label = "BOOM"
	Case 3
		label = "A WINNER IS YOU"
	Case 4
		label = "IT HAD A FAMILY"
	Case 5
		label = "WHAT HAVE YOU DONE!!"
	Case 6
		label = "YARRR"
	Case 7
		label = "CONGLATURATION!"
	EndSelect
	new_shakytext(label, 2500)
	'shakyText(label, 2500, False, 6)
	EndRem
	timeRate = .25
	
	'how long to leave timerate unchanged
	maintainTimeRate = 600
	
	new_shakytext("EARTH AVENGED", 2500)
	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

