// mobs mods define in entities/.json, drawn with a bedrock model from models/.geo.json import { assert, assertEquals, assertStringIncludes } from "@std/assert"; import { entity_model_from_geo, validate_entity } from "$/common/mod_data.ts"; import { Mob } from "$/server/game/entity/mob.ts"; import { create_entity } from "$/server/game/entity/entity_types.ts"; import { 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 bad = entity_model_from_geo({ "minecraft:geometry": [{ description: { texture_width: 16, texture_height: 16 }, bones: [{ name: "leg", parent: "body", cubes: [per_face] }, { name: "body" }], }], }, "a:b"); assertEquals(bad.model, undefined); assert(bad.problems.some((p) => p.includes("parent body must be a bone before it")), String(bad.problems)); assert(bad.problems.some((p) => p.includes("per face uvs aren't supported")), String(bad.problems)); assertEquals(entity_model_from_geo({ format_version: "1.12.0" }, "a:b").model, undefined); }); 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 a number of blocks above 0, at most 16", "max_health must be a number, 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" or "scripted"', ]); }); 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 bworld:item" }); assertStringIncludes(take(1).find((m) => m.type === "chat").text, "There's no entity bworld: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 = take(1).filter((m) => m.type === "container" && m.container === "inventory").at(-1).items; 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), "bworld: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), "bworld:air"); });