import { spriteImageName } from "../definitions/tileSprites.js"; import { EQUIPMENT_SLOT, EQUIPMENT_DRAW_ORDER, itemDefinition, itemEquipSprite } from "../definitions/items.js"; import { normalizeRgb, resolveEquipTint, resolveEquipDualTint } from "../render/tint.js"; /** Hair style ids per sex (PNG stems under `characters/player-body-*`). */ export const HAIR_STYLES_BY_SEX = Object.freeze({ male: Object.freeze(["hair-1", "hair-2", "hair-3", "hair-4"]), female: Object.freeze(["hair-1", "hair-2", "hair-3"]) }); export const DEFAULT_HAIR_STYLE = "hair-1"; export const DEFAULT_HAIR_HUE = 30; export const DEFAULT_HAIR_SATURATION = 0.55; export const DEFAULT_HAIR_DARKNESS = 0.45; /** * Curated NPC hair tints (linear RGB 0–1). Seeded picks only — no random rainbow hues. * Order is stable so name-hash indices stay consistent across builds. */ export const NPC_HAIR_COLORS = Object.freeze([ Object.freeze({ r: 0.07, g: 0.05, b: 0.04 }), // near-black Object.freeze({ r: 0.16, g: 0.10, b: 0.07 }), // dark brown Object.freeze({ r: 0.28, g: 0.17, b: 0.10 }), // medium brown Object.freeze({ r: 0.40, g: 0.26, b: 0.14 }), // light brown Object.freeze({ r: 0.48, g: 0.24, b: 0.12 }), // chestnut Object.freeze({ r: 0.55, g: 0.22, b: 0.10 }), // auburn Object.freeze({ r: 0.62, g: 0.28, b: 0.12 }), // copper Object.freeze({ r: 0.72, g: 0.32, b: 0.14 }), // ginger Object.freeze({ r: 0.58, g: 0.42, b: 0.22 }), // dirty blonde Object.freeze({ r: 0.72, g: 0.58, b: 0.32 }), // blonde Object.freeze({ r: 0.82, g: 0.72, b: 0.48 }), // light blonde Object.freeze({ r: 0.55, g: 0.52, b: 0.48 }), // ash / salt-and-pepper Object.freeze({ r: 0.70, g: 0.70, b: 0.68 }), // silver-gray Object.freeze({ r: 0.88, g: 0.86, b: 0.82 }) // white / pale gray ]); /** FNV-1a 32-bit — stable seed from display name. */ export function hashString (value) { const text = String(value ?? ""); let hash = 0x811c9dc5; for (let i = 0; i < text.length; i++) { hash ^= text.charCodeAt(i); hash = Math.imul(hash, 0x01000193); } return hash >>> 0; } /** * Deterministic hair for NPCs: style + palette colour from name seed. * @returns {{ style: string, color: { r: number, g: number, b: number }, sex: string }} */ export function hairFromNameSeed (displayName, sex) { const resolvedSex = sex === "female" ? "female" : "male"; const seed = hashString(String(displayName ?? "").trim().toLowerCase() || "npc"); const styles = listHairStyles(resolvedSex); const style = styles[seed % styles.length]; const color = normalizeRgb(NPC_HAIR_COLORS[(seed >>> 8) % NPC_HAIR_COLORS.length]); return { style, color, sex: resolvedSex }; } /** Random NPC hair palette colour (fairies, etc.). */ export function randomNpcHairColor (rng = Math.random) { const index = Math.floor(rng() * NPC_HAIR_COLORS.length) % NPC_HAIR_COLORS.length; return normalizeRgb(NPC_HAIR_COLORS[index]); } /** * Bright or pastel clothing colour for dual-channel fairy kits. * @returns {{ r: number, g: number, b: number }} */ export function randomBrightOrPastelColor (rng = Math.random) { const hue = rng() * 360; const pastel = rng() < 0.5; if (pastel) { return hslToRgb(hue, 0.42 + rng() * 0.38, 0.70 + rng() * 0.2); } return hslToRgb(hue, 0.72 + rng() * 0.28, 0.42 + rng() * 0.28); } export function bodySpriteName (sex) { const key = sex === "female" ? "player-body-female" : "player-body-male"; return spriteImageName("character", key, key); } export function listHairStyles (sex) { return HAIR_STYLES_BY_SEX[sex === "female" ? "female" : "male"] ?? HAIR_STYLES_BY_SEX.male; } export function isValidHairStyle (sex, styleId) { if (styleId == null || styleId === "" || styleId === "none") { return true; } return listHairStyles(sex).includes(String(styleId)); } export function normalizeHairStyle (sex, styleId) { if (styleId == null || styleId === "" || styleId === "none") { return null; } const id = String(styleId); return listHairStyles(sex).includes(id) ? id : DEFAULT_HAIR_STYLE; } export function clamp01 (value) { const n = Number(value); if (!Number.isFinite(n)) { return 0; } return Math.min(1, Math.max(0, n)); } export function normalizeHairSliders ({ hue, saturation, darkness } = {}) { let h = Number(hue); if (!Number.isFinite(h)) { h = DEFAULT_HAIR_HUE; } h = ((h % 360) + 360) % 360; return { hue: h, saturation: clamp01(saturation ?? DEFAULT_HAIR_SATURATION), darkness: clamp01(darkness ?? DEFAULT_HAIR_DARKNESS) }; } /** Convert hair sliders to RGB for magenta tint (darkness 1 → near-black). */ export function hairColorFromSliders (sliders) { const { hue, saturation, darkness } = normalizeHairSliders(sliders); const lightness = clamp01(1 - darkness * 0.85); return normalizeRgb(hslToRgb(hue, saturation, lightness)); } export function hairSpriteName (sex, styleId) { const style = normalizeHairStyle(sex, styleId); if (!style) { return null; } const bodyKey = sex === "female" ? "player-body-female" : "player-body-male"; return spriteImageName("character", bodyKey, style); } export function hairFromCharacterRow (row) { if (!row) { return null; } const sex = row.sex === "female" ? "female" : "male"; const style = normalizeHairStyle(sex, row.hair_style); if (!style) { return null; } const sliders = normalizeHairSliders({ hue: row.hair_hue, saturation: row.hair_saturation, darkness: row.hair_darkness }); return { style, ...sliders, color: hairColorFromSliders(sliders) }; } export function appearanceHairFields (hair) { if (!hair?.style) { return { hairStyle: null, hairHue: DEFAULT_HAIR_HUE, hairSaturation: DEFAULT_HAIR_SATURATION, hairDarkness: DEFAULT_HAIR_DARKNESS, hairColor: null }; } const sex = hair.sex === "female" ? "female" : "male"; const style = normalizeHairStyle(sex, hair.style); const sliders = normalizeHairSliders(hair); return { hairStyle: style, hairHue: sliders.hue, hairSaturation: sliders.saturation, hairDarkness: sliders.darkness, hairColor: hair.color ?? hairColorFromSliders(sliders) }; } /** Headgear hides hair unless `equipment.keepHair` is set on the item def. */ export function headgearKeepsHair (wornHead) { if (!wornHead) { return true; } const def = itemDefinition(wornHead.itemId); return !!def?.equipment?.keepHair; } export function shouldShowHair (appearance, equipmentSlots) { const style = appearance?.hairStyle ?? null; if (!style) { return false; } const head = equipmentSlots?.[EQUIPMENT_SLOT.head] ?? null; return headgearKeepsHair(head); } export function hairLayerFromAppearance (appearance) { const style = appearance?.hairStyle ?? null; if (!style) { return null; } const sex = appearance?.sex === "female" ? "female" : "male"; const sprite = hairSpriteName(sex, style); if (!sprite) { return null; } const color = appearance?.hairColor ? normalizeRgb(appearance.hairColor) : hairColorFromSliders({ hue: appearance?.hairHue, saturation: appearance?.hairSaturation, darkness: appearance?.hairDarkness }); return { sprite, tint: color, hair: true }; } /** * Shared draw order: body → boots → chest → weapon → legs → hair (if visible) → head → offhand. */ export function composeAppearanceLayers (appearance, equipmentSlots) { const sex = appearance?.sex === "female" ? "female" : "male"; const layers = [{ sprite: appearance?.sprite ?? bodySpriteName(sex), ...(appearance?.tint ? { tint: appearance.tint } : {}), ...(appearance?.dualTint ? { dualTint: appearance.dualTint } : {}), ...(appearance?.bloomBody ? { bloomBody: true } : {}) }]; const slots = equipmentSlots ?? {}; for (const slot of EQUIPMENT_DRAW_ORDER) { if (slot === EQUIPMENT_SLOT.head && shouldShowHair(appearance, slots)) { const hair = hairLayerFromAppearance(appearance); if (hair) { layers.push(hair); } } const worn = slots[slot]; if (!worn) { continue; } const overlay = itemEquipSprite(worn.itemId); if (!overlay) { continue; } const def = itemDefinition(worn.itemId); const dualTint = resolveEquipDualTint(worn, def); const tint = dualTint ? null : resolveEquipTint(worn.itemId, worn.instance); layers.push({ sprite: overlay, itemId: worn.itemId, ...(dualTint ? { dualTint } : {}), ...(tint ? { tint } : {}) }); } return layers; } /** * Preview layers for creation/barber UI. * @param {{ hideHeadgear?: boolean }} [options] */ export function layersForHairPreview (sex, bag, hair, { hideHeadgear = true } = {}) { const slots = { ...(bag?.equipment?.slots ?? {}) }; if (hideHeadgear) { slots[EQUIPMENT_SLOT.head] = null; } const style = normalizeHairStyle(sex, hair?.style); const appearance = { sex, sprite: bodySpriteName(sex), ...appearanceHairFields(style ? { ...hair, style, sex } : null) }; if (style) { appearance.hairStyle = style; } return composeAppearanceLayers(appearance, slots); } function hslToRgb (h, s, l) { const hue = ((h % 360) + 360) % 360 / 360; const sat = clamp01(s); const light = clamp01(l); if (sat === 0) { return { r: light, g: light, b: light }; } const q = light < 0.5 ? light * (1 + sat) : light + sat - light * sat; const p = 2 * light - q; return { r: hueToChannel(p, q, hue + 1 / 3), g: hueToChannel(p, q, hue), b: hueToChannel(p, q, hue - 1 / 3) }; } function hueToChannel (p, q, t) { let tt = t; if (tt < 0) { tt += 1; } if (tt > 1) { tt -= 1; } if (tt < 1 / 6) { return p + (q - p) * 6 * tt; } if (tt < 1 / 2) { return q; } if (tt < 2 / 3) { return p + (q - p) * (2 / 3 - tt) * 6; } return p; }