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

'can go through gates
s.controller.vars.init("gateworthy", 1)

'get input from the keyboard/mouse/joystick information
s.controller.downloadKeyboardInput()

'detect how far mouse is from the ship's location
Local m_dx# = (s.controller.aimx - s.x)
Local m_dy# = (s.controller.aimy - s.y)
Local m_theta# = ATan2(m_dy,m_dx) + 90

If m_theta >= 180 Then m_theta = m_theta - 360
If m_theta < -180 Then m_theta = m_theta + 360
Local m_radius = Floor(Sqr(m_dx^2 + m_dy^2))

'find which direction To turn To face the mouse
Local rotTar# =  m_theta - 90
If rotTar >= 180 Then rotTar = rotTar - 360
If rotTar < -180 Then rotTar = rotTar + 360


'fighters face the mouse and can strafe
If s.base.class = "fighter"
	'face the mouse
	s.turn(rotTar, 1)
	'strafe
	'If s.controller.strafeleft = 1 Then s.add_thrust(270, 1.0)
	'If s.controller.straferight = 1 Then s.add_thrust(90, 1.0)
	
'frigates turn when we move
ElseIf s.base.class = "frigate"
	If s.controller.afterburn Or s.controller.thrust Then s.turn(rotTar, 1)
	s.add_thrust(0,1.0)

'	If s.controller.strafeleft = 1 Then s.turn(-90, 0)
'	If s.controller.straferight = 1 Then s.turn(90, 0)
EndIf

'manuveur according to the input
If s.controller.afterburn = 1
	s.add_thrust(0,1.0)
	s.afterburn = 1
	timerBump(s.timerList, "barrierTimer", BARRIER_DELAY)

ElseIf s.base.class = "fighter"
	'KEYBOARD thrust commands
	'thrusting all geometry-wars like
	If s.controller.thrust = 1 Then s.add_thrust(0,1.0)
	
	'If s.controller.thrust = 1 Then s.add_thrust(270,1.0,True)
	'If s.controller.reverse = 1 Then s.add_thrust(90,1.0,True)
	'If s.controller.strafeleft = 1 Then s.add_thrust(180,1.0,True)
	'If s.controller.straferight = 1 Then s.add_thrust(0,1.0,True)
EndIf

'mouse controls!
If s.controller.fire1 = 1
	AI.fireWeapons(s, 0, s.controller.aimx, s.controller.aimy)
	If s.barrier > 0 And s.barrierhold = 0
		s.barrierhold = s.barrier
		s.barrier = 0
	EndIf
	timerBump(s.timerList, "barrierTimer", BARRIER_DELAY)
Else
	If s.barrierhold > 0 And s.barrier = 0
		s.barrier = s.barrierhold
		s.barrierhold = 0
	EndIf
EndIf
If s.controller.fire2 = 1 Then AI.fireWeapons(s, 1, s.controller.aimx, s.controller.aimy)

'switch weapons
'If s.controller.switch Then pilot.cycleweapons()

'activate shields/other ability?
If s.controller.ability = 1 Then s.activateAbility(s.controller.aimx, s.controller.aimy)

'attract points
s.attractItem("point")
s.attractItem("heal")
'If s.juice < s.stat.juice Then s.attractItem("point")


'cap amount of damage a player can take in a short time -- nobody likes to be insta-gibbed
If s.base.material <> MATTER_VECTOR
	s.controller.vars.init("armour_old", s.armour)
	
	timerInit(timerList, "gibshield_active", 2500)'(start the map with a couple of seconds of invulnerability)
	timerLoop(timerList, "gibshield_active", 0)
	
	'if we took X damage in the last 750 ms, heal and become invulnerable
	Local gibthreshold# = (s.stat.armour * .3)
	Local gibactivate = False
	If vars.get("armour_old") - s.armour >= gibthreshold
		gibactivate = True
		s.armour = vars.get("armour_old") - gibthreshold
		s.recent_damage = gibthreshold
	EndIf
	
	'give players a last chance
	If ListContains(s.shapeBonusList, "symmetrical") And s.stat.armour > 0
		If vars.get("armour_old") > 1 And s.armour <= 1 And timerDinged(timerList, "gibshield_active")
			'trigger the gibshield
			s.armour = .99
			s.recent_damage = vars.get("armour_old") - 1
			gibactivate = True
			
			'make it obvious what happened
			new_shakytext("-- LAST CHANCE --")
			'new_text("Last chance!", s.x, s.y+20)
			
			'make a splosion
			Local debrisList:TList = New TList
			debrisList.addLast(new_emitter("shipexplode"))		'make a ship explosion
			debrisList.addLast(new_emitter("shipexplode_slow"))	'make a ship explosion
			debrisList.addLast(new_emitter("shipexplode_smoke"))'make a ship explosion
			For Local e:Emitter = EachIn debrisList
				e.y = s.y
				e.x = s.x
				e.num = e.num * Ceil( s.mass / 25 )
				e.upload()
			Next
		EndIf
	EndIf
	
	'i don't understand how this works anymooooore...!
	If gibactivate Then timerSet(timerList, "gibshield_active", 2000)
	
	If Not timerDinged(timerList, "gibshield_active") And s.base.material <> MATTER_VECTOR
		s.addDamageBuffer(30)
	EndIf
	timerTick(timerList, "gibshield_active")
	
	'whenever you take damage and we're not counting down, reset the damage countdown and freeze armour_old
	timerInit(timerList, "gibshield", 750)
	If timerDinged(timerList, "gibshield")
		If s.controller.vars.get("armour_old") > s.armour Then timerSet(timerList, "gibshield")
		s.controller.vars.set("armour_old", s.armour)
	Else
		timerTick(timerList, "gibshield")
	EndIf


EndIf