Animators and coilhead animations

This commit is contained in:
2026-11-21 21:54:02 -03:00
parent 69ad4b80d0
commit 0c711b8163
21 changed files with 960 additions and 34 deletions
+145
View File
@@ -8,6 +8,11 @@ import { sample_channel, sample_clip } from "@/client/rendering/animation.ts";
import { apply, bone_matrices, type BoneOffsets, IDENTITY, type Vec3 } from "@/client/rendering/model_math.ts";
import { create_mod } from "@/tools/new_mod.ts";
import { load_and_check } from "@/tools/check_mods.ts";
import { type AnimatedEntity, AnimationState, type Clip, type EntityAnimator } from "@/common/mod_api/client.ts";
import { animation_for, type AnimatorSource, entity_animators } from "@/client/rendering/entity_animators.ts";
import { EverythingRegistry } from "@/common/everything_registry.ts";
import type { EntityData } from "@/common/protocol.ts";
import { copy_dir, errors_during, test_game, test_mob } from "./helpers.ts";
// the one clip a file with one animation reads into
function one_clip(animation: unknown): ClipJson {
@@ -195,3 +200,143 @@ Deno.test("a clip moves bones in the model's pixels, and scales them around thei
const pivot = [-2, 3, 4];
assertEquals(apply(scaled.get("arm")!, ...point), point.map((n, i) => pivot[i] + (n - pivot[i]) * [2, 3, 0.5][i]));
});
Deno.test("an animation state counts seconds from when it started, and knows when a clip has played through", () => {
const once: Clip = { id: "a:b/once", length: 1, loop: false };
const state = new AnimationState();
assertEquals([state.started, state.seconds(100), state.finished(once, 100)], [false, 0, false]);
state.animate_when(true, 10);
state.animate_when(true, 15);
assertEquals(state.seconds(20), 0.5);
assert(!state.finished(once, 29));
assert(state.finished(once, 30));
assert(!state.finished({ ...once, loop: true }, 300));
state.animate_when(false, 40);
assertEquals(state.started, false);
state.start_if_stopped(50);
state.start_if_stopped(60);
assertEquals(state.seconds(70), 1);
});
// an entity the way an animator sees it, standing still and looking ahead
function animated(type: string, data: EntityData = {}): AnimatorSource & { data: EntityData } {
return {
id: "e1",
type,
data,
age: 0,
yaw: 0,
head_yaw: 0,
pitch: 0,
walk_animation: () => ({ position: 0, speed: 0 }),
attack_animation: () => 0,
};
}
// registers an animator for a new entity type, and a clip turning the arm up to 90 degrees over a second
function animator_type(name: string, create: (entity: AnimatedEntity) => EntityAnimator) {
const type = `test:${name}`;
entity_animators.set(type, { mod: "test", create });
const clip_id = `test:${name}/raise`;
if (!EverythingRegistry.get("animations", clip_id)) {
EverythingRegistry.register<ClipJson>("animations", clip_id, {
id: clip_id,
length: 1,
loop: false,
bones: { arm: { rotation: [key(0, [0, 0, 0]), key(1, [90, 0, 0])] } },
});
}
return { type, raise: { id: clip_id, length: 1, loop: false } };
}
Deno.test("an animator hears about each client data field as it changes, and the ones there from the start", () => {
const heard: string[] = [];
const { type } = animator_type("listener", () => ({ pose() {}, on_data: (field) => heard.push(field) }));
const entity = animated(type, { client_data: { mood: 1 }, glowing: true });
const animation = animation_for(entity)!;
assertEquals(heard, ["mood"]);
const before = entity.data;
entity.data = { client_data: { mood: 1, state: 2 } };
animation.data_changed(before);
assertEquals(heard, ["mood", "state"]);
const again = entity.data;
entity.data = { client_data: { state: 3 } };
animation.data_changed(again);
assertEquals(heard, ["mood", "state", "mood", "state"]);
assertEquals(animation_for(animated("test:no_animator")), undefined);
});
Deno.test("an animator's pose plays clips and moves bones", () => {
const { type, raise } = animator_type("poser", () => ({
pose(pose, time) {
pose.play(raise, time / 20, 0.5);
pose.move("arm", 1, 2, 3);
pose.scale("leg", 2, 2, 2);
pose.look("head");
},
}));
const entity = { ...animated(type), head_yaw: Math.PI / 2, pitch: -Math.PI / 4 };
const offsets = animation_for(entity)!.pose(10)!;
assertEquals(offsets.get("arm"), { rotation: [22.5, 0, 0], position: [1, 2, 3], scale: [1, 1, 1] });
assertEquals(offsets.get("leg")!.scale, [2, 2, 2]);
// mirrored like a model's own rotations
const head = offsets.get("head")!.rotation;
assertAlmostEquals(head[0], 45);
assertAlmostEquals(head[1], -90);
});
Deno.test("a clip plays from when its state started, and with walking as strongly as it walks", () => {
const state = new AnimationState();
state.start(10);
const { type, raise } = animator_type("stateful", (entity) => ({
pose(pose, time) {
pose.play_state(raise, state, time);
pose.play_walk(raise, entity, time);
},
}));
// half a second of clip walked, at half speed
const animation = animation_for({ ...animated(type), walk_animation: () => ({ position: 10, speed: 0.5 }) })!;
// a quarter of a second into the state is 22.5 degrees, and walking half of 45
assertAlmostEquals(animation.pose(15)!.get("arm")!.rotation[0], 22.5 + 22.5);
state.stop();
assertAlmostEquals(animation.pose(15)!.get("arm")!.rotation[0], 22.5);
});
Deno.test("an animator that throws is removed, and the entity is posed without it", async () => {
const { type } = animator_type("thrower", () => ({
pose(pose) {
pose.play({ id: "test:nowhere/nothing", length: 1, loop: false }, 0);
},
}));
const animation = animation_for(animated(type))!;
const errors = await errors_during(() => assertEquals(animation.pose(0), undefined));
assertEquals(errors.length, 1);
assertStringIncludes(errors[0], "[test] the animator of test:thrower e1 threw in pose");
assertStringIncludes(errors[0], "test:nowhere/nothing isn't a clip");
assertEquals(animation.pose(1), undefined);
});
Deno.test("a mob's client data goes to the players who see it, and is saved with it", async () => {
const dir = Deno.makeTempDirSync({ prefix: "bworld_client_data_" });
copy_dir("mods/dangerous_business", `${dir}/dangerous_business`);
const t = await test_game(dir);
t.join(1, "alice");
const mob = test_mob(t.game, {}, 0.5, 100, 0.5);
const ref = t.game.mods.mob(mob);
t.game.tick();
t.take(1);
ref.set_client_data("state", { mood: 3 });
assertEquals(ref.get_client_data("state"), { mood: 3 });
t.game.tick();
const sent = t.take(1).find((m) => m.type === "entity_data" && m.id === mob.id);
assertEquals(sent && "data" in sent ? sent.data.client_data : undefined, { state: { mood: 3 } });
const copy = test_mob(t.game, {}, 2.5, 100, 0.5);
copy.load(mob.save());
assertEquals(t.game.mods.mob(copy).get_client_data("state"), { mood: 3 });
ref.set_client_data("state", undefined);
assertEquals(mob.synced_data().client_data, undefined);
Deno.removeSync(dir, { recursive: true });
});
+30 -2
View File
@@ -9,7 +9,8 @@ import { ItemStack } from "@/common/inventory.ts";
import { get_state_value, set_state_value } from "@/common/utils.ts";
import { create_entity } from "@/server/game/entity/entity_types.ts";
import type { Mob } from "@/server/game/entity/mob.ts";
import { copy_dir, errors_during, test_game, test_mob } from "./helpers.ts";
import { copy_dir, errors_during, load_as_player, test_game, test_mob } from "./helpers.ts";
import { animation_for } from "@/client/rendering/entity_animators.ts";
const HALF = "dangerous_business:concrete_wall_slab";
const CONNECTS = "dangerous_business:floor_grate";
@@ -138,15 +139,18 @@ function spawn_coilhead(t: Awaited<ReturnType<typeof setup>>["t"]) {
Deno.test("a coilhead can't move while a player looks at it, and comes for them once they look away", async () => {
const { t } = await setup();
const coilhead = spawn_coilhead(t);
const coilhead = spawn_coilhead(t) as Mob;
for (let i = 0; i < 100; i++) t.game.tick();
assertAlmostEquals(coilhead.x, 0.5, 1e-6);
assertAlmostEquals(coilhead.z, -2.5, 1e-6);
// what its animator reads
assertEquals(coilhead.client_data.frozen, true);
// looking south, with it behind her
t.send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw: Math.PI, pitch: 0 });
for (let i = 0; i < 100; i++) t.game.tick();
assert(coilhead.z > 0, `it only got to z ${coilhead.z}`);
assertEquals(coilhead.client_data.frozen, undefined);
});
Deno.test("a player can't stop a coilhead through a wall", async () => {
@@ -251,3 +255,27 @@ Deno.test("a coilhead seen while it waits at a closed door stops waiting, and do
assert(!open(), "it opened the door while it was seen");
assertEquals(coilhead.navigation.blocked_by, undefined);
});
// last, since it loads the mod the way a player gets it, in place of the server's
Deno.test("a coilhead's head springs back and forth when it freezes, and stays still when it stands", async () => {
const dir = Deno.makeTempDirSync({ prefix: "dangerous_business_client_" });
copy_dir("mods/dangerous_business", `${dir}/dangerous_business`);
await load_as_player(dir);
const coilhead = (frozen: boolean) => ({
id: "e1",
type: COILHEAD,
data: frozen ? { client_data: { frozen: true } } : {},
age: 0,
yaw: 0,
head_yaw: 0,
pitch: 0,
walk_animation: () => ({ position: 0, speed: 0 }),
attack_animation: () => 0,
});
const head = (frozen: boolean, time: number) => animation_for(coilhead(frozen))!.pose(time)!.get("head")?.rotation;
assertNotEquals(head(true, 5), [0, 0, 0]);
assertEquals(head(false, 5), [0, 0, 0]);
// it's over in under a minute
assertEquals(head(true, 1200), head(false, 1200));
Deno.removeSync(dir, { recursive: true });
});
+20 -5
View File
@@ -12,7 +12,7 @@ import {
validate_entity,
} from "@/common/mod_data.ts";
import type { Texture } from "@/client/renderer/mod.ts";
import { IDENTITY } from "@/client/rendering/model_math.ts";
import { bone_matrices, 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";
@@ -54,7 +54,13 @@ function model_vertices(model: EntityModelJson, only?: (bone: EntityBoneJson) =>
const out: number[][] = [];
const atlas = { width: model.texture_width, height: model.texture_height } as Texture;
const region = { x: 0, y: 0, width: model.texture_width, height: model.texture_height };
push_model(model, () => IDENTITY, atlas, region, (x, y, z) => [x, y, z], undefined, only, (x, y, z, u, v) => {
push_model(model, bone_matrices(model, () => IDENTITY), atlas, region, (x, y, z) => [x, y, z], undefined, only, (
x,
y,
z,
u,
v,
) => {
out.push([x, y, z, u * model.texture_width, v * model.texture_height]);
});
return out;
@@ -114,9 +120,18 @@ Deno.test("a cube's rotation turns it around its pivot like the same rotation on
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);
});
push_model(
model!,
bone_matrices(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");