'== included in the AI scripts file =='
If s.spin = 0 Then s.spin = binsgn(Rand(0,1)) * 1.5		

'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)

Rem
Local targetRange = 3000

'look for targets (ALLIED)
s.controller.vars.init("checkedFirst", 0)
s.controller.vars.init("checkinterval", 5000 + Rand(-200,200))
If s.controller.hasTimerTicked("multiTargetCheck", s.controller.vars.get("checkinterval")) Or s.controller.vars.get("checkedFirst") = 0
	s.controller.vars.set("checkedFirst", 1)
	
	'clear the target list
	ClearList(s.controller.targetList)
	
	'"target" ourselves, to be included in the ignore list
	s.controller.targetList.addLast(s.placeholder)
	
	'and get new targets
	Local tar:Gertrude
	Repeat
		'look for all nearby 
		tar = closestShip(s.x, s.y, s.faction, 1, targetRange, s.controller.targetList)
		If tar <> Null Then s.controller.targetList.addlast(tar)
	Until tar = Null'until we can't find a valid target within range
	
	'remove all targets that aren't other nodes (and ourselves)
	For Local tar:Gertrude = EachIn s.controller.targetList
		If (tar.behavior <> "node" And tar.behavior <> "node2") Or s.placeholder = tar Then ListRemove(s.controller.targetList, tar)
	Next

EndIf
EndRem

'Local circledist = 300
Local beamrange# = rangeGroup(s, 0)

'look for an enemy target
AI.getTarget(s, 4000)
If target <> Null

	'pew pew at player
	vars.set("dumbfire", 1)'(fire at location, not predicted position)
	fireShipWeapons(s, 1)
	
	'if it's time to blink
	timerInit(s.controller.timerList, "blink_interval", 6000 + Rand(-500,500))
	
	'do we blink or move?
	Local blink = False
	Local move = False
	Local census = False
	If timerTick(s.controller.timerList, "blink_interval") Then blink = True
	If s.controller.vars.get("move") = 1
		blink = False
		move = True
	EndIf
	
	'should we recalculate our movetarget?
	If move And timerTick(s.controller.timerList, "move_interval", 2500) Then move = True Else move = False
	
	'should we take a census?
	If timerTick(s.controller.timerList, "census_interval", Rand(2000,3000)) Then census = True
	
	If blink Or move Or census
		
		ClearList(s.controller.targetList)
		
		'look for other nodes
		Local nodenum# = 1	'number of found other nodes (plus us, for starters)
		Local node# = 1		'which node we are (can't be less than the first)
		For Local other:Ship = EachIn shipList
			If other.faction = s.faction And (other.behavior = "node" Or other.behavior = "node2") And (other <> s)
				nodenum:+ 1
				If other.netspriteIDNUM < s.netspriteIDNUM Then node:+ 1
				
				'connected nodes reset their blue beams
				If blink And ListContains(other.controller.targetList, s.placeholder)
					For Local g:GunBeam = EachIn other.gunGroup[0]
						g.heat = 0
					Next
				EndIf
				
				'keep track of the nodes we want to beam (beam to only every third node)
				Local track = False
				If vars.get("nodenum_old") <= 3
					track = True'only three nodes: all track eachother
				ElseIf vars.get("nodenum_old") <= 5
					If ((vars.get("nodeid") + other.controller.vars.get("nodeid")) Mod 2 = 1) Then track = True
				ElseIf vars.get("nodenum_old") <= 8
					If ((vars.get("nodeid") + other.controller.vars.get("nodeid")) Mod 4 = 1) Then track = True
				EndIf

				If track Then s.controller.targetList.addLast(other.placeholder)
				
				'also reset their blink interval (to get the whole swarm to teleport at once)
				If blink And timerTween(other.controller.timerList, "blink_interval") < .8
					timerBump(other.controller.timerList, "blink_interval", 100)
				EndIf
				
				'CAP the number of nodes on a map to, say, 6.
				If node > 8
					s.noItemDrop()
					s.armour = 0
				EndIf
			EndIf
			'force all other nodes to finish their countdowns as well
			'If other <> s And timerTime(other.controller.timerList, "blink_interval") <= 15000
			'	timerSet(other.controller.timerList, "blink_interval", Rand(0,60))
			'EndIf
		Next
		vars.set("nodenum_old", nodenum)
		vars.set("nodeid", node)
		
		
		
		'when we blink, reset our beams as well
		If blink
			For Local g:GunBeam = EachIn s.gunGroup[0]
				g.heat = 0
			Next
		EndIf
		
		'if run out of other nodes, move towards the player
		If nodenum = 1 Then vars.set("move", 1)
		
		If nodenum >= 1
			Local theta# = (node / nodenum) * 360
			'Print "blink:   node " + node + "    nodenum " + nodenum
			Local dist = 600 + Rand(-100,200)
	
			AI.setGoalTo(s, "target")
			'Print " -- adv node goal target at " + s.controller.goalx + ", " + s.controller.goaly
			'Print " ---- tarx adds " + Cos(theta)*dist + " from theta " + theta + " and dist " + dist
			Local tarx = s.controller.goalx + Cos(theta)*dist
			Local tary = s.controller.goaly + Sin(theta)*dist
			
			tarx = constrain(tarx, -(stage.width-100), stage.width-100)
			tary = constrain(tary, -(stage.height-100), stage.height-100)
		
		
			'blink?!
			If blink Then s.activateAbility(tarx, tary)
			'move?!
			If move
				'Print "-> setting adv node goal to " + tarx + ", " + tary
				s.controller.goalx = tarx
				s.controller.goaly = tary
			EndIf
		EndIf
	EndIf
	
	'move?!
	If s.controller.vars.get("move") = 1
		'try to avoid collisions: sets gox & goy to point away from nearby ss
		AI.setProximityAvoid(s)
		'push the go coord in the new direction
		s.controller.gox:+ (s.controller.goalx - s.x)
		s.controller.goy:+ (s.controller.goaly - s.y)
		'Print "          adv node go dir " + s.controller.gox + ", " + s.controller.goy
		AI.go(s, 1)
	EndIf
EndIf


'if we found other nodes to connect with
If Not ListIsEmpty(s.controller.targetList)
	'Print "NODEBEAMFIRE"
	Local i = 0
	For Local tar:Gertrude = EachIn s.controller.targetList
		'lose dead + stunned targets, can only have a limited number of targets
		If tar.dead' Or s.emp_timer > 0' Or i >= 2
			ListRemove(s.controller.targetList, tar)
			Continue
		EndIf

		'if within range
		Local dx = tar.x-s.x
		Local dy = tar.y-s.y
		Local dist# = approxDist(dx, dy)
		If dist < beamrange
			'fire NODE BEAM at the other target
			fireShipWeapons(s, 0, tar.x, tar.y)
		EndIf		
		i:+ 1
	Next
EndIf


'If s.controller.target <> Null Then fireShipWeapons(s, 1, s.controller.target.x, s.controller.target.y)


'circle / aim towards the target space
's.controller.vars.set("holdrad", circledist)
'holdingPattern(s)

'go in the decided direction
'go(s)

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


