Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🎨 feat: Add Lia's Pixel Art Renderer (#16682)

Marco Berettaf36fa7c15f33
diff --git a/client/src/components/Lia/__tests__/art.spec.ts b/client/src/components/Lia/__tests__/art.spec.ts
new file mode 100644
index 0000000000..e0a2174346
--- /dev/null
+++ b/client/src/components/Lia/__tests__/art.spec.ts
@@ -0,0 +1,194 @@
+import type { FaceState } from '../engine/face';
+import {
+ SX,
+ SY,
+ ARMS,
+ POSES,
+ GRID_W,
+ GRID_H,
+ drawArm,
+ drawBody,
+ drawFeet,
+ drawScreen,
+ BUILTIN_SCREENS,
+} from '../engine/body';
+import { drawFace, EXPRESSIONS, SCREEN_W, SCREEN_H } from '../engine/face';
+
+const BASE: FaceState = {
+ face: 'neutral',
+ elapsed: 2000,
+ dx: 0,
+ dy: 0,
+ feather: true,
+ t: 1000,
+ blink: false,
+};
+
+/** Records every pixel rectangle a drawing call paints, as `color:x,y,w,h`. */
+function paint(draw: (c: CanvasRenderingContext2D) => void) {
+ const canvas = document.createElement('canvas');
+ const c = canvas.getContext('2d') as CanvasRenderingContext2D;
+ const rects: Array<{ key: string; x: number; y: number; w: number; h: number }> = [];
+ const fill = c.fillRect.bind(c);
+ c.fillRect = (x: number, y: number, w: number, h: number) => {
+ rects.push({ key: `${String(c.fillStyle)}:${x},${y},${w},${h}`, x, y, w, h });
+ fill(x, y, w, h);
+ };
+ /* The face's glow blob is the one thing drawn as an image, from a cache painted with fillRect. */
+ const image = c.drawImage.bind(c) as (...args: unknown[]) => void;
+ c.drawImage = ((...args: unknown[]) => {
+ const source = args[0] as { width: number; height: number };
+ const at = args.slice(1).join(',');
+ /* drawImage(image, dx, dy[, dw, dh]) or drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh). */
+ const [x, y, w = source.width, h = source.height] = (
+ args.length === 9 ? args.slice(5) : args.slice(1)
+ ) as number[];
+ rects.push({ key: `image:${source.width}x${source.height}@${at}`, x, y, w, h });
+ image(...args);
+ }) as CanvasRenderingContext2D['drawImage'];
+ draw(c);
+ return rects;
+}
+
+const signature = (face: string, extra: Partial<FaceState> = {}) =>
+ paint((c) => drawFace(c, { ...BASE, face, ...extra }))
+ .map((r) => r.key)
+ .join('|');
+
+/** Every direction Lia can look, including the diagonals. */
+const GAZES = [-1, 0, 1].flatMap((dx) => [-1, 0, 1].map((dy) => [dx, dy] as const));
+
+describe("Lia's pixel art", () => {
+ it('gives every expression its own face', () => {
+ const faces = EXPRESSIONS.map((e) => signature(e.key));
+ expect(new Set(faces).size).toBe(EXPRESSIONS.length);
+ });
+
+ it('keeps every face inside the screen, even when looking around', () => {
+ for (const e of EXPRESSIONS) {
+ for (const [dx, dy] of GAZES) {
+ for (const r of paint((c) => drawFace(c, { ...BASE, face: e.key, dx, dy }))) {
+ expect(r.x).toBeGreaterThanOrEqual(0);
+ expect(r.y).toBeGreaterThanOrEqual(0);
+ expect(r.x + r.w).toBeLessThanOrEqual(SCREEN_W);
+ expect(r.y + r.h).toBeLessThanOrEqual(SCREEN_H);
+ }
+ }
+ }
+ });
+
+ it('keeps every face inside the screen while its entrance plays', () => {
+ for (const e of EXPRESSIONS) {
+ for (let elapsed = 0; elapsed <= 600; elapsed += 15) {
+ for (const r of paint((c) => drawFace(c, { ...BASE, face: e.key, elapsed }))) {
+ expect(r.y).toBeGreaterThanOrEqual(0);
+ expect(r.y + r.h).toBeLessThanOrEqual(SCREEN_H);
+ }
+ }
+ }
+ });
+
+ it('closes open eyes when blinking and leaves closed ones alone', () => {
+ expect(signature('neutral', { blink: true })).not.toBe(signature('neutral'));
+ expect(signature('happy', { blink: true })).toBe(signature('happy'));
+ });
+
+ it('drops the sunglasses in over the first moments of the expression', () => {
+ expect(signature('cool', { elapsed: 0 })).not.toBe(signature('cool', { elapsed: 2000 }));
+ });
+
+ it('keeps the body, feet and every arm pose inside the canvas', () => {
+ const rects = paint((c) => {
+ drawBody(c, '#000');
+ for (const mode of [
+ 'stand',
+ 'walk',
+ 'run',
+ 'march',
+ 'slide',
+ 'tap',
+ 'dangle',
+ 'tiptoe',
+ ] as const) {
+ for (const t of [0, 150, 300, 450]) {
+ drawFeet(c, mode, t);
+ }
+ }
+ for (const fn of Object.values(ARMS)) {
+ for (const t of [0, 120, 250, 500, 1000]) {
+ const pose = fn(t, 0.5);
+ drawArm(c, 11, 35, pose.l);
+ drawArm(c, 52, 35, pose.r);
+ }
+ }
+ });
+ for (const r of rects) {
+ expect(r.x).toBeGreaterThanOrEqual(0);
+ expect(r.y).toBeGreaterThanOrEqual(0);
+ expect(r.x + r.w).toBeLessThanOrEqual(GRID_W);
+ expect(r.y + r.h).toBeLessThanOrEqual(GRID_H);
+ }
+ });
+
+ it('draws every built-in screen as its own picture, distinct from the face', () => {
+ const draw = (name: string, p: number) =>
+ paint((c) => drawScreen(c, name, p * 2000, p, BASE))
+ .map((r) => r.key)
+ .join('|');
+ const face = draw('face', 0.6);
+ const screens = [...BUILTIN_SCREENS].map((name) => draw(name, 0.6));
+ expect(new Set(screens).size).toBe(BUILTIN_SCREENS.size);
+ expect(screens).not.toContain(face);
+ });
+
+ it("keeps the glitch screen's shifted strips inside the screen", () => {
+ for (let t = 0; t < 7000; t += 70) {
+ const canvas = document.createElement('canvas');
+ const c = canvas.getContext('2d') as CanvasRenderingContext2D;
+ const copies: number[][] = [];
+ c.drawImage = ((...args: unknown[]) => {
+ if (args.length === 9) {
+ copies.push(args.slice(1) as number[]);
+ }
+ }) as CanvasRenderingContext2D['drawImage'];
+ drawScreen(c, 'glitch', t, 0.5, BASE);
+ for (const [sx, sy, sw, sh, x, y, w, h] of copies) {
+ /* The source is read from the whole canvas, so it must lie on the screen at (SX, SY). */
+ expect(sx).toBeGreaterThanOrEqual(SX);
+ expect(sy).toBeGreaterThanOrEqual(SY);
+ expect(sx + sw).toBeLessThanOrEqual(SX + SCREEN_W);
+ expect(sy + sh).toBeLessThanOrEqual(SY + SCREEN_H);
+ expect(x).toBeGreaterThanOrEqual(0);
+ expect(y).toBeGreaterThanOrEqual(0);
+ expect(x + w).toBeLessThanOrEqual(SCREEN_W);
+ expect(y + h).toBeLessThanOrEqual(SCREEN_H);
+ }
+ }
+ });
+
+ it('advances the progress screens as their step runs', () => {
+ const draw = (name: string, p: number) =>
+ paint((c) => drawScreen(c, name, p * 2000, p, BASE))
+ .map((r) => r.key)
+ .join('|');
+ expect(draw('loading', 0.2)).not.toBe(draw('loading', 0.9));
+ const boot = [0.2, 0.35, 0.6, 1].map((p) => draw('boot', p));
+ expect(new Set(boot).size).toBe(boot.length);
+ expect(draw('boot', 1).endsWith(draw('face', 0))).toBe(true);
+ });
+
+ it('ends the hide pose where the hidden and emerge poses take over', () => {
+ expect(POSES.hide(1000, 1).oy).toBe(POSES.hidden(0, 0).oy);
+ expect(POSES.emerge(0, 0).oy).toBe(POSES.hidden(0, 0).oy);
+ });
+
+ it('describes every pose as a small offset', () => {
+ for (const fn of Object.values(POSES)) {
+ for (const p of [0, 0.5, 1]) {
+ const v = fn(p * 1000, p);
+ expect(Math.abs(v.ox ?? 0)).toBeLessThanOrEqual(4);
+ expect(Math.abs(v.oy ?? 0)).toBeLessThanOrEqual(40);
+ }
+ }
+ });
+});
diff --git a/client/src/components/Lia/engine/body.ts b/client/src/components/Lia/engine/body.ts
new file mode 100644
index 0000000000..21edb41e90
--- /dev/null
+++ b/client/src/components/Lia/engine/body.ts
@@ -0,0 +1,415 @@
+import type { FaceState } from './face';
+import type { Ctx } from './pixels';
+import { drawFace, drawBlob, SCREEN_W, SCREEN_H, FEATHER_ROWS, FEATHER_PAL } from './face';
+import { C, rect, line, sprite, hash, toggle, easeOut } from './pixels';
+
+/* Lia's canvas is a 64 x 58 grid. The monitor's top-left corner sits at (BX, BY), which leaves
+ * room around it for arms and raised hands; feet touch the ground on row FOOT_Y. */
+export const GRID_W = 64;
+export const GRID_H = 58;
+export const BX = 12;
+export const BY = 20;
+export const SX = BX + 4;
+export const SY = BY + 4;
+export const FOOT_Y = 57;
+/** Rows from the feet up to the top edge of the monitor. */
+export const HEAD_ROWS = FOOT_Y - BY;
+
+export type FeetMode =
+ | 'stand'
+ | 'walk'
+ | 'run'
+ | 'march'
+ | 'slide'
+ | 'tap'
+ | 'dangle'
+ | 'tiptoe'
+ | 'hidden';
+
+export function drawBody(c: Ctx, led: string) {
+ const x = BX;
+ const y = BY;
+ rect(c, x, y + 1, 40, 28, C.O);
+ rect(c, x + 1, y, 38, 30, C.O);
+ rect(c, x + 1, y + 1, 38, 28, C.BODY);
+ rect(c, x + 2, y + 1, 36, 1, C.HI);
+ rect(c, x + 1, y + 2, 1, 25, C.HI);
+ rect(c, x + 38, y + 3, 1, 25, C.LO);
+ rect(c, x + 2, y + 28, 36, 1, C.LO);
+ for (const [a, b] of [
+ [1, 1],
+ [38, 1],
+ [1, 28],
+ [38, 28],
+ ]) {
+ rect(c, x + a, y + b, 1, 1, C.O);
+ }
+ rect(c, x + 3, y + 3, 34, 23, C.O);
+ for (const a of [6, 8, 10]) {
+ rect(c, x + a, y + 27, 1, 1, C.LO);
+ }
+ rect(c, x + 31, y + 27, 3, 1, led);
+ rect(c, x + 15, y + 30, 10, 2, C.O);
+ rect(c, x + 16, y + 30, 8, 2, C.LO);
+ rect(c, x + 10, y + 32, 20, 3, C.O);
+ rect(c, x + 11, y + 32, 18, 2, C.BODY);
+ rect(c, x + 11, y + 32, 18, 1, C.HI);
+}
+
+const STEP_MS = { walk: 150, run: 80, march: 220 } as const;
+
+export function drawFeet(c: Ctx, mode: FeetMode, t: number) {
+ if (mode === 'hidden') {
+ return;
+ }
+ const fy = BY + 35;
+ let l = 0;
+ let r = 0;
+ let lx = 0;
+ let rx = 0;
+ if (mode === 'walk' || mode === 'run' || mode === 'march') {
+ const ms = STEP_MS[mode];
+ const lift = mode === 'march' ? 2 : 1;
+ const phase = Math.floor(t / ms) % 4;
+ l = phase === 0 ? -lift : 0;
+ r = phase === 2 ? -lift : 0;
+ lx = phase === 0 ? 1 : 0;
+ rx = phase === 2 ? -1 : 0;
+ } else if (mode === 'slide') {
+ lx = toggle(t, 200) ? -1 : 1;
+ rx = -lx;
+ } else if (mode === 'tap') {
+ r = toggle(t, 180) ? -1 : 0;
+ } else if (mode === 'dangle') {
+ const k = toggle(t, 160);
+ l = 1;
+ r = 1;
+ lx = k ? -1 : 1;
+ rx = k ? 1 : -1;
+ } else if (mode === 'tiptoe') {
+ l = -1;
+ r = -1;
+ }
+ const foot = (x: number, dy: number) => {
+ rect(c, x, fy + dy, 4, 2, C.O);
+ rect(c, x + 1, fy + dy, 2, 1, C.LO);
+ };
+ foot(BX + 12 + lx, l);
+ foot(BX + 24 + rx, r);
+}
+
+/** A hand at [x, y], optionally bent at an elbow, relative to the monitor's top-left corner. */
+export type Hand =
+ | readonly [number, number]
+ | readonly [number, number, number, number]
+ | readonly [number, number, number, number, 'thumb'];
+export interface ArmPose {
+ l?: Hand;
+ r?: Hand;
+}
+type ArmFn = (t: number, p: number) => ArmPose;
+
+const ROBOT: readonly ArmPose[] = [
+ { l: [-5, 8, -5, 15], r: [46, 22, 46, 15] },
+ { l: [-7, 22, -5, 15], r: [45, 8, 46, 15] },
+ { l: [-5, 22], r: [44, 22] },
+];
+const cheer: ArmFn = (t) => ({ l: [-4, -2 + toggle(t, 200)], r: [43, -2 + toggle(t, 200)] });
+const hug: ArmFn = () => ({ l: [26, 22, 6, 25], r: [13, 22, 33, 25] });
+const hips: ArmFn = () => ({ l: [-1, 24, -5, 19], r: [40, 24, 44, 19] });
+const scratch: ArmFn = (t) => ({ r: [36 + toggle(t, 120), -1, 44, 6] });
+
+export const ARMS: Readonly<Record<string, ArmFn>> = {
+ none: () => ({}),
+ out: () => ({ l: [-5, 20], r: [44, 20] }),
+ holdBoth: () => ({ l: [12, 29, 2, 23], r: [27, 29, 37, 23] }),
+ wave: (t) => ({ r: [44 + toggle(t, 160) * 2, 2, 45, 10] }),
+ waveL: (t) => ({ l: [-5 - toggle(t, 160) * 2, 2, -6, 10] }),
+ waveBoth: (t) => ({
+ l: [-5 - toggle(t, 160) * 2, 2, -6, 10],
+ r: [44 + toggle(t, 160) * 2, 2, 45, 10],
+ }),
+ cheer,
+ clap: (t) => ({
+ l: [toggle(t, 130) ? 17 : 12, 27, 2, 23],
+ r: [toggle(t, 130) ? 22 : 27, 27, 37, 23],
+ }),
+ shrug: () => ({ l: [-5, 9, -3, 16], r: [44, 9, 42, 16] }),
+ pointL: () => ({ l: [-10, 15] }),
+ pointR: () => ({ r: [49, 15] }),
+ pointUp: () => ({ r: [44, -5] }),
+ pointDown: () => ({ r: [46, 30] }),
+ hips,
+ facepalm: () => ({ r: [24, 9, 38, 18] }),
+ scratch,
+ stretch: () => ({ l: [2, -7], r: [37, -7] }),
+ flex: (t) => ({ l: [-5, 6 - toggle(t, 300), -7, 14], r: [44, 6 - toggle(t, 300), 46, 14] }),
+ hug,
+ salute: () => ({ r: [33, 2, 44, 8] }),
+ thumbs: () => ({ r: [45, 11, 44, 16, 'thumb'] }),
+ thumbsBoth: () => ({ l: [-6, 11, -5, 16, 'thumb'], r: [45, 11, 44, 16, 'thumb'] }),
+ rub: (t) => ({ l: [12 + toggle(t, 120), 13, 2, 20], r: [27 - toggle(t, 120), 13, 37, 20] }),
+ cover: () => ({ l: [10, 10, 2, 18], r: [29, 10, 37, 18] }),
+ conduct: (t) => ({
+ r: [44 + Math.round(Math.cos(t / 220) * 3), 6 + Math.round(Math.sin(t / 220) * 4)],
+ }),
+ airguitar: (t) => ({ l: [6, 25 + toggle(t, 110), -2, 22], r: [45, 16] }),
+ jazz: (t) => ({ l: [-7 + toggle(t, 90), 3], r: [46 - toggle(t, 90), 3] }),
+ box: (t) => (toggle(t, 180) ? { l: [-11, 14], r: [43, 18] } : { l: [-4, 18], r: [50, 14] }),
+ swim: (t) => ({
+ l: [-4 + Math.round(Math.cos(t / 200) * 4), 12 + Math.round(Math.sin(t / 200) * 6)],
+ r: [43 - Math.round(Math.cos(t / 200) * 4), 12 - Math.round(Math.sin(t / 200) * 6)],
+ }),
+ type: (t) => ({ l: [12, 29 - toggle(t, 100), 2, 24], r: [27, 28 + toggle(t, 100), 37, 24] }),
+ drum: (t) => ({
+ l: [12, toggle(t, 150) ? 24 : 28, 2, 22],
+ r: [27, toggle(t, 150) ? 28 : 24, 37, 22],
+ }),
+ reach: (t) => ({ r: [43, -10 + toggle(t, 200)] }),
+ reachBoth: (t) => ({ l: [-3, -9 + toggle(t, 200)], r: [42, -9 + toggle(t, 200)] }),
+ disco: (t) => (toggle(t, 350) ? { r: [47, -3], l: [-4, 25] } : { l: [-7, -3], r: [44, 25] }),
+ robot: (t) => ROBOT[Math.floor(t / 300) % ROBOT.length],
+ tpose: () => ({ l: [-11, 15], r: [50, 15] }),
+ flail: (t) => (toggle(t, 90) ? { l: [-7, 4], r: [46, 22] } : { l: [-7, 22], r: [46, 4] }),
+ bow: () => ({ l: [4, 31], r: [35, 31] }),
+ shield: () => ({ l: [10, 14, 2, 22], r: [29, 14, 37, 22] }),
+ catch: () => ({ l: [6, 2, -2, 10], r: [33, 2, 41, 10] }),
+ pray: () => ({ l: [19, 26, 4, 24], r: [20, 26, 35, 24] }),
+ knock: (t) => ({ r: [36, 6 + toggle(t, 160) * 2, 44, 12] }),
+ fan: (t) => ({ r: [36 + toggle(t, 100) * 3, 12, 44, 16] }),
+ jacks: (t) => (toggle(t, 250) ? cheer(t, 0) : { l: [-3, 28], r: [42, 28] }),
+ macarena: (t, p) =>
+ [
+ { r: [49, 15] as Hand },
+ { l: [-10, 15] as Hand, r: [49, 15] as Hand },
+ hug(t, p),
+ scratch(t, p),
+ hips(t, p),
+ ][Math.floor(t / 450) % 5],
+};
+
+export function drawArm(c: Ctx, shoulderX: number, shoulderY: number, hand?: Hand) {
+ if (!hand) {
+ return null;
+ }
+ const hx = BX + hand[0];
+ const hy = BY + hand[1];
+ if (hand.length >= 4) {
+ const ex = BX + (hand[2] as number);
+ const ey = BY + (hand[3] as number);
+ line(c, shoulderX, shoulderY, ex, ey, C.O);
+ line(c, ex, ey, hx, hy, C.O);
+ } else {
+ line(c, shoulderX, shoulderY, hx, hy, C.O);
+ }
+ rect(c, hx - 1, hy - 1, 2, 2, C.O);
+ if (hand[4] === 'thumb') {
+ rect(c, hx - 1, hy - 3, 1, 2, C.O);
+ }
+ return [hx, hy] as const;
+}
+
+/** A body offset in grid pixels plus rotation (degrees) and scale. */
+export interface PoseOffset {
+ ox?: number;
+ oy?: number;
+ rot?: number;
+ sx?: number;
+ sy?: number;
+}
+type PoseFn = (t: number, p: number) => PoseOffset;
+
+export const POSES: Readonly<Record<string, PoseFn>> = {
+ tilt: () => ({ rot: -8 }),
+ tiltR: () => ({ rot: 8 }),
+ lean: () => ({ rot: 10 }),
+ bounce: (t) => ({ oy: -Math.round(Math.abs(Math.sin(t / 130)) * 3) }),
+ hop: (t) => ({ oy: -Math.round(Math.abs(Math.sin(t / 180)) * 6) }),
+ jump: (_t, p) => ({
+ oy: -Math.round(Math.sin(Math.PI * p) * 14),
+ sy: p < 0.1 || p > 0.92 ? 0.9 : 1,
+ }),
+ spin: (t) => {
+ const cos = Math.cos(t / 110);
+ const sx =
+ Math.abs(cos) < 0.25
+ ? 0.2 * Math.sign(cos || 1)
+ : (Math.sign(cos) * Math.round(Math.abs(cos) * 4)) / 4;
+ return { sx };
+ },
+ flip: (_t, p) => ({
+ rot: Math.round((p * 360) / 15) * 15,
+ oy: -Math.round(Math.sin(Math.PI * p) * 12),
+ }),
+ shake: (t) => ({ ox: toggle(t, 50) ? 1 : -1 }),
+ shiver: (t) => ({ ox: toggle(t, 30) }),
+ sway: (t) => ({ rot: Math.round(Math.sin(t / 300) * 3) * 2 }),
+ wiggle: (t) => ({ rot: Math.round(Math.sin(t / 90) * 2) * 3 }),
+ squash: (t) => (toggle(t, 200) ? { sy: 0.9, sx: 1.08 } : { sy: 1.05, sx: 0.96 }),
+ bow: () => ({ sy: 0.88, oy: 2 }),
+ sit: () => ({ oy: 3 }),
+ slump: () => ({ oy: 2, rot: -4 }),
+ lie: () => ({ rot: -90, oy: -3 }),
+ fall: (_t, p) => ({ rot: -Math.round(Math.min(1, p * 1.6) * 90) }),
+ getup: (_t, p) => ({ rot: -Math.round((1 - p) * 90) }),
+ float: (t) => ({ oy: -(6 + Math.round(Math.sin(t / 400) * 2)) }),
+ dizzy: (t) => ({ rot: Math.round(Math.sin(t / 160) * 3) * 4 }),
+ headbang: (t) => (toggle(t, 140) ? { sy: 0.92, rot: 4 } : {}),
+ robot: (t) => ({ rot: [0, 6, 0, -6][Math.floor(t / 300) % 4] }),
+ disco: (t) => (toggle(t, 350) ? { rot: 8, ox: 2 } : { rot: -8, ox: -2 }),
+ twist: (t) => ({
+ sx: [1, 0.9, 0.8, 0.9][Math.floor(t / 120) % 4],
+ rot: Math.round(Math.sin(t / 120) * 2) * 3,
+ }),
+ tiptoe: () => ({ oy: -1 }),
+ hide: (_t, p) => ({ oy: Math.round(Math.min(1, p * 2) * 40) }),
+ hidden: () => ({ oy: 40 }),
+ emerge: (_t, p) => ({ oy: Math.round((1 - easeOut(p)) * 40) }),
+ stomp: (t) => (toggle(t, 250) ? { oy: -2 } : {}),
+ nod: (t) => (toggle(t, 200) ? { sy: 0.94, oy: 1 } : {}),
+ stretchUp: (_t, p) => ({
+ sy: 1 + 0.12 * Math.sin(Math.PI * p),
+ sx: 1 - 0.06 * Math.sin(Math.PI * p),
+ }),
+ sneeze: (_t, p) =>
+ p < 0.7 ? { rot: -Math.round(p * 10), sy: 1 + p * 0.1 } : { rot: 12, ox: 3, sy: 0.9 },
+ lookAround: (t) => ({ rot: [0, -6, 0, 6][Math.floor(t / 500) % 4] }),
+ crouch: () => ({ sy: 0.86 }),
+ collapse: (_t, p) => ({ sy: Math.max(0.04, 1 - p), sx: 1 + p * 0.3 }),
+ expand: (_t, p) => ({ sy: Math.max(0.04, p), sx: 1.3 - p * 0.3 }),
+ wobble: (t) => ({ rot: Math.round(Math.sin(t / 200) * 2) * 4 }),
+};
+
+const HEART = [
+ '.##...##.',
+ '####.####',
+ '#########',
+ '#########',
+ '.#######.',
+ '..#####..',
+ '...###...',
+ '....#....',
+];
+
+/** Screens that are part of Lia's own behavior (booting, sending, crashing). */
+const BUILTIN: Readonly<Record<string, (c: Ctx, t: number, p: number, fs: FaceState) => void>> = {
+ off: (c) => rect(c, 0, 0, SCREEN_W, SCREEN_H, C.BLACK),
+ boot: (c, t, p, fs) => {
+ rect(c, 0, 0, SCREEN_W, SCREEN_H, C.BLACK);
+ if (p < 0.12) {
+ if (toggle(t, 80)) {
+ rect(c, 16, 10, 1, 1, C.W);
+ }
+ } else if (p < 0.3) {
+ const w = Math.round(((p - 0.12) / 0.18) * 16);
+ rect(c, 16 - w, 10, w * 2, 1, C.W);
+ } else if (p < 0.42) {
+ const h = Math.round(((p - 0.3) / 0.12) * 10);
+ rect(c, 0, 10 - h, SCREEN_W, h * 2 + 1, '#cfeeff');
+ } else {
+ const level = Math.min(1, (p - 0.42) / 0.4);
+ if (level >= 0.99) {
+ drawFace(c, fs);
+ } else {
+ rect(c, 0, 0, SCREEN_W, SCREEN_H, C.SCREEN);
+ drawBlob(c, level);
+ }
+ }
+ },
+ file: (c) => {
+ rect(c, 0, 0, SCREEN_W, SCREEN_H, C.SCREEN);
+ drawBlob(c, 0.35);
+ rect(c, 10, 2, 12, 17, C.O);
+ rect(c, 11, 3, 10, 15, C.W);
+ rect(c, 18, 3, 3, 3, '#cfe0ea');
+ rect(c, 18, 5, 3, 1, C.O);
+ rect(c, 18, 3, 1, 3, C.O);
+ rect(c, 13, 8, 6, 1, C.GLOW);
+ rect(c, 13, 10, 6, 1, C.GLOW);
+ rect(c, 13, 12, 4, 1, C.GLOW);
+ rect(c, 13, 15, 1, 1, C.EYE);
+ rect(c, 18, 15, 1, 1, C.EYE);
+ rect(c, 15, 16, 2, 1, C.EYE);
+ },
+ loading: (c, _t, p) => {
+ rect(c, 0, 0, SCREEN_W, SCREEN_H, C.SCREEN);
+ rect(c, 4, 8, 24, 7, C.BLOB);
+ rect(c, 5, 9, 22, 5, C.SCREEN);
+ for (let i = 0; i < Math.min(7, Math.floor(p * 8)); i++) {
+ rect(c, 6 + i * 3, 10, 2, 3, C.GLOW);
+ }
+ },
+ heart: (c, t) => {
+ rect(c, 0, 0, SCREEN_W, SCREEN_H, C.SCREEN);
+ if (toggle(t, 280) === 0) {
+ for (const [ox, oy] of [
+ [-1, 0],
+ [1, 0],
+ [0, -1],
+ [0, 1],
+ ]) {
+ sprite(c, HEART, 12 + ox, 6 + oy, { '#': C.HEART_HI });
+ }
+ }
+ sprite(c, HEART, 12, 6, { '#': C.HEART });
+ rect(c, 13, 7, 1, 1, C.W);
+ },
+ bsod: (c, t) => {
+ rect(c, 0, 0, SCREEN_W, SCREEN_H, C.BSOD);
+ rect(c, 4, 4, 2, 2, C.W);
+ rect(c, 4, 8, 2, 2, C.W);
+ rect(c, 10, 3, 2, 1, C.W);
+ rect(c, 9, 4, 1, 6, C.W);
+ rect(c, 10, 10, 2, 1, C.W);
+ rect(c, 4, 14, 20, 1, '#bcd0ff');
+ rect(c, 4, 16, 14, 1, '#bcd0ff');
+ rect(c, 4, 18, Math.min(22, 4 + (Math.floor(t / 120) % 19)), 1, C.W);
+ },
+ glitch: (c, t, _p, fs) => {
+ drawFace(c, { ...fs, face: 'startled' });
+ const step = Math.floor(t / 70);
+ for (let i = 0; i < 4; i++) {
+ const y = Math.floor(hash(step * 7 + i) * 19);
+ const dx = Math.floor(hash(step * 13 + i) * 7) - 3;
+ /* Shorten the strip by its shift so it never spills onto the bezel. */
+ const w = SCREEN_W - Math.abs(dx);
+ c.drawImage(c.canvas, SX + Math.max(0, -dx), SY + y, w, 2, Math.max(0, dx), y, w, 2);
+ }
+ for (let i = 0; i < 12; i++) {
+ rect(
+ c,
+ Math.floor(hash(step * 31 + i) * SCREEN_W),
+ Math.floor(hash(step * 17 + i * 5) * SCREEN_H),
+ 1,
+ 1,
+ i % 2 ? '#ff4fd8' : '#4ff0ff',
+ );
+ }
+ },
+ saver: (c, t) => {
+ rect(c, 0, 0, SCREEN_W, SCREEN_H, C.BLACK);
+ const tri = (v: number) => {
+ const f = v % 2;
+ return f < 1 ? f : 2 - f;
+ };
+ sprite(
+ c,
+ FEATHER_ROWS,
+ Math.round(tri(t / 1700) * 24),
+ Math.round(tri(t / 1150) * 14),
+ FEATHER_PAL,
+ );
+ },
+};
+
+export const BUILTIN_SCREENS: ReadonlySet<string> = new Set(Object.keys(BUILTIN));
+
+/** Draws the screen content in screen-local coordinates; `face` falls through to the face. */
+export function drawScreen(c: Ctx, name: string, t: number, p: number, fs: FaceState) {
+ const builtin = BUILTIN[name];
+ if (builtin) {
+ builtin(c, t, p, fs);
+ } else {
+ drawFace(c, fs);
+ }
+}
diff --git a/client/src/components/Lia/engine/face.ts b/client/src/components/Lia/engine/face.ts
new file mode 100644
index 0000000000..01b1a3e0e5
--- /dev/null
+++ b/client/src/components/Lia/engine/face.ts
@@ -0,0 +1,689 @@
+import type { Ctx } from './pixels';
+import { C, rect, sprite, dither } from './pixels';
+
+/* The face is drawn in screen-local coordinates: the CRT screen is 32 x 21 pixels. */
+export const SCREEN_W = 32;
+export const SCREEN_H = 21;
+
+export const FEATHER_ROWS = [
+ '.....ooo',
+ '...ooaao',
+ '..oaabao',
+ '.oaabao.',
+ '.oabaoo.',
+ 'o.ooo...',
+ 'o.......',
+];
+export const FEATHER_PAL = { o: C.O, a: C.FEATHER, b: C.FEATHER_HI };
+
+type Eye =
+ | 'normal'
+ | 'blink'
+ | 'half'
+ | 'tired'
+ | 'closed'
+ | 'happy'
+ | 'sad'
+ | 'big'
+ | 'sparkle'
+ | 'dot'
+ | 'star'
+ | 'heart'
+ | 'question'
+ | 'x'
+ | 'spiral'
+ | 'wide'
+ | 'dash'
+ | 'equals'
+ | 'squeeze'
+ | 'dollar'
+ | 'angry'
+ | 'side'
+ | 'tear'
+ | 'cry';
+type Mouth =
+ | 'smile'
+ | 'grin'
+ | 'beam'
+ | 'laugh'
+ | 'o'
+ | 'gasp'
+ | 'tiny'
+ | 'sleep'
+ | 'flat'
+ | 'wavy'
+ | 'frown'
+ | 'pout'
+ | 'smirk'
+ | 'tongue'
+ | 'cat'
+ | 'teeth'
+ | 'kiss'
+ | 'whistle'
+ | 'drool'
+ | 'chew';
+type Brow = 'worried' | 'angry' | 'raised' | 'skeptic' | 'focus';
+type Extra =
+ | 'blush'
+ | 'sweat'
+ | 'vein'
+ | 'tears'
+ | 'sparkles'
+ | 'shades'
+ | 'glasses'
+ | 'monocle'
+ | 'mustache'
+ | 'sick'
+ | 'bandage'
+ | 'zzz'
+ | 'think';
+
+export interface FaceState {
+ face: string;
+ /** Milliseconds since this expression started; drives entrances like the sunglasses drop. */
+ elapsed: number;
+ dx: number;
+ dy: number;
+ feather: boolean;
+ t: number;
+ blink: boolean;
+}
+
+export interface Expression {
+ key: string;
+ /** Translation key for the feeling, like `com_ui_lia_feel_happy`. */
+ label: `com_ui_lia_feel_${string}`;
+ eyes: Eye | readonly [Eye, Eye];
+ mouth: Mouth;
+ brows?: Brow;
+ extras: readonly Extra[];
+ /** Arm pose, effect and body pose that suit the expression when Lia plays it as an emote. */
+ arms?: string;
+ pose?: string;
+}
+
+const BLINKABLE: ReadonlySet<Eye> = new Set([
+ 'normal',
+ 'big',
+ 'sparkle',
+ 'wide',
+ 'dot',
+ 'tear',
+ 'sad',
+ 'angry',
+]);
+
+export function blobDistance(x: number, y: number) {
+ const dx = (x + 0.5 - 16.5) / 12.5;
+ const dy = (y + 0.5 - 10.5) / 8.5;
+ return dx * dx + dy * dy;
+}
+
+/** The glowing face blob, optionally dithered in at `level` (used by the boot sequence). */
+export function drawBlob(c: Ctx, level: number) {
+ for (let y = 0; y < SCREEN_H; y++) {
+ for (let x = 0; x < SCREEN_W; x++) {
+ const d = blobDistance(x, y);
+ let color: string | null = null;
+ if (d < 0.78) {
+ color = C.BLOB;
+ } else if (d < 1) {
+ color = (x + y) & 1 ? C.BLOB : C.GLOW;
+ } else if (d < 1.45) {
+ color = dither(x, y, ((1.45 - d) / 0.45) * 0.7) ? C.GLOW : null;
+ } else if (x === 0 || x === SCREEN_W - 1 || y === 0 || y === SCREEN_H - 1) {
+ color = C.DEEP;
+ }
+ if (color && (level >= 1 || dither(x, y, level))) {
+ rect(c, x, y, 1, 1, color);
+ }
+ }
+ }
+}
+
+function drawEye(c: Ctx, kind: Eye, x: number, y: number, left: boolean, t: number) {
+ const E = C.EYE;
+ const round = (yy: number) => {
+ rect(c, x, yy, 1, 1, C.BLOB);
+ rect(c, x + 3, yy, 1, 1, C.BLOB);
+ };
+ switch (kind) {
+ case 'blink':
+ rect(c, x, y + 2, 4, 1, E);
+ return;
+ case 'half':
+ rect(c, x, y + 2, 4, 2, E);
+ round(y + 3);
+ return;
+ case 'tired':
+ rect(c, x, y + 2, 4, 2, E);
+ round(y + 3);
+ rect(c, x, y + 5, 4, 1, C.GLOW);
+ return;
+ case 'closed':
+ rect(c, x, y + 1, 1, 1, E);
+ rect(c, x + 1, y + 2, 2, 1, E);
+ rect(c, x + 3, y + 1, 1, 1, E);
+ return;
+ case 'happy':
+ rect(c, x, y + 2, 1, 1, E);
+ rect(c, x + 1, y + 1, 2, 1, E);
+ rect(c, x + 3, y + 2, 1, 1, E);
+ return;
+ case 'sad':
+ case 'angry': {
+ /* Both are a normal eye with one top corner cut away; the side decides the mood. */
+ const outer = kind === 'sad' ? left : !left;
+ rect(c, x, y, 4, 4, E);
+ round(y + 3);
+ rect(c, outer ? x : x + 2, y, 2, 1, C.BLOB);
+ rect(c, outer ? x : x + 3, y + 1, 1, 1, C.BLOB);
+ rect(c, outer ? x + 2 : x + 1, y + 2, 1, 1, C.W);
+ return;
+ }
+ case 'big':
+ rect(c, x, y - 1, 4, 5, E);
+ round(y - 1);
+ round(y + 3);
+ rect(c, x + 2, y, 1, 1, C.W);
+ rect(c, x + 1, y + 2, 1, 1, C.W);
+ return;
+ case 'sparkle':
+ rect(c, x - 1, y - 1, 6, 6, E);
+ for (const [a, b] of [
+ [x - 1, y - 1],
+ [x + 4, y - 1],
+ [x - 1, y + 4],
+ [x + 4, y + 4],
+ ]) {
+ rect(c, a, b, 1, 1, C.BLOB);
+ }
+ rect(c, x + 2, y, 2, 2, C.W);
+ rect(c, x, y + 3, 1, 1, C.W);
+ return;
+ case 'dot':
+ rect(c, x + 1, y + 1, 2, 2, E);
+ return;
+ case 'star':
+ rect(c, x + 1, y, 2, 4, C.STAR);
+ rect(c, x, y + 1, 4, 2, C.STAR);
+ rect(c, x + 1, y + 1, 1, 1, C.W);
+ return;
+ case 'heart':
+ sprite(c, ['.#.#.', '#####', '.###.', '..#..'], x - (left ? 1 : 0), y, { '#': C.HEART });
+ rect(c, x + (left ? 0 : 1), y + 1, 1, 1, C.HEART_HI);
+ return;
+ case 'question':
+ sprite(c, ['.oo.', 'o..o', '..o.', '..o.', '....', '..o.'], x, y - 1, { o: E });
+ return;
+ case 'x':
+ for (let i = 0; i < 4; i++) {
+ rect(c, x + i, y + i, 1, 1, E);
+ rect(c, x + 3 - i, y + i, 1, 1, E);
+ }
+ return;
+ case 'spiral': {
+ const ring = [
+ [0, 0],
+ [1, 0],
+ [2, 0],
+ [3, 0],
+ [3, 1],
+ [3, 2],
+ [3, 3],
+ [2, 3],
+ [1, 3],
+ [0, 3],
+ [0, 2],
+ [0, 1],
+ ];
+ const k = Math.floor(t / 90) % 12;
+ ring.forEach(([a, b], i) => {
+ if ((i - k + 12) % 12 > 2) {
+ rect(c, x + a, y + b, 1, 1, E);
+ }
+ });
+ rect(c, x + 1, y + 1, 1, 1, E);
+ return;
+ }
+ case 'wide':
+ rect(c, x - 1, y - 1, 5, 5, E);
+ rect(c, x, y, 3, 3, C.BLOB);
+ rect(c, x + 1, y + 1, 1, 1, E);
+ return;
+ case 'dash':
+ rect(c, x, y + 1, 4, 2, E);
+ return;
+ case 'equals':
+ rect(c, x, y + 1, 4, 1, E);
+ rect(c, x, y + 3, 4, 1, E);
+ return;
+ case 'squeeze':
+ sprite(
+ c,
+ left ? ['#...', '.##.', '...#', '.##.', '#...'] : ['...#', '.##.', '#...', '.##.', '...#'],
+ x,
+ y - 1,
+ { '#': E },
+ );
+ return;
+ case 'dollar':
+ sprite(c, ['.##.', '##..', '.##.', '..##', '.##.'], x, y - 1, { '#': C.MONEY });
+ rect(c, x + 1, y - 2, 1, 1, C.MONEY);
+ rect(c, x + 2, y + 4, 1, 1, C.MONEY);
+ return;
+ case 'side':
+ rect(c, x, y + 1, 4, 3, E);
+ rect(c, x, y + 2, 1, 1, C.W);
+ return;
+ case 'tear':
+ rect(c, x, y, 4, 4, E);
+ round(y);
+ round(y + 3);
+ rect(c, x + 2, y + 1, 1, 1, C.W);
+ rect(c, x + 1, y + 4 + (Math.floor(t / 160) % 4), 1, 2, C.TEAR);
+ return;
+ case 'cry': {
+ rect(c, x, y + 1, 4, 1, E);
+ const k = Math.floor(t / 110) % 3;
+ for (let i = 0; i < 4; i++) {
+ if ((i + k) % 3) {
+ rect(c, x, y + 2 + i, 1, 1, C.TEAR);
+ rect(c, x + 3, y + 2 + i, 1, 1, C.TEAR);
+ }
+ }
+ return;
+ }
+ default:
+ rect(c, x, y, 4, 4, E);
+ round(y);
+ round(y + 3);
+ rect(c, x + 2, y + 1, 1, 1, C.W);
+ }
+}
+
+function drawMouth(c: Ctx, kind: Mouth, t: number) {
+ const E = C.EYE;
+ const dots = (pts: ReadonlyArray<readonly [number, number]>) =>
+ pts.forEach(([x, y]) => rect(c, x, y, 1, 1, E));
+ switch (kind) {
+ case 'grin':
+ rect(c, 13, 15, 7, 1, E);
+ rect(c, 14, 16, 1, 1, E);
+ rect(c, 15, 16, 3, 1, C.PINK);
+ rect(c, 18, 16, 1, 1, E);
+ rect(c, 15, 17, 3, 1, E);
+ return;
+ case 'beam':
+ dots([
+ [13, 14],
+ [13, 15],
+ [14, 16],
+ [15, 17],
+ [16, 17],
+ [17, 17],
+ [18, 16],
+ [19, 15],
+ [19, 14],
+ ]);
+ rect(c, 14, 14, 5, 2, E);
+ rect(c, 15, 16, 3, 1, C.PINK);
+ return;
+ case 'laugh':
+ sprite(c, ['#######', '#ppppp#', '.#ppp#.', '..###..'], 13, 14 + (Math.floor(t / 120) % 2), {
+ '#': E,
+ p: C.PINK,
+ });
+ return;
+ case 'o':
+ rect(c, 15, 15, 3, 3, E);
+ rect(c, 16, 16, 1, 1, C.PINK);
+ return;
+ case 'gasp':
+ rect(c, 14, 14, 5, 4, E);
+ rect(c, 14, 14, 1, 1, C.BLOB);
+ rect(c, 18, 14, 1, 1, C.BLOB);
+ rect(c, 15, 15, 3, 2, C.PINK);
+ return;
+ case 'tiny':
+ rect(c, 15, 16, 3, 1, E);
+ return;
+ case 'sleep':
+ rect(c, 16, 16, 2, 1 + (Math.floor(t / 900) % 2), E);
+ return;
+ case 'flat':
+ rect(c, 14, 16, 5, 1, E);
+ return;
+ case 'wavy':
+ for (let i = 0; i < 7; i++) {
+ rect(c, 13 + i, i % 2 ? 15 : 16, 1, 1, E);
+ }
+ return;
+ case 'frown':
+ rect(c, 13, 17, 1, 1, E);
+ rect(c, 14, 16, 5, 1, E);
+ rect(c, 19, 17, 1, 1, E);
+ return;
+ case 'pout':
+ dots([
+ [15, 17],
+ [16, 16],
+ [17, 17],
+ ]);
+ return;
+ case 'smirk':
+ rect(c, 14, 16, 4, 1, E);
+ rect(c, 18, 15, 1, 1, E);
+ return;
+ case 'tongue':
+ drawMouth(c, 'smile', t);
+ rect(c, 16, 17, 2, 2, C.PINK);
+ rect(c, 16, 18, 1, 1, C.HEART);
+ return;
+ case 'cat':
+ dots([
+ [13, 15],
+ [14, 16],
+ [15, 16],
+ [16, 15],
+ [17, 16],
+ [18, 16],
+ [19, 15],
+ ]);
+ return;
+ case 'teeth':
+ rect(c, 13, 15, 7, 3, E);
+ rect(c, 14, 16, 5, 1, C.W);
+ rect(c, 15, 16, 1, 1, E);
+ rect(c, 17, 16, 1, 1, E);
+ return;
+ case 'kiss':
+ rect(c, 16, 15, 2, 1, E);
+ rect(c, 18, 16, 1, 1, E);
+ rect(c, 16, 17, 2, 1, E);
+ rect(c, 16, 16, 2, 1, C.HEART);
+ return;
+ case 'whistle':
+ rect(c, 18, 15, 2, 2, E);
+ rect(c, 18, 15, 1, 1, C.PINK);
+ return;
+ case 'drool':
+ drawMouth(c, 'smile', t);
+ rect(c, 18, 17, 1, 1 + (Math.floor(t / 250) % 3), C.GLOW);
+ return;
+ case 'chew':
+ if (Math.floor(t / 160) % 2) {
+ rect(c, 15, 15, 3, 2, E);
+ } else {
+ rect(c, 14, 16, 5, 1, E);
+ }
+ return;
+ default:
+ rect(c, 13, 15, 1, 1, E);
+ rect(c, 14, 16, 5, 1, E);
+ rect(c, 19, 15, 1, 1, E);
+ }
+}
+
+const BROWS: Record<Brow, ReadonlyArray<readonly [number, number, number]>> = {
+ worried: [
+ [8, 6, 2],
+ [10, 5, 2],
+ [21, 5, 2],
+ [23, 6, 2],
+ ],
+ angry: [
+ [8, 5, 2],
+ [10, 6, 2],
+ [21, 6, 2],
+ [23, 5, 2],
+ ],
+ raised: [
+ [8, 5, 4],
+ [21, 5, 4],
+ ],
+ skeptic: [
+ [8, 6, 4],
+ [21, 4, 4],
+ ],
+ focus: [
+ [8, 6, 4],
+ [21, 6, 4],
+ ],
+};
+
+const EXTRAS: Record<Extra, (c: Ctx, fs: FaceState) => void> = {
+ blush: (c) => {
+ rect(c, 4, 13, 3, 1, C.PINK);
+ rect(c, 25, 13, 3, 1, C.PINK);
+ rect(c, 5, 14, 1, 1, C.PINK);
+ rect(c, 26, 14, 1, 1, C.PINK);
+ },
+ sweat: (c, fs) => {
+ const y = 2 + (Math.floor(fs.t / 220) % 4);
+ rect(c, 28, y, 1, 1, C.TEAR);
+ rect(c, 27, y + 1, 3, 2, C.TEAR);
+ rect(c, 28, y + 1, 1, 1, C.W);
+ },
+ vein: (c) => sprite(c, ['.#.#.', '##.##', '.....', '##.##', '.#.#.'], 25, 1, { '#': C.VEIN }),
+ tears: (c, fs) => {
+ const k = Math.floor(fs.t / 120) % 4;
+ for (let i = 0; i < 3; i++) {
+ rect(c, 9, 12 + ((i * 2 + k) % 8), 1, 1, C.TEAR);
+ rect(c, 22, 12 + ((i * 2 + k + 1) % 8), 1, 1, C.TEAR);
+ }
+ },
+ sparkles: (c, fs) => {
+ const k = Math.floor(fs.t / 200) % 3;
+ const spots = [
+ [3, 3],
+ [28, 4],
+ [4, 16],
+ [27, 15],
+ ];
+ spots.forEach(([x, y], i) => {
+ if ((i + k) % 3) {
+ rect(c, x, y - 1, 1, 3, C.W);
+ rect(c, x - 1, y, 3, 1, C.W);
+ }
+ });
+ },
+ shades: (c, fs) => {
+ /* The glasses drop in from above when the expression starts. */
+ const y = 8 + Math.min(0, -8 + Math.floor(fs.elapsed / 45));
+ rect(c, 6, y, 8, 4, C.EYE);
+ rect(c, 19, y, 8, 4, C.EYE);
+ rect(c, 14, y + 1, 5, 1, C.EYE);
+ rect(c, 7, y + 1, 2, 1, '#5d7f9e');
+ rect(c, 20, y + 1, 2, 1, '#5d7f9e');
+ for (const x of [6, 13, 19, 26]) {
+ rect(c, x, y + 3, 1, 1, C.BLOB);
+ }
+ },
+ glasses: (c) => {
+ for (const x of [6, 19]) {
+ rect(c, x, 6, 8, 1, C.EYE);
+ rect(c, x, 12, 8, 1, C.EYE);
+ rect(c, x, 6, 1, 7, C.EYE);
+ rect(c, x + 7, 6, 1, 7, C.EYE);
+ }
+ rect(c, 14, 8, 5, 1, C.EYE);
+ },
+ monocle: (c) => {
+ rect(c, 19, 6, 7, 1, C.STAR);
+ rect(c, 19, 12, 7, 1, C.STAR);
+ rect(c, 19, 6, 1, 7, C.STAR);
+ rect(c, 25, 6, 1, 7, C.STAR);
+ rect(c, 25, 13, 1, 6, C.STAR);
+ },
+ mustache: (c) => sprite(c, ['.##.##.', '###.###', '#.....#'], 13, 12, { '#': '#5a3a22' }),
+ sick: (c) => {
+ for (let y = 1; y < SCREEN_H - 1; y++) {
+ for (let x = 2; x < SCREEN_W - 2; x++) {
+ if (blobDistance(x, y) < 0.9 && dither(x, y, 0.3)) {
+ rect(c, x, y, 1, 1, '#b5d99c');
+ }
+ }
+ }
+ },
+ bandage: (c) => {
+ rect(c, 24, 2, 6, 2, '#f1d3a8');
+ rect(c, 26, 2, 1, 2, '#c9a77a');
+ rect(c, 28, 2, 1, 2, '#c9a77a');
+ },
+ zzz: (c, fs) => {
+ if (Math.floor(fs.t / 500) % 2) {
+ sprite(c, ['###', '..#', '.#.', '#..', '###'], 26, 2, { '#': C.GLOW });
+ }
+ },
+ think: (c, fs) => {
+ const k = Math.floor(fs.t / 300) % 4;
+ for (let i = 0; i < k; i++) {
+ rect(c, 24 + i * 2, 3, 1, 1, C.EYE);
+ }
+ },
+};
+
+type Row = [
+ key: string,
+ label: `com_ui_lia_feel_${string}`,
+ eyes: Eye | readonly [Eye, Eye],
+ mouth: Mouth,
+ brows?: Brow | null,
+ extras?: readonly Extra[] | null,
+ arms?: string | null,
+ pose?: string | null,
+];
+
+const ROWS: Row[] = [
+ ['neutral', 'com_ui_lia_feel_neutral', 'normal', 'smile'],
+ ['happy', 'com_ui_lia_feel_happy', 'happy', 'grin'],
+ ['joyful', 'com_ui_lia_feel_joyful', 'happy', 'beam', null, ['sparkles'], 'cheer', 'bounce'],
+ ['laughing', 'com_ui_lia_feel_laughing', 'squeeze', 'laugh', null, null, null, 'shake'],
+ ['giggle', 'com_ui_lia_feel_giggle', 'closed', 'cat', null, null, 'cover', 'wiggle'],
+ ['content', 'com_ui_lia_feel_content', 'closed', 'smile'],
+ ['smug', 'com_ui_lia_feel_smug', 'side', 'smirk', 'raised'],
+ ['wink', 'com_ui_lia_feel_wink', ['normal', 'blink'], 'grin', null, null, 'thumbs'],
+ ['cheeky', 'com_ui_lia_feel_cheeky', ['blink', 'normal'], 'tongue'],
+ ['love', 'com_ui_lia_feel_love', 'heart', 'beam', null, ['blush'], 'hug', 'sway'],
+ ['starstruck', 'com_ui_lia_feel_starstruck', 'star', 'gasp', null, ['sparkles'], 'jazz'],
+ ['blushing', 'com_ui_lia_feel_blushing', 'closed', 'smile', null, ['blush']],
+ ['shy', 'com_ui_lia_feel_shy', 'dot', 'tiny', null, ['blush'], 'hug', 'tilt'],
+ ['curious', 'com_ui_lia_feel_curious', 'big', 'o', 'skeptic', null, null, 'tilt'],
+ ['confused', 'com_ui_lia_feel_confused', 'question', 'wavy', 'skeptic', null, 'scratch', 'tiltR'],
+ ['thinking', 'com_ui_lia_feel_thinking', 'side', 'flat', 'skeptic', ['think'], 'scratch'],
+ ['skeptical', 'com_ui_lia_feel_skeptical', 'half', 'flat', 'skeptic'],
+ ['surprised', 'com_ui_lia_feel_surprised', 'big', 'o', 'raised'],
+ ['shocked', 'com_ui_lia_feel_shocked', 'wide', 'gasp', 'raised', null, 'shrug', 'jump'],
+ ['scared', 'com_ui_lia_feel_scared', 'wide', 'wavy', 'worried', ['sweat'], 'shield', 'shiver'],
+ ['nervous', 'com_ui_lia_feel_nervous', 'normal', 'wavy', 'worried', ['sweat']],
+ ['sad', 'com_ui_lia_feel_sad', 'sad', 'frown', 'worried', null, null, 'slump'],
+ ['crying', 'com_ui_lia_feel_crying', 'cry', 'frown', 'worried', null, 'rub'],
+ ['sobbing', 'com_ui_lia_feel_sobbing', 'cry', 'gasp', 'worried', ['tears'], null, 'shake'],
+ ['pouting', 'com_ui_lia_feel_pouting', 'dot', 'pout', 'angry', ['blush'], 'hips'],
+ ['angry', 'com_ui_lia_feel_angry', 'angry', 'teeth', 'angry', ['vein'], 'box'],
+ ['furious', 'com_ui_lia_feel_furious', 'angry', 'gasp', 'angry', ['vein'], 'shrug', 'stomp'],
+ ['annoyed', 'com_ui_lia_feel_annoyed', 'equals', 'flat', null, null, 'hips'],
+ ['bored', 'com_ui_lia_feel_bored', 'half', 'flat', null, null, null, 'slump'],
+ ['sleepy', 'com_ui_lia_feel_sleepy', 'half', 'tiny', null, null, 'rub'],
+ ['asleep', 'com_ui_lia_feel_asleep', 'closed', 'sleep', null, ['zzz'], null, 'sit'],
+ ['dizzy', 'com_ui_lia_feel_dizzy', 'x', 'wavy', null, null, null, 'dizzy'],
+ ['dazed', 'com_ui_lia_feel_dazed', 'spiral', 'wavy', null, null, null, 'dizzy'],
+ ['sick', 'com_ui_lia_feel_sick', 'dash', 'wavy', 'worried', ['sick', 'sweat'], null, 'slump'],
+ ['cool', 'com_ui_lia_feel_cool', 'normal', 'smirk', null, ['shades'], 'thumbs'],
+ ['nerd', 'com_ui_lia_feel_nerd', 'normal', 'grin', null, ['glasses'], 'pointUp'],
+ ['fancy', 'com_ui_lia_feel_fancy', 'closed', 'smile', null, ['monocle', 'mustache'], 'hips'],
+ ['rich', 'com_ui_lia_feel_rich', 'dollar', 'grin', null, null, 'cheer'],
+ ['hungry', 'com_ui_lia_feel_hungry', 'big', 'drool'],
+ ['yummy', 'com_ui_lia_feel_yummy', 'happy', 'tongue', null, ['blush']],
+ ['kissy', 'com_ui_lia_feel_kissy', 'closed', 'kiss', null, ['blush']],
+ ['whistling', 'com_ui_lia_feel_whistling', 'half', 'whistle', null, null, null, 'sway'],
+ ['determined', 'com_ui_lia_feel_determined', 'angry', 'flat', 'focus', null, 'flex'],
+ ['proud', 'com_ui_lia_feel_proud', 'closed', 'beam', null, ['sparkles'], 'hips', 'tiptoe'],
+ ['relieved', 'com_ui_lia_feel_relieved', 'closed', 'smile', null, ['sweat']],
+ [
+ 'embarrassed',
+ 'com_ui_lia_feel_embarrassed',
+ 'dash',
+ 'wavy',
+ null,
+ ['blush', 'sweat'],
+ 'facepalm',
+ ],
+ ['mischievous', 'com_ui_lia_feel_mischievous', 'side', 'smirk', 'angry', null, 'rub'],
+ ['sneezy', 'com_ui_lia_feel_sneezy', 'squeeze', 'gasp', 'raised'],
+ ['focused', 'com_ui_lia_feel_focused', 'dash', 'flat', 'focus'],
+ ['hopeful', 'com_ui_lia_feel_hopeful', 'sparkle', 'smile', 'worried'],
+ ['grumpy', 'com_ui_lia_feel_grumpy', 'half', 'frown', 'angry'],
+ ['catface', 'com_ui_lia_feel_catface', 'happy', 'cat'],
+ ['deadpan', 'com_ui_lia_feel_deadpan', 'dot', 'flat'],
+ ['zen', 'com_ui_lia_feel_zen', 'closed', 'smile', null, ['sparkles'], null, 'float'],
+ ['excited', 'com_ui_lia_feel_excited', 'star', 'laugh', null, null, 'cheer', 'bounce'],
+ ['tired', 'com_ui_lia_feel_tired', 'tired', 'tiny'],
+ ['suspicious', 'com_ui_lia_feel_suspicious', 'side', 'pout', 'angry'],
+ ['hurt', 'com_ui_lia_feel_hurt', 'squeeze', 'teeth', 'worried', ['bandage']],
+ ['dreamy', 'com_ui_lia_feel_dreamy', 'half', 'smile', 'raised', ['sparkles'], null, 'sway'],
+ ['startled', 'com_ui_lia_feel_startled', 'wide', 'o', 'raised', null, 'shield', 'jump'],
+ ['chewing', 'com_ui_lia_feel_chewing', 'happy', 'chew'],
+ ['amazed', 'com_ui_lia_feel_amazed', 'sparkle', 'gasp', 'raised', ['sparkles']],
+];
+
+export const EXPRESSIONS: readonly Expression[] = ROWS.map(
+ ([key, label, eyes, mouth, brows, extras, arms, pose]) => ({
+ key,
+ label,
+ eyes,
+ mouth,
+ brows: brows ?? undefined,
+ extras: extras ?? [],
+ arms: arms ?? undefined,
+ pose: pose ?? undefined,
+ }),
+);
+export const EXPRESSION_BY_KEY: ReadonlyMap<string, Expression> = new Map(
+ EXPRESSIONS.map((e) => [e.key, e]),
+);
+
+let blobCache: HTMLCanvasElement | null = null;
+function blobCanvas() {
+ if (blobCache == null) {
+ blobCache = document.createElement('canvas');
+ blobCache.width = SCREEN_W;
+ blobCache.height = SCREEN_H;
+ const c = blobCache.getContext('2d');
+ if (c) {
+ drawBlob(c, 1);
+ }
+ }
+ return blobCache;
+}
+
+export function drawFace(c: Ctx, fs: FaceState) {
+ const ex = EXPRESSION_BY_KEY.get(fs.face) ?? EXPRESSIONS[0];
+ rect(c, 0, 0, SCREEN_W, SCREEN_H, C.SCREEN);
+ c.drawImage(blobCanvas(), 0, 0);
+ if (ex.extras.includes('sick')) {
+ EXTRAS.sick(c, fs);
+ }
+ if (fs.feather) {
+ sprite(c, FEATHER_ROWS, 15, 0, FEATHER_PAL);
+ }
+ let [left, right] = typeof ex.eyes === 'string' ? [ex.eyes, ex.eyes] : ex.eyes;
+ if (fs.blink) {
+ left = BLINKABLE.has(left) ? 'blink' : left;
+ right = BLINKABLE.has(right) ? 'blink' : right;
+ }
+ drawEye(c, left, 8 + fs.dx, 8 + fs.dy, true, fs.t);
+ drawEye(c, right, 21 + fs.dx, 8 + fs.dy, false, fs.t);
+ if (ex.brows) {
+ for (const [x, y, w] of BROWS[ex.brows]) {
+ rect(c, x + fs.dx, y + fs.dy, w, 1, C.EYE);
+ }
+ }
+ drawMouth(c, ex.mouth, fs.t);
+ for (const extra of ex.extras) {
+ if (extra !== 'sick') {
+ EXTRAS[extra](c, fs);
+ }
+ }
+}
diff --git a/client/src/components/Lia/engine/pixels.ts b/client/src/components/Lia/engine/pixels.ts
new file mode 100644
index 0000000000..0c834ebe0e
--- /dev/null
+++ b/client/src/components/Lia/engine/pixels.ts
@@ -0,0 +1,104 @@
+/**
+ * Lia's palette. These are brand art colors drawn into a canvas, like the pixels of a logo,
+ * so they are fixed values rather than theme roles: Lia looks the same in every theme.
+ */
+export const C = {
+ O: '#0f2a33',
+ BODY: '#3b9ea3',
+ HI: '#74d0cf',
+ LO: '#2a7378',
+ SCREEN: '#1f4f7c',
+ DEEP: '#173d61',
+ GLOW: '#8fcbe8',
+ BLOB: '#f3f6e4',
+ EYE: '#0b1218',
+ W: '#ffffff',
+ FEATHER: '#6cc4f0',
+ FEATHER_HI: '#c8ecff',
+ LED: '#f2a33a',
+ PINK: '#ff7a93',
+ HEART: '#ff5d7e',
+ HEART_HI: '#ffc2cf',
+ STAR: '#e9a21a',
+ BSOD: '#2a56c6',
+ BLACK: '#071018',
+ TEAR: '#7fd0ff',
+ MONEY: '#2f9e44',
+ VEIN: '#e5484d',
+} as const;
+
+export type Ctx = CanvasRenderingContext2D;
+export type Palette = Readonly<Record<string, string>>;
+export type Rows = readonly string[];
+
+const BAYER = [
+ [0, 8, 2, 10],
+ [12, 4, 14, 6],
+ [3, 11, 1, 9],
+ [15, 7, 13, 5],
+];
+
+/** Ordered dithering: whether pixel (x, y) is lit at a coverage `level` from 0 to 1. */
+export const dither = (x: number, y: number, level: number) =>
+ (BAYER[y & 3][x & 3] + 0.5) / 16 < level;
+
+export function rect(c: Ctx, x: number, y: number, w: number, h: number, color: string) {
+ c.fillStyle = color;
+ c.fillRect(x, y, w, h);
+}
+
+/** Draws a sprite given as rows of characters; `.` is transparent, other characters index `pal`. */
+export function sprite(c: Ctx, rows: Rows, x0: number, y0: number, pal: Palette, flip = false) {
+ for (let y = 0; y < rows.length; y++) {
+ const row = rows[y];
+ const n = row.length;
+ for (let x = 0; x < n; x++) {
+ const color = pal[row[x]];
+ if (color) {
+ rect(c, x0 + (flip ? n - 1 - x : x), y0 + y, 1, 1, color);
+ }
+ }
+ }
+}
+
+/** A one-pixel Bresenham line. */
+export function line(c: Ctx, ax: number, ay: number, bx: number, by: number, color: string) {
+ let x0 = Math.round(ax);
+ let y0 = Math.round(ay);
+ const x1 = Math.round(bx);
+ const y1 = Math.round(by);
+ const dx = Math.abs(x1 - x0);
+ const dy = -Math.abs(y1 - y0);
+ const sx = x0 < x1 ? 1 : -1;
+ const sy = y0 < y1 ? 1 : -1;
+ let err = dx + dy;
+ for (let i = 0; i < 400; i++) {
+ rect(c, x0, y0, 1, 1, color);
+ if (x0 === x1 && y0 === y1) {
+ return;
+ }
+ const e2 = 2 * err;
+ if (e2 >= dy) {
+ err += dy;
+ x0 += sx;
+ }
+ if (e2 <= dx) {
+ err += dx;
+ y0 += sy;
+ }
+ }
+}
+
+/** Deterministic pseudo-random value in [0, 1) for an integer seed. */
+export function hash(n: number) {
+ let x = Math.imul((n | 0) ^ 0x9e3779b9, 0x85ebca6b);
+ x ^= x >>> 13;
+ x = Math.imul(x, 0xc2b2ae35);
+ x ^= x >>> 16;
+ return (x >>> 0) / 4294967296;
+}
+
+export const clamp = (v: number, a: number, b: number) => Math.max(a, Math.min(b, v));
+export const easeOut = (p: number) => 1 - (1 - p) * (1 - p);
+/** Two-state square wave: 0 or 1, switching every `ms`. */
+export const toggle = (t: number, ms: number) => Math.floor(t / ms) % 2;
diff --git a/e2e/specs/mock/lia.helpers.ts b/e2e/specs/mock/lia.helpers.ts
new file mode 100644
index 0000000000..35fc5d3105
--- /dev/null
+++ b/e2e/specs/mock/lia.helpers.ts
@@ -0,0 +1,35 @@
+import fs from 'fs';
+import path from 'path';
+import ts from 'typescript';
+import type { Page } from '@playwright/test';
+
+const ENGINE = path.resolve(__dirname, '../../../client/src/components/Lia/engine');
+const ORIGIN = 'http://lia.test';
+
+/**
+ * Serves Lia's engine modules to a blank page and exposes them as `window.lia[name]`, so a spec
+ * can drive the renderer and the engine with a real browser canvas and animation frames.
+ */
+export async function openLiaEngine(page: Page, modules: readonly string[]) {
+ await page.route(`${ORIGIN}/**`, (route) => {
+ const name = new URL(route.request().url()).pathname.slice(1);
+ if (!name) {
+ return route.fulfill({ contentType: 'text/html', body: '<!doctype html><body></body>' });
+ }
+ if (!/^[a-z]+$/.test(name)) {
+ return route.fulfill({ status: 404 });
+ }
+ const source = fs.readFileSync(path.join(ENGINE, `${name}.ts`), 'utf8');
+ const { outputText } = ts.transpileModule(source, {
+ compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2022 },
+ });
+ return route.fulfill({ contentType: 'text/javascript', body: outputText });
+ });
+ await page.goto(`${ORIGIN}/`);
+ const imports = modules.map((name) => `import * as ${name} from '/${name}';`).join(' ');
+ await page.addScriptTag({
+ type: 'module',
+ content: `${imports} window.lia = { ${modules.join(', ')} };`,
+ });
+ await page.waitForFunction(() => 'lia' in window);
+}
diff --git a/e2e/specs/mock/scenarios/lia-art.spec.ts b/e2e/specs/mock/scenarios/lia-art.spec.ts
new file mode 100644
index 0000000000..4364f0f898
--- /dev/null
+++ b/e2e/specs/mock/scenarios/lia-art.spec.ts
@@ -0,0 +1,165 @@
+import { expect, test } from '@playwright/test';
+import type { Page } from '@playwright/test';
+import type * as Body from '../../../../client/src/components/Lia/engine/body';
+import type * as Face from '../../../../client/src/components/Lia/engine/face';
+import { openLiaEngine } from '../lia.helpers';
+
+/**
+ * Lia's pixel art, drawn by a real browser canvas. Nothing mounts the renderer yet, so each
+ * test serves the engine modules to a blank page and reads back the pixels they paint.
+ */
+type Lia = { face: typeof Face; body: typeof Body };
+
+const openEngine = (page: Page) => openLiaEngine(page, ['face', 'body']);
+
+test.describe("Lia's pixel art", () => {
+ test('every expression draws its own face @scenario:lia-art-distinct-faces', async ({ page }) => {
+ await openEngine(page);
+ const { count, distinct } = await page.evaluate(() => {
+ const { face } = (window as unknown as { lia: Lia }).lia;
+ const images = face.EXPRESSIONS.map((e) => {
+ const canvas = document.createElement('canvas');
+ canvas.width = face.SCREEN_W;
+ canvas.height = face.SCREEN_H;
+ const c = canvas.getContext('2d') as CanvasRenderingContext2D;
+ face.drawFace(c, {
+ face: e.key,
+ elapsed: 2000,
+ dx: 0,
+ dy: 0,
+ feather: true,
+ t: 1000,
+ blink: false,
+ });
+ return canvas.toDataURL();
+ });
+ return { count: images.length, distinct: new Set(images).size };
+ });
+ expect(count).toBeGreaterThan(50);
+ expect(distinct).toBe(count);
+ });
+
+ test('Lia paints only inside her canvas and her screen @scenario:lia-art-stays-in-bounds', async ({
+ page,
+ }) => {
+ await openEngine(page);
+ const strays = await page.evaluate(() => {
+ const { face, body } = (window as unknown as { lia: Lia }).lia;
+ const found: string[] = [];
+ /** Draws on a canvas with a margin around the box, and names any pixel painted outside it. */
+ const check = (
+ label: string,
+ w: number,
+ h: number,
+ draw: (c: CanvasRenderingContext2D) => void,
+ ) => {
+ const pad = 16;
+ const canvas = document.createElement('canvas');
+ canvas.width = w + pad * 2;
+ canvas.height = h + pad * 2;
+ const c = canvas.getContext('2d') as CanvasRenderingContext2D;
+ c.translate(pad, pad);
+ draw(c);
+ const { data } = c.getImageData(0, 0, canvas.width, canvas.height);
+ for (let y = 0; y < canvas.height; y++) {
+ for (let x = 0; x < canvas.width; x++) {
+ const inside = x >= pad && x < pad + w && y >= pad && y < pad + h;
+ if (!inside && data[(y * canvas.width + x) * 4 + 3] > 0) {
+ found.push(`${label} at ${x - pad},${y - pad}`);
+ return;
+ }
+ }
+ }
+ };
+ for (const e of face.EXPRESSIONS) {
+ /* Every direction Lia can look, including the diagonals. */
+ for (const [dx, dy] of [-1, 0, 1].flatMap((x) => [-1, 0, 1].map((y) => [x, y]))) {
+ /* From the first frame of the entrance (sunglasses dropping in) to long after. */
+ for (const elapsed of [0, 30, 200, 2000]) {
+ check(`face ${e.key} at ${elapsed}ms`, face.SCREEN_W, face.SCREEN_H, (c) =>
+ face.drawFace(c, {
+ face: e.key,
+ elapsed,
+ dx,
+ dy,
+ feather: true,
+ t: 1000,
+ blink: false,
+ }),
+ );
+ }
+ }
+ }
+ check('body, feet and arms', body.GRID_W, body.GRID_H, (c) => {
+ body.drawBody(c, '#000');
+ for (const mode of [
+ 'stand',
+ 'walk',
+ 'run',
+ 'march',
+ 'slide',
+ 'tap',
+ 'dangle',
+ 'tiptoe',
+ ] as const) {
+ for (const t of [0, 150, 300, 450]) {
+ body.drawFeet(c, mode, t);
+ }
+ }
+ for (const fn of Object.values(body.ARMS)) {
+ for (const t of [0, 120, 250, 500, 1000]) {
+ const pose = fn(t, 0.5);
+ body.drawArm(c, 11, 35, pose.l);
+ body.drawArm(c, 52, 35, pose.r);
+ }
+ }
+ });
+ /* Screens draw over the monitor in place, so compare against the bare body: every pixel a
+ * screen changes must lie on the screen. The glitch screen copies strips of the canvas. */
+ const grid = () => {
+ const canvas = document.createElement('canvas');
+ canvas.width = body.GRID_W;
+ canvas.height = body.GRID_H;
+ const c = canvas.getContext('2d') as CanvasRenderingContext2D;
+ body.drawBody(c, '#000');
+ return c;
+ };
+ const bare = grid().getImageData(0, 0, body.GRID_W, body.GRID_H).data;
+ const state = {
+ face: 'neutral',
+ elapsed: 2000,
+ dx: 0,
+ dy: 0,
+ feather: true,
+ t: 1000,
+ blink: false,
+ };
+ for (const name of body.BUILTIN_SCREENS) {
+ for (let t = 0; t < 3000; t += 70) {
+ const c = grid();
+ c.save();
+ c.translate(body.SX, body.SY);
+ body.drawScreen(c, name, t, (t % 1000) / 1000, state);
+ c.restore();
+ const { data } = c.getImageData(0, 0, body.GRID_W, body.GRID_H);
+ for (let i = 0; i < data.length; i += 4) {
+ const x = (i / 4) % body.GRID_W;
+ const y = Math.floor(i / 4 / body.GRID_W);
+ const onScreen =
+ x >= body.SX &&
+ x < body.SX + face.SCREEN_W &&
+ y >= body.SY &&
+ y < body.SY + face.SCREEN_H;
+ const changed = [0, 1, 2, 3].some((k) => data[i + k] !== bare[i + k]);
+ if (changed && !onScreen) {
+ found.push(`screen ${name} at ${x},${y} (t=${t})`);
+ break;
+ }
+ }
+ }
+ }
+ return found;
+ });
+ expect(strays).toEqual([]);
+ });
+});