A.I 3d help

Blitz3D Forums/Blitz3D Beginners Area/A.I 3d help

Here's some more detailed code which should give you a clearer idea of how to use the earlier code I posted.


; -- Set the 'current_enemy' variable to zero to indicate that no enemy is currently sighted by the bot.
current_enemy = 0

; -- Check if the player is within the bot's vision range (ie. If it's near enough to be seen).
If EntityDistance# ( bot, player ) < bot_vision_range#

	; -- Transform the player entity's coordinates from global space to local bot space for the viewing fustrum test.
	TFormPoint EntityX# ( player ), EntityY# ( player ), EntityZ# ( player ), 0, bot

	; -- Check if the player is within the bot's viewing fustrum.
	If Abs ( ATan2# ( TFormedX# (), TFormedZ# () ) ) < 40.0 Then If Abs ( ATan2# ( TFormedY# (), Sqr# ( TFormedX# () * TFormedX# () + TFormedZ# () * TFormedZ# () ) ) ) < 35.0

		; -- The player is within the bot's viewing fustrum so check if it is visible to the bot.
		If EntityVisible ( bot, player )

			; -- The player is visible to the bot so load the players entity handle into the 'current_enemy' variable to indicate both that an enemy has been sighted and, also, which enemy has been sighted.
			current_enemy = player

		EndIf

	EndIf

EndIf

; If 'current_enemy' variable holds a non-zero value, indicating that your bot has seen an enemy, then point your bot at the enemy and chase after it. Otherwise, rotate the bot to re-aquire the enemy.
If current_enemy
	PointEntity bot, current_enemy
	MoveEntity bot, 0.0, 0.0, bot_move_speed#
Else
  TurnEntity bot, 0.0, bot_guarding_mode_turn_speed#, 0.0, True
EndIf



Regarding that tip I gave you about giving your AI's access to the positional coordinates of the last enemy they sighted between look-arounds, you can also use this to simulate a highly efficient and convincing short term memory for the AI's. Just use a timeout to give the AI's access to the positional info of the enemy for a fraction of a second after they lose sight of it. You should loop through your AI's and delete the current enemy info of an enemy after it has been killed, though, or it will cause problems.

To set up and use a timeout use this code:
; Note: You should assign the value of MilliSecs() to a variable at the start of your game loop and use that variable here in place of the 'MilliSecs()' function for maximum speed.

; -- Declare the timeout variables and constants.
Const timeout_delay
Global timeout

; -- Initialize the timeout.
timeout = MilliSecs() + timeout_delay

; -- Check if the timeout variable has timed out.
If MilliSecs() > timeout
; Timeout variable has timed out. Perform whatever actions you need to do here.
EndIf