AI ALGORITHMS IN GAMES

Nishad Ahamed
Dev Genius
Published in
8 min readSep 2, 2022

--

In this article, I would like to brief you on some powerful AI algorithms used in gaming.

AI in Gaming illustration

Making a game is a challenging endeavor. It should be expanded because it keeps people interested and offers more difficulties. The main goal of AI in video games is to present a formidable opponent to increase player enjoyment. These days, many games are created using AI techniques and features. "Pathfinding and finite state machines are two of the main components of commercial game AI," explains Julian Togelius, an associate professor at New York University's department of computer science and engineering who focuses on the interface of AI and video games. Building trust between the player and the game is one of the goals of AI elements used in video games. The following are some of the game-related components.

Intelligent agents

The most common application of game AI is with NPCs or non-player characters. These in-game characters behave intelligently, as though actual players were controlling them. AI engines and algorithms control the actions of these characters. Decision trees are frequently employed to direct the behavior of these NPCs.

The goal of AI in most games is to create an intelligent agent, sometimes known as a non-player character (NPC).In the gaming world, this agent might take on the roles of an enemy, ally, or impartial being. Studying game AI from this angle is particularly beneficial because the agent is the main focus of most game AI. An agent continuously loops through three essential processes. The process is frequently referred to as the sense-think-act cycle. An optional fourth stage that may occur within this loop in addition to these three is learning or remembering.

Just peek at Monster Hunter: World to get a fair sense of NPC AI. Monsters driven by AI exist in a living ecosystem and are effectively acting independently. Even the game's director, Yuya Tokuda, mentions the game's NPCs assaulting one another randomly. He recalls a scene from a video game in which his character was engaged in a battle with a monster when another one suddenly burst in, attacked the first one with a rock, and fled. Another instance occurred when the player was engaged in battle with one monster when another suddenly flew in, and the two monsters engaged in combat.

The monsters in the newer versions of the Monster Hunter video games can perform a wide variety of actions and can choose which moves to make based on the position the player's character is in during a move. Earlier versions of the Monster Hunter video games had the monsters making a limited number of actions. This enables the NPC to make maneuvers that would be more difficult to avoid, block, or respond to under such circumstances.

Pathfinding

Getting from one place to another is a component of pathfinding. The most crucial aspect of pathfinding is the overall gaming environment. As you explore the game area, the AI can create the game's terrain or its world. The AI can construct the landscape based on feedback it receives from your actions, playing style, in-game choices, appearance, and tactics.

There are many ways to implement pathfinding, but not all return the shortest path or are efficient or reliable. For instance:

  • Random backstepping — instead, move forward one step at a time. Avoid obstacles by taking a slight detour in an unexpected direction before attempting again. Not at all dependable and prone to getting stuck in many circumstances.
  • Obstacle tracing — This is a different technique that is similar to random backstepping. However, after a collision is detected, the object is traced around as if the right hand was stuck to the wall, and you had to move while touching it. Continue traveling in the direction of the goal once there is no contact. You can again become trapped in various circumstances.

Methods that scan the horizon to discover the whole path at once:

Breadth-first search — When a path is found, cease visiting each layer of children in a graph using the breadth-first search technique. Although not very effective, it finds the shortest path when the graph is unweighted, meaning that the distance between each adjacent node is always the same. It may not always discover the shortest path in weighted graphs, but it will always find one if one exists.

Depth-first search — Another method of traversing a graph is called a "depth-first search," which seeks to explore deep inside the chart first rather than going through it layer by layer. It is often safer to implement this approach iteratively using a stack because it can have issues if the depth of the search is not constrained, especially when using a recursive implementation, which could result in a stack overflow.

Best First Search — Similar to a breadth-first search, the best first search prioritizes the most promising neighbor first. Though the resulting path might not be the shortest, it can be processed more quickly than a breadth-first search. One variety of Best First Search is A*.

Dijkstra's Method — The Dijkstra method calculates the optimum path through the graph by keeping track of the total cost from the beginning to each visited node. It returns the shortest path and works with weighted graphs. However, it may take a while to find.

