240 lines
8.9 KiB
TypeScript
240 lines
8.9 KiB
TypeScript
// 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(20, 20, noop, noop);
|
|
assertEquals([vitals.food, vitals.saturation], [20, 4]);
|
|
|
|
vitals.saturation = 0;
|
|
vitals.add_exhaustion(4);
|
|
vitals.tick(20, 20, 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();
|
|
for (let i = 0; i < 10; i++) fast.tick(10, 20, 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.food = 18;
|
|
slow.saturation = 0;
|
|
for (let i = 0; i < 80; i++) slow.tick(10, 20, heal, damage);
|
|
assertEquals(healed, 1);
|
|
|
|
// starving: 1 every 4 seconds, never below 1 health
|
|
const starving = new Vitals();
|
|
starving.food = 0;
|
|
starving.saturation = 0;
|
|
let health = 2;
|
|
for (let i = 0; i < 80; i++) starving.tick(health, 20, heal, (amount) => health -= amount);
|
|
assertEquals(health, 1);
|
|
for (let i = 0; i < 400; i++) starving.tick(health, 20, heal, (amount) => health -= amount);
|
|
assertEquals(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.health, MAX_HEALTH - 7);
|
|
|
|
// the same fall into water
|
|
t.player.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.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.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].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.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<ItemRegistry>("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);
|
|
});
|
|
|
|
Deno.test("under water they run out of air, shown in bubbles, then drown", async () => {
|
|
const t = await setup();
|
|
t.game.set_block(0, 100, 0, "bworld:water");
|
|
t.game.set_block(0, 101, 0, "bworld:water");
|
|
t.take(1);
|
|
for (let i = 0; i < 100; i++) t.game.tick();
|
|
const air = t.take(1).filter((m: Msg) => m.type === "vitals").at(-1).air;
|
|
assert(air > 0 && air < 20, `air ${air}`);
|
|
assertEquals(t.player.health, MAX_HEALTH);
|
|
for (let i = 0; i < 300; i++) t.game.tick();
|
|
assert(t.player.health < MAX_HEALTH);
|
|
});
|
|
|
|
Deno.test("the effects that show an icon are sent to the player when they change", async () => {
|
|
const t = await setup();
|
|
const api = t.game.mods.player(t.player);
|
|
api.add_effect("bworld:speed", 100, { amplifier: 1 });
|
|
api.add_effect("bworld:poison", 100, { show_icon: false });
|
|
t.take(1);
|
|
t.game.tick();
|
|
const sent = t.take(1).filter((m: Msg) => m.type === "effects");
|
|
assertEquals(sent.at(-1).effects, [{ id: "bworld:speed", amplifier: 1, duration: 99 }]);
|
|
t.game.tick();
|
|
assert(!t.take(1).some((m: Msg) => m.type === "effects"), "not again while they only count down");
|
|
});
|
|
|
|
Deno.test("/effect gives you an effect, for 30 seconds or as many as you say", async () => {
|
|
const t = await setup();
|
|
t.send(1, { type: "chat", text: "/effect bworld:speed" });
|
|
assertEquals(t.player.get_effect("bworld:speed")?.duration, 600);
|
|
t.send(1, { type: "chat", text: "/effect bworld:regeneration 5" });
|
|
assertEquals(t.player.get_effect("bworld:regeneration")?.duration, 100);
|
|
});
|
|
|
|
Deno.test("/tp x y z teleports you there", async () => {
|
|
const t = await setup();
|
|
t.send(1, { type: "chat", text: "/tp 10 120.5 -3" });
|
|
assertEquals([t.player.x, t.player.y, t.player.z], [10, 120.5, -3]);
|
|
});
|
|
|
|
Deno.test("/fly toggles flying, which is sent to the player, and flying doesn't fall", async () => {
|
|
const t = await setup();
|
|
t.take(1);
|
|
t.send(1, { type: "chat", text: "/fly" });
|
|
assert(t.player.flying);
|
|
t.game.tick();
|
|
assert(t.take(1).some((m: Msg) => m.type === "flying" && m.flying), "the player hears they fly");
|
|
t.move(110);
|
|
for (let y = 109; y >= 100; y--) t.move(y);
|
|
assertEquals(t.player.health, MAX_HEALTH);
|
|
t.send(1, { type: "chat", text: "/fly" });
|
|
assert(!t.player.flying);
|
|
});
|