Make players mobs too !

This commit is contained in:
2026-09-30 10:42:52 -03:00
parent 65f27d6099
commit 4fbcca6851
23 changed files with 314 additions and 132 deletions
+17
View File
@@ -0,0 +1,17 @@
import { test_game } from "../helpers.ts";
Deno.test("kb", async () => {
const t = await test_game("mods");
t.join(1, "alice");
t.join(2, "bob");
for (let x = -8; x <= 8; x++) for (let z = -8; z <= 8; z++) t.game.set_block(x, 99, z, "bworld:stone");
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
t.send(2, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0 });
t.game.tick();
t.take(2);
const bob = t.game.players()[1];
console.log("on_ground", bob.on_ground);
t.send(1, { type: "attack_entity", id: bob.id });
console.log("v after hurt", bob.vx, bob.vy, bob.vz, "health", bob.health);
t.game.tick();
console.log(t.take(2).filter((m) => m.type === "set_motion"));
});
+1 -1
View File
@@ -172,7 +172,7 @@ Deno.test("a stove is placed lit and heats the pot, coal relights it, and standi
// standing on it: 1 every 10 ticks
t.game.destroy_block(...POT);
t.send(1, { type: "move", x: 0.5, y: 101, z: 0.5, yaw: 0, pitch: 0 });
const health = () => t.game.players()[0].vitals.health;
const health = () => t.game.players()[0].health;
// (full hunger heals some of it back meanwhile, like minecraft)
const before = health();
t.ticks(40);
+47
View File
@@ -161,6 +161,53 @@ Deno.test("players hit entities they can reach, pushing them away", async () =>
send(1, { type: "attack_entity", id: "nothing" });
});
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);
take(1);
take(2);
send(1, { type: "attack_entity", id: bob.id });
assertEquals(bob.health, 19);
ticks(1);
const motion = take(2).find((m) => m.type === "set_motion");
assert(motion && motion.vz > 0, "bob's client is told he's knocked back, away from alice");
assert(take(1).some((m) => m.type === "entity_event" && m.id === bob.id && m.event === "hurt"));
send(1, { type: "attack_entity", id: alice.id });
assertEquals(alice.health, 20, "not yourself");
// past the hurt cooldown, and whatever full hunger healed back
ticks(20);
bob.health = 1;
send(1, { type: "attack_entity", id: bob.id });
assert(take(1).some((m) => m.type === "chat" && m.text === "bob was slain by alice"));
assertEquals(bob.health, 20, "respawned");
send(1, { type: "attack_entity", id: bob.id });
assertEquals(bob.health, 20, "too far away now, back at spawn");
});
Deno.test("mobs hurt players like anything else, and mods still get a say", async () => {
const { game, spawn, ticks } = await floor_game();
const alice = game.players()[0];
const causes: string[] = [];
game.mods.before.player_damage.for_mod("test").subscribe((event) => causes.push(event.cause));
const mob = spawn(0.5, 100, 1.5);
ticks(1);
assert(mob.do_hurt_target(alice));
assertEquals(alice.health, 18);
assertEquals(causes, ["mob_attack"]);
assertEquals(alice.last_hurt_by_mob, mob.id);
assertEquals(mob.do_hurt_target(alice), false, "not again straight away");
game.on_disconnect(1);
assertEquals(alice.alive, false, "gone, so mobs after her give up");
});
Deno.test("dying: it drops what it had, falls over for a second and is gone", async () => {
const { game, spawn, take, ticks } = await floor_game();
const mob = spawn(0.5, 100, 2.5);
+15 -17
View File
@@ -12,12 +12,12 @@ Deno.test("exhaustion eats saturation first, then hunger", () => {
const vitals = new Vitals();
const noop = () => {};
vitals.add_exhaustion(4.1);
vitals.tick(noop, noop);
vitals.tick(20, noop, noop);
assertEquals([vitals.food, vitals.saturation], [20, 4]);
vitals.saturation = 0;
vitals.add_exhaustion(4);
vitals.tick(noop, noop);
vitals.tick(20, noop, noop);
assertEquals([vitals.food, vitals.saturation], [19, 0]);
});
@@ -29,29 +29,27 @@ Deno.test("a full stomach heals, fast with saturation and slowly without, and st
// 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);
for (let i = 0; i < 10; i++) fast.tick(10, 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);
for (let i = 0; i < 80; i++) slow.tick(10, 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);
let health = 2;
for (let i = 0; i < 80; i++) starving.tick(health, heal, (amount) => health -= amount);
assertEquals(health, 1);
for (let i = 0; i < 400; i++) starving.tick(health, heal, (amount) => health -= amount);
assertEquals(health, 1);
assertEquals(hurt, 0);
});
@@ -84,20 +82,20 @@ Deno.test("falling more than 3 blocks hurts, 1 per block after that, and water b
// 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);
assertEquals(t.player.health, MAX_HEALTH - 7);
// the same fall into water
t.player.vitals.health = MAX_HEALTH;
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.vitals.health, MAX_HEALTH);
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.vitals.health = 15;
t.player.health = 15;
t.player.vitals.food = 12;
t.game.tick();
const vitals = t.take(1).filter((m) => m.type === "vitals").at(-1);
@@ -108,7 +106,7 @@ Deno.test("health and hunger are sent to the player when they change, and saved"
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].health, 15);
assertEquals(loaded.game.players()[0].vitals.food, 12);
});
@@ -144,7 +142,7 @@ Deno.test("dying drops everything, tells everyone, and respawns with full health
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]);
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 }]);
});