Home Journal Changelog
Characters ▾

Creature AI Upgrade: Behavior Tree Replaces Flat FSM

The creature decision-making system has been rebuilt from the ground up. The old flat, priority-ordered Finite State Machine (FSM) — with 130+ hand-authored transitions across 38 states — has been replaced by a composable Behavior Tree.

Why the Change?

The original FSM worked, but adding new behaviors meant inserting transitions into a long flat list where priority interactions were hard to reason about and anti-oscillation hysteresis was scattered across individual condition functions. The behavior tree preserves the exact same priority-based interruption model but organizes it into readable subtrees with decorator-based guards.

What’s New

  • BT Engine — Core node types: PrioritySelector (evaluates highest-priority child first), Sequence (all conditions must pass), Condition (predicate leaf), and StateAction (writes target state)
  • Decorators — Cooldown (prevents state re-entry within N seconds), FromState (restricts source states), StateGuard (excludes states), RandomGate (probabilistic gating), MinStateTime (minimum dwell time)
  • Fluent DSL — Tree definition reads like a blueprint: seq(cond(...), cooldown(..., action(...)))
  • 1:1 Transition Mapping — Every single FSM transition was faithfully ported. Weather flee, exhaustion collapse, scheduled meals, mating pursuit, ritual attendance, resource harvesting, teaching, philosophizing — all preserved with identical conditions, priorities, and hold times

Zero Behavioral Change

The new CreatureBehaviorTree class exposes the exact same public API as the old CreatureFSM. The integration was a single import swap in pet-brain.ts. Creatures continue their daily routines — sleeping, waking, eating, working, socializing, fleeing storms, pursuing mates, visiting eggs — exactly as before.

What This Enables

New behaviors can now be added as composable subtrees slotted into the priority hierarchy, without touching unrelated transitions. Debugging is easier because each branch is self-contained. And the old FSM is kept intact as a rollback option.

Files Changed

  • bt/bt-engine.ts — Core BT nodes (~130 lines)
  • bt/bt-decorators.ts — 5 decorator types (~100 lines)
  • bt/bt-builder.ts — Fluent DSL helpers (~50 lines)
  • bt/state-output.ts — Extracted shared output generation (~480 lines)
  • bt/creature-behavior-tree.ts — Full tree + class (~550 lines)
  • creature-fsm.ts — Refactored to import shared code (kept as rollback)
  • pet-brain.ts — 1-line import swap