AI: Finite State Machines

The next stage of making my prototype was to get the AI working. I had little experience with actual AI in games. One of the only things I ever really did related to games and AI was set markers for where an enemy would rotate and head towards another marker in a platformer. Another was when I got a turret to point its direction at the player when he was in view and shoot at the player. The only other AI related thing I have ever made was a neural network simulator in a simulated ecology. This was nothing compared to what I was planning to do in this game.

My game is going to be a stealth game where you have to get past guards that were either standing guard in the building or roaming around looking for intruders (YOU). This was a lot more complicated than anything I had done before. Since the game was randomly generated, I couldn’t  just set markers. This was not just because it was difficult to set markers in a randomly generated room. The way I made the random generation would actually not make adding markers that difficult anyway. The problem is, I would feel like the game was static. That the enemies would all have a simple way to defeat them and that wouldn’t make for an entertaining game. So I started reading around and came across finite state machines.

I had come across finite state machines before but I never truly understood them. I found them complicated and just went on to simpler methods. Only recently after I read this post on is when I realized that they weren’t that hard at all. I first made a very rough diagram, like the one in the blog post I linked, to see exactly I wanted my enemies to do.

Initial draft of the AI's finite state machine
Initial draft of the AI’s finite state machine

I then proceeded to look at GameMaker examples of finite state machines and came across a couple of good ones. I used the best one I saw as the backbone of my finite state machine. What this example did so well was that it would store all the wall object positions in a grid so that the enemy already knew how the room looked like. This helped because for the random room generation, the walls could be placed anywhere. After it knew this, it checked its state. If it hadn’t seen the player. It would set its destination to a random area in the room and make a path to that area avoiding all the wall obstacles. If it found the player, it would alert the other guards closest to it and they would all storm in on the player. This made stealth in the game very important but not necessary. You could still storm in and kill every guard the brute force method. Exactly what I wanted. It also chose when to strafe to avoid the players imminent gun shots or run away and hide. This was exactly what I needed in my original draft.

“I swear I saw someone”

After having this base, I decided to expand the scope. What were the senses of a realistic guard that I could put in this system. The first that came to mind was hearing. If the player shot off a gun and the guard was near enough to hear it, I wanted the guard to go to the location of where he thought he heard the sound, with a margin of error. If the room was dark, he would panic and shoot randomly at the place where he thought he heard the player and since it was dark, his accuracy was off put by a random integer within certain limits. When I programmed it into the game the results were great.

Guys, I think I heard something
“Guys, I think I heard something…” [The guard is the one at the top right, player is bottom left]
So I had set out to achieve and managed to achieve a working AI system. I obviously want to expand the scope of the AI in the future but this was a great start and did exactly what I wanted.


I also ditched my old lighting system, even though it worked and didn’t take much memory, I found more efficient engines that did the same but with much less memory and with greater results:


Thank you for reading this post, make sure to comment down below and share this post!


2 thoughts on “AI: Finite State Machines

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s