BlueGit

timfa questward

Files

questward / shared / definitions / tierFamilies.js

tierFamilies.js

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