AI

BlitzMax Forums/BlitzMax Programming/AI

The Forum Archive is locked. These are the original blitzbasic.com forums, kept so their posts can still be read and searched. No new topics, replies or edits can be made here. To join the conversation, head over to the Blitz3D+ forums.

because the enemy ships are at different positions each missile that gets fired needs to calculate the direction to fire the missile at, based off of where the enenmy ship that is firing at you is located
Not a problem because you (or at least your list of tenemy) knows the position of each tenemy and you know the position of the player ship.
When it's time for a tenemy to fire simply has a fire function/method within your tenemy type passing the tenemy x/y and the global playership x/y and use that information to call a tmissile.create(x,y,playerx,playery) function.
Once the tenemy has fired it's missile it can forget about it or, if you need to know which tenemy fired add it to a list of tmissiles specifically for that tenemy.
Once you have a list of tmissiles that are active the tmissile.update and tmissile.draw function can handle their movement.
This code doesn't have the ability to target anything but you can add the code for that...
SuperStrict
SeedRnd MilliSecs()
Type tenemy
	Global list:tlist=CreateList()
	Field firetime:Int
	Field lastfired:Int
	Field x:Float
	Field y:Float
	Field speed:tvector
	Function create()
		For Local x:Int = 1 To 5
			Local temp:tenemy = New tenemy
			 temp.firetime = Rand(1000 , 5000)
			 temp.lastfired=MilliSecs()
			 temp.x = Rand(10 , GraphicsWidth() )
			 temp.y = Rand(10 , GraphicsHeight() )
			 temp.speed = tvector.create()
			 ListAddLast list,temp
		Next
	End Function
	Function update()
		For Local all:tenemy = EachIn list
			all.x :+all.speed.x
			all.y :+all.speed.y
			If all.x > GraphicsWidth() all.speed.x = - all.speed.x
			If all.y > GraphicsHeight() all.speed.y = - all.speed.y
			If all.x < 0 all.speed.x = - all.speed.x
			If all.y < 0 all.speed.y = - all.speed.y
			If MilliSecs() > all.firetime + all.lastfired
				all.lastfired = MilliSecs()
				tmissile.create(all.x , all.y)
			EndIf				
		Next
	End Function
	Function Draw()
		For Local all:tenemy = EachIn list
			DrawRect all.x - 4 , all.y - 4 , 8 , 8
		Next
	End Function
End Type			
Type tvector
	Field x:Float
	Field y:Float
 	Function create:tvector()
		Local temp:tvector = New tvector
		temp.x = Rnd(-0.5 , 0.5) * 2.0
		temp.y = Rnd(-0.5 , 0.5) * 2.0
		Return temp
	End Function
End Type
Type tmissile
	Global list:tlist=CreateList()
	Field x:Float
	Field y:Float
	Field speed:tvector
	Field life:Int = 40000
	Field fired:Int=MilliSecs()
	Function create(x:Float , y:Float)
		Local temp:tmissile = New tmissile
			temp.x = x
			temp.y = y
			temp.speed = tvector.create()
			temp.speed.x = temp.speed.x* 4
			temp.speed.y =  temp.speed.y* 4
		ListAddLast list,temp
	End Function
	Function update()
		For Local all:tmissile = EachIn list
			all.x :+all.speed.x
			all.y :+ all.speed.y
			all.life:- (MilliSecs() - all.fired)
			If all.x > GraphicsWidth() Or all.x < 0 Or all.y > GraphicsHeight() Or all.y < 0 Or all.life < 0 
				ListRemove list , all
			EndIf
		Next
	End Function
	Function Draw()
		For Local all:tmissile = EachIn list
			DrawRect all.x - 2 , all.y - 2 , 4 , 4
		Next
	End Function
End Type	
Graphics 640 , 480
tenemy.create()
While Not KeyHit(KEY_ESCAPE)
	Cls
	tenemy.update
	tmissile.update
	tenemy.draw
	tmissile.draw
	Flip
Wend	

It's quick and dirty... as always.