AI Food Gathering code

Miscellaneous Forums/Blitz Showcase/AI Food Gathering code

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I had a little program myself. This is what I came up with (Blitz3D)

;Ant AI System 0.1
;Chris Bate

	Graphics3D 600,600,0,2


;;===========================;;
;;					VARIABLES					;;
;;------------------------------------------------------;;

;Frame Limiting
;-----------------------
	Global FrameTime
	Global Period
	Global FPS = 30
;-----------------------

;Ant Control
;-----------------------
	OutsideBoundry = 19
	
	SeedRnd MilliSecs() : NestX = Rnd(-10,10) : Delay 1
	SeedRnd MilliSecs() : NestZ = Rnd(-10,10)

	AntAmount = 400
	
	AntSpeed# = 0.1
		AntSpeedRnd = 2

	AntAttentionSpan# = 0.8 ;Amount of time (in seconds) an ant will stay on the same course without changing directions
		AntAttentionRnd# = 2
		
	FoodNumbers = 10
	FoodSpread = 18
	FoodAmount = 500

	ColType_Ant=1
	ColType_Nest=2
	ColType_Food=3

	
;-----------------------


;;===========================;;
;;						SETUP						;;
;;------------------------------------------------------;;

;Scene Setup
;-----------------------
	Camera = CreateCamera()
		CameraClsColor Camera, 0,40,0
		CameraProjMode Camera,2
		CameraZoom Camera,0.05
		PositionEntity Camera,0,20,0
		RotateEntity Camera,90,0,0

	AmbientLight 255,255,255

	Nest = CreateCube()
		PositionEntity Nest, NestX,0,NestZ
		EntityType Nest,ColType_Nest

	AntMesh = CreateCube()
		HideEntity AntMesh
;-----------------------

;Ant Type
;-----------------------
	Type Ant
		Field Model
		Field Size
		Field Speed#
		Field Direction
		Field AttentionSpan#
		Field AttentionLeft ; How many more left cycles until he changes direction
		Field Target ; Current Food Target
		Field Mode$
	End Type 

	For i = 0 To AntAmount
		SeedRnd MilliSecs() : Delay 1

		Hive.Ant = New Ant
			Hive\Model = CopyEntity (AntMesh)
				EntityColor Hive\Model, Rnd(139,180),100,30
				PositionEntity Hive\Model, NestX, 0, NestZ
				ScaleEntity Hive\Model, 0.1,0.1,0.1
				EntityType Hive\Model,ColType_Ant
				EntityRadius Hive\Model,0.1
				TurnEntity Hive\Model,0,Rnd(-0,360),0
			Hive\Speed = AntSpeed * Rnd(1,AntSpeedRnd)
			Hive\AttentionSpan = 0
			Hive\Mode = "Looking For Food"
	Next
;-----------------------

;Food Type
;-----------------------
	Type FoodT
		Field Model#
		Field Amount#
	End Type
		
	For i = 0 To FoodNumbers
		SeedRnd MilliSecs() : Delay 1

		Food.FoodT = New FoodT
			Food\Model = CreateSphere()
			EntityType Food\Model,ColType_Food
			PositionEntity Food\Model, EntityX (Nest),0,EntityZ(Nest)
			If EntityDistance (Food\Model, Nest) < 5 Then
				PositionEntity Food\Model, Rnd(-FoodSpread,FoodSpread),0,Rnd(-FoodSpread,FoodSpread)
			End If
			EntityColor Food\Model, 0,200,0
			Food\Amount = FoodAmount + Rnd (-(FoodAmount/3),+(FoodAmount/3))
			ScaleEntity Food\Model, (Food\Amount/300),1,(Food\Amount/300)
	Next 
;-----------------------


	;SetBuffer BackBuffer()
	Collisions ColType_Ant,ColType_Food,1,2



;;===========================;;
;;					MAIN LOOP					;;
;;------------------------------------------------------;;

	While Not KeyHit(1) 

	;Cls

		If KeyHit(57) Then
			For Hive.Ant = Each Ant
				Hive\Mode = "Looking For Food"
			Next
		End If 


		For Hive.Ant = Each Ant

			Select Hive\Mode

				Case "Looking For Food"
					If Hive\AttentionSpan > 0
						Gosub MoveAnt
						Hive\AttentionSpan = Hive\AttentionSpan - 1
					Else
						SeedRnd MilliSecs() : Delay 1
						TurnEntity Hive\Model,0,Rnd(-90,90),0
						Hive\AttentionSpan = (AntAttentionSpan * Rnd(1,AntAttentionRnd)) * FPS
					End If
					If EntityX (Hive\Model) > OutsideBoundry Or EntityX (Hive\Model) < -OutsideBoundry Or EntityZ (Hive\Model) > OutsideBoundry Or EntityZ (Hive\Model) < -OutsideBoundry Then
						Hive\Mode = "Gone Outside Boundry"
					End If
					If EntityCollided ( Hive\Model,ColType_Food ) Then
						Hive\Target= CollisionEntity (Hive\Model, 1)
						Hive\Mode = "Found Food"
					End If 

				Case "Gone Outside Boundry"
					PointEntity Hive\Model, Nest
					Hive\Mode = "Looking For Food"

				Case "Found Food"
					If EntityDistance (Hive\Model,Nest) <1.2 Then
						PointEntity Hive\Model, Hive\Target
					End If
					If EntityDistance (Hive\Model,Hive\Target) <1.2 Then
						For Food.FoodT = Each FoodT
							If Hive\Target = Food\Model Then Food\Amount = Food\Amount -1
							ScaleEntity Food\Model, (Food\Amount/300),1,(Food\Amount/300)
						Next
						PointEntity Hive\Model, Nest
					End If
					Gosub MoveAnt

			End Select

		Next



		For Food.FoodT = Each FoodT
			If Food\Amount < 0 Then
				For Hive.Ant = Each Ant
					If Hive\Target = Food\Model Then Hive\Mode = "Looking For Food"
				Next
				FreeEntity Food\Model
				Delete Food
			End If
		Next


		LimitFrameRate()
		UpdateWorld
		RenderWorld

;		i=10
;		For Food.FoodT = Each FoodT
;			Text 10,i, "Food Amount=" + Food\Amount
;			i = i+10
;		Next

		Flip
	Wend
	End

;;===========================;;
;;					FUNCTIONS					;;
;;------------------------------------------------------;;

Function LimitFrameRate()
If FrameTime = 0 Then
Period = 1000 / FPS
FrameTime = MilliSecs()
EndIf

; Make sure the framerate isn't above the specified setting
While (FrameTime + Period) > MilliSecs()
Delay 1
Wend
FrameTime = MilliSecs()
End Function

.MoveAnt
	MoveEntity Hive\Model,Rnd(-0.1,0.1),0,Hive\Speed
Return



The only borrowed code is the FPS Limiting Function. I am still working on it. Going to add scent trailers when I get the chance (like the Robs Caller Mode but more based on ants scent trails)