Programming Game AI by Example provides a comprehensive and practical introduction to the "bread and butter" AI techniques used by the game development industry, leading the reader through the process of designing, programming and implementing intelligent agents for action games using the C++ programming language.

Each technique or algorithm is described patiently, often accompanied by a step-by-step illustrated walkthrough, to ensure the reader thoroughly understands the principle before moving on to show how they can be combined to create intelligent agents capable of providing the modern video game player with an entertaining and rewarding experience. To this end the book includes detailed descriptions of the creation of game agents capable of playing team sports and deathmatch type games. Source code and pre-compiled executables for all the demos and projects found in the book can be downloaded from the Wordware website.

Here's what some industry professionals have to say about the book:


“Programming Game AI by Example stands out from the pack by providing industrial-strength solutions to difficult problems, like steering and goal-oriented behavior.  Mat guides the reader toward building a foundation robust enough for real games.  This book is a must have for anyone new to the field, and has tips for the seasoned professional as well.  I wish I read it eight years ago!”


Jeff Orkin, AI Architect, Monolith Productions, No One Lives Forever 2 and F.E.A.R.



“...a nice combination of a lot of really useful information, put together in a way that doesn’t make my brain leak.”


Gareth Lewis, Project Leader, Lionhead Studios, Black & White 2



“Programming Game AI by Example is an excellent book for both the game programming neophyte, the intermediate programmer and even the expert - it doesn't hurt to go over familiar ground does it? The book concisely covers all of the important areas, including basic mathematics and physics through to graph theory and scripting with LUA, to arm any programmer with the tools needed to create some very sophisticated agent behaviours. Unusually for books of the type Programming Game AI by Example is solid in it's software engineering too, with the example code demonstrating game uses of familiar design patterns. I'd have no qualms about recommending Programming Game AI by Example to any programmer, it's an excellent read and an excellent springboard for ideas.”


Chris Keegan, Technical Director, Climax Studios (Solent).



"Using easy to follow and well described examples, this book shows you how to use most of the techniques professional AI programmers use.  A great introduction for the beginner and an excellent reference for the more experienced!"


Eric Martel, AI Programmer, Ubisoft, Far Cry (XBox)



“Each chapter of Mat’s book gently introduces the reader to a fundamental game AI technology, before expanding the new idea into a fully formed solution replete with extensive code and clearly worded examples. The tone of the book is uncomplicated and accessible to the reader, allowing a novice programmer the opportunity to get to grips with the basics of game AI programming by implementing their own systems direct from theory or expanding upon code examples offered to gain understanding in a sandbox environment. Once individual technologies are fully understood the book goes on to combine these ideas into several complete game environments allowing the reader to understand the relationships between the interacting systems of an overarching game architecture.”


Mike Ducker, AI Programmer, Lionhead Studios, Fable






Programming Game AI by Example is on the shelves at last! Here are links to the Amazon listings:






Table Of Contents




Foreword by Steve Woodcock


Click to read






Chapter 1: Math & Physics


Some knowledge of mathematics and physics is necessary to understand game AI. This chapter assumes the reader has very little experience with either and provides explanations of all the math and physics used in the techniques described in this book.



Chapter 2: State Driven Agents


State based agent architectures have been the bread and butter of game AI for the last 25 years. This chapter describes how you can implement them in your own games.


Discussion, code and demos of: switch based, transition table based, embedded transitions, state design pattern, state class, templated state class, global states, blip states, templated state machine class, message handling.


Part of this chapter is now available to read online.



Chapter 3: Steering Behaviors  


Most computer game characters move around and this chapter shows you how to program them to do it.


Code and demos for: individual and group movement including the behaviors seek, flee, arrive, pursuit, wander, evade, obstacle avoidance, wall avoidance, interpose, hide, path following, offset pursuit, separation, alignment, cohesion, flocking, combining steering behaviors, weighted truncated sum, weighted prioritized truncated sum, prioritized dithering, non penetration constraints, space partitioning, smoothing.



Chapter 4: Simple Soccer


This chapter walks through the development of a five a side soccer game from start to finish explicitly demonstrating how the techniques the reader has learned in previous chapters (plus a couple of new ones) can be made to work together to implement a simple but entertaining AI for a team based sports game.


Discussion, code and demo of:  sports game framework, layered AI architecture, soccer ball physics, field player states and movement, goal keeper states and movement, team states, spot sampling, making estimates and assumptions work for you



Chapter 5: Graph Theory


