NPC AI Design

The next two logical steps are to give the NPCs their wanderer AI, and to add monsters that will hunt them. I’ll get a basic wanderer AI in, and then I’ll tackle adding the first monsters.


Let’s go over the wanderer AI again. You place a Town Center block, and after some time passes an NPC will spawn some distance from it. The NPC will wander around randomly from that spot, with a tendency to go towards the Town Center. Once they reach a minimum distance from the Town Center, near enough to it to be considered “within the town”, they’ll wander completely randomly. The wanderer AI will keep them within that minimum radius: when they go outside the “town limits” they’ll once again tend to move towards the Town Center.


The NPC will spawn at a pretty far distance away from the Town Center, though I’ll have to play with the exact number during playtesting to find a good one. I think it will be fine to let the distance be a constant, since it will be a random direction from the Town Center and that should make for a decent spawn distribution.

In what cases should the spawning fail, and how should that be handled? I don’t think they should be able to spawn too close to the player – having anything suddenly popping into existence near me would startle me. The spawning should also fail if the NPC would wind up over water. I don’t think anyone’s going to be wandering in the sea. Another obvious case is if they spawn within a solid block.

If it’s determined that the spawning failed, then the game will wait until the next frame before it tries again. I don’t want to halt the system if you put the Town Center block in the middle of the ocean. I could try to come up with a way for the Town Center to build a list of valid spots and then spawn in one of those, but I wont bother for now. I’m more concerned with seeing results than making the game run perfectly.

When the Town Block is placed, after a period of time it will try to spawn an NPC. I would think that 1min or something would be reasonable but this is another value I’ll have to play with during playtesting. After that length of time has passed, the Town Block will try to spawn a new NPC. Once the NPC has successfully spawned the block will check the number of NPCs against the NPC count maximum. If there’s room for more, it will start the timer up again to spawn a new one. If there isn’t room it will enter an idling state, waiting until there’s room for a new NPC – whether an NPC dies or the town levels up.


The ‘wandering’ process will have two states: moving and idling. The NPC will idle for a time period and then will decide on a direction to move and how long/far to move. While the NPC is moving, they will need to deal with stepping up blocks, and I’ll have to allow them to jump. At the end of the moving period the NPC will choose the length of time to idle, and re-enter the idle state.

When the NPC is deciding on a direction. they’ll start by picking one completely at random. The speed and length of time to move will be consistent, only the direction will be chosen. If the NPC is outside of the town limit, a vector towards the Town Center block will be added to the chosen direction, and the resulting vector will be normalized and used.

I’ll need the NPC to figure out when they’ll need to jump. I was thinking that if they were trying to move forward but hadn’t moved from the spot they were in last frame, that they aught to try to jump. This will solve the simplest cases, but has problems if they’re walking into an L corner that was 2 blocks high, like this:

I’ll look at more complicated pathfinding at another time.

About the Town Center

The distance from the Town Center block to the “town limits” is going to be another value I’ll have to play around with. Within this distance, NPC wanderer AI will stop bothering to try to go towards the Town Center when they choose their wandering direction.

The game will need to keep track of where the Town Center block is at if it’s moved. The way I’ll deal with it for now is that whenever you place a Town Center block, it will change the location of it. I will be limiting the number of Town Center blocks to one in the entire world, and I’ll be dealing with the case when the Town Center block has been removed.


Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: