import { SkillId } from "./skills.js"; import { EquipmentKind } from "./equipmentKinds.js"; import { MATERIAL_TIER_ORDER, WOOD_TIER_ORDER, CLOTH_TIER_ORDER, LEATHER_TIER_ORDER, MATERIAL_DISPLAY_NAME, CLOTH_DISPLAY_NAME, LEATHER_DISPLAY_NAME, WoodTier, tierStatBonus, tierRank } from "./materialTiers.js"; import { ARMOUR_CRAFT_PIECES, armourItemIdForTier } from "./armourItems.js"; import { sweaterItemId, shirtItemId, shortsItemId, skirtItemId, trousersItemId } from "./clothItems.js"; import { leatherShoesItemId } from "./leatherItems.js"; import { LEATHER_ARMOUR_PIECES, leatherArmourItemIdForTier } from "./leatherArmourItems.js"; import { wizardHatItemId, wizardRobeTopItemId, wizardRobeBottomsItemId, wizardShoesItemId } from "./wizardItems.js"; import { ItemId, itemDefinition, ITEM_DEFINITIONS } from "./items.js"; import { quarterstaffItemIdForWood } from "./woodItems.js"; import { swordItemIdForMetal } from "./swordItems.js"; import { warAxeItemIdForMetal } from "./warAxeItems.js"; import { arrowItemId, bowItemId } from "./rangedItems.js"; import { staffItemIdForWood } from "./gemItems.js"; /** Tier key for non-tiered singleton rows. */ export const SINGLETON_TIER_KEY = "_"; export const EquipmentStatCategoryId = Object.freeze({ metalArmour: "metalArmour", leatherArmour: "leatherArmour", meleeWeapons: "meleeWeapons", rangedWeapons: "rangedWeapons", magicStaves: "magicStaves", wizardArmour: "wizardArmour", clothing: "clothing" }); /** * Equipment kinds that grant worn combat/stat bonuses and must appear in a category. * Tools / lantern are excluded. */ export const BONUS_EQUIPMENT_KINDS = Object.freeze(new Set([ EquipmentKind.metalWeapon, EquipmentKind.clubWeapon, EquipmentKind.bow, EquipmentKind.arrowAmmo, EquipmentKind.metalArmour, EquipmentKind.leatherArmour, EquipmentKind.clothArmour, EquipmentKind.wizardCloth, EquipmentKind.shield, EquipmentKind.staff ])); function woodDisplayName (tier) { return MATERIAL_DISPLAY_NAME[tier] ?? tier; } function metalDisplayName (tier) { return MATERIAL_DISPLAY_NAME[tier] ?? tier; } /** * @typedef {object} EquipmentStatSection * @property {string} [id] * @property {string} [label] * @property {ReadonlyArray} tierOrder * @property {(tier: string) => string} tierDisplayName * @property {ReadonlyArray} rows */ /** * @typedef {object} EquipmentStatCategory * @property {string} id * @property {string} label * @property {ReadonlyArray} stats * @property {ReadonlyArray} sections */ /** @type {ReadonlyArray} */ export const EQUIPMENT_STAT_CATEGORIES = Object.freeze([ Object.freeze({ id: EquipmentStatCategoryId.metalArmour, label: "Metal armour", stats: Object.freeze([ SkillId.defence, SkillId.strength, SkillId.agility, SkillId.magic, SkillId.archery ]), sections: Object.freeze([ Object.freeze({ id: "metal", tierOrder: MATERIAL_TIER_ORDER, tierDisplayName: metalDisplayName, rows: Object.freeze(ARMOUR_CRAFT_PIECES.map((piece) => Object.freeze({ pieceKey: piece.suffix === "full-helm" ? "fullHelm" : piece.suffix === "horned-helm" ? "hornedHelm" : piece.suffix, label: piece.label.charAt(0).toUpperCase() + piece.label.slice(1), defaultBonusExtra: piece.fullHelm ? 1 : 0, defaultBonusByStat: Object.freeze({ [SkillId.defence]: "tier", [SkillId.strength]: "tierQuarterCeil", [SkillId.agility]: "tierQuarterCeil", [SkillId.magic]: "negativeRank", [SkillId.archery]: "negativeRank" }), resolveItemId: (tier) => armourItemIdForTier(tier, piece.idKey) }))) }) ]) }), Object.freeze({ id: EquipmentStatCategoryId.leatherArmour, label: "Leather armour", stats: Object.freeze([SkillId.agility, SkillId.defence]), sections: Object.freeze([ Object.freeze({ id: "leather", tierOrder: LEATHER_TIER_ORDER, tierDisplayName: (tier) => LEATHER_DISPLAY_NAME[tier] ?? tier, rows: Object.freeze([ Object.freeze({ pieceKey: "shoes", label: "Leather shoes", defaultBonusByStat: Object.freeze({ [SkillId.agility]: "tier", [SkillId.defence]: "tierHalf" }), resolveItemId: (tier) => leatherShoesItemId(tier) }), ...LEATHER_ARMOUR_PIECES.map((piece) => Object.freeze({ pieceKey: piece.pieceKey, label: piece.label.charAt(0).toUpperCase() + piece.label.slice(1), defaultBonusByStat: Object.freeze({ [SkillId.agility]: "tier", [SkillId.defence]: "tierHalf" }), resolveItemId: (tier) => leatherArmourItemIdForTier(tier, piece.idKey) })) ]) }) ]) }), Object.freeze({ id: EquipmentStatCategoryId.meleeWeapons, label: "Melee weapons", stats: Object.freeze([SkillId.strength, SkillId.swordfighting]), sections: Object.freeze([ Object.freeze({ id: "wood", label: "Clubs", stats: Object.freeze([SkillId.strength, SkillId.clubfighting]), tierOrder: WOOD_TIER_ORDER, tierDisplayName: woodDisplayName, rows: Object.freeze([ Object.freeze({ pieceKey: "club", label: "Stick / Quarterstaff", /** Strength curve only — cheap; no weapon-skill bonus. */ defaultBonusByStat: Object.freeze({ [SkillId.strength]: "tier" }), resolveItemId: (tier) => (tier === WoodTier.pine ? ItemId.stick : quarterstaffItemIdForWood(tier)) }) ]) }), Object.freeze({ id: "swords", label: "Swords", stats: Object.freeze([SkillId.strength, SkillId.swordfighting]), tierOrder: MATERIAL_TIER_ORDER, tierDisplayName: metalDisplayName, rows: Object.freeze([ Object.freeze({ pieceKey: "sword", label: "Sword", /** Normal strength + swordfighting equal to tier rank (1…7). */ defaultBonusByStat: Object.freeze({ [SkillId.strength]: "tier", [SkillId.swordfighting]: "rank" }), resolveItemId: (tier) => swordItemIdForMetal(tier) }) ]) }), Object.freeze({ id: "warAxes", label: "War axes", stats: Object.freeze([SkillId.strength, SkillId.axefighting]), tierOrder: MATERIAL_TIER_ORDER, tierDisplayName: metalDisplayName, rows: Object.freeze([ Object.freeze({ pieceKey: "warAxe", label: "War axe", /** Normal strength curve +1 at every tier. */ defaultBonusByStat: Object.freeze({ [SkillId.strength]: "tier" }), defaultBonusExtra: 1, resolveItemId: (tier) => warAxeItemIdForMetal(tier) }) ]) }) ]) }), Object.freeze({ id: EquipmentStatCategoryId.rangedWeapons, label: "Ranged weapons", stats: Object.freeze([SkillId.dexterity]), sections: Object.freeze([ Object.freeze({ id: "bows", label: "Bows", stats: Object.freeze([SkillId.archery]), tierOrder: WOOD_TIER_ORDER, tierDisplayName: woodDisplayName, rows: Object.freeze([ Object.freeze({ pieceKey: "bow", label: "Bow", resolveItemId: (tier) => bowItemId(tier) }) ]) }), Object.freeze({ id: "arrows", label: "Arrows", stats: Object.freeze([SkillId.dexterity]), tierOrder: MATERIAL_TIER_ORDER, tierDisplayName: metalDisplayName, rows: Object.freeze([ Object.freeze({ pieceKey: "arrow", label: "Arrow", resolveItemId: (tier) => arrowItemId(tier) }) ]) }) ]) }), Object.freeze({ id: EquipmentStatCategoryId.magicStaves, label: "Magic staves", stats: Object.freeze([SkillId.magic]), sections: Object.freeze([ Object.freeze({ id: "wood", tierOrder: WOOD_TIER_ORDER, tierDisplayName: woodDisplayName, rows: Object.freeze([ Object.freeze({ pieceKey: "staff", label: "Staff", resolveItemId: (tier) => staffItemIdForWood(tier) }) ]) }) ]) }), Object.freeze({ id: EquipmentStatCategoryId.wizardArmour, label: "Wizard armour", stats: Object.freeze([SkillId.magic]), sections: Object.freeze([ Object.freeze({ id: "cloth", tierOrder: CLOTH_TIER_ORDER, tierDisplayName: (tier) => CLOTH_DISPLAY_NAME[tier] ?? tier, rows: Object.freeze([ Object.freeze({ pieceKey: "hat", label: "Hat", defaultBonusByStat: Object.freeze({ [SkillId.magic]: "tier" }), resolveItemId: (tier) => wizardHatItemId(tier) }), Object.freeze({ pieceKey: "robetop", label: "Robe top", defaultBonusByStat: Object.freeze({ [SkillId.magic]: "tier" }), resolveItemId: (tier) => wizardRobeTopItemId(tier) }), Object.freeze({ pieceKey: "robebottoms", label: "Robe bottoms", defaultBonusByStat: Object.freeze({ [SkillId.magic]: "tier" }), resolveItemId: (tier) => wizardRobeBottomsItemId(tier) }), Object.freeze({ pieceKey: "shoes", label: "Shoes", defaultBonusByStat: Object.freeze({ [SkillId.magic]: "tier" }), resolveItemId: (tier) => wizardShoesItemId(tier) }) ]) }) ]) }), Object.freeze({ id: EquipmentStatCategoryId.clothing, label: "Clothing", stats: Object.freeze([SkillId.defence]), sections: Object.freeze([ Object.freeze({ id: "sweaters", label: "Sweaters", tierOrder: CLOTH_TIER_ORDER, tierDisplayName: (tier) => CLOTH_DISPLAY_NAME[tier] ?? tier, rows: Object.freeze([ Object.freeze({ pieceKey: "sweater", label: "Sweater", defaultBonus: 0, resolveItemId: (tier) => sweaterItemId(tier) }) ]) }), Object.freeze({ id: "shirts", label: "Shirts", tierOrder: CLOTH_TIER_ORDER, tierDisplayName: (tier) => CLOTH_DISPLAY_NAME[tier] ?? tier, rows: Object.freeze([ Object.freeze({ pieceKey: "shirt", label: "Shirt", defaultBonus: 0, resolveItemId: (tier) => shirtItemId(tier) }) ]) }), Object.freeze({ id: "shorts", label: "Shorts", tierOrder: CLOTH_TIER_ORDER, tierDisplayName: (tier) => CLOTH_DISPLAY_NAME[tier] ?? tier, rows: Object.freeze([ Object.freeze({ pieceKey: "shorts", label: "Shorts", defaultBonus: 0, resolveItemId: (tier) => shortsItemId(tier) }) ]) }), Object.freeze({ id: "skirts", label: "Skirts", tierOrder: CLOTH_TIER_ORDER, tierDisplayName: (tier) => CLOTH_DISPLAY_NAME[tier] ?? tier, rows: Object.freeze([ Object.freeze({ pieceKey: "skirt", label: "Skirt", defaultBonus: 0, resolveItemId: (tier) => skirtItemId(tier) }) ]) }), Object.freeze({ id: "trousers", label: "Trousers", tierOrder: CLOTH_TIER_ORDER, tierDisplayName: (tier) => CLOTH_DISPLAY_NAME[tier] ?? tier, rows: Object.freeze([ Object.freeze({ pieceKey: "trousers", label: "Trousers", defaultBonus: 0, resolveItemId: (tier) => trousersItemId(tier) }) ]) }) ]) }) ]); export function equipmentStatCategory (categoryId) { return EQUIPMENT_STAT_CATEGORIES.find((entry) => entry.id === categoryId) ?? null; } /** Stats columns for a section (section override, else category). */ export function equipmentStatSectionStats (category, section) { return section?.stats ?? category?.stats ?? []; } /** Flat list of all piece rows with category + section context. */ export function listEquipmentStatRows () { const out = []; for (const category of EQUIPMENT_STAT_CATEGORIES) { for (const section of category.sections) { const stats = equipmentStatSectionStats(category, section); for (const row of section.rows) { out.push({ categoryId: category.id, stats, section, row }); } } } return out; } /** * Map itemId → { categoryId, pieceKey, stats, singleton, section }. * Built lazily so item defs are fully loaded. */ let _itemLookup = null; export function equipmentStatLookupForItem (itemId) { if (!_itemLookup) { _itemLookup = buildItemLookup(); } return _itemLookup.get(itemId | 0) ?? null; } function buildItemLookup () { const map = new Map(); for (const entry of listEquipmentStatRows()) { const { categoryId, stats, section, row } = entry; for (const tier of section.tierOrder) { const itemId = row.resolveItemId?.(tier) ?? (row.singleton ? row.itemId : null); if (itemId == null) { continue; } map.set(itemId | 0, Object.freeze({ categoryId, pieceKey: row.pieceKey, stats, singleton: !!row.singleton, tierKey: row.singleton ? SINGLETON_TIER_KEY : tier })); } } return map; } /** Reset cached lookup (tests). */ export function clearEquipmentStatLookupCache () { _itemLookup = null; } function emptyStatBag (stats) { const bag = {}; for (const skillId of stats) { bag[skillId] = 0; } return bag; } function resolveDefaultBonusMode (mode, tier, row) { const key = tier === SINGLETON_TIER_KEY ? null : tier; const extra = Number(row.defaultBonusExtra) || 0; if (mode === "rank") { return Math.max(0, Math.floor(tierRank(key) + extra)); } if (mode === "negativeRank") { return -Math.max(0, Math.floor(tierRank(key))); } if (mode === "tierHalf") { const base = row.defaultBonus != null ? Number(row.defaultBonus) || 0 : tierStatBonus(key); return Math.max(0, Math.floor(base / 2) + extra); } if (mode === "tierQuarterCeil") { const base = row.defaultBonus != null ? Number(row.defaultBonus) || 0 : tierStatBonus(key); return Math.max(0, Math.ceil(base / 4)); } if (mode === "tier" || mode == null) { const base = row.defaultBonus != null ? Number(row.defaultBonus) || 0 : tierStatBonus(key); return Math.max(0, Math.floor(base + extra)); } const n = Math.floor(Number(mode)); return Number.isFinite(n) ? Math.max(0, n) : 0; } function defaultCellForRow (row, stats, tier) { const cell = emptyStatBag(stats); const byStat = row.defaultBonusByStat; if (byStat && typeof byStat === "object") { for (const [skillId, mode] of Object.entries(byStat)) { if (!Object.prototype.hasOwnProperty.call(cell, skillId)) { continue; } cell[skillId] = resolveDefaultBonusMode(mode, tier, row); } return cell; } const value = resolveDefaultBonusMode("tier", tier, row); if (row.bonusStat) { if (Object.prototype.hasOwnProperty.call(cell, row.bonusStat)) { cell[row.bonusStat] = value; } return cell; } for (const skillId of stats) { cell[skillId] = value; } return cell; } /** Seed defaults from current tier curve (full helm +1, cosmetics 0). */ export function defaultEquipmentStatBonuses () { const out = {}; for (const category of EQUIPMENT_STAT_CATEGORIES) { const cat = {}; for (const section of category.sections) { const stats = equipmentStatSectionStats(category, section); for (const row of section.rows) { const byTier = {}; for (const tier of section.tierOrder) { const itemId = row.resolveItemId?.(tier) ?? (row.singleton ? row.itemId : null); if (itemId == null && !row.singleton) { continue; } byTier[tier] = defaultCellForRow(row, stats, tier); } cat[row.pieceKey] = byTier; } } out[category.id] = cat; } return out; } function coerceBonusInt (raw) { const n = Math.floor(Number(raw)); if (!Number.isFinite(n)) { return 0; } return Math.max(-99, Math.min(99, n)); } /** * Merge authored blob with defaults; drop unknown keys; fill missing cells. * @param {object|null|undefined} raw */ export function normalizeEquipmentStatBonuses (raw) { const defaults = defaultEquipmentStatBonuses(); const src = raw && typeof raw === "object" ? raw : {}; const out = {}; for (const category of EQUIPMENT_STAT_CATEGORIES) { const srcCat = src[category.id] && typeof src[category.id] === "object" ? src[category.id] : {}; const defCat = defaults[category.id] ?? {}; const cat = {}; for (const section of category.sections) { const stats = equipmentStatSectionStats(category, section); for (const row of section.rows) { const srcPiece = srcCat[row.pieceKey] && typeof srcCat[row.pieceKey] === "object" ? srcCat[row.pieceKey] : {}; const defPiece = defCat[row.pieceKey] ?? {}; const byTier = {}; for (const tier of section.tierOrder) { const itemId = row.resolveItemId?.(tier) ?? (row.singleton ? row.itemId : null); if (itemId == null && !row.singleton) { continue; } const srcCell = srcPiece[tier] && typeof srcPiece[tier] === "object" ? srcPiece[tier] : null; const defCell = defPiece[tier] ?? defaultCellForRow(row, stats, tier); const cell = {}; for (const skillId of stats) { if (srcCell && Object.prototype.hasOwnProperty.call(srcCell, skillId)) { cell[skillId] = coerceBonusInt(srcCell[skillId]); } else if ( skillId === SkillId.archery && row.pieceKey === "bow" && srcCell && Object.prototype.hasOwnProperty.call(srcCell, SkillId.dexterity) ) { // Legacy: bows stored dexterity before archery column split. cell[skillId] = coerceBonusInt(srcCell[SkillId.dexterity]); } else { cell[skillId] = coerceBonusInt(defCell[skillId]); } } byTier[tier] = cell; } cat[row.pieceKey] = byTier; } } out[category.id] = cat; } return out; } /** * Resolve per-skill bonuses for one equipped item def. * @returns {Record|null} */ export function bonusForEquippedItem (definition, settingsBonuses = null) { const equipment = definition?.equipment; if (!equipment?.kind || !BONUS_EQUIPMENT_KINDS.has(equipment.kind)) { return null; } const itemId = definition.itemId; const lookup = equipmentStatLookupForItem(itemId); if (!lookup) { return null; } const table = settingsBonuses && typeof settingsBonuses === "object" ? settingsBonuses : defaultEquipmentStatBonuses(); const piece = table[lookup.categoryId]?.[lookup.pieceKey]; if (!piece) { return emptyStatBag(lookup.stats); } const tierKey = lookup.singleton ? SINGLETON_TIER_KEY : (equipment.tier ?? definition.tier ?? lookup.tierKey); const cell = piece[tierKey] ?? piece[lookup.tierKey]; if (!cell) { return emptyStatBag(lookup.stats); } const out = {}; for (const skillId of lookup.stats) { out[skillId] = coerceBonusInt(cell[skillId]); } return out; } /** * Changelog bullets for equipmentStatBonuses edits. * @param {(itemId: number) => string|null} [displayNameFor] */ export function diffEquipmentStatBonuses (previous, next, displayNameFor = null) { const prev = normalizeEquipmentStatBonuses(previous); const upcoming = normalizeEquipmentStatBonuses(next); const bullets = []; for (const category of EQUIPMENT_STAT_CATEGORIES) { for (const section of category.sections) { const stats = equipmentStatSectionStats(category, section); for (const row of section.rows) { for (const tier of section.tierOrder) { const from = prev[category.id]?.[row.pieceKey]?.[tier]; const to = upcoming[category.id]?.[row.pieceKey]?.[tier]; if (!from && !to) { continue; } for (const skillId of stats) { const a = coerceBonusInt(from?.[skillId]); const b = coerceBonusInt(to?.[skillId]); if (a === b) { continue; } const itemId = row.resolveItemId?.(tier) ?? row.itemId; const name = (itemId != null && displayNameFor?.(itemId)) || `${row.label} (${section.tierDisplayName(tier)})`; bullets.push(`${name} ${skillId} bonus changed from ${a} to ${b}.`); } } } } } return bullets; } export function equipmentStatBonusMapsEqual (a, b) { const left = normalizeEquipmentStatBonuses(a); const right = normalizeEquipmentStatBonuses(b); return JSON.stringify(left) === JSON.stringify(right); } /** * Every equippable item with a bonus kind must appear in exactly one category row. * @returns {{ ok: true } | { ok: false, missing: number[], duplicates: number[] }} */ export function assertAllBonusGearCategorized () { clearEquipmentStatLookupCache(); const seen = new Map(); const duplicates = []; for (const entry of listEquipmentStatRows()) { for (const tier of entry.section.tierOrder) { const itemId = entry.row.resolveItemId?.(tier) ?? entry.row.itemId; if (itemId == null) { continue; } const id = itemId | 0; if (seen.has(id)) { duplicates.push(id); } else { seen.set(id, entry); } } } const missing = []; for (const def of Object.values(ITEM_DEFINITIONS)) { const kind = def?.equipment?.kind; if (!kind || !BONUS_EQUIPMENT_KINDS.has(kind)) { continue; } if (!seen.has(def.itemId | 0)) { missing.push(def.itemId | 0); } } if (missing.length || duplicates.length) { return { ok: false, missing, duplicates }; } return { ok: true }; } /** Preview item id for a row at tier (editor icon). */ export function equipmentStatRowItemId (row, tier) { if (row.singleton) { return row.itemId ?? row.resolveItemId?.(SINGLETON_TIER_KEY) ?? null; } return row.resolveItemId?.(tier) ?? null; } export function equipmentStatRowDefaultTier (section, row) { for (const tier of section.tierOrder) { if (equipmentStatRowItemId(row, tier) != null) { return tier; } } return section.tierOrder[0] ?? SINGLETON_TIER_KEY; } /** Display helper — re-export itemDefinition for callers that only import this module. */ export { itemDefinition };