BlueGit

timfa questward

Files

questward / shared / definitions / equipmentStatCategories.js

equipmentStatCategories.js

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