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

'slightly randomize the power of the drone's beam to give it a unique signature
If s.controller.vars.get("init") = 0
	For Local g:GunBeam = EachIn s.gunGroup[0]
		g.shotdamage:- Rnd()
		s.controller.vars.set("signature", Abs(g.shotdamage))
	Next
	s.controller.vars.set("init", 1)
EndIf

'list of things to not avoid running into (for when we're ramming stuff)
'Local proxIgnoreList:TList = New TList
ClearList(s.controller.ignoreProxList)

'we're going to average all target positions to find the final goal
s.controller.goalx = 0
s.controller.goaly = 0
Local circledist = 50
Local beamrange# = rangeGroup(s, 0)

'get the closest allied target
AI.getTarget(s, 900, 1000, 1)
target = s.controller.target

'if there are no allies, look for an enemy target
If target = Null Then AI.getTarget(s, 2000, 4000)
target = s.controller.target

If target <> Null
	'Print "bubble drone target : " + target.name

	'if we're in ally-protect mode
	If fTable[s.faction, target.faction] = 1

		'look for other allies near the target (to shield them)
		If ListIsEmpty(s.controller.targetList) Then s.controller.targetList.addLast(target)
	
		s.controller.vars.init("checkinterval", 2000 + Rand(-200,200))
		If s.controller.hasTimerTicked("groupCheck", s.controller.vars.get("checkinterval")) = 1
			'clear the target list
			ClearList(s.controller.targetList)
			
			'ignore self, target
			s.controller.targetList.addLast(s.placeholder)
			s.controller.targetList.addLast(target)
			
			Local groupRange = 600
			
			'and get new targets
			Local tar:Gertrude
			Repeat
				tar = closestShip(target.x, target.y, s.faction, 1, groupRange, s.controller.targetList)
				If tar <> Null Then s.controller.targetList.addlast(tar)
			Until tar = Null'until we can't find a valid target within range
		EndIf
	
		If Not ListIsEmpty(s.controller.targetList)
			'Local firstx, firsty'get the coords of the first target
		
			'Print "bubble drone target analysis:  from " + s.x + ", " + s.y
			For Local tar:Gertrude = EachIn s.controller.targetList
				'only non-bubble drone enemies without a damage buffer (or ones that we're already shielding)
				Local match = (Left(String(tar.damageBuffer), 7) = Left(String(s.controller.vars.get("signature")), 7))
				
				'forget the dead, interred in the cold ground // lose thyself // lose such phantoms of the mind // relenquish the possessions of others
				If tar.dead Or tar = s.placeholder Or tar.cloaked Or (tar.damageBuffer > 0 And Not match) Or tar.behavior = "bubbledrone"
					ListRemove(s.controller.targetList, tar)
					Continue
				EndIf
				
				If (tar.damageBuffer = 0 Or match)
					'Print "   shielding"
					'(if there's only one, just circle them)
					If CountList(s.controller.targetList) = 1
						s.controller.goalx = tar.x
						s.controller.goaly = tar.y
						circledist = 400
					Else'average the goal coords between the target's current positions
						s.controller.goalx = (s.controller.goalx + tar.x) / 2
						s.controller.goaly = (s.controller.goaly + tar.y) / 2
					EndIf
					
					'Print "     commanding fire at " + tar.x + ", " + tar.y
					'IF WITHIN RANGE???
					Local dist# = approxDist(tar.x-s.x, tar.y-s.y)
					If dist < beamrange
						s.controller.target = tar
						fireShipWeapons(s, 0, tar.x, tar.y)'fire REPAIR BEAM at each target coord
					EndIf
				'Else
				'	Print "    skipping"
				EndIf
			Next
		EndIf
		
		
	Else'targeting enemy, kamikazie!!!
		ClearList(s.controller.targetList)
	
		'set goal to target
		circledist = 1
		AI.setGoalTo(s, "target")
		
		Local dist# = approxDist(s.controller.goalx-s.x, s.controller.goaly-s.y)
		'shield self if we're far away
		If dist > 400 Then s.addDamageBuffer(12) Else AI.face(s, "goal")
		'ram player if close
		If dist < 150
			s.afterburn = 1
			s.controller.ignoreProxList.addLast(target)
		EndIf
		If dist < 50 Then s.addDamageBuffer(6)
	EndIf
'Else
	'Print "null drone target"
EndIf

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


'circle / aim towards the target space
s.controller.vars.set("holdrad", circledist)
AI.holdingPattern(s)

'go in the decided direction
AI.go(s)


