# @ferret/ai: API

NavMesh queries, crowd agents, steering and the small behaviours and trees NPCs run.

48 exports. Generated from the source (`packages/ai/src`), so it matches the code.

## Classes

### Attention

*class* · `packages/ai/src/behaviours.ts:76`

```ts
class Attention extends Component<AttentionParams>
```

Stops and turns to face a target that comes near (the player), resumes when it leaves; `startTalk` / `endTalk` hold the
character in place regardless of distance. Roam is paused while attending. Emits `ai:attend` and `ai:resume`.

- `attending: boolean`
- `talking: boolean`
- `onAttach()`
- `startTalk()`
- `endTalk()`
- `onFixedUpdate()`

### AutoWalk

*class* · `packages/ai/src/autowalk.ts:16`

```ts
class AutoWalk
```

Walks a character along a NavMesh path to a point, on the fixed step, through the same movement code a person uses. It fails when the
capsule falls through the ground, sticks in a wall or does not get there in time. This is the regression test of a level's collision.

- `constructor(private world: World, private character: () => Walker | null)`
- `get active()`
- `buildNav(o: { agentRadius?: number; agentHeight?: number; maxClimb?: number; maxSlope?: number; cellSize?: number }): NavService` — A NavMesh of the whole level for walking (the level's own may cover only the volume of its NavMeshBoundsVolume).
- `go(nav: NavService, goal: BABYLON.Vector3, timeout = 60): Promise<WalkResult>`

### BehaviorTree

*class* · `packages/ai/src/tree.ts:108`

```ts
class BehaviorTree
```

- `readonly bb: Blackboard`
- `lastStatus: Status`
- `constructor(readonly root: BTNode, bb: Blackboard = {})`
- `update(dt: number): Status`
- `reset()`

### BTNode

*class* · `packages/ai/src/tree.ts:12`

```ts
abstract class BTNode
```

- `abstract tick(ctx: TreeContext): Status`
- `reset(): void` — called when a running node is interrupted by a higher-priority branch

### Follow

*class* · `packages/ai/src/behaviours.ts:130`

```ts
class Follow extends Component<FollowParams>
```

Walk after an entity and stop `distance` short of it.

- `onAttach()`
- `onFixedUpdate()`

### NavAgent

*class* · `packages/ai/src/agent.ts:41`

```ts
class NavAgent extends Component<NavAgentParams>
```

Moves an entity over the NavMesh with the crowd: asks for a target, follows the path, steers round other agents.
The crowd's position is the entity's position (the NavMesh is the floor). Exposes `velocity`, `grounded` and
`motionState()` so an Animator drives the same graph as for a player. `speedScale` slows or stops it (talking, time dilation).

- `agent: CrowdAgent`
- `nav: NavService`
- `velocity: Vector3`
- `grounded: boolean`
- `crouching: boolean`
- `speedScale: number` — 0..1 multiplier on the speed; 0 holds the agent still
- `facing: Vec3 | null` — if set, the body turns to face this point whenever the agent is (nearly) still
- `yaw: number`
- `yawRate: number` — radians per second the body turned at in the last step (+ to the right, toward +x); an Animator feeds it as `turnRate`
- `heading: number | null` — the way the path asks the body to face (radians, `atan2(x, z)`), when it is moving along one
- `get target(): BABYLON.Vector3 | null`
- `get arrived(): boolean` — True when there is no target, or the agent got to it: within `arriveDistance`, or standing still within three times that (another agent may be on the spot, and the crowd then lets it stop short instead of pushing).
- `get speed(): number`
- `onAttach()`
- `falling: boolean` — True while the agent is off the NavMesh under its own fall (after `launch`): the crowd does not move it then.
- `launch(v: Vec3)` — Throw the agent off the NavMesh (an explosion, a ledge, a catapult). It leaves the crowd, flies and falls under the gravity field of capability G when that is on (else under `params.gravity`, 9.81 by default), and rejoins the crowd where it lands on the NavMesh (`nav:landed`). Hundreds of agents that never launch pay nothing.
- `moveTo(p: Vec3): boolean` — Ask to walk to p (snapped to the NavMesh). False if p is off the mesh.
- `stop()`
- `warp(p: Vec3)` — Put the agent somewhere at once (a spawn, a transformation that moves it).
- `setSpeed(v: number)` — change the walking speed (m/s): a roaming animal walks, trots or runs trip by trip
- `get topSpeed(): number`
- `motionState()`
- `onFixedUpdate(dt: number)`
- `onDetach()`

### NavService

*class* · `packages/ai/src/nav.ts:45`

```ts
class NavService implements Service
```

The navigation service: builds or loads a NavMesh, answers path and point queries, and steps the crowd that moves the
NavAgents (path following, steering, avoiding each other). One NavMesh per world; a level change builds or loads another.

- `readonly name: "nav"`
- `navMesh: NavMesh | null`
- `query: NavMeshQuery | null`
- `crowd: Crowd | null`
- `readonly random: () => number`
- `constructor(readonly opts: NavOptions = {})`
- `init(world: World)`
- `get hasMesh(): boolean`
- `build(o: NavBuildOptions = {}): number` — Build the NavMesh from level geometry. Returns the number of polygons.
- `load(data: Uint8Array)` — Load a NavMesh made earlier (`export()`), the level's navmesh.bin.
- `export(): Uint8Array`
- `polyCount(): number`
- `clear()`
- `closest(p: Vec3, range = 2): BABYLON.Vector3 | null` — The nearest point of the NavMesh to p, or null if nothing is within `range` metres.
- `isOnMesh(p: Vec3, tolerance = 0.3): boolean`
- `randomPointAround(centre: Vec3, radius: number): BABYLON.Vector3 | null` — A random point on the NavMesh within `radius` of `centre` (the service's seeded random numbers).
- `queryExtent: { x: number; y: number; z: number; }` — How far from the NavMesh a path's ends may be and still be projected onto it (half sizes, metres). Unreal's `ANavigationData::DefaultQueryExtent` is 50 x 50 x 250 cm: a goal up to 2.5 m above or below the floor (an actor's origin, a book on its stand) still has a path. recast's own default (1, 1, 1) loses such goals. The horizontal 1 m is kept as before.
- `path(from: Vec3, to: Vec3): BABYLON.Vector3[] | null` — The straight-line corners of the shortest path, or null when there is none.
- `pathLength(from: Vec3, to: Vec3): number`
- `showDebug(on: boolean)` — Show the NavMesh as a translucent mesh (debug view).
- `fixedUpdate(dt: number)` — Called before the components' fixed update, at the fixed rate.
- `dispose()`

### Roam

*class* · `packages/ai/src/behaviours.ts:20`

```ts
class Roam extends Component<RoamParams>
```

Walk to random points within a radius of home, wait a while at each, repeat. Needs a NavAgent.

- `home: Vector3`
- `active: boolean` — a game or another behaviour may switch roaming off (talking, a cutscene)
- `onAttach()`
- `onFixedUpdate(dt: number)`

## Functions

### clipFor

*function* · `packages/ai/src/posture.ts:192`

```ts
function clipFor(s: PostureState, id: number, table: Record<Posture, string[]> = CC_CLIPS): ClipChoice
```

the clip for a person: `id` picks among the variants of a posture, the same person always the same.

### clipToBounds

*function* · `packages/ai/src/nav-geometry.ts:111`

```ts
function clipToBounds(g: { positions: Float32Array; indices: Uint32Array }, bounds?: NavBuildOptions['bounds'])
```

cut the geometry to the box `bounds`: triangles are clipped against its six planes (a ground made of two big triangles is cut too)

### collectGeometry

*function* · `packages/ai/src/nav-geometry.ts:30`

```ts
function collectGeometry(meshes: BABYLON.AbstractMesh[]): { positions: Float32Array; indices: Uint32Array }
```

World-space triangles of the meshes, in the flat arrays recast takes.

### cutBoxes

*function* · `packages/ai/src/nav-geometry.ts:67`

```ts
function cutBoxes(g: { positions: Float32Array; indices: Uint32Array }, boxes?: NavExcludeBox[])
```

remove the parts of the geometry inside each box: a triangle is split by the box's six planes and the pieces outside are kept

### fireActuator

*function* · `packages/ai/src/actuate.ts:20`

```ts
function fireActuator(world: World, actuator: string): BTNode
```

Run an actuator from the start: one impulse or one cycle (a stroke).

### goPosture

*function* · `packages/ai/src/posture.ts:167`

```ts
function goPosture(s: PostureState, p: Posture)
```

order him into a posture now (a worker who bends to a task and rises from it): the change takes the time of the posture chain and goes on from the pose the body shows

### initNavigation

*function* · `packages/ai/src/nav.ts:25`

```ts
function initNavigation(opts: NavigationInit = {}): Promise<void>
```

Load the WebAssembly of recast-navigation (once). Await it before `new NavService()`.

### newPosture

*function* · `packages/ai/src/posture.ts:50`

```ts
function newPosture(seed: number, posture: Posture = 'stand'): PostureState
```

### poseKeys

*function* · `packages/ai/src/posture.ts:150`

```ts
function poseKeys(s: PostureState): Array<{ posture: Posture; weight: number }>
```

what the body shows now as a blend of key poses: the weights (summing to 1) of the postures to draw. A change of posture is a chain of poses the body passes through
(stand, crouch, sit, lie), two neighbours in the chain blended by a smooth step, so a man lowers and rises instead of one picture dissolving into another.

### poseOf

*function* · `packages/ai/src/posture.ts:131`

```ts
function poseOf(s: PostureState): { posture: Posture; amount: number }
```

what the body shows now: a posture and how far into it (0 standing, 1 all the way down), through the change under way. A change between two low postures goes
up out of the first and down into the second. The game draws `posture` at `amount` (a pose blended with standing by it).

### setCommand

*function* · `packages/ai/src/actuate.ts:15`

```ts
function setCommand(world: World, actuator: string, value: number | ((bb: Blackboard) => number)): BTNode
```

Set an actuator's command (a throttle, a lever); the value may be read from the blackboard.

### standUp

*function* · `packages/ai/src/posture.ts:169`

```ts
function standUp(s: PostureState)
```

order him up (before he walks)

### stepPosture

*function* · `packages/ai/src/posture.ts:70`

```ts
function stepPosture(s: PostureState, dt: number, activity: Activity, o: { crowded?: number } = {}): PostureResult
```

Advance one person by dt seconds in the given activity. `crowded`: 0..1 how close the neighbours are (a sitting man in a crowd is unlikely, a lying one more so).

### switchActuator

*function* · `packages/ai/src/actuate.ts:25`

```ts
function switchActuator(world: World, actuator: string, on: boolean): BTNode
```

Switch an actuator on or off.

### worldPos

*function* · `packages/ai/src/agent.ts:8`

```ts
function worldPos(n: BABYLON.TransformNode): BABYLON.Vector3
```

The world position of a node, up to date even when nothing has rendered since it moved (Babylon caches the matrix per render id).

## Interfaces

### AttentionParams

*interface* · `packages/ai/src/behaviours.ts:61`

```ts
interface AttentionParams
```

- `target?: string` — the entity to look at: found by name or tag (default tag "player")
- `radius?: number` — closer than this the character stops and turns to the target
- `leaveRadius?: number` — stays interested until the target is this far (hysteresis; default radius * 1.3)
- `visible?: (from: BABYLON.Vector3, to: BABYLON.Vector3) => boolean` — whether the character can see the target from here (smoke, fog, walls); a target that cannot be seen is not attended to. Default: always

### ClipChoice

*interface* · `packages/ai/src/posture.ts:183`

```ts
interface ClipChoice
```

- `clip: string`
- `from: number` — play only this part of the clip, as fractions of its length (a lying man holds the end of Death_Backward and stirs within it)
- `to: number`
- `pingPong: boolean` — play back and forth over the part instead of looping it
- `loop: boolean`

### FollowParams

*interface* · `packages/ai/src/behaviours.ts:121`

```ts
interface FollowParams
```

- `target?: string`
- `distance?: number` — keep this far from the target
- `tolerance?: number` — re-plan when the target moved more than this

### NavAgentParams

*interface* · `packages/ai/src/agent.ts:12`

```ts
interface NavAgentParams
```

- `speed?: number` — walking speed, m/s
- `radius?: number`
- `height?: number`
- `acceleration?: number` — m/s^2
- `separation?: number` — how hard it keeps away from other agents (0..)
- `faceMovement?: boolean` — turn the body toward where it moves (default true); rate in rad/s
- `turnRate?: number`
- `arriveDistance?: number` — arrived when this close to the target
- `gravity?: number` — m/s^2 while falling or flying after a launch, when no gravity field (capability G) is on
- `turnFirst?: number` — An animal's way of setting off: the body first turns toward where the path goes and walks only as far as it points that way. The speed falls to nothing when the heading is off by this many radians or more (the body then pivots on the spot) and is full within a quarter of it. Default: off, the agent walks at once and turns as it goes.

### NavBuildOptions

*interface* · `packages/ai/src/nav-geometry.ts:9`

```ts
interface NavBuildOptions
```

- `agentRadius?: number` — the character the mesh is made for (metres)
- `agentHeight?: number`
- `maxClimb?: number` — highest step the agent walks up
- `maxSlope?: number` — steepest slope, degrees
- `cellSize?: number` — voxel size: smaller is more exact and slower to build (default agentRadius / 2)
- `bounds?: NavBox | NavBox[]` — only geometry inside this box (world metres) is used, like an Unreal NavMeshBoundsVolume; default: everything. A list is several volumes: geometry inside any of them is used (Unreal builds in the union of its volumes' boxes)
- `exclude?: NavExcludeBox[]` — boxes (world metres) where nothing is walkable, like Unreal's NavModifierVolume with the null area: the geometry inside them is cut away before the mesh is built. A box with `yaw` is a turned volume
- `meshes?: BABYLON.AbstractMesh[]` — meshes to build from; default: every mesh with `checkCollisions` and no skeleton

### NavigationInit

*interface* · `packages/ai/src/nav.ts:16`

```ts
interface NavigationInit
```

- `wasmUrl?: string` — where recast-navigation.wasm.wasm is (default: `recast/` under the library folder, `globalThis.FERRET_LIB` or `lib/ferret/`; the OS Beroun page sets it to `/lib/ferret/`)
- `wasmBinary?: ArrayBuffer | Uint8Array` — or the bytes themselves (tests, hosts without fetch)

### NavOptions

*interface* · `packages/ai/src/nav.ts:8`

```ts
interface NavOptions
```

- `maxAgents?: number`
- `maxAgentRadius?: number`
- `seed?: number` — seed of the service's random numbers (roaming); the same seed gives the same walks

### PostureResult

*interface* · `packages/ai/src/posture.ts:62`

```ts
interface PostureResult
```

- `canWalk: boolean` — the person can walk now (standing, not changing); a walking order should wait for this and call `standUp`
- `wantsStep: boolean` — a few steps about the spot would be natural now (a standing waiter shifting position): the game picks the point

### PostureState

*interface* · `packages/ai/src/posture.ts:32`

```ts
interface PostureState
```

- `posture: Posture`
- `to: Posture | null` — the posture he is changing to (null when settled) and the time left in the change, s
- `change: number`
- `changeTotal: number` — the whole time of the change under way, s
- `hold: number` — seconds left in the current posture
- `fidget: number` — a small happening now: weight shift, glance, stir; 0 when none, its time left in s
- `fidgetKind: 'none' | 'shift' | 'look' | 'stir' | 'scratch'`
- `nextFidget: number` — time to the next fidget
- `rs: number` — the person's seed state

### RoamParams

*interface* · `packages/ai/src/behaviours.ts:6`

```ts
interface RoamParams
```

- `radius?: number` — roam within this distance of the home point (default: where the entity starts)
- `home?: [number, number, number]`
- `pause?: [number, number]` — seconds to wait at each stop: [min, max]
- `paces?: { speed: number; weight?: number; minDistance?: number }[]` — The paces of a trip, m/s, chosen by weight each time it sets off (an animal mostly walks, sometimes trots or runs). Default: the agent's own speed. Trips farther than `minDistance` metres are the ones that may be faster than the first pace.

### TreeContext

*interface* · `packages/ai/src/tree.ts:10`

```ts
interface TreeContext
```

- `bb: Blackboard`
- `dt: number`

### Walker

*interface* · `packages/ai/src/autowalk.ts:6`

```ts
interface Walker
```

What the walk drives: a character's mesh (position), its yaw and its move input (`CharacterMovement` fits).

- `mesh: BABYLON.AbstractMesh`
- `yaw: number`
- `setMoveInput(dir: BABYLON.Vector3, run: boolean): void`

### WalkResult

*interface* · `packages/ai/src/autowalk.ts:10`

```ts
interface WalkResult
```

- `goal: number[]`
- `reached: boolean`
- `reason: 'reached' | 'stuck' | 'fell' | 'no path' | 'timeout'`
- `seconds: number`
- `position: number[]`
- `distanceLeft: number`

## Types

### Activity

*type* · `packages/ai/src/posture.ts:11`

```ts
type Activity = 'wait' | 'queue' | 'cover' | 'walk' | 'aboard'
```

### Blackboard

*type* · `packages/ai/src/tree.ts:8`

```ts
type Blackboard = Record<string, any>
```

### NavBox

*type* · `packages/ai/src/nav-geometry.ts:5`

```ts
type NavBox = { min: [number, number, number]; max: [number, number, number] }
```

### NavExcludeBox

*type* · `packages/ai/src/nav-geometry.ts:7`

```ts
type NavExcludeBox = NavBox & { yaw?: number }
```

a box cut out of the geometry; `yaw` (radians about +y, Babylon's sense, the same as a node's rotation.y) turns the box about its own centre

### Posture

*type* · `packages/ai/src/posture.ts:10`

```ts
type Posture = 'stand' | 'talk' | 'sit' | 'crouch' | 'lie'
```

Posture AI for crowds: what a person who is not going anywhere does with his body. Each person is in one posture (stand, talk, sit, crouch, lie) for a while, then changes
to another by a weight table that depends on what he is doing (waiting, queueing, taking cover), with the small happenings of real waiting in between: shifting weight,
looking round, a few steps, a stir (a lying man turns over, lifts his head), getting up before he walks. Pure numbers (no engine): a seeded generator per person, a timer,
the posture and the "doing" flags; the game maps `posture` and `fidget` to animation clips (`clipFor`) or to a vertex-animation pose, and moves the person when `wantsStep`
gives a point. Deterministic from the seed, so the same crowd waits the same way on every peer.

### Status

*type* · `packages/ai/src/tree.ts:7`

```ts
type Status = 'success' | 'failure' | 'running'
```

A small behaviour tree runner (the equivalent of UE's Behavior Tree + Blackboard): composites, decorators and leaves
tick every update and answer `success`, `failure` or `running`. A running leaf is resumed where it was. The blackboard
is a plain object shared by the nodes. Pure logic, no Babylon.

### Vec3

*type* · `packages/ai/src/nav-geometry.ts:27`

```ts
type Vec3 = { x: number; y: number; z: number }
```

## Constants

### bt

*const* · `packages/ai/src/tree.ts:83`

```ts
const bt: { action: (fn: (ctx: TreeContext) => boolean | void | Status) => BTNode; condition: (fn: (bb: Blackboard) => boolean) => BTNode; sequence: (...kids: BTNode[]) => BTNode; selector: (...kids: BTNode[]) => BTNode; wait: (seconds: number | ((bb: Blackboard) => number)) => BTNode; not: (kid: BTNode) => BTNode; succeed: (kid: BTNode) => BTNode; guard: (cond: (bb: Blackboard) => boolean, kid: BTNode) =>  …
```

### CC_CLIPS

*const* · `packages/ai/src/posture.ts:176`

```ts
const CC_CLIPS: Record<Posture, string[]>
```

the clips of the Character Customizer set (`cc-clips`) for each posture; a game with other clips passes its own table

### POSTURE_DOWN

*const* · `packages/ai/src/posture.ts:29`

```ts
const POSTURE_DOWN: Record<Posture, number>
```

the time it takes to get from standing into a posture or back up out of it, s (a man sits down in two or three seconds, lies down in four; a change between two
low postures goes up through standing and down again, taking both)

### POSTURE_HOLD

*const* · `packages/ai/src/posture.ts:26`

```ts
const POSTURE_HOLD: Record<Posture, [number, number]>
```

how long (s) a posture is held before he thinks again: [min, max]

### POSTURE_WEIGHTS

*const* · `packages/ai/src/posture.ts:14`

```ts
const POSTURE_WEIGHTS: Record<Activity, Partial<Record<Posture, number>>>
```

weights of the next posture by activity; a missing posture has weight 0

