BlueGit

timfa questward

Files

questward / shared / appearance / npcEquipment.js

npcEquipment.js

npcEquipment.js 1.9 KiB Download Raw
1import { hashString } from "./hair.js";
2import { ItemId } from "../definitions/items.js";
3import { sweaterItemId } from "../definitions/clothItems.js";
4import { CLOTH_TIER_ORDER } from "../definitions/materialTiers.js";
5import { LeatherItemId } from "../definitions/leatherItems.js";
6import { normalizeRgb } from "../render/tint.js";
7
8/** Minimum byte channel when rolling random NPC cloth dyes (avoids near-black). */
9export const NPC_CLOTH_DYE_BYTE_MIN = 40;
10
11/**
12 * Deterministic dye from a display name (+ optional salt).
13 * @returns {{ r: number, g: number, b: number }} linear RGB 0–1
14 */
15export function npcClothDyeFromSeed (displayName, salt = 0)
16{
17 const seed = hashString(`${String(displayName ?? "").trim().toLowerCase() || "npc"}:${salt}`);
18 const span = 256 - NPC_CLOTH_DYE_BYTE_MIN;
19 const r = NPC_CLOTH_DYE_BYTE_MIN + (seed % span);
20 const g = NPC_CLOTH_DYE_BYTE_MIN + ((seed >>> 8) % span);
21 const b = NPC_CLOTH_DYE_BYTE_MIN + ((seed >>> 16) % span);
22 return normalizeRgb({ r: r / 255, g: g / 255, b: b / 255 });
23}
24
25/**
26 * Default humanoid spawner clothing when a slot is left empty.
27 * Chest/legs use random dyes; boots are soft-leather shoes (tier tint).
28 *
29 * @returns {Partial<Record<"chest"|"legs"|"boots", { itemId: number, dyeColor: object|null }>>}
30 */
31export function npcDefaultEquipment (displayName, sex = "male")
32{
33 const name = String(displayName ?? "").trim().toLowerCase() || "npc";
34 const seed = hashString(name);
35
36 const wantsSweater = (seed >>> 12) % 5 === 0;
37 const chestTier = CLOTH_TIER_ORDER[(seed >>> 6) % CLOTH_TIER_ORDER.length];
38 const chestItemId = wantsSweater
39 ? sweaterItemId(chestTier)
40 : ItemId.clothShirt;
41
42 const legsItemId = (seed & 1) === 0
43 ? LeatherItemId.clothShorts
44 : LeatherItemId.clothTrousers;
45
46 return {
47 chest: { itemId: chestItemId, dyeColor: npcClothDyeFromSeed(name, 1) },
48 legs: { itemId: legsItemId, dyeColor: npcClothDyeFromSeed(name, 2) },
49 boots: { itemId: LeatherItemId.leatherShoes, dyeColor: null }
50 };
51}