Verdigris wire and lever
This commit is contained in:
+11
-100
@@ -4,107 +4,18 @@
|
||||
// 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, is_solid } from "$/common/block_shapes.ts";
|
||||
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import { tag_api } from "$/common/tags.ts";
|
||||
import { AIR_ID } from "$/common/protocol.ts";
|
||||
import { get_state_value, set_state_value } from "$/common/utils.ts";
|
||||
import type { Face, SharedWorld } from "$/common/mod_api/shared.ts";
|
||||
import { setup as setup_shared } from "$/mods/bworld/scripts/shared.ts";
|
||||
import { test_game } from "./helpers.ts";
|
||||
import { get_state_value } from "$/common/utils.ts";
|
||||
import type { Face } from "$/common/mod_api/shared.ts";
|
||||
import { type Area, client_of, server_blocks, test_game } from "./helpers.ts";
|
||||
|
||||
type Game = Awaited<ReturnType<typeof test_game>>;
|
||||
const AREA = { x: [-3, 3], y: [99, 101], z: [-3, 3] };
|
||||
|
||||
// every block in the area, as id and state bits
|
||||
function blocks_in(get: (x: number, y: number, z: number) => [string, number]) {
|
||||
const out: string[] = [];
|
||||
for (let x = AREA.x[0]; x <= AREA.x[1]; x++) {
|
||||
for (let y = AREA.y[0]; y <= AREA.y[1]; y++) {
|
||||
for (let z = AREA.z[0]; z <= AREA.z[1]; z++) out.push(`${x} ${y} ${z} ${get(x, y, z).join(" ")}`);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
const AREA: Area = { x: [-3, 3], y: [99, 101], z: [-3, 3] };
|
||||
|
||||
function state(t: Game, x: number, y: number, z: number, name: string) {
|
||||
return get_state_value(t.game.world.get_block_value(x, y, z), t.game.world.get_block_info(x, y, z)!, name);
|
||||
}
|
||||
|
||||
function server_blocks(t: Game) {
|
||||
return blocks_in((x, y, z) => [t.game.world.get_block_id(x, y, z), t.game.world.get_block_value(x, y, z) >>> 16]);
|
||||
}
|
||||
|
||||
// a client: its copy of the area, and the bworld shared script's rules running on it the way client/mods.ts does
|
||||
function client_of(t: Game) {
|
||||
const copy = new Map<string, [string, number]>();
|
||||
for (const line of server_blocks(t)) {
|
||||
const [x, y, z, id, state] = line.split(" ");
|
||||
copy.set(`${x} ${y} ${z}`, [id, Number(state)]);
|
||||
}
|
||||
const at = (x: number, y: number, z: number) => copy.get(`${x} ${y} ${z}`);
|
||||
const info = (id: string) => EverythingRegistry.get<BlockRegistry>("blocks", id);
|
||||
|
||||
const rules = new BlockRules((_mod, _what, fn) => fn(), (x, y, z) => at(x, y, z)?.[0]);
|
||||
const world: SharedWorld = {
|
||||
get_block: (x, y, z) => at(x, y, z)?.[0],
|
||||
get_state(x, y, z, name) {
|
||||
const [id, state] = at(x, y, z) ?? [AIR_ID, 0];
|
||||
const block = info(id);
|
||||
return block?.states?.some((s) => s.name === name) ? get_state_value(state << 16, block, name) : undefined;
|
||||
},
|
||||
set_state(x, y, z, name, value) {
|
||||
const [id, state] = at(x, y, z) ?? [AIR_ID, 0];
|
||||
const block = info(id);
|
||||
if (!block?.states?.some((s) => s.name === name)) return false;
|
||||
const changed = set_state_value(state << 16, block, name, value)! >>> 16;
|
||||
if (changed !== state) {
|
||||
copy.set(`${x} ${y} ${z}`, [id, changed]);
|
||||
rules.notify_neighbors(x, y, z);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
is_face_sturdy(x, y, z, face: Face) {
|
||||
const [id, state] = at(x, y, z) ?? [AIR_ID, 0];
|
||||
const block = info(id);
|
||||
return block !== undefined && covers_face(collision_shape_at(block, state << 16), face);
|
||||
},
|
||||
biome_at: () => undefined,
|
||||
season_at: () => undefined,
|
||||
};
|
||||
setup_shared({
|
||||
mod: { id: "bworld", version: "test" },
|
||||
side: "client",
|
||||
tags: tag_api(),
|
||||
blocks: {
|
||||
exists: (id) => info(id) !== undefined,
|
||||
components: (id) => structuredClone(info(id)!.components ?? {}),
|
||||
solid: (id) => id !== AIR_ID && is_solid(info(id)!),
|
||||
},
|
||||
world,
|
||||
components: { register_block: (id, block_rules) => rules.register("bworld", id, block_rules) },
|
||||
});
|
||||
|
||||
return {
|
||||
// what game_mode does when this client places held against face of x y z, at hit
|
||||
place(id: string, x: number, y: number, z: number, yaw: number, face: Face, hit: [number, number, number]) {
|
||||
const value = rules.placement_value(info(id)!, yaw, 0, { x, y, z, face, hit, sneaking: false });
|
||||
copy.set(`${x} ${y} ${z}`, [id, value! >>> 16]);
|
||||
rules.notify_neighbors(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, sneaking: false }, yaw);
|
||||
},
|
||||
break(x: number, y: number, z: number) {
|
||||
copy.set(`${x} ${y} ${z}`, [AIR_ID, 0]);
|
||||
rules.notify_neighbors(x, y, z);
|
||||
},
|
||||
blocks: () => blocks_in((x, y, z) => at(x, y, z)!),
|
||||
};
|
||||
}
|
||||
|
||||
// alice on a stone floor at y 99, south of 0 100 0, holding id
|
||||
async function setup(id: string, yaw = 0) {
|
||||
const t = await test_game("mods");
|
||||
@@ -128,11 +39,11 @@ Deno.test("a client places a fence joined to the fences and walls beside it, the
|
||||
t.game.set_block(-1, 100, 0, "bworld:oak_fence");
|
||||
t.game.set_block(0, 100, -1, "bworld:stone");
|
||||
t.game.set_block(1, 100, 0, "bworld:oak_leaves");
|
||||
const client = client_of(t);
|
||||
const client = client_of(t.game, AREA);
|
||||
|
||||
client.place("bworld:oak_fence", 0, 100, 0, 0, "top", [0.5, 1, 0.5]);
|
||||
use(0, 99, 0, "top", [0.5, 1, 0.5]);
|
||||
assertEquals(client.blocks(), server_blocks(t));
|
||||
assertEquals(client.blocks(), server_blocks(t.game, AREA));
|
||||
// it joined the fence and the stone, and the fence joined it back
|
||||
assertEquals(state(t, 0, 100, 0, "west"), 1);
|
||||
assertEquals(state(t, 0, 100, 0, "north"), 1);
|
||||
@@ -141,21 +52,21 @@ Deno.test("a client places a fence joined to the fences and walls beside it, the
|
||||
// breaking it lets go of the one beside it, on both
|
||||
client.break(0, 100, 0);
|
||||
t.send(1, { type: "break_block", x: 0, y: 100, z: 0 });
|
||||
assertEquals(client.blocks(), server_blocks(t));
|
||||
assertEquals(client.blocks(), server_blocks(t.game, AREA));
|
||||
assertEquals(state(t, -1, 100, 0, "east"), 0);
|
||||
});
|
||||
|
||||
Deno.test("a client places a trapdoor turned and in the half the server puts it", async () => {
|
||||
const { t, use } = await setup("bworld:oak_trapdoor", Math.PI / 2);
|
||||
t.game.set_block(0, 100, 0, "bworld:stone");
|
||||
const client = client_of(t);
|
||||
const client = client_of(t.game, AREA);
|
||||
|
||||
// on the upper half of the stone's east side, and on top of the floor facing alice, who looks west
|
||||
client.place("bworld:oak_trapdoor", 1, 100, 0, Math.PI / 2, "east", [1, 0.8, 0.5]);
|
||||
use(0, 100, 0, "east", [1, 0.8, 0.5]);
|
||||
client.place("bworld:oak_trapdoor", 0, 100, 2, Math.PI / 2, "top", [0.5, 1, 0.5]);
|
||||
use(0, 99, 2, "top", [0.5, 1, 0.5]);
|
||||
assertEquals(client.blocks(), server_blocks(t));
|
||||
assertEquals(client.blocks(), server_blocks(t.game, AREA));
|
||||
assertEquals(state(t, 1, 100, 0, "half"), 1);
|
||||
assertEquals(state(t, 0, 100, 2, "facing"), 1);
|
||||
});
|
||||
@@ -163,12 +74,12 @@ Deno.test("a client places a trapdoor turned and in the half the server puts it"
|
||||
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);
|
||||
const client = client_of(t.game, AREA);
|
||||
|
||||
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(client.blocks(), server_blocks(t.game, AREA));
|
||||
assertEquals(state(t, 0, 100, 0, "open"), open);
|
||||
}
|
||||
// alice still has all of hers
|
||||
|
||||
Reference in New Issue
Block a user