Can You Program a Sophisticated Game AI in C?


When it comes to AI in general, you will find a lot about scripting in Python and learning algorithms. For Game AI you will find a lot about scripting in LUA or algorithms in C++. But when you want to write your Game (Engine) in pure C you will find not that much. Is it possible to write a sophisticated Game AI in C?

Programming a Sophisticated Game AI in C is possible. There are various types of AI which are used in Games and you can program all of them in C. Because C is relatively close to hardware you will gain an advantage in execution speed for complex AI algorithms.

AI in Games
Artificial Intelligence in Games (Image from Rafael Javier at Pixabay)

Which various types of AI do exist and which of them are suitable to be implemented in C? We will cover this in the rest of the article.

Can You Program a Sophisticated Game AI in C?

It is totally possible to program a Game AI in C. It is even possible to program a sophisticated Game AI in C. This means that the decisions are made somewhat intelligent and are not just “dumb” fixed reactions to pre-programmed situations.

What distinguishes Game AI from real world AI? In the real world the focus lies heavily on machine learning algorithms in order to make sense of noisy and unrelated data. These algorithms use pre-training and neuronal networks to accomplish this.

A Game AI on the other hand knows which data is present and how it should be interpreted. The focus here lies on the decision making within the logic and boundaries of the actual game. The decisions have to be made in real-time and must not occupy the whole CPU because the rest of the game also has to run.

The Sense/Act/Think Cycle

A basic concept of AI is called the Sense/Think/Act cycle. It describes the three stages of AI based decision making:

Sense/Think/Act Cycle
The Sense/Think/Act Cycle

Each of these stages has a distinct purpose:

  • Sense: Detecting relevant things in the environment
  • Think: Make a decision about how to response
  • Act: Perform the response

For your C algorithms, the Think stage will be the one where you will have to do the most work.

Basic AI Concepts and Examples in C

The simplest form of AI is called a Decision Tree. It is nothing more (but also nothing less) than hard coded decisions for each situation in the game. In a Decision Tree, the AI checks if the detected state is equal to a predefined condition until it finds something or the end of the tree is reached.

int main() 
{
    ...
    if(detected == condition1) {
        /* do something */
    } else if(detected == condition 2) {
        /* do something else */
    } else {
        /* end of the tree - fallback condition */
    }
    ...
}

A more sophisticated approach is called Finite State Machine (FSM). Here we have different states and the possibility to move between these, if a certain condition is met. The number of states is, as the name suggests, finite. You will have a begin state and sometimes but not always there is a final state. When this final state is reached, the program (algorithm, function, …) is over and the states do not change anymore.

Game Finite State Machine Example
An example of a Finite State Machine (FSM) for a Game AI

The begin state of this FSM is Idle. If we’re getting attacked, we have to defend, if an enemy is within reach we can attack ourselves. In each case, a win in the battle brings us back to Idle, a loss makes us flee. You can see that Flee is a final state, because from there you cannot get back into any other state.

If you had to program this FSM in C it could look something like this:


...
enum game_state { idle, defend, attack, flee };

static int currentState = idle;

void checkAI()
{
    switch(currentState) {
    case idle:
        if(enemyIsWithinReach()) {
            currentState = attack;
        } else if (underAttack()) {
            currentState = defend;
        }
        break;
    case defend:
        if(attackIsOver()) {
            currentState = idle;
        } else if (battleIsLost) {
            currentState = flee;
        }
        break;
    case attack:
        if(battleIsWon()) {
            currentState = idle;
        } else if (battleIsLost) {
            currentState = flee;
        }
        break;
    case flee:
        /* Nothing more to do */
        break;
    }
}

int main()
{
    int gameIsRunning = 1;
    ...
    while(gameIsRunning) {
        ...
        checkAI();
        ...
    }
    ...
}

This way you can check the current AI reactions every cycle in within the game loop. The “thinking” here is very straightforward but the checks for these conditions can become quite tricky.

More Sophisticated AI Concepts

The Finite State Machines can be extended by the usage of Hierarchical State Machines which simply means that you put a FSM instead of a state. When this state is reached you have to look which state in this nested FSM is current and if there are conditions met that change these state.

There is a whole other topic about Pathfinding Algorithms that would go beyond out of scope of this article. This includes navigation of vehicles, finding a way to walk, finding the shortest path to a point and much more. One of these algorithms is the famous C* (C Star) algorithm.

Instead of simple Decision Trees you could pimp them up to Behaviour Trees. Here you can combine the decisions with sequences. For your implementation you then have to distinguish between actions and decisions. Each node of the tree now has three states, either success, failure or running.

Is It a Good Idea to Program Game AI in C?

This depends on what you want to achieve and what your project actually is. If you are writing a Game Engine in (pure) C than it is indeed a good idea to also write your AI part in C. If, on the other hand, your application is written in C++ than there is no reason to write your AI part in pure C, because you can achieve the same thing with C++ and the performance will be nearly the same.

If you don’t have a project and just want to learn some things about Game AI then I would say using C is a very good idea. You have a procedural language that is well documented and supported, you can go cross-platform and also have a good performance. Focus on the basic concepts first and make your way.

For machine learning and AI outside of game development I would rather go with Python. There are good AI libraries for python and you will find tons of documentation, tutorials and more on how to start with this.

Marco Lieblang

Professional Programmer since 2003, passionate Programmer since the mid 90's. Developing in many languages from C/C++ to Java, C#, Python and some more. And I also may know a bit about Assembly Languages and Retro Systems.

Recent Posts