Matt Tuttle


No Comments

Finite State Machines for Game Developers

Finite State Machines for Game Developers

Several of the games I'm working on require the use of finite state machines for believable AI. For example, the game Pounce is using an FSM for the mice characters as they scurry around the level. Using them in games makes the AI characters seem more believable and help them react to many different situations. In fact they are used frequently in AAA game titles for bots and other autonomous characters. So just how do we implement them in our own games?

First thing is to define a list of states we'll be using. It doesn't have to be complete at first, just a few that pop into your head. So I'm going to be using mice as an example from my game Pounce. The states I came up with are Scout, Roam, and Flee. The scout state will have the mouse check if a cat is around, the roam state is simply a walking state, and the flee state is a faster walk motion.

Mouse States

Now that we have a few states to play with we need to understand how they are related to each other. Obviously if the mouse sees a cat in the Scout state then we are going to flee away from the cat. If we're in the flee state and we're far enough from the cat we'll switch to idle, and if the cat is in front of the mouse when we're roaming then we switch to flee. Here's a diagram to show the relationships.

Finite State Machine

So just how do we go about coding this state machine? Well first we need to have a state variable that holds the information about the current state we're in. We also need to check each frame if the conditions are met to switch to a different state. So instead of explain it all in this document I'm going to show you an example class that implements our mouse finite state machine. This example is not fully functional but will give you an idea of how to implement it yourself. It is written in Actionscript 3.0.

package
{

    public class Mouse
    {

        // Create a static constant for each state
        private static const kStateSearch:int = 0;
        private static const kStateRoam:int = 1;
        private static const kStateFlee:int = 2;

        // Holds the current state id
        private var _state:int;

        public function Mouse()
        {
            // Set the initial state
            switchState(kStateRoam);
        }

        // This function will initialize any variables when we switch states
        public function switchState(state:int)
        {
            switch(state)
            {
                case kStateSearch:
                    doneSearching = false;
                    break;
                case kStateRoam:
                    speed = kWalkSpeed;
                    break;
                case kStateFlee:
                    speed = kFleeSpeed;
                    break;
            }
            _state = state;
        }

        // Needs to be called every frame to update the state we're on
        public function updateState()
        {
            switch(_state)
            {
                case kStateSearch:
                    // Did the cat get close again?
                    if (distanceToCat < 100)
                    {
                        switchState(kStateFlee);
                    }
                    // Am I done looking around (not paranoid anymore)
                    if (doneSearching)
                    {
                        switchState(kStateRoam);
                    }
                    break;
                case kStateRoam:
                    // Did the cat jump in front of me?
                    if (catInFront)
                    {
                        switchState(kStateFlee);
                    }
                    break;
                case kStateFlee:
                    // Finally got away from the cat...
                    if (distanceToCat > 200)
                    {
                        switchState(kStateSearch);
                    }
                    break;
            }
        }

    }

}

Posted in AS3 Tutorials

Tags: Actionscript, Flash, Pounce, AI


No Comments

Why I use a Distributed Version Control System (DVCS)

Why I use a Distributed Version Control System (DVCS)

If you are a programmer you probably already have a good understanding about version control (also know as source code management) and have probably used something like Subversion, Perforce, ClearCase, Visual SourceSafe or something of that nature. Each of those run on a server and allow multiple programmers to check out and check in changes to ease code merging and branching. In my experience there is usually at least one person who has to make sure the software plays nice and when merges don't go well that person is responsible to manually make the changes so that everyone's code works as expected.

I used to use Subversion for all my personal projects but I couldn't stand setting up a server and how awful the code merging was when I wanted to merge a test branch. It always bothered me that merging wasn't based on the actual data in a file and more on the line numbers. If you make two changes on line 153 and try to merge the file together it will ask you which one to keep or else you have to manually fix it before changing. All these frustrations lead me to take a leap into the DVCS world by using a tool called Mercurial in my workflow.

The reason I chose Mercurial was because I had heard it was similar enough to Subversion that it would allow me to learn new things as I go without having to learn an entirely new tool (IE. git). I immediately noticed that Mercurial allowed me to setup a version control system right within a project I was currently working on. Absolutely no setup was needed I just had to use "hg init". I then added all my files and committed them to the repository and viola I had version control working. Just to show you how easy it is, here are the commands I used.

hg init
hg add
hg commit -m "Initial commit"

The next thing I wanted to do was work on my laptop since I was going to be travelling that day so I popped in a USB and cloned the project to my USB drive. I made a bunch of changes and commits that day and when I was finally back at my desktop I simply pushed the project back to my desktop and continued coding. The best part of the process was the fact that I didn't have to be connect to a server at any point to make my changes so I didn't have to wait to make commits.

hg clone usb-folder
hg commit
hg push
hg update

