Curios and artifacts !

This commit is contained in:
2026-09-30 15:22:32 -03:00
parent 342b45d593
commit 3af5f6e54b
45 changed files with 729 additions and 43 deletions
+46
View File
@@ -0,0 +1,46 @@
// artifacts: items worn in curios slots that change the wearer's attributes, see mods/artifacts
import { assertEquals } from "@std/assert";
import { ATTRIBUTES } from "$/server/game/entity/attributes.ts";
import { test_game } from "./helpers.ts";
// deno-lint-ignore no-explicit-any
type Msg = any;
async function setup() {
const t = await test_game("mods");
t.join(1, "alice");
t.send(1, { type: "chat", text: "/give artifacts:power_glove" });
t.send(1, { type: "chat", text: "/give artifacts:feral_claws" });
t.take(1);
return { ...t, alice: t.game.players()[0] };
}
Deno.test("the power glove and feral claws go in the hands slot, and only work while worn", async () => {
const t = await setup();
t.send(1, { type: "mod", channel: "curios:open", data: {} });
const screen = t.take(1).find((m: Msg) => m.type === "open_screen");
assertEquals(screen.layout.slots.map((s: Msg) => s.icon), ["curios:empty_hands_slot"]);
t.send(1, { type: "click", container: "inventory", index: 0, button: 0, shift: true });
t.game.tick();
assertEquals(t.alice.attribute(ATTRIBUTES.attack_damage), 1 + 4);
// swapped for the claws by hand: the glove's damage goes, the claws' speed comes
t.send(1, { type: "click", container: "inventory", index: 1, button: 0 });
t.send(1, { type: "click", container: "screen", index: 6, button: 0 });
t.game.tick();
assertEquals(t.alice.attribute(ATTRIBUTES.attack_damage), 1);
assertEquals(t.alice.attribute(ATTRIBUTES.attack_speed), 4 * 1.3);
});
Deno.test("worn artifacts work again after rejoining, and say what they do", async () => {
const t = await setup();
t.send(1, { type: "mod", channel: "curios:open", data: {} });
t.send(1, { type: "click", container: "inventory", index: 0, button: 0, shift: true });
const curios = t.take(1).filter((m: Msg) => m.type === "container" && m.container === "screen").at(-1).items;
assertEquals(curios[6].lore, "When worn:\n+4 Attack Damage");
t.game.on_disconnect(1);
t.join(1, "alice");
assertEquals(t.game.players()[0].attribute(ATTRIBUTES.attack_damage), 1 + 4);
});
+74
View File
@@ -0,0 +1,74 @@
// curios: accessory slots for items in the curios tags, opened over mod channels, see mods/curios
import { assert, assertEquals } from "@std/assert";
import { copy_dir, test_game } from "./helpers.ts";
// deno-lint-ignore no-explicit-any
type Msg = any;
const last_container = (messages: Msg[], container: string) =>
messages.filter((m) => m.type === "container" && m.container === container).at(-1)?.items;
// the base game, curios, and a mod with a ring and an amulet that goes in any slot
async function setup() {
const dir = Deno.makeTempDirSync({ prefix: "bworld_curios_" });
copy_dir("mods/bworld", `${dir}/bworld`);
copy_dir("mods/curios", `${dir}/curios`);
const write = (path: string, json: unknown) => {
Deno.mkdirSync(`${dir}/jewelry/${path}`.replace(/\/[^/]+$/, ""), { recursive: true });
Deno.writeTextFileSync(`${dir}/jewelry/${path}`, JSON.stringify(json));
};
write("manifest.json", { format_version: 1, id: "jewelry", name: "Jewelry", version: "1.0.0" });
write("items/ring.json", { format_version: 1, item: { id: "jewelry:ring", texture: "bworld:coal" } });
write("items/amulet.json", { format_version: 1, item: { id: "jewelry:amulet", texture: "bworld:coal" } });
write("tags/items/curios/ring.json", { format_version: 1, tag: { values: ["jewelry:ring"] } });
write("tags/items/curios/curio.json", { format_version: 1, tag: { values: ["jewelry:amulet"] } });
const t = await test_game(dir);
t.join(1, "alice");
for (const id of ["jewelry:ring", "jewelry:amulet", "bworld:coal"]) {
t.send(1, { type: "chat", text: `/give ${id}` });
}
t.take(1);
return t;
}
Deno.test("the curios screen shows the slot types some item goes in, with their icons", async () => {
const t = await setup();
t.send(1, { type: "mod", channel: "curios:open", data: {} });
const screen = t.take(1).find((m) => m.type === "open_screen");
assertEquals(screen.layout.slots, [
{ index: 0, x: 3.5, y: 0, icon: "curios:empty_curio_slot" },
{ index: 7, x: 4.5, y: 0, icon: "curios:empty_ring_slot" },
]);
});
Deno.test("items only go in the slots they fit, and fall out when the player dies", async () => {
const t = await setup();
t.send(1, { type: "mod", channel: "curios:open", data: {} });
t.take(1);
// coal fits nowhere, the ring only in the ring slot, and #curios:curio items in any slot, the curio slot first
t.send(1, { type: "click", container: "inventory", index: 2, button: 0, shift: true });
t.send(1, { type: "click", container: "inventory", index: 0, button: 0, shift: true });
t.send(1, { type: "click", container: "inventory", index: 1, button: 0, shift: true });
const messages = t.take(1);
assertEquals(last_container(messages, "inventory").slice(0, 3), [null, null, { id: "bworld:coal", count: 1 }]);
const curios = last_container(messages, "screen");
assertEquals(curios[0], { id: "jewelry:amulet", count: 1 });
assertEquals(curios[7], { id: "jewelry:ring", count: 1 });
const player = t.game.players()[0];
t.game.damage_player(player, 100, "fall");
const dropped = t.game.item_entities().map((e) => e.item.type_id).sort();
assertEquals(dropped, ["bworld:coal", "jewelry:amulet", "jewelry:ring"]);
t.send(1, { type: "mod", channel: "curios:open", data: {} });
assertEquals(last_container(t.take(1), "screen").filter(Boolean), []);
});
Deno.test("mod messages that are too big or too many are dropped", async () => {
const t = await setup();
t.send(1, { type: "mod", channel: "curios:open", data: "x".repeat(20_000) });
assertEquals(t.take(1).filter((m) => m.type === "open_screen"), []);
for (let i = 0; i < 100; i++) t.send(1, { type: "mod", channel: "curios:open", data: {} });
assert(t.take(1).filter((m) => m.type === "open_screen").length < 100);
});
+26 -2
View File
@@ -149,7 +149,8 @@ Deno.test("players hit entities they can reach, pushing them away", async () =>
const mob = spawn(0.5, 100, 2.5);
const far = spawn(0.5, 100, 20.5);
far.tags.add("stays");
ticks(2);
// long enough for a full strength attack
ticks(5);
send(1, { type: "attack_entity", id: mob.id });
assertEquals(mob.health, 9);
@@ -161,12 +162,35 @@ Deno.test("players hit entities they can reach, pushing them away", async () =>
send(1, { type: "attack_entity", id: "nothing" });
});
Deno.test("attacking again before the attack speed allows hits weaker, like minecraft", async () => {
const { game, spawn, ticks, send } = await floor_game();
const [alice] = game.players();
alice.attributes.get(ATTRIBUTES.attack_damage)!.base = 5;
const mob = spawn(0.5, 100, 2.5);
ticks(5);
send(1, { type: "attack_entity", id: mob.id });
assertEquals(mob.health, 5, "4 attacks a second, so 5 ticks is full strength");
// right away is a fifth, a tick later a bit more. past the hurt cooldown, so it isn't just the difference
ticks(20);
alice.attack_strength_ticker = 0;
send(1, { type: "attack_entity", id: mob.id });
assertEquals(mob.health, 5 - 5 * (0.2 + 0.1 * 0.1 * 0.8));
// faster attack speed fills up sooner
ticks(10);
alice.attributes.get(ATTRIBUTES.attack_speed)!.base = 20;
alice.attack_strength_ticker = 1;
assertEquals(alice.attack_strength(), 1);
});
Deno.test("players hit each other, knocking them back and flashing red for everyone else", async () => {
const { game, ticks, send, join, take } = await floor_game();
join(2, "bob");
send(2, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0 });
const [alice, bob] = game.players();
ticks(1);
// long enough for a full strength attack
ticks(5);
take(1);
take(2);
+7 -1
View File
@@ -29,7 +29,13 @@ Deno.test("welcome lists what to download, and nothing happens until ready", asy
send(2, { type: "hello", name: "bob", protocol: PROTOCOL_VERSION });
const [welcome] = take(2);
assertEquals(welcome.type, "welcome");
assertEquals(welcome.mods.map((m: { id: string }) => m.id), ["bworld", "farmersdelight", "recipe_viewer"]);
assertEquals(welcome.mods.map((m: { id: string }) => m.id), [
"bworld",
"curios",
"artifacts",
"farmersdelight",
"recipe_viewer",
]);
assert(welcome.mods.every((m: { sha256: string; file: string }) => m.sha256 && m.file));
assert(!("players" in welcome) && !("changes" in welcome), "welcome shouldn't have world state");