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
+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);
});