BlueGit

timfa questward

Files

questward / shared / definitions / equipmentStatCategories.js

equipmentStatCategories.js

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