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

'Print "updating bomb AI"

If s.spin = 0 Then s.spin = binsgn(Rand(0,1)) * Rand(1,3) / 3

'add a caution effect
If s.controller.vars.get("caution") = 0 Then s.controller.vars.set("caution", 1)

'slightly randomize the range of the tractor beam to spread them out a little
If s.controller.vars.get("init") = 0
	For Local g:GunBeam = EachIn s.gunGroup[0]
		g.shotrange:+ (g.shotrange * (Rnd() - .5))/2
		s.controller.vars.set("signature", Abs(g.shotdamage))
	Next
	s.controller.vars.set("init", 1)
EndIf

Local towRange# = rangeGroup(s, 0)

If s.controller.target = Null
	'slow down
	'If s.speed > 50 Then s.speed = s.speed - 5
	If s.speed > 1 Then s.speed = s.speed - 1*timeMod Else s.speed = 0

	'look for target
	If s.controller.hasTimerTicked("targetCheck", 1000)
		'make a list of things to ignore, AKA self
		Local ignoreList:TList = New TList
		ignoreList.addLast(s)
		
		'find an enemy to latch onto
		Repeat
			s.controller.target = closestShip(s.x, s.y, s.faction, -1, towRange, ignoreList)
			If s.controller.target <> Null Then ignoreList.addLast(s.controller.target)
		Until s.controller.target = Null Or (s.controller.target.behavior <> "cryopod" And Not s.controller.target.ord)'(not other cryopods or ordinance)
	EndIf

Else
	'are we still in range of them?
	Local dist# = approxDist(s.x-s.controller.target.x,s.y-s.controller.target.y)
	
	If dist <= towRange
		'tractor beam to them
		AI.fireWeapons(s, 0, s.controller.target.x, s.controller.target.y)
	Else
		s.controller.target = Null
	EndIf
EndIf

'Print "   bom splode?"

'explodes on sufficient damage or reached target or fuse (fuse set when gun fires it) goes off
Local splode = False
If (s.armour / s.stat.armour) <= .5 Then splode = True
If s.controller.hasTimerTicked("fuse") Then splode = True

'display a countdown
vars.init("countdown", -1)
Local count = Ceil(timerTime(s.controller.timerList, "fuse") / 1000) 
If vars.get("countdown") <> count
	If count <= 2 Then new_text((count+1) + "...", s.x, s.y-20)
	vars.set("countdown", count)
EndIf

If splode
	AI.fireAIWeapons(s, 0)
	AI.fireAIWeapons(s, 1)
	s.armour = 0
	'Print "       yes"
EndIf


'clear the initial owner after a half-second
timerInit(s.controller.timerList, "clearowner", 1500)
timerLoop(s.controller.timerList, "clearowner", 0)
If timerTick(s.controller.timerList, "clearowner", 1500) Then ClearList(s.ignoreList)




