| 1 | import { spriteImageName } from "../definitions/tileSprites.js"; |
| 2 | import { |
| 3 | EQUIPMENT_SLOT, |
| 4 | EQUIPMENT_DRAW_ORDER, |
| 5 | itemDefinition, |
| 6 | itemEquipSprite |
| 7 | } from "../definitions/items.js"; |
| 8 | import { normalizeRgb, resolveEquipTint, resolveEquipDualTint } from "../render/tint.js"; |
| 9 | |
| 10 | /** Hair style ids per sex (PNG stems under `characters/player-body-*`). */ |
| 11 | export const HAIR_STYLES_BY_SEX = Object.freeze({ |
| 12 | male: Object.freeze(["hair-1", "hair-2", "hair-3", "hair-4"]), |
| 13 | female: Object.freeze(["hair-1", "hair-2", "hair-3"]) |
| 14 | }); |
| 15 | |
| 16 | export const DEFAULT_HAIR_STYLE = "hair-1"; |
| 17 | export const DEFAULT_HAIR_HUE = 30; |
| 18 | export const DEFAULT_HAIR_SATURATION = 0.55; |
| 19 | export const DEFAULT_HAIR_DARKNESS = 0.45; |
| 20 | |
| 21 | /** |
| 22 | * Curated NPC hair tints (linear RGB 0–1). Seeded picks only — no random rainbow hues. |
| 23 | * Order is stable so name-hash indices stay consistent across builds. |
| 24 | */ |
| 25 | export const NPC_HAIR_COLORS = Object.freeze([ |
| 26 | Object.freeze({ r: 0.07, g: 0.05, b: 0.04 }), // near-black |
| 27 | Object.freeze({ r: 0.16, g: 0.10, b: 0.07 }), // dark brown |
| 28 | Object.freeze({ r: 0.28, g: 0.17, b: 0.10 }), // medium brown |
| 29 | Object.freeze({ r: 0.40, g: 0.26, b: 0.14 }), // light brown |
| 30 | Object.freeze({ r: 0.48, g: 0.24, b: 0.12 }), // chestnut |
| 31 | Object.freeze({ r: 0.55, g: 0.22, b: 0.10 }), // auburn |
| 32 | Object.freeze({ r: 0.62, g: 0.28, b: 0.12 }), // copper |
| 33 | Object.freeze({ r: 0.72, g: 0.32, b: 0.14 }), // ginger |
| 34 | Object.freeze({ r: 0.58, g: 0.42, b: 0.22 }), // dirty blonde |
| 35 | Object.freeze({ r: 0.72, g: 0.58, b: 0.32 }), // blonde |
| 36 | Object.freeze({ r: 0.82, g: 0.72, b: 0.48 }), // light blonde |
| 37 | Object.freeze({ r: 0.55, g: 0.52, b: 0.48 }), // ash / salt-and-pepper |
| 38 | Object.freeze({ r: 0.70, g: 0.70, b: 0.68 }), // silver-gray |
| 39 | Object.freeze({ r: 0.88, g: 0.86, b: 0.82 }) // white / pale gray |
| 40 | ]); |
| 41 | |
| 42 | /** FNV-1a 32-bit — stable seed from display name. */ |
| 43 | export function hashString (value) |
| 44 | { |
| 45 | const text = String(value ?? ""); |
| 46 | let hash = 0x811c9dc5; |
| 47 | for (let i = 0; i < text.length; i++) |
| 48 | { |
| 49 | hash ^= text.charCodeAt(i); |
| 50 | hash = Math.imul(hash, 0x01000193); |
| 51 | } |
| 52 | return hash >>> 0; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Deterministic hair for NPCs: style + palette colour from name seed. |
| 57 | * @returns {{ style: string, color: { r: number, g: number, b: number }, sex: string }} |
| 58 | */ |
| 59 | export function hairFromNameSeed (displayName, sex) |
| 60 | { |
| 61 | const resolvedSex = sex === "female" ? "female" : "male"; |
| 62 | const seed = hashString(String(displayName ?? "").trim().toLowerCase() || "npc"); |
| 63 | const styles = listHairStyles(resolvedSex); |
| 64 | const style = styles[seed % styles.length]; |
| 65 | const color = normalizeRgb(NPC_HAIR_COLORS[(seed >>> 8) % NPC_HAIR_COLORS.length]); |
| 66 | return { style, color, sex: resolvedSex }; |
| 67 | } |
| 68 | |
| 69 | /** Random NPC hair palette colour (fairies, etc.). */ |
| 70 | export function randomNpcHairColor (rng = Math.random) |
| 71 | { |
| 72 | const index = Math.floor(rng() * NPC_HAIR_COLORS.length) % NPC_HAIR_COLORS.length; |
| 73 | return normalizeRgb(NPC_HAIR_COLORS[index]); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Bright or pastel clothing colour for dual-channel fairy kits. |
| 78 | * @returns {{ r: number, g: number, b: number }} |
| 79 | */ |
| 80 | export function randomBrightOrPastelColor (rng = Math.random) |
| 81 | { |
| 82 | const hue = rng() * 360; |
| 83 | const pastel = rng() < 0.5; |
| 84 | if (pastel) |
| 85 | { return hslToRgb(hue, 0.42 + rng() * 0.38, 0.70 + rng() * 0.2); } |
| 86 | return hslToRgb(hue, 0.72 + rng() * 0.28, 0.42 + rng() * 0.28); |
| 87 | } |
| 88 | |
| 89 | export function bodySpriteName (sex) |
| 90 | { |
| 91 | const key = sex === "female" ? "player-body-female" : "player-body-male"; |
| 92 | return spriteImageName("character", key, key); |
| 93 | } |
| 94 | |
| 95 | export function listHairStyles (sex) |
| 96 | { |
| 97 | return HAIR_STYLES_BY_SEX[sex === "female" ? "female" : "male"] ?? HAIR_STYLES_BY_SEX.male; |
| 98 | } |
| 99 | |
| 100 | export function isValidHairStyle (sex, styleId) |
| 101 | { |
| 102 | if (styleId == null || styleId === "" || styleId === "none") |
| 103 | { return true; } |
| 104 | return listHairStyles(sex).includes(String(styleId)); |
| 105 | } |
| 106 | |
| 107 | export function normalizeHairStyle (sex, styleId) |
| 108 | { |
| 109 | if (styleId == null || styleId === "" || styleId === "none") |
| 110 | { return null; } |
| 111 | const id = String(styleId); |
| 112 | return listHairStyles(sex).includes(id) ? id : DEFAULT_HAIR_STYLE; |
| 113 | } |
| 114 | |
| 115 | export function clamp01 (value) |
| 116 | { |
| 117 | const n = Number(value); |
| 118 | if (!Number.isFinite(n)) |
| 119 | { return 0; } |
| 120 | return Math.min(1, Math.max(0, n)); |
| 121 | } |
| 122 | |
| 123 | export function normalizeHairSliders ({ hue, saturation, darkness } = {}) |
| 124 | { |
| 125 | let h = Number(hue); |
| 126 | if (!Number.isFinite(h)) |
| 127 | { h = DEFAULT_HAIR_HUE; } |
| 128 | h = ((h % 360) + 360) % 360; |
| 129 | return { |
| 130 | hue: h, |
| 131 | saturation: clamp01(saturation ?? DEFAULT_HAIR_SATURATION), |
| 132 | darkness: clamp01(darkness ?? DEFAULT_HAIR_DARKNESS) |
| 133 | }; |
| 134 | } |
| 135 | |
| 136 | /** Convert hair sliders to RGB for magenta tint (darkness 1 → near-black). */ |
| 137 | export function hairColorFromSliders (sliders) |
| 138 | { |
| 139 | const { hue, saturation, darkness } = normalizeHairSliders(sliders); |
| 140 | const lightness = clamp01(1 - darkness * 0.85); |
| 141 | return normalizeRgb(hslToRgb(hue, saturation, lightness)); |
| 142 | } |
| 143 | |
| 144 | export function hairSpriteName (sex, styleId) |
| 145 | { |
| 146 | const style = normalizeHairStyle(sex, styleId); |
| 147 | if (!style) |
| 148 | { return null; } |
| 149 | const bodyKey = sex === "female" ? "player-body-female" : "player-body-male"; |
| 150 | return spriteImageName("character", bodyKey, style); |
| 151 | } |
| 152 | |
| 153 | export function hairFromCharacterRow (row) |
| 154 | { |
| 155 | if (!row) |
| 156 | { return null; } |
| 157 | const sex = row.sex === "female" ? "female" : "male"; |
| 158 | const style = normalizeHairStyle(sex, row.hair_style); |
| 159 | if (!style) |
| 160 | { return null; } |
| 161 | const sliders = normalizeHairSliders({ |
| 162 | hue: row.hair_hue, |
| 163 | saturation: row.hair_saturation, |
| 164 | darkness: row.hair_darkness |
| 165 | }); |
| 166 | return { |
| 167 | style, |
| 168 | ...sliders, |
| 169 | color: hairColorFromSliders(sliders) |
| 170 | }; |
| 171 | } |
| 172 | |
| 173 | export function appearanceHairFields (hair) |
| 174 | { |
| 175 | if (!hair?.style) |
| 176 | { |
| 177 | return { |
| 178 | hairStyle: null, |
| 179 | hairHue: DEFAULT_HAIR_HUE, |
| 180 | hairSaturation: DEFAULT_HAIR_SATURATION, |
| 181 | hairDarkness: DEFAULT_HAIR_DARKNESS, |
| 182 | hairColor: null |
| 183 | }; |
| 184 | } |
| 185 | const sex = hair.sex === "female" ? "female" : "male"; |
| 186 | const style = normalizeHairStyle(sex, hair.style); |
| 187 | const sliders = normalizeHairSliders(hair); |
| 188 | return { |
| 189 | hairStyle: style, |
| 190 | hairHue: sliders.hue, |
| 191 | hairSaturation: sliders.saturation, |
| 192 | hairDarkness: sliders.darkness, |
| 193 | hairColor: hair.color ?? hairColorFromSliders(sliders) |
| 194 | }; |
| 195 | } |
| 196 | |
| 197 | /** Headgear hides hair unless `equipment.keepHair` is set on the item def. */ |
| 198 | export function headgearKeepsHair (wornHead) |
| 199 | { |
| 200 | if (!wornHead) |
| 201 | { return true; } |
| 202 | const def = itemDefinition(wornHead.itemId); |
| 203 | return !!def?.equipment?.keepHair; |
| 204 | } |
| 205 | |
| 206 | export function shouldShowHair (appearance, equipmentSlots) |
| 207 | { |
| 208 | const style = appearance?.hairStyle ?? null; |
| 209 | if (!style) |
| 210 | { return false; } |
| 211 | const head = equipmentSlots?.[EQUIPMENT_SLOT.head] ?? null; |
| 212 | return headgearKeepsHair(head); |
| 213 | } |
| 214 | |
| 215 | export function hairLayerFromAppearance (appearance) |
| 216 | { |
| 217 | const style = appearance?.hairStyle ?? null; |
| 218 | if (!style) |
| 219 | { return null; } |
| 220 | const sex = appearance?.sex === "female" ? "female" : "male"; |
| 221 | const sprite = hairSpriteName(sex, style); |
| 222 | if (!sprite) |
| 223 | { return null; } |
| 224 | const color = appearance?.hairColor |
| 225 | ? normalizeRgb(appearance.hairColor) |
| 226 | : hairColorFromSliders({ |
| 227 | hue: appearance?.hairHue, |
| 228 | saturation: appearance?.hairSaturation, |
| 229 | darkness: appearance?.hairDarkness |
| 230 | }); |
| 231 | return { sprite, tint: color, hair: true }; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Shared draw order: body → boots → chest → weapon → legs → hair (if visible) → head → offhand. |
| 236 | */ |
| 237 | export function composeAppearanceLayers (appearance, equipmentSlots) |
| 238 | { |
| 239 | const sex = appearance?.sex === "female" ? "female" : "male"; |
| 240 | const layers = [{ |
| 241 | sprite: appearance?.sprite ?? bodySpriteName(sex), |
| 242 | ...(appearance?.tint ? { tint: appearance.tint } : {}), |
| 243 | ...(appearance?.dualTint ? { dualTint: appearance.dualTint } : {}), |
| 244 | ...(appearance?.bloomBody ? { bloomBody: true } : {}) |
| 245 | }]; |
| 246 | const slots = equipmentSlots ?? {}; |
| 247 | for (const slot of EQUIPMENT_DRAW_ORDER) |
| 248 | { |
| 249 | if (slot === EQUIPMENT_SLOT.head && shouldShowHair(appearance, slots)) |
| 250 | { |
| 251 | const hair = hairLayerFromAppearance(appearance); |
| 252 | if (hair) |
| 253 | { layers.push(hair); } |
| 254 | } |
| 255 | const worn = slots[slot]; |
| 256 | if (!worn) |
| 257 | { continue; } |
| 258 | const overlay = itemEquipSprite(worn.itemId); |
| 259 | if (!overlay) |
| 260 | { continue; } |
| 261 | const def = itemDefinition(worn.itemId); |
| 262 | const dualTint = resolveEquipDualTint(worn, def); |
| 263 | const tint = dualTint ? null : resolveEquipTint(worn.itemId, worn.instance); |
| 264 | layers.push({ |
| 265 | sprite: overlay, |
| 266 | itemId: worn.itemId, |
| 267 | ...(dualTint ? { dualTint } : {}), |
| 268 | ...(tint ? { tint } : {}) |
| 269 | }); |
| 270 | } |
| 271 | return layers; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Preview layers for creation/barber UI. |
| 276 | * @param {{ hideHeadgear?: boolean }} [options] |
| 277 | */ |
| 278 | export function layersForHairPreview (sex, bag, hair, { hideHeadgear = true } = {}) |
| 279 | { |
| 280 | const slots = { ...(bag?.equipment?.slots ?? {}) }; |
| 281 | if (hideHeadgear) |
| 282 | { slots[EQUIPMENT_SLOT.head] = null; } |
| 283 | const style = normalizeHairStyle(sex, hair?.style); |
| 284 | const appearance = { |
| 285 | sex, |
| 286 | sprite: bodySpriteName(sex), |
| 287 | ...appearanceHairFields(style ? { ...hair, style, sex } : null) |
| 288 | }; |
| 289 | if (style) |
| 290 | { appearance.hairStyle = style; } |
| 291 | return composeAppearanceLayers(appearance, slots); |
| 292 | } |
| 293 | |
| 294 | function hslToRgb (h, s, l) |
| 295 | { |
| 296 | const hue = ((h % 360) + 360) % 360 / 360; |
| 297 | const sat = clamp01(s); |
| 298 | const light = clamp01(l); |
| 299 | if (sat === 0) |
| 300 | { return { r: light, g: light, b: light }; } |
| 301 | const q = light < 0.5 ? light * (1 + sat) : light + sat - light * sat; |
| 302 | const p = 2 * light - q; |
| 303 | return { |
| 304 | r: hueToChannel(p, q, hue + 1 / 3), |
| 305 | g: hueToChannel(p, q, hue), |
| 306 | b: hueToChannel(p, q, hue - 1 / 3) |
| 307 | }; |
| 308 | } |
| 309 | |
| 310 | function hueToChannel (p, q, t) |
| 311 | { |
| 312 | let tt = t; |
| 313 | if (tt < 0) |
| 314 | { tt += 1; } |
| 315 | if (tt > 1) |
| 316 | { tt -= 1; } |
| 317 | if (tt < 1 / 6) |
| 318 | { return p + (q - p) * 6 * tt; } |
| 319 | if (tt < 1 / 2) |
| 320 | { return q; } |
| 321 | if (tt < 2 / 3) |
| 322 | { return p + (q - p) * (2 / 3 - tt) * 6; } |
| 323 | return p; |
| 324 | } |