Finally I had my code to where I wanted it it and shared it with a friend. He decided to make some minor tweaks to my binary files and I continued working on the code. Neither of us was working from a centralized copy of the project we just kept working on our local copies. And after my friend had made changes I simply merged them into my copy and cloned him a new copy.

hg merge
hg commit

So you can see that by keeping the projects away from a centralized server it limits the number of headaches that most version control systems create. I would highly suggest using a DVCS for any personal projects at first and then suggesting it to co-workers to use in a team environment. Depending on which DVCS you use will determine the learning curve but once you are familiar with one it will make your life so easy you will never want to go back to any other version control system.

Posted in General

Tags: Mercurial, Subversion


No Comments

Prototype: Pounce

Prototype: Pounce

I'm working on yet another prototype game and I feel like this one is already nearing completion. Pounce is a casual game that lets you catch mice by enticing the cat character with a toy. There isn't any way to die and the levels are easy for most people to pick up.

My original goal was to make a game that anyone could play. This meant that the gameplay had to be simple but with enough twists to keep people interested. So I decided to use a cat for the protagonist and have the goal of catching mice in each level.

Instructions

You have to clear each level of mice before moving onto the next one. As you progress you'll notice that there is an added puzzle element to the game that makes it fun. Moving cardboard boxes, blocking mouse paths, and figuring out how to jump to certain areas keeps things challenging.

The best thing about this game is that it could be considered iPhone friendly. There aren't any buttons you need to press or tricky combos you need to memorize, just a stick with a toy attached to it. Dragging boxes is as simple as clicking and moving them around as well.

Hopefully I can release this game on a flash portal within the next few days. Keep your eyes peeled for more news and perhaps some more information on the other games I've posted on my blog.

Posted in Prototypes

Tags: Flash, games, prototyping, FlashPunk, Pounce


No Comments

3 Tips to Becoming a Master Craftsman

3 Tips to Becoming a Master Craftsman

In order to become a part of a European guild one would have to first advance from apprentice to journeyman and then attempt to become a master. This was done by producing a sum of money and a masterpiece before the guild members would allow them to join. If the applicant was rejected they would have to remain a journeyman for the rest of their life.

While we don't have many game development guilds that I know of I still think it is important to understand what it takes to be a master. In an earlier article I emphasized that to become good at what you do you need to practice. You'll notice that a person does not instantly get to apply for the master position, they must first become a journeyman through many years of practicing their craft and hard work. Only after achieving the rank of journeyman will someone be able to be a master craftsman.

Although this does not mean you have to be good at everything. Even among craftsmen there are areas of expertise which would be the same as in game development. We have artists, programmers, musicians, producers, etc... that all work on the same goal but have very different roles. So what does it take to become a master in your area? I think it boils down to the following things.

1. Know your tools

Probably the first thing you learn is how to use your tools. Once you become proficient at them you'll start to notice an increase in productivity and the end results are much improved. Most people will just scratch the surface of a tool but a true master knows how to bend it to their will. When tools are used to their fullest potential, artists can make breath taking scenes, programmers can debug even the hardest problems, and musicians can compose a symphony.

2. Have other masters critique your work

By letting others look at your finished product you will begin to understand what areas you need to improve on. Perhaps it is something simple that you just happened to overlook. Also, notice that I said other "masters" because they have already went through this process before and know what makes a great game. Unfortunately the games industry is still in its infancy stage and it may be hard to find masters in your area. At least try to find people you trust and know have good judgement when it comes to video games.

3. Make mistakes often

I mentioned this in my "Practice Makes Perfect" that one of the easiest ways to learn is to make mistakes. As long as you figure out how to do something right then making mistakes is perfectly normal. In fact, this is probably the best way to learn something new since you are going into uncharted waters and are more than likely to step in a few traps a long the way. Masters often know how to avoid the traps and the only way they know that is by having already fallen into them before.

Posted in Game Development

Tags: practice


No Comments

Spelunk: New Mechanics

Today I revisited the world of Spelunk and added a few new mechanics to the game. The first was a pretty generic wall jump which is found in a lot of games. I also added different climbable surfaces, beyond ladders, including chain walls, and a soft wall that requires a special climbing pick.

Spelunk outdoors The entire game hinges on fun mechanics and exploration more than story. I've been trying to come up with some unique ideas as well as providing the player with the traditional Metroidvania style gameplay. The biggest thing that is missing from the game at this point is consumable items. Think back to Super Metroid where you collected missile, energy, and bomb tanks to increase the capacity of the respective items. I need something along the lines of these types of things to add to Spelunk.

Overall the game is progressing nicely and I've added several more areas to explore as well as a few puzzles with the new mechanics. Hopefully within the next week I'll have something up for people to play and give feedback.

Posted in Game Development

Tags: Flash, Spelunk