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