Utility AI is a decision architecture used for NPC brains (The Sims, XCOM, many strategy games). Instead of a fixed if/else tree, every candidate action gets a numeric score every tick, and the agent simply does whichever action scores highest right now:
U(Forage) = hunger^k + 0.25 · proximity(nearestFood)
U(Rest) = fatigue^k + 0.25 · proximity(restZone)
U(Flee) = s · max(0, 1 − dist(threat) / R) [only if threat is near]
U(Wander) = 0.15 [idle floor, always available]
score = U(action) + noise · (rand() − 0.5)
choose action = argmax(score)
- k — response curve exponent. A low exponent (~1) makes agents react to a need as soon as it starts rising. A high exponent (~4) makes them ignore it almost entirely until it is nearly maxed out, then react sharply — this curve shape is the actual tuning knob game designers expose to balance NPC behavior.
- s — threat sensitivity scales how strongly a nearby threat overrides everything else, inside a fixed detection radius R.
- Decision noise adds small random jitter to every score, so near-ties don't all resolve identically — this keeps a crowd of agents from looking robotically synchronized, at the cost of some sub-optimal choices.
- Each agent tracks its own hunger and fatigue, which grow over time and reset when the agent reaches a food node or the rest zone — so the same utility formula produces different observed behavior per agent purely from their current need state.
- This 2D reimplementation reuses the exact same scoring formula as the 3D version (verified consistent — proximity terms are normalized by the map diagonal, threat falloff is linear inside radius R), rendered top-down instead of in perspective. The stacked chart below the map is the same action-mix data the 3D legend shows, but plotted as a scrolling history rather than an instantaneous snapshot.