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

'limit range of beams
For Local g:GunBeam = EachIn s.gunGroup[0]
	g.shotrange = 600
Next

'try to avoid collisions: sets gox & goy to point away from nearby ss
'setProximityAvoid(s)
'look for the axel / other spokes
Local justFoundAngel = False
s.controller.vars.init("checkinterval", 1000 + Rand(-500,500))
If ListIsEmpty(s.controller.targetList) Or timerTick(s.controller.timerList, "spokecheck", s.controller.vars.get("checkinterval"))
	Local spokenum#					'number of found other spokes
	Local spoke#					'which spoke we are
	Local lowestnextspokeIDNUM = -1	'the lowest IDNUM of a spoke that is higher than us that we've found
	Local lowestIDNUM = -1			'the lowest IDNUM of a spoke we've found

	'Print "SPOKE SEARCH :"
	For Local other:Ship = EachIn shipList
		'readjust which spoke we are
		If other.behavior = "boss_angelspoke"
			spokenum:+ 1
			If other.netspriteIDNUM < s.netspriteIDNUM Then spoke:+ 1
			
			'Print "        found another spoke IDNUM " + other.netspriteIDNUM + "  we are now spoke " + spoke + "     out of spokenum " + spokenum
			'If other = s Then Print "                (that was us)"
			'target this spoke (trying to get the next spoke first in the list w/ wrapping from last spoke to first) (I'm so sorry future debugger)
			If other <> s
				'if this is the next lowest IDNUM we've found
				If other.netspriteIDNUM > s.netspriteIDNUM And (lowestnextspokeIDNUM = -1 Or other.netspriteIDNUM < lowestnextspokeIDNUM)
					lowestnextspokeIDNUM = other.netspriteIDNUM
					s.controller.targetList.addFirst(other.placeholder)
					'Print "                that was the next lowest we found : " + lowestnextspokeIDNUM
				'if we haven't found anything higher than us yet
				ElseIf lowestnextspokeIDNUM = -1
					'track the very lowest thing
					If lowestIDNUM = -1 Or other.netspriteIDNUM < lowestIDNUM
						lowestIDNUM = other.netspriteIDNUM
						s.controller.targetList.addFirst(other.placeholder)
						'Print "                that was the very lowest we found : " + lowestIDNUM
					Else
						s.controller.targetList.addLast(other.placeholder)
					EndIf
				'if it's an irrelevant spoke to us
				Else
					s.controller.targetList.addLast(other.placeholder)
				EndIf
			EndIf
			
		EndIf
		'find the archangel if we haven't already
		If s.controller.target = Null And other.behavior = "boss_archangel"
			s.controller.target = other.placeholder
			justFoundAngel = True
		EndIf
	Next
	s.controller.vars.set("spoke", spoke)
	s.controller.vars.set("spokenum", spokenum)
EndIf

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

'track the ARCHANGEL
If s.controller.target <> Null
	'get target coordinates
	s.controller.goalx = s.controller.target.x
	s.controller.goaly = s.controller.target.y
				
	'if the archangel somehow dies, lose it
	If s.controller.target.dead Then s.controller.target = Null
EndIf

'circle around the axel (every 45s)
timerInit(s.controller.timerList, "wheel", 45000)
timerTick(s.controller.timerList, "wheel")

'current dist theta we should be from the axel
Local circledist = 325
Local spokemod# = s.controller.vars.get("spoke") / s.controller.vars.get("spokenum")
Local theta# = spokemod * 360 + timerTween(s.controller.timerList, "wheel") * 360

Local movx = s.controller.goalx + Cos(theta)*circledist
Local movy = s.controller.goaly + Sin(theta)*circledist

'if we JUST found the archangel, teleport to them
If justFoundAngel
	s.x = movx
	s.y = movy
	s.rot = theta
EndIf

'find distance and angle to the target move
Local dx = movx-s.x
Local dy = movy-s.y
If approxDist(dx, dy) > 10
	Local movtheta = ATan2(dy, dx)
		
	'weight desire To go towards that direction
	Local gx = Cos(movtheta)*80
	Local gy = Sin(movtheta)*80
		
	'store the new direction
	s.controller.gox = s.controller.gox + gx
	s.controller.goy = s.controller.goy + gy	
	
	'go in the decided direction, face away from the axel
	AI.go(s)
EndIf
s.turn(theta)

'if we found other spokes, maybe fire node beam
Local spoke = s.controller.vars.get("spoke")
Local spokenum = s.controller.vars.get("spokenum")
If spokenum > 2
	'the entire beam cycle goes around this fast
	timerTick(s.controller.timerList, "beam", 12000)
	
	'where we are in the beam cycle
	Local cycle# = timerTween(s.controller.timerList, "beam")
	
	'how large a chunk of the cycle each gap is
	Local chunk# = 1.0 / spokenum	

	'if it's this spoke's turn to not shoot
	Local gap = False
	If cycle > (spoke-1)*chunk And cycle < (spoke)*chunk Then gap = True

	If Not gap And Not ListIsEmpty(s.controller.targetList)
		'fire node beam at the next spoke
		Local nextSpoke:Gertrude = Gertrude(s.controller.targetList.First())
		fireShipWeapons(s, 0, nextSpoke.x, nextSpoke.y)
	EndIf
Else
	'turn into a circle-and-shooter when most are destroyed
	'ClearList(s.gunGroup[0])
	For Local g:GunBeam = EachIn s.gunGroup[0]
		g.enabled = False
	Next

	s.behavior = "capitolship"
EndIf

'fire torpedos at the player
Local angel:Gertrude = s.controller.target'(preserve the angel pointer)

s.controller.target = Null
AI.getTarget(s, 800)
If s.controller.target <> Null And Not s.controller.target.cloaked
	'find relative angle to player
	Local ptheta# = ATan2(s.controller.target.y - s.y, s.controller.target.x - s.x)
	ptheta = convert_to_relative(ptheta, s.rot)
	
	'don't fire backwards
	If Abs(ptheta) < 90 Then AI.fireAIWeapons(s, 1)'fire at predicted target coords
EndIf

'restore angel target
s.controller.target = angel
