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

'start out happy with where we are
If s.controller.goalx = 0 Then s.controller.goalx = s.x
If s.controller.goaly = 0 Then s.controller.goaly = s.y

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

s.iconable = 0

'be colorful
timerInit(timerList, "rainbow", 6000, True)
timerLoop(timerList, "rainbow", True)
timerTick(timerList, "rainbow")
Local colormod# = timerTween(timerList, "rainbow") * 360

'if they're still friendly
If s.faction = 4
	'small cuttlefish look for bigger ones
	If s.behavior = "cuttlefish_small"
		If target = Null
			Local tardist = 3000
			If timerTick(timerList, "bosssearch", 4000 + Rand(0,1000))
				For Local tar:Ship = EachIn shipList
					If tar.behavior = "cuttlefish"
						Local cuttledist = 3000
						If s.controller.target <> Null Then cuttledist = approxDist(s.x-s.controller.target.x, s.y-s.controller.target.y)
						If s.controller.target = Null Or cuttledist < tardist
							s.controller.target = tar.placeholder
							tardist = cuttledist
						EndIf
					EndIf
				Next
			EndIf
	
		Else
			s.controller.goalx = target.x
			s.controller.goaly = target.y
		
			If target.dead Then target = Null
		EndIf
		
	
		
		AI.setProximityAvoid(s, 75)
		
		s.controller.vars.init("holdrad", Rand(100, 200))
	Else
		s.controller.vars.init("holdrad", 400 + Rand(0,600))
		
		If Abs(s.controller.goalx) > (stage.width-300) Then s.controller.goalx = Rand(-(stage.width-300), (stage.width-300))
		If Abs(s.controller.goaly) > (stage.height-300) Then s.controller.goaly = Rand(-(stage.height-300), (stage.height-300))
	EndIf

	s.RGB[0] = constrain(Cos(colormod) * 55 + 200, 100, 255)
	s.RGB[1] = constrain(Cos(colormod + 90) * 55 + 200, 100, 255)
	s.RGB[2] = constrain(Cos(colormod + 180) * 55 + 200, 100, 255)
	
Else'if they've been hurt
	'look for JERKS
	AI.getTarget(s, 800)
	
	'be red
	s.RGB[0] = constrain(Cos(colormod) * 40 + 210, 180, 250)
	s.RGB[1] = constrain(Cos(colormod + 90) * 70 + 20, 20, 90)
	s.RGB[2] = constrain(Cos(colormod + 180) * 70 + 20, 20, 90)
	
	'run away from things
	If s.controller.target <> Null
		s.controller.vars.set("holdrad", 30)
	
		Local angle# = ATan2(s.y-s.controller.target.y, s.x-s.controller.target.x)
																
		s.controller.goalx = s.controller.target.x' + Cos(angle)*1000
		s.controller.goaly = s.controller.target.y' + Cos(angle)*1000
		s.afterburn = 1
	
	Else'just wander
		s.controller.vars.set("holdrad", 200)
	
		'COPYPASTA
		If Abs(s.controller.goalx) > (stage.width-300) Then s.controller.goalx = Rand(-(stage.width-300), (stage.width-300))
		If Abs(s.controller.goaly) > (stage.height-300) Then s.controller.goaly = Rand(-(stage.height-300), (stage.height-300))
	EndIf
EndIf

'sad coo if gets hurt
timerInit(timerList, "sadcoo", 8000)
timerLoop(timerList, "sadcoo", False)
timerTick(timerList, "sadcoo")
If timerDinged(timerList, "sadcoo")
	If s.recent_damage > 1
		playLocationalSFX(getSound("misc_coo_bad1.wav"), Null, s.x, s.y)
		timerSet(timerList, "sadcoo")
		
		'get the net ID of the jerk nemesis ship
		If s.faction = 4'(if still friendly)
			For Local n:Ship = EachIn shipList
				If n.placeholder.idnum = s.placeholder.nemesis_idnum'this is the one that hurt us!
					If n.faction = 1'if it was a player-associated thing
						s.faction = 7'change to a mean alien faction
					EndIf
				EndIf
			Next
		EndIf
	EndIf
EndIf

'occasional happy coo if not hurt
timerInit(timerList, "coo", 8000 + Rand(0,8000))
timerLoop(timerList, "coo", False)
timerTick(timerList, "coo")

timerInit(timerList, "chirp", 1500 + Rand(0,2000))
timerLoop(timerList, "chirp", False)
timerTick(timerList, "chirp")

If s.recent_damage <= 0
	If timerDinged(timerList, "coo")
		If s.recent_damage <= 0
			If s.behavior = "cuttlefish"
				playLocationalSFX(getSound("misc_coo_good1.wav"), Null, s.x, s.y, .5 + Rnd()/2)
			Else
				playLocationalSFX(getSound("misc_coo_good2.wav"), Null, s.x, s.y, .5 + Rnd()/2)
			EndIf
			timerSet(timerList, "coo")
		EndIf
	EndIf
	
	If timerDinged(timerList, "chirp")
		playLocationalSFX(getSound("game_pointpickup.wav"), Null, s.x, s.y, .8 + Rnd()/5)
		timerSet(timerList, "chirp")
	EndIf
EndIf



'fly in circles around the goal
AI.holdingPattern(s)
		
AI.face(s, "go")
AI.go(s, 1)
s.add_thrust(0, 1.0, 0)

'run away if hurt
If s.recent_damage >= 1 Then s.afterburn = 1




	
