| 1 | import { hashString } from "./hair.js"; |
| 2 | import { ItemId } from "../definitions/items.js"; |
| 3 | import { sweaterItemId } from "../definitions/clothItems.js"; |
| 4 | import { CLOTH_TIER_ORDER } from "../definitions/materialTiers.js"; |
| 5 | import { LeatherItemId } from "../definitions/leatherItems.js"; |
| 6 | import { normalizeRgb } from "../render/tint.js"; |
| 7 | |
| 8 | /** Minimum byte channel when rolling random NPC cloth dyes (avoids near-black). */ |
| 9 | export 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 | */ |
| 15 | export 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 | */ |
| 31 | export 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 | } |