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

'can't go through gates (until we're jumpstarted or it's time to leave
s.controller.vars.init("gateworthy", 0)

'should we head for the gate?
vars.init("stageend", 0)
'has the player come near yet?
vars.init("jumpstarted", 0)

'should we shoot at enemies?
vars.init("weaponshot", 1)

'tell no lies
s.noItemDrop()

'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


'if we've been jumpstarted
If vars.get("jumpstarted") = 1
	
	'try to avoid collisions: sets gox & goy to point away from nearby ss
	Local avoidrad = TSIZE
	If vars.get("stageend") = 1 Then avoidrad = 50
	AI.setProximityAvoid(s, avoidrad)
	AI.holdingPattern(s)

	'if the stage is not over yet
	If vars.get("stageend") = 0
		
	
		vars.init("holdrad", 400)
		Local circledist = Min(rangeGroup(s, 0) - 25, vars.get("holdrad"))
		
		'if have no weapons, ram!
		If ListIsEmpty(s.gunGroup[0]) Then circleDist = 5
		
		vars.set("holdrad", circledist)
				
		'fly in circles around the goal
		'holdingPattern(s)
		
		'look for a s.target
		AI.getTarget(s, 3900)
		
		If target = Null
			AI.face(s, "go")
			
			'Local throttle = 1
			's.add_thrust(0, throttle, 0)
		Else
		
			'set the goal coords to the target (to circle them)
			AI.setGoalTo(s, "target")
			
			'face the target & go in the direction we decided	
			AI.face(s, "goal")
	
			'shoot
			'fireAIWeapons(s, 0, target.x,  target.y)'fire at current target coords
			If vars.get("weaponshot") Then AI.fireAIWeapons(s, 0)'fire at predicted target coords
		EndIf
	
	'if it's time to head for the gate
	Else
	
		'if they could shield themselves all along, why weren't they doing it earlier huh!!!
		'(this is so they don't kill themselves by running into the gate or some bollocks like that)
		'('cause it's way easier to do this than actually making the AI smart)
		s.addDamageBuffer(6)
	
		's.hitTween = 1
		AI.setGoalTo(s, "center")
		vars.set("holdrad", 50)
		
		vars.set("gateworthy", 1)
		
		AI.face(s, "go")
		'go(s, 1)
	EndIf
	
	
	AI.go(s, 1)
	
'if we're just chillin waiting for the player to come near
Else

	'look for the player
	If target = Null
		If timerTick(timerList, "searchforplayer", 1000)
			For Local p:Ship = EachIn shipList
				If p.behavior = "player"
					s.controller.target = p.placeholder
					Exit
				EndIf
			Next
		EndIf
	Else
		'wait for the player to get near
		Local wid = 10+(s.wid/2) * s.hitScale
		Local het = 10+(s.het/2) * s.hitScale
		Local owid = (target.wid/2)
		Local ohet = (target.het/2)
		
		Local colliding = OvalsCollide(s.x,s.y,wid,het,s.rot,target.x,target.y,owid,ohet,target.rot)
		
		'If approxDist(target.x - s.x, target.y - s.y) <= Max(s.wid, s.het)+20
		If colliding
			'juicy!
			new_text("JUMPSTARTED!", s.x, s.y - 50)
			playLocationalSFX(getSound("misc_jumpstart.wav"), Null, s.x, s.y)
			Local e:Emitter = new_emitter("emp_burst")
			e.x = s.x
			e.y = s.y
			e.upload()

			'attache a radio effect to them
			'Local rad:Emitter = new_emitter("radio")
			'rad.attach(s)
			'rad.upload()

			vars.set("jumpstarted", 1)
			
			vars.set("gateworthy", 1)
			
			'stage script triggers speech
		EndIf
	EndIf
	
	timerInit(s.timerList, "distress", Rand(1600,1800))
	If timerTick(s.timerList, "distress")
		Rem
		Local txt$
		Select Rand(1,5)
		Case 1
			txt = "SOS!"
		Case 2
			txt = "Distress beacon detected!"
		Case 3
			txt = "Human lifesigns!"
		Case 4
			txt = "Help me!"
		Case 5
			txt = "Immediate assistance requested!"
		EndSelect
		EndRem
		
		Local txt$ = "Apply percussive maintenance!"
		new_text(txt, s.x, s.y - 20)
	EndIf

	
	AI.face(s, "go")
EndIf


'for chatbot
If Lower(s.name) = "chatbot"
	's.attractItem("point")
	If s.armour <= 0 And ListIsEmpty(shakytextList) And Not s.abilityHave("Ankh") Then new_shakytext("VIRUS SAVED TO HARD-DRIVE")
Else
	'SIGNAL LOST on death
	If s.armour <= 0 And ListIsEmpty(shakytextList) Then new_shakytext("SIGNAL LOST")
EndIf
