diff --git a/CHANGELOG.md b/CHANGELOG.md index 6abf2f9..3349907 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- `@botharness/pixel-avatar` 0.4.0: elf, dwarf, orc and flower species, an optional `beard` (`short`, `full`, `braided`) that always leaves the speaking mouth visible, armor/robe/tunic/cloak outfits and helmet/hood headwear (version 2 only, in `AVATAR_PARTS_V2`), and the flower's `petals` (`trumpet`, `daisy`, `sunflower`, `tulip`, `sakura`) and `flowerBase` (`leaves`, `pot`) in `AVATAR_EXTRA_PARTS`. A flower has simple bead eyes and its own mouth instead of a person's face. Petals take hair and per-side hair colors; a pot takes the shirt color. `hiddenChoices(recipe)` lists saved choices a species or headwear keeps but does not draw (a flower's hair, outfit, headwear, beard, eyes, brows, nose, cheeks and glasses, hair under a helmet or hood, a dwarf's nose part). `createSeededRecipeV2` / `seededRecipeV2` seed a version 2 face from a name across every species, with optional beards, version 2 outfits and flower parts; version 1 seeds are unchanged, so a consumer picks the seed version per identity. Version 1 recipes and every 0.3.0 goblin recipe render unchanged. - `@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-banner` 0.1.0 (new): seeded 3:1 pixel nature banners for profile headers in ten scenes. A `{ scene, seed }` recipe renders 150×50 pixels and scales them ×10 to exactly 1500×500 RGBA with no smoothing; `seededBannerRecipe(name)` gives the same banner for the same name, and a golden test locks every scene's output. - `@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. diff --git a/packages/avatar/README.md b/packages/avatar/README.md index dd9b168..1ed82f5 100644 --- a/packages/avatar/README.md +++ b/packages/avatar/README.md @@ -19,5 +19,6 @@ const svg = pixelAvatarSvg(recipe); // 32×32 viewBox, crisp edges, no ids or sc - `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. +- `AVATAR_PARTS_V2`, `AVATAR_EXTRA_PARTS`, `hiddenChoices(recipe)`: version 2 adds elf, dwarf, orc and flower species, armor/robe/tunic/cloak outfits, helmet/hood headwear, an optional `beard`, and a flower's `petals` and `flowerBase`. `hiddenChoices` names saved choices the current species or headwear keeps but does not draw, so an editor can say so instead of discarding them. - `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 ad5e398..46bf4e7 100644 --- a/packages/avatar/package.json +++ b/packages/avatar/package.json @@ -1,6 +1,6 @@ { "name": "@botharness/pixel-avatar", - "version": "0.3.0", + "version": "0.4.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 e3f7766..898f36b 100644 --- a/packages/avatar/src/figure.ts +++ b/packages/avatar/src/figure.ts @@ -474,6 +474,46 @@ function outfit(recipe: Recipe, g: Grid, s: number): void { paint(g, (x, y) => y === 24 && Math.abs(dx(x)) <= 4.5, WHITE); sprite(g, ['RR.RR', '.RRR.'], Math.round(C + s * 0.5) - 2, 24, { R: shirt }); break; + case 'armor': { + const metal = '#9aa3ad'; + paint(g, torso, metal); + paint(g, (x, y) => torso(x, y) && y >= 26 && Math.abs(dx(x)) <= 2.5, shirt); + paint( + g, + (x, y) => torso(x, y) && y <= 26 && Math.abs(dx(x)) >= half(y) - 3.5, + mix(metal, WHITE, 0.35), + ); + paint( + g, + (x, y) => torso(x, y) && y === 27 && Math.abs(dx(x)) >= half(y) - 3.5, + shade(metal, 0.7), + ); + paint(g, (x, y) => y === 24 && Math.abs(dx(x)) <= 3.5, shade(metal, 0.8)); + sprite(g, ['G'], Math.round(C + s * 0.5), 28, { G: GOLD }); + break; + } + case 'robe': + paint(g, v(5, 4), mix(shirt, WHITE, 0.7)); + paint( + g, + (x, y) => torso(x, y) && y <= 29 && Math.abs(Math.abs(dx(x)) - (4 - (y - 24) * 0.8)) <= 0.5, + GOLD, + ); + paint(g, (x, y) => torso(x, y) && y >= 28 && Math.abs(dx(x)) >= half(y) - 1.5, dark); + break; + case 'tunic': + paint(g, v(2, 2), shade(skin, 0.85)); + sprite(g, ['L.L', '.L.'], Math.round(C + s * 0.5) - 1, 25, { L: '#6b4a2b' }); + paint(g, (x, y) => torso(x, y) && y === 29, '#6b4a2b'); + sprite(g, ['G'], Math.round(C + s * 0.5), 29, { G: GOLD }); + break; + case 'cloak': { + const cloak = mix(shirt, '#2f3a4a', 0.55); + paint(g, (x, y) => torso(x, y) && Math.abs(dx(x)) >= 2.5, cloak); + paint(g, (x, y) => torso(x, y) && y === 24, shade(cloak, 0.8)); + sprite(g, ['G...G'], Math.round(C + s * 0.5) - 2, 25, { G: GOLD }); + break; + } case 'jacket': paint(g, v(6, 4.5), WHITE); paint( @@ -583,6 +623,39 @@ function accessory(recipe: Recipe, g: Grid, s: number, face: Mask): void { sprite(g, horn, Math.round(C + s) + 6, 1, { H: '#5b3a6e', h: '#7d5694' }, true); return; } + case 'helmet': { + const metal = '#9aa3ad'; + paint(g, (x, y) => cap(x, y) && y <= 11, metal); + paint(g, (x, y) => cap(x, y) && y === 11, shade(metal, 0.75)); + paint( + g, + (x, y) => cap(x, y) && y >= 12 && y <= 19 && Math.abs(x + 0.5 - C - s) >= 7.5, + metal, + ); + paint(g, (x, y) => Math.abs(x + 0.5 - C - s) <= 0.5 && y >= 11 && y <= 16, shade(metal, 0.7)); + paint( + g, + (x, y) => cap(x, y) && y >= 2 && y <= 5 && x + 0.5 < C + s - 2, + mix(metal, WHITE, 0.4), + ); + for (let x = Math.round(C + s) - 9; x <= Math.round(C + s) + 9; x += 3) + sprite(g, ['G'], x, 10, { G: GOLD }); + return; + } + case 'hood': { + const outer = ellipse(C + s, 15, 12.6, 12.4); + paint(g, (x, y) => outer(x, y) && y <= 25 && !(face(x, y) && y >= 10), shirt); + paint( + g, + (x, y) => + outer(x, y) && + !face(x, y) && + y >= 9 && + (face(x + 1, y) || face(x - 1, y) || face(x, y + 1)), + shade(shirt, 0.7), + ); + return; + } case 'beret': paint(g, (x, y) => ellipse(C + s + 1.5, 4.6, 10.5, 3.6)(x, y), shirt); paint(g, (x, y) => ellipse(C + s + 1.5, 4.6, 10.5, 3.6)(x, y) && y >= 6, shade(shirt, 0.78)); @@ -671,6 +744,76 @@ function accessory(recipe: Recipe, g: Grid, s: number, face: Mask): void { } } +const POINTED_EARS: Partial< + Record, { rows: string[]; rise: number }> +> = { + goblin: { rows: ['E...', 'EE..', '.EiE', '..Ee'], rise: 2 }, + elf: { rows: ['E....', 'EE...', '.EE..', '..EiE', '...Ee'], rise: 3 }, + orc: { rows: ['E..', 'EE.', '.Ee'], rise: 1 }, +}; + +const leafShape = + (cx: number, cy: number, long: number, wide: number, angle: number): Mask => + (x, y) => { + const px = x + 0.5 - cx; + const py = y + 0.5 - cy; + const u = px * Math.cos(angle) + py * Math.sin(angle); + const v = -px * Math.sin(angle) + py * Math.cos(angle); + return (u / long) ** 2 + (v / wide) ** 2 <= 1; + }; + +function petals(style: NonNullable, s: number): Mask { + const cx = C + s; + const cy = 16.6; + const ring = + (n: number, radius: number, long: number, wide: number, phase = 0): Mask => + (x, y) => + Array.from({ length: n }, (_, k) => phase + (2 * Math.PI * k) / n).some((a) => + leafShape(cx + radius * Math.cos(a), cy + radius * Math.sin(a), long, wide, a)(x, y), + ); + switch (style) { + case 'daisy': + return ring(14, 10.4, 3.4, 1.7, Math.PI / 14); + case 'sunflower': + return (x, y) => ring(16, 11.2, 2.6, 1.5)(x, y) || ellipse(cx, cy, 10.6, 10.2)(x, y); + case 'tulip': + return (x, y) => + y <= cy + 6 && + (ellipse(cx - 6.5, cy - 3, 4.5, 7.5)(x, y) || + ellipse(cx + 6.5, cy - 3, 4.5, 7.5)(x, y) || + ellipse(cx, cy - 6, 5, 8)(x, y)); + case 'trumpet': + return (x, y) => + ellipse(cx, cy - 0.5, 11.6, 10.4)(x, y) || + (y <= cy - 6 && ellipse(cx, cy - 3, 13, 8.6)(x, y)); + case 'sakura': + return (x, y) => + ring(5, 8.6, 4.6, 3.8, -Math.PI / 2)(x, y) && !ring(5, 13.4, 1.4, 1.4, -Math.PI / 2)(x, y); + } +} + +function flowerBase(recipe: Recipe, g: Grid, s: number): void { + const leaf = '#5f9e4a'; + const stem = C + s * 0.5; + paint(g, (x, y) => y >= 22 && Math.abs(x + 0.5 - stem) <= 1, shade(leaf, 0.82)); + if (recipe.flowerBase === 'pot') { + const pot = recipe.shirtColor; + const body: Mask = (x, y) => y >= 27 && Math.abs(x + 0.5 - stem) <= 6.5 - (y - 27) * 0.45; + paint(g, leafShape(stem - 3, 25, 2.8, 1.2, -0.5), leaf); + paint(g, leafShape(stem + 3, 24.5, 2.8, 1.2, 0.5), shade(leaf, 0.9)); + paint(g, body, pot); + paint(g, (x, y) => body(x, y) && y === 27, mix(pot, WHITE, 0.3)); + paint(g, (x, y) => body(x, y) && y >= 28 && x + 0.5 > stem + 3, shade(pot, 0.8)); + return; + } + const left = leafShape(stem - 4.5, 27, 4.4, 1.9, -0.55); + const right = leafShape(stem + 4.5, 25.5, 4.4, 1.9, 0.55); + paint(g, left, leaf); + paint(g, right, shade(leaf, 0.92)); + paint(g, leafShape(stem - 4.5, 27, 3.6, 0.5, -0.55), shade(leaf, 0.75)); + paint(g, leafShape(stem + 4.5, 25.5, 3.6, 0.5, 0.55), shade(leaf, 0.7)); +} + export function pixelTileColor(hair: string): string { const [r, g, b] = channels(hair).map((v) => v / 255) as [number, number, number]; const max = Math.max(r, g, b); @@ -716,14 +859,43 @@ export function pixelFigure( return front(x - Math.round(d * (1 + Math.max(0, y - 20) * 0.4) - d * tuck), y); } : front; - const masks: ReturnType = - recipe.bangs === undefined ? hair(recipe, s, b) : detailedHair(recipe, s, b); + const flower = recipe.species === 'flower'; + const covered = recipe.accessory === 'helmet' || recipe.accessory === 'hood'; + const bare: Mask = () => false; + const petalRing = flower ? petals(recipe.petals ?? 'trumpet', s) : bare; + const masks: ReturnType = flower + ? { + back: petalRing, + front: bare, + sides: [ + (x, y) => petalRing(x, y) && x + 0.5 < C + s, + (x, y) => petalRing(x, y) && x + 0.5 >= C + s, + ], + } + : covered + ? { back: bare, front: bare } + : recipe.bangs === undefined + ? hair(recipe, s, b) + : detailedHair(recipe, s, b); const body = blank(); - outfit(recipe, body, s); + if (flower) flowerBase(recipe, body, s); + else outfit(recipe, body, s); const back = blank(); - paint(back, masks.back, shade(hairColor, 0.82)); + if (flower) { + paint(back, petalRing, hairColor); + paint( + back, + (x, y) => petalRing(x, y) && (x + 0.5 - C - s) ** 2 + (y + 0.5 - 16.6) ** 2 < 90, + shade(hairColor, 0.84), + ); + paint( + back, + (x, y) => petalRing(x, y) && x + 0.5 < C + s - 3 && y < 10, + mix(hairColor, WHITE, 0.3), + ); + } else paint(back, masks.back, shade(hairColor, 0.82)); if (masks.bands) paint( back, @@ -733,19 +905,49 @@ export function pixelFigure( const head = blank(); paint(head, face, skin); + const h = recipe.height ?? 0; + if (recipe.beard && !flower) { + const centre = C + fs * 0.6; + const mouthX = Math.round(C - 0.5 + fs * 0.6); + const chin: Mask = (x, y) => face(x, y) || face(x, y - 2); + const open = (x: number, y: number) => y >= 20 + h && y <= 22 + h && Math.abs(x - mouthX) <= 3; + const full: Mask = (x, y) => + chin(x, y) && (y >= 22 + h || (Math.abs(x + 0.5 - centre) >= 4 && y >= 17 + h)); + const shape: Mask = + recipe.beard === 'short' + ? (x, y) => chin(x, y) && y >= 22 + h + : recipe.beard === 'full' + ? full + : (x, y) => full(x, y) || (Math.abs(x + 0.5 - centre) <= 1 && y >= 24 && y <= 30); + paint(head, (x, y) => shape(x, y) && !open(x, y), shade(hairColor, 0.9)); + paint( + head, + (x, y) => shape(x, y) && !open(x, y) && (x + y * 2) % 5 === 0, + shade(hairColor, 0.72), + ); + } const earY = 15; const edge = (side: number) => { const xs = [...Array(N).keys()].filter((x) => face(x, earY)); return side < 0 ? xs[0]! - 1 : xs.at(-1)! + 1; }; - 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 pointed = recipe.species ? POINTED_EARS[recipe.species] : undefined; + if (flower) { + // A flower has petals instead of ears. + } else if (pointed) { + // Pointed ears are drawn over hair: the outer tip rises above the ear line. const pal = { E: skin, e: shade(skin, 0.86), i: shade(skin, 0.74) }; + const width = pointed.rows[0]!.length; 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); + sprite( + head, + pointed.rows, + ex < 0 ? edge(ex) - (width - 1) : edge(ex), + earY - pointed.rise, + 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) }); @@ -754,10 +956,10 @@ export function pixelFigure( E: skin, e: shade(skin, 0.8), }); - if (!goblin) paint(head, masks.front, hairColor); + if (!pointed && !flower) 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); + if (!flower) accessory(recipe, head, s, face); const shadeHair = (g: Grid) => { const out = g.map((r) => [...r]); @@ -821,7 +1023,6 @@ export function pixelFigure( const closed = blank(); const glasses = blank(); const sp = recipe.spacing ?? 0; - const h = recipe.height ?? 0; const eyeTop = 15 + h; const eyeW = (side: -1 | 1) => (turned && Math.sign(fs) === side ? 3 : 4); const eyeX = (side: -1 | 1) => { @@ -841,20 +1042,29 @@ export function pixelFigure( I: recipe.eyeColor, L: mix(recipe.eyeColor, WHITE, 0.45), }; - const style = EYES[recipe.eyes]; - const extra = style[0]!.length - 4; - sprite(eyes, fit(style, left.w + extra), left.x - extra, eyeTop, pal); - sprite( - eyes, - recipe.eyes === 'wink' ? fit(CLOSED, right.w) : fit(style, right.w + extra), - right.x, - eyeTop, - pal, - true, - ); - sprite(closed, fit(CLOSED, left.w), left.x, eyeTop, pal); - sprite(closed, fit(CLOSED, right.w), right.x, eyeTop, pal, true); - const browRows = BROWS[recipe.brows]; + if (flower) { + const centre = Math.round(C + fs * 0.6); + const bead = { K: '#1d1b22', W: WHITE }; + for (const ex of [centre - 3, centre + 1]) { + sprite(eyes, ['KK', 'KW', 'KK', 'KK'], ex, eyeTop - 1, bead); + sprite(closed, ['..', '..', 'KK', '..'], ex, eyeTop - 1, bead); + } + } else { + const style = EYES[recipe.eyes]; + const extra = style[0]!.length - 4; + sprite(eyes, fit(style, left.w + extra), left.x - extra, eyeTop, pal); + sprite( + eyes, + recipe.eyes === 'wink' ? fit(CLOSED, right.w) : fit(style, right.w + extra), + right.x, + eyeTop, + pal, + true, + ); + sprite(closed, fit(CLOSED, left.w), left.x, eyeTop, pal); + sprite(closed, fit(CLOSED, right.w), right.x, eyeTop, pal, true); + } + const browRows = flower ? [] : BROWS[recipe.brows]; if (browRows.length) { sprite(features, fit(browRows, left.w), left.x, eyeTop - 2, { B: shade(hairColor, 0.55) }); sprite( @@ -867,9 +1077,17 @@ export function pixelFigure( ); } const cx = Math.round(C + fs * (turned ? 0.85 : 0.6)); - if (recipe.nose === 'dot') sprite(features, ['N'], cx, 18 + h, { N: shade(skin, 0.78) }); - if (recipe.nose === 'button') sprite(features, ['N'], cx, 18 + h, { N: shade(skin, 0.88) }); - if (recipe.nose === 'line') sprite(features, ['N', 'N'], cx, 17 + h, { N: shade(skin, 0.82) }); + if (flower) { + // A flower's face is only its bead eyes and its mouth. + } else if (recipe.species === 'dwarf') + sprite(features, ['.N.', 'NnN'], cx - 1, 17 + h, { + N: shade(skin, 0.84), + n: shade(skin, 0.72), + }); + else if (recipe.nose === 'dot') sprite(features, ['N'], cx, 18 + h, { N: shade(skin, 0.78) }); + else if (recipe.nose === 'button') sprite(features, ['N'], cx, 18 + h, { N: shade(skin, 0.88) }); + else if (recipe.nose === 'line') + sprite(features, ['N', 'N'], cx, 17 + h, { N: shade(skin, 0.82) }); const mx = C - 0.5 + fs * 0.6; const withMouth = (mouth: readonly string[]) => { const layer = features.map((row) => [...row]); @@ -882,11 +1100,14 @@ export function pixelFigure( 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') { + if (recipe.species === 'orc') + for (const tx of [Math.round(mx - 2.5), Math.round(mx + 2.5)]) + sprite(layer, ['T', 'T'], tx, 20 + h, { T: TUSK }); + if (recipe.cheeks === 'blush' && !flower) { 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) }); } - if (recipe.cheeks === 'freckles') { + if (recipe.cheeks === 'freckles' && !flower) { sprite(layer, ['F.F'], left.x, 19 + h, { F: shade(skin, 0.7) }); sprite(layer, ['F.F'], right.x + 1, 19 + h, { F: shade(skin, 0.7) }); } @@ -919,12 +1140,13 @@ export function pixelFigure( }, INK, ); - if (recipe.glasses === 'round' || recipe.glasses === 'square') { - frame(left.x, left.w, recipe.glasses === 'round'); - frame(right.x, right.w, recipe.glasses === 'round'); + const lenses = flower ? 'none' : recipe.glasses; + if (lenses === 'round' || lenses === 'square') { + frame(left.x, left.w, lenses === 'round'); + frame(right.x, right.w, lenses === 'round'); paint(glasses, (x, y) => y === eyeTop + 1 && x > left.x + left.w && x < right.x - 1, INK); } - if (recipe.glasses === 'shades') { + if (lenses === 'shades') { for (const e of [left, right]) paint( glasses, @@ -934,7 +1156,7 @@ export function pixelFigure( paint(glasses, (x, y) => y === eyeTop && x > left.x + left.w && x < right.x - 1, INK); for (const e of [left, right]) sprite(glasses, ['W'], e.x, eyeTop, { W: '#7d7b88' }); } - if (recipe.glasses === 'monocle') { + if (lenses === 'monocle') { frame(right.x, right.w, true); sprite(glasses, ['G', 'G', 'G'], right.x + right.w, eyeTop + 5, { G: GOLD }); } diff --git a/packages/avatar/src/index.ts b/packages/avatar/src/index.ts index 8e85c4c..2d60d4d 100644 --- a/packages/avatar/src/index.ts +++ b/packages/avatar/src/index.ts @@ -8,8 +8,10 @@ export { } from './figure.js'; export { AVATAR_COLORS, + AVATAR_EXTRA_PARTS, AVATAR_HAIR_PARTS, AVATAR_PARTS, + AVATAR_PARTS_V2, AVATAR_PIECE_COLORS, AVATAR_PRESETS, AVATAR_RANGES, @@ -19,11 +21,15 @@ export { DEFAULT_RECIPE, canonicalRecipe, createSeededRecipe, + createSeededRecipeV2, detailedRecipe, + hiddenChoices, isPixelAvatarRecipe, seededRecipe, + seededRecipeV2, withSpecies, type AvatarColor, + type AvatarExtraPart, type AvatarHairPart, type AvatarPart, type AvatarPieceColor, diff --git a/packages/avatar/src/recipe.ts b/packages/avatar/src/recipe.ts index 61b4082..66f60a2 100644 --- a/packages/avatar/src/recipe.ts +++ b/packages/avatar/src/recipe.ts @@ -154,36 +154,55 @@ 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 const AVATAR_SPECIES = ['human', 'goblin', 'elf', 'dwarf', 'orc', 'flower'] 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]; -type BaseRecipe = { - schemaVersion: 1; - family: 'illustrated'; - rigVersion: 1; -} & { [P in AvatarPart]: (typeof AVATAR_PARTS)[P][number] } & Record; +/** Part choices that exist only in asset version 2, added after every version 1 choice. */ +export const AVATAR_PARTS_V2 = { + ...AVATAR_PARTS, + outfit: [...AVATAR_PARTS.outfit, 'armor', 'robe', 'tunic', 'cloak'], + accessory: [...AVATAR_PARTS.accessory, 'helmet', 'hood'], +} as const satisfies { [P in AvatarPart]: readonly string[] }; +/** Optional asset version 2 parts; an absent part is not drawn (or uses the first choice). */ +export const AVATAR_EXTRA_PARTS = { + beard: ['short', 'full', 'braided'], + petals: ['trumpet', 'daisy', 'sunflower', 'tulip', 'sakura'], + flowerBase: ['leaves', 'pot'], +} as const; +export type AvatarExtraPart = keyof typeof AVATAR_EXTRA_PARTS; + +type Meta = { schemaVersion: 1; family: 'illustrated'; rigVersion: 1 } & Record< + AvatarColor, + string +>; /** Asset version 1: optional split hair and geometry, all six or none. */ -export type PixelAvatarRecipeV1 = BaseRecipe & { assetVersion: 1 } & { +export type PixelAvatarRecipeV1 = Meta & { assetVersion: 1 } & { + [P in AvatarPart]: (typeof AVATAR_PARTS)[P][number]; +} & { [P in AvatarHairPart]?: (typeof AVATAR_PARTS)['hair'][number]; } & { [P in AvatarRange]?: number } & { species?: never; rightSideHair?: never; -} & { [P in AvatarPieceColor]?: never }; +} & { [P in AvatarPieceColor | AvatarExtraPart]?: 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 } & { +export type PixelAvatarRecipeV2 = Meta & { assetVersion: 2 } & { + [P in AvatarPart]: (typeof AVATAR_PARTS_V2)[P][number]; +} & { [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 }; +} & { [P in AvatarPieceColor]?: string } & { + [P in AvatarExtraPart]?: (typeof AVATAR_EXTRA_PARTS)[P][number]; +}; export type PixelAvatarRecipe = PixelAvatarRecipeV1 | PixelAvatarRecipeV2; @@ -219,7 +238,10 @@ export function isPixelAvatarRecipe(value: unknown): value is PixelAvatarRecipe if (detailed !== 0 && detailed !== DETAIL_KEYS.length) 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; + const extras = (Object.keys(AVATAR_EXTRA_PARTS) as AvatarExtraPart[]).filter((key) => + Object.hasOwn(r, key), + ); + const v2Keys = v2 ? 2 + pieces.length + extras.length : 0; if (Object.keys(r).length !== Object.keys(DEFAULT_RECIPE).length + detailed + v2Keys) return false; if ( @@ -227,9 +249,11 @@ export function isPixelAvatarRecipe(value: unknown): value is PixelAvatarRecipe (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]))) + !pieces.every((key) => typeof r[key] === 'string' && /^#[\da-f]{6}$/iu.test(r[key])) || + !extras.every((key) => (AVATAR_EXTRA_PARTS[key] as readonly unknown[]).includes(r[key]))) ) return false; + const catalog: { [P in AvatarPart]: readonly string[] } = v2 ? AVATAR_PARTS_V2 : AVATAR_PARTS; return ( (detailed === 0 || ((Object.keys(AVATAR_HAIR_PARTS) as AvatarHairPart[]).every( @@ -246,10 +270,7 @@ export function isPixelAvatarRecipe(value: unknown): value is PixelAvatarRecipe r['family'] === 'illustrated' && (r['assetVersion'] === 1 || v2) && r['rigVersion'] === 1 && - PART_KEYS.every( - (key) => - typeof r[key] === 'string' && (AVATAR_PARTS[key] as readonly string[]).includes(r[key]), - ) && + PART_KEYS.every((key) => typeof r[key] === 'string' && catalog[key].includes(r[key])) && AVATAR_COLORS.every((key) => typeof r[key] === 'string' && /^#[\da-f]{6}$/iu.test(r[key])) ); } @@ -269,10 +290,38 @@ export function canonicalRecipe(recipe: PixelAvatarRecipe): PixelAvatarRecipe { canonical['rightSideHair'] = recipe.rightSideHair; for (const key of AVATAR_PIECE_COLORS) if (recipe[key] !== undefined) canonical[key] = recipe[key].toLowerCase(); + for (const key of Object.keys(AVATAR_EXTRA_PARTS) as AvatarExtraPart[]) + if (recipe[key] !== undefined) canonical[key] = recipe[key]; } return canonical as PixelAvatarRecipe; } +const HAIR_PIECES = ['bangs', 'sideHair', 'rightSideHair', 'backHair'] as const; + +/** + * Saved choices the recipe keeps but does not draw: a flower shows petals and a stem instead of + * hair, outfit, beard and headwear, and a helmet or hood covers the hair. The choices return when + * the species or headwear changes back. + */ +export function hiddenChoices(recipe: PixelAvatarRecipe): readonly string[] { + if (recipe.species === 'flower') + return [ + ...HAIR_PIECES, + 'outfit', + 'accessory', + 'eyes', + 'brows', + 'nose', + 'cheeks', + 'glasses', + ...(recipe.beard ? ['beard'] : []), + ]; + const hidden: string[] = []; + if (recipe.accessory === 'helmet' || recipe.accessory === 'hood') hidden.push(...HAIR_PIECES); + if (recipe.species === 'dwarf') hidden.push('nose'); + return hidden; +} + /** * 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. @@ -406,6 +455,38 @@ export function createSeededRecipe(namespace: string): (seed: string) => PixelAv }; } +/** + * A version 2 name-seeded recipe factory over every species. Species are equally likely; a + * person may get a beard or a version 2 outfit, and a flower gets petals and a base. Version 1 + * factories (`createSeededRecipe`) keep their faces, so a consumer chooses per identity which + * seed version it was created with. + */ +export function createSeededRecipeV2(namespace: string): (seed: string) => PixelAvatarRecipeV2 { + const base = createSeededRecipe(`${namespace}:base`); + return (seed) => { + const random = seededRandom(`${namespace}:${seed.trim().toLowerCase()}`); + const pick = (values: readonly T[]) => values[Math.floor(random() * values.length)]!; + const species = pick(AVATAR_SPECIES); + const recipe: PixelAvatarRecipeV2 = { + ...withSpecies(base(seed), species), + skinColor: pick(AVATAR_SPECIES_SWATCHES[species]), + }; + if (species === 'flower') + return { + ...recipe, + petals: pick(AVATAR_EXTRA_PARTS.petals), + flowerBase: pick(AVATAR_EXTRA_PARTS.flowerBase), + }; + const beard = random() < 0.3 ? pick(AVATAR_EXTRA_PARTS.beard) : undefined; + const outfit = random() < 0.25 ? pick(AVATAR_PARTS_V2.outfit.slice(-4)) : recipe.outfit; + return { ...recipe, outfit, ...(beard ? { beard } : {}) }; + }; +} + +/** Version 2 counterpart of `seededRecipe` (BotHarness's namespace). */ +export const seededRecipeV2: (seed: string) => PixelAvatarRecipeV2 = + createSeededRecipeV2('botharness-avatar-v2'); + /** The same name always gives the same face (BotHarness's namespace). */ export const seededRecipe: (seed: string) => PixelAvatarRecipe = createSeededRecipe('botharness-avatar'); @@ -414,6 +495,10 @@ export const seededRecipe: (seed: string) => PixelAvatarRecipe = export const AVATAR_SPECIES_SWATCHES: Record = { human: AVATAR_SWATCHES.skinColor, goblin: ['#9cc464', '#7fae4f', '#6a9a45', '#b3cf7a', '#5c8a4a', '#8fa86a'], + elf: ['#fbe7d6', '#f2d3c0', '#e2c9b0', '#d9c7e8', '#b9c6dd', '#8f7fa8'], + dwarf: ['#f2c4a0', '#e9b08a', '#e0a87e', '#c68863', '#f0b8a0', '#9a6142'], + orc: ['#8a9a5b', '#6f8a4e', '#7d8c6a', '#5f6f4a', '#9aa070', '#6a7a7a'], + flower: ['#f6d36b', '#f2b84b', '#ffe3cf', '#e8a85a', '#c98b4a', '#f4f1ec'], }; const preset = (parts: Partial): PixelAvatarRecipeV1 => ({ diff --git a/packages/avatar/test/fixtures/species-more-golden.json b/packages/avatar/test/fixtures/species-more-golden.json new file mode 100644 index 0000000..b0c411b --- /dev/null +++ b/packages/avatar/test/fixtures/species-more-golden.json @@ -0,0 +1,172 @@ +{ + "elf": { + "front": "5215edb346d77629", + "turns": "210e6ecb819b147b", + "mouths": "0ee8ce0e8e8911d9" + }, + "dwarf": { + "front": "07f7085fb8193e96", + "turns": "22474f2c3aced89f", + "mouths": "fc88403e13a1f315" + }, + "orc": { + "front": "840822625620149b", + "turns": "9119c3b1d43a9a0e", + "mouths": "9ed85c495e5a2bd5" + }, + "flower": { + "front": "2d5a1fe217d5a685", + "turns": "e137cd0904bbb963", + "mouths": "4a18005fbfa96018" + }, + "beard-short-round": { + "front": "1f02b7eee00f2cc0", + "turns": "7f8860471fd456c1", + "mouths": "09cd8dd63a670092" + }, + "beard-short-square": { + "front": "fe388ce81f17c441", + "turns": "03cf0549feafedc0", + "mouths": "03064abd0584fb6e" + }, + "beard-full-round": { + "front": "00c8c4c383c6a44e", + "turns": "e9a73a870c3b0792", + "mouths": "fdedb118d880acef" + }, + "beard-full-square": { + "front": "64f2fc3bca17e36c", + "turns": "e05a54f8db2e13fe", + "mouths": "0f5fbfc04b13069b" + }, + "beard-braided-round": { + "front": "caff515eafe82628", + "turns": "790a2002bc94d397", + "mouths": "fef8930d85fd2e99" + }, + "beard-braided-square": { + "front": "6a6fbe19a176af42", + "turns": "8910eb9359293718", + "mouths": "9f3d7060f75f8654" + }, + "outfit-armor": { + "front": "ccc1b18b36999f82", + "turns": "3058a63e7b6d50f8", + "mouths": "fc88403e13a1f315" + }, + "outfit-robe": { + "front": "cf20c43748127470", + "turns": "a93f8ab2bab8503e", + "mouths": "fc88403e13a1f315" + }, + "outfit-tunic": { + "front": "d9f910b06a8b6f71", + "turns": "c171777eab438686", + "mouths": "fc88403e13a1f315" + }, + "outfit-cloak": { + "front": "d575fc517f876550", + "turns": "add8016a0bfec1b6", + "mouths": "fc88403e13a1f315" + }, + "accessory-helmet": { + "front": "db5fc770c35b7e1a", + "turns": "d0dcbaac42b66278", + "mouths": "27defd729e331400" + }, + "accessory-hood": { + "front": "066f3563fbc4ad43", + "turns": "b6436098c853a1ac", + "mouths": "cc9c702d8cc945c4" + }, + "flower-trumpet-leaves": { + "front": "2d5a1fe217d5a685", + "turns": "e137cd0904bbb963", + "mouths": "4a18005fbfa96018" + }, + "flower-trumpet-pot": { + "front": "00a8e10191ba9a20", + "turns": "3ea98458d8ac9dda", + "mouths": "49e0c972604878ba" + }, + "flower-daisy-leaves": { + "front": "eca5f7f645baa53f", + "turns": "e7258b550e353fe6", + "mouths": "62d327d25401dac2" + }, + "flower-daisy-pot": { + "front": "3d41fdd8a8c560fe", + "turns": "21e9f33479e9df06", + "mouths": "ca198e261aab8f5f" + }, + "flower-sunflower-leaves": { + "front": "d94e4e6536f9de8c", + "turns": "85c3802a2d11a5bb", + "mouths": "3ca3b8c9f71f50a1" + }, + "flower-sunflower-pot": { + "front": "dbf1af8e36481e2c", + "turns": "dc62080ed66e09d4", + "mouths": "fb3d9b3201e38bcd" + }, + "flower-tulip-leaves": { + "front": "588383a46e642f69", + "turns": "0b28cca2ab1186ba", + "mouths": "d8d52209f7d6d7bb" + }, + "flower-tulip-pot": { + "front": "bc25b7957e8fe5e6", + "turns": "957d13f07762e5a0", + "mouths": "d8d52209f7d6d7bb" + }, + "flower-sakura-leaves": { + "front": "76c3f187c3b3a50e", + "turns": "89915f9f5bdfaf8f", + "mouths": "eeda49701a55e2bc" + }, + "flower-sakura-pot": { + "front": "747bb90d5a473e50", + "turns": "1fbeaea37de6a931", + "mouths": "cab3c5244c660713" + }, + "flower-two-tone": { + "front": "62141d463e196c07", + "turns": "2d42e3f9075c57db", + "mouths": "5b997c26249730e9" + }, + "seed-v2-Ada": { + "front": "2c83354c4f2bfc1d", + "turns": "53412b05f6d16e8a", + "mouths": "1cafd08079c3aa6d" + }, + "seed-v2-Grace": { + "front": "2e759f757e74ea8c", + "turns": "0023b4ee4eeb42dc", + "mouths": "3507341cffa95b6c" + }, + "seed-v2-Linus": { + "front": "148ad21f26cd646f", + "turns": "bf9fdac6029d23c9", + "mouths": "17c8adf1b547264d" + }, + "seed-v2-DeepSeekBot": { + "front": "ee9bd7d0074b2970", + "turns": "b29ceaeb4f4b892e", + "mouths": "6e1c2b03e91e0f37" + }, + "seed-v2-Gimli": { + "front": "e96ad6a0c3a2bbd5", + "turns": "bba0f25e8f34f527", + "mouths": "1bc89d032efe42fb" + }, + "seed-v2-Daisy": { + "front": "5e91746f1f3ac026", + "turns": "81cbbd98734b2d6e", + "mouths": "cc834e3d2d0d1124" + }, + "dwarf-extremes": { + "front": "74207120892ffbe9", + "turns": "764841ff6d7195db", + "mouths": "0d5a78f1f62a5521" + } +} diff --git a/packages/avatar/test/species-more.test.ts b/packages/avatar/test/species-more.test.ts new file mode 100644 index 0000000..62bd0b6 --- /dev/null +++ b/packages/avatar/test/species-more.test.ts @@ -0,0 +1,153 @@ +import { createHash } from 'node:crypto'; +import { readFileSync, writeFileSync } from 'node:fs'; +import { describe, expect, it } from 'vitest'; +import { + AVATAR_EXTRA_PARTS, + AVATAR_PARTS_V2, + AVATAR_TURNS, + DEFAULT_RECIPE, + canonicalRecipe, + hiddenChoices, + isPixelAvatarRecipe, + pixelAvatarSvg, + pixelFigure, + seededRecipe, + seededRecipeV2, + 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 dwarf = withSpecies(DEFAULT_RECIPE, 'dwarf'); +const flower = withSpecies(DEFAULT_RECIPE, 'flower'); + +describe('medieval species, beards, outfits and headwear', () => { + it('accepts the new species, extras and version 2 catalog choices only in version 2', () => { + for (const species of ['elf', 'dwarf', 'orc', 'flower'] as const) + expect(isPixelAvatarRecipe(withSpecies(DEFAULT_RECIPE, species))).toBe(true); + expect(isPixelAvatarRecipe({ ...dwarf, beard: 'braided', outfit: 'armor' })).toBe(true); + expect(isPixelAvatarRecipe({ ...dwarf, accessory: 'helmet' })).toBe(true); + expect(isPixelAvatarRecipe({ ...flower, petals: 'tulip', flowerBase: 'pot' })).toBe(true); + expect(isPixelAvatarRecipe({ ...dwarf, beard: 'goatee' })).toBe(false); + expect(isPixelAvatarRecipe({ ...DEFAULT_RECIPE, outfit: 'armor' })).toBe(false); + expect(isPixelAvatarRecipe({ ...DEFAULT_RECIPE, beard: 'full' })).toBe(false); + expect(canonicalRecipe({ ...dwarf, beard: 'full' })).toEqual({ ...dwarf, beard: 'full' }); + }); + + it('reports choices it keeps but does not draw', () => { + expect(hiddenChoices(DEFAULT_RECIPE)).toEqual([]); + expect(hiddenChoices({ ...dwarf, accessory: 'hood' })).toEqual([ + 'bangs', + 'sideHair', + 'rightSideHair', + 'backHair', + 'nose', + ]); + expect(hiddenChoices({ ...flower, beard: 'full' })).toEqual([ + 'bangs', + 'sideHair', + 'rightSideHair', + 'backHair', + 'outfit', + 'accessory', + 'eyes', + 'brows', + 'nose', + 'cheeks', + 'glasses', + 'beard', + ]); + }); + + it('hides hair under a helmet or hood and restores it when removed', () => { + for (const accessory of ['helmet', 'hood'] as const) { + const long = { ...dwarf, accessory, backHair: 'long' as const, bangs: 'messy' as const }; + expect(pixelAvatarSvg(long)).toBe(pixelAvatarSvg({ ...dwarf, accessory })); + } + expect(pixelAvatarSvg({ ...dwarf, backHair: 'long' })).not.toBe(pixelAvatarSvg(dwarf)); + }); + + it('keeps hair, outfit and beard saved but undrawn on a flower', () => { + const dressed = { + ...flower, + outfit: 'armor' as const, + beard: 'full' as const, + bangs: 'messy' as const, + }; + expect(pixelAvatarSvg(dressed)).toBe(pixelAvatarSvg(flower)); + }); + + it('gives a flower bead eyes instead of the saved eyes, brows, nose, cheeks and glasses', () => { + const plain = pixelAvatarSvg(flower); + for (const [part, value] of [ + ['eyes', 'sparkle'], + ['brows', 'angry'], + ['nose', 'line'], + ['cheeks', 'freckles'], + ['glasses', 'shades'], + ] as const) + expect(pixelAvatarSvg({ ...flower, [part]: value }), part).toBe(plain); + for (const state of ['closed', 'half-open', 'open']) + expect(pixelFigure(flower, 0, { mouthLayers: true }).head).toContain( + `data-avatar-mouth="${state}"`, + ); + }); + + it('leaves every speaking mouth visible through a beard', () => { + for (const beard of AVATAR_EXTRA_PARTS.beard) { + const head = pixelFigure({ ...dwarf, beard }, 0, { mouthLayers: true }).head; + for (const state of ['closed', 'half-open', 'open']) { + const layer = head.split(`data-avatar-mouth="${state}"`)[1]!.split('')[0]!; + expect(layer, `${beard} ${state}`).toContain('#7a2a38'); + } + } + }); + + it('renders the new species, parts, turns and mouths as pinned', () => { + const recipes: Record = {}; + for (const species of ['elf', 'dwarf', 'orc', 'flower'] as const) + recipes[species] = withSpecies(DEFAULT_RECIPE, species); + for (const beard of AVATAR_EXTRA_PARTS.beard) + for (const head of ['round', 'square'] as const) + recipes[`beard-${beard}-${head}`] = { ...dwarf, beard, head }; + for (const outfit of AVATAR_PARTS_V2.outfit.slice(-4)) + recipes[`outfit-${outfit}`] = { ...dwarf, outfit }; + for (const accessory of AVATAR_PARTS_V2.accessory.slice(-2)) + recipes[`accessory-${accessory}`] = { ...withSpecies(DEFAULT_RECIPE, 'elf'), accessory }; + for (const petals of AVATAR_EXTRA_PARTS.petals) + for (const flowerBase of AVATAR_EXTRA_PARTS.flowerBase) + recipes[`flower-${petals}-${flowerBase}`] = { ...flower, petals, flowerBase }; + recipes['flower-two-tone'] = { + ...flower, + petals: 'sakura', + leftSideHairColor: '#f06292', + rightSideHairColor: '#3fc1b8', + }; + for (const name of ['Ada', 'Grace', 'Linus', 'DeepSeekBot', 'Gimli', 'Daisy']) + recipes[`seed-v2-${name}`] = seededRecipeV2(name); + recipes['dwarf-extremes'] = { ...dwarf, beard: 'braided', height: 1, spacing: 1 }; + const actual = Object.fromEntries(Object.entries(recipes).map(([k, r]) => [k, render(r)])); + const file = new URL('./fixtures/species-more-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'))); + }); + + it('seeds a valid, stable face from every species without changing version 1 seeds', () => { + const names = Array.from({ length: 300 }, (_, i) => `bot-${i}`); + const faces = names.map((name) => seededRecipeV2(name)); + for (const face of faces) expect(isPixelAvatarRecipe(face)).toBe(true); + expect(new Set(faces.map((face) => face.species))).toEqual( + new Set(['human', 'goblin', 'elf', 'dwarf', 'orc', 'flower']), + ); + expect(faces.some((face) => face.beard)).toBe(true); + expect(seededRecipeV2('Ada')).toEqual(seededRecipeV2(' ada ')); + expect(seededRecipe('Ada').assetVersion).toBe(1); + }); +});