/** * Dialogue line / choice text placeholders: * [playername], [playerfullname], [playersurname], and gendered forms ([he]/[Mr], …). * Gender tokens keep the casing of the placeholder ([He] → She). */ const NAME_TOKENS = Object.freeze({ playername: "firstName", playerfullname: "fullName", playersurname: "lastName" }); /** Male form → [male, female] lowercase pair. Lookup is case-insensitive. */ const GENDERED_TOKENS = Object.freeze({ mr: ["mr", "ms"], he: ["he", "she"], him: ["him", "her"], his: ["his", "her"], hers: ["his", "hers"], himself: ["himself", "herself"], man: ["man", "woman"], men: ["men", "women"], male: ["male", "female"], boy: ["boy", "girl"], lad: ["lad", "lass"], brother: ["brother", "sister"], sir: ["sir", "ma'am"], lord: ["lord", "lady"], king: ["king", "queen"], husband: ["husband", "wife"], son: ["son", "daughter"], father: ["father", "mother"], uncle: ["uncle", "aunt"], nephew: ["nephew", "niece"] }); /** * Catalog for the editor help modal. * @returns {{ token: string, meaning: string }[]} */ export function dialogueReplacementCatalog () { return [ { token: "[playername]", meaning: "Player’s first name" }, { token: "[playerfullname]", meaning: "Player’s first and last name" }, { token: "[playersurname]", meaning: "Player’s last name" }, ...Object.entries(GENDERED_TOKENS).map(([key, [male, female]]) => ({ token: `[${key}]`, meaning: `${male} / ${female} (casing follows the tag, e.g. [He] → She)` })) ]; } /** * Apply name + gendered replacements to dialogue text. * @param {string} text * @param {{ firstName?: string, lastName?: string, sex?: string }} identity */ export function applyDialogueTextReplacements (text, identity = {}) { const raw = String(text ?? ""); if (!raw.includes("[")) { return raw; } const firstName = String(identity.firstName ?? "").trim() || "Adventurer"; const lastName = String(identity.lastName ?? "").trim(); const fullName = lastName ? `${firstName} ${lastName}` : firstName; const female = identity.sex === "female"; const names = { firstName, lastName: lastName || firstName, fullName }; return raw.replace(/\[([^\]]+)\]/g, (match, inner) => { const key = String(inner).trim().toLowerCase(); const nameField = NAME_TOKENS[key]; if (nameField) { return names[nameField]; } const pair = GENDERED_TOKENS[key]; if (!pair) { return match; } const word = female ? pair[1] : pair[0]; return applyTokenCasing(inner.trim(), word); }); } /** Copy casing style from the placeholder body onto the replacement word. */ export function applyTokenCasing (template, word) { const src = String(template ?? ""); const dest = String(word ?? ""); if (!src || !dest) { return dest; } if (src === src.toUpperCase()) { return dest.toUpperCase(); } if (src[0] === src[0].toUpperCase() && src.slice(1) === src.slice(1).toLowerCase()) { return dest.charAt(0).toUpperCase() + dest.slice(1).toLowerCase(); } return dest.toLowerCase(); }