# @ferret/ferretic: API

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

## Classes

### FerreticError

*class* · `packages/ferretic/src/parse.ts:9`

```ts
class FerreticError extends Error
```

Ferretic: the engine's language for playing a game by script (tests, checks, measured runs). BASIC to read: one statement a
line (or several with `:`), keywords in any case, no line numbers and no GOTO; structured IF / WHILE / DO / FOR / REPEAT / SUB /
FUNCTION, game time WAITs, the game's own input actions, what the game shows (positions, speeds, exposed values, events),
EXPECT / ASSERT, LOG / SAMPLE tables, PARALLEL branches and ON EVENT handlers. This file reads the text into a tree.

- `constructor(message: string, readonly line = 0, readonly col = 0)`

### FerreticRun

*class* · `packages/ferretic/src/run.ts:143`

```ts
class FerreticRun
```

One run of a program. Call `tick()` after every fixed step of the game (and, while the game is paused, from time to time:
then no game time passes). `done` resolves with the report.

- `readonly program: Program`
- `readonly report: FerreticReport`
- `readonly done: Promise<FerreticReport>`
- `constructor(readonly host: FerreticHost, source: string | Program, readonly opts: RunOptions = {})`
- `get running()`
- `get isFinished()`
- `shot(name: string): string | undefined` — the pictures SHOT took, by name
- `takeManualSteps(): number`
- `now(): number` — game seconds since the run began; the sum of fixed steps carries float noise (2 s is 1.999999999999993), taken off at a nanosecond
- `start(): void`
- `stop(): void` — stop from outside (the command test.stop): the report says "stopped"
- `tick(stepped = true): void` — one fixed step of the game has passed (`stepped`), or the game is paused and nothing passed (`stepped = false`)
- `*ev(e: Expr, scope: Scope): GenOf<Value>`
- `rnd(): number`
- `pos(v: Value, line: number): Vec`
- `velocity(name: string, line: number): Vec`
- `frameCount()`
- `currentTask()`
- `failures()`

### FerreticTest

*class* · `packages/ferretic/src/session.ts:14`

```ts
class FerreticTest
```

