Goal-Oriented Action Planning
In a state machine or behavior tree, the designer decides the order of actions in advance: "pick up the gun, load it, shoot". Goal-Oriented Action Planning (GOAP) lets the agent work out the order itself. You give it a goal ("the target is dead"), a list of actions with preconditions, effects and costs, and the current world state. A planner searches for the cheapest sequence of actions that turns the current state into one where the goal holds.
GOAP was popularised by Jeff Orkin's AI for F.E.A.R. (2005), whose soldiers seemed to improvise: flanking, knocking over tables for cover, falling back to reload. Each soldier had a handful of goals and a pool of actions, and the planner combined them differently depending on the situation.
The pieces
| Piece | Example |
|---|---|
| World state: a set of facts | HasGun = false, GunLoaded = false, NearTarget = false, KnifeNearby = true |
| Goal: facts that must be true | TargetDead = true |
| Action: preconditions → effects, with a cost | Shoot: needs HasGun, GunLoaded → TargetDead; cost 2 |
| Planner: search over world states | A* where each node is a world state and each edge an action |
Each action is small and knows nothing about the others. "Load gun" only knows it needs a gun and ammo and produces a loaded gun. The plan is discovered.
A GOAP planner in C#
World states are bit masks (one bit per fact), so comparing and hashing states is cheap. The planner is A* forward from the current state: g is the cost so far, and the heuristic h counts goal facts not yet true. Ties are broken by insertion order, so the result is deterministic.
using System;
using System.Collections.Generic;
using System.Linq;
string[] factNames = { "HasGun", "HasAmmo", "GunLoaded", "NearTarget", "HasKnife", "TargetDead", "GunInRoom", "KnifeNearby" };
int F(string name) => 1 << Array.IndexOf(factNames, name);
int Facts(params string[] names) => names.Aggregate(0, (m, n) => m | F(n));
var actions = new List<GAction>
{
new("PickUpGun", Facts("GunInRoom"), Facts("HasGun"), 3),
new("PickUpAmmo", 0, Facts("HasAmmo"), 2),
new("LoadGun", Facts("HasGun", "HasAmmo"), Facts("GunLoaded"), 1),
new("Shoot", Facts("GunLoaded"), Facts("TargetDead"), 2),
new("GrabKnife", Facts("KnifeNearby"), Facts("HasKnife"), 1),
new("MoveToTarget", 0, Facts("NearTarget"), 4),
new("Stab", Facts("HasKnife", "NearTarget"), Facts("TargetDead"), 1),
};
int goal = Facts("TargetDead");
Plan("gun in the room, knife nearby", Facts("GunInRoom", "KnifeNearby"));
Plan("only a gun in the room", Facts("GunInRoom"));
Plan("already armed and loaded", Facts("HasGun", "GunLoaded", "KnifeNearby"));
Plan("nothing available", 0);
void Plan(string title, int start)
{
var (plan, cost, expanded) = Planner.Search(start, goal, actions);
string steps = plan == null ? "no plan" : string.Join(" -> ", plan);
Console.WriteLine(quot;{title}:\n {steps}" + (plan == null ? "" : quot; (cost {cost})") + quot; [{expanded} states expanded]");
}
record GAction(string Name, int Pre, int Effects, int Cost);
static class Planner
{
public static (List<string>? plan, int cost, int expanded) Search(int start, int goal, List<GAction> actions)
{
int H(int s) => System.Numerics.BitOperations.PopCount((uint)(goal & ~s)); // goal facts still missing
var open = new List<(int f, int order, int state)> { (H(start), 0, start) };
var g = new Dictionary<int, int> { [start] = 0 };
var cameFrom = new Dictionary<int, (int prev, string action)>();
var closed = new HashSet<int>();
int order = 1, expanded = 0;
while (open.Count > 0)
{
open.Sort((a, b) => a.f != b.f ? a.f.CompareTo(b.f) : a.order.CompareTo(b.order));
var (_, _, s) = open[0];
open.RemoveAt(0);
if (!closed.Add(s)) continue;
expanded++;
if ((s & goal) == goal)
{
var plan = new List<string>();
for (int x = s; x != start; x = cameFrom[x].prev) plan.Insert(0, cameFrom[x].action);
return (plan, g[s], expanded);
}
foreach (var a in actions)
{
if ((s & a.Pre) != a.Pre) continue; // preconditions not met
int next = s | a.Effects;
if (next == s) continue; // action changes nothing
int cost = g[s] + a.Cost;
if (g.TryGetValue(next, out int old) && old <= cost) continue;
g[next] = cost;
cameFrom[next] = (s, a.Name);
open.Add((cost + H(next), order++, next));
}
}
return (null, 0, expanded);
}
}gun in the room, knife nearby:
GrabKnife -> MoveToTarget -> Stab (cost 6) [10 states expanded]
only a gun in the room:
PickUpAmmo -> PickUpGun -> LoadGun -> Shoot (cost 8) [9 states expanded]
already armed and loaded:
Shoot (cost 2) [2 states expanded]
nothing available:
no plan [4 states expanded]
The same seven actions produce four different behaviours. With a knife nearby, grabbing it, walking over and stabbing (cost 6) beats fetching the gun and ammo (cost 8). Without the knife, the gun route is the only one. An already loaded gun is used straight away. And when nothing is available, the planner reports failure, so the agent can fall back to another goal (flee, call for help).
Note: This heuristic (number of missing goal facts) is admissible here because every action costs at least 1 and the goal has one fact. With multi-fact goals and actions that satisfy several facts at once, it can overestimate; then use
h = 0(Dijkstra) or scale it down. Plans are short in practice, so the search is small either way.
Making GOAP practical
- Keep the fact set small. F.E.A.R.-style planners use a few dozen facts at most. Anything continuous ("distance to target") becomes a fact only through thresholds ("NearTarget").
- Procedural preconditions. Some preconditions can't be facts: "is there a path to the ammo box?" Actions get an extra
CanRun(agent)check that the planner calls during the search. - Plan rarely, follow often. Plan when the goal changes or the plan fails, not every frame. While executing, check each action's preconditions before running it; if the world changed (the ammo box was taken), re-plan.
- Goals are chosen separately. Picking which goal to pursue (kill, survive, investigate) is usually a utility or priority decision; see Utility AI. GOAP only answers "how".
- Costs are design levers. Make
Stabcheaper and agents prefer melee; makeMoveToTargetcostly for cowardly enemies.
GOAP vs behavior trees
| Behavior tree | GOAP | |
|---|---|---|
| Who orders the actions | the designer, in the tree | the planner, at runtime |
| Surprises | few: easy to predict and test | more emergent, sometimes unwanted |
| Cost | cheap ticks | a search on each re-plan |
| Authoring | visual trees, easy for designers | actions + facts + costs, harder to debug |
| Good for | most games | agents with many interchangeable ways to reach goals (immersive sims, survival, tactics) |
Hierarchical Task Network (HTN) planners are the other big planning family: they decompose high-level tasks into methods and subtasks the designer writes, which keeps more control in the designer's hands. Several big open-world and tactical games have used HTN planners.
Test yourself
In GOAP, what does the planner search over?
- Navigation mesh polygons
- World states, connected by actions whose preconditions hold
- Behavior tree nodes
- Animation frames
Why did the planner choose the knife in the first scenario?
- Knives are always preferred
- Its total cost (6) was lower than the gun route (8)
- The gun was missing
- Actions are tried alphabetically
When should a GOAP agent re-plan?
- Every frame
- When the goal changes or an action's preconditions no longer hold
- Never
- Only when the game loads