AI

Blitz3D Forums/Blitz3D Programming/AI

hi,

i've been trying some basics with AI but i ran into a problem which i am guessing is very simple, i just can't figure it out

here is my code:

Graphics3D 800,600,0,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()

Global cam = CreateCamera()
	PositionEntity cam,0,30,0
	RotateEntity cam,90,0,0

	
lit = CreateLight()


pl = CreatePlane()
	PositionEntity pl,0,-1,0
	EntityColor pl,0,0,0
	wdStatic(pl)
	

;bullet type
Type bullet
	Field shape
End Type

;enemy type
Type enemy
	Field shape,sight,turn#,move#
	Field state
	Field health
End Type


;good guy type
Type good
	Field shape
End Type

;enemy states of being
Global roam = 1
Global chase = 2
Global attack = 3
Global dead = 4


;create enemies
For x = 0 To 4

	e.enemy = New enemy
		e\shape = CreateSphere(2)
		EntityColor e\shape,0,0,255
		PositionEntity e\shape,Rand(-15,15),0,Rand(-15,15)
		EntityType e\shape,1
		e\turn = 1
		e\state = roam
		e\move# = .1
		e\health = 10

		

		
Next

;create the good guy
For x = 0 To 0

	g.good = New good
		g\shape = CreateSphere(2)
		PositionEntity g\shape,Rand(-20,20),0,Rand(-20,20)
		EntityPickMode g\shape,2
		EntityType g\shape,2
		
Next


;COLLISIONS

;enemy to enemy
Collisions 1,1,1,2

;enemy to good guy
Collisions 1,2,1,2
;goodguy to enemy
Collisions 2,1,1,2

;bullet to enemy
Collisions 3,1,1,1
;enemy to bullet
Collisions 1,3,1,1

;bullet to goodguy
Collisions 3,2,1,1
;goodguy = bullet
Collisions 2,3,1,1


br = 0

While Not KeyHit(1)

;update the enemies and good guy
For g.good = Each good

;player controls
If KeyDown(203) TurnEntity g\shape,0,1,0
If KeyDown(205) TurnEntity g\shape,0,-1,0
If KeyDown(200) MoveEntity g\shape,0,0,.2
If KeyHit(57) CreateBullet(g\shape)

;variable to control the rate of bullets from enemies
br = br + 1

For e.enemy = Each enemy

	dist# = EntityDistance(e\shape,g\shape)

	;if state of enemy is chase, chase the player
	If e\state = chase

		PointEntity e\shape,g\shape
		MoveEntity e\shape,0,0,e\move#

	;if roam then move around
	ElseIf e\state = roam
	
		TurnEntity e\shape,0,e\turn,0
		MoveEntity e\shape,0,0,e\move#
		
	; if attack then shoot at enemy
	ElseIf e\state = attack
	
		PointEntity e\shape,g\shape
		
		If br > 10
		
			CreateBullet(e\shape)
		
			br = 0
			
		EndIf
		
	; if dead stop from turning and moving
	ElseIf e\state = dead
	
		e\move = 0
		e\turn = 0
		
	EndIf
	
	;set enemy states according to current state
		If e\state <> dead
	
			If (dist < 10 And dist > 5)
	
				e\state = chase
		
			ElseIf (dist < 5 And dist > 0)		
		
				e\state = attack
				
			ElseIf (dist > 10)
		
				e\state = roam
		
			EndIf
		
		EndIf

		;if enemy health is less than or equal to 0 kill it
		If e\health <= 0
		
			e\state = dead

		EndIf
		
	
	;keep enemy from going off screen
	If EntityX(e\shape) < -17 Or EntityX(e\shape) > 17 Or EntityZ(e\shape) < -17 Or EntityZ(e\shape) > 17
	
		TurnEntity e\shape,0,3,0
		
	EndIf
	
	
Next








Next



UpdateBullet()

UpdateWorld
RenderWorld




Flip


Wend



;creates a bullet(sphere)
Function CreateBullet(source)


b.bullet = New bullet

b\shape = CreateSphere(2)

	ScaleEntity b\shape,.3,.3,.3
	RotateEntity b\shape,EntityPitch(source),EntityYaw(source),EntityRoll(source)
	PositionEntity b\shape,EntityX(source),EntityY(source),EntityZ(source)
	EntityType b\shape,3

End Function

; the bullet forward and destroys it when neccessary
Function UpdateBullet()

	For b.bullet = Each bullet
	
		MoveEntity b\shape,0,0,1
	
		For e.enemy = Each enemy
	
		
		If EntityDistance(b\shape,cam) > 50
		
			FreeEntity b\shape
			Delete b
		
		
		; this is the problem area---------\/\/\/\/\/\/\/\/\/\/\/\/\/\/;i keep getting a MAV saying 'collsion index out of range'	 
		ElseIf CollisionEntity(  b\shape , CountCollisions( b\shape )  ) = e\shape
					
			e\health = e\health - 1
				
			FreeEntity b\shape
			Delete b
			
		ElseIf EntityCollided(b\shape,2)
		
			FreeEntity b\shape
			Delete b
			
		EndIf
		
		Next
				
	Next
	
End Function

























End


my problem is at CollisionEntity(b\shape,countcollisions(b\shape))

i get a MAV saying 'collision index out of range'

any suggestions?