AI

BlitzMax Forums/BlitzMax Programming/AI

ok basically what is happening is, I have several enemy ship objects.

I want them each to fire a missile at the player, (obviously based on certain conditions)

problem I am having is, only ship at a time is able to fire, and it is the last ship I instance. You press a button to add in enemies. So as you press, the new enemy picks up the firing. And you see the missile coming from that ship instead.

Interestingly as well, the amount of damage being done by this "single" missile hitting your ship, is equivelant to the amount of enemy ships you have. FOR example, if a missile does 10 damage from one hit, if you have 5 enemeies instances, the missile that is fired does 50 damage.

enemy missile Type:
Type Etmissile Extends tmissile
	Field lastfired%
	Global list:TList = CreateList()
	
	
	Function FireTorp()			
		'creates the missile object and fires it really.
		Firemissile (ship.x , ship.y)   				
	End Function
	
	
	Function Update()
		'Local eachenemy:tenemyship
		For emissile:etmissile = EachIn List 
			emissile.age :+ 1		
			emissile.frame:+ 1	
			If emissile.frame = 30 Then emissile.frame = 0
				
			emissile.x :+ Sin(emissile.angle) * (emissile.Speed)' + eachEnemy.current_speed *delta
			emissile.y :- Cos(emissile.angle) * (emissile.Speed)' + eachEnemy.current_speed *delta
				
			'remove missile if is older than 150
			If emissile.age > 150 
				PlaySound(missileExplosionFx:TSound)
				RemoveLink emissile.link
			End If 			 
		Next
	End Function 

	
	Function draw()
		For Local all:etmissile = EachIn list
			SetScale 1 + newscale, 1.0 + newscale'scales the missilees according to zoom
			SetBlend ALPHABLEND
			SetColor(255,255,255)
			MidHandleImage (all.photonImage)
			DrawImage(all.PhotonImage,all.x,all.y,all.frame)
		Next
	End Function
	'=======================================================================================
	'create a new object missile based on the type weapon, adds each to a list, and appends
	'deletes from the list as each missilees lifetime goes to 0 or as you fire.
	'=======================================================================================
	Function Firemissile(x#,y#)
		'controls how rapid you can fire missilees. 100= 100 split seconds
		If (MilliSecs() - emissile.lastFired) < 600 Then Return 				
					
		Local emissile:etmissile = New eTmissile			
		PlaySound(sound.CurrentmissileSound)
		emissile.frame = 0		
		
		For Local eachenemy:tenemyship = EachIn tenemyship.eshiplist
			emissile.angle = ATan2( eachEnemy.x - ship.x , ship.y - eachEnemy.y )+180 
			emissile.lastFired = MilliSecs() 
			emissile.x = eachEnemy.x + (Sin(emissile.angle))  * delta'so torp orgin from ship
			emissile.y = eachEnemy.y + (Cos(emissile.angle))  * delta
		Next
		
 		emissile.age = 0
		emissile.link = ListAddLast(list,emissile)			
	End Function
	
	
		
End Type





This is called in an AI() function in the main loop

'fires enemy missiles 
Function Fire()
	Local weaponRange = 600
	
	'fire if distance is less than weapon range
	For Local eachenemy:tenemyship = EachIn tenemyship.eshiplist	
		If ListIsEmpty(tenemyship.eshiplist) = False  
			If ListIsEmpty(ship.shiplist) = False
				If ship.iscloaked = False
					If EachEnemy.iscloaked = False
						If eachenemy.distance < weaponRange
							etMissile.fireTorp()	
						End If 
					End If 
				End If
			End If 
		End If
	Next
End Function





in the main loop "AI()" is before these:


etMissile.update()
etMissile.draw()

of course I want each enemy ship to fire on the player. so hence the for each loop in the fire() function

there there is a for each loop in the FireMissile() function of the enemy missile type because, the missile must originate from the position of the enemy ship that fired it, and the angle/direction of the missile etc, must be calculated based from the ship that fired it, hence for that piece of code.

fire missile is basically a create() function