FERRETGame engine over Babylon.js
Ferret

@ferret/dataflow

The game language: a scene's logic as streams, kinds of entities, motion programs and TypeScript externs; its check, interpreter, scene forms, live change and the game.* commands.

48 exports · source in packages/dataflow/src

Classes

FlowError class

class FlowError extends Error
constructor(message: string, readonly pos?: Pos, readonly form?: string)

packages/dataflow/src/check.ts:12

Machine class

class Machine
time: number
dt: number
stepNo: number
readonly rt: MotionRuntime
readonly mod: Module
readonly state: GraphState
readonly entities: Map<number, Entity>
readonly removed: Set<string>the origins of level entities that the game removed while running: a changed file does not bring them back
previous: Machine | undefined
readonly stats: Map<string, GraphStats>statistics by graph: game and kind:<name>
readonly externStats: Map<string, GraphStats>extern calls by name
onStep: Array<(m: Machine) => void>called after every step (the trace of game.trace reads the tokens here)
constructor(readonly game: Game, readonly host: FlowHost = {}, opts: MachineOptions = {})
dispose()
byOrigin(origin: string): Entity | undefinedthe live entity a level made under this name
carry(old: Entity, declared: Record<string, unknown> = old.declared, origin: string | undefined = old.origin): Entitytake an entity over from the replaced machine: the same id; a field keeps its running value unless what the file says it starts as has changed
commit(): { carried: number; made: number; dropped: number }the new file is accepted: the entities made while running come over, the replaced machine's other programs stop and it is done with
abort()the new file is refused: what this machine started is stopped, the replaced machine goes on untouched
despawn(id: number): booleanremove an entity: its programs stop; one a level made stays removed when the file changes
deliver(event: string, payload: unknown = true)a world event reaches the game: the streams (event "name") have it at the next step (the last payload of the step if several came)
stats_(name: string): GraphStats
spawn(kind: string, fields: Record<string, unknown> = {}, from?: { origin: string }): Entitymake an entity of a kind with its fields (the defaults of the kind for the rest)
alive(kind?: string): Entity[]
step(dt: number): void
kind(name: string): KindDef | undefined
tokens(entity?: number): unknown[]the tokens of the last step: game, or the entity's graph

packages/dataflow/src/interp.ts:96

Functions

bindExterns function

function bindExterns(rt: MotionRuntime, resolve: (name: string) => ExternFn | undefined): void

how the words of a runtime find their functions (the game sets it; the lookup is made at each call, so a function may come later)

packages/dataflow/src/externs.ts:29

compile function

function compile(source: string | Form[], opts: CompileOptions = {}): Game

Read and check a file of the game language: the text (or the forms of the JSON form) -> the game (graph, kinds, externs, scene forms). Throws with file:line:col.

packages/dataflow/src/check.ts:598

dataToForm function

function dataToForm(v: unknown): Form

packages/dataflow/src/scene.ts:39

declareExternWord function

function declareExternWord(rt: MotionRuntime, d: Pick<ExternDecl, 'name' | 'kind' | 'from'>): void

make the declared function a word of the motion runtime (again, when its kind has changed); does nothing for a name that is already another word

packages/dataflow/src/externs.ts:32

emptyGraph function

function emptyGraph: () => Graph

packages/dataflow/src/graph.ts:66

externFiles function

function externFiles(text: string): string[]

the files the externs of a game language text name (:from), in order and without repeats; comments and strings are passed over as the reader does

packages/dataflow/src/extern-files.ts:11

externModulePath function

function externModulePath: (from: string) => string

