| 1 | import { SkillId } from "./skills.js"; |
| 2 | import { EquipmentKind } from "./equipmentKinds.js"; |
| 3 | import |
| 4 | { |
| 5 | MATERIAL_TIER_ORDER, |
| 6 | WOOD_TIER_ORDER, |
| 7 | CLOTH_TIER_ORDER, |
| 8 | LEATHER_TIER_ORDER, |
| 9 | MATERIAL_DISPLAY_NAME, |
| 10 | CLOTH_DISPLAY_NAME, |
| 11 | LEATHER_DISPLAY_NAME, |
| 12 | WoodTier, |
| 13 | tierStatBonus, |
| 14 | tierRank |
| 15 | } from "./materialTiers.js"; |
| 16 | import { ARMOUR_CRAFT_PIECES, armourItemIdForTier } from "./armourItems.js"; |
| 17 | import { sweaterItemId, shirtItemId, shortsItemId, skirtItemId, trousersItemId } from "./clothItems.js"; |
| 18 | import { leatherShoesItemId } from "./leatherItems.js"; |
| 19 | import { LEATHER_ARMOUR_PIECES, leatherArmourItemIdForTier } from "./leatherArmourItems.js"; |
| 20 | import |
| 21 | { |
| 22 | wizardHatItemId, |
| 23 | wizardRobeTopItemId, |
| 24 | wizardRobeBottomsItemId, |
| 25 | wizardShoesItemId |
| 26 | } from "./wizardItems.js"; |
| 27 | import { ItemId, itemDefinition, ITEM_DEFINITIONS } from "./items.js"; |
| 28 | import { quarterstaffItemIdForWood } from "./woodItems.js"; |
| 29 | import { swordItemIdForMetal } from "./swordItems.js"; |
| 30 | import { warAxeItemIdForMetal } from "./warAxeItems.js"; |
| 31 | import { arrowItemId, bowItemId } from "./rangedItems.js"; |
| 32 | import { staffItemIdForWood } from "./gemItems.js"; |
| 33 | |
| 34 | /** Tier key for non-tiered singleton rows. */ |
| 35 | export const SINGLETON_TIER_KEY = "_"; |
| 36 | |
| 37 | export const EquipmentStatCategoryId = Object.freeze({ |
| 38 | metalArmour: "metalArmour", |
| 39 | leatherArmour: "leatherArmour", |
| 40 | meleeWeapons: "meleeWeapons", |
| 41 | rangedWeapons: "rangedWeapons", |
| 42 | magicStaves: "magicStaves", |
| 43 | wizardArmour: "wizardArmour", |
| 44 | clothing: "clothing" |
| 45 | }); |
| 46 | |
| 47 | /** |
| 48 | * Equipment kinds that grant worn combat/stat bonuses and must appear in a category. |
| 49 | * Tools / lantern are excluded. |
| 50 | */ |
| 51 | export const BONUS_EQUIPMENT_KINDS = Object.freeze(new Set([ |
| 52 | EquipmentKind.metalWeapon, |
| 53 | EquipmentKind.clubWeapon, |
| 54 | EquipmentKind.bow, |
| 55 | EquipmentKind.arrowAmmo, |
| 56 | EquipmentKind.metalArmour, |
| 57 | EquipmentKind.leatherArmour, |
| 58 | EquipmentKind.clothArmour, |
| 59 | EquipmentKind.wizardCloth, |
| 60 | EquipmentKind.shield, |
| 61 | EquipmentKind.staff |
| 62 | ])); |
| 63 | |
| 64 | function woodDisplayName (tier) |
| 65 | { |
| 66 | return MATERIAL_DISPLAY_NAME[tier] ?? tier; |
| 67 | } |
| 68 | |
| 69 | function metalDisplayName (tier) |
| 70 | { |
| 71 | return MATERIAL_DISPLAY_NAME[tier] ?? tier; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @typedef {object} EquipmentStatSection |
| 76 | * @property {string} [id] |
| 77 | * @property {string} [label] |
| 78 | * @property {ReadonlyArray<string>} tierOrder |
| 79 | * @property {(tier: string) => string} tierDisplayName |
| 80 | * @property {ReadonlyArray<object>} rows |
| 81 | */ |
| 82 | |
| 83 | /** |
| 84 | * @typedef {object} EquipmentStatCategory |
| 85 | * @property {string} id |
| 86 | * @property {string} label |
| 87 | * @property {ReadonlyArray<string>} stats |
| 88 | * @property {ReadonlyArray<EquipmentStatSection>} sections |
| 89 | */ |
| 90 | |
| 91 | /** @type {ReadonlyArray<EquipmentStatCategory>} */ |
| 92 | export const EQUIPMENT_STAT_CATEGORIES = Object.freeze([ |
| 93 | Object.freeze({ |
| 94 | id: EquipmentStatCategoryId.metalArmour, |
| 95 | label: "Metal armour", |
| 96 | stats: Object.freeze([ |
| 97 | SkillId.defence, |
| 98 | SkillId.strength, |
| 99 | SkillId.agility, |
| 100 | SkillId.magic, |
| 101 | SkillId.archery |
| 102 | ]), |
| 103 | sections: Object.freeze([ |
| 104 | Object.freeze({ |
| 105 | id: "metal", |
| 106 | tierOrder: MATERIAL_TIER_ORDER, |
| 107 | tierDisplayName: metalDisplayName, |
| 108 | rows: Object.freeze(ARMOUR_CRAFT_PIECES.map((piece) => Object.freeze({ |
| 109 | pieceKey: piece.suffix === "full-helm" ? "fullHelm" |
| 110 | : piece.suffix === "horned-helm" ? "hornedHelm" |
| 111 | : piece.suffix, |
| 112 | label: piece.label.charAt(0).toUpperCase() + piece.label.slice(1), |
| 113 | defaultBonusExtra: piece.fullHelm ? 1 : 0, |
| 114 | defaultBonusByStat: Object.freeze({ |
| 115 | [SkillId.defence]: "tier", |
| 116 | [SkillId.strength]: "tierQuarterCeil", |
| 117 | [SkillId.agility]: "tierQuarterCeil", |
| 118 | [SkillId.magic]: "negativeRank", |
| 119 | [SkillId.archery]: "negativeRank" |
| 120 | }), |
| 121 | resolveItemId: (tier) => armourItemIdForTier(tier, piece.idKey) |
| 122 | }))) |
| 123 | }) |
| 124 | ]) |
| 125 | }), |
| 126 | Object.freeze({ |
| 127 | id: EquipmentStatCategoryId.leatherArmour, |
| 128 | label: "Leather armour", |
| 129 | stats: Object.freeze([SkillId.agility, SkillId.defence]), |
| 130 | sections: Object.freeze([ |
| 131 | Object.freeze({ |
| 132 | id: "leather", |
| 133 | tierOrder: LEATHER_TIER_ORDER, |
| 134 | tierDisplayName: (tier) => LEATHER_DISPLAY_NAME[tier] ?? tier, |
| 135 | rows: Object.freeze([ |
| 136 | Object.freeze({ |
| 137 | pieceKey: "shoes", |
| 138 | label: "Leather shoes", |
| 139 | defaultBonusByStat: Object.freeze({ |
| 140 | [SkillId.agility]: "tier", |
| 141 | [SkillId.defence]: "tierHalf" |
| 142 | }), |
| 143 | resolveItemId: (tier) => leatherShoesItemId(tier) |
| 144 | }), |
| 145 | ...LEATHER_ARMOUR_PIECES.map((piece) => Object.freeze({ |
| 146 | pieceKey: piece.pieceKey, |
| 147 | label: piece.label.charAt(0).toUpperCase() + piece.label.slice(1), |
| 148 | defaultBonusByStat: Object.freeze({ |
| 149 | [SkillId.agility]: "tier", |
| 150 | [SkillId.defence]: "tierHalf" |
| 151 | }), |
| 152 | resolveItemId: (tier) => leatherArmourItemIdForTier(tier, piece.idKey) |
| 153 | })) |
| 154 | ]) |
| 155 | }) |
| 156 | ]) |
| 157 | }), |
| 158 | Object.freeze({ |
| 159 | id: EquipmentStatCategoryId.meleeWeapons, |
| 160 | label: "Melee weapons", |
| 161 | stats: Object.freeze([SkillId.strength, SkillId.swordfighting]), |
| 162 | sections: Object.freeze([ |
| 163 | Object.freeze({ |
| 164 | id: "wood", |
| 165 | label: "Clubs", |
| 166 | stats: Object.freeze([SkillId.strength, SkillId.clubfighting]), |
| 167 | tierOrder: WOOD_TIER_ORDER, |
| 168 | tierDisplayName: woodDisplayName, |
| 169 | rows: Object.freeze([ |
| 170 | Object.freeze({ |
| 171 | pieceKey: "club", |
| 172 | label: "Stick / Quarterstaff", |
| 173 | /** Strength curve only — cheap; no weapon-skill bonus. */ |
| 174 | defaultBonusByStat: Object.freeze({ [SkillId.strength]: "tier" }), |
| 175 | resolveItemId: (tier) => (tier === WoodTier.pine |
| 176 | ? ItemId.stick |
| 177 | : quarterstaffItemIdForWood(tier)) |
| 178 | }) |
| 179 | ]) |
| 180 | }), |
| 181 | Object.freeze({ |
| 182 | id: "swords", |
| 183 | label: "Swords", |
| 184 | stats: Object.freeze([SkillId.strength, SkillId.swordfighting]), |
| 185 | tierOrder: MATERIAL_TIER_ORDER, |
| 186 | tierDisplayName: metalDisplayName, |
| 187 | rows: Object.freeze([ |
| 188 | Object.freeze({ |
| 189 | pieceKey: "sword", |
| 190 | label: "Sword", |
| 191 | /** Normal strength + swordfighting equal to tier rank (1…7). */ |
| 192 | defaultBonusByStat: Object.freeze({ |
| 193 | [SkillId.strength]: "tier", |
| 194 | [SkillId.swordfighting]: "rank" |
| 195 | }), |
| 196 | resolveItemId: (tier) => swordItemIdForMetal(tier) |
| 197 | }) |
| 198 | ]) |
| 199 | }), |
| 200 | Object.freeze({ |
| 201 | id: "warAxes", |
| 202 | label: "War axes", |
| 203 | stats: Object.freeze([SkillId.strength, SkillId.axefighting]), |
| 204 | tierOrder: MATERIAL_TIER_ORDER, |
| 205 | tierDisplayName: metalDisplayName, |
| 206 | rows: Object.freeze([ |
| 207 | Object.freeze({ |
| 208 | pieceKey: "warAxe", |
| 209 | label: "War axe", |
| 210 | /** Normal strength curve +1 at every tier. */ |
| 211 | defaultBonusByStat: Object.freeze({ [SkillId.strength]: "tier" }), |
| 212 | defaultBonusExtra: 1, |
| 213 | resolveItemId: (tier) => warAxeItemIdForMetal(tier) |
| 214 | }) |
| 215 | ]) |
| 216 | }) |
| 217 | ]) |
| 218 | }), |
| 219 | Object.freeze({ |
| 220 | id: EquipmentStatCategoryId.rangedWeapons, |
| 221 | label: "Ranged weapons", |
| 222 | stats: Object.freeze([SkillId.dexterity]), |
| 223 | sections: Object.freeze([ |
| 224 | Object.freeze({ |
| 225 | id: "bows", |
| 226 | label: "Bows", |
| 227 | stats: Object.freeze([SkillId.archery]), |
| 228 | tierOrder: WOOD_TIER_ORDER, |
| 229 | tierDisplayName: woodDisplayName, |
| 230 | rows: Object.freeze([ |
| 231 | Object.freeze({ |
| 232 | pieceKey: "bow", |
| 233 | label: "Bow", |
| 234 | resolveItemId: (tier) => bowItemId(tier) |
| 235 | }) |
| 236 | ]) |
| 237 | }), |
| 238 | Object.freeze({ |
| 239 | id: "arrows", |
| 240 | label: "Arrows", |
| 241 | stats: Object.freeze([SkillId.dexterity]), |
| 242 | tierOrder: MATERIAL_TIER_ORDER, |
| 243 | tierDisplayName: metalDisplayName, |
| 244 | rows: Object.freeze([ |
| 245 | Object.freeze({ |
| 246 | pieceKey: "arrow", |
| 247 | label: "Arrow", |
| 248 | resolveItemId: (tier) => arrowItemId(tier) |
| 249 | }) |
| 250 | ]) |
| 251 | }) |
| 252 | ]) |
| 253 | }), |
| 254 | Object.freeze({ |
| 255 | id: EquipmentStatCategoryId.magicStaves, |
| 256 | label: "Magic staves", |
| 257 | stats: Object.freeze([SkillId.magic]), |
| 258 | sections: Object.freeze([ |
| 259 | Object.freeze({ |
| 260 | id: "wood", |
| 261 | tierOrder: WOOD_TIER_ORDER, |
| 262 | tierDisplayName: woodDisplayName, |
| 263 | rows: Object.freeze([ |
| 264 | Object.freeze({ |
| 265 | pieceKey: "staff", |
| 266 | label: "Staff", |
| 267 | resolveItemId: (tier) => staffItemIdForWood(tier) |
| 268 | }) |
| 269 | ]) |
| 270 | }) |
| 271 | ]) |
| 272 | }), |
| 273 | Object.freeze({ |
| 274 | id: EquipmentStatCategoryId.wizardArmour, |
| 275 | label: "Wizard armour", |
| 276 | stats: Object.freeze([SkillId.magic]), |
| 277 | sections: Object.freeze([ |
| 278 | Object.freeze({ |
| 279 | id: "cloth", |
| 280 | tierOrder: CLOTH_TIER_ORDER, |
| 281 | tierDisplayName: (tier) => CLOTH_DISPLAY_NAME[tier] ?? tier, |
| 282 | rows: Object.freeze([ |
| 283 | Object.freeze({ |
| 284 | pieceKey: "hat", |
| 285 | label: "Hat", |
| 286 | defaultBonusByStat: Object.freeze({ [SkillId.magic]: "tier" }), |
| 287 | resolveItemId: (tier) => wizardHatItemId(tier) |
| 288 | }), |
| 289 | Object.freeze({ |
| 290 | pieceKey: "robetop", |
| 291 | label: "Robe top", |
| 292 | defaultBonusByStat: Object.freeze({ [SkillId.magic]: "tier" }), |
| 293 | resolveItemId: (tier) => wizardRobeTopItemId(tier) |
| 294 | }), |
| 295 | Object.freeze({ |
| 296 | pieceKey: "robebottoms", |
| 297 | label: "Robe bottoms", |
| 298 | defaultBonusByStat: Object.freeze({ [SkillId.magic]: "tier" }), |
| 299 | resolveItemId: (tier) => wizardRobeBottomsItemId(tier) |
| 300 | }), |
| 301 | Object.freeze({ |
| 302 | pieceKey: "shoes", |
| 303 | label: "Shoes", |
| 304 | defaultBonusByStat: Object.freeze({ [SkillId.magic]: "tier" }), |
| 305 | resolveItemId: (tier) => wizardShoesItemId(tier) |
| 306 | }) |
| 307 | ]) |
| 308 | }) |
| 309 | ]) |
| 310 | }), |
| 311 | Object.freeze({ |
| 312 | id: EquipmentStatCategoryId.clothing, |
| 313 | label: "Clothing", |
| 314 | stats: Object.freeze([SkillId.defence]), |
| 315 | sections: Object.freeze([ |
| 316 | Object.freeze({ |
| 317 | id: "sweaters", |
| 318 | label: "Sweaters", |
| 319 | tierOrder: CLOTH_TIER_ORDER, |
| 320 | tierDisplayName: (tier) => CLOTH_DISPLAY_NAME[tier] ?? tier, |
| 321 | rows: Object.freeze([ |
| 322 | Object.freeze({ |
| 323 | pieceKey: "sweater", |
| 324 | label: "Sweater", |
| 325 | defaultBonus: 0, |
| 326 | resolveItemId: (tier) => sweaterItemId(tier) |
| 327 | }) |
| 328 | ]) |
| 329 | }), |
| 330 | Object.freeze({ |
| 331 | id: "shirts", |
| 332 | label: "Shirts", |
| 333 | tierOrder: CLOTH_TIER_ORDER, |
| 334 | tierDisplayName: (tier) => CLOTH_DISPLAY_NAME[tier] ?? tier, |
| 335 | rows: Object.freeze([ |
| 336 | Object.freeze({ |
| 337 | pieceKey: "shirt", |
| 338 | label: "Shirt", |
| 339 | defaultBonus: 0, |
| 340 | resolveItemId: (tier) => shirtItemId(tier) |
| 341 | }) |
| 342 | ]) |
| 343 | }), |
| 344 | Object.freeze({ |
| 345 | id: "shorts", |
| 346 | label: "Shorts", |
| 347 | tierOrder: CLOTH_TIER_ORDER, |
| 348 | tierDisplayName: (tier) => CLOTH_DISPLAY_NAME[tier] ?? tier, |
| 349 | rows: Object.freeze([ |
| 350 | Object.freeze({ |
| 351 | pieceKey: "shorts", |
| 352 | label: "Shorts", |
| 353 | defaultBonus: 0, |
| 354 | resolveItemId: (tier) => shortsItemId(tier) |
| 355 | }) |
| 356 | ]) |
| 357 | }), |
| 358 | Object.freeze({ |
| 359 | id: "skirts", |
| 360 | label: "Skirts", |
| 361 | tierOrder: CLOTH_TIER_ORDER, |
| 362 | tierDisplayName: (tier) => CLOTH_DISPLAY_NAME[tier] ?? tier, |
| 363 | rows: Object.freeze([ |
| 364 | Object.freeze({ |
| 365 | pieceKey: "skirt", |
| 366 | label: "Skirt", |
| 367 | defaultBonus: 0, |
| 368 | resolveItemId: (tier) => skirtItemId(tier) |
| 369 | }) |
| 370 | ]) |
| 371 | }), |
| 372 | Object.freeze({ |
| 373 | id: "trousers", |
| 374 | label: "Trousers", |
| 375 | tierOrder: CLOTH_TIER_ORDER, |
| 376 | tierDisplayName: (tier) => CLOTH_DISPLAY_NAME[tier] ?? tier, |
| 377 | rows: Object.freeze([ |
| 378 | Object.freeze({ |
| 379 | pieceKey: "trousers", |
| 380 | label: "Trousers", |
| 381 | defaultBonus: 0, |
| 382 | resolveItemId: (tier) => trousersItemId(tier) |
| 383 | }) |
| 384 | ]) |
| 385 | }) |
| 386 | ]) |
| 387 | }) |
| 388 | ]); |
| 389 | |
| 390 | export function equipmentStatCategory (categoryId) |
| 391 | { |
| 392 | return EQUIPMENT_STAT_CATEGORIES.find((entry) => entry.id === categoryId) ?? null; |
| 393 | } |
| 394 | |
| 395 | /** Stats columns for a section (section override, else category). */ |
| 396 | export function equipmentStatSectionStats (category, section) |
| 397 | { |
| 398 | return section?.stats ?? category?.stats ?? []; |
| 399 | } |
| 400 | |
| 401 | /** Flat list of all piece rows with category + section context. */ |
| 402 | export function listEquipmentStatRows () |
| 403 | { |
| 404 | const out = []; |
| 405 | for (const category of EQUIPMENT_STAT_CATEGORIES) |
| 406 | { |
| 407 | for (const section of category.sections) |
| 408 | { |
| 409 | const stats = equipmentStatSectionStats(category, section); |
| 410 | for (const row of section.rows) |
| 411 | { |
| 412 | out.push({ |
| 413 | categoryId: category.id, |
| 414 | stats, |
| 415 | section, |
| 416 | row |
| 417 | }); |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | return out; |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Map itemId → { categoryId, pieceKey, stats, singleton, section }. |
| 426 | * Built lazily so item defs are fully loaded. |
| 427 | */ |
| 428 | let _itemLookup = null; |
| 429 | |
| 430 | export function equipmentStatLookupForItem (itemId) |
| 431 | { |
| 432 | if (!_itemLookup) |
| 433 | { _itemLookup = buildItemLookup(); } |
| 434 | return _itemLookup.get(itemId | 0) ?? null; |
| 435 | } |
| 436 | |
| 437 | function buildItemLookup () |
| 438 | { |
| 439 | const map = new Map(); |
| 440 | for (const entry of listEquipmentStatRows()) |
| 441 | { |
| 442 | const { categoryId, stats, section, row } = entry; |
| 443 | for (const tier of section.tierOrder) |
| 444 | { |
| 445 | const itemId = row.resolveItemId?.(tier) ?? (row.singleton ? row.itemId : null); |
| 446 | if (itemId == null) |
| 447 | { continue; } |
| 448 | map.set(itemId | 0, Object.freeze({ |
| 449 | categoryId, |
| 450 | pieceKey: row.pieceKey, |
| 451 | stats, |
| 452 | singleton: !!row.singleton, |
| 453 | tierKey: row.singleton ? SINGLETON_TIER_KEY : tier |
| 454 | })); |
| 455 | } |
| 456 | } |
| 457 | return map; |
| 458 | } |
| 459 | |
| 460 | /** Reset cached lookup (tests). */ |
| 461 | export function clearEquipmentStatLookupCache () |
| 462 | { |
| 463 | _itemLookup = null; |
| 464 | } |
| 465 | |
| 466 | function emptyStatBag (stats) |
| 467 | { |
| 468 | const bag = {}; |
| 469 | for (const skillId of stats) |
| 470 | { bag[skillId] = 0; } |
| 471 | return bag; |
| 472 | } |
| 473 | |
| 474 | function resolveDefaultBonusMode (mode, tier, row) |
| 475 | { |
| 476 | const key = tier === SINGLETON_TIER_KEY ? null : tier; |
| 477 | const extra = Number(row.defaultBonusExtra) || 0; |
| 478 | if (mode === "rank") |
| 479 | { return Math.max(0, Math.floor(tierRank(key) + extra)); } |
| 480 | if (mode === "negativeRank") |
| 481 | { return -Math.max(0, Math.floor(tierRank(key))); } |
| 482 | if (mode === "tierHalf") |
| 483 | { |
| 484 | const base = row.defaultBonus != null |
| 485 | ? Number(row.defaultBonus) || 0 |
| 486 | : tierStatBonus(key); |
| 487 | return Math.max(0, Math.floor(base / 2) + extra); |
| 488 | } |
| 489 | if (mode === "tierQuarterCeil") |
| 490 | { |
| 491 | const base = row.defaultBonus != null |
| 492 | ? Number(row.defaultBonus) || 0 |
| 493 | : tierStatBonus(key); |
| 494 | return Math.max(0, Math.ceil(base / 4)); |
| 495 | } |
| 496 | if (mode === "tier" || mode == null) |
| 497 | { |
| 498 | const base = row.defaultBonus != null |
| 499 | ? Number(row.defaultBonus) || 0 |
| 500 | : tierStatBonus(key); |
| 501 | return Math.max(0, Math.floor(base + extra)); |
| 502 | } |
| 503 | const n = Math.floor(Number(mode)); |
| 504 | return Number.isFinite(n) ? Math.max(0, n) : 0; |
| 505 | } |
| 506 | |
| 507 | function defaultCellForRow (row, stats, tier) |
| 508 | { |
| 509 | const cell = emptyStatBag(stats); |
| 510 | const byStat = row.defaultBonusByStat; |
| 511 | if (byStat && typeof byStat === "object") |
| 512 | { |
| 513 | for (const [skillId, mode] of Object.entries(byStat)) |
| 514 | { |
| 515 | if (!Object.prototype.hasOwnProperty.call(cell, skillId)) |
| 516 | { continue; } |
| 517 | cell[skillId] = resolveDefaultBonusMode(mode, tier, row); |
| 518 | } |
| 519 | return cell; |
| 520 | } |
| 521 | const value = resolveDefaultBonusMode("tier", tier, row); |
| 522 | if (row.bonusStat) |
| 523 | { |
| 524 | if (Object.prototype.hasOwnProperty.call(cell, row.bonusStat)) |
| 525 | { cell[row.bonusStat] = value; } |
| 526 | return cell; |
| 527 | } |
| 528 | for (const skillId of stats) |
| 529 | { cell[skillId] = value; } |
| 530 | return cell; |
| 531 | } |
| 532 | |
| 533 | /** Seed defaults from current tier curve (full helm +1, cosmetics 0). */ |
| 534 | export function defaultEquipmentStatBonuses () |
| 535 | { |
| 536 | const out = {}; |
| 537 | for (const category of EQUIPMENT_STAT_CATEGORIES) |
| 538 | { |
| 539 | const cat = {}; |
| 540 | for (const section of category.sections) |
| 541 | { |
| 542 | const stats = equipmentStatSectionStats(category, section); |
| 543 | for (const row of section.rows) |
| 544 | { |
| 545 | const byTier = {}; |
| 546 | for (const tier of section.tierOrder) |
| 547 | { |
| 548 | const itemId = row.resolveItemId?.(tier) ?? (row.singleton ? row.itemId : null); |
| 549 | if (itemId == null && !row.singleton) |
| 550 | { continue; } |
| 551 | byTier[tier] = defaultCellForRow(row, stats, tier); |
| 552 | } |
| 553 | cat[row.pieceKey] = byTier; |
| 554 | } |
| 555 | } |
| 556 | out[category.id] = cat; |
| 557 | } |
| 558 | return out; |
| 559 | } |
| 560 | |
| 561 | function coerceBonusInt (raw) |
| 562 | { |
| 563 | const n = Math.floor(Number(raw)); |
| 564 | if (!Number.isFinite(n)) |
| 565 | { return 0; } |
| 566 | return Math.max(-99, Math.min(99, n)); |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Merge authored blob with defaults; drop unknown keys; fill missing cells. |
| 571 | * @param {object|null|undefined} raw |
| 572 | */ |
| 573 | export function normalizeEquipmentStatBonuses (raw) |
| 574 | { |
| 575 | const defaults = defaultEquipmentStatBonuses(); |
| 576 | const src = raw && typeof raw === "object" ? raw : {}; |
| 577 | const out = {}; |
| 578 | for (const category of EQUIPMENT_STAT_CATEGORIES) |
| 579 | { |
| 580 | const srcCat = src[category.id] && typeof src[category.id] === "object" |
| 581 | ? src[category.id] |
| 582 | : {}; |
| 583 | const defCat = defaults[category.id] ?? {}; |
| 584 | const cat = {}; |
| 585 | for (const section of category.sections) |
| 586 | { |
| 587 | const stats = equipmentStatSectionStats(category, section); |
| 588 | for (const row of section.rows) |
| 589 | { |
| 590 | const srcPiece = srcCat[row.pieceKey] && typeof srcCat[row.pieceKey] === "object" |
| 591 | ? srcCat[row.pieceKey] |
| 592 | : {}; |
| 593 | const defPiece = defCat[row.pieceKey] ?? {}; |
| 594 | const byTier = {}; |
| 595 | for (const tier of section.tierOrder) |
| 596 | { |
| 597 | const itemId = row.resolveItemId?.(tier) ?? (row.singleton ? row.itemId : null); |
| 598 | if (itemId == null && !row.singleton) |
| 599 | { continue; } |
| 600 | const srcCell = srcPiece[tier] && typeof srcPiece[tier] === "object" |
| 601 | ? srcPiece[tier] |
| 602 | : null; |
| 603 | const defCell = defPiece[tier] ?? defaultCellForRow(row, stats, tier); |
| 604 | const cell = {}; |
| 605 | for (const skillId of stats) |
| 606 | { |
| 607 | if (srcCell && Object.prototype.hasOwnProperty.call(srcCell, skillId)) |
| 608 | { cell[skillId] = coerceBonusInt(srcCell[skillId]); } |
| 609 | else if ( |
| 610 | skillId === SkillId.archery |
| 611 | && row.pieceKey === "bow" |
| 612 | && srcCell |
| 613 | && Object.prototype.hasOwnProperty.call(srcCell, SkillId.dexterity) |
| 614 | ) |
| 615 | { |
| 616 | // Legacy: bows stored dexterity before archery column split. |
| 617 | cell[skillId] = coerceBonusInt(srcCell[SkillId.dexterity]); |
| 618 | } |
| 619 | else |
| 620 | { cell[skillId] = coerceBonusInt(defCell[skillId]); } |
| 621 | } |
| 622 | byTier[tier] = cell; |
| 623 | } |
| 624 | cat[row.pieceKey] = byTier; |
| 625 | } |
| 626 | } |
| 627 | out[category.id] = cat; |
| 628 | } |
| 629 | return out; |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * Resolve per-skill bonuses for one equipped item def. |
| 634 | * @returns {Record<string, number>|null} |
| 635 | */ |
| 636 | export function bonusForEquippedItem (definition, settingsBonuses = null) |
| 637 | { |
| 638 | const equipment = definition?.equipment; |
| 639 | if (!equipment?.kind || !BONUS_EQUIPMENT_KINDS.has(equipment.kind)) |
| 640 | { return null; } |
| 641 | const itemId = definition.itemId; |
| 642 | const lookup = equipmentStatLookupForItem(itemId); |
| 643 | if (!lookup) |
| 644 | { return null; } |
| 645 | const table = settingsBonuses && typeof settingsBonuses === "object" |
| 646 | ? settingsBonuses |
| 647 | : defaultEquipmentStatBonuses(); |
| 648 | const piece = table[lookup.categoryId]?.[lookup.pieceKey]; |
| 649 | if (!piece) |
| 650 | { return emptyStatBag(lookup.stats); } |
| 651 | const tierKey = lookup.singleton |
| 652 | ? SINGLETON_TIER_KEY |
| 653 | : (equipment.tier ?? definition.tier ?? lookup.tierKey); |
| 654 | const cell = piece[tierKey] ?? piece[lookup.tierKey]; |
| 655 | if (!cell) |
| 656 | { return emptyStatBag(lookup.stats); } |
| 657 | const out = {}; |
| 658 | for (const skillId of lookup.stats) |
| 659 | { out[skillId] = coerceBonusInt(cell[skillId]); } |
| 660 | return out; |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * Changelog bullets for equipmentStatBonuses edits. |
| 665 | * @param {(itemId: number) => string|null} [displayNameFor] |
| 666 | */ |
| 667 | export function diffEquipmentStatBonuses (previous, next, displayNameFor = null) |
| 668 | { |
| 669 | const prev = normalizeEquipmentStatBonuses(previous); |
| 670 | const upcoming = normalizeEquipmentStatBonuses(next); |
| 671 | const bullets = []; |
| 672 | for (const category of EQUIPMENT_STAT_CATEGORIES) |
| 673 | { |
| 674 | for (const section of category.sections) |
| 675 | { |
| 676 | const stats = equipmentStatSectionStats(category, section); |
| 677 | for (const row of section.rows) |
| 678 | { |
| 679 | for (const tier of section.tierOrder) |
| 680 | { |
| 681 | const from = prev[category.id]?.[row.pieceKey]?.[tier]; |
| 682 | const to = upcoming[category.id]?.[row.pieceKey]?.[tier]; |
| 683 | if (!from && !to) |
| 684 | { continue; } |
| 685 | for (const skillId of stats) |
| 686 | { |
| 687 | const a = coerceBonusInt(from?.[skillId]); |
| 688 | const b = coerceBonusInt(to?.[skillId]); |
| 689 | if (a === b) |
| 690 | { continue; } |
| 691 | const itemId = row.resolveItemId?.(tier) ?? row.itemId; |
| 692 | const name = (itemId != null && displayNameFor?.(itemId)) |
| 693 | || `${row.label} (${section.tierDisplayName(tier)})`; |
| 694 | bullets.push(`${name} ${skillId} bonus changed from ${a} to ${b}.`); |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | return bullets; |
| 701 | } |
| 702 | |
| 703 | export function equipmentStatBonusMapsEqual (a, b) |
| 704 | { |
| 705 | const left = normalizeEquipmentStatBonuses(a); |
| 706 | const right = normalizeEquipmentStatBonuses(b); |
| 707 | return JSON.stringify(left) === JSON.stringify(right); |
| 708 | } |
| 709 | |
| 710 | /** |
| 711 | * Every equippable item with a bonus kind must appear in exactly one category row. |
| 712 | * @returns {{ ok: true } | { ok: false, missing: number[], duplicates: number[] }} |
| 713 | */ |
| 714 | export function assertAllBonusGearCategorized () |
| 715 | { |
| 716 | clearEquipmentStatLookupCache(); |
| 717 | const seen = new Map(); |
| 718 | const duplicates = []; |
| 719 | for (const entry of listEquipmentStatRows()) |
| 720 | { |
| 721 | for (const tier of entry.section.tierOrder) |
| 722 | { |
| 723 | const itemId = entry.row.resolveItemId?.(tier) ?? entry.row.itemId; |
| 724 | if (itemId == null) |
| 725 | { continue; } |
| 726 | const id = itemId | 0; |
| 727 | if (seen.has(id)) |
| 728 | { duplicates.push(id); } |
| 729 | else |
| 730 | { seen.set(id, entry); } |
| 731 | } |
| 732 | } |
| 733 | const missing = []; |
| 734 | for (const def of Object.values(ITEM_DEFINITIONS)) |
| 735 | { |
| 736 | const kind = def?.equipment?.kind; |
| 737 | if (!kind || !BONUS_EQUIPMENT_KINDS.has(kind)) |
| 738 | { continue; } |
| 739 | if (!seen.has(def.itemId | 0)) |
| 740 | { missing.push(def.itemId | 0); } |
| 741 | } |
| 742 | if (missing.length || duplicates.length) |
| 743 | { return { ok: false, missing, duplicates }; } |
| 744 | return { ok: true }; |
| 745 | } |
| 746 | |
| 747 | /** Preview item id for a row at tier (editor icon). */ |
| 748 | export function equipmentStatRowItemId (row, tier) |
| 749 | { |
| 750 | if (row.singleton) |
| 751 | { return row.itemId ?? row.resolveItemId?.(SINGLETON_TIER_KEY) ?? null; } |
| 752 | return row.resolveItemId?.(tier) ?? null; |
| 753 | } |
| 754 | |
| 755 | export function equipmentStatRowDefaultTier (section, row) |
| 756 | { |
| 757 | for (const tier of section.tierOrder) |
| 758 | { |
| 759 | if (equipmentStatRowItemId(row, tier) != null) |
| 760 | { return tier; } |
| 761 | } |
| 762 | return section.tierOrder[0] ?? SINGLETON_TIER_KEY; |
| 763 | } |
| 764 | |
| 765 | /** Display helper — re-export itemDefinition for callers that only import this module. */ |
| 766 | export { itemDefinition }; |