Skip to main content

The engine

@engine/core is an in-house, game-agnostic TypeScript engine: an ECS, a fixed-step deterministic sim loop, a WebGL2 renderer (2D and 3D), input, animation, a spatial index, audio, and WASM bindings. It is consumed by four games and knows about none of them.

The two arrows that do NOT exist are the point: nothing goes upward. Engine never imports game, and sim-core never imports the renderer — and a test enforces both — open full screen, where search, tracing and the guided views are usable.

The four-layer dependency rule​

Every arrow points downward. A layer may depend only on the layers beneath it — and the four games are siblings that never touch. This isn't a convention you hope holds: layering.test.ts classifies every workspace on disk and fails if the engine reaches into a game, or one game into another.

clients / servers
@farm/client · @farm/server · @citadel/client · @hollow/client · @mathquest/client
Renderer + transport. Never touched by sim logic.
game sim-cores
@farm/sim-core · @citadel/sim-core · @hollow/sim-core · @mathquest/sim-core
Deterministic sim. Node + browser safe. Never imports a renderer.
engine
@engine/core · @engine/ui
Generic ECS, sim loop, renderer, UI. Never imports a game.
kernels
@engine/wasm-modules
AssemblyScript: pathfinder · noise · rng · floodfill.
↑ imports only ever point downward  ·  no game ever imports another
note

Inside the monorepo, packages resolve raw TypeScript source via subpath exports — there's no build step for the engine. The same packages are also shippable as MIT libraries (v0.1.0) that resolve emitted dist/ for external consumers, via a prepack manifest swap. Read the full map in Architecture.

Determinism is the foundation​

20Hz
fixed sim tick
~100ms
render delay buffer
mulberry32
seeded RNG + forks
byte
identical replays

Every source of randomness flows through a seeded Rng (mulberry32) with named fork(label) derivation. No Math.random. No Date.now. A tick's output depends only on the tick count — the host's setInterval is wall-clock pacing, nothing more. The consequence is powerful and cheap: a run is fully described by its seed + params, round-tripped through the URL hash, and reproduces byte-for-byte. There is no snapshot-save or input-log model because there doesn't need to be one.

Fixed step, interpolated render

The sim ticks at a fixed 20 Hz (FixedStepClock). The renderer interpolates between the latest two snapshots with smoothstep(alpha), drawing ~2 ticks behind latest to absorb dropped frames — logic and framerate fully decoupled.

Interpolate position, snap frames

Animation is render-side and wall-clock-driven (zero determinism risk). The rule: never interpolate frames — interpolate position via alpha, snap the sprite frame. A semantic snapshot (moving/facing/action) lets render resolve the frame.

The sim↔render boundary​

The ECS world and scheduler always run off the render path, behind a snapshot stream. The transport differs per game — but bootstrapSim() stays transport-agnostic, so the same sim runs in a server, a Worker, or a bare test.

Farm Valley → server

Sim runs in a Node SimHost; the browser opens a WebSocket and receives one RenderSnapshot per tick as JSON. Drop-stale backpressure + permessage-deflate (~70–80% shrink) keep the wire honest.

Citadel, Hollow, MateQuest → Web Worker

Sim runs in an in-browser Web Worker, posting snapshots over postMessage. No SharedArrayBuffer, so no COOP/COEP headers required. (Citadel additionally keeps a server for online multiplayer; solo needs none.)

Headless → main thread

The run-sim tools and every test drive the scheduler directly — no server, no Worker. This is the canonical way to exercise sim behavior.

Under the hood​

In-house ECS

Replaced miniplex while keeping spawn/query/despawn. Queries are cached by component-key set; for…of borrows a pooled scratch buffer, so steady-state iteration allocates zero arrays and despawn-mid-loop is safe.

Message bus (FIPA-ACL)

Generic pub/sub with a buffer-swap (inflight↔deliverable, no realloc). Messages carry performative + ontology + body — mirroring the Python SPADE prototype the gameplay descends from.

Scheduler in 10 bands

Systems register in a deliberate order that encodes real data dependencies — e.g. an event feed must snoop the inbox before PerceiveSystem clears it. A clear placed too early is a correctness bug, not a perf one.

WASM kernels

AssemblyScript, artifacts committed: pathfinder (A*), noise (~8× faster than JS), rng, floodfill. A pure-JS pathfinder is a tested fallback — but the two aren't route-equivalent, so the server uses WASM to preserve what players see.

Palette discipline

A fixed palette is enforced per game, with no raw hex anywhere — every color is a named role constant. Engine + Farm use EDG32; Citadel and Hollow use Apollo-46 under the same role keys; MateQuest uses Resurrect-64. The guard test is scoped by path and reads HTML and CSS too, not just TS — so each game is validated against its own palette. See Decisions.