BlueGit

timfa questward

Files

questward / shared / definitions / tierFamilies.js

tierFamilies.js

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