Whenever a game agent needs to find a path from A to B or the best way to traverse a technology tree it will more often than not be using search algorithms to traverse some kind of graph-like data structure. This chapter provides an overview of graphs and very detailed (no more headaches trying to understand this stuff!) walkthroughs of the most commonly used search algorithms and their  implementation in C++.


Discussions of: trees, graphs, digraphs, graph density, state graphs, dependency graphs, navigation graphs, iterative deepening search, bi-directional search, heuristic policies


Discussion, code and demos of:  graph node class, graph edge class, graph class, depth first search, breadth first search, edge relaxation, shortest path trees, Dijkstra's search, A*.



Chapter 6: Scripting


Over the last few years scripting  has grown enormously in popularity with game developers. This chapter will tell you why, what scripting is, what you can use it for and how to implement it in your own games.


Discussion, code and demos of:  what scripting is, what scripts are, how they are used by AI developers, introduction to Lua, interfacing with C++, exposing C++ functions to Lua, using Lua scripts with C++, introduction to Luabind, exposing C++ classes to Lua, creating a scripted state machine.



Chapter 7: Raven - An Overview


This chapter provides a detailed overview of  the Raven game architecture and the design of the AI of the agents that fight within its maps. Raven is a game framework that combines elements of FPS, RPG and RTS type AI, which I use to demonstrate several newly introduced techniques. In particular Raven is used to demonstrate explicitly how just about all the methods described in the book can combined to create sophisticated game playing agents. (Some of the techniques mentioned  are too complex a topic to cover properly in a single chapter and are discussed in detail in later chapters).


Discussion, code and demos of:  game architecture, map objects, weapon objects, projectile objects, triggers, trigger regions, respawning triggers, limited lifetime triggers, giver triggers, sound notification triggers, trigger system class, AI design considerations, decision making, path planning, movement, perception, sensory and memory modeling, target selection, weapon selection, weapon handling, AI update frequencies.



Chapter 8: Practical Path Planning


There's probably more written about path planning than any other technique currently used in the game industry. This chapter provides an overview of the problems faced by today's game developers and shows you -- using the Raven framework -- how to solve them.


Discussions of: tile based navgraphs, points of visibility navgraphs, expanded geometry navgraphs, navmeshes, hierarchical pathfinding


Discussion, code and demos of:  coarse versus fine grained navgraphs, using spatial partitioning to speed up proximity queries, creating a path planner class, planning a path to a specific location, planning a path to an item type, creating custom termination condition policies for graph searches, node paths versus edge paths, annotating navgraphs, smoothing paths, reducing CPU overhead, pre-calculating path lookup tables, pre-calculating cost lookup tables, time-sliced path planning, preventing the twiddling of thumbs, getting out of sticky situations




Chapter 9: Hierarchical Goal Based Agents


This chapter introduces agents that are motivated by hierarchical goals. This type of architecture is far more flexible than the one described in Chapter 2 allowing AI programmers to easily imbue game characters with the brains necessary to do all sorts of funky stuff.


Discussion, code and demos of: atomic goals, composite goals, goal arbitration, creating goal evaluation functions,  implementation in Raven, using goal evaluations to create personalities, goals and agent memory, automatic resuming of interrupted activities, negotiating special path obstacles such as elevators, doors or moving platforms, command queuing, scripting behavior.




Chapter 10: Fuzzy Logic


Fuzzy logic, enables a computer to reason about linguistic terms and rules in a way similar to humans thereby making it easy to extract domain specific knowledge from an expert and to present it in a form a game playing agent can utilize. This chapter provides a solid grounding in the theory and practice of fuzzy logic and demonstrates how it is used by the Raven agents to help select the correct weapon for the correct situation.


Discussion, code and demos of: crisp set theory, fuzzy set theory, membership functions, fuzzy operators, hedges, fuzzy linguistic variables (FLVs), fuzzy rules, designing FLVs for weapon selection, duzzification, fuzzy inference, defuzzification methods (mean of maxima, average of maxima, centroid), FuzzyModule class, FuzzySet class hierarchy, FLV class, how to build a fuzzy rule base using the C++ classes, the Combs method for reducing combinatorial explosion.



Final Words




Appendix A: C++ Templates


Appendix B: UML Notation (available online)


Appendix C: Setting up the Development Environment




Source Code and Binaries


You can download the source code and binaries that accompany the book from here.


(1/8/12: Petr Bilek is writing Java versions of the examples in Programming Game AI by Example. You can find them here.)