Files
Rosea/tools/art-prep/src/manifest.ts
T

255 lines
12 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* art/manifest.json: every picture the game needs, and the checks that keep the
* list honest before a single prompt is written (docs/ART.md §2).
*/
import { findForbidden } from '@rosea/content';
export type Background = 'full' | 'transparent';
export type AssetType = 'sheet' | 'spritesheet' | 'sprite' | 'layer' | 'icon';
export interface Cell {
readonly id: string;
/** What this one picture shows, finishing the sentence "Cell n: …". */
readonly does: string;
}
export interface Asset {
readonly id: string;
readonly type: AssetType;
/** The roadmap phase that first shows it (docs/ROADMAP.md §4). The manifest lists phases in order. */
readonly phase: number;
readonly width: number;
readonly height: number;
readonly background: Background;
/** False for a reference that never appears in the app (a character sheet). */
readonly shipped: boolean;
/** The smallest size, in px across, the app ever shows it; null when it is only shown large. */
readonly smallest: number | null;
readonly character?: string;
readonly usedBy: readonly string[];
readonly purpose: string;
readonly subject?: string;
readonly setting?: string;
readonly composition?: string;
readonly angle?: string;
readonly grid?: { readonly cols: number; readonly rows: number };
readonly cells?: readonly Cell[];
readonly include: readonly string[];
readonly avoid: readonly string[];
/** Shared avoid lines this picture may break, each named exactly (the gardener is a person: planet-garden.md). */
readonly allow?: readonly string[];
/** Indices into STYLE.md §7, the review points shown on its card. */
readonly checks: readonly number[];
/**
* A layer of a layered character: the sprite sheet it is painted over, in register (STYLE.md §3b).
* Its grid and cells are the guide's; it is made whole over the guide, never one picture at a time.
*/
readonly over?: string;
/** For a layer: which part it is, and its place in the stack (0 at the back). */
readonly part?: { readonly name: string; readonly stack: number };
}
/** One part of a layered character: one layer sheet for each combination of its axes' values. */
export interface LayerPart {
readonly part: string;
/** The choices (gardener.schema.json fields) this part is painted for, in id order. */
readonly axes: readonly string[];
/** Values that need no picture (no hat). */
readonly skip?: readonly string[];
/** The subject, with {axis} replaced by the words for each value. */
readonly subject: string;
}
/** A character drawn as stacked layers, all painted over one guide (STYLE.md §3b). */
export interface Layered {
readonly character: string;
/** The guide: a sprite sheet of the whole first look in every pose, never shipped. */
readonly over: string;
readonly phase: number;
/** The words each value stands for in a prompt, per axis. */
readonly words: Readonly<Record<string, Readonly<Record<string, string>>>>;
/** The first looks: their layers come first, so a whole first look can be judged early. */
readonly first: readonly Readonly<Record<string, string>>[];
/** Back to front. */
readonly parts: readonly LayerPart[];
readonly include: readonly string[];
readonly avoid: readonly string[];
readonly allow?: readonly string[];
readonly checks: readonly number[];
}
export interface Character {
readonly sheet: string;
readonly description: string;
}
export interface Manifest {
readonly characters: Readonly<Record<string, Character>>;
/** What no picture may show, appended to every prompt (STYLE.md §4). */
readonly avoid: readonly string[];
readonly assets: readonly Asset[];
/** Characters drawn as layers; expandLayers writes each out as ordinary sprite sheets. */
readonly layers?: readonly Layered[];
}
/** Every combination of the axes' values, in the order the words list them. */
function combinations(l: Layered, part: LayerPart): Record<string, string>[] {
let out: Record<string, string>[] = [{}];
for (const axis of part.axes) {
const values = Object.keys(l.words[axis] ?? {}).filter((v) => !(part.skip ?? []).includes(v));
out = out.flatMap((c) => values.map((v) => ({ ...c, [axis]: v })));
}
return out;
}
/** The id of a layer sheet: the character, the part, then each value (gardener-hair-braids-brown). */
export const layerId = (character: string, part: string, values: readonly string[]): string => [character, part, ...values].join('-');
/**
* The manifest with every layered character written out as ordinary sprite sheets, placed right
* after their guide: the first looks' layers first, then the rest. Expanding twice changes nothing.
*/
export function expandLayers(m: Manifest): Manifest {
if (!m.layers) return m;
const assets = [...m.assets];
for (const l of m.layers) {
const guide = assets.find((a) => a.id === l.over);
if (!guide) continue; // checkManifest names it
const firsts: Asset[] = [], rest: Asset[] = [];
l.parts.forEach((part, stack) => {
for (const values of combinations(l, part)) {
const id = layerId(l.character, part.part, part.axes.map((a) => values[a] ?? ''));
const subject = part.subject.replace(/\{(\w+)\}/g, (whole, axis: string) => l.words[axis]?.[values[axis] ?? ''] ?? whole);
const isFirst = l.first.some((f) => part.axes.every((a) => f[a] === values[a]));
const pose = (cell: string): string => cell.replace(`${l.character}-`, '');
const asset: Asset = {
id, type: 'spritesheet', phase: l.phase, width: guide.width, height: guide.height, background: 'transparent',
shipped: true, smallest: guide.smallest, character: l.character,
usedBy: [`The ${l.character}'s ${part.part}, layer ${stack + 1} of ${l.parts.length} from the back, stacked with the others in the scene (STYLE.md §3b).`],
purpose: `One layer of the ${l.character} (${part.part}${part.axes.length ? `: ${part.axes.map((a) => values[a]).join(', ')}` : ''}), in every pose of ${l.over}.`,
subject, angle: guide.angle ?? '', grid: guide.grid ?? { cols: 1, rows: 1 },
cells: (guide.cells ?? []).map((c) => ({ id: `${id}-${pose(c.id)}`, does: c.does })),
include: l.include, avoid: l.avoid, checks: l.checks, over: l.over,
part: { name: part.part, stack },
...(l.allow ? { allow: l.allow } : {})
};
(isFirst ? firsts : rest).push(asset);
}
});
assets.splice(assets.indexOf(guide) + 1, 0, ...firsts, ...rest);
}
const { layers: _expanded, ...plain } = m;
return { ...plain, assets };
}
/** The canvases image generators offer (STYLE.md §5). Anything else gets resized by the tool, unasked. */
export const GENERATOR_SIZES: readonly (readonly [number, number])[] = [
[1024, 1024],
[1536, 1024],
[1024, 1536]
];
const ID = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
const TYPES: readonly AssetType[] = ['sheet', 'spritesheet', 'sprite', 'layer', 'icon'];
/** Every text a prompt is built from, so the forbidden-names check reads all of it. */
function textsOf(a: Asset): string[] {
return [
a.purpose, a.subject, a.setting, a.composition, a.angle,
...a.usedBy, ...a.include, ...a.avoid,
...(a.cells ?? []).flatMap((c) => [c.id, c.does])
].filter((t): t is string => typeof t === 'string');
}
/**
* Problems with the manifest, each naming the asset. An empty list is a pass.
* `checklistLength` is the number of review points in STYLE.md §7.
*/
export function checkManifest(raw: Manifest, checklistLength: number): string[] {
const problems: string[] = [];
for (const l of raw.layers ?? []) {
const where = `layers of ${l.character}`;
const guide = raw.assets.find((a) => a.id === l.over);
if (!guide) problems.push(`${where}: the guide "${l.over}" is not in the manifest`);
else if (guide.type !== 'spritesheet') problems.push(`${where}: the guide "${l.over}" is a ${guide.type}, not a sprite sheet`);
else if (guide.shipped) problems.push(`${where}: the guide "${l.over}" is shipped; the app stacks the layers, so the guide never appears`);
for (const part of l.parts) {
for (const axis of part.axes) if (!l.words[axis]) problems.push(`${where}: part ${part.part} needs words for "${axis}"`);
for (const [, axis = ''] of part.subject.matchAll(/\{(\w+)\}/g)) {
if (!part.axes.includes(axis)) problems.push(`${where}: part ${part.part} says {${axis}} but is not painted per ${axis}`);
}
}
for (const text of [...Object.values(l.words).flatMap((w) => Object.values(w)), ...l.parts.map((p) => p.subject)]) {
for (const hit of findForbidden(text)) problems.push(`${where}: "${hit.term}" (${hit.category}) may not appear in a prompt`);
}
}
const m = expandLayers(raw);
const seen = new Set<string>();
const claim = (id: string, where: string): void => {
if (!ID.test(id)) problems.push(`${where}: id "${id}" must be lower-case words joined by hyphens`);
if (seen.has(id)) problems.push(`${where}: id "${id}" is used twice; each picture needs its own`);
seen.add(id);
};
for (const [name, c] of Object.entries(m.characters)) {
const sheet = m.assets.find((a) => a.id === c.sheet);
if (!sheet) problems.push(`character ${name}: its sheet "${c.sheet}" is not in the manifest`);
else if (sheet.type !== 'sheet') problems.push(`character ${name}: "${c.sheet}" is a ${sheet.type}, not a character sheet`);
}
let lastPhase = 0;
for (const a of m.assets) {
claim(a.id, a.id);
if (!Number.isInteger(a.phase) || a.phase < 1 || a.phase > 6) problems.push(`${a.id}: phase ${String(a.phase)} is not a roadmap phase (1–6)`);
else if (a.phase < lastPhase) problems.push(`${a.id}: phase ${a.phase} comes after phase ${lastPhase}; list the pictures in phase order`);
else lastPhase = a.phase;
if (!TYPES.includes(a.type)) problems.push(`${a.id}: unknown type "${a.type}"`);
if (!GENERATOR_SIZES.some(([w, h]) => w === a.width && h === a.height)) {
problems.push(`${a.id}: ${a.width} × ${a.height} is not a size image generators offer (${GENERATOR_SIZES.map(([w, h]) => `${w} × ${h}`).join(', ')})`);
}
if (a.purpose.trim() === '') problems.push(`${a.id}: no purpose`);
if (a.usedBy.length === 0) problems.push(`${a.id}: usedBy is empty; say where it appears`);
if (a.character !== undefined && !(a.character in m.characters)) problems.push(`${a.id}: unknown character "${a.character}"`);
if (a.type === 'sheet' && a.shipped) problems.push(`${a.id}: a character sheet is a reference and never shipped`);
if ((a.type === 'layer' || a.type === 'sprite' || a.type === 'spritesheet') && a.background !== 'transparent') {
problems.push(`${a.id}: a ${a.type} sits over other pictures, so its background must be transparent`);
}
if (a.type !== 'spritesheet' && (a.subject ?? '').trim() === '') problems.push(`${a.id}: no subject`);
for (const line of a.allow ?? []) {
if (!m.avoid.includes(line)) problems.push(`${a.id}: allows "${line}", which is not in the shared avoid list`);
}
for (const i of a.checks) {
if (!Number.isInteger(i) || i < 0 || i >= checklistLength) problems.push(`${a.id}: review point ${i} is not in STYLE.md §7`);
}
if (a.type === 'spritesheet') {
const g = a.grid;
const cells = a.cells ?? [];
if (!g) problems.push(`${a.id}: a sprite sheet needs a grid`);
else {
if (cells.length !== g.cols * g.rows) problems.push(`${a.id}: ${cells.length} cells for a ${g.cols} × ${g.rows} grid`);
if (a.width % g.cols !== 0 || a.height % g.rows !== 0) problems.push(`${a.id}: ${a.width} × ${a.height} does not divide into ${g.cols} × ${g.rows} whole-pixel cells`);
}
if ((a.angle ?? '').trim() === '') problems.push(`${a.id}: a sprite sheet needs an angle, so every cell is drawn alike`);
for (const c of cells) {
claim(c.id, `${a.id} cell`);
if (c.does.trim() === '') problems.push(`${a.id}: cell ${c.id} does not say what it shows`);
}
} else if (a.grid || a.cells) {
problems.push(`${a.id}: only a sprite sheet has a grid and cells`);
}
for (const text of textsOf(a)) {
for (const hit of findForbidden(text)) problems.push(`${a.id}: "${hit.term}" (${hit.category}) may not appear in a prompt`);
}
}
for (const [name, c] of Object.entries(m.characters)) {
for (const hit of findForbidden(c.description)) problems.push(`character ${name}: "${hit.term}" (${hit.category}) may not appear in a prompt`);
}
for (const text of m.avoid) {
for (const hit of findForbidden(text)) problems.push(`avoid list: "${hit.term}" (${hit.category}) may not appear in a prompt`);
}
return problems;
}