Client Side Bots

In this section, we'll describe how to implement a simple client-side bot for Quake 2. We're using this engine since it's one of the best around for developing game AI, and there's a nice library available to interface with the server. Doing this manually is a tediously long process, especially if you're not familiar with the workings of the engine.

What You Need

The Game

Quake 2

Obviously, you need the game itself! I'd be surprised if you don't have it, since it's a universal reference and one of my all time favourites. You can find it cheap right here. See the third page of this tutorial for more information.

Make sure that's up and running before you get into any artificial intelligence development.

Q2 Bot Core

Ben Swartzlander has written a very useful library that handles the interaction with the server. Everything is wrapped into a neat DLL, and you only need to compile one C++ file with a header to get a working client side bot!

The first think you need to do is visit the Q2 Bot Core web page at http://www.telefragged.com/Q2BotCore/. In the downloads section, find the file named q2botcore.zip and download that. Extract that to a new directory, and we're ready to go.

Visual C++

Visual C++

Those of you that have never programmed before will struggle to start writing AI code right from the start. The rest of you will be aware that Visual C++ is one of the best programming environments you can find. Notably, the edit and continue feature will allow you to tweak your AI in real-time, without having to recompile and restart the game. This has saved me hours of time! Together with this, most source releases nowadays are compatible with VC++, including prepared workspaces. This too will allow you to dive straight in.

If you don't have Visual C++, I've noticed Amazon is doing an offer at the moment, so you can get it for just over $80. It's a worthy investment, so if need be cut down on your weekly beer allowance!

Things To Do First

First, you need to test your ability to get even the simplest bot working perfectly. When you're starting out, it's good practice to do this just to check that everything you're relying on is working as you expect.

Starting A Server

Now start a dedicated server. This is a game that runs in a small window, and does not allow you to visualise the game. We do this so that a graphical version of Quake 2 is not constantly running, so you're AI will have more processing power. You can start a dedicated server by typing this at the DOS prompt:

quake2.exe +set dedicated 1 +set deathmatch 1 +set maxclients 32 +map q2dm1

This will launch Quake 2 in text mode, with a maximum of 32 players on the first death-match map. This will be left running throughout the programming session.

Visualising the Game

You may want to see what's going on in the game. To do this you can connect to the server as a graphical client:

quake2.exe +connect 127.0.0.1 +set spectator 1

This will connect to the local server, and put you in the game as a spectator. If you actually want to interact with the bots, remove the last part of the command.

Adding a Simple Bot

The Q2 Bot Core is very nice for testing you configuration as it comes with a prepared example: a bot connects to the server, says hello while moving forward for 200 frames, and disconnects. This is ideal to test everything is in functioning order. Find the Q2BC directory, and click on the workspace. This will start Visual C++.

Compile the example so it's ready to run. Then run it by pressing Ctrl-F5. Remember to hold on to your girl-friend's knickers 'cos this might get rough.

If everything works ok, the client program will exit correctly without crashing. The server console window will also display the text message sent by the bot. If it didn't work, take a look at the README with Q2BC, that should help you quite a bit. Once you are successful, you can move on to something more elaborate.

Cooler Things

Basics

In the bot_test.cpp file you have just compiled, you'll find the code that is of interest. The main loop basically updates the angles and velocity of the bot. This will control the movement of the entity.

Velocity controls the translation. The first variable in the vector corresponds to forward and backwards movement (positive and negative values respectively). The second variable in the vector controls left or right movement (velocity[1]), and the third handles the jumps (velocity[2]). Maximum values are generally 400, 200, and 200 respectively. I think the server clips values too large anyway, but you can still try.

For the angles, the first index in the angle vector controls looking up and down (angles[0]). Angular measurements are in degrees. The second value (angles[1]) controls left and right turns. This is the most important aspect of the bot's navigation and firing.

Every frame, you then send orders to the server, using syntax similar to this:

qbMovementOrder(angles,velocity,1);

The 1 close to the end of the line indicates that the bot must fire.

Taking it Further

Deciding what to do, when to turn, and by how much is what it's all about. There are many ways of doing this, but among others:

It's up to you to look into more specific fields of AI, and apply them to solving your bot's problems

Remember you can visit the Message Store to discuss this tutorial. Comments are always welcome!. There are already replies in the thread, why not join in?