import { SkillId, SKILL_DEFINITIONS } from "../definitions/skills.js"; import { MATERIAL_TIER_ORDER, WOOD_TIER_ORDER, CLOTH_TIER_ORDER, LEATHER_TIER_ORDER, GEM_TIER_ORDER, ORE_BEARING_MATERIALS, MATERIAL_DISPLAY_NAME, CLOTH_DISPLAY_NAME, LEATHER_DISPLAY_NAME, woodTierForMetal, tierMinSkillLevel, tierMineSkillLevel, tierSmeltSkillLevel, isGemstoneMaterial, GatherMaterialId } from "../definitions/materialTiers.js"; import { firestartingDifficulty } from "../definitions/craftingStations.js"; import { HERB_DEFINITIONS, HERB_GATHER_SKILL_LEVEL } from "../definitions/herbs.js"; import { SPELL_DEFINITIONS, spellMinMagicLevel } from "../definitions/spells.js"; import { ALCHEMY_POTION_DEFINITIONS } from "../definitions/alchemy.js"; /** * Unlock lines for skill thresholds crossed when leveling from `fromLevel` (exclusive) * to `toLevel` (inclusive). Pure / shared — used by client toasts. * @param {string} skillId * @param {number} fromLevel * @param {number} toLevel * @returns {string[]} */ export function skillUnlockMessages (skillId, fromLevel, toLevel) { const from = Math.max(0, Math.floor(Number(fromLevel) || 0)); const to = Math.max(0, Math.floor(Number(toLevel) || 0)); if (to <= from) { return []; } const entries = unlockCatalog().filter((entry) => entry.skillId === skillId && entry.level > from && entry.level <= to); entries.sort((a, b) => a.level - b.level || a.message.localeCompare(b.message)); return entries.map((entry) => entry.message); } /** Level-up toast title line. */ export function skillLevelUpToastTitle (skillId, level) { const name = SKILL_DEFINITIONS[skillId]?.displayName ?? skillId; return `Your ${name} leveled up to ${level | 0}!`; } /** * Full toast body: title + unlock lines. * @returns {string} */ export function skillLevelUpToastText (skillId, level, previousLevel = null) { const prev = previousLevel == null ? Math.max(0, (level | 0) - 1) : previousLevel | 0; const lines = [ skillLevelUpToastTitle(skillId, level), ...skillUnlockMessages(skillId, prev, level) ]; return lines.join("\n"); } /** @type {null | Array<{ skillId: string, level: number, message: string }>} */ let _catalog = null; function unlockCatalog () { if (!_catalog) { _catalog = buildUnlockCatalog(); } return _catalog; } /** Reset cache (tests). */ export function clearSkillUnlockCatalogCache () { _catalog = null; } function buildUnlockCatalog () { /** @type {Array<{ skillId: string, level: number, message: string }>} */ const out = []; const add = (skillId, level, message) => { if ((level | 0) <= 1) { return; } out.push({ skillId, level: level | 0, message }); }; for (const metal of MATERIAL_TIER_ORDER) { const name = MATERIAL_DISPLAY_NAME[metal] ?? metal; const wood = woodTierForMetal(metal); const woodName = MATERIAL_DISPLAY_NAME[wood] ?? wood; const equip = tierMinSkillLevel(metal); const craft = tierSmeltSkillLevel(metal); add(SkillId.defence, equip, `You can now wear ${name} armour.`); add(SkillId.swordfighting, equip, `You can now equip ${name} swords.`); add(SkillId.axefighting, equip, `You can now equip ${name} war axes.`); add(SkillId.archery, equip, `You can now equip ${woodName} bows and ${name} arrows.`); add(SkillId.woodcutting, equip, `You can now equip ${name} axes.`); add(SkillId.mining, equip, `You can now equip ${name} pickaxes.`); add(SkillId.herbalism, equip, `You can now equip ${name} plant clippers.`); add(SkillId.smithing, craft, `You can now smith ${name} items.`); add(SkillId.crafting, craft, `You can now craft ${name} tools.`); add(SkillId.crafting, equip, `You can now craft ${name} arrows.`); } for (const wood of WOOD_TIER_ORDER) { const name = MATERIAL_DISPLAY_NAME[wood] ?? wood; const equip = tierMinSkillLevel(wood); const craft = tierSmeltSkillLevel(wood); const fell = tierMineSkillLevel(wood); const light = firestartingDifficulty(wood); add(SkillId.clubfighting, equip, `You can now equip ${name} quarterstaves.`); add(SkillId.magic, equip, `You can now equip ${name} staves.`); add(SkillId.fishing, equip, `You can now equip ${name} fishing rods.`); add(SkillId.woodworking, craft, `You can now carve ${name} wood items.`); add(SkillId.woodworking, equip, `You can now carve ${name} bows.`); add(SkillId.jewelering, equip, `You can now craft ${name} staves.`); add(SkillId.woodcutting, fell, `You can now chop ${name} trees.`); add(SkillId.firestarting, light, `You can now light ${name} logs.`); } for (const leather of LEATHER_TIER_ORDER) { const name = LEATHER_DISPLAY_NAME[leather] ?? leather; const craft = tierSmeltSkillLevel(leather); add(SkillId.leatherworking, craft, `You can now craft ${name}.`); } for (const cloth of CLOTH_TIER_ORDER) { const name = CLOTH_DISPLAY_NAME[cloth] ?? cloth; const equip = tierMinSkillLevel(cloth); const craft = tierSmeltSkillLevel(cloth); add(SkillId.defence, equip, `You can now wear ${name} clothing.`); add(SkillId.tailoring, craft, `You can now tailor ${name} goods.`); } for (const gem of GEM_TIER_ORDER) { const name = MATERIAL_DISPLAY_NAME[gem] ?? gem; const cut = tierSmeltSkillLevel(gem); const equip = tierMinSkillLevel(gem); add(SkillId.jewelering, cut, `You can now cut ${name}.`); add(SkillId.jewelering, equip, `You can now craft ${name} jewellery (wearable at combat level ${equip}).`); } /** @type {Map} */ const mineByLevel = new Map(); for (const material of ORE_BEARING_MATERIALS) { if (material === GatherMaterialId.bronze || material === GatherMaterialId.steel) { continue; } const level = tierMineSkillLevel(material); const name = MATERIAL_DISPLAY_NAME[material] ?? material; let label = name; if (material === GatherMaterialId.coal) { label = "Coal"; } else if (!isGemstoneMaterial(material)) { label = `${name} ore`; } const list = mineByLevel.get(level) ?? []; list.push(label); mineByLevel.set(level, list); } for (const [level, labels] of mineByLevel) { add(SkillId.mining, level, `You can now mine ${joinNames(labels)}.`); } /** @type {Map} */ const herbsByLevel = new Map(); for (const herb of Object.values(HERB_DEFINITIONS)) { const level = HERB_GATHER_SKILL_LEVEL[herb.tier] ?? 1; const list = herbsByLevel.get(level) ?? []; list.push(herb.displayName); herbsByLevel.set(level, list); } for (const [level, names] of herbsByLevel) { add(SkillId.herbalism, level, `You can now gather ${joinNames(names)}.`); } /** @type {Map} */ const spellsByLevel = new Map(); for (const spell of Object.values(SPELL_DEFINITIONS)) { if (spell.mobOnly || spell.starter) { continue; } const level = spellMinMagicLevel(spell); const entry = spellsByLevel.get(level) ?? { names: [], tier: spell.tier | 0 || 1 }; entry.names.push(spell.displayName); entry.tier = Math.min(entry.tier, spell.tier | 0 || 1); spellsByLevel.set(level, entry); } for (const [level, { names, tier }] of spellsByLevel) { if (names.length <= 4) { add(SkillId.magic, level, `You can now cast ${joinNames(names)}.`); } else { add(SkillId.magic, level, `You can now cast Magic tier ${tier} spells.`); } } /** @type {Map} */ const potionsByLevel = new Map(); for (const potion of Object.values(ALCHEMY_POTION_DEFINITIONS)) { const level = Math.max(1, potion.minAlchemyLevel | 0 || 1); const list = potionsByLevel.get(level) ?? []; list.push(potion.displayName); potionsByLevel.set(level, list); } for (const [level, names] of potionsByLevel) { add(SkillId.alchemy, level, `You can now brew ${joinNames(names)}.`); } for (const material of [GatherMaterialId.silver, GatherMaterialId.gold, GatherMaterialId.electrum]) { const name = MATERIAL_DISPLAY_NAME[material] ?? material; add(SkillId.smithing, tierSmeltSkillLevel(material), `You can now smith ${name} bars.`); } return Object.freeze(out.map((entry) => Object.freeze(entry))); } function joinNames (names) { const list = [...names]; if (list.length === 0) { return "new materials"; } if (list.length === 1) { return list[0]; } if (list.length === 2) { return `${list[0]} and ${list[1]}`; } return `${list.slice(0, -1).join(", ")}, and ${list[list.length - 1]}`; }