# 64. Every realm runs on the Metaverse Engine

> **Status: partly implemented.** Engine-core task: `ec-migrate` (#12609,
> #12650). Every realm with fixed steps steps them on
> [metaverse-engine/core/loop.coax](../core/loop.coax), and every walking realm walks
> on [metaverse-engine/move/walk.coax](../move/walk.coax) with its own declared
> collision profile. Dead Gears' entities are
> [metaverse-engine/core/entity.coax](../core/entity.coax)'s, and Ice Cream Man's
> camera is [metaverse-engine/cam/follow.coax](../cam/follow.coax), stepped with its
> rules. The engine modules moved from `metaverse/` to `metaverse-engine/`.
> [metaverse-engine/tools/realm-inventory.coax](../tools/realm-inventory.coax) reads
> each realm engine's `#load` closure and fails a realm that no longer loads
> a shared module it is recorded to run on. The cross-realm checks hold the
> camera, the feet and replay to one rule in all seven walking realms. The
> example below runs natively in debug and release. Made realms walk on the
> clock and the shared walker's push (#12721). Not done: the space games and Hold the
> Stair have their own cameras and input, iOS's Metal host allocates on its
> own, and no recorded session per realm replays in the browser yet. See
> [Still to do](#still-to-do).

The book ends where the engine-core milestone does: the Grid, made realms,
Everglade, Dead Gears, Ice Cream Man, Hecate's Cemetery, Fairy Forest and the
space games all run on the OpenAgents Metaverse Engine's shared loop, spaces, input, queries and
camera, and their private copies are deleted. A realm is then its content,
its rules and its look, and nothing else.

## What this chapter teaches

- How to migrate a running realm without changing what players see, except
  where a fix is visible.
- How to keep an inventory of what each realm runs on, generated from the
  code, and how a check keeps private copies from coming back.
- The order: foundations first, then the smallest realm, then the largest.

## The method

Migration is per realm and per subsystem. Each step is a commit with the
realm's own checks, its golden and the 60 FPS rule passing:

1. Find the realm's private loop, camera follow, input path or ground
   logic.
2. Replace it with the Metaverse Engine module (chapters 1, 2, 4, 6, 7, 9 and 10),
   keeping the realm's tuning as data.
3. Check that it behaves the same at every frame rate, step by step. Where
   the old code was frame-rate dependent, the difference is the fix, and it
   is named.
4. Delete the private copy.

The commits on `main` follow this method:

| Realm | What moved | Commit |
| --- | --- | --- |
| All | Player, camera, terrain, figure, sticks and 17 more modules from `metaverse/` to `metaverse-engine/`, with every `#load` and build line | 2df3c484bf |
| Belt 2200 | Its lockstep ticks onto a `FrameClock` of 50 ms steps, at most 6 a frame. A tick waiting on the partner stays owed (`loop_ran_some`) | 5c8606795a |
| Ice Cream Man | Keys and thumb to one `Intent` through `metaverse-engine/core/input.coax`, with one dead zone | 43e396014d |
| Fairy Forest | Flight from `fairy-face.c` to `metaverse-engine/move/flight.coax`, a per-step hook in the walker, so a glide is the same at 30 and 144 FPS | f2a8d11368 |
| Hecate's Cemetery | Its free orbit eases once a fixed step, not once a frame | 8fd86f5423 |
| Dead Gears | Its entity store replaced by `metaverse-engine/core/entity.coax`'s, with a despawn that cancels a waiting spawn. The fight is the same at every step at 30, 60, 120 and 144 FPS | a97d85dab4 |
| Ice Cream Man | Its private boom camera replaced by `metaverse-engine/cam/follow.coax`, stepped with the 1/30 s rules | 6796c59c32 |
| Everglade, Fairy Forest, Belt 2200 | River floats and lily pads on a `FrameClock`. Belt's tactical battle capped at 8 steps a frame | 8330d80b8b |
| All walkers | The cross-realm checks: camera, feet and replay in all seven walking profiles, and the Grid's floor lattice | b3c05f3c1d |

The renderer has its own path: every catalogue realm's WGSL is generated by
Coax, and chapter 11's shared `Render*` facade and GPU ledger
([registry.coax](../render/registry.coax)) draw the browser Grid and the
native Vulkan host (#12628).

## The inventory

A realm runs on a shared module when that module is in its engine's `#load`
closure. `coax deps FILE` prints the closure as JSON. The inventory tool
counts thirteen modules:

<!-- source: metaverse-engine/tools/realm-inventory.coax modules -->
```jai
// The shared modules counted, by their path under metaverse-engine/: one bit each, in this order.
INV_MODULES :: 13;
INV_NAMES :: [13] string.["loop", "input", "replay", "rand", "entity", "query", "ground", "walk",
  "player", "follow", "camera", "flight", "quality"];
```

Each realm lists the units its engine is built from, as
`apps/openagents-web/build.sh` builds them (the module, then the libraries
built beside it), and the modules it is recorded to run on:

<!-- source: metaverse-engine/tools/realm-inventory.coax record -->
```jai
// Each realm: its page, the units its engine is built from (the module and the libraries beside
// it, as apps/openagents-web/build.sh builds them; the Grid's as coax/web/project/build-home.sh
// does), and the shared modules it is recorded to run on.
InvRealm :: struct { slug: string; want: string; units: string; }
INV_REALMS :: [15] InvRealm.[
  InvRealm.{slug = "the-grid", want = "loop input replay rand query ground walk follow flight",
    units = "apps/openagents-web/newland.coax metaverse/gridwalk.coax"},
  InvRealm.{slug = "made realms", want = "loop input query ground walk follow flight",
    units = "apps/openagents-web/myrealm.coax metaverse/edit/computer-wire.coax coax/lib/crypto/entropy.coax"},
  InvRealm.{slug = "haunted-house", want = "loop input query ground walk player follow flight",
    units = "apps/openagents-web/realm.coax metaverse/haunt.coax metaverse/haunt-buffer.coax metaverse-engine/move/player.coax metaverse/grid.coax metaverse-engine/render/particles.coax metaverse-engine/cam/cinematic.coax metaverse/setpiece.coax"},
  InvRealm.{slug = "garden", want = "loop input query ground walk player follow camera flight",
    units = "apps/openagents-web/garden.coax metaverse-engine/move/player.coax metaverse/grid.coax"},
  InvRealm.{slug = "everglade", want = "loop input query ground walk player follow camera flight quality",
    units = "apps/openagents-web/everglade5e.coax everglade/shared/everglade-step.coax metaverse/grid.coax everglade/world/river.coax everglade/npc/guard-guide-policy.coax apps/openagents-web/old/v1/everglade5e-rules.coax"},
  InvRealm.{slug = "everglade-classic", want = "loop input query ground walk player follow camera flight quality",
    units = "apps/openagents-web/everglade.coax everglade/shared/everglade-step.coax metaverse/grid.coax everglade/world/river.coax everglade/npc/guard-guide-policy.coax"},
  InvRealm.{slug = "dead-gears", want = "loop input entity query ground walk follow flight",
    units = "apps/openagents-web/deadgears.coax"},
  InvRealm.{slug = "ice-cream-man", want = "loop input query ground follow",
    units = "apps/openagents-web/icecream.coax metaverse/icecream/scene.coax"},
  InvRealm.{slug = "hecates-cemetery", want = "loop input query ground walk player follow camera flight",
    units = "apps/openagents-web/villa.coax metaverse-engine/move/player.coax metaverse/grid.coax apps/openagents-web/villa/villafig.coax"},
  InvRealm.{slug = "fairy-forest", want = "loop input query ground walk player follow camera flight",
    units = "apps/openagents-web/fairy.coax metaverse-engine/move/player.coax metaverse/grid.coax metaverse/fairy-forest/stream.coax coax/lib/qr/qr-memory.coax"},
  InvRealm.{slug = "orbit-league", want = "loop",
    units = "apps/openagents-web/orbitleague.coax"},
  InvRealm.{slug = "belt-2200", want = "loop",
    units = "apps/openagents-web/belt.coax"},
  InvRealm.{slug = "hold-the-stair", want = "loop",
    units = "apps/openagents-web/stair.coax"},
  InvRealm.{slug = "watchtower", want = "loop",
    units = "apps/openagents-web/watchtower.coax"},
  InvRealm.{slug = "drop", want = "",
    units = "apps/openagents-web/droprealm.coax"}
];
```

The tool reads each unit's closure and compares it with the record. A realm
that no longer loads a recorded module fails. Either it went back to a
private copy, or it lost the shared one. A realm that loads more than its
record says so, so that the record can be raised:

<!-- source: metaverse-engine/tools/realm-inventory.coax masks -->
```jai
// Word k of a list of words split by single spaces ("loop input"), or "" past the last. The end
// of the list ends its last word as a space does.
inv_word :: (s: string, k: int) -> string {
  n := 0; from := 0;
  for i: 0..<s.count + 1 {
    if i == s.count || s[i] == ' ' {
      if n == k { return s[from..<i]; }
      n += 1; from = i + 1;
    }
  }
  return "";
}
// The modules named in a list of words; -1 when a word names none.
inv_words :: (s: string) -> int {
  mask := 0; k := 0;
  while true {
    w := inv_word(s, k);
    if w.count == 0 { return mask; }
    bit := 0;
    for m: 0..<INV_MODULES { if w == INV_NAMES[m] { bit = 1 << m; } }
    if bit == 0 { return -1; }
    mask |= bit; k += 1;
  }
  return mask;
}
// Where pattern first starts in text, or -1.
inv_find :: (text: string, pattern: string) -> int {
  for i: 0..<text.count - pattern.count + 1 { if text[i..<i + pattern.count] == pattern { return i; } }
  return -1;
}
// The modules in `coax deps`'s JSON: those named by a path in its "sources" array, as
// ".../metaverse-engine/NAME.coax"; -1 with no such array.
inv_sources :: (json: string) -> int {
  key := "\"sources\":[";
  at := inv_find(json, key);
  if at == -1 { return -1; }
  rest := json[at + key.count..<json.count];
  close := inv_find(rest, "]");
  list := ifx close == -1 then rest else rest[0..<close];
  mask := 0;
  for k: 0..<INV_MODULES {
    p: [64] u8; n := 0;
    parts := [3] string.["/metaverse-engine/", INV_PATHS[k], ".coax\""];
    for part: parts { for c: part { p[n] = c; n += 1; } }
    if inv_find(list, cast(string) p[0..<n]) != -1 { mask |= 1 << k; }
  }
  return mask;
}
// What a realm's record wants that it doesn't load (a private copy back, or the shared one lost),
// and what it loads past its record.
inv_lost :: (have: int, want: int) -> int { return want ^ (want & have); }
inv_gained :: (have: int, want: int) -> int { return have ^ (have & want); }
```

`coax/web/tool.sh metaverse-engine/tools/realm-inventory.coax`, on 2026-11-03 (about
2 minutes):

```text
ok   the-grid (2 units): loop input replay rand query ground walk follow flight
ok   made realms (3 units): input
ok   haunted-house (8 units): loop input query ground walk player follow flight
ok   garden (3 units): loop input query ground walk player follow camera flight
ok   everglade (6 units): loop input query ground walk player follow camera flight quality
ok   everglade-classic (5 units): loop input query ground walk player follow camera flight quality
ok   dead-gears (1 unit): loop input entity query ground walk follow flight
ok   ice-cream-man (2 units): loop input query ground follow
ok   hecates-cemetery (4 units): loop input query ground walk player follow camera flight
ok   fairy-forest (5 units): loop input query ground walk player follow camera flight
ok   orbit-league (1 unit): loop
ok   belt-2200 (1 unit): loop
ok   hold-the-stair (1 unit): loop
ok   watchtower (1 unit): loop
ok   drop (1 unit): none
realm-inventory: 15 of 15 realms keep their record
```

Since #12721 made realms' line reads
`ok   made realms (3 units): loop input query ground walk follow flight`.

Read across, the gaps are the remaining migration. Every walker loads
`flight.coax`, because the walker owns the per-step flight hook, and Fairy
Forest is the only realm whose profile turns it on. Made realms keep their
own controls (W and S walk, A and D turn) but, since #12721, `realm_step` in
`apps/openagents-web/myrealm.coax` runs them in `metaverse-engine/core/loop.coax`'s fixed
1/64 s steps (eight at most a frame) and draws you between the last two, and
`walk_to` is pushed out of what stands by `metaverse-engine/move/walk.coax`'s `move_across`.
`metaverse-engine/tests/cross-realm-made.coax` holds its feet and replay, and
`myrealm.coax` checks the same walk at 30, 60, 120 and 144 FPS. The
space games, Hold the Stair and Watchtower have the clock and nothing else.
Drop's realm has no fixed steps.

The tool counts what is loaded, not what is called. A realm could load the
walker and still keep its own step beside it. That is why each migration
above also has a step-by-step check at several frame rates: the inventory
keeps a module from going away, and the frame-rate checks show that the
module is what runs.

## One walker in every walking realm

The seven walking realms step the same `metaverse-engine/move/walk.coax`, each by the name
its collision profile is declared under. The example,
[metaverse-engine/examples/one-walk-every-realm.coax](../examples/one-walk-every-realm.coax),
plays one session in each, at 60 FPS and again at 30:

<!-- source: metaverse-engine/examples/one-walk-every-realm.coax example -->
```jai
#load "../move/walk.coax";
#import "Basic";

cm :: (m: f32) -> int { return cast(int) (m * 100 + ifx m < 0 then -0.5f32 else 0.5f32); }

// The seven walking realms, by the names their collision profiles are declared under
// (metaverse-engine/physics/collision-profiles.coax), each stepping the one walker, metaverse-engine/move/walk.coax, on the one
// clock, metaverse-engine/core/loop.coax. The same 300 frames in each, close to the session that
// metaverse-engine/tests/cross-realm.coax plays: walk, run from frame 44, jump at 60, run onto a 0.4 m step
// and off it, jump again at 200, walk, stop. Then the same at 30 FPS, which takes the same steps.
REALMS :: [7] string.["the-grid", "haunted-house", "garden", "fairy-forest", "everglade", "hecates-cemetery", "dead-gears"];

walk :: (realm: string, frames: int, frame: f32) -> Player {
  walls := [6] f32.[-2, 0, 9, 2, 0.4, 13];   // the step: min x, y, z, max x, y, z
  none: [1] f32;
  p := walk_place(0, 0, 0, 0);
  per := cast(int) (frame * 60 + 0.5);       // sixtieths of a second a frame
  for f: 0..<frames {
    t := f * per;                            // the frame's time in sixtieths
    p = walk_input(p, 0, ifx t < 260 then 1 else 0, 0, 0, t >= 44 && t < 230, t == 60 || t == 200, 0, 0);
    p = walk_advance_in_realm(p, frame, none[0..<0], walls[..], none[0..<0], realm);
  }
  return p;
}

main :: () {
  for realm: REALMS {
    p := walk(realm, 300, 1.0 / 60.0);
    q := walk(realm, 150, 1.0 / 30.0);
    same := p.body.clock.ticks == q.body.clock.ticks && p.body.pos.x == q.body.pos.x && p.body.pos.y == q.body.pos.y && p.body.pos.z == q.body.pos.z;
    print("%: % steps, % jumps, feet at (% cm, % cm, % cm), collision %; at 30 FPS the same: %\n", realm,
          p.body.clock.ticks, p.body.jumps.taken, cm(p.body.pos.x), cm(p.body.pos.y), cm(p.body.pos.z), walk_collision_status(p), same);
  }
}
```

```text
the-grid: 320 steps, 2 jumps, feet at (0 cm, 0 cm, 2759 cm), collision 0; at 30 FPS the same: true
haunted-house: 320 steps, 2 jumps, feet at (0 cm, 0 cm, 2759 cm), collision 0; at 30 FPS the same: true
garden: 320 steps, 2 jumps, feet at (0 cm, 0 cm, 2759 cm), collision 0; at 30 FPS the same: true
fairy-forest: 320 steps, 2 jumps, feet at (0 cm, 0 cm, 2759 cm), collision 0; at 30 FPS the same: true
everglade: 320 steps, 2 jumps, feet at (0 cm, 0 cm, 2759 cm), collision 0; at 30 FPS the same: true
hecates-cemetery: 320 steps, 2 jumps, feet at (0 cm, 0 cm, 2759 cm), collision 0; at 30 FPS the same: true
dead-gears: 320 steps, 2 jumps, feet at (0 cm, 0 cm, 2759 cm), collision 0; at 30 FPS the same: true
```

300 frames of 1/60 s are 5 s, and 5 s of 1/64 s steps are 320 steps. Every
realm's walk is the same: every declared profile is the same capsule
(0.3 m radius, 1.8 m tall, a 0.55 m step up), and this session has no
bodies and no presses in the air, which only Fairy Forest's flight would
take. In play, what differs is each realm's own bodies, walls and floors,
handed to the same step. At 30 FPS, 150 frames of 1/30 s
take the same 320 steps to the same feet, bit for bit, because the controls
change only on frames both rates share (frame 44, 60, 200, 230 and 260 in
sixtieths). [metaverse-engine/tests/cross-realm.coax](../tests/cross-realm.coax)
holds the camera, the drawn feet and replay to one rule in the same seven
profiles.

## Checks

- `coax/bin/coax test metaverse-engine/tools/realm-inventory.coax --library`: 12 of
  12 (the word lists, a deps record's sources, and what's lost or gained).
- `coax/bin/coax mutate metaverse-engine/tools/realm-inventory.coax --library
  --root-only`: 74 mutants, 74 killed.
- `coax/web/tool.sh metaverse-engine/tools/realm-inventory.coax`: 15 of 15 realms
  keep their record (shown above).
- Cited, not rerun here: `metaverse-engine/tests/cross-realm.coax` 272 of 272 and
  `cross-realm-floor.coax` 65 of 65 (b3c05f3c1d), and each migration
  commit's own checks.

## Still to do

| Realm | Remaining migration |
| --- | --- |
| The Grid | Other people as chapter 6 entities, and the Grid's content apart from the homepage's scene ([chapter 56](ch056-the-grid.md)). |
| Made realms | Camera modes, side-view collision and the drop realm's walk and follow. The fixed steps and the walker's push landed in #12721. |
| Everglade | Forest-specific spatial and body adapters, the edge hold, effect plumbing and a complete recorded session. The owner is changing it now (#12705–#12718), so it is left alone here. |
| Dead Gears | Rule and input adapters, and the arena's spatial representation. |
| Ice Cream Man | Its own action controls (sprint, fire, use, tools). Approving its new camera look waits for the owner (NEEDS_OWNER.md). |
| Hecate's Cemetery | The flight in from space handed over through the shared camera, and the remaining floor-relative adapters. |
| Fairy Forest | Its recorded-session integration. |
| Space games | Space-camera and world-frame adapters. Belt's network commits and game-time ticks need an explicit session integration. |
| Hold the Stair, Watchtower | Input and camera on the shared modules. |
| Anime Lab | Shared renderer adoption. Its existing renderer is tracked in `metaverse-engine/render/adoption.coax`. |
| iOS | `HomeRenderer.swift` allocates and submits on its own, outside the GPU ledger. |

Across every realm: a recorded session per realm that replays bit for bit
natively and in the browser (the Grid's does: `coax/web/tool.sh
replay-hosts grid`), and a check that calls, not only loads, the shared
step.

## Acceptance checks and oracles

From `ec-migrate`:

- No realm keeps a private loop, camera follow, input path or ground logic.
  Not yet: see the table above. The inventory keeps what has moved from
  moving back.
- Goldens change only where a fix is visible, and the 60 FPS checks hold.
  Each migration commit above ran its realm's goldens or frame-rate checks.
  `coax/web/realm-perf.coax` measures every realm
  ([chapter 83](ch083-inspect-and-profile-a-running-realm.md)).
- The Grid's floor is one consistent world-space grid from any position.
  Holds: `cross-realm-floor.coax`, from 121 positions.

From the chapters it depends on: each realm's own tests
(`metaverse/deadgears/test.sh`, `metaverse/icecream/test.sh` and the modules'
`#test`s), a recorded session per realm that replays bit for bit natively
and in the browser, and every realm declaring its budgets.

## Dependencies

Every earlier chapter, through the realms of chapters 56–63. Blocked by the
engine-core tasks `ec-loop`, `ec-space`, `ec-state`, `ec-input`,
`ec-replay`, `ec-query`, `ec-entity`, `ec-camera` and `ec-budget`.
