Curios slots rendering !

This commit is contained in:
2026-10-01 17:57:22 -03:00
parent ea9f3f61bb
commit f2c3cb8135
39 changed files with 1032 additions and 226 deletions
+55 -2
View File
@@ -1,7 +1,7 @@
// curios: accessory slots for items in the curios tags, next to the player's inventory, see
// mods/bworld/scripts/curios.ts
import { assert, assertEquals } from "@std/assert";
import { PLAYER_MENU_EXTRA } from "$/common/protocol.ts";
import { PLAYER_MENU_EXTRA, PROTOCOL_VERSION } from "$/common/protocol.ts";
import { copy_dir, test_game } from "./helpers.ts";
import type { WorldSave } from "$/server/game/saves.ts";
@@ -27,7 +27,20 @@ async function setup(save?: WorldSave) {
version: "1.0.0",
dependencies: [{ id: "bworld", version: "*" }],
});
write("items/ring.json", { format_version: 1, item: { id: "jewelry:ring", texture: "bworld:coal" } });
// the ring shows on the player's right arm, the amulet doesn't show
const worn = { model: "jewelry:ring_worn", texture: "bworld:coal" };
write("items/ring.json", { format_version: 1, item: { id: "jewelry:ring", texture: "bworld:coal", worn } });
write("models/ring_worn.geo.json", {
format_version: "1.12.0",
"minecraft:geometry": [{
description: { identifier: "geometry.ring", texture_width: 16, texture_height: 16 },
bones: [{
name: "right_arm",
pivot: [-5, 22, 0],
cubes: [{ origin: [-8, 12, -2], size: [4, 1, 4], uv: [0, 0] }],
}],
}],
});
write("items/amulet.json", { format_version: 1, item: { id: "jewelry:amulet", texture: "bworld:coal" } });
write("tags/items/bworld/curios/ring.json", { format_version: 1, tag: { values: ["jewelry:ring"] } });
write("tags/items/bworld/curios/curio.json", { format_version: 1, tag: { values: ["jewelry:amulet"] } });
@@ -141,3 +154,43 @@ Deno.test("the giant chestplate goes in the body slot and gives 20 more health w
assertEquals(t.game.players()[0].max_health, 20);
assertEquals(t.game.players()[0].health, 20);
});
Deno.test("everyone sees what a player wears that has a worn model, and players joining later too", async () => {
const t = await setup();
const alice = t.game.players()[0].id;
t.join(2, "bob");
const worn_messages = (conn: number) => t.take(conn).filter((m: Msg) => m.type === "player_worn");
// the ring (given first) and the amulet, only the ring has a worn model
t.send(1, { type: "click", menu: 0, slot: 0, button: 0, shift: true });
t.send(1, { type: "click", menu: 0, slot: 1, button: 0, shift: true });
t.game.tick();
const ring = [{ id: alice, worn: [{ id: "jewelry:ring", count: 1 }] }];
for (const conn of [1, 2]) {
assertEquals(worn_messages(conn).map(({ id, worn }: Msg) => ({ id, worn })), ring);
}
t.game.tick();
assertEquals(worn_messages(2), [], "only sent when it changes");
// someone joining later gets it with the players already there
const carol = t.join(3, "carol");
assertEquals(carol.players.find((p: Msg) => p.id === alice).worn, ring[0].worn);
// rejoining wears it again: alice is told about herself, the others with player_join
t.game.on_disconnect(1);
t.take(2);
t.game.on_connect(1);
t.send(1, { type: "hello", name: "alice", protocol: PROTOCOL_VERSION });
t.send(1, { type: "ready" });
const messages = t.take(1);
const rejoined = messages.find((m: Msg) => m.type === "join");
assertEquals(messages.filter((m: Msg) => m.type === "player_worn").map((m: Msg) => m.worn), [ring[0].worn]);
const joined = t.take(2).find((m: Msg) => m.type === "player_join");
assertEquals(joined.player.worn, ring[0].worn);
// and taking it off
const ring_slot = PLAYER_MENU_EXTRA + curio_slot(t, "ring");
t.send(1, { type: "click", menu: 0, slot: ring_slot, button: 0, shift: true });
t.game.tick();
assertEquals(worn_messages(2).map((m: Msg) => [m.id, m.worn]), [[rejoined.id, []]]);
});
+57
View File
@@ -2,6 +2,7 @@ import { assert, assertEquals, assertThrows } from "@std/assert";
import { check_mods } from "$/tools/check_mods.ts";
import { MAX_TEXTURE_SIZE } from "$/common/constants.ts";
import { create_mod } from "$/tools/new_mod.ts";
import { item_from_json, item_to_json } from "$/common/mod_data.ts";
function temp_mods_dir() {
const dir = Deno.makeTempDirSync({ prefix: "bworld_mods_" });
@@ -26,6 +27,21 @@ function write_json(path: string, value: unknown) {
Deno.writeTextFileSync(path, JSON.stringify(value));
}
// a blockbench model with one bone, a box like an arm's
function geo(bone: string) {
return {
format_version: "1.12.0",
"minecraft:geometry": [{
description: { identifier: "geometry.test", texture_width: 32, texture_height: 32 },
bones: [{
name: bone,
pivot: [-5, 22, 0],
cubes: [{ origin: [-8, 12, -2], size: [4, 12, 4], uv: [16, 0] }],
}],
}],
};
}
Deno.test("a mod made from the template passes the checks", async () => {
const dir = temp_mods_dir();
create_mod("copper_tools", "Copper Tools", dir);
@@ -82,6 +98,16 @@ Deno.test("check-mods finds broken mods", async () => {
});
// not json
Deno.writeTextFileSync(`${mod}/items/bad.json`, "{ nope");
// worn with a model that doesn't exist, as something that isn't { model, texture }, and with a model where nothing
// is named like a player part
const item = (id: string, worn: unknown) => ({ format_version: 1, item: { id, texture: "bworld:coal", worn } });
write_json(
`${mod}/items/ghost.json`,
item("broken:ghost", { model: "broken:no_such_worn", texture: "bworld:coal" }),
);
write_json(`${mod}/items/shapeless.json`, item("broken:shapeless", "broken:hat"));
write_json(`${mod}/items/stiff.json`, item("broken:stiff", { model: "broken:stiff", texture: "bworld:coal" }));
write_json(`${mod}/models/stiff.geo.json`, geo("hat"));
// uses a letter the key doesn't define
write_json(`${mod}/recipes/bad.json`, {
format_version: 1,
@@ -120,7 +146,38 @@ Deno.test("check-mods finds broken mods", async () => {
has(report.errors, "textures/huge.png: is");
has(report.errors, "scripts don't typecheck");
has(report.errors, `depends on "missing_mod", which isn't installed`);
has(report.errors, "entity model broken:no_such_worn doesn't exist");
has(report.errors, "worn must be { model, texture }");
has(report.warnings, "worn model broken:stiff has no bone named like a player part");
has(report.warnings, "texture broken:nope doesn't exist");
has(report.warnings, `uses bworld:stone but doesn't list "bworld" in dependencies`);
Deno.removeSync(dir, { recursive: true });
});
Deno.test("an item can say how it looks worn, with a model for slim arms too", async () => {
const dir = temp_mods_dir();
create_mod("gloves", "Gloves", dir);
const mod = `${dir}/gloves`;
Deno.mkdirSync(`${mod}/models`);
write_json(`${mod}/models/glove_wide.geo.json`, geo("right_arm"));
write_json(`${mod}/models/glove_slim.geo.json`, geo("right_arm"));
const worn = {
model: "gloves:glove_wide",
texture: "gloves:example_item",
slim_model: "gloves:glove_slim",
slim_texture: "gloves:example_item",
};
write_json(`${mod}/items/glove.json`, {
format_version: 1,
item: { id: "gloves:glove", texture: "gloves:example_item", worn },
});
const report = (await check_mods(dir)).find((r) => r.id === "gloves")!;
assertEquals(report.errors, []);
assertEquals(report.warnings, []);
// and the registry, which clients draw from, has it
const glove = JSON.parse(Deno.readTextFileSync(`${mod}/items/glove.json`)).item;
assertEquals(item_from_json(glove).worn, worn);
assertEquals(item_to_json("gloves:glove", item_from_json(glove)).worn, worn);
Deno.removeSync(dir, { recursive: true });
});
+113
View File
@@ -0,0 +1,113 @@
// what players wear is drawn on their parts, moving with them, see client/rendering/worn_renderer.ts
import { assertAlmostEquals, assertEquals, assertNotEquals } from "@std/assert";
import { entity_model_from_geo } from "$/common/mod_data.ts";
import { apply, bone_matrices, type Vec3 } from "$/client/rendering/model_math.ts";
import {
on_part,
part_point,
PARTS,
pose_at,
SLIM_PARTS,
worn_bone_pose,
worn_look,
} from "$/client/rendering/player_model.ts";
// a glove made over blockbench's player template: its right arm's box, and a claw on a bone under it
const glove = entity_model_from_geo({
format_version: "1.12.0",
"minecraft:geometry": [{
description: { identifier: "geometry.glove", texture_width: 32, texture_height: 32 },
bones: [
{ name: "right_arm", pivot: [-5, 22, 0], cubes: [{ origin: [-8, 12, -2], size: [4, 12, 4], uv: [0, 0] }] },
{ name: "claw", parent: "right_arm", pivot: [-6, 12, 0], cubes: [] },
],
}],
}, "test:glove").model!;
const walking = (position: number, slim = false) =>
pose_at({ x: 0, y: 0, z: 0, yaw: 0, pitch: 0, slim, walk: { position, speed: 1 } });
const near = (actual: Vec3, expected: Vec3) => actual.forEach((n, i) => assertAlmostEquals(n, expected[i], 1e-9));
Deno.test("a worn bone named like a player part moves with that part as they walk", () => {
const arm = PARTS.find((part) => part.name === "right_arm")!;
const corners: Vec3[][] = [];
for (const position of [0, 1.5, 3]) {
const pose = walking(position);
const bones = bone_matrices(glove, worn_bone_pose(pose));
// the cube's corners, mirrored on x into the engine's axes like the renderer does, are the arm's corners
const low = apply(bones.get("right_arm")!, 4, 12, -2);
const high = apply(bones.get("right_arm")!, 8, 24, 2);
near(low, part_point(pose, arm, -1, -10, -2));
near(high, part_point(pose, arm, 3, 2, 2));
// and the bone under it goes along
near(apply(bones.get("claw")!, 6, 12, 0), apply(bones.get("right_arm")!, 6, 12, 0));
corners.push([low, high]);
}
assertNotEquals(corners[0], corners[1], "it swung");
});
Deno.test("slim players wear the slim model, on their slim arms", () => {
const worn = { model: "a:wide", texture: "a:wide_texture", slim_model: "a:slim", slim_texture: "a:slim_texture" };
assertEquals(worn_look(worn, false), { model: "a:wide", texture: "a:wide_texture" });
assertEquals(worn_look(worn, true), { model: "a:slim", texture: "a:slim_texture" });
// without slim ones, the wide ones
assertEquals(worn_look({ model: "a:wide", texture: "a:t" }, true), { model: "a:wide", texture: "a:t" });
// the slim arm turns around its own pivot, half a pixel lower
const slim_arm = SLIM_PARTS.find((part) => part.name === "right_arm")!;
const pose = walking(1.5, true);
const bones = bone_matrices(glove, worn_bone_pose(pose));
near(apply(bones.get("right_arm")!, ...slim_arm.pivot), slim_arm.pivot);
});
Deno.test("first person draws the bones on the right arm, and the ones under them", () => {
const [arm, claw] = glove.bones;
assertEquals([on_part(glove, arm, "right_arm"), on_part(glove, claw, "right_arm")], [true, true]);
assertEquals(on_part(glove, arm, "left_arm"), false);
});
// artifacts' models, see mods/artifacts/models: a cube's box in the engine's axes, mirrored like the renderer does
const artifact = (name: string) => {
const json = JSON.parse(Deno.readTextFileSync(`mods/artifacts/models/${name}.geo.json`));
return entity_model_from_geo(json, `artifacts:${name}`).model!;
};
const box = (
{ origin: [x, y, z], size: [w, h, d] }: { origin: Vec3; size: Vec3 },
) => [[-(x + w), y, z], [-x, y + h, z + d]];
Deno.test("artifacts' power glove is a sleeve over the right arm, wide and slim", () => {
for (const [name, parts] of [["power_glove_wide", PARTS], ["power_glove_slim", SLIM_PARTS]] as const) {
const arm = parts.find((part) => part.name === "right_arm")!;
const from = arm.from.map((n, i) => n + arm.pivot[i]);
const arm_box = [from, from.map((n, i) => n + arm.size[i])];
for (const cube of artifact(name).bones[0].cubes) assertEquals(box(cube), arm_box, name);
}
});
Deno.test("artifacts' feral claws come out of the bottom of the right hand, wide and slim", () => {
for (const [name, parts] of [["feral_claws_wide", PARTS], ["feral_claws_slim", SLIM_PARTS]] as const) {
const arm = parts.find((part) => part.name === "right_arm")!;
const hand = arm.pivot[1] + arm.from[1];
const claws = artifact(name).bones[0].cubes.map(box);
// they hang from the hand, the long ones across the arm. on slim arms they're half a pixel off its middle, like
// in artifacts' ArmsMeshDefinitions
for (const [, high] of claws) assertEquals(high[1], hand, name);
const [left, right] = [arm.pivot[0] + arm.from[0], arm.pivot[0] + arm.from[0] + arm.size[0]];
for (const [low, high] of claws.slice(0, 2)) {
assertEquals(Math.abs((low[0] + high[0]) / 2 - (left + right) / 2) <= 0.5, true, name);
}
}
});
Deno.test("the giant chestplate covers the player's body and arms, a pixel out like minecraft's armor", () => {
const json = JSON.parse(Deno.readTextFileSync("mods/bworld/models/giant_chestplate.geo.json"));
const chestplate = entity_model_from_geo(json, "bworld:giant_chestplate").model!;
for (const bone of chestplate.bones) {
const part = PARTS.find((part) => part.name === bone.name)!;
const from = part.from.map((n, i) => n + part.pivot[i]);
assertEquals(box(bone.cubes[0]), [from, from.map((n, i) => n + part.size[i])], bone.name);
assertEquals(bone.cubes[0].inflate, 1);
}
assertEquals(chestplate.bones.map((bone) => bone.name), ["body", "right_arm", "left_arm"]);
});