BlueGit

timfa questward

Files

questward / shared / definitions / equipmentStatCategories.js

equipmentStatCategories.js

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