Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

8.2 KB
Raw Blame
/**
 * Wearable accessories for the agent symbol and its map companion.
 *
 * accessories.json is generated by scripts/art/companion_accessories.py. Every
 * part is a small 3D primitive in symbol units around a slot anchor; the flat
 * symbol draws its front projection and the map companion loads the same parts
 * as meshes from accessories.glb, so both surfaces always show the same look.
 */

import catalogJson from "./accessories.json";
import type { SymbolShape } from "./appearance";

export const ACCESSORY_SLOTS = ["back", "outfit", "neck", "mouth", "face", "head", "held"] as const;
export type AccessorySlot = typeof ACCESSORY_SLOTS[number];
/** Persisted choice: at most one item per slot. */
export type AccessoryChoice = Partial<Record<AccessorySlot, string>>;

type Vec2 = [number, number];
type Vec3 = [number, number, number];
/** "2d" parts are flat-only shading; "3d" parts exist only as meshes. */
interface PartBase {
  fill: string; glow?: boolean; rot?: number; only?: "2d" | "3d";
  /** Flat shading: a translucent shadow or highlight. */
  opacity?: number;
  /** 3D finish: polished metal or a glossy surface. */
  metal?: boolean; gloss?: boolean;
  /** Flat vertical gradient, top to bottom. */
  gradient?: [string, string];
  /** A looping flat animation (agentSymbol.css). */
  anim?: "twinkle" | "twinkle-late" | "ember";
}
export type AccessoryPart = PartBase & (
  | { t: "sphere" | "dome"; c: Vec3; r: Vec3 }
  | { t: "box"; c: Vec3; s: Vec3; round: number }
  | { t: "cyl"; c: Vec3; r: [number, number]; h: number; dz: number }
  | { t: "torus"; c: Vec3; R: number; r: number; tilt: number }
  | { t: "poly"; pts: Vec2[]; z: number; d: number; smooth: boolean }
  | { t: "tube"; pts: Vec3[]; w: number; smooth: boolean }
  | { t: "region"; pts: Vec2[]; mode: "wrap" | "front" }
  /** The body silhouette grown by w symbol units, behind the body (a hood). */
  | { t: "rim"; w: number; maxY?: number }
  /** Animated smoke rising from c (flat puffs; particles in the 3D world). */
  | { t: "smoke"; c: Vec3; size: number }
);
export interface AccessoryItem {
  id: string;
  slot: AccessorySlot;
  /** Item size on top of the slot anchor's scale. */
  scale?: number;
  /** Front-projection bounds around the anchor, before the shape's scale. */
  bounds: [number, number, number, number];
  /** True when some parts are patches of the body surface (clothing). */
  regions: boolean;
  parts: AccessoryPart[];
}
/** [x, y, scale] in symbol coordinates. */
export type AnchorPoint = [number, number, number];
export interface AccessoryShape {
  top: number; bottom: number; eyeY: number; anchors: Record<AccessorySlot, AnchorPoint>;
  /** Measured body surface depth (m, toward the viewer) under each slot anchor. */
  depth?: Partial<Record<AccessorySlot, number>>;
}

/** Where a slot's free parts rest in front of (or behind) the body centre, in metres. */
export function slotDepthM(shape: SymbolShape, slot: AccessorySlot): number {
  return ACCESSORY_CATALOG.shapes[shape].depth?.[slot] ?? ACCESSORY_CATALOG.slotDepth[slot] * ACCESSORY_CATALOG.frontDepthM;
}
interface Catalog {
  schema: number;
  slots: AccessorySlot[];
  /** Fallback depth as a fraction of frontDepthM, for a shape without measured depths. */
  slotDepth: Record<AccessorySlot, number>;
  /** Flat-only x shift that recentres clothing under the tilted flat face. */
  slotFlatShift: Partial<Record<AccessorySlot, number>>;
  frontDepthM: number;
  shapes: Record<SymbolShape, AccessoryShape>;
  items: AccessoryItem[];
}

export const ACCESSORY_CATALOG = catalogJson as unknown as Catalog;
export const ACCESSORY_ITEMS: readonly AccessoryItem[] = ACCESSORY_CATALOG.items;
const BY_ID = new Map(ACCESSORY_ITEMS.map(item => [item.id, item]));
export const ACCESSORY_IDS_BY_SLOT: Record<AccessorySlot, string[]> = Object.fromEntries(
  ACCESSORY_SLOTS.map(slot => [slot, ACCESSORY_ITEMS.filter(item => item.slot === slot).map(item => item.id)]),
) as Record<AccessorySlot, string[]>;
/** Slots that currently offer at least one item. */
export const OFFERED_SLOTS: AccessorySlot[] = ACCESSORY_SLOTS.filter(slot => ACCESSORY_IDS_BY_SLOT[slot].length > 0);

/** Slots whose parts follow the tilted face of the flat symbol. */
export const FACE_SLOTS: ReadonlySet<AccessorySlot> = new Set(["face", "mouth"]);

