BlueGit

timfa questward

Files

questward / shared / definitions / tierFamilies.js

tierFamilies.js

description edit
timfa Tim Falken · pushed by timfa · 2 months ago
0059064d History
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 /** Red = metal blades, blue = leather grips. */
242 dualTintFromTier: true,
243 leatherTierFromMetal: true,
244 npcShopAllowed: true,
245 equipmentKind: EquipmentKind.plantClippers
246 }),
247 [ItemFamilyId.shears]: Object.freeze({
248 familyId: ItemFamilyId.shears,
249 axis: TierAxis.metal,
250 label: "shears",
251 description: () => "All-metal shears for clipping fleece from animals.",
252 category: ItemCategory.tool,
253 iconKey: "shears",
254 defaultState: "shears",
255 stackable: false,
256 maxStackSize: 1,
257 seedValue: 10,
258 tintFromTier: true,
259 npcShopAllowed: true,
260 equipmentKind: EquipmentKind.shears,
261 equipmentTintFromTier: true
262 }),
263 [ItemFamilyId.arrow]: Object.freeze({
264 familyId: ItemFamilyId.arrow,
265 axis: TierAxis.metal,
266 label: "arrow",
267 description: () => "A fletched arrow with matching shaft and head.",
268 category: ItemCategory.finished,
269 iconKey: "arrow",
270 defaultState: "full-arrow",
271 stackable: true,
272 maxStackSize: 999,
273 seedValue: 2,
274 dualTintFromTier: true,
275 woodTierFromMetal: true,
276 equipmentKind: EquipmentKind.arrowAmmo,
277 equipmentSlot: "offhand",
278 hideEquipSprite: true
279 }),
280 [ItemFamilyId.unstrungBow]: Object.freeze({
281 familyId: ItemFamilyId.unstrungBow,
282 axis: TierAxis.wood,
283 label: "unstrung bow",
284 description: () => "A carved bow frame awaiting a string.",
285 category: ItemCategory.intermediate,
286 iconKey: "bow",
287 defaultState: "unstrung-bow",
288 stackable: false,
289 maxStackSize: 1,
290 seedValue: 6,
291 /** Red = wood; blue unused (same dual shader as bows/arrows). */
292 dualTintFromTier: true
293 }),
294 [ItemFamilyId.bow]: Object.freeze({
295 familyId: ItemFamilyId.bow,
296 axis: TierAxis.wood,
297 label: "bow",
298 description: (name) => `A ${name.toLowerCase()} bow strung with matching yarn.`,
299 category: ItemCategory.finished,
300 iconKey: "bow",
301 defaultState: "bow",
302 stackable: false,
303 maxStackSize: 1,
304 seedValue: 12,
305 /** Red = wood, blue = string (cloth tier). */
306 dualTintFromTier: true,
307 clothTierFromWood: true,
308 equipmentKind: EquipmentKind.bow,
309 equipmentAttack: Object.freeze({
310 mode: "ranged",
311 rangeTiles: COMBAT_RANGE_TILES,
312 hitSkillId: SkillId.archery,
313 damageSkillId: SkillId.dexterity,
314 attackType: AttackType.piercing,
315 cooldownMs: 1200
316 })
317 }),
318 [ItemFamilyId.bowstring]: Object.freeze({
319 familyId: ItemFamilyId.bowstring,
320 axis: TierAxis.cloth,
321 label: "bowstring",
322 description: () => "Twisted yarn spun tight for a bow.",
323 category: ItemCategory.intermediate,
324 iconKey: "bow",
325 defaultState: "bowstring",
326 stackable: true,
327 maxStackSize: DEFAULT_STACK,
328 seedValue: 2,
329 tintFromTier: true
330 }),
331 [ItemFamilyId.quarterstaff]: Object.freeze({
332 familyId: ItemFamilyId.quarterstaff,
333 axis: TierAxis.wood,
334 label: "quarterstaff",
335 description: (name) => `A ${name.toLowerCase()} quarterstaff, long enough to keep foes at a distance.`,
336 category: ItemCategory.finished,
337 iconKey: "quarterstaff",
338 defaultState: "quarterstaff",
339 stackable: false,
340 maxStackSize: 1,
341 seedValue: 8,
342 tintFromTier: true,
343 equipmentKind: EquipmentKind.clubWeapon,
344 equipmentSlot: "weapon",
345 equipmentTintFromTier: true,
346 equipmentAttack: Object.freeze({
347 mode: "melee",
348 rangeTiles: 1,
349 hitSkillId: SkillId.clubfighting,
350 damageSkillId: SkillId.strength,
351 attackType: AttackType.crushing,
352 cooldownMs: 700
353 })
354 }),
355 [ItemFamilyId.swordBlade]: Object.freeze({
356 familyId: ItemFamilyId.swordBlade,
357 axis: TierAxis.metal,
358 label: "sword blade",
359 description: (name) => `A smithed ${name.toLowerCase()} blade. Haft it with leather at a crafting bench.`,
360 category: ItemCategory.intermediate,
361 iconKey: "sword",
362 defaultState: "sword-blade",
363 stackable: true,
364 maxStackSize: DEFAULT_STACK,
365 seedValue: 6,
366 tintFromTier: true
367 }),
368 [ItemFamilyId.sword]: Object.freeze({
369 familyId: ItemFamilyId.sword,
370 axis: TierAxis.metal,
371 label: "sword",
372 description: (name) => `A ${name.toLowerCase()} sword, leather-wrapped for a sure grip.`,
373 category: ItemCategory.tool,
374 iconKey: "sword",
375 defaultState: "sword",
376 stackable: false,
377 maxStackSize: 1,
378 seedValue: 10,
379 /** Red = metal, blue = leather wrap (wood haft hidden under the wrap). */
380 dualTintFromTier: true,
381 leatherTierFromMetal: true,
382 equipmentKind: EquipmentKind.metalWeapon,
383 equipmentAttack: Object.freeze({
384 mode: "melee",
385 rangeTiles: 1,
386 hitSkillId: SkillId.swordfighting,
387 damageSkillId: SkillId.strength,
388 attackType: AttackType.slashing,
389 cooldownMs: 700
390 })
391 })
392});
393
394export function tierOrderForAxis (axis)
395{
396 if (axis === TierAxis.wood)
397 { return WOOD_TIER_ORDER; }
398 if (axis === TierAxis.metal)
399 { return MATERIAL_TIER_ORDER; }
400 if (axis === TierAxis.cloth)
401 { return CLOTH_TIER_ORDER; }
402 return [];
403}
404
405function buildFamilyEquipment (family, tier)
406{
407 if (!family.equipmentKind)
408 { return null; }
409 const equipment = {
410 slot: family.equipmentSlot ?? "weapon",
411 tier,
412 kind: family.equipmentKind
413 };
414 if (family.equipmentTintFromTier)
415 { equipment.tintFromTier = true; }
416 if (family.hideEquipSprite)
417 { equipment.hideEquipSprite = true; }
418 if (family.equipmentAttack)
419 { equipment.attack = family.equipmentAttack; }
420 return Object.freeze(equipment);
421}
422
423export function familyDefinition (familyId)
424{
425 return ITEM_FAMILIES[familyId] ?? null;
426}
427
428/**
429 * Build one tiered item definition from a family template.
430 * @param {string} familyId
431 * @param {string} tier
432 * @param {number} itemId stable numeric id (inventory / saves)
433 * @param {object} [extra] per-tier overrides (key, seedValue, description, …)
434 */
435export function buildTierItemDef (familyId, tier, itemId, extra = {})
436{
437 const family = familyDefinition(familyId);
438 if (!family)
439 { throw new Error(`Unknown item family: ${familyId}`); }
440 const name = family.axis === TierAxis.cloth
441 ? (CLOTH_DISPLAY_NAME[tier] ?? tier)
442 : (MATERIAL_DISPLAY_NAME[tier] ?? tier);
443 const key = extra.key ?? `${tier}-${family.label.replace(/ /g, "-")}`;
444 const seedValue = extra.seedValue ?? (
445 typeof family.seedValue === "function" ? family.seedValue(tier) : family.seedValue
446 );
447 const description = extra.description ?? family.description(name);
448 const equipment = extra.equipment ?? buildFamilyEquipment(family, tier);
449 return Object.freeze({
450 itemId,
451 familyId,
452 key,
453 displayName: extra.displayName ?? `${name} ${family.label}`,
454 description,
455 seedValue,
456 stackable: extra.stackable ?? family.stackable,
457 maxStackSize: extra.maxStackSize ?? family.maxStackSize,
458 uniqueInstance: false,
459 category: extra.category ?? family.category,
460 iconKey: extra.iconKey ?? family.iconKey,
461 defaultState: extra.defaultState ?? family.defaultState,
462 tier,
463 ...(family.tintFromTier ? { tintFromTier: true } : {}),
464 ...(family.npcShopAllowed ? { npcShopAllowed: true } : {}),
465 ...(family.woodTierFromMetal ? { woodTier: woodTierForMetal(tier), dualTintFromTier: true } : {}),
466 ...(family.leatherTierFromMetal
467 ? { leatherTier: leatherTierForMetal(tier), dualTintFromTier: true }
468 : {}),
469 ...(family.dualTintFromTier && !family.woodTierFromMetal && !family.leatherTierFromMetal
470 ? { dualTintFromTier: true }
471 : {}),
472 ...(family.clothTierFromWood ? { clothTier: clothTierForWood(tier) } : {}),
473 ...(equipment ? { equipment } : {})
474 });
475}
476
477/** Build `{ [itemId]: def }` from a tier → itemId map. */
478export function buildFamilyDefinitionMap (familyId, tierToItemId, extraByTier = {})
479{
480 const defs = {};
481 for (const [tier, id] of Object.entries(tierToItemId))
482 {
483 const itemId = id | 0;
484 if (!itemId)
485 { continue; }
486 defs[itemId] = buildTierItemDef(familyId, tier, itemId, extraByTier[tier] ?? {});
487 }
488 return defs;
489}
490
491/** Resolve itemId for a family tier from a lookup table. */
492export function itemIdForFamilyTier (familyTierMap, tier)
493{
494 return familyTierMap[tier] ?? null;
495}
496
497/** Reverse lookup: tier for an item id within a family map, or null. */
498export function tierForFamilyItemId (familyTierMap, itemId)
499{
500 const want = itemId | 0;
501 for (const [tier, id] of Object.entries(familyTierMap))
502 {
503 if ((id | 0) === want)
504 { return tier; }
505 }
506 return null;
507}