| 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 } 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 | export function bodySpriteName (sex) |
| 70 | { |
| 71 | const key = sex === "female" ? "player-body-female" : "player-body-male"; |
| 72 | return spriteImageName("character", key, key); |
| 73 | } |
| 74 | |
| 75 | export function listHairStyles (sex) |
| 76 | { |
| 77 | return HAIR_STYLES_BY_SEX[sex === "female" ? "female" : "male"] ?? HAIR_STYLES_BY_SEX.male; |
| 78 | } |
| 79 | |
| 80 | export function isValidHairStyle (sex, styleId) |
| 81 | { |
| 82 | if (styleId == null || styleId === "" || styleId === "none") |
| 83 | { return true; } |
| 84 | return listHairStyles(sex).includes(String(styleId)); |
| 85 | } |
| 86 | |
| 87 | export function normalizeHairStyle (sex, styleId) |
| 88 | { |
| 89 | if (styleId == null || styleId === "" || styleId === "none") |
| 90 | { return null; } |
| 91 | const id = String(styleId); |
| 92 | return listHairStyles(sex).includes(id) ? id : DEFAULT_HAIR_STYLE; |
| 93 | } |
| 94 | |
| 95 | export function clamp01 (value) |
| 96 | { |
| 97 | const n = Number(value); |
| 98 | if (!Number.isFinite(n)) |
| 99 | { return 0; } |
| 100 | return Math.min(1, Math.max(0, n)); |
| 101 | } |
| 102 | |
| 103 | export function normalizeHairSliders ({ hue, saturation, darkness } = {}) |
| 104 | { |
| 105 | let h = Number(hue); |
| 106 | if (!Number.isFinite(h)) |
| 107 | { h = DEFAULT_HAIR_HUE; } |
| 108 | h = ((h % 360) + 360) % 360; |
| 109 | return { |
| 110 | hue: h, |
| 111 | saturation: clamp01(saturation ?? DEFAULT_HAIR_SATURATION), |
| 112 | darkness: clamp01(darkness ?? DEFAULT_HAIR_DARKNESS) |
| 113 | }; |
| 114 | } |
| 115 | |
| 116 | /** Convert hair sliders to RGB for magenta tint (darkness 1 → near-black). */ |
| 117 | export function hairColorFromSliders (sliders) |
| 118 | { |
| 119 | const { hue, saturation, darkness } = normalizeHairSliders(sliders); |
| 120 | const lightness = clamp01(1 - darkness * 0.85); |
| 121 | return normalizeRgb(hslToRgb(hue, saturation, lightness)); |
| 122 | } |
| 123 | |
| 124 | export function hairSpriteName (sex, styleId) |
| 125 | { |
| 126 | const style = normalizeHairStyle(sex, styleId); |
| 127 | if (!style) |
| 128 | { return null; } |
| 129 | const bodyKey = sex === "female" ? "player-body-female" : "player-body-male"; |
| 130 | return spriteImageName("character", bodyKey, style); |
| 131 | } |
| 132 | |
| 133 | export function hairFromCharacterRow (row) |
| 134 | { |
| 135 | if (!row) |
| 136 | { return null; } |
| 137 | const sex = row.sex === "female" ? "female" : "male"; |
| 138 | const style = normalizeHairStyle(sex, row.hair_style); |
| 139 | if (!style) |
| 140 | { return null; } |
| 141 | const sliders = normalizeHairSliders({ |
| 142 | hue: row.hair_hue, |
| 143 | saturation: row.hair_saturation, |
| 144 | darkness: row.hair_darkness |
| 145 | }); |
| 146 | return { |
| 147 | style, |
| 148 | ...sliders, |
| 149 | color: hairColorFromSliders(sliders) |
| 150 | }; |
| 151 | } |
| 152 | |
| 153 | export function appearanceHairFields (hair) |
| 154 | { |
| 155 | if (!hair?.style) |
| 156 | { |
| 157 | return { |
| 158 | hairStyle: null, |
| 159 | hairHue: DEFAULT_HAIR_HUE, |
| 160 | hairSaturation: DEFAULT_HAIR_SATURATION, |
| 161 | hairDarkness: DEFAULT_HAIR_DARKNESS, |
| 162 | hairColor: null |
| 163 | }; |
| 164 | } |
| 165 | const sex = hair.sex === "female" ? "female" : "male"; |
| 166 | const style = normalizeHairStyle(sex, hair.style); |
| 167 | const sliders = normalizeHairSliders(hair); |
| 168 | return { |
| 169 | hairStyle: style, |
| 170 | hairHue: sliders.hue, |
| 171 | hairSaturation: sliders.saturation, |
| 172 | hairDarkness: sliders.darkness, |
| 173 | hairColor: hair.color ?? hairColorFromSliders(sliders) |
| 174 | }; |
| 175 | } |
| 176 | |
| 177 | /** Headgear hides hair unless `equipment.keepHair` is set on the item def. */ |
| 178 | export function headgearKeepsHair (wornHead) |
| 179 | { |
| 180 | if (!wornHead) |
| 181 | { return true; } |
| 182 | const def = itemDefinition(wornHead.itemId); |
| 183 | return !!def?.equipment?.keepHair; |
| 184 | } |
| 185 | |
| 186 | export function shouldShowHair (appearance, equipmentSlots) |
| 187 | { |
| 188 | const style = appearance?.hairStyle ?? null; |
| 189 | if (!style) |
| 190 | { return false; } |
| 191 | const head = equipmentSlots?.[EQUIPMENT_SLOT.head] ?? null; |
| 192 | return headgearKeepsHair(head); |
| 193 | } |
| 194 | |
| 195 | export function hairLayerFromAppearance (appearance) |
| 196 | { |
| 197 | const style = appearance?.hairStyle ?? null; |
| 198 | if (!style) |
| 199 | { return null; } |
| 200 | const sex = appearance?.sex === "female" ? "female" : "male"; |
| 201 | const sprite = hairSpriteName(sex, style); |
| 202 | if (!sprite) |
| 203 | { return null; } |
| 204 | const color = appearance?.hairColor |
| 205 | ? normalizeRgb(appearance.hairColor) |
| 206 | : hairColorFromSliders({ |
| 207 | hue: appearance?.hairHue, |
| 208 | saturation: appearance?.hairSaturation, |
| 209 | darkness: appearance?.hairDarkness |
| 210 | }); |
| 211 | return { sprite, tint: color, hair: true }; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Shared draw order: body → boots → chest → weapon → legs → hair (if visible) → head → offhand. |
| 216 | */ |
| 217 | export function composeAppearanceLayers (appearance, equipmentSlots) |
| 218 | { |
| 219 | const sex = appearance?.sex === "female" ? "female" : "male"; |
| 220 | const layers = [{ |
| 221 | sprite: appearance?.sprite ?? bodySpriteName(sex), |
| 222 | ...(appearance?.tint ? { tint: appearance.tint } : {}), |
| 223 | ...(appearance?.bloomBody ? { bloomBody: true } : {}) |
| 224 | }]; |
| 225 | const slots = equipmentSlots ?? {}; |
| 226 | for (const slot of EQUIPMENT_DRAW_ORDER) |
| 227 | { |
| 228 | if (slot === EQUIPMENT_SLOT.head && shouldShowHair(appearance, slots)) |
| 229 | { |
| 230 | const hair = hairLayerFromAppearance(appearance); |
| 231 | if (hair) |
| 232 | { layers.push(hair); } |
| 233 | } |
| 234 | const worn = slots[slot]; |
| 235 | if (!worn) |
| 236 | { continue; } |
| 237 | const overlay = itemEquipSprite(worn.itemId); |
| 238 | if (!overlay) |
| 239 | { continue; } |
| 240 | const tint = resolveEquipTint(worn.itemId, worn.instance); |
| 241 | layers.push({ |
| 242 | sprite: overlay, |
| 243 | itemId: worn.itemId, |
| 244 | ...(tint ? { tint } : {}) |
| 245 | }); |
| 246 | } |
| 247 | return layers; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Preview layers for creation/barber UI. |
| 252 | * @param {{ hideHeadgear?: boolean }} [options] |
| 253 | */ |
| 254 | export function layersForHairPreview (sex, bag, hair, { hideHeadgear = true } = {}) |
| 255 | { |
| 256 | const slots = { ...(bag?.equipment?.slots ?? {}) }; |
| 257 | if (hideHeadgear) |
| 258 | { slots[EQUIPMENT_SLOT.head] = null; } |
| 259 | const style = normalizeHairStyle(sex, hair?.style); |
| 260 | const appearance = { |
| 261 | sex, |
| 262 | sprite: bodySpriteName(sex), |
| 263 | ...appearanceHairFields(style ? { ...hair, style, sex } : null) |
| 264 | }; |
| 265 | if (style) |
| 266 | { appearance.hairStyle = style; } |
| 267 | return composeAppearanceLayers(appearance, slots); |
| 268 | } |
| 269 | |
| 270 | function hslToRgb (h, s, l) |
| 271 | { |
| 272 | const hue = ((h % 360) + 360) % 360 / 360; |
| 273 | const sat = clamp01(s); |
| 274 | const light = clamp01(l); |
| 275 | if (sat === 0) |
| 276 | { return { r: light, g: light, b: light }; } |
| 277 | const q = light < 0.5 ? light * (1 + sat) : light + sat - light * sat; |
| 278 | const p = 2 * light - q; |
| 279 | return { |
| 280 | r: hueToChannel(p, q, hue + 1 / 3), |
| 281 | g: hueToChannel(p, q, hue), |
| 282 | b: hueToChannel(p, q, hue - 1 / 3) |
| 283 | }; |
| 284 | } |
| 285 | |
| 286 | function hueToChannel (p, q, t) |
| 287 | { |
| 288 | let tt = t; |
| 289 | if (tt < 0) |
| 290 | { tt += 1; } |
| 291 | if (tt > 1) |
| 292 | { tt -= 1; } |
| 293 | if (tt < 1 / 6) |
| 294 | { return p + (q - p) * 6 * tt; } |
| 295 | if (tt < 1 / 2) |
| 296 | { return q; } |
| 297 | if (tt < 2 / 3) |
| 298 | { return p + (q - p) * (2 / 3 - tt) * 6; } |
| 299 | return p; |
| 300 | } |