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

Rem
'has an offset firing pattern from other turrets
If s.controller.vars.get("scrambled") = 0
	s.controller.vars.set("scrambled", 1)
	For Local g:Gun = EachIn s.gunGroup[0]
		Local bump = Rand(0, timerMaxTime(g.timerList, "fireDelay"))
		timerBump(g.timerList, "fireDelay", bump)
	Next
EndIf
EndRem

vars.init("turnspeed", 180)
vars.init("aimspeed", 24)


'look for a target
AI.getTarget(s, 4000)
	
'act as a turret
If target <> Null And Not s.controller.target.cloaked 
	'face the target
	'setGoalTo(s, "target")
		
	'strafe around sometimes
	timerInit(timerList, "firing", 2500)
	timerLoop(timerList, "firing", False)
	timerInit(timerList, "strafing", 1500)
	timerLoop(timerList, "strafing", False)
	
	If timerTick(timerList, "firing")
		timerSet(timerList, "strafing")
		s.controller.goalx = s.x + Rand(50,150) * binsgn(Rand(0,1))
		s.controller.goaly = s.y + Rand(50,150) * binsgn(Rand(0,1))
	EndIf
	If timerTick(timerList, "strafing") Then timerSet(timerList, "firing")
	
	If Not timerDinged(timerList, "strafing")
		'fly in circles around the goal
		'holdingPattern(s)
		'push the go coord in the new direction
		Local dx = (s.controller.goalx - s.x) / 10
		Local dy = (s.controller.goaly - s.y) / 10
		If Abs(dx) > 8 Or Abs(dy) > 8
			s.controller.gox:+ dx
			s.controller.goy:+ dy
		Else
			If s.speed > 5 Then s.speed = s.speed - 5*timeMod Else s.speed = 0
		EndIf
		
		'turn fast
		AI.face(s, "target", vars.get("turnspeed"))
		
		AI.setProximityAvoid(s)

		AI.go(s, 1)
	Else
		'fire if within range
		'If approxDist(s.x-target.x,s.y-target.y) <= rangeGroup(s, 0) 
			'fireAIWeapons(s, 0, target.x,  target.y)		'fire at current target coords
			AI.fireAIWeapons(s, 0)								'fire at predicted target coords
		'EndIf
		
		vars.init("turnspeed", 180)
		
		'turn slow
		AI.face(s, "target", vars.get("aimspeed"))

		'slow down
		If s.speed > 5 Then s.speed = s.speed - 5*timeMod Else s.speed = 0
	EndIf
Else

	'slow down
	If s.speed > 5 Then s.speed = s.speed - 5*timeMod Else s.speed = 0
EndIf


