npc-brain

What an NPC decides, as pure rules: it stands guard or wanders, notices you, chases, attacks when it can reach you, loses you, searches where it last knew you were, and goes back. A noise sends it to look. Deterministic and seeded, so the same scene plays the same way twice and a test can pin it down. b3d-npc puts it in a biped.

It follows AI-DESIGN: what it perceives (sensorium: in front out to 50 m with a clear line, behind within 10 m, a third of each when you crouch, noises in any direction) drives its ALERTNESS, which decays when nothing feeds it; SKILL multiplies how often it thinks again. The low end of the skill dial is where the design effort goes, because believable mistakes are what make an enemy fun:

import { npcBrain } from 'tosijs-3d/npc-brain'

const brain = npcBrain({ home: { x: 0, z: 0 }, seed: 7, skill: 0.2 })
// every frame:
const intent = brain.step({
  self: { x, z, heading },
  target: { x: tx, z: tz, clearLine: true, conspicuity: 1 },
  noises: [{ x: nx, z: nz, carries: 25 }],
}, dt)
// intent.moveTo, intent.pace ('stop' | 'walk' | 'run'), intent.attack, intent.mode

wanderRadius 0 makes a guard: it stands where it was put, facing the way it was facing, until something gets its attention. patrol (a list of points) makes it walk a route in a loop instead.