Patrones en los videojuegos II: Inteligencia artificial

by Aitor Llamas Jiménez 20/2/2021
building-blocksartificial-intelligencestate-machinebehavior-treegoap
Archivado
Este artículo se publicó originalmente en mi antiguo blog de tecnología: Building Blocks. Actualmente está archivado y probablemente obsoleto.

One of the most interesting and important thing in the videogame development ecosystem is the Artificial Intelligence development. With AI you can for example develop behaviours for your game’s NPCs, and Enemies. When talking about AI in videogames, we need to keep in mind that most of the times it’s not the same kind of software we refer when talking about AI in software development. These times AI in software development it’s more focused on Machine Learning, Neural Networks and things like that, while game development is more focused on providing realistic behaviours for NPCs, but things are starting to change because Machine Learning is being used on game development fields like procedural generation. And now, here are the 3 patterns in videogames AI development I like the most:

State Machine

State Machine Diagram

If you have worked in software development, this could be familiar to you. Finite State Machines have been used a lot as a way to define different states an entity can have, and the transitions able to do between different states. It’s build on top of the State pattern, but it’s not the pattern itself, but a way to implement it. Specially if your state starts getting complex.

The basic building blocks of a state machine are states and transitions. A state is a situation of a system depending on previous inputs and causes a reaction on following inputs. One state is marked as the initial state; this is where the execution of the machine starts. A state transition defines for which input a state is changed from one to another. Depending on the state machine type, states and/or transitions produce outputs.

If you want to learn more about state machines here are a few links:

And if you want a nice implementation of it in JavaScript/TypeScript: XState. This one is extremelly powerful because it allows Statecharts, that’s a more complex version of FSMs.

Behavior Tree

Behavior Tree Diagram

Unlike a Finite State Machine, or other systems used for AI programming, a behaviour tree is a tree of hierarchical nodes that control the flow of decision making of an AI entity. At the extents of the tree, the leaves, are the actual commands that control the AI entity, and forming the branches are various types of utility nodes that control the AI’s walk down the trees to reach the sequences of commands best suited to the situation.

The trees can be extremely deep, with nodes calling sub-trees which perform particular functions, allowing for the developer to create libraries of behaviours that can be chained together to provide very convincing AI behaviour. Development is highly iterable, where you can start by forming a basic behaviour, then create new branches to deal with alternate methods of achieving goals, with branches ordered by their desirability, allowing for the AI to have fallback tactics should a particular behaviour fail. This is where they really shine.

If you want to learn more about behavior trees here are a few links:

Some JavaScript/TypeScript implementations:

GOAP (Goal-Oriented Action Planning)

GOAP Diagram

Goal Oriented Action Planning (GOAP) is an AI system that will easily give your agents choices and the tools to make smart decisions without having to maintain a large and complex finite state machine.

GOAP is an artificial intelligence system for autonomous agents that allows them to dynamically plan a sequence of actions to satisfy a set goal. The sequence of actions selected by the agent is contingent on both the current state of the agent and the current state of the world, hence despite two agents being assigned the same goal; both agents could select a completely different sequence of actions.

If you want to learn more about GOAP here are a few links:

Some JavaScript/TypeScript implementations: