Controlling NPCs with Types

Blitz3D Forums/Blitz3D Tutorials/Controlling NPCs with Types

Drak(Posted 2010) [#1]
I've seen a few posts lately about how to control enemies or NPCs and simple AI. Here's a simple way to control your NPCs using types.

All you need to do is have a status or state field in your NPC type. Each frame, act upon the chosen state. It's pretty simple.

First: What do we want to do?
We want the enemy NPC to sit idle, look for the player..
If they see the player, follow him, and if he gets close enough, attack!
Then, we want to return the enemy NPC to it's starting location.
When the enemy returns to it's post, it will begin searthing for the player again....

To see the difference in states, we'll change the color of the enemy NPCs so we can visually see what state they are in.

If the player gets within say 10 units of the enemy, the enemy will become aggressive. We want the enemy to follow the player either until the player gets too far away or the enemy reaches the player, in which case it will attack the player.

That's all we need to know to get started!

Here is the commented code:



Blitzplotter(Posted 2011) [#2]
Exceptional piece of coding Drak, thanks for sharing!

Hope you do not mind, I posted a slightly modified version of your code in the following thread as an include file to implement homing missiles/spheres in some JV-ODE physics car code :)

http://www.blitzbasic.com/Community/posts.php?topic=89252#1066963

Last edited 2011


Drak(Posted 2011) [#3]
Glad someone found it useful!


Blitzplotter(Posted 2011) [#4]
@Drak, hell yeah, I feel myself being sucked back into B3D with physics(JV-ODE) at precisely the wrong time - I'm embarking on my penultimate course of study towards my degree with the OU, nevertheless I love excuses to play in B3D with physics & now a fantastic piece of code that lends itself to homing missiles - I'm curious to explore whether your code homes in on the y axis as well as the x & z axis. Not too hard to prove, but thats where I'm headin'....


Drak(Posted 2011) [#5]
It will home in on the Y axis in this program, but since the player is viewed from above here, and not allowed to move on the Y axis it's not relevant to this program. The "enemies" simply use the PointEntity() command so they actually can move and pursue in all axis.


Drak(Posted 2012) [#6]
2/3/12
Added a 6th state to the NPC enemies. Now they will wander randomly if they don't see the player.