BlueGit

timfa questward

Files

questward / shared / definitions / equipmentStatCategories.js

equipmentStatCategories.js

lots of things
timfa Tim Falken · pushed by timfa · 2 months ago
07a95f71 History
equipmentStatCategories.js 14.9 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 } 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 (shorts, cloth shirt, …). */
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: "singletons",
221 label: "Other",
222 tierOrder: Object.freeze([SINGLETON_TIER_KEY]),
223 tierDisplayName: () => "—",
224 rows: Object.freeze([
225 Object.freeze({
226 pieceKey: "shorts",
227 label: "Cloth shorts",
228 defaultBonus: 0,
229 singleton: true,
230 itemId: LeatherItemId.clothShorts,
231 resolveItemId: () => LeatherItemId.clothShorts
232 }),
233 Object.freeze({
234 pieceKey: "trousers",
235 label: "Cloth trousers",
236 defaultBonus: 0,
237 singleton: true,
238 itemId: LeatherItemId.clothTrousers,
239 resolveItemId: () => LeatherItemId.clothTrousers
240 }),
241 Object.freeze({
242 pieceKey: "clothShirt",
243 label: "Cloth shirt",
244 defaultBonus: 1,
245 singleton: true,
246 itemId: ItemId.clothShirt,
247 resolveItemId: () => ItemId.clothShirt
248 })
249 ])
250 })
251 ])
252 })
253]);
254
255export function equipmentStatCategory (categoryId)
256{
257 return EQUIPMENT_STAT_CATEGORIES.find((entry) => entry.id === categoryId) ?? null;
258}
259
260/** Flat list of all piece rows with category + section context. */
261export function listEquipmentStatRows ()
262{
263 const out = [];
264 for (const category of EQUIPMENT_STAT_CATEGORIES)
265 {
266 for (const section of category.sections)
267 {
268 for (const row of section.rows)
269 {
270 out.push({
271 categoryId: category.id,
272 stats: category.stats,
273 section,
274 row
275 });
276 }
277 }
278 }
279 return out;
280}
281
282/**
283 * Map itemId → { categoryId, pieceKey, stats, singleton, section }.
284 * Built lazily so item defs are fully loaded.
285 */
286let _itemLookup = null;
287
288export function equipmentStatLookupForItem (itemId)
289{
290 if (!_itemLookup)
291 { _itemLookup = buildItemLookup(); }
292 return _itemLookup.get(itemId | 0) ?? null;
293}
294
295function buildItemLookup ()
296{
297 const map = new Map();
298 for (const entry of listEquipmentStatRows())
299 {
300 const { categoryId, stats, section, row } = entry;
301 for (const tier of section.tierOrder)
302 {
303 const itemId = row.resolveItemId?.(tier) ?? (row.singleton ? row.itemId : null);
304 if (itemId == null)
305 { continue; }
306 map.set(itemId | 0, Object.freeze({
307 categoryId,
308 pieceKey: row.pieceKey,
309 stats,
310 singleton: !!row.singleton,
311 tierKey: row.singleton ? SINGLETON_TIER_KEY : tier
312 }));
313 }
314 }
315 return map;
316}
317
318/** Reset cached lookup (tests). */
319export function clearEquipmentStatLookupCache ()
320{
321 _itemLookup = null;
322}
323
324function emptyStatBag (stats)
325{
326 const bag = {};
327 for (const skillId of stats)
328 { bag[skillId] = 0; }
329 return bag;
330}
331
332function defaultCellForRow (row, stats, tier)
333{
334 const cell = emptyStatBag(stats);
335 const base = row.defaultBonus != null
336 ? Number(row.defaultBonus) || 0
337 : tierStatBonus(tier === SINGLETON_TIER_KEY ? null : tier);
338 const extra = Number(row.defaultBonusExtra) || 0;
339 const value = Math.max(0, Math.floor(base + extra));
340 for (const skillId of stats)
341 { cell[skillId] = value; }
342 return cell;
343}
344
345/** Seed defaults from current tier curve (full helm +1, cosmetics 0). */
346export function defaultEquipmentStatBonuses ()
347{
348 const out = {};
349 for (const category of EQUIPMENT_STAT_CATEGORIES)
350 {
351 const cat = {};
352 for (const section of category.sections)
353 {
354 for (const row of section.rows)
355 {
356 const byTier = {};
357 for (const tier of section.tierOrder)
358 {
359 const itemId = row.resolveItemId?.(tier) ?? (row.singleton ? row.itemId : null);
360 if (itemId == null && !row.singleton)
361 { continue; }
362 byTier[tier] = defaultCellForRow(row, category.stats, tier);
363 }
364 cat[row.pieceKey] = byTier;
365 }
366 }
367 out[category.id] = cat;
368 }
369 return out;
370}
371
372function coerceBonusInt (raw)
373{
374 const n = Math.floor(Number(raw));
375 if (!Number.isFinite(n))
376 { return 0; }
377 return Math.max(0, Math.min(99, n));
378}
379
380/**
381 * Merge authored blob with defaults; drop unknown keys; fill missing cells.
382 * @param {object|null|undefined} raw
383 */
384export function normalizeEquipmentStatBonuses (raw)
385{
386 const defaults = defaultEquipmentStatBonuses();
387 const src = raw && typeof raw === "object" ? raw : {};
388 const out = {};
389 for (const category of EQUIPMENT_STAT_CATEGORIES)
390 {
391 const srcCat = src[category.id] && typeof src[category.id] === "object"
392 ? src[category.id]
393 : {};
394 const defCat = defaults[category.id] ?? {};
395 const cat = {};
396 for (const section of category.sections)
397 {
398 for (const row of section.rows)
399 {
400 const srcPiece = srcCat[row.pieceKey] && typeof srcCat[row.pieceKey] === "object"
401 ? srcCat[row.pieceKey]
402 : {};
403 const defPiece = defCat[row.pieceKey] ?? {};
404 const byTier = {};
405 for (const tier of section.tierOrder)
406 {
407 const itemId = row.resolveItemId?.(tier) ?? (row.singleton ? row.itemId : null);
408 if (itemId == null && !row.singleton)
409 { continue; }
410 const srcCell = srcPiece[tier] && typeof srcPiece[tier] === "object"
411 ? srcPiece[tier]
412 : null;
413 const defCell = defPiece[tier] ?? defaultCellForRow(row, category.stats, tier);
414 const cell = {};
415 for (const skillId of category.stats)
416 {
417 cell[skillId] = srcCell && Object.prototype.hasOwnProperty.call(srcCell, skillId)
418 ? coerceBonusInt(srcCell[skillId])
419 : coerceBonusInt(defCell[skillId]);
420 }
421 byTier[tier] = cell;
422 }
423 cat[row.pieceKey] = byTier;
424 }
425 }
426 out[category.id] = cat;
427 }
428 return out;
429}
430
431/**
432 * Resolve per-skill bonuses for one equipped item def.
433 * @returns {Record<string, number>|null}
434 */
435export function bonusForEquippedItem (definition, settingsBonuses = null)
436{
437 const equipment = definition?.equipment;
438 if (!equipment?.kind || !BONUS_EQUIPMENT_KINDS.has(equipment.kind))
439 { return null; }
440 const itemId = definition.itemId;
441 const lookup = equipmentStatLookupForItem(itemId);
442 if (!lookup)
443 { return null; }
444 const table = settingsBonuses && typeof settingsBonuses === "object"
445 ? settingsBonuses
446 : defaultEquipmentStatBonuses();
447 const piece = table[lookup.categoryId]?.[lookup.pieceKey];
448 if (!piece)
449 { return emptyStatBag(lookup.stats); }
450 const tierKey = lookup.singleton
451 ? SINGLETON_TIER_KEY
452 : (equipment.tier ?? definition.tier ?? lookup.tierKey);
453 const cell = piece[tierKey] ?? piece[lookup.tierKey];
454 if (!cell)
455 { return emptyStatBag(lookup.stats); }
456 const out = {};
457 for (const skillId of lookup.stats)
458 { out[skillId] = coerceBonusInt(cell[skillId]); }
459 return out;
460}
461
462/**
463 * Changelog bullets for equipmentStatBonuses edits.
464 * @param {(itemId: number) => string|null} [displayNameFor]
465 */
466export function diffEquipmentStatBonuses (previous, next, displayNameFor = null)
467{
468 const prev = normalizeEquipmentStatBonuses(previous);
469 const upcoming = normalizeEquipmentStatBonuses(next);
470 const bullets = [];
471 for (const category of EQUIPMENT_STAT_CATEGORIES)
472 {
473 for (const section of category.sections)
474 {
475 for (const row of section.rows)
476 {
477 for (const tier of section.tierOrder)
478 {
479 const from = prev[category.id]?.[row.pieceKey]?.[tier];
480 const to = upcoming[category.id]?.[row.pieceKey]?.[tier];
481 if (!from && !to)
482 { continue; }
483 for (const skillId of category.stats)
484 {
485 const a = coerceBonusInt(from?.[skillId]);
486 const b = coerceBonusInt(to?.[skillId]);
487 if (a === b)
488 { continue; }
489 const itemId = row.resolveItemId?.(tier) ?? row.itemId;
490 const name = (itemId != null && displayNameFor?.(itemId))
491 || `${row.label} (${section.tierDisplayName(tier)})`;
492 bullets.push(`${name} ${skillId} bonus changed from ${a} to ${b}.`);
493 }
494 }
495 }
496 }
497 }
498 return bullets;
499}
500
501export function equipmentStatBonusMapsEqual (a, b)
502{
503 const left = normalizeEquipmentStatBonuses(a);
504 const right = normalizeEquipmentStatBonuses(b);
505 return JSON.stringify(left) === JSON.stringify(right);
506}
507
508/**
509 * Every equippable item with a bonus kind must appear in exactly one category row.
510 * @returns {{ ok: true } | { ok: false, missing: number[], duplicates: number[] }}
511 */
512export function assertAllBonusGearCategorized ()
513{
514 clearEquipmentStatLookupCache();
515 const seen = new Map();
516 const duplicates = [];
517 for (const entry of listEquipmentStatRows())
518 {
519 for (const tier of entry.section.tierOrder)
520 {
521 const itemId = entry.row.resolveItemId?.(tier) ?? entry.row.itemId;
522 if (itemId == null)
523 { continue; }
524 const id = itemId | 0;
525 if (seen.has(id))
526 { duplicates.push(id); }
527 else
528 { seen.set(id, entry); }
529 }
530 }
531 const missing = [];
532 for (const def of Object.values(ITEM_DEFINITIONS))
533 {
534 const kind = def?.equipment?.kind;
535 if (!kind || !BONUS_EQUIPMENT_KINDS.has(kind))
536 { continue; }
537 if (!seen.has(def.itemId | 0))
538 { missing.push(def.itemId | 0); }
539 }
540 if (missing.length || duplicates.length)
541 { return { ok: false, missing, duplicates }; }
542 return { ok: true };
543}
544
545/** Preview item id for a row at tier (editor icon). */
546export function equipmentStatRowItemId (row, tier)
547{
548 if (row.singleton)
549 { return row.itemId ?? row.resolveItemId?.(SINGLETON_TIER_KEY) ?? null; }
550 return row.resolveItemId?.(tier) ?? null;
551}
552
553export function equipmentStatRowDefaultTier (section, row)
554{
555 for (const tier of section.tierOrder)
556 {
557 if (equipmentStatRowItemId(row, tier) != null)
558 { return tier; }
559 }
560 return section.tierOrder[0] ?? SINGLETON_TIER_KEY;
561}
562
563/** Display helper — re-export itemDefinition for callers that only import this module. */
564export { itemDefinition };