In theory, there’s no difference between theory and practice. Of course, in practice when you build a game AI engine, things turn out very differently.
PC games have always been a great source of inspiration for enthusiasts, as they often allow you to look under the hood to see what’s going on. It’s great when the developers release the original SDK, but it’s even better when it’s packed full of AI technology!
This article looks into the latest SDK behind F.E.A.R., the hit first-person shooter acclaimed for its artificial intelligence. The codebase is in C++, like the rest of the AAA games industry, but the code is written in a style that should provide valuable insights for the developers among you using C# or Java.
Downloading and Installing the SDK
A fully working installation of F.E.A.R. is required to install the SDK. Here’s how you should proceed:
You need an original copy of the game. The F.E.A.R. trilogy is particularly good value for money for AI enthusiasts.
Make sure the game is patched to the latest version, 108. Go to this FTP directory and pick the right update for your language.
Now get the F.E.A.R. public tools version 108. This is the latest version of the SDK so it should match the latest patch also.
For those of you that already own the game and don’t have 1 GB of bandwidth to spare, I’ve made a backup of the engine and game source code and attached the package to this thread in the forums. (You’ll need to register to download it.)
Once the installation is finished and all the files have unpacked, you’ll have a .\Tools\ directory — typically within your game folder.
Screenshot 1: A sample of the files available freely in the SDK.
Finding Your Way Around the Codebase
You’ll find a Visual Studio solution file called Game.sln file within the ./Source/Game/ directory. The free Express edition of Visual C++ is suitable for opening that file.
The AI code lives in the ./Source/Game/ObjectDLL directory, along with the rest of the game logic that runs on the server. Predominantly in that directory, you’ll find:
Actions, Goals, Sensors and Nodes — Most of the files are modular tasks for gathering information (sensors) and making changes to the world (actions), using objects in space with animations (nodes), and evaluating objectives and for the AI (goals).
AI Framework & Algorithms — The other files starting with the prefix AI contain the supporting code for these specific objects listed above. The SDK also contains the implementation also contains the necessary algorithms for making decisions.
Game Logic — The rest of the files are the typical game DLL code, with the application logic, code to manage the client/server, etc.
Of course, the SDK doesn’t give you complete access to all the engine code, but there’s a surprisingly large amount of the AI code fully available. Either way, it’s more than enough to get an idea of how the architecture is structured.
Overview
There are a few things you should look out for in particular.
- The Planner
- F.E.A.R. is famous for its use of planning technology! You get access to a surprisingly large part of it in the SDK. It’s implemented at the core as A* in AIAStarMachine.[h,cpp], which serves as the basis for an object-oriented design including the goal planner (in AIAStarPlanner.[h,cpp]) as well as the pathfinder (in AIAStarNavMesh.[h,cpp]).
- Architecture
- While you can find similar STRIPS planners elsewhere, what’s interesting about this codebase is how it was integrated with the rest of the game engine. The files AIPlanner.[h,cpp] integrate the low-level A* algorithm by apply the actions over time. Also be sure to look at the way the goals are implemented modularly, based on AIGoalAbstract.[h,cpp], to control the outcome of the planning itself and compete for activation.
- Navigation Mesh & SmartObjects
- The planner is based on solid underlying systems, specifically the navigation mesh technology. For instance, you’ll find a navmesh generator for the raw polygons from the World Editor in all files called AINavMeshGen*.[h,cpp], as well as the code for handling different kinds of movement within the world in AINavMeshLink(Stairs|Jump|FlyThrough|DuckRun).[h,cpp]. The smart object code is stored in files derived from AINode.[h,cpp], integrating with the animation system.
The Main AI Class
Figure 3: The C++ class hierarchy in F.E.A.R.
Here’s a quick overview of the most important classes that compose the primary CAI character class:
The static blackboard is used as a software design pattern to help reduce coupling between the AI sub-systems. It provides a central place for modular pieces of code to store their data.
A working memory stores the AI’s observations about the environment. By having this separate world model, the AI can be duped and tricked by the player based on what it knows and senses.
The world state is a very simple list of attributes that are used to represent the current situation, as used by the planner to perform its reasoning.
The brain stores properties only, like roll distance or grenade throw times.
The remaining classes are a bit more self-explanatory. You’ll find some comments in the code, particularly in the lower-level classes, but be sure to browse around to get the big picture.
Download the AI source code from the SDK in this thread of the forum. Free registration required.















