Writing my first AI

I’m hoping to move my code from XNA to Mono shortly, Vogles is helping me with a template. So, while waiting I’ve turned my mind to the AI problem I have looming.

I’m writing something just for giggles, a kind of remake of the old BBC game Starship Command. If you haven’t played it immediately download a Beeb emulator and try it, as a 9 year old kid it captivated me!

If you can’t remember it, and/or can’t be bothered to download it, It’s a downward looking 2D space game where your ship stays static in the middle of the playing field and the world revolves around you. There were alien ships which shared the space and obviously tried to destroy you.

All directional movements are performed as slowly changing vectors, (a bit like driving a car) so the sprites will need to speed up, slow down, chose when to turn, shoot etc.

I’ve been coding business systems for years which are all very logical and do many exciting things, but I’ve never faced a problem like coding an AI, especially in a vector driven revolving environment!

Does anyone have any tips before I start writing code and end up in a quagmire of my own confused mind?

The first site that comes to my mind for AI related stuff is http://aigamedev.com/
It’s a good resource of articles where you can learn about widely adopted techniques before you dive into the code.

What you’re describing is pretty much a 2D physics world, so piloting the ships is just a matter of adding the correct forces at the correct time. You’re going to need a brain for your pilots and that can be implemented in many ways. You should definitely look up stuff like finite state machines, behavior trees, goal oriented action planning…

There’s a lot to read about before you venture into designing your own system.

Thanks Tanis. I think that’s decided my evening’s reading. :o)

And yes, you’re right, it is a 2D physics world, with quite a lot going on. What amazes me is how the original developer managed to get everything down to 18k. Yes, 18k.

Ok, I’ve read a few topics and fast forwarded a few you tube videos, I get the idea. I’ve written business systems with stupidly complex logic, but only in 1 dimension, and only ever with one goal.

What I can’t seem to find is a decent code example that explains how the tree structure is managed. Do people work with a single ‘All knowing’ class that iterates, or do people prefer a multi class structure? Which is easier to manage?

Jees I would love to talk to the developer of the game I’m trying to replicate!

Note that the behavior tree stuff on aigamedev is potentially orders of magnitude more powerful than what you really need for your project.

For your first project I would really recommend taking the following approach:

  1. Make a base class for game objects controlled by ai.
  2. For each game object that needs to behave uniquely, derive a child class.
  3. Implement the behavior directly in the child class for that game object type by overloading Update/etc methods.

As far as controlling the motion of a 2d object, you should read up on ‘steering behaviors’. If this is something that all game objects need access to then it goes in the base class, and child classes control parameters which affect steering.

‘Game AI By Example’ covers statemachines and steering behaviors (i think in C++ but that code can be ported, and anyway, it is an example), and makes a great first book for ai game programming.

The more important part of my last post was “read up on steering behaviors”. Where you end up putting/organizing the code itself is not super important, and really only becomes a factor when you have a larger project (or a small project you need to be robust enough to become a big project).