where the module built from the file from (a path from the project's root) is found in a built game, a package or a model

packages/dataflow/src/extern-files.ts:8

formatForm function

function formatForm(f: Form, indent = 0): string

the text of a form: a form that does not fit in 110 columns puts its :key value pairs and its sub-forms on lines of their own

packages/dataflow/src/scene.ts:212

formToData function

function formToData(f: Form, at?: Pos): unknown

packages/dataflow/src/scene.ts:50

formToGame function

function formToGame(f: Form): GameData

(game "name" :start "level" :levels (list "a" "b") (level ...) ...): the levels named in :levels first, then the inline ones

packages/dataflow/src/scene.ts:136

formToLevel function

function formToLevel(f: Form): Record<string, unknown>

packages/dataflow/src/scene.ts:103

formToManifest function

function formToManifest(f: Form): unknown

packages/dataflow/src/scene.ts:126

gameToForm function

function gameToForm(g: GameData): Form

packages/dataflow/src/scene.ts:153

installGame function

function installGame(world: World, options: GameOptions = {}): GameBinding

packages/dataflow/src/commands.ts:88

isLangError function

function isLangError: (e: unknown) => e is FlowError | MotionError

packages/dataflow/src/check.ts:18

isLevelManifest function

function isLevelManifest: (m: unknown) => m is Record<string, unknown>

packages/dataflow/src/scene.ts:80

levelToForm function

function levelToForm(m: Record<string, unknown>): Form

packages/dataflow/src/scene.ts:82

manifestToForm function

function manifestToForm(m: unknown): Form

a manifest as forms: a level's by level, a package's by manifest

packages/dataflow/src/scene.ts:122

plain function

function plain: (x: unknown) => unknown

a token as plain data (absent is null, a symbol its name) for reports

packages/dataflow/src/interp.ts:374

registeredExternModule function

function registeredExternModule: (from: string) => Record<string, unknown> | undefined

the registered module of the file an extern names, if the code holds it

packages/dataflow/src/externs.ts:24

registerExternModule function

function registerExternModule(path: string, module: Record<string, unknown>): void

the module of an extern file that is part of the code itself: what a builder adds to the game

packages/dataflow/src/externs.ts:22

sceneOf function

function sceneOf(game: Game): { game: GameData | null; levels: Record<string, unknown>[]; entities: Record<string, unknown>[]; links: Record<string, unknown>[] }

the scene forms of a compiled file: its game, its levels, the entities and links written outside a level

packages/dataflow/src/scene.ts:164

spawnLevelEntities function

function spawnLevelEntities(machine: Machine, level: Record<string, unknown>): number[]

make the entities of a level that name a kind of the machine's file: :kind the kind, :fields its fields, :position the field position. In a machine that replaces another, an entity of the same level and id comes over with its running state, and one the game removed stays removed

packages/dataflow/src/scene.ts:178

Interfaces

Clock interface

interface Clock

a clock: the base (every step), or a stream's subset of steps (when), or the steps an event came; interned, so equal clocks are the same object

id: number
parent: Clock | null
cond: number | null
neg: boolean
name: string

packages/dataflow/src/graph.ts:13

CompileOptions interface

interface CompileOptions
file?: string
motion?: MotionRuntime
externs?: ExternDecl[]functions the host gives the files without an extern of theirs (the functions of the models of a scene)

packages/dataflow/src/check.ts:97

Entity interface

interface Entity
id: number
kind: string
fields: Record<string, unknown>
state: GraphState
alive: boolean
origin?: stringfor an entity a level made: the level's name and the entity's id, by which it is found again when the file changes
declared: Record<string, unknown>the field values the level gave it (over the kind's initial ones)

packages/dataflow/src/interp.ts:30

ExternDecl interface

interface ExternDecl
name: string
from: string
kind: ExternKind | null
args: T[] | null
returns: T | null
pos?: Pos

packages/dataflow/src/graph.ts:44

FieldDef interface

interface FieldDef
name: string
init: unknown
type: T

packages/dataflow/src/graph.ts:41

FlowHost interface

interface FlowHost
sense?(name: string, args: unknown[]): unknowna sensor of the world (position, speed, pressed ...); the machine itself answers time, dt and step
effect?(name: string, args: unknown[]): voidan effector of the world, applied at the end of the step (command, animate, play-sound, move, force, motor, effect)
emit?(event: string, payload: unknown): void(emit "name" x): a world event
externs?: Record<string, (...args: any[]) => unknown>the TS/JS functions of externs, by name
error?(e: { form: string; kind: string | null; entity: number | null; message: string }): voida run-time error: the form, the entity (or null), the message
motionEvent?(event: string, payload: unknown, time: number): voidevery event the motion programs emit, with the program clock

packages/dataflow/src/interp.ts:15

Game interface

interface Game

a scene form of the file: game, level, entity, link as the tree they were written (scene.ts reads them)

file?: string
graph: Graph
kinds: Map<string, KindDef>
externs: Map<string, ExternDecl>
motions: Form[]defmotion and defn forms for the motion runtime (the motion subset of the language)
scene: Form[]the scene forms (game, level, entity, link)
clocks: Clock[]
names: Map<string, number>the top-level named streams of the game, by name
flows: string[]the defflow/defn names (they are inlined at every call)

packages/dataflow/src/graph.ts:47

GameBinding interface

interface GameBinding
readonly sources: Map<string, string>
machine(): Machine | null
load(files: Record<string, string>): Game
prepare(files: Record<string, string>): Promise<void>load the modules that the files' externs name and nobody has given: awaited before load (the commands do it by themselves)
provide(functions: Record<string, ExternFn>, declarations?: Array<Pick<ExternDecl, 'name' | 'kind'> & Partial<Pick<ExternDecl, 'args' | 'returns'>>>): () => voidGive functions to the game from outside its files (a model's code); returns what takes them back. A function that is declared (declarations) is a word of the files loaded after this, with no extern of theirs; without a declaration it only answers an extern that is declared in a file.
running(): boolean
dispose(): void

packages/dataflow/src/commands.ts:40

GameData interface

interface GameData
name?: string
start?: string
levels: unknown[]
[k: string]: unknown

packages/dataflow/src/scene.ts:134

GameOptions interface

interface GameOptions
externs?: Record<string, (...args: any[]) => unknown>the TS/JS functions of the file's externs
effects?: Record<string, (...args: unknown[]) => void>effectors of the game by name (over the built-in command and emit)
sensors?: Record<string, (...args: unknown[]) => unknown>sensors of the game by name (over the built-in ones)
motion?: MotionRuntimea motion runtime with the host's own words (the forms of models, of the physics): the game's motion programs run on it, and the running game ticks it
modules?: (from: string) => Promise<Record<string, unknown>>how the module of a file an extern names is loaded when the code does not hold it itself (the builders put it in and register it). Default: externModulePath(from) beside the page

packages/dataflow/src/commands.ts:28

Graph interface

interface Graph
nodes: Node[]
sinks: number[]the nodes that act on something (emit, effect, set-field, send, spawn, despawn)
events: string[]the world events the graph reads, by name

packages/dataflow/src/graph.ts:33

GraphState interface

interface GraphState
vals: unknown[]
pre: Map<number, unknown>
ticks: Map<number, number>
programs: Map<number, Program>
dead: Set<number>

packages/dataflow/src/interp.ts:37

GraphStats interface

interface GraphStats
calls: number
ms: number

packages/dataflow/src/interp.ts:94

KindDef interface

interface KindDef
name: string
fields: FieldDef[]
graph: Graph
pos?: Pos

packages/dataflow/src/graph.ts:42

MachineOptions interface

interface MachineOptions
motion?: MotionRuntime
previous?: Machinethe machine this one replaces (a changed file): its time, its mail and its state are carried over where the names match. Nothing of it is touched until commit(); abort() leaves it as it was

packages/dataflow/src/interp.ts:78

Node interface

interface Node
id: number
op: stringconst | sensor | op | pre | when | merge | tick | event | extern | motion | field | self | collect | query | nearest | of | gref | fwd | sinks: emit | effect | set-field | send | spawn | despawn
args: number[]
attr?: anythe name of the operation or sensor, the constant, the field, the event ...
pos?: Pos
type: T
clock: Clock | nullnull: a constant, which takes the clock of what it is combined with
text: stringhow the node reads in messages: the form it came from
key?: stringfor a node that keeps state (pre, tick, motion): its name by where it stands (the def, statement or flow it is in), so that a changed file keeps its state

packages/dataflow/src/graph.ts:15

Types

ExternFn type

type ExternFn = (...args: any[]) => unknown

packages/dataflow/src/externs.ts:18

ExternKind type

type ExternKind = 'pure' | 'world' | 'lasting' | 'action'

packages/dataflow/src/graph.ts:43

T type

type T = 'num' | 'bool' | 'vec3' | 'str' | 'entity' | 'list' | 'any'

packages/dataflow/src/graph.ts:10

Token type

type Token = unknown

packages/dataflow/src/graph.ts:64

Constants

ABSENT const

const ABSENT: typeof ABSENT

packages/dataflow/src/graph.ts:63

MOTION_HEADS const

const MOTION_HEADS: Set<string>

the forms the motion language runs itself: a program of these in a kind or at the top is one node of the graph

packages/dataflow/src/check.ts:25

PURE_OPS const

const PURE_OPS: string[]

packages/dataflow/src/check.ts:80