A* — Comparable to Dijkstra but also employs a heuristic to determine how probable each node is to be near the target. This approach makes it significantly quicker for A* to locate the shortest path in a weighted graph.

We can see these algorithms mostly on maze-type games and packman games. Pathfinding algorithms are necessary for the game Maze Runner to reach the goal in a fewest amount of time. This algorithm uses an NPC that will go from the start node to the destination node. However, using the wrong algorithms can lengthen the time it takes to compute the shortest path. The participants must wait longer as the computation procedure takes longer when we contrast the Maze Runner game's A *, Dijkstra, and BFS pathfinding algorithms. When comparing algorithms, we look at the time they take, how long their paths are, and how many blocks they use in the current computer system. Concerning these parameters, we can choose the best for our needs. The performance of each method may vary with the game's nature, obstacles, and path. So at once, we can't decide on the best algorithm unless we implement and compare the test results and the computational powers needed.

Case-based reasoning

Creating effective AI models for games with inaccurate information can be particularly difficult. The exponential development of the number of nodes at each level makes it particularly challenging to design game trees for these games, given the vast number of possible moves and the integrated uncertainty. This project aims to offer a Case-Based Reasoning and AI Planning combination that significantly shrinks game trees. By concentrating on the movements that lead us to particular tactics rather than considering all conceivable combinations, we can effectively ignore pointless moves. These tactics are chosen by looking for cases in the CBR database that resemble them. A group of intended goals shapes the strategies.AI planning is in charge of drafting a strategy to accomplish these objectives. The design is a series of actions that lead the player to this objective. By following these stages and ignoring the enormous number of additional moves that are feasible, the model creates Game Trees that grow more slowly, allowing for the construction of more feature moves that are constrained by the same amount of memory.

Case-based reasoning is a popular strategy in many vintage board games and adventure games. The field of real-time strategy games has also utilized CBR. Modern computer chess systems excel at playing the game of chess. CBR has been successfully applied to computers in games like chess, where human players rely heavily on their knowledge. Case-based reasoning works like this in the game of chess. It first assesses the chess player's position to determine which of the 15 plans to use and which specific variations to calculate.

The player’s patterns help him decide which move to play or, more accurately, which strategy to adopt in a particular position. Game flow in RTS games is simultaneous and continuous, unlike, for instance, the game of chess, where each player waits until the other player makes a move. The game AI must make decisions in real-time in an inaccessible, non-deterministic, dynamic, continuous environment with enormous search spaces due to the nature of the genre. Here, conventional search techniques are no longer helpful. In this case, CBR only decides how non-player characters in video games should behave. These non-player characters are those that the players do not have control over. These are governed by behaviors, which are produced by game players' choices and acts.

Decision-making

AI will allow your choices to have a more significant influence on the gameplay. For instance, in Red Dead Redemption 2, NPC interactions and conduct are influenced by things like the sort of hat you are wearing or if you have blood stains on your clothes. Your choices can affect the entire game world because there is a vast matrix of potential outcomes. There might be incredibly intricate cause-and-effect connections.

Data mining

With the aid of artificial intelligence, game developers and studios can undertake data mining on player behavior to understand better how users come to play the game, which features they enjoy playing the most, and why they stop playing it. As a result, game designers can enhance gameplay or find income options.

Behavior Trees

Thus, the name contains a hint. A behavior tree is a tree of hierarchical nodes that regulates the decision-making flow for an AI entity, in contrast to a finite state machine or other systems used for AI programming. The tree branches are made up of different sorts of utility nodes, which guide the AI’s movement down the tree to reach the commands most appropriate for the given circumstance. The tree leaves represent the commands that control the AI creature.

The developer can build libraries of behaviors that can be linked to produce very realistic AI behavior since the trees can be intense, with nodes invoking sub-trees that carry out specific functions. A fundamental behavior can be formed first; other branches can then be created to deal with alternative ways to achieve goals, with the branches being ordered by their desirability. This allows the AI to have fallback strategies if a particular behavior fails. They excel the most in this situation.

--

--

Hi, I am Nishad Ahamed, an IT undergrad. I am passionate about web development, Data Science, and Artificial intelligence.