Send chunks to players connected to server
This commit is contained in:
@@ -59,7 +59,7 @@ A mod can have up to four scripts, each running in a different place:
|
||||
| `common` | the server and every client, before the others | yes | content: registering blocks, items, models and recipes, and changing other mods' content |
|
||||
| `server` | the server, in a worker | **never** | game logic: block components, ticking, tile data, containers, commands, events |
|
||||
| `client` | every client, main thread | yes | presentation: custom screens, HUD, keybinds |
|
||||
| `worldgen` | server and client chunk workers | yes | terrain and features, must be deterministic |
|
||||
| `worldgen` | the server | no | terrain and features, must be deterministic |
|
||||
|
||||
**The server is the authority.** Clients send what the player is trying to do ("break the block at x, y, z", "click slot
|
||||
3"). The server checks it, runs mod logic and sends back what happened. For responsiveness, clients show the expected
|
||||
@@ -72,9 +72,9 @@ result of breaking and placing immediately, and the server sends a correction if
|
||||
│ ┌───────────────────────────────────┐ │ ws │ + mod common and client │
|
||||
│ │ game server worker │◄├──────────┤► scripts │
|
||||
│ │ world, inventories, tile data │ │ protocol │ │
|
||||
│ │ + mod common and server scripts │ │ │ chunk workers │
|
||||
│ └───────────────────────────────────┘ │ │ + mod worldgen scripts │
|
||||
│ chunk workers + mod worldgen scripts │ └──────────────────────────────────┘
|
||||
│ │ + mod common and server scripts │ │ chunks │ chunk workers (meshing, light) │
|
||||
│ │ + mod worldgen scripts │ │ └──────────────────────────────────┘
|
||||
│ └───────────────────────────────────┘ │
|
||||
└───────────────────────────────────────┘
|
||||
```
|
||||
|
||||
@@ -87,8 +87,8 @@ server sends its order to clients, and they follow it.
|
||||
common script sees its own data and everything from the mods before it, and can change it.
|
||||
2. **Freeze.** The registries close: blocks get their numeric ids, block items are created and recipes are indexed.
|
||||
Registering or changing content after this throws.
|
||||
3. **Scripts.** `server` scripts' `setup` runs on the server, `client` scripts' on each client, and `worldgen` scripts in
|
||||
every chunk worker. These can read every registry but not change them.
|
||||
3. **Scripts.** `server` scripts' `setup` runs on the server, `client` scripts' on each client, and `worldgen` scripts
|
||||
where the server generates chunks. These can read every registry but not change them.
|
||||
4. The server starts ticking, or the client sends `ready` and joins.
|
||||
|
||||
**Content has to come out the same on every side.** Common scripts run on the server and on every client, and all of
|
||||
@@ -132,7 +132,7 @@ mods/
|
||||
common.ts # content, runs on both sides
|
||||
server.ts # game logic, stays on the server
|
||||
client.ts # sent to players
|
||||
worldgen.ts # sent to players, also runs on the server
|
||||
worldgen.ts # stays on the server, players get the chunks it makes
|
||||
shared/ # anything several scripts import, bundled into each
|
||||
```
|
||||
|
||||
@@ -175,13 +175,13 @@ Only `manifest.json` is required. Data folders can have subfolders, with one def
|
||||
| `scripts.common` | no | Content entry, run on the server and sent to every player. _Planned, phase 1._ |
|
||||
| `scripts.server` | no | Server entry. Never sent to clients. |
|
||||
| `scripts.client` | no | Client entry, sent to every player. |
|
||||
| `scripts.worldgen` | no | Worldgen entry, sent to every player and also run on the server. |
|
||||
| `scripts.worldgen` | no | Worldgen entry, run on the server. Never sent to clients, they get chunks. |
|
||||
| `engine_access` | no | `true` lets the mod's scripts import `bworld/engine`, see [Engine access](#engine-access). _Planned, phase 3._ |
|
||||
| `credits` | no | A markdown file in the mod, shown on the Credits screen. For asset licenses and thanks. |
|
||||
|
||||
Scripts can be `.js` or `.ts`. The build bundles each entry separately into one ES module. Code imported by several
|
||||
entries is copied into each bundle, so **don't import secrets into shared code**. Anything the common, client or worldgen
|
||||
bundles import is visible to players.
|
||||
entries is copied into each bundle, so **don't import secrets into shared code**. Anything the common or client bundles
|
||||
import is visible to players.
|
||||
|
||||
## Identifiers
|
||||
|
||||
@@ -812,8 +812,8 @@ first minute of the night and dawn the last, so it's bright all day. `common/tim
|
||||
`is_day` and `daylight` for turning it into something readable. Client scripts can read it as `ctx.world.time`, and
|
||||
players can use `/time`, `/time set <ticks|day|noon|sunset|night|midnight>` and `/time add <ticks>`.
|
||||
|
||||
The server loads chunks around every player (its simulation distance) and generates them itself with the same generator
|
||||
as the clients. Blocks in unloaded chunks can't be read or changed, and don't tick.
|
||||
The server loads chunks around every player (its simulation distance) and generates them itself, then sends them to
|
||||
players whole (see [Chunks](#chunks)). Blocks in unloaded chunks can't be read or changed, and don't tick.
|
||||
|
||||
### Commands
|
||||
|
||||
@@ -1209,8 +1209,9 @@ ctx.net.send("copper_tools:reset_stats", {});
|
||||
|
||||
## World generation
|
||||
|
||||
Chunks are generated from the world seed by chunk workers on **both** the server and every client, and all of them must
|
||||
produce identical terrain. So worldgen scripts must be **deterministic**.
|
||||
Chunks are generated from the world seed on the server only, and players get the finished chunks (see
|
||||
[Chunks](#chunks)). Saves only keep what players changed on top of generated terrain, so a chunk has to come out the
|
||||
same every time it's generated: worldgen scripts must be **deterministic**.
|
||||
|
||||
Each chunk is generated in three passes:
|
||||
|
||||
@@ -1417,9 +1418,8 @@ function hook<T, K extends keyof T>(target: T, method: K, handlers: {
|
||||
- JavaScript's `#private` members can't be hooked or read. The engine keeps things that mods should be able to change
|
||||
as normal members, and `#private` for things with rules that must hold (like a container's slots). Making something
|
||||
hookable is an ordinary engine change, and asking for one is how the Game API grows.
|
||||
- Hooks reach one JavaScript realm: the game server worker for server scripts, the page for client scripts, and one
|
||||
chunk worker for worldgen scripts. Each chunk worker runs its own copy of the engine, so a hook in a client script
|
||||
doesn't change meshing; a worldgen script with engine access can.
|
||||
- Hooks reach one JavaScript realm: the game server worker for server and worldgen scripts, and the page for client
|
||||
scripts. Each chunk worker runs its own copy of the engine, so a hook in a client script doesn't change meshing.
|
||||
|
||||
### Rules
|
||||
|
||||
@@ -1467,8 +1467,8 @@ textures, the ids it uses from other mods, and that its dependencies are there.
|
||||
from starting, saying which mod and what's wrong, so the game never runs with some mods missing. Then it sorts them so
|
||||
dependencies load first.
|
||||
|
||||
Players get each mod as a `.bmod` too, **without its server script**: the server makes a copy without
|
||||
`scripts/server.js` and serves it at `/mods/<sha256>.bmod`, named by its hash so it can be cached forever. The code is in
|
||||
Players get each mod as a `.bmod` too, **without its server and worldgen scripts**: the server makes a copy without
|
||||
`scripts/server.js` and `scripts/worldgen.js` and serves it at `/mods/<sha256>.bmod`, named by its hash so it can be cached forever. The code is in
|
||||
`common/bmod.ts` (reading and writing), `tools/pack_mod.ts` and `server/load_bmods.ts`.
|
||||
|
||||
The engine's own files (the client, its UI sprites and font, and the `engine:` textures) aren't in any mod. They're in
|
||||
@@ -1480,7 +1480,7 @@ The engine's own files (the client, its UI sprites and font, and the `engine:` t
|
||||
client server
|
||||
│ hello { name, protocol } │
|
||||
├────────────────────────────────────────►│ a different protocol gets rejected { reason }
|
||||
│ welcome { protocol, seed, mods[] } │ mods[i] = { id, name, version, sha256, file, size }
|
||||
│ welcome { protocol, mods[] } │ mods[i] = { id, name, version, sha256, file, size }
|
||||
│◄────────────────────────────────────────┤ file is the .bmod's path on the server
|
||||
│ │
|
||||
│ [cross-origin: confirm screen] │
|
||||
@@ -1489,10 +1489,12 @@ client server
|
||||
│ content in the listed order, run │
|
||||
│ client setup(), start chunk workers │
|
||||
│ │
|
||||
│ ready { registry_hash } │ a different hash gets rejected { reason }
|
||||
│ ready { view_distance, registry_hash } │ a different hash gets rejected { reason }
|
||||
├────────────────────────────────────────►│
|
||||
│ join { id, name, players, entities, │ the player is created here, player_join fires,
|
||||
│ changes, spawn, selected_slot } │ and their inventory follows as container messages
|
||||
│ blocks, spawn, selected_slot } │ and their inventory follows as container messages
|
||||
│◄────────────────────────────────────────┤
|
||||
│ set_center_chunk, then chunk batches │ see Chunks
|
||||
│◄────────────────────────────────────────┤
|
||||
```
|
||||
|
||||
@@ -1513,6 +1515,29 @@ client server
|
||||
from other origins.
|
||||
- Leaving a server reloads the page, so one server's mod code never stays loaded while playing on another.
|
||||
|
||||
### Chunks
|
||||
|
||||
The server sends the world the way Minecraft Java does since 1.20.2. Clients never generate terrain, so worldgen
|
||||
scripts stay on the server and clients can't be handed a world that differs from the server's. The code is in
|
||||
`common/chunk_streaming.ts`, `server/game/player_chunks.ts` and `client/level/client_level.ts`.
|
||||
|
||||
- **Whole chunks.** A `level_chunk` is a binary websocket message holding every block of a 16×256×16 chunk and its
|
||||
columns' biomes, deflated. Blocks are in 16 sections of 16×16×16, each a paletted container like Minecraft's: one
|
||||
value when the section is all one block, a palette and 1, 2, 4 or 8 bits per block up to 256 kinds, and whole values
|
||||
past that. Light isn't sent, clients light chunks themselves when they mesh them.
|
||||
- **Numeric ids.** Chunks use the server's numeric block ids, and `join.blocks` lists the string id for each, like
|
||||
Minecraft's registry sync. Clients turn them into their own numbers as a chunk loads.
|
||||
- **The view.** Players send their view distance with `ready` and `view_distance` when it changes. The server uses up
|
||||
to 10 chunks, and sends chunks in a round area around the player plus the ring just past it, which clients need to
|
||||
mesh and light the edge of what they show (Minecraft's `ChunkTrackingView`). When a player moves into another chunk
|
||||
the server sends `set_center_chunk`, and `forget_level_chunk` for each chunk that left the view.
|
||||
- **Batches.** Chunks go out nearest first, between `chunk_batch_start` and `chunk_batch_finished { size }`. The client
|
||||
times how long it took to load them and answers `chunk_batch_received { chunks_per_tick }`: as many chunks as fit in
|
||||
7 ms a tick. Until the first answer there's one batch of 9 in flight, after it up to 10. The server also stops
|
||||
building chunks for a tick after 20 ms, so players loading far away don't slow the game down.
|
||||
- **Changes.** `set_block` only goes to players that have the chunk. Everyone else gets the change with the chunk when
|
||||
they come close.
|
||||
|
||||
## Security
|
||||
|
||||
**Client scripts.** Mod client code runs in the game page with the page's full access. How much that matters depends on
|
||||
@@ -1781,7 +1806,7 @@ any order after them.
|
||||
- Move the ore table to `mods/bworld/worldgen/ores.json`, and take the content out of `common/generation.ts`, leaving
|
||||
the passes and noise helpers.
|
||||
- The server refuses to start without a terrain generator, naming the mods that could provide one.
|
||||
- Check: `terrain.json` matches exactly, client and server terrain still agree, and old saves load unchanged.
|
||||
- Check: `terrain.json` matches exactly, and old saves load unchanged.
|
||||
|
||||
**Done when:**
|
||||
|
||||
@@ -1794,9 +1819,6 @@ any order after them.
|
||||
|
||||
## Open questions
|
||||
|
||||
- **Chunk delivery.** Clients generate terrain from the seed, which needs deterministic worldgen and trusts clients to
|
||||
run the same generator. Sending chunks from the server instead (like Minecraft) removes both problems but costs
|
||||
bandwidth, roughly 5–15 KB per chunk compressed. It could be worth it once the server generates terrain anyway.
|
||||
- **Client-only mods.** Should players be able to install their own client mods (minimaps, UI tweaks, shaders) that work
|
||||
on any server, like Fabric's client mods? They'd need a separate `"side": "client"` kind of mod that can't add content
|
||||
or change the registry hash, and servers might want to forbid them.
|
||||
|
||||
Reference in New Issue
Block a user