Artificial Intelligence 1

Getting Started (Plans)

A quick guide to planning artificial intelligence for your games.

Tutorial contributed by Vaza of PSP-Programming.com's forums.

I had some free time today so I decided to sit down and write the Artificial Intelligence tutorial that several forum members were asking for. This is the first of a series of tutorials about artificial intelligence that I am planning on writing. I will write more tutorials on different "styles" of AI.

First, a preface. In this tutorial "SBA" stands for Sonic Battle Arena, a game that I am involved with. I am responsible for coding the Artificial Intelligence and a lot of other stuff, so if I mention the game, now you know why.

Please note that this is my first tutorial (well... except for a "Hello World" guide I wrote months ago), so if I don't explain everything correctly, please bear with me. [I have tried my hardest to clarify any weaker points in the tutorial before I posted it on the site. -Yeldarb]

Before I start, I'd like to thank:

Please Note: The diagrams used in the tutorials are drafts. For your Artificial Intelligence to be used how you want it, you will need to plan and mold it to your program's own, specific "shape." Do not just copy somebody else's diagrams (like mine) and call it good! You might as well not even plan it out. The point of flow charts and diagrams is not only to define what your program will do, but also to help you better understand how to do it.

Now for the disclaimer, this isn't much of a tutorial, there isn't any use for it... alone. You're probably wondering... WTF? No AI?! What is this? Just plain old boring planning?!

Well, yes, it is all planning. But planning is actually extremely important. When I was first starting out, I made the mistake of not planning and not "molding" my Sonic Battle Arena AI. All my characters ended up being the same, and they all acted as if they were "Lost" and "Confused".

This disorganization ended up forcing me to redo the entire Artificial Intelligence engine, which was very cumbersonme, annoying and, worst of all, unneccessary. By teaching you to plan upfront, I am saving you some major headaches down the road. Trust me, you'll appreciate it later!

So, the first thing I need you to do is make a Flowchart. You can make it in any graphics program (Photoshop, Paint, Gimp, or Fireworks will all work fine). For each character, you should plan what will happen when the character does certain things. You want to plan out all of the logic. Pretend you are playing as the enemy you are coding. What would you do in any possible situation?

For example, let's say that I'm doing a game that has one normal enemy and one Boss. The Boss' AI will need to be more advanced than the normal enemy.

After thinking this through, you should know that more moves and actions will be available to the boss. You will need seperate plans for each character. Let's focus on the boss for now.

Here are some questions that should go through your head: What will happen with the boss moves left? What if the player is close? What if they're far away? What if they're attacking? What needs to happen when the boss attacks? How about when he jumps? What factors come into play when determining how the boss will act?

All of these questions and more should go through your head when you're planning out the boss' AI.

It is important that for each group of enemies you make seperate plans. You can then use that AI template for all of the specific instances of the group that you will need. Let's say, for example, that you want an infantry enemy and a sniper enemy. You should make one flow chart for each enemy, profiling how each will act.

Now, let's say that in your game you need five snipers and a hundred infantry men. The snipers can all use the code derived from their plan and the infantry the code from theirs. So now you have a hundred and five enemeies and two flow charts. It doesn't matter the number of instances of the members of the groups, whether you have one member or a hundred in a group you only need one flow chart for the entire group (unless you want each individual to be different... that's a whole 'nother can of worms).

You can see the flowchart that I made for our imaginary "boss" below. It's just a quick draft that I whipped up for this tutorial. If you were making a complete game, yours would need to be much more complicated and detailed. This helps get more of an idea of what your Enemy's settings should be.

Flow Chart

NOTE: This is for a "normal" enemy, no "personality" has been added (personality will be covered in a later tutorial).

If you have a look, it is pretty simple. There are four main actions that the enemy can do: move left, move right, attack, and jump.

All of these area's branch off to several outcomes, and these join back into the main 4 after the outcome has returned, the branches are all connected to "Take Action" and "Do Nothing," this adds more randomness to the enemy.

Now I want you to do you own flow chart. It doesnt have to be as big or as small as mine, just make up some characters and plan out how they will act and react. Try to think though what you would do if you were playing the part of the computer.

Now that you've tried it out on your own, we're going to walk through what the process should be. This is how we'd think through the actions for a computer-controlled enemy running left and right:

  1. Make 2 Boxes at the top. Label them "Walk Left" and "Walk Right."
  2. Branch off as many boxes for outcomes as your character will need. I did two for each.
  3. Label them. In "Walk Right" I had "Move to the edge of the screen."
    I coded psuedo-coded it like this:
    if x = 480 then End branch
    In my other "Walk Right" box, I had:
    if walkright == true then random outcome = Action/ Nothing
    Basically, this is going to make it randomly decide whether or not it should keep walking.

    I did the same thing for "Walk Left" (except made it move to the other edge of the screen).
  4. Join the all the boxes, to a big box at the bottom, that says action and nothing. Every single box must be connected, telling you that there will always be a randomised outcome of what happens.

Note: Your first flowchart shouldn't be for a full-fledged game. Do some simple ones for test projects first. If you try to flowchart the artificial intelligence for a full game right away, chances are you will become overwhelmed and just give up. It's a lot of work and takes some experience to get mastered. Practice makes perfect!

That concludes the first of the Artificial Intelligence tutorials. The next tutorial will cover "personalizing" your characters.

Be sure to add the feed to your RSS Aggregator (or Google Homepage, or Firefox Live Bookmark) to stay updated with the latest tutorials.

If you have any questions about this or about PSP programming in general, stop by the forums.
Home | Artificial Intelligence 1
Tutorial originally by Vaza. Editted and rewritten by Yeldarb. Copyright 2006.