# New to creating AI - where to start? [closed]

I am new to game programming and am trying to make a basic 2d top-down space game with 2 space ships that fight each other. I am doing well with the user controlled space ship, but have no idea how to even start programming an AI. Are certain methods/patterns better for this situation? Where would I even begin?

• Here's my slides on AI from the course I made: sol.gfxile.net/gpc/05ai.pdf Feb 11, 2011 at 14:58
• if statements. lots of them. Jun 13, 2012 at 22:41

The Strategy pattern is great for determining what to do but not when to do it. You're also going to need to use Finite State Machines to know which state your AI is in and what appropriate decisions are available.

A Practical Guide to Building a Complete Game AI: Volume I

If this is your first time developing AI, you don't have to worry about things like the state pattern, behavioural maths or even get a book on the subject.

You can literally get away with something like this:

if( targetInRange == true )
{
FireAtTarget();
}
else if( bulletComingTowardsMe == true )
{
MoveAwayFromBullet();
}
else
{
WanderAroundAimlessly();
}


The main aspect of getting AI correct is by thinking about what behaviours you want the character to have and how this can be achieved realistically. So for a basic spaceship enemy, I'd imagine the possible actions it could carry out would be to:

• Move around
• Fire
• Run away

And when could these actions happen?

• Move around - Nothing else to do
• Fire - When I spot an enemy
• Run away - When I'm low on health/When I'm taking fire/etc

After you have done this due to the low number of "states" that your character can be in, you can use a simple if statement like the one above. I would then recommend you look at Steering Behaviours as these are very simple behaviours to implement and can result in really good looking movement.

More can be found here: http://www.red3d.com/cwr/steer/
And I would definitely recommend Mat Buckland's book Programming Game AI By Example, especially when you're starting out.

When you are really comfortable with these, I would suggest looking at the State and Strategy Patterns.

The point is to start out small and simple. Don't worry about all these terms that everyone keeps flinging around about FSMs and design patterns and whatever. Design and build first of all, then worry about how you can improve it by using existing methods or standards.

Hope that helps!

• @Fuu Cheers for the edit mate :) Feb 11, 2011 at 17:21

You might want to look at OpenSteer, and the Steering Behaviors documentation that goes with it. The source code is not beginner-level, but the concepts behind it should give you some good ideas.

• Do people use opensteer in games or is it more for simulations?
– jle
Aug 5, 2010 at 18:12
• I'm at least trying to use it in a game under development. It's working well for me so far. I believe the author developed much of it while working for the Playstation group at Sony - Its likely it made it into some games. MobyGames lists him on the credits of JohnMadden Football and one of the Leisure Suit Larry games. Aug 6, 2010 at 18:54
• FreeOrion is using OpenSteer in its tactical combat engine. Aug 10, 2010 at 15:12
• +1 For mentioning opensteer. If you want to see some great examples of compounding very simple behaviors together to form something that looks very very complex and intricate. Checkout steering behaviors! Google, "Boids" and look at some cool uses. It's not quiet what you want, but it will show you that out of simplicity comes complexity Feb 12, 2011 at 22:09

You might want to look at the Strategy design pattern. Essentially write up same basic strategies of how the ships will behave:

• Move closer to target
• Attack target
• Run away from target
• Move towards power ups
• Use power ups

Then you will use logic (state machines) to choose between these strategies. For example: If the ships shields have fallen below 50%, then run away from the target and move toward power ups/healing items and so forth.

• so each time through the game loop, it decides which of those 'strategies' takes priority and then executes that?
– jle
Aug 4, 2010 at 21:08
• @jle Pretty much. So each of your strategies will broken down into their own classes, keeping your code organized. Aug 4, 2010 at 21:10

May I suggest that you buy the book Artificial Intelligence for Games by Ian Millington - it's excellent! :) http://www.ai4g.com/

The source code is at Github - MIT license.

Of course, if you're not using C/C++, then this might be less relevant.

But really an awesome introduction to the world of Artificial stupidity/intelligence.

Things you're going to need from it are steering and state machines. For starters.

Some practical advice if you go for state machines: beware the megastate.

It's tempting to have a "flee" state which handles setting a position to flee to, setting a high target speed, clearing all targets, etc. It's much better to instead have many state machines, each handling one or few ship controls - eg a state machine that handles selecting a target position, one that handles guns, etc.

This may seem stupid, as you'd have to add flee states to each individual machine, but since you're constrained to only one or two outputs, your states end up very small so this should not be a problem.

Also, since each control is now under individual control, you can end up selecting a flee target position while still having the attack state control your guns. You can strafe an enemy while still conserving engine power because of a coolant leak. You can steer to avoid a collision while still dumping countermeasures. All these would be impossible with a single all-encompassing state machine.

Behavioral Mathematics for Game AI

Adding this book to the list of starting points for learning AI programming. With its focus on observation and behavior implementation rather than algorithms of limited application, I believe it is well suited to the task.

How to create AI is a tricky question, because it consists of many parts:

• You choose a general architecture/paradigm for your AI. Is it based on scripts? Will it use planning? Is it a simple FSM or do you prefer BTs?
• Then, you apply these paradigms with a specific implementation/set of tools.
• Now, you're left with game-design choices, as you have to think of behaviors that your architecture will use.

Other posters suggested great books and I'm fond of AI4Games suggested by jacmoe, even though it's quite general in parts (like a lot of books, which is of course understandable).

Of course, each type of game has its specific needs, and arcade/shooter games don't need complex architectures to support interesting gameplay. Simple steering libs will do the job as the basis of that behavior, and then you add details for various types of enemies (some, shoot periodically, some shoot by predicting the player's movement, some turn in order to face the enemy, others may coordinate their attacks etc).

• I think the best way to learn about game (AI) coding is too look at good, running code. So, explore ohloh.net and github and look for existing FOSS projects!
• Of course, you can choose the hard part and play an existing (but simple) game. Try to reverse engineer the behavior and think "How would I design this?". Of course, hints always help, and that's probably why you asked here at gamedev.SE!
• Something in-between is too look at existing shooter games within the GameMaker forums! You can find a lot of simple scripts that will do the trick! ;)

Think about what you do when you play the game. Ask yourself some questions.

• What information do I need to be able to make a decision?
• When do I react to changes?
• What are my "long term" goals in this particular play session?

Then, what I do is have the AI look at the play field and make a list of possible actions or movements that the AI could make based on those questions. I rank and sort these moves with a "smartness factor". Then, it's as simple as picking and executing a movement/action based on some combination of randomness and whatever difficulty level the player chose at the beginning of the game.

Then, after 500 milliseconds, I do it again.

It doesn't have any learning abilities or anything but with enough tweaking it can be pretty convincing, which it all that really matters