388 lines
15 KiB
TypeScript
388 lines
15 KiB
TypeScript
import { assert, assertAlmostEquals, assertEquals } from "@std/assert";
|
|
import { ItemStack } from "$/common/inventory.ts";
|
|
import { Attribute, ATTRIBUTES } from "$/server/game/entity/attributes.ts";
|
|
import { EffectInstance, EFFECTS } from "$/server/game/entity/effects.ts";
|
|
import { DAMAGE } from "$/server/game/entity/entity.ts";
|
|
import { register_entity_type } from "$/server/game/entity/entity_types.ts";
|
|
import { Goal, GoalSelector, RandomStrollGoal } from "$/server/game/entity/goals.ts";
|
|
import { Mob } from "$/server/game/entity/mob.ts";
|
|
import { test_game } from "./helpers.ts";
|
|
|
|
// a mob for testing, walking like a pig
|
|
class TestMob extends Mob {
|
|
readonly type = "test:mob";
|
|
stroll = false;
|
|
|
|
override default_attributes(): ReturnType<Mob["default_attributes"]> {
|
|
return [...super.default_attributes(), [ATTRIBUTES.movement_speed, 0.25], [ATTRIBUTES.max_health, 10]];
|
|
}
|
|
|
|
override register_goals() {
|
|
if (this.stroll) this.goals.add_goal(1, new RandomStrollGoal(this, 1, 1));
|
|
}
|
|
|
|
override remove_when_far_away() {
|
|
return !this.tags.has("stays");
|
|
}
|
|
}
|
|
register_entity_type("test:mob", (level) => new TestMob(level));
|
|
|
|
// a game with alice standing on a stone floor at y 99, high above the generated terrain
|
|
async function floor_game() {
|
|
const t = await test_game("mods");
|
|
t.join(1, "alice");
|
|
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 });
|
|
const spawn = (x: number, y: number, z: number) => {
|
|
const mob = new TestMob(t.game.entities);
|
|
mob.set_position(x, y, z);
|
|
t.game.entities.add_entity(mob);
|
|
return mob;
|
|
};
|
|
const ticks = (n: number) => {
|
|
for (let i = 0; i < n; i++) t.game.tick();
|
|
};
|
|
return { ...t, spawn, ticks };
|
|
}
|
|
|
|
Deno.test("attribute modifiers add, then multiply the base, then multiply the total, like minecraft", () => {
|
|
const speed = new Attribute(ATTRIBUTES.movement_speed, 0.25);
|
|
assertEquals(speed.value, 0.25);
|
|
speed.add_modifier({ id: "a", amount: 0.25, operation: "add_value" });
|
|
speed.add_modifier({ id: "b", amount: 1, operation: "add_multiplied_base" });
|
|
speed.add_modifier({ id: "c", amount: 1, operation: "add_multiplied_base" });
|
|
speed.add_modifier({ id: "d", amount: 0.5, operation: "add_multiplied_total" });
|
|
// x = 0.5, y = 0.5 + 0.5 + 0.5 = 1.5, then * 1.5
|
|
assertEquals(speed.value, 2.25);
|
|
speed.add_modifier({ id: "a", amount: 0.75, operation: "add_value" });
|
|
assertEquals(speed.value, 4.5, "the same id replaces the modifier");
|
|
speed.remove_modifier("d");
|
|
assertEquals(speed.value, 3);
|
|
|
|
const health = new Attribute(ATTRIBUTES.max_health);
|
|
health.add_modifier({ id: "a", amount: -100, operation: "add_value" });
|
|
assertEquals(health.value, 1, "clamped to the attribute's minimum");
|
|
});
|
|
|
|
Deno.test("a stronger, shorter effect hides a weaker one, which takes over when it runs out", async () => {
|
|
const { spawn } = await floor_game();
|
|
const mob = spawn(0.5, 100, 0.5);
|
|
const base_speed = mob.attribute(ATTRIBUTES.movement_speed);
|
|
|
|
mob.add_effect(new EffectInstance(EFFECTS.speed, 100));
|
|
mob.add_effect(new EffectInstance(EFFECTS.speed, 10, { amplifier: 2 }));
|
|
const effect = mob.get_effect(EFFECTS.speed)!;
|
|
assertEquals([effect.amplifier, effect.duration, effect.hidden?.amplifier], [2, 10, 0]);
|
|
assertAlmostEquals(mob.attribute(ATTRIBUTES.movement_speed), base_speed * 1.6);
|
|
|
|
for (let i = 0; i < 10; i++) mob.tick_effects();
|
|
assertEquals([effect.amplifier, effect.duration], [0, 90], "the hidden one kept counting down");
|
|
assertAlmostEquals(mob.attribute(ATTRIBUTES.movement_speed), base_speed * 1.2);
|
|
|
|
for (let i = 0; i < 90; i++) mob.tick_effects();
|
|
assertEquals(mob.has_effect(EFFECTS.speed), false);
|
|
assertEquals(mob.attribute(ATTRIBUTES.movement_speed), base_speed);
|
|
});
|
|
|
|
Deno.test("effects: regeneration heals, poison never kills, health boost goes away with its health", async () => {
|
|
const { spawn } = await floor_game();
|
|
const mob = spawn(0.5, 100, 0.5);
|
|
|
|
mob.health = 2;
|
|
mob.add_effect(new EffectInstance(EFFECTS.poison, 200, { amplifier: 3 }));
|
|
for (let i = 0; i < 200; i++) mob.tick_effects();
|
|
assertEquals(mob.health, 1);
|
|
|
|
mob.add_effect(new EffectInstance(EFFECTS.regeneration, 100, { amplifier: 1 }));
|
|
for (let i = 0; i < 100; i++) mob.tick_effects();
|
|
// every 25 ticks at level 2
|
|
assertEquals(mob.health, 5);
|
|
|
|
mob.add_effect(new EffectInstance(EFFECTS.instant_health, 1));
|
|
assertEquals(mob.health, 9);
|
|
|
|
mob.add_effect(new EffectInstance(EFFECTS.health_boost, 20));
|
|
assertEquals(mob.max_health, 14);
|
|
mob.health = 14;
|
|
mob.remove_effect(EFFECTS.health_boost);
|
|
assertEquals([mob.max_health, mob.health], [10, 10]);
|
|
});
|
|
|
|
Deno.test("hurting: a weaker hit right after does nothing, a harder one only the difference", async () => {
|
|
const { spawn, take, ticks } = await floor_game();
|
|
const mob = spawn(0.5, 100, 2.5);
|
|
ticks(1);
|
|
take(1);
|
|
|
|
assert(mob.hurt(3, DAMAGE.generic));
|
|
assertEquals(mob.health, 7);
|
|
assert(take(1).some((m) => m.type === "entity_event" && m.id === mob.id && m.event === "hurt"));
|
|
assertEquals(mob.hurt(2, DAMAGE.generic), false);
|
|
assert(mob.hurt(5, DAMAGE.generic));
|
|
assertEquals(mob.health, 5);
|
|
|
|
ticks(11);
|
|
assert(mob.hurt(1, DAMAGE.generic), "hurt normally again after the cooldown");
|
|
assertEquals(mob.health, 4);
|
|
|
|
ticks(11);
|
|
mob.invulnerable = true;
|
|
assertEquals(mob.hurt(3, DAMAGE.generic), false);
|
|
assert(mob.hurt(1, DAMAGE.void), "falling out of the world hurts anyway");
|
|
});
|
|
|
|
Deno.test("hits push away from the attacker", async () => {
|
|
const { spawn, ticks } = await floor_game();
|
|
const mob = spawn(0.5, 100, 2.5);
|
|
ticks(2);
|
|
mob.hurt(5, { type: "physical", cause: "mob_attack", attacker: { id: "x", x: 0.5, y: 100, z: 0.5 } });
|
|
assertEquals(mob.health, 5);
|
|
assert(mob.vz > 0 && mob.vy > 0, "knocked back, away from the attacker at a smaller z");
|
|
});
|
|
|
|
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);
|
|
mob.set_item_slot("mainhand", new ItemStack("bworld:stick"));
|
|
mob.drop_chances.mainhand = 2;
|
|
mob.set_item_slot("head", new ItemStack("bworld:dirt"));
|
|
mob.drop_chances.head = 0;
|
|
ticks(1);
|
|
take(1);
|
|
|
|
mob.hurt(20, DAMAGE.generic);
|
|
assert(mob.dead);
|
|
assertEquals(game.item_entities().map((item) => item.item.type_id), ["bworld:stick"]);
|
|
assert(take(1).some((m) => m.type === "entity_event" && m.id === mob.id && m.event === "death"));
|
|
|
|
ticks(19);
|
|
assert(!mob.removed);
|
|
ticks(1);
|
|
assert(mob.removed);
|
|
assert(take(1).some((m) => m.type === "remove_entity" && m.id === mob.id));
|
|
});
|
|
|
|
Deno.test("mobs fall, and landing from high up hurts them past 3 blocks", async () => {
|
|
const { spawn, ticks } = await floor_game();
|
|
const mob = spawn(0.5, 110, 2.5);
|
|
ticks(40);
|
|
assert(mob.on_ground);
|
|
assertEquals(mob.y, 100);
|
|
assertEquals(mob.health, 10 - 7);
|
|
|
|
const floating = spawn(-2.5, 110, 2.5);
|
|
floating.no_ai = true;
|
|
ticks(20);
|
|
assertEquals(floating.y, 110, "no ai means not even falling");
|
|
});
|
|
|
|
Deno.test("mobs drown after their air runs out, unless they breathe water", async () => {
|
|
const { game, spawn, ticks } = await floor_game();
|
|
for (let y = 100; y <= 102; y++) game.set_block(3, y, 3, "bworld:water");
|
|
for (const [x, z] of [[2, 3], [4, 3], [3, 2], [3, 4]]) {
|
|
for (let y = 100; y <= 102; y++) game.set_block(x, y, z, "bworld:stone");
|
|
}
|
|
const mob = spawn(3.5, 100, 3.5);
|
|
mob.no_ai = true;
|
|
ticks(1);
|
|
assert(mob.in_water && mob.eyes_in_water);
|
|
ticks(299);
|
|
assertEquals(mob.air, 0);
|
|
assertEquals(mob.health, 10);
|
|
ticks(20);
|
|
assertEquals(mob.health, 8, "2 damage when the air gets to -20");
|
|
|
|
const breather = spawn(3.5, 100, 3.5);
|
|
breather.no_ai = true;
|
|
breather.add_effect(new EffectInstance(EFFECTS.water_breathing, -1));
|
|
ticks(340);
|
|
assertEquals(breather.health, 10);
|
|
});
|
|
|
|
Deno.test("the move control walks a mob to where it's told at minecraft's speed", async () => {
|
|
const { spawn, ticks } = await floor_game();
|
|
const mob = spawn(0.5, 100, -5.5);
|
|
ticks(2);
|
|
mob.move_control.move_to(0.5, 100, 5.5, 1);
|
|
ticks(20);
|
|
// 0.25 movement speed walks 0.25 * 0.25 * 0.98 / (1 - 0.546) blocks a tick, 2.7 a second
|
|
assertAlmostEquals(mob.z, -5.5 + 2.7, 0.5);
|
|
assertAlmostEquals(Math.abs(mob.yaw), Math.PI, 0.05, "facing south, +z");
|
|
ticks(100);
|
|
assert(Math.abs(mob.z - 5.5) < 0.5 && Math.abs(mob.x - 0.5) < 0.1, "got there");
|
|
});
|
|
|
|
Deno.test("goals: a more important goal takes a flag from a less important one", () => {
|
|
const log: string[] = [];
|
|
class Test extends Goal {
|
|
want = false;
|
|
constructor(readonly name: string) {
|
|
super();
|
|
this.flags = ["move"];
|
|
}
|
|
can_use() {
|
|
return this.want;
|
|
}
|
|
override start() {
|
|
log.push(`start ${this.name}`);
|
|
}
|
|
override stop() {
|
|
log.push(`stop ${this.name}`);
|
|
}
|
|
}
|
|
const selector = new GoalSelector();
|
|
const wander = new Test("wander");
|
|
const flee = new Test("flee");
|
|
selector.add_goal(5, wander);
|
|
selector.add_goal(1, flee);
|
|
wander.want = true;
|
|
selector.tick();
|
|
flee.want = true;
|
|
selector.tick();
|
|
assertEquals(selector.running, [flee]);
|
|
flee.want = false;
|
|
selector.tick();
|
|
assertEquals(log, ["start wander", "stop wander", "start flee", "stop flee", "start wander"]);
|
|
});
|
|
|
|
Deno.test("strolling mobs wander around on their own, within their home", async () => {
|
|
const { spawn, ticks } = await floor_game();
|
|
const mob = spawn(0.5, 100, 0.5);
|
|
mob.stroll = true;
|
|
mob.restrict_to(0.5, 100, 0.5, 4);
|
|
const visited = new Set<string>();
|
|
for (let i = 0; i < 400; i++) {
|
|
ticks(1);
|
|
visited.add(`${Math.floor(mob.x)},${Math.floor(mob.z)}`);
|
|
assert(Math.hypot(mob.x - 0.5, mob.z - 0.5) < 5.5, "stays near home");
|
|
}
|
|
assert(visited.size > 3, "went somewhere");
|
|
});
|
|
|
|
Deno.test("mobs that can pick up loot take items into their hand, and always drop them", async () => {
|
|
const { game, spawn, take, ticks } = await floor_game();
|
|
const mob = spawn(4.5, 100, 4.5);
|
|
mob.can_pick_up_loot = true;
|
|
game.spawn_item(4.5, 100, 4.5, new ItemStack("bworld:stick", 3));
|
|
ticks(1);
|
|
assertEquals(mob.main_hand_item?.to_data(), { id: "bworld:stick", count: 1 });
|
|
assertEquals(game.item_entities().map((item) => item.item.amount), [2]);
|
|
assertEquals(mob.drop_chances.mainhand, 2);
|
|
assert(mob.persistence_required);
|
|
const data = take(1).find((m) => m.type === "entity_data" && m.id === mob.id);
|
|
assertEquals(data?.data.equipment, { mainhand: { id: "bworld:stick", count: 1 } });
|
|
});
|
|
|
|
Deno.test("mobs far from every player despawn, named and persistent ones don't", async () => {
|
|
const { game, spawn, send } = await floor_game();
|
|
const mobs = [spawn(0.5, 100, 0.5), spawn(0.5, 100, 0.5), spawn(0.5, 100, 0.5), spawn(0.5, 100, 0.5)];
|
|
mobs[1].custom_name = "Bob";
|
|
mobs[2].persistence_required = true;
|
|
mobs[3].add_tag("stays");
|
|
send(1, { type: "move", x: 200.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
|
|
for (const mob of mobs) mob.check_despawn();
|
|
assertEquals(mobs.map((mob) => mob.removed), [true, false, false, false]);
|
|
game.entities.sync();
|
|
assertEquals(game.entities.all().length, 3);
|
|
});
|
|
|
|
Deno.test("clients hear about entities in their view distance, and forget them when they leave it", async () => {
|
|
const { game, spawn, take, send, join, ticks } = await floor_game();
|
|
const near = spawn(2.5, 100, 2.5);
|
|
const far = spawn(1000.5, 100, 0.5);
|
|
far.no_ai = true;
|
|
const joined = join(2, "bob");
|
|
assertEquals(joined.entities.map((e: { id: string }) => e.id), [near.id]);
|
|
assertEquals(joined.entities[0].kind, "entity");
|
|
assertEquals(joined.entities[0].type, "test:mob");
|
|
|
|
near.custom_name = "Nearby";
|
|
near.move_control.move_to(5.5, 100, 2.5, 1);
|
|
ticks(5);
|
|
const messages = take(2);
|
|
const move = messages.find((m) => m.type === "move_entity" && m.id === near.id);
|
|
assert(move && typeof move.yaw === "number" && typeof move.head_yaw === "number");
|
|
assert(messages.some((m) => m.type === "entity_data" && m.id === near.id && m.data.custom_name === "Nearby"));
|
|
assert(!messages.some((m) => m.id === far.id));
|
|
|
|
send(2, { type: "move", x: 1000.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
|
|
game.tick();
|
|
const moved = take(2);
|
|
assert(moved.some((m) => m.type === "remove_entity" && m.id === near.id));
|
|
assert(moved.some((m) => m.type === "add_entity" && m.entity.id === far.id));
|
|
});
|
|
|
|
Deno.test("entities are saved with everything about them, riders on what they ride", async () => {
|
|
const { game, spawn, ticks } = await floor_game();
|
|
const mob = spawn(0.5, 100, 2.5);
|
|
const rider = spawn(0.5, 100, 2.5);
|
|
ticks(2);
|
|
mob.custom_name = "Horse";
|
|
mob.add_tag("test");
|
|
mob.data.mood = "happy";
|
|
mob.health = 6;
|
|
mob.add_effect(new EffectInstance(EFFECTS.speed, 100));
|
|
mob.add_effect(new EffectInstance(EFFECTS.speed, 10, { amplifier: 1 }));
|
|
mob.attributes.get(ATTRIBUTES.physical_shielding)!.base = 4;
|
|
mob.set_item_slot("mainhand", new ItemStack("bworld:stick"));
|
|
mob.restrict_to(1, 2, 3, 8);
|
|
assert(rider.start_riding(mob));
|
|
ticks(1);
|
|
assertEquals([rider.x, rider.y, rider.z], [mob.x, mob.y + mob.height, mob.z]);
|
|
|
|
// an entity of a kind that doesn't exist anymore, from a mod that's gone
|
|
const saved = game.save();
|
|
const level = JSON.parse(saved.level);
|
|
assertEquals(level.version, 5);
|
|
level.entities.push({ id: "gone:thing", uuid: "1234", pos: [0, 100, 0], motion: [0, 0, 0], rotation: [0, 0] });
|
|
saved.level = JSON.stringify(level);
|
|
|
|
const loaded = await test_game("mods", saved);
|
|
const [horse, loaded_rider] = [loaded.game.entities.get_entity(mob.id), loaded.game.entities.get_entity(rider.id)];
|
|
assert(horse instanceof TestMob && loaded_rider instanceof TestMob);
|
|
assertEquals(horse.custom_name, "Horse");
|
|
assertEquals([...horse.tags], ["test"]);
|
|
assertEquals(horse.data, { mood: "happy" });
|
|
assertEquals(horse.health, 6);
|
|
const speed = horse.get_effect(EFFECTS.speed)!;
|
|
assertEquals([speed.amplifier, speed.duration, speed.hidden?.amplifier, speed.hidden?.duration], [1, 9, 0, 99]);
|
|
assertAlmostEquals(horse.attribute(ATTRIBUTES.movement_speed), 0.25 * 1.4);
|
|
assertEquals(horse.attribute(ATTRIBUTES.physical_shielding), 4);
|
|
assertEquals(horse.main_hand_item?.type_id, "bworld:stick");
|
|
assertEquals([horse.home, horse.home_radius], [{ x: 1, y: 2, z: 3 }, 8]);
|
|
assertEquals(loaded_rider.vehicle, horse);
|
|
assertEquals(horse.passengers, [loaded_rider]);
|
|
|
|
const resaved = JSON.parse(loaded.game.save().level);
|
|
assert(resaved.entities.some((e: { id: string }) => e.id === "gone:thing"), "kept for when the mod comes back");
|
|
});
|
|
|
|
Deno.test("items on the ground in a version 4 save load as entities", async () => {
|
|
const { game } = await floor_game();
|
|
game.spawn_item(0.5, 100, 0.5, new ItemStack("bworld:dirt", 7));
|
|
const saved = game.save();
|
|
const level = JSON.parse(saved.level);
|
|
level.version = 4;
|
|
level.entities = [{
|
|
x: 2.5,
|
|
y: 100,
|
|
z: 0.5,
|
|
vx: 0,
|
|
vy: 0,
|
|
vz: 0,
|
|
item: { id: "bworld:dirt", count: 7 },
|
|
age: 5,
|
|
pickup_delay: 3,
|
|
}];
|
|
saved.level = JSON.stringify(level);
|
|
|
|
const loaded = await test_game("mods", saved);
|
|
const [item] = loaded.game.item_entities();
|
|
assertEquals([item.x, item.y, item.z, item.age, item.pickup_delay], [2.5, 100, 0.5, 5, 3]);
|
|
assertEquals(item.item.to_data(), { id: "bworld:dirt", count: 7 });
|
|
});
|