BlueGit

timfa questward

Files

questward / shared / definitions / tierFamilies.js

tierFamilies.js

tierFamilies.js 14.6 KiB Download Raw
1import { ItemCategory } from "./itemCategories.js";
2import {
3 WoodTier,
4 MATERIAL_DISPLAY_NAME,
5 WOOD_TIER_ORDER,
6 MATERIAL_TIER_ORDER,
7 CLOTH_TIER_ORDER,
8 CLOTH_DISPLAY_NAME,
9 woodTierForMetal,
10 clothTierForWood,
11 leatherTierForMetal
12} from "./materialTiers.js";
13import { EquipmentKind } from "./equipmentKinds.js";
14import { AttackType } from "./attackTypes.js";
15import { SkillId } from "./skills.js";
16import { COMBAT_RANGE_TILES } from "../spells/constants.js";
17
18/** Which tier list drives a family (`wood` → pine…elder, `metal` → bronze…dragon). */
19export const TierAxis = Object.freeze({
20 wood: "wood",
21 metal: "metal",
22 cloth: "cloth"
23});
24
25/** Stable family ids — one template per intermediate / gather product shape. */
26export const ItemFamilyId = Object.freeze({
27 log: "log",
28 handle: "handle",
29 ore: "ore",
30 bar: "bar",
31 pickaxeHead: "pickaxeHead",
32 axeHead: "axeHead",
33 pickaxe: "pickaxe",
34 axe: "axe",
35 warAxeHead: "warAxeHead",
36 warAxe: "warAxe",
37 swordBlade: "swordBlade",
38 sword: "sword",
39 plantClippers: "plantClippers",
40 shears: "shears",
41 arrowShaft: "arrowShaft",
42 arrowTip: "arrowTip",
43 arrow: "arrow",
44 unstrungBow: "unstrungBow",
45 bow: "bow",
46 bowstring: "bowstring",
47 quarterstaff: "quarterstaff"
48});
49
50const DEFAULT_STACK = 99;
51
52/**
53 * Family templates: display suffix, sprite, stacking, and tier axis.
54 * Per-tier itemIds stay in woodItems / metalItems / rangedItems; defs are built here.
55 */
56export const ITEM_FAMILIES = Object.freeze({
57 [ItemFamilyId.log]: Object.freeze({
58 familyId: ItemFamilyId.log,
59 axis: TierAxis.wood,
60 label: "log",
61 description: (name) => `A sturdy ${name.toLowerCase()} log.`,
62 category: ItemCategory.ingredient,
63 iconKey: "wood",
64 defaultState: "log",
65 stackable: true,
66 maxStackSize: DEFAULT_STACK,
67 seedValue: (tier) => (tier === WoodTier.pine ? 5 : 2),
68 tintFromTier: true,
69 npcShopAllowed: true
70 }),
71 [ItemFamilyId.handle]: Object.freeze({
72 familyId: ItemFamilyId.handle,
73 axis: TierAxis.wood,
74 label: "handle",
75 description: (name) => `Shaped ${name.toLowerCase()} haft. Combine with a matching toolhead.`,
76 category: ItemCategory.intermediate,
77 iconKey: "wood",
78 defaultState: "handle",
79 stackable: true,
80 maxStackSize: DEFAULT_STACK,
81 seedValue: 3,
82 tintFromTier: true
83 }),
84 [ItemFamilyId.ore]: Object.freeze({
85 familyId: ItemFamilyId.ore,
86 axis: TierAxis.metal,
87 label: "ore",
88 description: (name) => `Mined ${name.toLowerCase()} ore. Smelt at a furnace.`,
89 category: ItemCategory.ingredient,
90 iconKey: "metal",
91 defaultState: "ore",
92 stackable: true,
93 maxStackSize: DEFAULT_STACK,
94 seedValue: 2,
95 tintFromTier: true
96 }),
97 [ItemFamilyId.bar]: Object.freeze({
98 familyId: ItemFamilyId.bar,
99 axis: TierAxis.metal,
100 label: "bar",
101 description: (name) => `Smelted ${name.toLowerCase()}. Smith at an anvil.`,
102 category: ItemCategory.intermediate,
103 iconKey: "metal",
104 defaultState: "bar",
105 stackable: true,
106 maxStackSize: DEFAULT_STACK,
107 seedValue: 4,
108 tintFromTier: true
109 }),
110 [ItemFamilyId.pickaxeHead]: Object.freeze({
111 familyId: ItemFamilyId.pickaxeHead,
112 axis: TierAxis.metal,
113 label: "pickaxe head",
114 description: (name) => `Smithed ${name.toLowerCase()} head. Attach a matching handle at a crafting bench.`,
115 category: ItemCategory.intermediate,
116 iconKey: "pickaxe",
117 defaultState: "pickaxe-head",
118 stackable: true,
119 maxStackSize: DEFAULT_STACK,
120 seedValue: 5,
121 tintFromTier: true
122 }),
123 [ItemFamilyId.axeHead]: Object.freeze({
124 familyId: ItemFamilyId.axeHead,
125 axis: TierAxis.metal,
126 label: "axe head",
127 description: (name) => `Smithed ${name.toLowerCase()} head. Attach a matching handle at a crafting bench.`,
128 category: ItemCategory.intermediate,
129 iconKey: "axe",
130 defaultState: "axe-head",
131 stackable: true,
132 maxStackSize: DEFAULT_STACK,
133 seedValue: 5,
134 tintFromTier: true
135 }),
136 [ItemFamilyId.arrowShaft]: Object.freeze({
137 familyId: ItemFamilyId.arrowShaft,
138 axis: TierAxis.wood,
139 label: "arrow shaft",
140 description: () => "A straight wooden shaft, ready for fletching.",
141 category: ItemCategory.intermediate,
142 iconKey: "arrow",
143 defaultState: "shaft",
144 stackable: true,
145 maxStackSize: DEFAULT_STACK,
146 seedValue: 1,
147 tintFromTier: true
148 }),
149 [ItemFamilyId.arrowTip]: Object.freeze({
150 familyId: ItemFamilyId.arrowTip,
151 axis: TierAxis.metal,
152 label: "arrow tip",
153 description: (name) => `A smithed ${name.toLowerCase()} point for arrows.`,
154 category: ItemCategory.intermediate,
155 iconKey: "arrow",
156 defaultState: "tip",
157 stackable: true,
158 maxStackSize: DEFAULT_STACK,
159 seedValue: 1,
160 tintFromTier: true
161 }),
162 [ItemFamilyId.pickaxe]: Object.freeze({
163 familyId: ItemFamilyId.pickaxe,
164 axis: TierAxis.metal,
165 label: "pickaxe",
166 description: (name) => `A ${name.toLowerCase()} pickaxe for breaking stone and ore.`,
167 category: ItemCategory.tool,
168 iconKey: "pickaxe",
169 defaultState: "pickaxe",
170 stackable: false,
171 maxStackSize: 1,
172 seedValue: 8,
173 dualTintFromTier: true,
174 woodTierFromMetal: true,
175 equipmentKind: EquipmentKind.pickaxeTool
176 }),
177 [ItemFamilyId.axe]: Object.freeze({
178 familyId: ItemFamilyId.axe,
179 axis: TierAxis.metal,
180 label: "axe",
181 description: (name) => `A ${name.toLowerCase()} axe for felling trees.`,
182 category: ItemCategory.tool,
183 iconKey: "axe",
184 defaultState: "axe",
185 stackable: false,
186 maxStackSize: 1,
187 seedValue: 8,
188 dualTintFromTier: true,
189 woodTierFromMetal: true,
190 equipmentKind: EquipmentKind.axeTool
191 }),
192 [ItemFamilyId.warAxeHead]: Object.freeze({
193 familyId: ItemFamilyId.warAxeHead,
194 axis: TierAxis.metal,
195 label: "war axe head",
196 description: (name) => `A heavy ${name.toLowerCase()} axe head, meant for battle rather than timber.`,
197 category: ItemCategory.intermediate,
198 iconKey: "waraxe",
199 defaultState: "waraxe-head",
200 stackable: true,
201 maxStackSize: DEFAULT_STACK,
202 seedValue: 8,
203 tintFromTier: true
204 }),
205 [ItemFamilyId.warAxe]: Object.freeze({
206 familyId: ItemFamilyId.warAxe,
207 axis: TierAxis.metal,
208 label: "war axe",
209 description: (name) => `A ${name.toLowerCase()} war axe, hafted for the press of combat.`,
210 category: ItemCategory.tool,
211 iconKey: "waraxe",
212 defaultState: "waraxe",
213 stackable: false,
214 maxStackSize: 1,
215 seedValue: 12,
216 /** Red = wood haft, blue = metal head. */
217 dualTintFromTier: true,
218 woodTierFromMetal: true,
219 equipmentKind: EquipmentKind.metalWeapon,
220 equipmentAttack: Object.freeze({
221 mode: "melee",
222 rangeTiles: 1,
223 hitSkillId: SkillId.axefighting,
224 damageSkillId: SkillId.strength,
225 attackType: AttackType.chopping,
226 cooldownMs: 800
227 })
228 }),
229 [ItemFamilyId.plantClippers]: Object.freeze({
230 familyId: ItemFamilyId.plantClippers,
231 axis: TierAxis.metal,
232 label: "plant clippers",
233 description: () => "Sharp clippers for cutting herbs, flax, and blooms.",
234 category: ItemCategory.tool,
235 iconKey: "plant-clippers",
236 defaultState: "plant-clippers",
237 stackable: false,
238 maxStackSize: 1,
239 seedValue: 10,
240 /** Red = metal blades, blue = leather grips. */
241 dualTintFromTier: true,
242 leatherTierFromMetal: true,
243 npcShopAllowed: true,
244 equipmentKind: EquipmentKind.plantClippers
245 }),
246 [ItemFamilyId.shears]: Object.freeze({
247 familyId: ItemFamilyId.shears,
248 axis: TierAxis.metal,
249 label: "shears",
250 description: () => "All-metal shears for clipping fleece from animals.",
251 category: ItemCategory.tool,
252 iconKey: "shears",
253 defaultState: "shears",
254 stackable: false,
255 maxStackSize: 1,
256 seedValue: 10,
257 tintFromTier: true,
258 npcShopAllowed: true,
259 equipmentKind: EquipmentKind.shears,
260 equipmentTintFromTier: true
261 }),
262 [ItemFamilyId.arrow]: Object.freeze({
263 familyId: ItemFamilyId.arrow,
264 axis: TierAxis.metal,
265 label: "arrow",
266 description: () => "A fletched arrow with matching shaft and head.",
267 category: ItemCategory.finished,
268 iconKey: "arrow",
269 defaultState: "full-arrow",
270 stackable: true,
271 maxStackSize: 999,
272 seedValue: 2,
273 dualTintFromTier: true,
274 woodTierFromMetal: true,
275 equipmentKind: EquipmentKind.arrowAmmo,
276 equipmentSlot: "offhand",
277 hideEquipSprite: true
278 }),
279 [ItemFamilyId.unstrungBow]: Object.freeze({
280 familyId: ItemFamilyId.unstrungBow,
281 axis: TierAxis.wood,
282 label: "unstrung bow",
283 description: () => "A carved bow frame awaiting a string.",
284 category: ItemCategory.intermediate,
285 iconKey: "bow",
286 defaultState: "unstrung-bow",
287 stackable: false,
288 maxStackSize: 1,
289 seedValue: 6,
290 /** Red = wood; blue unused (same dual shader as bows/arrows). */
291 dualTintFromTier: true
292 }),
293 [ItemFamilyId.bow]: Object.freeze({
294 familyId: ItemFamilyId.bow,
295 axis: TierAxis.wood,
296 label: "bow",
297 description: (name) => `A ${name.toLowerCase()} bow strung with matching yarn.`,
298 category: ItemCategory.finished,
299 iconKey: "bow",
300 defaultState: "bow",
301 stackable: false,
302 maxStackSize: 1,
303 seedValue: 12,
304 /** Red = wood, blue = string (cloth tier). */
305 dualTintFromTier: true,
306 clothTierFromWood: true,
307 equipmentKind: EquipmentKind.bow,
308 equipmentAttack: Object.freeze({
309 mode: "ranged",
310 rangeTiles: COMBAT_RANGE_TILES,
311 hitSkillId: SkillId.archery,
312 damageSkillId: SkillId.dexterity,
313 attackType: AttackType.piercing,
314 cooldownMs: 1200
315 })
316 }),
317 [ItemFamilyId.bowstring]: Object.freeze({
318 familyId: ItemFamilyId.bowstring,
319 axis: TierAxis.cloth,
320 label: "bowstring",
321 description: () => "Twisted yarn spun tight for a bow.",
322 category: ItemCategory.intermediate,
323 iconKey: "bow",
324 defaultState: "bowstring",
325 stackable: true,
326 maxStackSize: DEFAULT_STACK,
327 seedValue: 2,
328 tintFromTier: true
329 }),
330 [ItemFamilyId.quarterstaff]: Object.freeze({
331 familyId: ItemFamilyId.quarterstaff,
332 axis: TierAxis.wood,
333 label: "quarterstaff",
334 description: (name) => `A ${name.toLowerCase()} quarterstaff, long enough to keep foes at a distance.`,
335 category: ItemCategory.finished,
336 iconKey: "quarterstaff",
337 defaultState: "quarterstaff",
338 stackable: false,
339 maxStackSize: 1,
340 seedValue: 8,
341 tintFromTier: true,
342 equipmentKind: EquipmentKind.clubWeapon,
343 equipmentSlot: "weapon",
344 equipmentTintFromTier: true,
345 equipmentAttack: Object.freeze({
346 mode: "melee",
347 rangeTiles: 1,
348 hitSkillId: SkillId.clubfighting,
349 damageSkillId: SkillId.strength,
350 attackType: AttackType.crushing,
351 cooldownMs: 700
352 })
353 }),
354 [ItemFamilyId.swordBlade]: Object.freeze({
355 familyId: ItemFamilyId.swordBlade,
356 axis: TierAxis.metal,
357 label: "sword blade",
358 description: (name) => `A smithed ${name.toLowerCase()} blade. Haft it with leather at a crafting bench.`,
359 category: ItemCategory.intermediate,
360 iconKey: "sword",
361 defaultState: "sword-blade",
362 stackable: true,
363 maxStackSize: DEFAULT_STACK,
364 seedValue: 6,
365 tintFromTier: true
366 }),
367 [ItemFamilyId.sword]: Object.freeze({
368 familyId: ItemFamilyId.sword,
369 axis: TierAxis.metal,
370 label: "sword",
371 description: (name) => `A ${name.toLowerCase()} sword, leather-wrapped for a sure grip.`,
372 category: ItemCategory.tool,
373 iconKey: "sword",
374 defaultState: "sword",
375 stackable: false,
376 maxStackSize: 1,
377 seedValue: 10,
378 /** Red = metal, blue = leather wrap (wood haft hidden under the wrap). */
379 dualTintFromTier: true,
380 leatherTierFromMetal: true,
381 equipmentKind: EquipmentKind.metalWeapon,
382 equipmentAttack: Object.freeze({
383 mode: "melee",
384 rangeTiles: 1,
385 hitSkillId: SkillId.swordfighting,
386 damageSkillId: SkillId.strength,
387 attackType: AttackType.slashing,
388 cooldownMs: 700
389 })
390 })
391});
392
393export function tierOrderForAxis (axis)
394{
395 if (axis === TierAxis.wood)
396 { return WOOD_TIER_ORDER; }
397 if (axis === TierAxis.metal)
398 { return MATERIAL_TIER_ORDER; }
399 if (axis === TierAxis.cloth)
400 { return CLOTH_TIER_ORDER; }
401 return [];
402}
403
404function buildFamilyEquipment (family, tier)
405{
406 if (!family.equipmentKind)
407 { return null; }
408 const equipment = {
409 slot: family.equipmentSlot ?? "weapon",
410 tier,
411 kind: family.equipmentKind
412 };
413 if (family.equipmentTintFromTier)
414 { equipment.tintFromTier = true; }
415 if (family.hideEquipSprite)
416 { equipment.hideEquipSprite = true; }
417 if (family.equipmentAttack)
418 { equipment.attack = family.equipmentAttack; }
419 return Object.freeze(equipment);
420}
421
422export function familyDefinition (familyId)
423{
424 return ITEM_FAMILIES[familyId] ?? null;
425}
426
427/**
428 * Build one tiered item definition from a family template.
429 * @param {string} familyId
430 * @param {string} tier
431 * @param {number} itemId stable numeric id (inventory / saves)
432 * @param {object} [extra] per-tier overrides (key, seedValue, description, …)
433 */
434export function buildTierItemDef (familyId, tier, itemId, extra = {})
435{
436 const family = familyDefinition(familyId);
437 if (!family)
438 { throw new Error(`Unknown item family: ${familyId}`); }
439 const name = family.axis === TierAxis.cloth
440 ? (CLOTH_DISPLAY_NAME[tier] ?? tier)
441 : (MATERIAL_DISPLAY_NAME[tier] ?? tier);
442 const key = extra.key ?? `${tier}-${family.label.replace(/ /g, "-")}`;
443 const seedValue = extra.seedValue ?? (
444 typeof family.seedValue === "function" ? family.seedValue(tier) : family.seedValue
445 );
446 const description = extra.description ?? family.description(name);
447 const equipment = extra.equipment ?? buildFamilyEquipment(family, tier);
448 return Object.freeze({
449 itemId,
450 familyId,
451 key,
452 displayName: extra.displayName ?? `${name} ${family.label}`,
453 description,
454 seedValue,
455 stackable: extra.stackable ?? family.stackable,
456 maxStackSize: extra.maxStackSize ?? family.maxStackSize,
457 uniqueInstance: false,
458 category: extra.category ?? family.category,
459 iconKey: extra.iconKey ?? family.iconKey,
460 defaultState: extra.defaultState ?? family.defaultState,
461 tier,
462 ...(family.tintFromTier ? { tintFromTier: true } : {}),
463 ...(family.npcShopAllowed ? { npcShopAllowed: true } : {}),
464 ...(family.woodTierFromMetal ? { woodTier: woodTierForMetal(tier), dualTintFromTier: true } : {}),
465 ...(family.leatherTierFromMetal
466 ? { leatherTier: leatherTierForMetal(tier), dualTintFromTier: true }
467 : {}),
468 ...(family.dualTintFromTier && !family.woodTierFromMetal && !family.leatherTierFromMetal
469 ? { dualTintFromTier: true }
470 : {}),
471 ...(family.clothTierFromWood ? { clothTier: clothTierForWood(tier) } : {}),
472 ...(equipment ? { equipment } : {})
473 });
474}
475
476/** Build `{ [itemId]: def }` from a tier → itemId map. */
477export function buildFamilyDefinitionMap (familyId, tierToItemId, extraByTier = {})
478{
479 const defs = {};
480 for (const [tier, id] of Object.entries(tierToItemId))
481 {
482 const itemId = id | 0;
483 if (!itemId)
484 { continue; }
485 defs[itemId] = buildTierItemDef(familyId, tier, itemId, extraByTier[tier] ?? {});
486 }
487 return defs;
488}
489
490/** Resolve itemId for a family tier from a lookup table. */
491export function itemIdForFamilyTier (familyTierMap, tier)
492{
493 return familyTierMap[tier] ?? null;
494}
495
496/** Reverse lookup: tier for an item id within a family map, or null. */
497export function tierForFamilyItemId (familyTierMap, itemId)
498{
499 const want = itemId | 0;
500 for (const [tier, id] of Object.entries(familyTierMap))
501 {
502 if ((id | 0) === want)
503 { return tier; }
504 }
505 return null;
506}