235 lines
11 KiB
TypeScript
235 lines
11 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// mobs mods define in entities/<name>.json, drawn with a bedrock model from models/<name>.geo.json
|
|
import { assert, assertAlmostEquals, assertEquals, assertNotEquals, assertStringIncludes } from "@std/assert";
|
|
import {
|
|
entity_category,
|
|
entity_model_from_geo,
|
|
type EntityBoneJson,
|
|
type EntityJson,
|
|
validate_entity,
|
|
} from "@/common/mod_data.ts";
|
|
import type { Texture } from "@/client/renderer/mod.ts";
|
|
import { IDENTITY } from "@/client/rendering/model_math.ts";
|
|
import { push_model } from "@/client/rendering/model_renderer.ts";
|
|
import { Mob } from "@/server/game/entity/mob.ts";
|
|
import { create_entity } from "@/server/game/entity/entity_types.ts";
|
|
import { mod_entity_factory } from "@/server/game/entity/mod_entity.ts";
|
|
import { container_items, test_game } from "./helpers.ts";
|
|
|
|
Deno.test("a bedrock geo.json becomes an entity model with only what the engine uses", () => {
|
|
const geo = JSON.parse(Deno.readTextFileSync("mods/bworld/models/chicken.geo.json"));
|
|
const { model, problems } = entity_model_from_geo(geo, "bworld:chicken");
|
|
assertEquals(problems, []);
|
|
assertEquals(model!.texture_width, 16);
|
|
assertEquals(model!.bones.map((bone) => bone.name), ["root", "left_feet", "right_feet", "body", "head"]);
|
|
assertEquals(model!.bones[4], {
|
|
name: "head",
|
|
parent: "root",
|
|
pivot: [1, 2, -1],
|
|
cubes: [
|
|
{ origin: [-1, 4, -2], size: [2, 2, 2], uv: [0, 6] },
|
|
{ origin: [-1, 4, -3], size: [2, 1, 1], uv: [8, 6] },
|
|
],
|
|
});
|
|
|
|
// per face uvs and parents that come later aren't supported
|
|
const per_face = { origin: [0, 0, 0], size: [1, 1, 1], uv: { north: { uv: [0, 0], uv_size: [1, 1] } } };
|
|
const geo_problems = (bones: unknown[]) => {
|
|
const geometry = { description: { texture_width: 16, texture_height: 16 }, bones };
|
|
const { model, problems } = entity_model_from_geo({ "minecraft:geometry": [geometry] }, "a:b");
|
|
assertEquals(model, undefined);
|
|
return problems;
|
|
};
|
|
const late_parent = geo_problems([{ name: "leg", parent: "body" }, { name: "body" }]);
|
|
assert(late_parent.some((p) => p.includes("parent body must be a bone before it")), String(late_parent));
|
|
const per_face_problems = geo_problems([{ name: "leg", cubes: [per_face] }]);
|
|
assert(per_face_problems.some((p) => p.includes("per face uvs aren't supported")), String(per_face_problems));
|
|
assertEquals(entity_model_from_geo({ format_version: "1.12.0" }, "a:b").model, undefined);
|
|
});
|
|
|
|
Deno.test("a cube's rotation turns it around its pivot like the same rotation on its bone", () => {
|
|
const pivot = [1, 2, 3];
|
|
const rotation = [30, -45, 60];
|
|
const cube = { origin: [0, 0, 0], size: [2, 3, 4], uv: [0, 0] };
|
|
const geometry = {
|
|
description: { texture_width: 16, texture_height: 16 },
|
|
bones: [
|
|
{ name: "turned_bone", pivot, rotation, cubes: [cube] },
|
|
{ name: "turned_cube", pivot: [0, 0, 0], cubes: [{ ...cube, pivot, rotation }] },
|
|
{ name: "still", pivot: [0, 0, 0], cubes: [cube] },
|
|
],
|
|
};
|
|
const { model, problems } = entity_model_from_geo({ "minecraft:geometry": [geometry] }, "a:b");
|
|
assertEquals(problems, []);
|
|
const points = (name: string) => {
|
|
const out: number[] = [];
|
|
const atlas = { width: 16, height: 16 } as Texture;
|
|
const region = { x: 0, y: 0, width: 16, height: 16 };
|
|
const only = (bone: EntityBoneJson) => bone.name === name;
|
|
push_model(model!, () => IDENTITY, atlas, region, (x, y, z) => [x, y, z], undefined, only, (x, y, z) => {
|
|
out.push(x, y, z);
|
|
});
|
|
return out;
|
|
};
|
|
const turned = points("turned_cube");
|
|
points("turned_bone").forEach((n, i) => assertAlmostEquals(turned[i], n, 1e-9));
|
|
assertNotEquals(turned, points("still"));
|
|
});
|
|
|
|
Deno.test("an entity needs a texture and a size", () => {
|
|
assertEquals(validate_entity({ id: "a:b", model: "a:b", texture: "a:b", width: 0.5, height: 1 }), []);
|
|
assertEquals(validate_entity({ id: "a:b", model: "a:b", texture: "a:b", width: 0, height: 1, max_health: -1 }), [
|
|
"width must be above 0",
|
|
"max_health must be 0 or more",
|
|
]);
|
|
});
|
|
|
|
Deno.test("an entity without a model is drawn as its texture, and a scripted one has no ai", () => {
|
|
assertEquals(validate_entity({ id: "a:b", texture: "a:b", behavior: "scripted", width: 0.25, height: 0.25 }), []);
|
|
assertEquals(validate_entity({ id: "a:b", texture: "a:b", behavior: "flying", width: 0.25, height: 0.25 }), [
|
|
'behavior must be "animal", "monster", "scripted" or a behavior id like "my_mod:thing"',
|
|
]);
|
|
assertEquals(validate_entity({ id: "a:b", texture: "a:b", behavior: "a:flying", width: 0.25, height: 0.25 }), []);
|
|
});
|
|
|
|
Deno.test("an entity can say it doesn't jump and how far it drops, in whole blocks", () => {
|
|
const entity = { id: "a:b", texture: "a:b", width: 1, height: 1 };
|
|
assertEquals(validate_entity({ ...entity, jumps: false, max_fall: 0 }), []);
|
|
assertEquals(validate_entity({ ...entity, max_fall: 1.5 }).length, 1);
|
|
assertEquals(validate_entity({ ...entity, max_fall: -1 }).length, 1);
|
|
});
|
|
|
|
Deno.test("an entity's category is monster for monsters unless it says otherwise", () => {
|
|
const entity = { id: "a:b", texture: "a:b", width: 1, height: 1 };
|
|
assertEquals(entity_category(entity), "creature");
|
|
assertEquals(entity_category({ ...entity, behavior: "monster" }), "monster");
|
|
assertEquals(entity_category({ ...entity, behavior: "a:flying", category: "monster" }), "monster");
|
|
assertEquals(entity_category({ ...entity, behavior: "monster", category: "creature" }), "creature");
|
|
assertEquals(validate_entity({ ...entity, category: "boss" }), ['category must be "monster" or "creature"']);
|
|
});
|
|
|
|
Deno.test("scripted entities stay put and aren't saved with the world", async () => {
|
|
const { game } = await test_game("mods");
|
|
const hook = create_entity(game.entities, "bworld:fishing_hook")!;
|
|
assert(!(hook instanceof Mob));
|
|
hook.set_position(0.5, 100, 0.5);
|
|
game.entities.add_entity(hook);
|
|
for (let i = 0; i < 20; i++) game.tick();
|
|
assertEquals([hook.x, hook.y, hook.z, hook.removed], [0.5, 100, 0.5, false]);
|
|
assertEquals(game.entities.save().filter((saved) => saved.id === "bworld:fishing_hook"), []);
|
|
});
|
|
|
|
Deno.test("/summon spawns a mod's entity where the player is", async () => {
|
|
const { join, take, send, game } = await test_game("mods");
|
|
join(1, "alice");
|
|
send(1, { type: "move", x: 3.5, y: 100, z: -2.5, yaw: 1, pitch: 0 });
|
|
take(1);
|
|
|
|
send(1, { type: "suggest", id: 1, text: "/summon bworld:chi" });
|
|
assertEquals(take(1).find((m) => m.type === "suggestions").suggestions, [{ text: "bworld:chicken" }]);
|
|
|
|
send(1, { type: "chat", text: "/summon bworld:chicken" });
|
|
const messages = take(1);
|
|
const added = messages.find((m) => m.type === "add_entity");
|
|
assertEquals(added.entity.type, "bworld:chicken");
|
|
assertEquals([added.entity.x, added.entity.y, added.entity.z], [3.5, 100, -2.5]);
|
|
|
|
const chicken = game.entities.get_entity(added.entity.id) as Mob;
|
|
assert(chicken instanceof Mob);
|
|
assertEquals([chicken.width, chicken.height, chicken.max_health], [0.3, 0.4, 4]);
|
|
// animals don't despawn
|
|
assertEquals(chicken.remove_when_far_away(1000), false);
|
|
|
|
send(1, { type: "chat", text: "/summon cubi:item" });
|
|
assertStringIncludes(take(1).find((m) => m.type === "chat").text, "There's no entity cubi:item");
|
|
});
|
|
|
|
Deno.test("solid blocks can't be placed where a mob or player is", async () => {
|
|
const { join, take, send, game } = await test_game("mods");
|
|
join(1, "alice");
|
|
game.set_block(1, 100, 1, "bworld:stone");
|
|
send(1, { type: "move", x: 3.5, y: 101, z: 3.5, yaw: 0, pitch: 0 });
|
|
send(1, { type: "chat", text: "/give bworld:stone 2" });
|
|
const inventory = container_items(take(1), "inventory");
|
|
send(1, { type: "select_slot", slot: inventory.findIndex((i: { id: string } | null) => i?.id === "bworld:stone") });
|
|
|
|
// the chicken's box pokes into the block above the stone, even from the one next to it
|
|
const chicken = create_entity(game.entities, "bworld:chicken")!;
|
|
chicken.set_position(2.1, 101, 1.5);
|
|
game.entities.add_entity(chicken);
|
|
send(1, { type: "use_block", x: 1, y: 100, z: 1, face: "top" });
|
|
assertEquals(game.world.get_block_id(1, 101, 1), "cubi:air");
|
|
|
|
chicken.set_position(2.5, 101, 1.5);
|
|
send(1, { type: "use_block", x: 1, y: 100, z: 1, face: "top" });
|
|
assertEquals(game.world.get_block_id(1, 101, 1), "bworld:stone");
|
|
|
|
// nor where a player is, the one placing it included
|
|
game.set_block(3, 100, 3, "bworld:stone");
|
|
send(1, { type: "use_block", x: 3, y: 100, z: 3, face: "top" });
|
|
assertEquals(game.world.get_block_id(3, 101, 3), "cubi:air");
|
|
});
|
|
|
|
// a mob with no goals, so it only walks where it's sent, at 0.5 100 0.5 on a stone floor that ends at x 3 with a deep
|
|
// pit beyond, and a wall at x 2 when wall is set. alice is there too, so the mob thinks
|
|
async function walker(fields: Partial<EntityJson>, wall = false) {
|
|
const t = await test_game("mods");
|
|
for (let x = -4; x <= 8; x++) {
|
|
for (let z = -4; z <= 4; z++) {
|
|
for (let y = 90; y < 104; y++) t.game.set_block(x, y, z, y === 99 && x <= 3 ? "bworld:stone" : "cubi:air");
|
|
if (wall && x === 2) t.game.set_block(x, 100, z, "bworld:stone");
|
|
}
|
|
}
|
|
t.join(1, "alice");
|
|
t.send(1, { type: "move", x: -3.5, y: 100, z: -3.5, yaw: 0, pitch: 0 });
|
|
const json: EntityJson = {
|
|
id: "test:walker",
|
|
texture: "bworld:chicken",
|
|
behavior: "test:still",
|
|
width: 0.6,
|
|
height: 1.8,
|
|
...fields,
|
|
};
|
|
const mob = mod_entity_factory(json, new Map([["test:still", () => {}]]))(t.game.entities) as Mob;
|
|
mob.set_position(0.5, 100, 0.5);
|
|
t.game.entities.add_entity(mob);
|
|
return { game: t.game, mob };
|
|
}
|
|
|
|
// the highest and lowest its feet got over ticks, walking to x
|
|
function walk(game: { tick(): void }, mob: Mob, x: number, ticks: number) {
|
|
let [lowest, highest] = [mob.y, mob.y];
|
|
for (let i = 0; i < ticks; i++) {
|
|
mob.move_control.move_to(x, 100, 0.5, 1);
|
|
game.tick();
|
|
lowest = Math.min(lowest, mob.y);
|
|
highest = Math.max(highest, mob.y);
|
|
}
|
|
return { lowest, highest };
|
|
}
|
|
|
|
Deno.test("a mob that doesn't jump stays down at a wall a mob that jumps gets onto", async () => {
|
|
const jumper = await walker({}, true);
|
|
assert(walk(jumper.game, jumper.mob, 2.5, 60).highest >= 101);
|
|
|
|
const stays = await walker({ jumps: false }, true);
|
|
assertAlmostEquals(walk(stays.game, stays.mob, 2.5, 60).highest, 100, 1e-6);
|
|
});
|
|
|
|
Deno.test("a mob that doesn't drop stops at an edge, unless something knocks it off", async () => {
|
|
const faller = await walker({});
|
|
assert(walk(faller.game, faller.mob, 6.5, 100).lowest < 99);
|
|
|
|
const { game, mob } = await walker({ max_fall: 0 });
|
|
assertAlmostEquals(walk(game, mob, 6.5, 100).lowest, 100, 1e-6);
|
|
// like a sneaking player, it can lean out until only the edge of its box is over the floor
|
|
assert(mob.x - mob.width / 2 < 4, `it got to x ${mob.x}`);
|
|
|
|
// hit from the side away from the pit
|
|
mob.knockback(0.4, -1, 0);
|
|
for (let i = 0; i < 40; i++) game.tick();
|
|
assert(mob.y < 99, `it's still at y ${mob.y}`);
|
|
});
|