Mind Imitation: How Artificial Intelligence Works in Games

AI in games is a set of algorithms that dictate the behavior of NPCs in different situations. Game AI is incapable of thinking or creativity, its actions are predetermined by the developers. Despite such limitations, a well-crafted AI adjusts to the situation and changes behavior depending on the context. The AI is needed to imitate the intelligence of the NPC, and its task is not to beat the user, but to entertain him. Modern games use different approaches to create AI. There is a general principle: get information → analysis – action. Here are the most popular methods and examples of using AI in games.

How game AI gets information

Getting information is approximately the same as in the real world – the AI has special sensors, with which it researches the environment and monitors what is happening. The sensors come in a variety of forms. It can be a traditional cone of vision, “ears” that pick up loud sounds, or even olfactory receptors. Of course, such sensors are just an imitation of real senses, which allows to make the game situations more believable and interesting. 

In Tom Clancy’s Splinter Cell: Blacklist enemy dogs can track the hero by scent. This forces the player to constantly change cover and not to linger in one place.

The presence and implementation of sensors depends on gameplay. Many active shooters do not need complex receptors – a cone of sight is enough to respond to the appearance of the player. And in stealth action games, all gameplay is based on hiding from enemies, so the virtual senses are arranged more complex.

  • Artificial intelligence (AI) in gaming refers to the use of computer algorithms to generate intelligent behavior in non-player characters (NPCs) or game agents.
  • There are several categories of AI that can be utilized in gaming, including rule-based AI, decision tree AI, and machine learning AI.
  • Rule-based AI follows a predetermined set of rules to determine its actions, while decision tree AI uses a branching tree structure to make decisions based on specific conditions.
  • Machine learning AI utilizes data and algorithms to learn and adapt over time, enabling it to improve its performance without explicit programming.
  • AI can be used in a variety of gaming genres, including strategy games, role-playing games, and first-person shooters.
  • It is often used to create realistic and dynamic NPC behavior, such as enemy characters that adapt to the player’s actions or NPCs that have their own goals and motivations.
  • AI can also be used to create more immersive gameplay experiences, such as NPCs that can respond to player dialogue or interact with the game world in a more realistic way.

In the Serious Sam series, most enemies just mindlessly run at the player when he enters the battlefield. 

Virtual senses are arranged differently. In Metal Gear Solid, enemies have a very simple cone of vision, no peripheral vision, so they only see what’s going on right in front of them.

The cone of vision in Metal Gear Solid

Shadow Tactics: Blades of the Shogun also uses a cone, but it’s more complex. The cone is divided into visibility zones, which depend on the features of the landscape. If the player enters a uniform green zone, he will be seen almost immediately. In the striped zone visibility is difficult, so the player will be invisible if he moves with his head down. And in the zone covered with dots the hero is completely hidden.

In Tom Clancy’s Splinter Cell: Blacklist the visual sensor of the enemies is complex. Enemies have a basic visibility area in the form of an elongated hexagon. This is where the mob sees best, so it’s best not to go into this space at all. There is also a larger hexagon, which imitates peripheral vision – the enemy sees worse there. The same zone includes distant areas that are not seen well enough.

But the most interesting thing is that there are also zones behind the opponent in which he can spot the player. These zones simulate the feeling of someone standing behind you, so the player won’t be able to sneak up close to the enemy for long.

The peculiarity of Splinter Cell: Blacklist enemies is that they can see the player in the partial cover. If a few parts of the body peek out from behind the obstacle, the enemy notices him. To implement this the developers used raycasting technology – they cast rays from the eyes of enemy, and if they cross some parts of the body, the enemy raises an alarm.

How does the AI make a decision?

When the AI has received the information it starts to “think” its actions, analyzing the situation. This usually involves several AI systems responsible for different things at once.

Often developers add a kind of collective intelligence, which makes sure that the actions of individual agents do not contradict or interfere with each other. At the same time, the mobs themselves are often not even aware of the existence of their allies – they don’t need this information, because the higher-level AI is responsible for coordinating the actions.

There are artificial intelligence systems that act as directors. They make sure that the game maintains a balance of difficulty, and they are also responsible for the appearance of interesting and memorable situations.