export function accessoryItem(id: string | undefined): AccessoryItem | undefined {
  return id ? BY_ID.get(id) : undefined;
}

/** Known items in draw order. Unknown ids (from a newer build) are skipped. */
export function wornAccessories(choice: AccessoryChoice | undefined): AccessoryItem[] {
  if (!choice) return [];
  const worn: AccessoryItem[] = [];
  for (const slot of ACCESSORY_SLOTS) {
    const item = accessoryItem(choice[slot]);
    if (item && item.slot === slot) worn.push(item);
  }
  return worn;
}

/** A light or dark tint of the agent's own colour, for token fills. */
export function shadeHex(hex: string, amount: number): string {
  const value = parseInt(hex.slice(1), 16);
  const channel = (shift: number) => {
    const c = (value >> shift) & 255;
    const next = amount < 0 ? c * (1 + amount) : c + (255 - c) * amount;
    return Math.round(Math.max(0, Math.min(255, next)));
  };
  return `#${[16, 8, 0].map(s => channel(s).toString(16).padStart(2, "0")).join("")}`;
}

export function resolveFill(fill: string, bodyColor: string): string {
  if (fill === "body") return bodyColor;
  if (fill === "bodyDark") return shadeHex(bodyColor, -0.3);
  if (fill === "bodyLight") return shadeHex(bodyColor, 0.45);
  if (fill === "ink") return "#101014";
  return fill;
}

/** Closed or open Catmull-Rom spline through the points, as SVG cubic segments. */
export function smoothPath(points: readonly Vec2[], closed: boolean): string {
  const n = points.length;
  if (n < 3) return `M${points.map(p => p.join(" ")).join(" L")}${closed ? "Z" : ""}`;
  const at = (i: number): Vec2 => closed ? points[(i + n) % n]! : points[Math.max(0, Math.min(n - 1, i))]!;
  const f = (v: number) => +v.toFixed(2);
  let d = `M${f(points[0]![0])} ${f(points[0]![1])}`;
  const segments = closed ? n : n - 1;
  for (let i = 0; i < segments; i++) {
    const p0 = at(i - 1), p1 = at(i), p2 = at(i + 1), p3 = at(i + 2);
    d += ` C${f(p1[0] + (p2[0] - p0[0]) / 6)} ${f(p1[1] + (p2[1] - p0[1]) / 6)} ${f(p2[0] - (p3[0] - p1[0]) / 6)} ${f(p2[1] - (p3[1] - p1[1]) / 6)} ${f(p2[0])} ${f(p2[1])}`;
  }
  return closed ? `${d}Z` : d;
}

/** Depth used to paint parts back to front inside one item. */
export function partDepth(part: AccessoryPart): number {
  if (part.t === "poly") return part.z;
  if (part.t === "tube") return part.pts.reduce((sum, p) => sum + p[2], 0) / part.pts.length;
  if (part.t === "region" || part.t === "rim") return -Infinity;
  return part.c[2];
}

/** The symbol's viewBox, grown just enough to show every worn accessory whole. */
export function symbolViewBox(shape: SymbolShape, worn: readonly AccessoryItem[]): string {
  if (!worn.length) return "0 0 40 44";
  const anchors = ACCESSORY_CATALOG.shapes[shape].anchors;
  let x0 = 0, y0 = 0, x1 = 40, y1 = 44;
  for (const item of worn) {
    const [ax, ay, anchorK] = anchors[item.slot];
    const k = anchorK * (item.scale ?? 1);
    const [bx0, by0, bx1, by1] = item.bounds;
    if (bx0 === bx1 && by0 === by1) continue;
    // The face tilt moves points by at most ~2 units; pad instead of rotating.
    const pad = FACE_SLOTS.has(item.slot) ? 2.5 : 0.8;
    x0 = Math.min(x0, ax + bx0 * k - pad); y0 = Math.min(y0, ay + by0 * k - pad);
    x1 = Math.max(x1, ax + bx1 * k + pad); y1 = Math.max(y1, ay + by1 * k + pad);
  }
  // Keep the original 40:44 aspect so a worn item never squashes the body.
  let w = x1 - x0, h = y1 - y0;
  if (w / h < 40 / 44) { const grow = h * 40 / 44 - w; x0 -= grow / 2; w += grow; }
  else { const grow = w * 44 / 40 - h; y0 -= grow / 2; h += grow; }
  const r = (v: number) => +v.toFixed(2);
  return `${r(x0)} ${r(y0)} ${r(w)} ${r(h)}`;
}

/** A random look: each slot independently worn about every third time. */
export function randomAccessories(random: () => number = Math.random): AccessoryChoice {
  const choice: AccessoryChoice = {};
  for (const slot of ACCESSORY_SLOTS) {
    const ids = ACCESSORY_IDS_BY_SLOT[slot];
    if (ids.length && random() < (slot === "head" ? 0.7 : 0.3)) choice[slot] = ids[Math.floor(random() * ids.length)];
  }
  return choice;
}