Verdigris wire and lever

This commit is contained in:
2026-10-22 13:43:57 -03:00
parent 0d80e85dec
commit 6af6c52673
24 changed files with 3191 additions and 733 deletions
+99 -2
View File
@@ -2,17 +2,23 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import { resolve, toFileUrl } from "@std/path";
import { EverythingRegistry } from "$/common/everything_registry.ts";
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
import { load_and_check, load_order } from "$/tools/check_mods.ts";
import { mod_data } from "$/common/mod_check.ts";
import type { ServerModSource } from "$/server/game/load_mods.ts";
import { GameServer } from "$/server/game/game_server.ts";
import { start_game } from "$/server/game/load_mods.ts";
import { apply_config } from "$/server/load_bmods.ts";
import { PROTOCOL_VERSION } from "$/common/protocol.ts";
import { AIR_ID, PROTOCOL_VERSION } from "$/common/protocol.ts";
import { decode_level_chunk } from "$/common/chunk_streaming.ts";
import { CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "$/common/constants.ts";
import type { SavedChunk, SavedChunks, WorldSave } from "$/server/game/saves.ts";
import { BlockRules } from "$/common/block_rules.ts";
import { collision_shape_at, covers_face, is_solid } from "$/common/block_shapes.ts";
import { tag_api } from "$/common/tags.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";
// the repo's own mods/, read once: reading and checking every mod's files is most of starting a game in a test. only
// mods/ is kept, since tests change the temporary folders they make between games
@@ -184,3 +190,94 @@ export const container_items = (messages: Msg[], container: string) =>
// what the open menu's data last was among messages
export const menu_data = (messages: Msg[]) => messages.findLast((m) => m.type === "menu_data")?.data;
// a box of blocks, by the lowest and highest of each coordinate in it
export interface Area {
x: [number, number];
y: [number, number];
z: [number, number];
}
// every block in the area, as id and state bits
function blocks_in(area: Area, 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;
}
export function server_blocks(game: GameServer, area: Area) {
return blocks_in(area, (x, y, z) => [game.world.get_block_id(x, y, z), 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
export function client_of(game: GameServer, area: Area) {
const copy = new Map<string, [string, number]>();
for (const line of server_blocks(game, area)) {
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(area, (x, y, z) => at(x, y, z)!),
};
}