From 7f1024112ce45e5381b36bd8e43baffb0d5e7a27 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 8 Oct 2026 16:48:18 +0000 Subject: [PATCH 1/2] feat(avatar): goblin Avatar Species and split side hair as asset version 2 withSpecies() turns a recipe into asset version 2 with a species, a separate right side hair piece and optional per-side hair colors. The goblin adds pointed ears drawn over hair and small tusks while keeping every head turn and speech mouth layer. Version 1 recipes, seeded faces and the BotHarness golden fixture are unchanged; a human version 2 recipe renders exactly like its version 1 source. New species golden entries pin the goblin artwork. Issue: BotHarness/DeepSeekBot#1210 Agent-Task: claude/cloud/session_01MozvQ2eMvqX1MzSqm9G2o8 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MozvQ2eMvqX1MzSqm9G2o8 --- CHANGELOG.md | 1 + packages/avatar/README.md | 1 + packages/avatar/package.json | 2 +- packages/avatar/src/figure.ts | 47 ++- packages/avatar/src/index.ts | 6 + packages/avatar/src/recipe.ts | 69 +++- .../avatar/test/fixtures/species-golden.json | 387 ++++++++++++++++++ packages/avatar/test/species.test.ts | 125 ++++++ 8 files changed, 626 insertions(+), 12 deletions(-) create mode 100644 packages/avatar/test/fixtures/species-golden.json create mode 100644 packages/avatar/test/species.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index fc95838..1dc1dbd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- `@botharness/pixel-avatar` 0.3.0: Avatar Species and split side hair as asset version 2. `withSpecies(recipe, species)` adds a `species` (`human` or `goblin`), a separate `rightSideHair` and optional `leftSideHairColor`/`rightSideHairColor`, moving a suggested skin color to the new species' `AVATAR_SPECIES_SWATCHES`. The goblin has pointed ears drawn over hair and small tusks, and keeps every head turn and speech mouth layer. Version 1 recipes, seeded faces and the existing golden output are unchanged, and a human version 2 recipe renders exactly like its version 1 source. Older consumers reject version 2 recipes through `isPixelAvatarRecipe`, so they can fall back to a saved snapshot. - `@botharness/pixel-avatar` 0.2.0: opt-in `mouthLayers` adds saved/closed/half-open/open speech features to every head turn. Existing recipes, static snapshots and default golden output remain unchanged; text pacing stays with the consumer. - `@botharness/pixel-morph`: one morph that throws no longer stops the shared animation loop for the others, and a morph started from inside a frame no longer starts a second frame chain. diff --git a/packages/avatar/README.md b/packages/avatar/README.md index da032a0..dd9b168 100644 --- a/packages/avatar/README.md +++ b/packages/avatar/README.md @@ -18,5 +18,6 @@ const svg = pixelAvatarSvg(recipe); // 32×32 viewBox, crisp edges, no ids or sc - `AVATAR_PARTS`, `AVATAR_SWATCHES`, `AVATAR_PRESETS`, `DEFAULT_RECIPE`: everything an editor needs. - `isPixelAvatarRecipe`, `canonicalRecipe`: validate and normalise saved recipes. - `AVATAR_HAIR_PARTS`, `AVATAR_RANGES`, `detailedRecipe(recipe)`: optional finer controls. A recipe may carry `bangs`, `sideHair`, `backHair`, `spacing`, `height` and `hairLength`, all six or none; `detailedRecipe` derives them from the plain `hair` so an editor can start from what is on screen. Recipes without them render exactly as before. +- `AVATAR_SPECIES`, `AVATAR_SPECIES_SWATCHES`, `AVATAR_PIECE_COLORS`, `withSpecies(recipe, species)`: Avatar Species (`human`, `goblin`) on the same rig, as asset version 2. `withSpecies` keeps every choice, splits side hair into `sideHair` (left) and `rightSideHair`, and accepts optional `leftSideHairColor`/`rightSideHairColor`, where left and right are as seen on screen. Consumers built before version 2 reject these recipes with `isPixelAvatarRecipe` and can show a saved snapshot instead. - `faceCells`, `pixelSymbolCells`, `symbolArtCells`: pixels for [`@botharness/pixel-morph`](../morph). - `pixelAvatarSvg(recipe, { turns, classPrefix })`: optional pre-rendered head turns, and rig layer classes (`-body`, `-head`, `-face`, `-gaze`, `-blink`) for CSS animation. diff --git a/packages/avatar/package.json b/packages/avatar/package.json index 6ab435a..ad5e398 100644 --- a/packages/avatar/package.json +++ b/packages/avatar/package.json @@ -1,6 +1,6 @@ { "name": "@botharness/pixel-avatar", - "version": "0.2.0", + "version": "0.3.0", "description": "Seeded, editable 32×32 chibi pixel avatars with state symbols that morph in and out", "keywords": [ "agent-ui", diff --git a/packages/avatar/src/figure.ts b/packages/avatar/src/figure.ts index a0d3d1a..e3f7766 100644 --- a/packages/avatar/src/figure.ts +++ b/packages/avatar/src/figure.ts @@ -1,5 +1,5 @@ import { escapeAttribute, type PixelCell } from '@botharness/pixel-morph'; -import type { PixelAvatarRecipe } from './recipe.js'; +import { AVATAR_PIECE_COLORS, type PixelAvatarRecipe } from './recipe.js'; type Cell = string | undefined; type Grid = Cell[][]; @@ -15,6 +15,7 @@ const MOUTH = '#7a2a38'; const MOUTH_INSIDE = '#b8415a'; const TONGUE = '#ef6f84'; const GOLD = '#efb93f'; +const TUSK = '#f6f0d8'; export type PixelGrid = Grid; export type PixelMouthState = 'saved' | 'closed' | 'half-open' | 'open'; @@ -353,24 +354,31 @@ function detailedHair( recipe: Recipe, s: number, b: number, -): { back: Mask; front: Mask; ties?: Point[]; bands?: boolean } { +): { back: Mask; front: Mask; ties?: Point[]; bands?: boolean; sides?: [Mask, Mask] } { const of = (style: Recipe['hair'] | undefined) => hair({ ...recipe, hair: style ?? 'none' }, s, b); const bangs = of(recipe.bangs); const side = of(recipe.sideHair); + const right = recipe.rightSideHair === undefined ? side : of(recipe.rightSideHair); const back = of(recipe.backHair); const length = (recipe.hairLength ?? 0) * 2; const lengthen = (mask: Mask): Mask => (x, y) => mask(x, y > 18 ? Math.round(y - length) : y); + // Side pieces cover |dx| >= 7 below the crown; with a right piece, each side has its own. + const sideOf = + (sign: -1 | 1): Mask => + (x, y) => + y > 7 && sign * (x + 0.5 - C - s) >= 7 && lengthen((sign < 0 ? side : right).front)(x, y); return { front: (x, y) => y <= 7 ? bangs.front(x, y) || back.front(x, y) : Math.abs(x + 0.5 - C - s) < 7 ? bangs.front(x, y) - : lengthen(side.front)(x, y), + : lengthen((x + 0.5 - C - s < 0 ? side : right).front)(x, y), + sides: [sideOf(-1), sideOf(1)], back: length === 0 ? back.back : lengthen(back.back), ...(back.ties ? { ties: back.ties } : {}), ...(back.bands ? { bands: back.bands } : {}), @@ -708,7 +716,8 @@ export function pixelFigure( return front(x - Math.round(d * (1 + Math.max(0, y - 20) * 0.4) - d * tuck), y); } : front; - const masks = recipe.bangs === undefined ? hair(recipe, s, b) : detailedHair(recipe, s, b); + const masks: ReturnType = + recipe.bangs === undefined ? hair(recipe, s, b) : detailedHair(recipe, s, b); const body = blank(); outfit(recipe, body, s); @@ -729,7 +738,15 @@ export function pixelFigure( const xs = [...Array(N).keys()].filter((x) => face(x, earY)); return side < 0 ? xs[0]! - 1 : xs.at(-1)! + 1; }; - if (!turned) + const goblin = recipe.species === 'goblin'; + if (goblin) { + // Long pointed ears drawn over hair: the outer tip rises above the ear line. + const ear = ['E...', 'EE..', '.EiE', '..Ee']; + const pal = { E: skin, e: shade(skin, 0.86), i: shade(skin, 0.74) }; + paint(head, masks.front, hairColor); + for (const ex of turned ? [-d] : [-1, 1]) + sprite(head, ear, ex < 0 ? edge(ex) - 3 : edge(ex), earY - 2, pal, ex > 0); + } else if (!turned) for (const ex of [-1, 1]) sprite(head, ['E', 'E', 'e'], edge(ex), earY, { E: skin, e: shade(skin, 0.86) }); else @@ -737,7 +754,7 @@ export function pixelFigure( E: skin, e: shade(skin, 0.8), }); - paint(head, masks.front, hairColor); + if (!goblin) paint(head, masks.front, hairColor); for (const [x, y] of masks.ties ?? []) sprite(back, ['T'], Math.round(x), y, { T: shade(recipe.shirtColor, 0.8) }); accessory(recipe, head, s, face); @@ -862,6 +879,9 @@ export function pixelFigure( T: TONGUE, W: WHITE, }); + if (recipe.species === 'goblin') + for (const tx of [Math.round(mx - 2.5), Math.round(mx + 2.5)]) + sprite(layer, ['T'], tx, 21 + h, { T: TUSK }); if (recipe.cheeks === 'blush') { sprite(layer, ['PP'], left.x - 1, 19 + h, { P: mix(BLUSH, skin, 0.25) }); sprite(layer, ['PP'], right.x + right.w - 1, 19 + h, { P: mix(BLUSH, skin, 0.25) }); @@ -920,6 +940,21 @@ export function pixelFigure( } const full = clip(outline(figure)); + AVATAR_PIECE_COLORS.forEach((key, i) => { + const color = recipe[key]; + const mask = masks.sides?.[i]; + if (color === undefined || !mask || color.toLowerCase() === hairColor.toLowerCase()) return; + const variant = blank(); + const plain = { ...recipe, hairColor: color }; + for (const k of AVATAR_PIECE_COLORS) delete plain[k]; + for (const cell of pixelFigure(plain, yawDeg).cells) variant[cell.y]![cell.x] = cell.c; + const near = (x: number, y: number) => + mask(x, y) || mask(x - 1, y) || mask(x + 1, y) || mask(x, y - 1) || mask(x, y + 1); + for (let y = 0; y < N; y++) + for (let x = 0; x < N; x++) + if (full[y]![x] && near(x, y) && !eyes[y]![x] && !glasses[y]![x]) + full[y]![x] = variant[y]![x]; + }); const bodyCells = blank(); const headCells = blank(); for (let y = 0; y < N; y++) diff --git a/packages/avatar/src/index.ts b/packages/avatar/src/index.ts index f717635..d5396ee 100644 --- a/packages/avatar/src/index.ts +++ b/packages/avatar/src/index.ts @@ -10,8 +10,11 @@ export { AVATAR_COLORS, AVATAR_HAIR_PARTS, AVATAR_PARTS, + AVATAR_PIECE_COLORS, AVATAR_PRESETS, AVATAR_RANGES, + AVATAR_SPECIES, + AVATAR_SPECIES_SWATCHES, AVATAR_SWATCHES, DEFAULT_RECIPE, canonicalRecipe, @@ -19,10 +22,13 @@ export { detailedRecipe, isPixelAvatarRecipe, seededRecipe, + withSpecies, type AvatarColor, type AvatarHairPart, type AvatarPart, + type AvatarPieceColor, type AvatarRange, + type AvatarSpecies, type PixelAvatarRecipe, } from './recipe.js'; export { seededRandom } from './random.js'; diff --git a/packages/avatar/src/recipe.ts b/packages/avatar/src/recipe.ts index c98c757..48a9912 100644 --- a/packages/avatar/src/recipe.ts +++ b/packages/avatar/src/recipe.ts @@ -150,14 +150,29 @@ export type AvatarPart = keyof typeof AVATAR_PARTS; export const AVATAR_COLORS = ['skinColor', 'hairColor', 'eyeColor', 'shirtColor'] as const; export type AvatarColor = (typeof AVATAR_COLORS)[number]; +/** + * Avatar Species: a base on the pixel bust rig. Species share motion and anchors; each sets its + * own ears, face details and suggested body colors. A recipe with a species is asset version 2. + */ +export const AVATAR_SPECIES = ['human', 'goblin'] as const; +export type AvatarSpecies = (typeof AVATAR_SPECIES)[number]; +/** Optional per-piece hair colors (asset version 2); an absent piece uses `hairColor`. */ +export const AVATAR_PIECE_COLORS = ['leftSideHairColor', 'rightSideHairColor'] as const; +export type AvatarPieceColor = (typeof AVATAR_PIECE_COLORS)[number]; + export type PixelAvatarRecipe = { schemaVersion: 1; family: 'illustrated'; - assetVersion: 1; + assetVersion: 1 | 2; rigVersion: 1; } & { [P in AvatarPart]: (typeof AVATAR_PARTS)[P][number] } & Record & { [P in AvatarHairPart]?: (typeof AVATAR_PARTS)['hair'][number]; - } & { [P in AvatarRange]?: number }; + } & { [P in AvatarRange]?: number } & { + /** Asset version 2 only. */ + species?: AvatarSpecies; + /** Asset version 2 only: right side hair; `sideHair` is then the left side. */ + rightSideHair?: (typeof AVATAR_HAIR_PARTS)['sideHair'][number]; + } & { [P in AvatarPieceColor]?: string }; export const DEFAULT_RECIPE: PixelAvatarRecipe = { schemaVersion: 1, @@ -189,7 +204,19 @@ export function isPixelAvatarRecipe(value: unknown): value is PixelAvatarRecipe const r = value as Record; const detailed = DETAIL_KEYS.filter((key) => Object.hasOwn(r, key)).length; if (detailed !== 0 && detailed !== DETAIL_KEYS.length) return false; - if (Object.keys(r).length !== Object.keys(DEFAULT_RECIPE).length + detailed) return false; + const v2 = r['assetVersion'] === 2; + const pieces = AVATAR_PIECE_COLORS.filter((key) => Object.hasOwn(r, key)); + const v2Keys = v2 ? 2 + pieces.length : 0; + if (Object.keys(r).length !== Object.keys(DEFAULT_RECIPE).length + detailed + v2Keys) + return false; + if ( + v2 && + (detailed === 0 || + !(AVATAR_SPECIES as readonly unknown[]).includes(r['species']) || + !(AVATAR_HAIR_PARTS.sideHair as readonly unknown[]).includes(r['rightSideHair']) || + !pieces.every((key) => typeof r[key] === 'string' && /^#[\da-f]{6}$/iu.test(r[key]))) + ) + return false; return ( (detailed === 0 || ((Object.keys(AVATAR_HAIR_PARTS) as AvatarHairPart[]).every( @@ -204,7 +231,7 @@ export function isPixelAvatarRecipe(value: unknown): value is PixelAvatarRecipe }))) && r['schemaVersion'] === 1 && r['family'] === 'illustrated' && - r['assetVersion'] === 1 && + (r['assetVersion'] === 1 || v2) && r['rigVersion'] === 1 && PART_KEYS.every( (key) => @@ -218,15 +245,41 @@ export function canonicalRecipe(recipe: PixelAvatarRecipe): PixelAvatarRecipe { const canonical: Record = { schemaVersion: 1, family: 'illustrated', - assetVersion: 1, + assetVersion: recipe.assetVersion, rigVersion: 1, }; for (const key of PART_KEYS) canonical[key] = recipe[key]; for (const key of AVATAR_COLORS) canonical[key] = recipe[key].toLowerCase(); if (recipe.bangs !== undefined) for (const key of DETAIL_KEYS) canonical[key] = recipe[key]; + if (recipe.assetVersion === 2) { + canonical['species'] = recipe.species; + canonical['rightSideHair'] = recipe.rightSideHair; + for (const key of AVATAR_PIECE_COLORS) + if (recipe[key] !== undefined) canonical[key] = recipe[key].toLowerCase(); + } return canonical as PixelAvatarRecipe; } +/** + * Returns the recipe as asset version 2 with the given species, keeping every other choice. + * Side hair splits into left (`sideHair`) and right (`rightSideHair`) pieces that start equal. + * When the skin color is one of the previous species' suggested colors, it moves to the new + * species' first suggestion; a custom color is kept. + */ +export function withSpecies(recipe: PixelAvatarRecipe, species: AvatarSpecies): PixelAvatarRecipe { + const detailed = detailedRecipe(recipe); + const previous = recipe.species ?? 'human'; + const suggested = AVATAR_SPECIES_SWATCHES[previous].includes(recipe.skinColor.toLowerCase()); + return { + ...detailed, + assetVersion: 2, + species, + rightSideHair: detailed.rightSideHair ?? detailed.sideHair!, + skinColor: + suggested && previous !== species ? AVATAR_SPECIES_SWATCHES[species][0]! : recipe.skinColor, + } as PixelAvatarRecipe; +} + export function detailedRecipe(recipe: PixelAvatarRecipe): PixelAvatarRecipe { if (recipe.bangs !== undefined) return recipe; const pick =

