Simulating NPC Memory Decay in Stealth AI

Dynamic memory decay is an artificial intelligence design technique used in stealth games to simulate non-player character (NPC) forgetfulness, allowing players to break line of sight, evade pursuit, and reset hostile states. AI programmers implement this by combining timestamped stimulus tracking, mathematical decay functions, and dynamic modifiers that degrade an NPC’s certainty over time. This guide breaks down the core architecture, mathematical models, and behavioral transitions used to build believable memory decay systems in modern game development.

Stimulus Tracking and the Knowledge Base

To forget information, an NPC must first record it in a structured format. Programmers typically store sensory events on an NPC-specific blackboard or within a perception component. A memory entry generally consists of:

  • Stimulus Type: Visual, auditory, or environmental (e.g., an open door).
  • Target Entity: A reference to the player or object of interest.
  • Last Known Position (LKP): A 3D vector representing where the stimulus occurred.
  • Timestamp: The game time when the stimulus was registered or refreshed.
  • Confidence/Salience Value: A floating-point value (typically normalized between 0.0 and 1.0) representing the NPC’s certainty about the target's presence.

When an NPC loses direct sensory contact (such as the player breaking line of sight behind cover), the memory record stops updating its position and begins its decay lifecycle.

Mathematical Decay Functions

Programmers decay memory by decreasing the confidence score during each AI tick. Two primary mathematical approaches are commonly used:

1. Linear Decay

Linear decay subtracts a fixed value multiplied by delta time:

\[\text{Confidence} = \text{Confidence} - (\text{DecayRate} \times \Delta t)\]

While simple to compute, linear decay often feels artificial because real human recall drops off rapidly before leveling off into passive suspicion.

2. Exponential or Logarithmic Decay

Exponential decay mimics human cognitive fading more naturally. Confidence drops steeply immediately after losing sight, then slows down:

\[\text{Confidence}(t) = \text{Confidence}_0 \times e^{-\lambda t}\]

Where \(\lambda\) represents the decay coefficient and \(t\) is the elapsed time since the stimulus was last observed. This ensures NPCs react with high urgency immediately after losing sight, followed by a prolonged, lower-certainty search phase.

Contextual Modifiers to Decay Rates

Memory decay is rarely static; programmers apply contextual multipliers to the decay rate (\(\lambda\)) to mirror situational awareness:

  • Alertness Level: An NPC in a high-alert "combat" state decays memory much slower than an NPC in an "unalert" patrol state who merely heard a faint footstep.
  • Stimulus Intensity: A direct visual confirmation of the player decays slower than a secondary clue, like an extinguished torch or a fleeting shadow.
  • Environmental Obstacles: Distance, rain, fog, and darkness accelerate decay rates, making NPCs question their perceptions faster.
  • Distractions: Introducing new stimuli (e.g., a thrown rock or an alarm) applies a spike to the decay rate of the previous memory, simulating divided attention.

Translating Decay to Behavior: The Search Radius

As an NPC's confidence value decays, AI systems transform that fading data into actionable spatial behavior. Rather than searching a single static coordinate, programmers expand the Last Known Position into a dynamic search volume:

  1. Confidence Near 1.0: The NPC navigates directly to the exact LKP with high precision.
  2. Confidence Dropping (0.8 to 0.4): The AI generates an expanding uncertainty radius around the LKP. The lower the confidence, the wider the radius grows, causing the NPC to look around neighboring rooms, alcoves, or hiding spots.
  3. Confidence Threshold Reached (Below 0.2): The NPC abandons the localized search, plays a "lost target" bark, and initiates a sweep back to their standard patrol route.

Integration with State Machines and Behavior Trees

Dynamic memory decay drives state transitions in Behavior Trees or Hierarchical Finite State Machines (HFSMs). Decorator nodes continuously evaluate memory confidence against predefined thresholds:

  • Combat State: Requires active visual contact or confidence \(> 0.8\).
  • Investigate/Search State: Active when direct contact is lost, but confidence remains \(> 0.3\). The AI queries the navigation mesh within the expanding search radius.
  • Suspicious/Cautious State: Confidence between \(0.1\) and \(0.3\). The NPC resumes patrol animations but retains a heightened detection speed and tighter vision cones.
  • Idle/Patrol State: Confidence hits \(0.0\). The memory entry is garbage-collected or flagged inactive, fully restoring the NPC to baseline behavior.