- `readonly program: Program`
- `readonly runs: number`
- `readonly done: Promise<FerreticTestReport>`
- `run: FerreticRun | null`
- `constructor(readonly host: FerreticHost, source: string, readonly opts: TestOptions = {})`
- `get index()`
- `get finished()`
- `tick(stepped = true): void` — after every fixed step of the game (`stepped`), or now and then while it is paused
- `stop(): void`
- `reportOf(run?: number): FerreticReport` — the full report of one run (1..n; default: the last that ran or the one running)
- `report(): FerreticTestReport` — the report so far (the last run's, with the runs before it)

## Functions

### bindFerretic

*function* · `packages/ferretic/src/bind.ts:101`

```ts
function bindFerretic(world: World): FerreticBinding
```

Ferretic on a world: the commands test.*; returns the binding. Once per world.

### checkFerretic

*function* · `packages/ferretic/src/run.ts:758`

```ts
function checkFerretic(source: string): { ok: boolean; error: { line: number; col: number; message: string } | null; routines: string[]; runs: number | null }
```

Parse only: the problems with their lines, the SUBs and FUNCTIONs, RUNS.

### frameColour

*function* · `packages/ferretic/src/probes.ts:154`

```ts
async function frameColour(dataUrl: string, reference?: string)
```

the mean colour, the contrast (standard deviation of the luminance, 0..1) and, with a reference, how far the two frames are apart (mean absolute difference, 0..1)

### makeProbes

*function* · `packages/ferretic/src/probes.ts:69`

```ts
function makeProbes(world: World): ProbeSet
```

### parseFerretic

*function* · `packages/ferretic/src/parse.ts:130`

```ts
function parseFerretic(source: string): Program
```

### tokenizeFerretic

*function* · `packages/ferretic/src/parse.ts:36`

```ts
function tokenize(src: string): Tok[]
```

## Interfaces

### FerreticBinding

*interface* · `packages/ferretic/src/bind.ts:25`

```ts
interface FerreticBinding
```

- `start(source: string, opts?: { name?: string; runs?: number; timeout?: number; keepRunning?: boolean; seed?: number }): { id: string; test: FerreticTest }` — start a test (any number may run at once); its `done` resolves with the report when it ends
- `get(id?: string): { id: string; test: FerreticTest } | null` — a test by id, or the last one started
- `dispose(): void`

### FerreticFailure

*interface* · `packages/ferretic/src/run.ts:53`

```ts
interface FerreticFailure
```

- `line: number`
- `code: string`
- `message: string`
- `time: number`

### FerreticHost

*interface* · `packages/ferretic/src/run.ts:22`

```ts
interface FerreticHost
```

What the interpreter needs from the game. Everything is by name: entity id or name, or a scene node's name.

- `time(): number` — the game's simulated time in seconds
- `fixedStep(): number` — the fixed step in seconds
- `actions(): string[]` — the game's input actions; press(action, null) lets go
- `press(action: string, v: { x?: number; y?: number; down?: boolean } | null): void`
- `actionValue(action: string): { down: boolean; x: number; y: number }` — what an action reads now (the device and the injected value together)
- `position(name: string): Vec | null` — world position / rotation (degrees, x y z) of an entity or a node; null when there is none
- `rotation(name: string): Vec | null`
- `exposed(name: string): unknown` — a value the game exposes (world.expose); throws when it does not
- `prop(entity: string, path: string): unknown` — a field of a component of an entity: path "Component.field.sub"
- `on(event: string, fn: (payload: unknown) => void): () => void` — listen to a game event; returns the function that stops listening
- `command(name: string, args: Record<string, unknown>): unknown | Promise<unknown>` — run an engine command
- `screenshot?(): Promise<string>` — a picture of what the player sees, as a data URL
- `restart?(level?: string): Promise<void>` — enter the level again (the current one when none is named)
- `getTimeScale(): number`
- `setTimeScale(s: number): void`
- `isPaused(): boolean`
- `setPaused(p: boolean): void`
- `probes?: ProbeSet` — the probes of the scene (view, count, ground, errors); a host without a scene has none

### FerreticProgram

*interface* · `packages/ferretic/src/parse.ts:127`

```ts
interface Program
```

- `body: Stmt[]`
- `subs: Map<string, Routine>`
- `runs: number | null`
- `source: string`

### FerreticReport

*interface* · `packages/ferretic/src/run.ts:54`

```ts
interface FerreticReport
```

- `ok: boolean`
- `status: 'running' | 'passed' | 'failed' | 'error' | 'timeout' | 'stopped'`
- `name?: string`
- `time: number` — game seconds from the start of the run, and fixed steps
- `frames: number`
- `realMs: number`
- `expectations: { passed: number; failed: number }`
- `failures: FerreticFailure[]`
- `error: { line: number; code: string; message: string } | null`
- `prints: { time: number; text: string }[]`
- `log: Record<string, Value>` — the last value LOGged under each name
- `logRows: { time: number; name: string; value: Value }[]`
- `samples: { columns: string[]; rows: Value[][]; total: number }`
- `shots: { name: string; time: number }[]`

### FerreticRunOptions

*interface* · `packages/ferretic/src/run.ts:132`

```ts
interface RunOptions
```

- `name?: string`
- `timeout?: number` — game seconds the run may take, when its author wants a limit (the same as TIMEOUT at its start); none by default
- `seed?: number`

### FerreticTestOptions

*interface* · `packages/ferretic/src/session.ts:12`

```ts
interface TestOptions extends RunOptions
```

- `runs?: number`

### FerreticTestReport

*interface* · `packages/ferretic/src/session.ts:6`

```ts
interface FerreticTestReport extends FerreticReport
```

- `runs?: { run: number; status: FerreticReport['status']; time: number; failures: number; log: Record<string, Value> }[]` — with RUNS n (or the option runs): each run's short result and, by LOG name, the last values of all runs
- `summary?: { runs: number; passed: number; failed: number; log: Record<string, { min: number; mean: number; max: number; values: Value[] }> }`

### GroundProbe

*interface* · `packages/ferretic/src/probes.ts:28`

```ts
interface GroundProbe
```

- `onGround: boolean` — the lowest point is within `tolerance` of what is under it (or sunk by no more than that)
- `height: number | null` — metres from the lowest point down to the first thing under it; negative when the point is inside it; null when nothing is above or below
- `over: string | null` — the name of that mesh

### ProbeSet

*interface* · `packages/ferretic/src/probes.ts:36`

```ts
interface ProbeSet
```

- `view(name: string): ViewProbe`
- `count(what: string): number`
- `ground(name: string, tolerance?: number): GroundProbe`
- `errors(): string[]`
- `dispose(): void`

### ViewProbe

*interface* · `packages/ferretic/src/probes.ts:14`

```ts
interface ViewProbe
```

- `inView: boolean` — inside the camera's frustum and at least one sample point of it is seen past everything else
- `inFrustum: boolean` — inside the frustum (whatever hides it)
- `share: number` — the share of the frame the thing's bounding box covers on the screen, 0..1 (a box that crosses the camera's plane is clipped to the frame)
- `visible: number` — the share of 9 sample points (centre and shrunk corners) that are in the frame and not hidden, 0..1
- `occludedBy: string | null` — what hides most of the sample points (a mesh name), or null
- `distance: number` — distance from the camera to the centre of the box

## Constants

### FERRETIC_FUNCTIONS

*const* · `packages/ferretic/src/run.ts:755`

```ts
const FERRETIC_FUNCTIONS: string[]
```

### FERRETIC_REFERENCE

*const* · `packages/ferretic/src/reference.ts:3`

```ts
const FERRETIC_REFERENCE: "FERRETIC: play the game by script. One statement a line (or several with \":\"), keywords in any case, ' starts a comment.\nThe program runs in the game's fixed step: it goes on until it WAITs, then game steps pass. Same game + same program = same presses at the same steps.\nNumbers may carry units: 0.3 s, 250 ms, 2 min, 90 deg, 1.5 rad (-> deg), 100 km/h (-> m/s), 5 m, 30 cm, 7000 rpm, 50 %.\n\n …
```

The language in one page: what the command test.help returns (the skill ferret-ferretic says the same at length).

