Change default view distance to 16 and add /viewdistance command

This commit is contained in:
2026-10-02 19:01:08 -03:00
parent a4fec0681f
commit 3202c18ab5
7 changed files with 56 additions and 12 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ import type { KeyCode } from "./input_manager.ts";
// the player's settings, like minecraft's Options and its key mappings
export class Options {
render_distance = 6;
render_distance = 16;
key_forward: KeyCode = "KeyW";
key_back: KeyCode = "KeyS";
+1
View File
@@ -117,5 +117,6 @@ Commands can only be registered during `setup`.
| `/tp <x> <y> <z>` | Teleports you there |
| `/fly` | Toggles flying: jump goes up, sneak goes down, sprint goes faster |
| `/tps` | Shows how the game loop keeps up |
| `/viewdistance [chunks]` | Shows or changes how far the server sends chunks, until it restarts |
| `/skin [url] [slim\|normal]` | Changes your skin to a 64×64 PNG, or back to the default |
| `/help [command]` | Every command's usage, or one's |
+1 -1
View File
@@ -48,7 +48,7 @@ stay on the server.
- Chunks are 16×256×16 and sent whole, compressed, in batches, nearest first. The server paces batches by how fast each
client says it loads them.
- Players see chunks up to their view distance, at most 10 on the server's side. Light isn't sent: clients light chunks
- Players see chunks up to their view distance, at most 16 on the server's side. Light isn't sent: clients light chunks
themselves.
- Block changes go only to players who have the chunk. Everyone else gets the change with the chunk when they come
close.
+1 -1
View File
@@ -299,7 +299,7 @@ How the server sends the world, like Minecraft Java since 1.20.2. The code is in
Light isn't sent.
- **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 translate them as a chunk loads.
- **The view.** Players send their view distance with `ready` and `view_distance`. The server uses up to 10 chunks, and
- **The view.** Players send their view distance with `ready` and `view_distance`. The server uses up to 16 chunks, and
sends a round area around the player plus the ring past it, which clients need to mesh and light the edge (Minecraft's
`ChunkTrackingView`). Moving into another chunk sends `set_center_chunk`, and `forget_level_chunk` for each chunk that
left the view.
+38 -9
View File
@@ -95,8 +95,9 @@ const GROUND_EPSILON = 0.02;
const MAX_CATCH_UP_DAYS = 64;
// how long a client gets between welcome and ready to download and load everything
const READY_TIMEOUT_TICKS = 60 * TICKS_PER_SECOND;
// the furthest the server sends chunks around a player, and what players who don't say get. minecraft's default
const SERVER_VIEW_DISTANCE = 10;
// the furthest the server sends chunks around a player, and what players who don't say get, until /viewdistance
// changes it
const SERVER_VIEW_DISTANCE = 16;
// how long a tick can spend building chunks for players, the rest go out on the next ticks
const CHUNK_SEND_BUDGET_MS = 20;
@@ -171,6 +172,8 @@ export class GameServer {
do_mob_spawning = true;
// what runs tick(), for /tps. tests call tick() themselves
loop?: GameLoop;
// SERVER_VIEW_DISTANCE, or what /viewdistance set it to. not saved, it's for testing
view_distance = SERVER_VIEW_DISTANCE;
// how long a tick can spend building chunks for players. tests lift it so what gets sent doesn't depend on speed
chunk_send_budget_ms = CHUNK_SEND_BUDGET_MS;
@@ -392,6 +395,15 @@ export class GameServer {
return this.#tick;
}
// the furthest the server sends chunks to anyone, for now. players who asked for more get more, up to it
set_view_distance(distance: number) {
this.view_distance = distance;
for (const player of this.players()) {
player.chunks.view_distance = view_distance(player.requested_view_distance, distance);
this.#update_view(player, true);
}
}
players(): ServerPlayer[] {
return [...this.#players.values()];
}
@@ -695,7 +707,7 @@ export class GameServer {
return;
}
this.#pending.delete(conn);
this.#join(conn, pending.name, view_distance(message.view_distance));
this.#join(conn, pending.name, message.view_distance);
}
}
@@ -704,14 +716,15 @@ export class GameServer {
this.#host.close(conn);
}
#join(conn: number, raw_name: unknown, view_distance: number) {
#join(conn: number, raw_name: unknown, requested_view_distance: unknown) {
const player = new ServerPlayer(
this.entities,
conn,
this.#unique_name(raw_name),
view_distance,
view_distance(requested_view_distance, this.view_distance),
() => this.recipes.shaped,
);
if (is_int(requested_view_distance)) player.requested_view_distance = requested_view_distance;
const saved = this.#saved_players[player.name];
if (saved) {
player.load_player(saved);
@@ -778,7 +791,8 @@ export class GameServer {
}
case "view_distance":
if (is_int(message.distance)) {
player.chunks.view_distance = view_distance(message.distance);
player.requested_view_distance = message.distance;
player.chunks.view_distance = view_distance(message.distance, this.view_distance);
this.#update_view(player, true);
}
break;
@@ -1293,6 +1307,21 @@ export class GameServer {
}),
);
// TODO: only let operators do this
register(
literal<ServerCommandSource>("viewdistance").describe("Shows or changes the server's view distance")
.executes((context) => {
context.source.send_message(`The view distance is ${this.view_distance} chunks`);
})
.then(
argument<number, ServerCommandSource>("chunks", integer(MIN_VIEW_DISTANCE, MAX_VIEW_DISTANCE))
.executes((context) => {
this.set_view_distance(context.get<number>("chunks"));
context.source.send_message(`Set the view distance to ${this.view_distance} chunks`);
}),
),
);
register(
literal<ServerCommandSource>("tps").describe("Shows how the game loop keeps up").executes((context) => {
if (!this.loop) {
@@ -1815,11 +1844,11 @@ function is_int(value: unknown): value is number {
}
// what a player asked for, up to the server's
function view_distance(requested: unknown): number {
function view_distance(requested: unknown, server: number): number {
if (!is_int(requested)) {
return SERVER_VIEW_DISTANCE;
return server;
}
return Math.min(SERVER_VIEW_DISTANCE, Math.max(MIN_VIEW_DISTANCE, Math.min(MAX_VIEW_DISTANCE, requested)));
return Math.min(server, Math.max(MIN_VIEW_DISTANCE, Math.min(MAX_VIEW_DISTANCE, requested)));
}
// messages are parsed json, so the types in ClientMessage are only what a well behaved client sends
+2
View File
@@ -94,6 +94,8 @@ export class ServerPlayer extends LivingEntity {
sent = new Map<string, string>();
// the chunks their client has and still needs
chunks: PlayerChunks;
// the view distance their client asked for, which the server's might cap, undefined when it didn't say
requested_view_distance: number | undefined;
constructor(
level: EntityLevel,
+12
View File
@@ -130,3 +130,15 @@ Deno.test("block changes only go to players who have the chunk, the others get t
const chunk = decode_level_chunk(game.world.level_chunk(0, 0));
assertEquals(chunk.blocks[120 * CHUNK_AREA + 5 * CHUNK_SIZE + 3], game.world.get_block_value(3, 120, 5));
});
Deno.test("/viewdistance caps every player's view distance, and raising it gives them what they asked for", async () => {
const t = await test_game("mods");
t.join(1, "alice");
const player = t.game.players()[0];
t.send(1, { type: "view_distance", distance: 12 });
assertEquals(player.chunks.view_distance, 12);
t.send(1, { type: "chat", text: "/viewdistance 4" });
assertEquals([t.game.view_distance, player.chunks.view_distance], [4, 4]);
t.send(1, { type: "chat", text: "/viewdistance 32" });
assertEquals([t.game.view_distance, player.chunks.view_distance], [32, 12]);
});