(part: P) => @@ -341,6 +394,12 @@ export function createSeededRecipe(namespace: string): (seed: string) => PixelAv export const seededRecipe: (seed: string) => PixelAvatarRecipe = createSeededRecipe('botharness-avatar'); +/** Suggested body colors per species; any color remains allowed. */ +export const AVATAR_SPECIES_SWATCHES: Record = { + human: AVATAR_SWATCHES.skinColor, + goblin: ['#9cc464', '#7fae4f', '#6a9a45', '#b3cf7a', '#5c8a4a', '#8fa86a'], +}; + const preset = (parts: Partial): PixelAvatarRecipe => ({ ...DEFAULT_RECIPE, ...parts, diff --git a/packages/avatar/test/fixtures/species-golden.json b/packages/avatar/test/fixtures/species-golden.json new file mode 100644 index 0000000..8bb7771 --- /dev/null +++ b/packages/avatar/test/fixtures/species-golden.json @@ -0,0 +1,387 @@ +{ + "goblin": { + "front": "9ea6b01ec8554c81", + "turns": "14256b9703382496", + "mouths": "b20d2ee2e500015d" + }, + "preset-0": { + "front": "8946971f3c0c1e73", + "turns": "db84f830cdb282b4", + "mouths": "28992b51bf6f8952" + }, + "preset-1": { + "front": "4be5cfd7fc334235", + "turns": "18d1617ca62f7d54", + "mouths": "9a490f815f64ca7a" + }, + "preset-2": { + "front": "a3a1c674ddec0f69", + "turns": "710c55e5f2d334f1", + "mouths": "f59bc1c7e10e7525" + }, + "preset-3": { + "front": "b4c9d9617a8b1e98", + "turns": "addf432823851099", + "mouths": "e31e347a62b4baba" + }, + "preset-4": { + "front": "e4ad317ab872509f", + "turns": "187305614b17a660", + "mouths": "4b2521939418292d" + }, + "preset-5": { + "front": "32f49bf5ce3c4f50", + "turns": "67fb6a6901d761c7", + "mouths": "90b726bb2639d2d7" + }, + "preset-6": { + "front": "3f322ce8080da94f", + "turns": "42b68bce3538db7d", + "mouths": "459fee379a940045" + }, + "preset-7": { + "front": "c98937fe5280ac52", + "turns": "b0af49a5e7b0f24a", + "mouths": "1ac61857030a026d" + }, + "preset-8": { + "front": "a81a0ff011eeecd5", + "turns": "fccabeb3692cc1b4", + "mouths": "c2075acdca048bbd" + }, + "preset-9": { + "front": "fe8b338c5488f7b6", + "turns": "5486e40d6ea18d68", + "mouths": "1dc193b7646abb6b" + }, + "preset-10": { + "front": "c3d5fca956a5a377", + "turns": "fc9b1c9f8a456903", + "mouths": "88e5eb61877b1c4c" + }, + "preset-11": { + "front": "675b856adb13e378", + "turns": "042b43fe0b84465b", + "mouths": "63f04bec41c0daeb" + }, + "head-round": { + "front": "9ea6b01ec8554c81", + "turns": "14256b9703382496", + "mouths": "b20d2ee2e500015d" + }, + "head-oval": { + "front": "9610dc13d9d1c2c0", + "turns": "c0778e496e3794d7", + "mouths": "fd8c793d3caa3ec6" + }, + "head-square": { + "front": "9d1d011d37eeca20", + "turns": "1d197b575b75f528", + "mouths": "e757d98449767c24" + }, + "head-long": { + "front": "bfd9f11f4725330c", + "turns": "c4052ba3f3c8f4eb", + "mouths": "353197d9e5da9d67" + }, + "head-heart": { + "front": "0048764473889da0", + "turns": "308e3edc87ab52b0", + "mouths": "0564324382c9fe6d" + }, + "head-vchin": { + "front": "0bfb23ea3c7f7b9a", + "turns": "e00a764990b4bbbb", + "mouths": "c55e81282898fb9d" + }, + "head-chubby": { + "front": "48bcff712b6b40ae", + "turns": "7b7da1f30c013442", + "mouths": "0cf0808c0a055bc3" + }, + "head-diamond": { + "front": "75931bfc247d0a0a", + "turns": "8ae422e323fd5538", + "mouths": "f7faf6ae0f338bf6" + }, + "eyes-round": { + "front": "9ea6b01ec8554c81", + "turns": "14256b9703382496", + "mouths": "b20d2ee2e500015d" + }, + "eyes-dot": { + "front": "699b596645a72228", + "turns": "9af3a47d1b16200d", + "mouths": "ce8bf15e776cbc61" + }, + "eyes-sparkle": { + "front": "59d16443126d4476", + "turns": "e437a1fe444d8009", + "mouths": "8ad123c9222e6698" + }, + "eyes-lashes": { + "front": "787e2365e983be88", + "turns": "2507a72c862a7bed", + "mouths": "c8d95a67887be2ab" + }, + "eyes-sleepy": { + "front": "1206a6356d73d486", + "turns": "d0e231988d514d4f", + "mouths": "5de24f720777bdb6" + }, + "eyes-happy": { + "front": "2cc8a5bb11058377", + "turns": "d0192718648f8b26", + "mouths": "bc0e2440a551998a" + }, + "eyes-wink": { + "front": "a8685721ea16d1df", + "turns": "a089ac414dc3a8fa", + "mouths": "66fee2df79f936df" + }, + "eyes-sharp": { + "front": "0fc79a49f3e73674", + "turns": "fdffb208d347f55d", + "mouths": "524f15c3ec3330a3" + }, + "mouth-smile": { + "front": "9ea6b01ec8554c81", + "turns": "14256b9703382496", + "mouths": "b20d2ee2e500015d" + }, + "mouth-grin": { + "front": "5b5c34bc8771d4de", + "turns": "4244c8572b501cc4", + "mouths": "a75c6c4bb8562b16" + }, + "mouth-open": { + "front": "9cd8ba254b585687", + "turns": "a6235ad7c96672d3", + "mouths": "96328376d88bec3a" + }, + "mouth-flat": { + "front": "9cab2c41bb39613a", + "turns": "9897fdb6ce638b40", + "mouths": "8c0448bb638a85df" + }, + "mouth-smirk": { + "front": "e8758e05f0735a28", + "turns": "5279f907cf10080b", + "mouths": "97d1124dd4041d85" + }, + "mouth-cat": { + "front": "15ab5ad04aae5ef1", + "turns": "d8da17290b00309d", + "mouths": "478d897210a0719f" + }, + "mouth-tongue": { + "front": "3308303c3f08b5a4", + "turns": "d1209ad3dd053473", + "mouths": "e0b4cd9fdaeb974b" + }, + "mouth-o": { + "front": "2e17ad35b0ffeda4", + "turns": "ddabf6dc063565cb", + "mouths": "1811a314e83587f6" + }, + "accessory-none": { + "front": "9ea6b01ec8554c81", + "turns": "14256b9703382496", + "mouths": "b20d2ee2e500015d" + }, + "accessory-beanie": { + "front": "1aa17d1aef66732b", + "turns": "e062cff1972408c2", + "mouths": "634d89736cab0149" + }, + "accessory-cap": { + "front": "5fee40bd0e4fb34e", + "turns": "603fa1e4e4815aaf", + "mouths": "9f23fb3e80d9cadc" + }, + "accessory-headphones": { + "front": "d0b65c58b26de992", + "turns": "d09675a9059fb004", + "mouths": "34ba3fa5440a2655" + }, + "accessory-flower": { + "front": "83cf569826e07137", + "turns": "f6e34946929a717e", + "mouths": "84d8b352b877f4f3" + }, + "accessory-bow": { + "front": "ce57a1ace3c068dc", + "turns": "8cd09a54d7943486", + "mouths": "970b37e4ce729793" + }, + "accessory-earring": { + "front": "69e3d460b200d5de", + "turns": "f059e9260c67877f", + "mouths": "afb7a2d522f46827" + }, + "accessory-crown": { + "front": "21e057a62b37b7d5", + "turns": "d4bd3857c085d938", + "mouths": "cecc4e3d8a9a411c" + }, + "accessory-halo": { + "front": "30e42ccf6f9f51ed", + "turns": "da5bdb10f2172a61", + "mouths": "898599c4b7a96acd" + }, + "accessory-catears": { + "front": "bf15f2e0bce0dbe2", + "turns": "6f40fccaf697ea8d", + "mouths": "da0c538ea801d61f" + }, + "accessory-hairclip": { + "front": "61306bf63a99d951", + "turns": "61f1c0d6e9368487", + "mouths": "e0c1d8c4770ab0f7" + }, + "accessory-horns": { + "front": "f8646793fe9b566d", + "turns": "b0140392f4b0aafd", + "mouths": "ac1731bdce51df06" + }, + "accessory-beret": { + "front": "ed58897b7755c059", + "turns": "e0bd399c49b3908f", + "mouths": "d1f2efb29d3e38c5" + }, + "accessory-ribbon": { + "front": "b8dcc50e4973f032", + "turns": "828c888b4bc3493a", + "mouths": "616f10733d8a0ab3" + }, + "accessory-headband": { + "front": "95d53a08d9632580", + "turns": "1db122d960eca636", + "mouths": "76eaa4beaadd8d0b" + }, + "accessory-bunnyears": { + "front": "81cae16a65a559dd", + "turns": "e3a8b02fd36ce5e7", + "mouths": "a09668806b50496a" + }, + "accessory-horseears": { + "front": "4543e7a3ca735bd8", + "turns": "703d7933e0861718", + "mouths": "708c091abdf714a2" + }, + "accessory-flowercrown": { + "front": "df3b8fe0e37b58c7", + "turns": "01bc42ce54cac92c", + "mouths": "c262d1af14661a9d" + }, + "accessory-witch": { + "front": "e00c08bc48b14285", + "turns": "c7dbee39218bf037", + "mouths": "ce2fbcf3cf1b14d9" + }, + "accessory-pins": { + "front": "5d2b30c9c3688b96", + "turns": "31c79dc3240be7b3", + "mouths": "ff3d678221eeb677" + }, + "glasses-none": { + "front": "9ea6b01ec8554c81", + "turns": "14256b9703382496", + "mouths": "b20d2ee2e500015d" + }, + "glasses-round": { + "front": "5d46d56eb91840b1", + "turns": "856a8bb42ed10198", + "mouths": "2b1569f48a09d63a" + }, + "glasses-square": { + "front": "2ec8c6ca0d730855", + "turns": "14594478e945cecd", + "mouths": "a4df12731a89e22c" + }, + "glasses-shades": { + "front": "b0613bc95d0667d2", + "turns": "c3b095e1ff347a3a", + "mouths": "fb2df2609b50dacc" + }, + "glasses-monocle": { + "front": "b7347c41dc6c88b2", + "turns": "638b0add1f3e1d34", + "mouths": "7fb7f0da34f8c87a" + }, + "right-crop": { + "front": "581090110b645139", + "turns": "c23775ba5007d815", + "mouths": "12dd1b6bf4bc19fd" + }, + "right-bun": { + "front": "b6a573ade98bf7af", + "turns": "c511911b25f727f6", + "mouths": "5c77d3855634336c" + }, + "right-bob": { + "front": "80c350445fbc1120", + "turns": "8e59e039432b650f", + "mouths": "9ca3c1f678290692" + }, + "right-long": { + "front": "93c0a8f7ca366c27", + "turns": "796ef35784840763", + "mouths": "1482b88ca77e0924" + }, + "right-hime": { + "front": "2d005d6f9d8d6445", + "turns": "30a6b4fd1b7eee42", + "mouths": "2eb63e2dfcecfd6f" + }, + "right-curly": { + "front": "cb1e74ad5822acce", + "turns": "e313a03f4eb3b812", + "mouths": "9daaa490de5fbcc3" + }, + "right-braids": { + "front": "1d1c40c88bd4156d", + "turns": "783ebcd47f4c83c7", + "mouths": "9f0003610eb036e3" + }, + "right-none": { + "front": "7fdd35d37d1fc1b2", + "turns": "78e335f38603fd34", + "mouths": "a2b401c6cd883b18" + }, + "spacing=-1": { + "front": "9600ec09c52e7f2f", + "turns": "b3de25ff991c2f04", + "mouths": "3af3231c9b5933a1" + }, + "spacing=1": { + "front": "473d5531b84d834f", + "turns": "ee0a734eb117de0a", + "mouths": "de5f247f0a9bc658" + }, + "height=-1": { + "front": "1607d3d7565bffce", + "turns": "55dddd86daff91a2", + "mouths": "2a615e58d73a3dea" + }, + "height=1": { + "front": "064a175ee6c09ed0", + "turns": "4ec5a43ce9b745c7", + "mouths": "1d706e2401ea740e" + }, + "hairLength=-2": { + "front": "dae4f98b1a6a3e5a", + "turns": "4ebb107f091a8654", + "mouths": "a0542222be186a43" + }, + "hairLength=2": { + "front": "086e59622cf7cb09", + "turns": "92f6400e5c8003d6", + "mouths": "844bfc3d30b354f3" + }, + "two-tone": { + "front": "0b9aeccfc4dfdfaa", + "turns": "6e7b7215c670a2d1", + "mouths": "7ab874e2997586ba" + } +} diff --git a/packages/avatar/test/species.test.ts b/packages/avatar/test/species.test.ts new file mode 100644 index 0000000..c566ae1 --- /dev/null +++ b/packages/avatar/test/species.test.ts @@ -0,0 +1,125 @@ +import { createHash } from 'node:crypto'; +import { readFileSync, writeFileSync } from 'node:fs'; +import { describe, expect, it } from 'vitest'; +import { + AVATAR_HAIR_PARTS, + AVATAR_PARTS, + AVATAR_PRESETS, + AVATAR_RANGES, + AVATAR_SPECIES_SWATCHES, + AVATAR_TURNS, + DEFAULT_RECIPE, + canonicalRecipe, + detailedRecipe, + isPixelAvatarRecipe, + pixelAvatarSvg, + pixelFigure, + withSpecies, + type PixelAvatarRecipe, +} from '../src/index.js'; + +const hash = (s: string) => createHash('sha256').update(s).digest('hex').slice(0, 16); +const render = (recipe: PixelAvatarRecipe) => ({ + front: hash(pixelAvatarSvg(recipe)), + turns: hash(pixelAvatarSvg(recipe, { turns: AVATAR_TURNS })), + mouths: hash(pixelFigure(recipe, 0, { mouthLayers: true }).head), +}); + +const goblin = withSpecies(DEFAULT_RECIPE, 'goblin'); + +describe('asset version 2 recipes', () => { + it('validates species, split side hair and piece colors', () => { + expect(isPixelAvatarRecipe(goblin)).toBe(true); + expect(isPixelAvatarRecipe({ ...goblin, leftSideHairColor: '#E2B04A' })).toBe(true); + expect( + isPixelAvatarRecipe({ ...goblin, leftSideHairColor: '#e2b04a', rightSideHairColor: '#000' }), + ).toBe(false); + expect(isPixelAvatarRecipe({ ...goblin, species: 'dragon' })).toBe(false); + expect(isPixelAvatarRecipe({ ...goblin, rightSideHair: 'afro' })).toBe(false); + expect(isPixelAvatarRecipe({ ...goblin, assetVersion: 1 })).toBe(false); + expect(isPixelAvatarRecipe({ ...DEFAULT_RECIPE, assetVersion: 2 })).toBe(false); + const { bangs: _, ...partial } = goblin; + expect(isPixelAvatarRecipe(partial)).toBe(false); + const { species: __, rightSideHair: ___, ...v1 } = { ...goblin, assetVersion: 1 as const }; + expect(isPixelAvatarRecipe(v1)).toBe(true); + }); + + it('keeps version 2 fields when canonicalising', () => { + const recipe = { ...goblin, rightSideHair: 'bob' as const, rightSideHairColor: '#3FC1B8' }; + expect(canonicalRecipe(recipe)).toEqual({ ...recipe, rightSideHairColor: '#3fc1b8' }); + expect(canonicalRecipe(DEFAULT_RECIPE)).toEqual(DEFAULT_RECIPE); + }); + + it('switches species keeping choices and moving suggested skin colors only', () => { + expect(goblin).toMatchObject({ assetVersion: 2, species: 'goblin', rightSideHair: 'crop' }); + expect(goblin.skinColor).toBe(AVATAR_SPECIES_SWATCHES.goblin[0]); + const custom = withSpecies({ ...DEFAULT_RECIPE, skinColor: '#123456' }, 'goblin'); + expect(custom.skinColor).toBe('#123456'); + const back = withSpecies({ ...goblin, rightSideHair: 'bob' }, 'human'); + expect(back).toMatchObject({ species: 'human', rightSideHair: 'bob', hair: 'crop' }); + expect(back.skinColor).toBe(AVATAR_SPECIES_SWATCHES.human[0]); + }); + + it('renders a human version 2 recipe exactly like its version 1 source', () => { + const sources = [DEFAULT_RECIPE, ...AVATAR_PRESETS]; + for (const source of sources) { + const v2 = { ...withSpecies(source, 'human'), skinColor: source.skinColor }; + expect(pixelAvatarSvg(v2, { turns: AVATAR_TURNS })).toBe( + pixelAvatarSvg(detailedRecipe(source), { turns: AVATAR_TURNS }), + ); + } + }); + + it('ignores a piece color equal to the hair color', () => { + const same = { ...goblin, leftSideHairColor: goblin.hairColor.toUpperCase() }; + expect(pixelAvatarSvg(same)).toBe(pixelAvatarSvg(goblin)); + }); + + it('recolors only the chosen side piece', () => { + const bob = withSpecies({ ...DEFAULT_RECIPE, hair: 'bob' }, 'human'); + const left = pixelFigure({ ...bob, leftSideHairColor: '#f06292' }, 0).cells; + const plain = pixelFigure(bob, 0).cells; + const changed = left.filter((cell, i) => cell.c !== plain[i]!.c); + expect(changed.length).toBeGreaterThan(10); + expect(changed.every((cell) => cell.x < 16)).toBe(true); + }); + + it('draws goblin ears and tusks without hiding the speaking mouth', () => { + const human = pixelFigure(withSpecies(DEFAULT_RECIPE, 'human'), 0, { mouthLayers: true }); + const green = pixelFigure(goblin, 0, { mouthLayers: true }); + expect(green.head).toContain('#f6f0d8'); + expect(human.head).not.toContain('#f6f0d8'); + for (const state of ['closed', 'half-open', 'open']) + expect(green.head).toContain(`data-avatar-mouth="${state}"`); + const width = (cells: { x: number }[]) => + Math.max(...cells.map((c) => c.x)) - Math.min(...cells.map((c) => c.x)); + expect(width(green.cells)).toBeGreaterThanOrEqual(width(human.cells)); + }); +}); + +// New golden entries for asset version 2. They are generated once (BOTPIXEL_WRITE_SPECIES_GOLDEN=1) +// and then pin the artwork; never regenerate them to make an unrelated change pass. +describe('species golden', () => { + it('renders goblin parts, extremes, turns and split hair as pinned', () => { + const recipes: Record = { goblin }; + AVATAR_PRESETS.forEach((r, i) => (recipes[`preset-${i}`] = withSpecies(r, 'goblin'))); + for (const part of ['head', 'eyes', 'mouth', 'accessory', 'glasses'] as const) + for (const value of AVATAR_PARTS[part]) + recipes[`${part}-${value}`] = { ...goblin, [part]: value }; + for (const value of AVATAR_HAIR_PARTS.sideHair) + recipes[`right-${value}`] = { ...goblin, sideHair: 'none', rightSideHair: value }; + for (const [key, [min, max]] of Object.entries(AVATAR_RANGES)) + for (const value of [min, max]) + recipes[`${key}=${value}`] = { ...goblin, backHair: 'long', [key]: value }; + recipes['two-tone'] = { + ...withSpecies({ ...DEFAULT_RECIPE, hair: 'bob' }, 'goblin'), + leftSideHairColor: '#f06292', + rightSideHairColor: '#3fc1b8', + }; + const actual = Object.fromEntries(Object.entries(recipes).map(([k, r]) => [k, render(r)])); + const file = new URL('./fixtures/species-golden.json', import.meta.url); + if (process.env['BOTPIXEL_WRITE_SPECIES_GOLDEN'] === '1') + writeFileSync(file, `${JSON.stringify(actual, null, 2)}\n`); + expect(actual).toEqual(JSON.parse(readFileSync(file, 'utf8'))); + }); +}); From 5f727b6ba7c753cf1aec4b13bd4be344a8b333d2 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 8 Oct 2026 17:10:13 +0000 Subject: [PATCH 2/2] fix(avatar): type version 2 recipes so they carry what validation requires PixelAvatarRecipe becomes a union of PixelAvatarRecipeV1 and PixelAvatarRecipeV2. Version 2 requires species, the full split hair, geometry and rightSideHair; version 1 cannot carry version 2 fields. withSpecies returns PixelAvatarRecipeV2, DEFAULT_RECIPE and presets are version 1, and a type-level test rejects both mismatches. Issue: BotHarness/DeepSeekBot#1210 Agent-Task: claude/cloud/session_01MozvQ2eMvqX1MzSqm9G2o8 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MozvQ2eMvqX1MzSqm9G2o8 --- packages/avatar/src/index.ts | 2 ++ packages/avatar/src/recipe.ts | 44 +++++++++++++++++++--------- packages/avatar/test/species.test.ts | 9 ++++++ 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/packages/avatar/src/index.ts b/packages/avatar/src/index.ts index d5396ee..8e85c4c 100644 --- a/packages/avatar/src/index.ts +++ b/packages/avatar/src/index.ts @@ -30,6 +30,8 @@ export { type AvatarRange, type AvatarSpecies, type PixelAvatarRecipe, + type PixelAvatarRecipeV1, + type PixelAvatarRecipeV2, } from './recipe.js'; export { seededRandom } from './random.js'; export { AVATAR_TURNS, faceCells, pixelAvatarSvg, type PixelAvatarSvgOptions } from './svg.js'; diff --git a/packages/avatar/src/recipe.ts b/packages/avatar/src/recipe.ts index 48a9912..61b4082 100644 --- a/packages/avatar/src/recipe.ts +++ b/packages/avatar/src/recipe.ts @@ -160,21 +160,34 @@ export type AvatarSpecies = (typeof AVATAR_SPECIES)[number]; export const AVATAR_PIECE_COLORS = ['leftSideHairColor', 'rightSideHairColor'] as const; export type AvatarPieceColor = (typeof AVATAR_PIECE_COLORS)[number]; -export type PixelAvatarRecipe = { +type BaseRecipe = { schemaVersion: 1; family: 'illustrated'; - assetVersion: 1 | 2; rigVersion: 1; -} & { [P in AvatarPart]: (typeof AVATAR_PARTS)[P][number] } & Record & { - [P in AvatarHairPart]?: (typeof AVATAR_PARTS)['hair'][number]; - } & { [P in AvatarRange]?: number } & { - /** Asset version 2 only. */ - species?: AvatarSpecies; - /** Asset version 2 only: right side hair; `sideHair` is then the left side. */ - rightSideHair?: (typeof AVATAR_HAIR_PARTS)['sideHair'][number]; - } & { [P in AvatarPieceColor]?: string }; +} & { [P in AvatarPart]: (typeof AVATAR_PARTS)[P][number] } & Record; -export const DEFAULT_RECIPE: PixelAvatarRecipe = { +/** Asset version 1: optional split hair and geometry, all six or none. */ +export type PixelAvatarRecipeV1 = BaseRecipe & { assetVersion: 1 } & { + [P in AvatarHairPart]?: (typeof AVATAR_PARTS)['hair'][number]; +} & { [P in AvatarRange]?: number } & { + species?: never; + rightSideHair?: never; +} & { [P in AvatarPieceColor]?: never }; + +/** + * Asset version 2: a species, the full split hair and geometry, a separate right side hair + * (`sideHair` is then the left side) and optional per-side hair colors. + */ +export type PixelAvatarRecipeV2 = BaseRecipe & { assetVersion: 2 } & { + [P in AvatarHairPart]: (typeof AVATAR_PARTS)['hair'][number]; +} & { [P in AvatarRange]: number } & { + species: AvatarSpecies; + rightSideHair: (typeof AVATAR_HAIR_PARTS)['sideHair'][number]; +} & { [P in AvatarPieceColor]?: string }; + +export type PixelAvatarRecipe = PixelAvatarRecipeV1 | PixelAvatarRecipeV2; + +export const DEFAULT_RECIPE: PixelAvatarRecipeV1 = { schemaVersion: 1, family: 'illustrated', assetVersion: 1, @@ -266,7 +279,10 @@ export function canonicalRecipe(recipe: PixelAvatarRecipe): PixelAvatarRecipe { * When the skin color is one of the previous species' suggested colors, it moves to the new * species' first suggestion; a custom color is kept. */ -export function withSpecies(recipe: PixelAvatarRecipe, species: AvatarSpecies): PixelAvatarRecipe { +export function withSpecies( + recipe: PixelAvatarRecipe, + species: AvatarSpecies, +): PixelAvatarRecipeV2 { const detailed = detailedRecipe(recipe); const previous = recipe.species ?? 'human'; const suggested = AVATAR_SPECIES_SWATCHES[previous].includes(recipe.skinColor.toLowerCase()); @@ -277,7 +293,7 @@ export function withSpecies(recipe: PixelAvatarRecipe, species: AvatarSpecies): rightSideHair: detailed.rightSideHair ?? detailed.sideHair!, skinColor: suggested && previous !== species ? AVATAR_SPECIES_SWATCHES[species][0]! : recipe.skinColor, - } as PixelAvatarRecipe; + } as PixelAvatarRecipeV2; } export function detailedRecipe(recipe: PixelAvatarRecipe): PixelAvatarRecipe { @@ -400,7 +416,7 @@ export const AVATAR_SPECIES_SWATCHES: Record = goblin: ['#9cc464', '#7fae4f', '#6a9a45', '#b3cf7a', '#5c8a4a', '#8fa86a'], }; -const preset = (parts: Partial): PixelAvatarRecipe => ({ +const preset = (parts: Partial): PixelAvatarRecipeV1 => ({ ...DEFAULT_RECIPE, ...parts, }); diff --git a/packages/avatar/test/species.test.ts b/packages/avatar/test/species.test.ts index c566ae1..9576aa1 100644 --- a/packages/avatar/test/species.test.ts +++ b/packages/avatar/test/species.test.ts @@ -44,6 +44,15 @@ describe('asset version 2 recipes', () => { expect(isPixelAvatarRecipe(v1)).toBe(true); }); + it('types version 2 recipes so they carry what validation requires', () => { + // @ts-expect-error a version 2 recipe needs a species, split hair and geometry + const missing: PixelAvatarRecipe = { ...DEFAULT_RECIPE, assetVersion: 2 }; + expect(isPixelAvatarRecipe(missing)).toBe(false); + // @ts-expect-error species belongs to version 2 only + const stray: PixelAvatarRecipe = { ...DEFAULT_RECIPE, species: 'goblin' }; + expect(isPixelAvatarRecipe(stray)).toBe(false); + }); + it('keeps version 2 fields when canonicalising', () => { const recipe = { ...goblin, rightSideHair: 'bob' as const, rightSideHairColor: '#3FC1B8' }; expect(canonicalRecipe(recipe)).toEqual({ ...recipe, rightSideHairColor: '#3fc1b8' });