Basic Wandering AI

Blitz3D Forums/Blitz3D Programming/Basic Wandering 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.

Watch 'em duke it out! Red usually wins for some odd reason. Might get a mav here and there...haven't sussed that out yet (needs some serious optimization anyway).


Global gameFPS = 50

Graphics3D 1024,768,32,2
SetBuffer BackBuffer()

SeedRnd MilliSecs()

Global camera = CreateCamera ()
CameraZoom camera,1.6
CameraRange camera,.1,100000
CameraClsColor camera,0,0,0
PositionEntity camera,0,140,-800

AmbientLight 150,150,150

light = CreateLight()
PositionEntity light,0,0,0

quit = False

framePeriod = 1000 / gameFPS
frameTime = MilliSecs () - framePeriod

redhome = CreateSphere()
ScaleEntity redhome,6,6,6
PositionEntity redhome,-400,10,0

bluehome = CreateSphere()
ScaleEntity bluehome,6,6,6
PositionEntity bluehome,400,10,0

Type cube
	Field mesh
	Field walktimer
	Field turntimer
	Field moving
	Field decision
	Field distfromhome
	Field id
	Field team
	Field level
	Field strength
	Field stamina
	Field dmg
	Field dly
	Field hp
	Field home
	Field angry
	Field timer
	Field dice
	Field damage
	Field alive
End Type

For i = 1 To 10
	c.cube = New cube
	c\id = i
	c\team = 1
	c\hp = 100
	c\dmg = 8
	c\dly = 1200
	c\stamina = 18
	c\strength = 20
	c\level = 10
	c\alive = True
	c\home = redhome
	c\mesh = CreateCube()
	EntityColor c\mesh,255,0,0
	ScaleEntity c\mesh,10,10,10
	PositionEntity c\mesh,EntityX(c\home),EntityY(c\home),EntityZ(c\home)
	d.cube = c.cube
Next

For i = 1 To 10
	c.cube = New cube
	c\id = i
	c\team = 2
	c\hp = 100
	c\dmg = 8
	c\dly = 1200
	c\stamina = 18
	c\strength = 20
	c\level = 10
	c\alive = True
	c\home = bluehome
	c\mesh = CreateCube()
	EntityColor c\mesh,0,0,255
	ScaleEntity c\mesh,10,10,10
	PositionEntity c\mesh,EntityX(c\home),EntityY(c\home),EntityZ(c\home)
	d.cube = c.cube
Next

Plane=CreatePlane()
EntityAlpha plane,.8
EntityColor plane,50,50,50
CreateMirror()

Repeat

	Repeat
		frameElapsed = MilliSecs () - frameTime
	Until frameElapsed

	frameTicks = frameElapsed / framePeriod

	frameTween# = Float (frameElapsed Mod framePeriod) / Float (framePeriod)

	For frameLimit = 1 To frameTicks

		If frameLimit = frameTicks Then CaptureWorld
		frameTime = frameTime + framePeriod
		
		If KeyHit(1) Then quit = True
		
		If KeyDown(203) Then TurnEntity camera,0,1,0
		If KeyDown(205) Then TurnEntity camera,0,-1,0
		If KeyDown(200) Then MoveEntity camera,0,0,2
		If KeyDown(208) Then MoveEntity camera,0,0,-2
		
		For c.cube = Each cube
		
			For d.cube = Each cube
		
				If d\alive = True
					
					If d\id <> c\id And d\moving = True
						avoid_entity( d\mesh, c\mesh, 2, 2 )
					EndIf
	
					If d\team <> c\team And EntityDistance (d\mesh,c\mesh) < 500
						c\angry = True
						dy#=DeltaYaw(d\mesh,c\mesh)*.1
						TurnEntity d\mesh,0,dy,0
						
						If EntityDistance (d\mesh,c\mesh) > 300
							MoveEntity d\mesh,0,0,4
						EndIf
						
						If d\timer + d\dly < MilliSecs()
						
							d\dice = Rnd(1,5)
							d\damage = Ceil((d\strength * d\stamina * d\dmg) * d\dice * d\level / 10000)
							c\hp = c\hp - d\damage
							d\timer = MilliSecs()
							
						EndIf
						
						If d\hp <=0 Then d\alive = False
						
					Else
						d\angry = False
					EndIf
				Else
					FreeEntity d\mesh
					Delete d
				EndIf
				
			Next
			
			If c\alive = True
			
				c\distfromhome = EntityDistance(c\mesh,c\home)
					
				If c\angry = False
					
					If c\moving = False
						c\decision = Rand(1,200)
						Select True
							Case c\decision = 10
								c\walktimer = MilliSecs() + 3000
							Case c\decision = 100
								c\turntimer = MilliSecs() + 1000
						End Select
					EndIf
			
					If c\walktimer > MilliSecs() 
						c\moving = True
						MoveEntity c\mesh,0,0,2
					Else If c\turntimer > MilliSecs()
						c\moving = True
						TurnEntity c\mesh,0,2,0
					Else
						c\moving = False
					EndIf
			
					If c\distfromhome > 400
						dy#=DeltaYaw(c\mesh,c\home)*.04
						TurnEntity c\mesh,0,dy,0
					EndIf
				
				EndIf
			Else
				FreeEntity c\mesh
				Delete c
			EndIf
			
		Next

		UpdateWorld

	Next
	
	RenderWorld frameTween
	
	For c.cube = Each cube
		CameraProject(camera,EntityX(c\mesh),EntityY(c\mesh),EntityZ(c\mesh))
		
		Color 0,255,0
			
		Rect ProjectedX()-25,ProjectedY()-20,c\hp/2,4

	Next
	
	Flip

Until quit = True

End

Function avoid_entity( scr_entity, dest_entity, rate#, spd# )

	If EntityDistance(scr_entity,dest_entity) < 100 Then
		MoveEntity scr_entity,0,0,spd#
		dist1# = EntityDistance(scr_entity,dest_entity)
		MoveEntity scr_entity,0,0,-spd#
		TurnEntity scr_entity,0,rate#,0
		MoveEntity scr_entity,0,0,spd#
		dist2# = EntityDistance(scr_entity,dest_entity)
		MoveEntity scr_entity,0,0,-spd#
		TurnEntity scr_entity,0,-2*rate#,0
		MoveEntity scr_entity,0,0,spd#
		dist3# = EntityDistance(scr_entity,dest_entity)
		MoveEntity scr_entity,0,0,-spd#
		
		If dist1# > dist2# And dist1# > dist3# Then TurnEntity scr_entity,0,rate#,0
		If dist2# > dist3# And dist2# > dist1# Then TurnEntity scr_entity,0,2*rate#,0
		
		Return True
	Else
		Return False
	EndIf

End Function