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

'start out happy with where we are
s.controller.vars.init("homex", s.x)
s.controller.vars.init("homey", s.y)'heehee homey

'if escorting the player, do that
If s.controller.vars.get("escortplayer")
	If ListIsEmpty(s.controller.targetList) And timerTick(s.controller.timerList, "lookforplayer", 1000)
		For Local pship:Ship = EachIn shipList
			If pship.behavior = "player" Then s.controller.targetList.addLast(pship.placeholder)
		Next
	EndIf
	
	For Local p:Gertrude = EachIn s.controller.targetList
		s.controller.vars.set("homex", p.x)
		s.controller.vars.set("homey", p.y)
	Next
EndIf

'when approaching a target, get a unique angle to strafe
s.controller.vars.init("strafeangle", Rand(-100, 100))

If s.controller.goalx = 0 Then s.controller.goalx = s.x
If s.controller.goaly = 0 Then s.controller.goaly = s.y

'fly in circles around the goal (desired distance will change depending on claw gun state)
AI.holdingPattern(s)

'look for a s.target
If s.controller.vars.get("escortplayer")
	AI.getTarget(s, 500, 500)
Else
	AI.getTarget(s, 1000)
EndIf


If target = Null
	'set the goal coords back home
	s.controller.goalx = s.controller.vars.get("homex")
	s.controller.goaly = s.controller.vars.get("homey")

	'come back to some base holding pattern
	Local holdrad = s.controller.vars.get("holdrad")
	If holdrad < 100 Or holdrad > 300 Then s.controller.vars.set("holdrad", Rand(100,300))

	'go around in circles
	AI.face(s, "go")
	AI.go(s, 1)
	
Else
	'face the target
	AI.setGoalTo(s, "target")
	
	Local throttle# = 1
	
	Local dist = approxDist(target.x - s.x, target.y - s.y)
	
	'go close or far depending on gun state
	For Local g:Gun = EachIn s.gunGroup[0]
		'based on the first one we find : check if it's ready to fire/firing
		If timerDinged(g.timerList, "fireDelay") Or g.burst > 0
			s.controller.vars.set("holdrad", 1)'be super close
			
			'if we're actually pretty far away, strafe a little
			If dist > 100 Then s.add_thrust(s.controller.vars.get("strafeangle"), 1.0)
		Else
			s.controller.vars.set("holdrad", 300)'be super far away
		EndIf
		'throttle based on reload state
		If g.burst > 10 Then throttle = 2.5
		
		Exit
	Next
	
	'face the target & go in the direction we decided	
	AI.face(s, "goal")
	
	'shoot if we're close
	If approxDist(s.x-s.controller.goalx,s.y-s.controller.goaly) <= 140
		AI.fireAIWeapons(s, 0, s.controller.goalx, s.controller.goaly)	'fire at current target coords
	Else
		throttle = 1'if far away, normal speed
	EndIf
	AI.go(s, throttle)
EndIf




