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