Predict interactions with blocks

This commit is contained in:
2026-10-10 14:21:27 -03:00
parent 7fe9c15b67
commit 79ff6f9ad5
15 changed files with 355 additions and 73 deletions
+68
View File
@@ -0,0 +1,68 @@
// what this client changed ahead of the server, like minecraft's BlockStatePredictionHandler. each break_block and
// use_block gets a sequence number, and the server sends block_ack with it once it's handled it, after the set_blocks
// it caused. until then, what the server says about a block a guess changed is kept here instead of shown, so the
// answer to an older click can't undo a newer guess, like a trapdoor clicked twice flickering open again
// a block as the server sends it, see set_block
export interface ServerBlock {
x: number;
y: number;
z: number;
id: string;
state: number;
}
export class BlockPredictions {
// the last sequence number handed out
#seq = 0;
// whether a guess is running, see start
#predicting = false;
// the blocks guesses changed, by "x,y,z": the newest guess that changed it, and what the server last said is there
#server = new Map<string, { seq: number; block: ServerBlock }>();
// a guess is starting: the blocks it changes are kept track of until end. returns its sequence number, for the
// message that tells the server what the player did
start(): number {
this.#seq++;
this.#predicting = true;
return this.#seq;
}
end() {
this.#predicting = false;
}
// this client is about to change the block at x y z, which is what current gives now. the first guess to change it
// keeps that as what the server said, later ones only make it wait for their ack too
retain(x: number, y: number, z: number, current: () => ServerBlock) {
if (!this.#predicting) return;
const key = `${x},${y},${z}`;
const known = this.#server.get(key);
if (known) {
known.seq = this.#seq;
} else {
this.#server.set(key, { seq: this.#seq, block: current() });
}
}
// the server says x y z is id with state. returns true when a guess about it is waiting for its ack, then it's kept
// for then instead of shown
from_server(x: number, y: number, z: number, id: string, state: number): boolean {
const known = this.#server.get(`${x},${y},${z}`);
if (!known) return false;
known.block = { x, y, z, id, state };
return true;
}
// the server handled everything up to seq: the blocks only those guesses changed, as the server says they are now,
// to show instead of the guesses
ack(seq: number): ServerBlock[] {
const done: ServerBlock[] = [];
for (const [key, { seq: newest, block }] of this.#server) {
if (newest > seq) continue;
this.#server.delete(key);
done.push(block);
}
return done;
}
}
+32 -1
View File
@@ -29,6 +29,7 @@ import type { Entity } from "../entity/entity.ts";
import type { ModelJson } from "$/common/block_models.ts";
import { clip_shape, collision_shape_at, FULL_SHAPE, type Shape, shape_at } from "$/common/block_shapes.ts";
import type { ItemStack } from "$/common/inventory.ts";
import { BlockPredictions } from "./block_predictions.ts";
export interface Block {
id: string;
@@ -119,6 +120,8 @@ export class ClientLevel {
view_distance: number;
// the chunk the server says the player is in
center: { x: number; z: number } | undefined;
// what this client changed ahead of the server, see MultiPlayerGameMode.predict
predictions = new BlockPredictions();
workers: ChunkWorkerPool;
#blocks = EverythingRegistry.get_registry<BlockRegistry>("blocks");
@@ -259,6 +262,7 @@ export class ClientLevel {
}
const [nid, info] = EverythingRegistry.get_full<BlockRegistry>("blocks", block.id)!;
this.#retain(block.x, block.y, block.z);
const lx = block.x - block_chunk_x * CHUNK_SIZE;
const lz = block.z - block_chunk_z * CHUNK_SIZE;
@@ -375,6 +379,8 @@ export class ClientLevel {
return;
}
this.#retain(x, y, z);
const lx = x - block_chunk_x * CHUNK_SIZE;
const lz = z - block_chunk_z * CHUNK_SIZE;
const ly = y;
@@ -451,8 +457,33 @@ export class ClientLevel {
return [x, y, z];
}
// set a block the server told us about. the server only sends them for chunks this client has
// set a block the server told us about. the server only sends them for chunks this client has. a block this
// client guessed waits for the server's ack instead, see BlockPredictions
apply_change(x: number, y: number, z: number, id: string, state = 0) {
if (this.predictions.from_server(x, y, z, id, state)) {
return;
}
this.#set_from_server(x, y, z, id, state);
}
// the server handled this client's guesses up to seq, what it said about the blocks they changed shows now
ack_predictions(seq: number) {
for (const { x, y, z, id, state } of this.predictions.ack(seq)) {
this.#set_from_server(x, y, z, id, state);
}
}
// while guessing, keeps what's at x y z before the guess changes it, as the server would say it
#retain(x: number, y: number, z: number) {
this.predictions.retain(x, y, z, () => {
const value = this.get_block_value(x, y, z);
const nid = value & ID_MASK;
const id = nid === AIR ? AIR_ID : this.#blocks[nid]?.id ?? AIR_ID;
return { x, y, z, id, state: value >>> 16 };
});
}
#set_from_server(x: number, y: number, z: number, id: string, state: number) {
const chunk = this.get_chunk(Math.floor(x / CHUNK_SIZE), Math.floor(z / CHUNK_SIZE));
if (!chunk) {
return;