// health, hunger and saturation, with minecraft's rules import { assert, assertAlmostEquals, assertEquals } from "@std/assert"; import { MAX_HEALTH, Vitals } from "$/server/game/vitals.ts"; import { test_game } from "./helpers.ts"; import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts"; import { ItemStack } from "$/common/inventory.ts"; // deno-lint-ignore no-explicit-any type Msg = any; Deno.test("exhaustion eats saturation first, then hunger", () => { const vitals = new Vitals(); const noop = () => {}; vitals.add_exhaustion(4.1); vitals.tick(noop, noop); assertEquals([vitals.food, vitals.saturation], [20, 4]); vitals.saturation = 0; vitals.add_exhaustion(4); vitals.tick(noop, noop); assertEquals([vitals.food, vitals.saturation], [19, 0]); }); Deno.test("a full stomach heals, fast with saturation and slowly without, and starving stops at half a heart", () => { let healed = 0; let hurt = 0; const heal = (amount: number) => healed += amount; const damage = (amount: number) => hurt += amount; // full hunger and saturation: every 10 ticks, up to a whole heart, paid for with exhaustion const fast = new Vitals(); fast.health = 10; for (let i = 0; i < 10; i++) fast.tick(heal, damage); assertAlmostEquals(healed, 5 / 6); assertEquals(fast.exhaustion, 5); // 18 hunger and no saturation: 1 every 4 seconds healed = 0; const slow = new Vitals(); slow.health = 10; slow.food = 18; slow.saturation = 0; for (let i = 0; i < 80; i++) slow.tick(heal, damage); assertEquals(healed, 1); // starving: 1 every 4 seconds, never below 1 health const starving = new Vitals(); starving.food = 0; starving.saturation = 0; starving.health = 2; for (let i = 0; i < 80; i++) starving.tick(heal, (amount) => starving.health -= amount); assertEquals(starving.health, 1); for (let i = 0; i < 400; i++) starving.tick(heal, (amount) => starving.health -= amount); assertEquals(starving.health, 1); assertEquals(hurt, 0); }); Deno.test("eating fills hunger, and saturation never goes above it", () => { const vitals = new Vitals(); vitals.food = 10; vitals.saturation = 0; vitals.eat(3, 0.6); assertEquals(vitals.food, 13); assertAlmostEquals(vitals.saturation, 3.6); vitals.eat(20, 1.2); assertEquals([vitals.food, vitals.saturation], [20, 20]); }); // a player standing on a stone floor at y 99 async function setup() { const t = await test_game("mods"); t.join(1, "alice"); for (let x = -3; x <= 3; x++) { for (let z = -3; z <= 3; z++) t.game.set_block(x, 99, z, "bworld:stone"); } const move = (y: number) => t.send(1, { type: "move", x: 0.5, y, z: 0.5, yaw: 0, pitch: 0 }); move(100); const player = t.game.players()[0]; return { ...t, move, player }; } Deno.test("falling more than 3 blocks hurts, 1 per block after that, and water breaks the fall", async () => { const t = await setup(); // up 10 blocks, then down in steps like a fall t.move(110); for (let y = 109; y >= 100; y--) t.move(y); assertEquals(t.player.vitals.health, MAX_HEALTH - 7); // the same fall into water t.player.vitals.health = MAX_HEALTH; t.game.set_block(0, 100, 0, "bworld:water"); t.move(110); for (let y = 109; y >= 100; y--) t.move(y); assertEquals(t.player.vitals.health, MAX_HEALTH); }); Deno.test("health and hunger are sent to the player when they change, and saved", async () => { const t = await setup(); t.take(1); t.player.vitals.health = 15; t.player.vitals.food = 12; t.game.tick(); const vitals = t.take(1).filter((m) => m.type === "vitals").at(-1); assertEquals([vitals.health, vitals.food], [15, 12]); t.game.tick(); assert(!t.take(1).some((m) => m.type === "vitals"), "only when they change"); t.game.on_disconnect(1); const loaded = await test_game("mods", t.game.save()); loaded.join(1, "alice"); assertEquals(loaded.game.players()[0].vitals.health, 15); assertEquals(loaded.game.players()[0].vitals.food, 12); }); Deno.test("right clicking food eats it when hungry, and not when full", async () => { const t = await setup(); t.send(1, { type: "chat", text: "/give carrot 2" }); const slot = t.player.inventory.to_data().findIndex((i) => i?.id === "bworld:carrot"); t.send(1, { type: "select_slot", slot }); t.send(1, { type: "use_item" }); assertEquals(t.player.inventory.get_item(slot)?.amount, 2, "full, so it isn't eaten"); t.player.vitals.food = 10; t.player.vitals.saturation = 0; t.send(1, { type: "use_item" }); assertEquals(t.player.vitals.food, 13); assertAlmostEquals(t.player.vitals.saturation, 3.6); assertEquals(t.player.inventory.get_item(slot)?.amount, 1); }); Deno.test("dying drops everything, tells everyone, and respawns with full health and hunger", async () => { const t = await setup(); const deaths: string[] = []; t.game.mods.after.player_death.for_mod("test").subscribe(({ player, cause }) => deaths.push(`${player.name} ${cause}`) ); t.send(1, { type: "chat", text: "/give coal 5" }); t.player.vitals.food = 3; t.take(1); t.game.damage_player(t.player, 25, "fall"); const messages = t.take(1); assertEquals(deaths, ["alice fall"]); assert(messages.some((m: Msg) => m.type === "chat" && m.text === "alice hit the ground too hard")); assert(messages.some((m: Msg) => m.type === "teleport")); assertEquals([t.player.vitals.health, t.player.vitals.food], [MAX_HEALTH, 20]); assert(t.player.inventory.to_data().every((i) => i === null)); assertEquals(t.game.item_entities().map((e) => e.item.to_data()), [{ id: "bworld:coal", count: 5 }]); }); Deno.test("dying takes 1 durability off every tool carried, and ones that run out break", async () => { const t = await setup(); const max = EverythingRegistry.get("items", "bworld:iron_pickaxe")!.tool!.durability; const pickaxe = new ItemStack("bworld:iron_pickaxe"); pickaxe.damage(max - 2); t.player.inventory.set_item(0, pickaxe); t.player.inventory.set_item(1, new ItemStack("bworld:coal", 5)); t.game.damage_player(t.player, 25, "fall"); const dropped = t.game.item_entities().map((e) => e.item); assertEquals(dropped.map((item) => item.durability()), [1, undefined]); assertEquals(dropped[1].to_data(), { id: "bworld:coal", count: 5 }); for (const e of t.game.item_entities()) e.remove(); t.player.inventory.set_item(0, dropped[0]); t.game.damage_player(t.player, 25, "fall"); assertEquals(t.game.item_entities(), []); }); Deno.test("mods can stop or change damage, and hurt, heal and feed players", async () => { const t = await setup(); t.game.mods.before.player_damage.for_mod("test").subscribe((event) => { if (event.cause === "fall") event.cancel = true; if (event.cause === "poison") event.amount = 1; }); const api = t.game.mods.player(t.player); t.game.damage_player(t.player, 10, "fall"); assertEquals(api.health, MAX_HEALTH); api.damage(10, "poison"); assertEquals(api.health, MAX_HEALTH - 1); api.heal(5); assertEquals(api.health, MAX_HEALTH); t.player.vitals.food = 2; api.feed(4, 0.5); assertEquals(api.food, 6); });