# 86. One server owns the scene

> **Status: partly implemented.** The Everglade room server, oa-room
> ([coax/web/room/](../../coax/web/room/room.coax)), steps every room's
> world headless at 30 Hz and starts from every declared default, checked by
> `bin/room --start-check` (#12774, commit c3e170d689). The same tick also
> runs in a Web Worker as the default local host (chapter 95, #12882);
> `?offline=worker` keeps it local. Shared profiles and session contracts
> live in `metaverse-engine/net/` and `metaverse-engine/session/`; command and replication
> extraction remains work (#12845).

Part XII builds a multiplayer engine from an empty server to the full
Everglade room. Each chapter adds one capability. This chapter adds the
first: one process that owns the shared scene. Every later chapter assumes
it.

A multiplayer game needs one place where gameplay is decided. If two
programs each decide where a creature stands, they disagree as soon as their
inputs arrive in a different order. The server runs the simulation; a client
sends what its player asks for and draws what the server says. The research
behind this part
([docs/research/mmo.md](../../docs/research/mmo.md), sections 5 and 17)
lists a headless runtime as the first foundation and "one authoritative
scene" as the first step of its development order.

## What this chapter teaches

- A server that runs the simulation without drawing anything.
- A fixed tick that owns time on the server, as chapter 1's clock owns it
  on the page.
- State that starts from declared values, never from whatever memory held.
- A check that fails the build when one field starts wrong.

## The design

The server is a loop over rooms. Each room has seats, a world and a tick
counter. One tick does four things in a fixed order:

1. Step each seat's queued inputs through the movement step (chapter 87).
2. Step the rules: casts that strike now, effects, creatures and the town.
3. Write each seat's state frame (chapters 91 and 92).
4. Send every socket's frames once.

The server never draws. It loads the collision and rules data it needs to
decide play, and none of the textures, meshes or particles that only a
client draws.

The tick rate is a declared constant. Everglade runs at 30 Hz: a step is
33.3 ms, so a command that arrives at a random time waits about 16.7 ms on
average for the next tick (research doc, section 6). If a tick takes longer
than its 33.3 ms, inputs queue behind it, and no network is fast enough to
hide that; chapter 99 measures it.

### State starts from its declarations

Coax gives every struct field a default (chapter 3). The room server is a
C host that calls Coax code, and C memory starts as whatever `malloc` or
`calloc` returned. A field whose default is 2 reads as 0 in zeroed memory.
That is how a server with a level cap of 2 sent `CAP 0` and an empty kit,
which the page drew as the full high-level hotbar (chapter 100's
counterexample).

So the host does two things before the first tick:

1. It writes every field's declared default with the compiler's generated
   `coax_default_t_RmServer`.
2. It calls `rm_init`, which sets only what a declaration can't say.

<!-- source: coax/web/room/room.coax server-start -->
```jai
// The server at its start. The host first gives every field its declared default
// (coax_default_t_RmServer, room-host.c server_new: the C host's memory is otherwise zeros, and a
// default such as the level cap's 2 would never hold), then calls this once. What it sets is what a
// declared default can't say; rm_start_check holds the started server to the declarations (#12774).
rm_init :: (v: [] RmServer) #export_c #effects(all) {
  for i: 0..<CONNS { v[0].conns[i] = RmConn.{}; }
  for r: 0..<RELAYS { v[0].relays[r].live = false; }
  for k: 0..<EG_ROOMS { v[0].eg[k].live = false; v[0].eg[k].relay = -1; }
  eg_seats_bind(v);   // each room's seats: its own, until a zone hands room 0 more (eg_zone_use)
  v[0].ob_n = 0; v[0].ob_at = 0; v[0].q_n = 0; v[0].rto_conn = 0;
  rm_games_init(v);
  e2zoo_shipped(v);
  v[0].dice_pool[0] = RsDice.{};
}
```

`rm_start_check` compares the started server with fresh declared values,
field by field. The host's `--start-check` also starts a second server in
memory filled with `0xa5` bytes and compares the two byte for byte, so a
field that depends on prior memory fails.

## Run it

Build the server and run the start check. The build script runs the same
check and fails when it does.

```shell
coax/web/room/build.sh
coax/web/room/bin/room --start-check
```

```text
room: start check: 0 fields off their defaults, the same from any memory
```

The exit status is 0. Remove the `coax_default_t_RmServer` call from
`server_new` in `room-host.c` and the check reports how many fields are off
their defaults and exits 1.

### Runnable native multiplayer contract example

[`metaverse-engine/examples/part12-server-scene.coax`](../examples/part12-server-scene.coax)
exercises the shared `#authority` multiplayer contracts in
[`metaverse-engine/net/session.coax`](../net/session.coax),
[`metaverse-engine/net/command.coax`](../net/command.coax), and
[`metaverse-engine/net/replication.coax`](../net/replication.coax) natively in
debug and release without opening a socket: version/readiness admission,
uninitialized-world refusal (`CP_NO_CAPACITY`), rules mismatch refusal
(`CP_RULES`), seat generation fencing, the 15 s reconnect hold boundary,
30 Hz earned simulation credit capped at `30` (`per_tick = 4`), and
acknowledged-baseline replication with owed removal retention
(committed transcript
[`metaverse-engine/fixtures/part12-server-scene.out`](../fixtures/part12-server-scene.out)):

<!-- source: metaverse-engine/examples/part12-server-scene.coax part12-server-scene -->
```jai
part12_run :: () -> bool {
  // 1. Server admission: matching pins + initialized world admits; uninitialized or wrong rules refuses.
  pins := CompatPins.{engine = 1, rules = 2, content = 3, wire = 5, schema = 2};
  server := CompatServer.{pins = pins, wire_min = 5};
  admit_ok := net_session_admit(server, pins, true);
  admit_cold := net_session_admit(server, pins, false);
  bad := pins; bad.rules = 1;
  admit_rules := net_session_admit(server, bad, true);
  steps_live := net_world_steps(true, true, false);
  steps_cold := net_world_steps(false, true, false);
  gen_same := net_session_current(SeatGeneration.{value = 1}, SeatGeneration.{value = 1});
  gen_stale := net_session_current(SeatGeneration.{value = 1}, SeatGeneration.{value = 2});
  hold_in := net_session_holds(15000, 100, 15099);
  hold_out := net_session_holds(15000, 100, 15100);
  print("admit ok=% cold=% rules=% steps live=% cold=% gen same=% stale=% hold in=% out=%\n",
    admit_ok == CP_OK, admit_cold == CP_NO_CAPACITY, admit_rules == CP_RULES,
    steps_live, steps_cold, gen_same, gen_stale, hold_in, hold_out);

  // 2. Command ordering & 30 Hz earned simulation credit: duplicates do not advance, bank caps at 30.
  a1 := net_accepted_result(6, 7);
  a_dup := net_accepted_result(7, 7);
  prof := NetCreditProfile.{hz = 30, most = 30, per_tick = 4, excess = 4};
  c := net_credit_step(prof, NetCredit.{since = 0}, 500);     // 15 steps earned in 0.5 s
  c_cap := net_credit_step(prof, c, 2500);                    // stalled link capped at 30
  can_4th := net_credit_can_step(prof, c.bank, 3);
  can_5th := net_credit_can_step(prof, c.bank, 4);
  print("cmd adv=% dup=% credit_500ms=% credit_cap=% limited=% step4=% step5=%\n",
    a1.advanced, a_dup.advanced, c.bank, c_cap.bank, c_cap.limited, can_4th, can_5th);

  // 3. Replication lifecycle: replacement retains owed removals until acknowledged whole tick.
  r := NetReplica.{held = 8, base = 3, sent = (1 << 3), last = 3};
  r = net_replica_observe(r, 9, true, 4, 32);                 // entity 8 replaced by 9 -> gone=8 owed
  ready_before := net_replica_ready(r);
  r.gone_t = 4;
  ready_after := net_replica_ready(r);
  r = net_replica_sent(r, 9, 5, 32);
  r = net_replica_ack(r, 4, 32);                              // tick 4 ack clears owed removal of 8
  r = net_replica_ack(r, 5, 32);                              // tick 5 ack advances baseline to 5
  print("replica ready_before=% ready_after=% base=% gone=%\n",
    ready_before, ready_after, r.base, r.gone);

  return admit_ok == CP_OK && admit_cold == CP_NO_CAPACITY && admit_rules == CP_RULES &&
         steps_live && !steps_cold && gen_same && !gen_stale && hold_in && !hold_out &&
         a1.advanced && !a_dup.advanced && c.bank == 15 && c_cap.bank == 30 && c_cap.limited &&
         can_4th && !can_5th && !ready_before && ready_after && r.base == 5 && r.gone == 0;
}
```

```shell
coax/bin/coax run metaverse-engine/examples/part12-server-scene.coax
```

```text
admit ok=true cold=true rules=true steps live=true cold=false gen same=true stale=false hold in=true out=false
cmd adv=true dup=false credit_500ms=15 credit_cap=30 limited=true step4=true step5=false
replica ready_before=false ready_after=true base=5 gone=0
ok=true
```

## What exists today

- [coax/web/room/room-host.c](../../coax/web/room/room-host.c): the
  single-threaded host (TCP, WebSocket frames, the `poll` loop),
  `everglade_tick`, `server_new` and `start_check`.
- [coax/web/room/room-everglade.coax](../../coax/web/room/room-everglade.coax):
  the rooms' simulation (`rm_eg_begin`, `rm_eg_input`, `rm_eg_moved`,
  `rm_eg_end`), 16 rooms of 16 seats a process at `EG_HZ` 30.
- [coax/web/room/room-world.c](../../coax/web/room/room-world.c): each room's
  headless Everglade world, built from the page's own units with nothing
  drawn.

## What is left

- The shared tick lives in `coax/web/room/` and remains Everglade-specific.
  Network profiles and lifecycle policy are shared, but commands, accepted
  results and replication still need shared interfaces (#12845).

## Acceptance checks

- `bin/room --start-check` exits 0 on every build.
- A room steps the same inputs to the same state at any host frame rate
  (chapter 1's rule, on the server's tick).

## Dependencies

Chapters 1 (the fixed step), 3 (state from its defaults) and 6 (entity
identity).