The next common way to make decisions is with finite automata. This approach allows NPCs to move seamlessly between different states. For example, there is a mob whose base state is patrolling along a certain path. If a player suddenly appears, the NPC switches to a new state – it starts shooting. End automata provide these transitions: they take information from the previous state and transmit it to the new state.

The AI in Half-Life is accomplished with finite state machines. They have more than 80 unique states.

The advantage of this approach is that the character will always be in some state and won’t hover somewhere between them. Since the developer must prescribe all transitions, he knows exactly what states the game object can be in. The disadvantage of the method is that as the number of mechanics increases, so does the system of finite state machines. This increases the risk of bugs and can also reduce the speed of operations.

Behavior tree is a more formalized approach of constructing the behavior of mobs. Its peculiarity is that all character states are organized as a branching structure with a clear hierarchy. The behavior tree contains all the possible states in which a mob can find itself. When an event happens in the game, the AI checks what conditions the NPC is in and goes through all the states looking for the one that’s appropriate for the current situation.

An example of a behavior tree from Just Cause 3. Each behavior type has a huge number of variations.

A behavior tree is great for systematizing NPC states in games that have many mechanics and gameplay elements. In a situation where a mob is involved in a firefight, he will not need to search for the appropriate action in the patrol branch. This approach helps make the NPC’s behavior responsive and provides a smooth transition between different states.

Let’s say there is a mob whose main task is to patrol the area with a vehicle. Suddenly a player shows up, steals the vehicle, and blows it up. The NPC will shoot back for a while, but when the player is out of range, the mob has to go back to patrolling with the car. But the car is destroyed, so the AI cannot return to its original state. In such a situation, it must switch to a foot patrol state on its own, just like the rest of the enemies. The behavior tree is just that, allowing the NPC to easily find the new state and not get hung up.

In Gears Tactics, each type of unit has its own behavior tree, which takes into account all the individual characteristics and abilities of the NPC.

Hierarchical finite automata

Hierarchical finite automata combine the features of finite automata and the behavior tree. The peculiarity of this approach is that different graphs within the logic can refer to each other. For example, we need to prescribe behavior for several mobs. It’s not necessary to make separate logics for each one. We can create a general basic behavior and just refer to it when necessary.

There are also less popular solutions that haven’t been able to fully establish themselves in the industry. For example, F.E.A.R. used Goal-Oriented Action Planning (GOAP) – it creates an action plan for all NPCs based on information about the game world. For example, if a mob needs to move to another room, the system first checks how far to go to the door, whether there are any obstacles on the way, whether the door is open, and so on. When the system has all the information about the environment, it makes a plan, and the NPC just plays the animation sequence.

This approach works on the basis of finite automata, but they are only responsible for playing animations. The automata have only three states, each responsible for a different set of animations: movement (running, walking), actions (shooting, reactions), interaction with objects (open door, turn on light).

F.E.A.R. is remembered for its rather clever opponents who know how to work together. In fact, they are not even aware of each other’s existence – just the AI intelligently coordinates their actions.

GOAP has gradually been replaced by an AI system called the Hierarchical Task Network Planner (HTN). This network creates plans that consist of macros of actions that already have a certain sequence in them. 

Such a system is used in Horizon Zero Dawn. It allows the robotic animals to exchange information and ask the group AI for a plan of action to solve the problem.

Each enemy in Horizon Zero Dawn has its own passport, which is used to identify and add to the new pack. When a mob enters it, it is assigned a role within the group agent.

How the AI moves through space

The AI’s actions are directly related to its task: it can be a melee strike, taking a better position to shoot, helping an ally. Regardless of the task, the agent has to get to the place where the action is to be performed – to do this, it needs information about the environment.

Usually such data is contained in a navigation grid (NavMesh), which is a special map on which is marked where NPCs can move. 

Horizon Zero Dawn has six navMesh’s at once: four for creatures of different sizes, one for floating creatures, and one so that the player can correctly saddle up a mob.

On top of the navigation grid there can be a system of nodes, which is used to ensure that NPCs correctly perform behavioral scenarios. For example, in Assassin’s Creed Origins all NPCs have their own daily routine, which they follow even without player participation. To make sure the characters follow the script and do not get stuck in the environment, the developers made a special network of nodes – points on the plane, where NPCs perform their personal task. The described method is suitable for predetermined scenarios. NPCs seem to move on rails from one point to another.