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
+64
View File
@@ -0,0 +1,64 @@
// what a client guesses about blocks waits for the server's block_ack before the server's word replaces it, like
// minecraft's BlockStatePredictionHandler, so clicking fast doesn't flicker. see client/level/block_predictions.ts
import { assertEquals } from "@std/assert";
import { BlockPredictions } from "$/client/level/block_predictions.ts";
import { test_game } from "./helpers.ts";
const CLOSED = { x: 0, y: 100, z: 0, id: "bworld:oak_trapdoor", state: 0 };
const OPEN = { ...CLOSED, state: 4 };
Deno.test("what the server says about a guessed block waits for the ack of the newest guess that changed it", () => {
const predictions = new BlockPredictions();
// a trapdoor clicked open, then closed again before the server answers
const first = predictions.start();
predictions.retain(0, 100, 0, () => CLOSED);
predictions.end();
const second = predictions.start();
predictions.retain(0, 100, 0, () => OPEN);
predictions.end();
// the answer to the first click opens it, but the second is still on its way
assertEquals(predictions.from_server(0, 100, 0, OPEN.id, OPEN.state), true);
assertEquals(predictions.ack(first), []);
// and the answer to the second closes it, which shows now
assertEquals(predictions.from_server(0, 100, 0, CLOSED.id, CLOSED.state), true);
assertEquals(predictions.ack(second), [CLOSED]);
assertEquals(predictions.from_server(0, 100, 0, OPEN.id, OPEN.state), false);
});
Deno.test("a guess the server says nothing about goes back to what it said before, once acked", () => {
const predictions = new BlockPredictions();
const seq = predictions.start();
predictions.retain(0, 100, 0, () => CLOSED);
predictions.end();
assertEquals(predictions.ack(seq), [CLOSED]);
});
Deno.test("changes outside a guess aren't kept track of", () => {
const predictions = new BlockPredictions();
predictions.retain(0, 100, 0, () => CLOSED);
assertEquals(predictions.from_server(0, 100, 0, OPEN.id, OPEN.state), false);
assertEquals(predictions.ack(1), []);
});
Deno.test("the server acks a use_block or break_block after the set_blocks it caused, even one it ignores", async () => {
const { game, join, take, send } = await test_game("mods");
join(1, "alice");
send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw: 0, pitch: 0 });
game.set_block(0, 99, 0, "bworld:stone");
game.set_block(0, 100, 0, "bworld:oak_trapdoor");
// until alice's client has the chunk, nothing in it is sent there
while (!game.players()[0].chunks.has_block(0, 0)) game.tick();
take(1);
send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 1], seq: 1 });
const types = take(1).map((m) => m.type).filter((type) => type === "set_block" || type === "block_ack");
assertEquals(types.at(-1), "block_ack");
assertEquals(types.includes("set_block"), true);
send(1, { type: "break_block", x: 0, y: 1000, z: 0, seq: 2 });
assertEquals(take(1).filter((m) => m.type === "block_ack"), [{ type: "block_ack", seq: 2 }]);
// without a seq, from a client that doesn't guess, there's nothing to ack
send(1, { type: "break_block", x: 0, y: 100, z: 0 });
assertEquals(take(1).filter((m) => m.type === "block_ack"), []);
});
+21 -2
View File
@@ -1,5 +1,5 @@
// shared scripts' block rules run on clients too, so what a client shows the moment it places or breaks a block is
// what the server ends up with
// shared scripts' block rules run on clients too, so what a client shows the moment it places, breaks or right clicks
// a block is what the server ends up with
import { assertEquals } from "@std/assert";
import { BlockRules } from "$/common/block_rules.ts";
import { collision_shape_at, covers_face } from "$/common/block_shapes.ts";
@@ -85,6 +85,10 @@ function client_of(t: Game) {
copy.set(`${x} ${y} ${z}`, [id, value >>> 16]);
rules.notify_neighbors(world.get_block, x, y, z);
},
// what game_mode does when this client right clicks face of x y z, at hit. whether the rules did something
interact(x: number, y: number, z: number, yaw: number, face: Face, hit: [number, number, number]) {
return rules.interact(info(at(x, y, z)![0])!, x, y, z, { face, hit }, yaw);
},
break(x: number, y: number, z: number) {
copy.set(`${x} ${y} ${z}`, [AIR_ID, 0]);
rules.notify_neighbors(world.get_block, x, y, z);
@@ -147,3 +151,18 @@ Deno.test("a client places a trapdoor turned and in the half the server puts it"
assertEquals(state(t, 1, 100, 0, "half"), 1);
assertEquals(state(t, 0, 100, 2, "facing"), 1);
});
Deno.test("a client opens and closes a trapdoor right away, the same as the server, without placing anything", async () => {
const { t, use } = await setup("bworld:oak_trapdoor");
t.game.set_block(0, 100, 0, "bworld:oak_trapdoor");
const client = client_of(t);
for (const open of [1, 0]) {
assertEquals(client.interact(0, 100, 0, 0, "south", [0.5, 0.5, 1]), true);
use(0, 100, 0, "south", [0.5, 0.5, 1]);
assertEquals(client.blocks(), server_blocks(t));
assertEquals(state(t, 0, 100, 0, "open"), open);
}
// alice still has all of hers
assertEquals(t.game.players()[0].inventory.get_slot(0).amount, 64);
});