BlueGit

timfa questward

Files

questward / shared / appearance / hair.js

hair.js

hair.js 8.4 KiB Download Raw
1import { spriteImageName } from "../definitions/tileSprites.js";
2import {
3 EQUIPMENT_SLOT,
4 EQUIPMENT_DRAW_ORDER,
5 itemDefinition,
6 itemEquipSprite
7} from "../definitions/items.js";
8import { normalizeRgb, resolveEquipTint } from "../render/tint.js";
9
10/** Hair style ids per sex (PNG stems under `characters/player-body-*`). */
11export 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
16export const DEFAULT_HAIR_STYLE = "hair-1";
17export const DEFAULT_HAIR_HUE = 30;
18export const DEFAULT_HAIR_SATURATION = 0.55;
19export 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 */
25export 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. */
43export 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 */
59export 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
69export function bodySpriteName (sex)
70{
71 const key = sex === "female" ? "player-body-female" : "player-body-male";
72 return spriteImageName("character", key, key);
73}
74
75export function listHairStyles (sex)
76{
77 return HAIR_STYLES_BY_SEX[sex === "female" ? "female" : "male"] ?? HAIR_STYLES_BY_SEX.male;
78}
79
80export function isValidHairStyle (sex, styleId)
81{
82 if (styleId == null || styleId === "" || styleId === "none")
83 { return true; }
84 return listHairStyles(sex).includes(String(styleId));
85}
86
87export 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
95export 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
103export 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). */
117export 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
124export 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
133export 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
153export 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. */
178export function headgearKeepsHair (wornHead)
179{
180 if (!wornHead)
181 { return true; }
182 const def = itemDefinition(wornHead.itemId);
183 return !!def?.equipment?.keepHair;
184}
185
186export 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
195export 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 */
217export function composeAppearanceLayers (appearance, equipmentSlots)
218{
219 const sex = appearance?.sex === "female" ? "female" : "male";
220 const layers = [{ sprite: appearance?.sprite ?? bodySpriteName(sex) }];
221 const slots = equipmentSlots ?? {};
222 for (const slot of EQUIPMENT_DRAW_ORDER)
223 {
224 if (slot === EQUIPMENT_SLOT.head && shouldShowHair(appearance, slots))
225 {
226 const hair = hairLayerFromAppearance(appearance);
227 if (hair)
228 { layers.push(hair); }
229 }
230 const worn = slots[slot];
231 if (!worn)
232 { continue; }
233 const overlay = itemEquipSprite(worn.itemId);
234 if (!overlay)
235 { continue; }
236 const tint = resolveEquipTint(worn.itemId, worn.instance);
237 layers.push({
238 sprite: overlay,
239 itemId: worn.itemId,
240 ...(tint ? { tint } : {})
241 });
242 }
243 return layers;
244}
245
246/**
247 * Preview layers for creation/barber UI.
248 * @param {{ hideHeadgear?: boolean }} [options]
249 */
250export function layersForHairPreview (sex, bag, hair, { hideHeadgear = true } = {})
251{
252 const slots = { ...(bag?.equipment?.slots ?? {}) };
253 if (hideHeadgear)
254 { slots[EQUIPMENT_SLOT.head] = null; }
255 const style = normalizeHairStyle(sex, hair?.style);
256 const appearance = {
257 sex,
258 sprite: bodySpriteName(sex),
259 ...appearanceHairFields(style ? { ...hair, style, sex } : null)
260 };
261 if (style)
262 { appearance.hairStyle = style; }
263 return composeAppearanceLayers(appearance, slots);
264}
265
266function hslToRgb (h, s, l)
267{
268 const hue = ((h % 360) + 360) % 360 / 360;
269 const sat = clamp01(s);
270 const light = clamp01(l);
271 if (sat === 0)
272 { return { r: light, g: light, b: light }; }
273 const q = light < 0.5 ? light * (1 + sat) : light + sat - light * sat;
274 const p = 2 * light - q;
275 return {
276 r: hueToChannel(p, q, hue + 1 / 3),
277 g: hueToChannel(p, q, hue),
278 b: hueToChannel(p, q, hue - 1 / 3)
279 };
280}
281
282function hueToChannel (p, q, t)
283{
284 let tt = t;
285 if (tt < 0)
286 { tt += 1; }
287 if (tt > 1)
288 { tt -= 1; }
289 if (tt < 1 / 6)
290 { return p + (q - p) * 6 * tt; }
291 if (tt < 1 / 2)
292 { return q; }
293 if (tt < 2 / 3)
294 { return p + (q - p) * (2 / 3 - tt) * 6; }
295 return p;
296}