How does a GOAP planner use action preconditions, effects and costs to get an assault NPC through a locked door to its target?
answer
- world state as named facts
- what an action needs, changes, costs
- the goal is a set of facts
- search back from the goal
- cheapest total chain wins
basics
~20 sGOAP models the world as facts and each action as preconditions, effects and a cost; a graph search finds the cheapest action chain that turns the current facts into the goal facts: fetch key, open door, enter, attack.
solid answer
~40 sThe **world state** is a small set of facts such as `hasKey`, `doorOpen`, `inRoom` and `targetDead`. Each **action** lists the facts it needs (**preconditions**), the facts it changes (**effects**) and a **cost**. The **goal** is a set of facts to make true, here `targetDead = true`. The planner searches a graph whose nodes are sets of facts and whose edges are actions, usually with A* and often **regressively**: start from the goal, choose an action whose effect satisfies an unmet fact, replace that fact with the action's preconditions, and stop when everything still needed already holds. With `PickUpKey` (2), `OpenDoor` (1), `KickDoor` (5), `EnterRoom` (1) and `AttackTarget` (1), the key route costs 5 and the kick route 7, so the plan runs fetch key, open door, enter, attack.
code
pseudocode · 20 lines// Regressive GOAP search with A*: facts are key = value pairs.
// Simplified: no visited-set, no procedural preconditions.
function plan(current, goal, actions):
open = min-priority queue ordered by g + h
push(open, {needs: goal, steps: [], g: 0})
while open is not empty:
node = popLowest(open)
if every fact in node.needs holds in current:
return node.steps // already in execution order
for each action in actions:
if action.effects satisfy no fact in node.needs: continue
if action.effects set a fact in node.needs to another value: continue
rest = node.needs minus the facts action.effects satisfy
if action.preconditions disagree with rest on any fact: continue
push(open, {needs: rest union action.preconditions,
steps: [action] + node.steps,
g: node.g + action.cost})
return no plan
// h(node) = number of facts in node.needs that do not hold in currentgo deeper
Recall the vocabulary: world-state facts, actions with preconditions, effects and costs, and a goal that is a set of facts to make true.
Walk the key-and-door example backwards from the goal, showing which actions branch, how costs add up and why the key route beats kicking the door.
Discuss what makes planning expensive: branching from many relevant actions, procedural checks inside the search and an inadmissible heuristic, and how you would cap or cache each.
Judge whether emergent action chains are worth their cost for this game: designers lose direct control of sequences and must author actions and costs that combine sensibly.
## The three ingredients **Goal-oriented action planning (GOAP)** lets a non-player character work out a sequence of actions at run time instead of following one a designer wrote. It needs three things: - A **world state**: a small set of named facts the planner reasons about, such as `hasKey = false`, `doorOpen = false`, `inRoom = false` and `targetDead = false`. Facts are symbolic summaries, not the full game world. - **Actions**, each with **preconditions** (facts that must hold before it can run), **effects** (facts it changes when it completes) and a **cost** (how undesirable it is: time, risk, noise). - A **goal**: a set of facts the agent wants to be true, chosen by a separate goal-selection step. Here the goal is `targetDead = true`. ## The example domain An assault NPC must kill a target behind a locked door. Its actions: | Action | Preconditions | Effects | Cost | |---|---|---|---| | `PickUpKey` | none | `hasKey = true` | 2 | | `OpenDoor` | `hasKey = true` | `doorOpen = true` | 1 | | `KickDoor` | none | `doorOpen = true` | 5 | | `EnterRoom` | `doorOpen = true` | `inRoom = true` | 1 | | `AttackTarget` | `inRoom = true` | `targetDead = true` | 1 | All four facts start false. ## How the search runs GOAP turns planning into a shortest-path problem: nodes are sets of facts, edges are actions, edge weights are costs. Many implementations run A*, which is covered with shortest-path algorithms; what GOAP adds is the graph. Many search **regressively**, from the goal back to the present: 1. Start with the set of facts still needed: `{targetDead}`. 2. Choose an action whose effects satisfy a needed fact without contradicting another needed fact. 3. Remove the facts it satisfies and add its preconditions to the needed set. 4. Stop when every needed fact already holds in the current world state. The actions, read in the reverse of the order they were chosen, are the plan. Trace on the domain above: - `AttackTarget` satisfies `targetDead` and needs `{inRoom}`; cost so far 1. - `EnterRoom` satisfies `inRoom` and needs `{doorOpen}`; cost 2. - Two actions satisfy `doorOpen`: `OpenDoor` leaves `{hasKey}` needed at cost 3, and `KickDoor` leaves nothing needed at cost 7. - `PickUpKey` satisfies `hasKey` and needs nothing, at cost 5. The cheapest complete branch costs 5, so the plan executes as `PickUpKey`, `OpenDoor`, `EnterRoom`, `AttackTarget`. If picking up the key cost more than 4, for example because the planner prices it by walking distance and the key is far away, the key route would cost more than 7 and the same search would kick the door instead. ## Forward versus regressive search - **Forward** search starts from the current state and applies any action whose preconditions hold. Every node carries a complete world state, which makes extra checks easy, but an NPC with dozens of actions has many that are applicable and irrelevant to the goal. - **Regressive** search considers only actions that supply a fact the plan still needs, so its branching factor is usually smaller. Its nodes hold only the facts still required, not a whole world state. ## Heuristic and cost A* needs an estimate of the remaining cost. A cheap, common choice is the number of needed facts that do not yet hold. In this domain it never overestimates, because every action costs at least 1 and each supplies at most one fact the plan needs. If one action costing 1 could satisfy two needed facts, the count would overestimate and A* could return a costlier plan than the best. Some teams accept that trade, and small domains often drop the heuristic altogether. Costs are where designers steer behaviour: a high cost on `KickDoor` makes the NPC prefer the quiet route, and a cost can be computed at planning time, for example from the walking distance to the key. ## Procedural preconditions and goal selection Some conditions are awkward as facts: is the key reachable, and is there a key in the level at all? Implementations attach **procedural** (or context) preconditions, code checks run during the search that prune an action when they fail. They keep the fact set small, but they run inside the search, so expensive checks multiply planning cost. GOAP plans for one goal at a time. A goal-selection layer ranks the goals that are currently relevant (kill the target, take cover, patrol) and hands the best one to the planner; if no plan exists, it tries the next.
- Why do many GOAP planners search backwards from the goal instead of forwards from the current state?Forward search branches on every action whose preconditions hold right now, and an NPC with dozens of actions has many that are irrelevant to the goal. Regressive search branches only on actions whose effects supply a fact the plan still needs, so the branching factor is usually smaller. Forward search keeps a complete world state at each node, which makes procedural checks simpler, so both directions are used.
- What is a procedural, or context, precondition in GOAP?A condition the symbolic facts cannot express cheaply, such as whether the key is reachable on the navigation mesh or whether any cover exists nearby. The action runs a code check during planning and is pruned when it fails. It keeps the fact set small, but because the check runs inside the search, an expensive test is paid many times per plan.
- What happens when no chain of actions reaches the GOAP goal?The search empties its frontier, or hits a depth or node limit, and reports failure. The goal-selection layer then tries the next most relevant goal, such as taking cover instead of attacking. Without that fallback the NPC freezes, so every agent should have a goal it can always satisfy.
- GOAP runs a fixed sequence of actions that a designer wrote in order.
- The GOAP planner searches over map positions, the way pathfinding does.
- The planner greedily takes the cheapest single action at each step.
- Every condition, even reachability, must be modelled as a world-state fact.
- Counting unmet goal facts is an admissible GOAP heuristic in every domain.