BlueGit

timfa questward

Files

questward / shared / definitions / tierFamilies.js

tierFamilies.js

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