How game designers describe AI: from hardcoded behavior to HTN
Article languages: English and Russian
Contents
Someone in the Manzhety chat asked how game designers describe AI—the kind that controls mobs, not LLMs or ChatGPT.
Let’s start with the basics:
Hardcoded: we give each NPC a function that we call at some interval. It is unique to that NPC and does something.
void NPC::DoWork (Time dt)
{
//...
} After a while, we notice that some things repeat. So we start extracting them into separate blocks—behaviors, senses and so on—and reusing them.
Then we need some way to organize the logic of what happens. And we start using patterns.
🔸 FSM (finite-state machine)—a simple model in which an NPC occupies one of a set of distinct states, such as patrol, attack or rest, and transitions when conditions are met. Easy to understand and predict, but managing transitions becomes difficult when there are many states.
🔸 HFSM (hierarchical FSM)—an extension of the FSM in which states are organized into hierarchies. For example, a general Attack state can contain subcontrollers for choosing a specific move. This helps structure complex behavior.
We create several states and the conditions for moving between them. With a hierarchical state machine, we either “collapse” blocks into a single state or create several “layers,” each of which is a state machine of its own.
For example, “peaceful behavior” / “suspicious” / “combat behavior.” These then contain states such as “pursue,” “attack,” “defend,” “flee,” and so on.
This is fine for simple behaviors. The downsides: difficult to extend, difficult to reuse pieces, and difficult to inherit behaviors.
🔸 Behavior Tree—behavior is represented as a tree whose nodes are actions or decisions. Trees are easy to extend and more convenient than FSMs for complex and varied logic.
The downsides are the same: difficulties with reuse and inheritance, and duplicated pieces.
🔸 Utility AI—action selection is based on evaluating the utility of each option using formulas or curves. This makes it possible to dynamically select the best behavior, producing more “natural” and varied decisions.
This is one of the two most common approaches. We calculate weights for each behavior, then either select the one with the highest weight or make a weighted random choice among several of the highest-scoring options.
Advantages: modular, configurable, and convenient for inheritance and reuse. Diminishing returns—temporary penalties—can be built straight into the weight formulas.
🔸 GOAP (Goal-Oriented Action Planning)—a more advanced planner that constructs an optimal path of actions leading to a goal. Good for AI where adaptation and planning matter.
Essentially, it is A* pathfinding through a state space to find the “cheapest” action sequence—the shortest or the most economical in resources. It is sometimes combined with utility.
🔸 HTN (Hierarchical Task Network)—an approach aimed at decomposing high-level goals into concrete tasks. HTN describes how to turn large goals into an action sequence; it is flexible and suitable for strategy games.
It is essentially a variation on GOAP.
Self-learning and self-balancing can be layered on top of any of these approaches. Work on that AFTER you have AI working without self-learning.