BlueGit

timfa questward

Files

questward / shared / definitions / equipmentStatCategories.js

equipmentStatCategories.js

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