BlueGit

timfa questward

Files

questward / shared / definitions / tierFamilies.js

tierFamilies.js

lots of things
timfa Tim Falken · pushed by timfa · 2 months ago
07a95f71 History
tierFamilies.js 11.5 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: "bow",
227 stackable: false,
228 maxStackSize: 1,
229 seedValue: 6,
230 tintFromTier: true
231 }),
232 [ItemFamilyId.bow]: Object.freeze({
233 familyId: ItemFamilyId.bow,
234 axis: TierAxis.wood,
235 label: "bow",
236 description: (name) => `A ${name.toLowerCase()} bow strung with matching yarn.`,
237 category: ItemCategory.finished,
238 iconKey: "bow",
239 defaultState: "bow",
240 stackable: false,
241 maxStackSize: 1,
242 seedValue: 12,
243 tintFromTier: true,
244 clothTierFromWood: true,
245 equipmentKind: EquipmentKind.bow,
246 equipmentTintFromTier: true,
247 equipmentAttack: Object.freeze({
248 mode: "ranged",
249 rangeTiles: COMBAT_RANGE_TILES,
250 hitSkillId: SkillId.archery,
251 damageSkillId: SkillId.dexterity,
252 attackType: AttackType.piercing,
253 cooldownMs: 1200
254 })
255 }),
256 [ItemFamilyId.bowstring]: Object.freeze({
257 familyId: ItemFamilyId.bowstring,
258 axis: TierAxis.cloth,
259 label: "bowstring",
260 description: () => "Twisted yarn spun tight for a bow.",
261 category: ItemCategory.intermediate,
262 iconKey: "cloth",
263 defaultState: "yarn",
264 stackable: true,
265 maxStackSize: DEFAULT_STACK,
266 seedValue: 2,
267 tintFromTier: true
268 }),
269 [ItemFamilyId.quarterstaff]: Object.freeze({
270 familyId: ItemFamilyId.quarterstaff,
271 axis: TierAxis.wood,
272 label: "quarterstaff",
273 description: (name) => `A ${name.toLowerCase()} quarterstaff, long enough to keep foes at a distance.`,
274 category: ItemCategory.finished,
275 iconKey: "quarterstaff",
276 defaultState: "quarterstaff",
277 stackable: false,
278 maxStackSize: 1,
279 seedValue: 8,
280 tintFromTier: true,
281 equipmentKind: EquipmentKind.clubWeapon,
282 equipmentSlot: "weapon",
283 equipmentTintFromTier: true,
284 equipmentAttack: Object.freeze({
285 mode: "melee",
286 rangeTiles: 1,
287 hitSkillId: SkillId.clubfighting,
288 damageSkillId: SkillId.strength,
289 attackType: AttackType.blunt,
290 cooldownMs: 700
291 })
292 })
293});
294
295export function tierOrderForAxis (axis)
296{
297 if (axis === TierAxis.wood)
298 { return WOOD_TIER_ORDER; }
299 if (axis === TierAxis.metal)
300 { return MATERIAL_TIER_ORDER; }
301 if (axis === TierAxis.cloth)
302 { return CLOTH_TIER_ORDER; }
303 return [];
304}
305
306function buildFamilyEquipment (family, tier)
307{
308 if (!family.equipmentKind)
309 { return null; }
310 const equipment = {
311 slot: family.equipmentSlot ?? "weapon",
312 tier,
313 kind: family.equipmentKind
314 };
315 if (family.equipmentTintFromTier)
316 { equipment.tintFromTier = true; }
317 if (family.hideEquipSprite)
318 { equipment.hideEquipSprite = true; }
319 if (family.equipmentAttack)
320 { equipment.attack = family.equipmentAttack; }
321 return Object.freeze(equipment);
322}
323
324export function familyDefinition (familyId)
325{
326 return ITEM_FAMILIES[familyId] ?? null;
327}
328
329/**
330 * Build one tiered item definition from a family template.
331 * @param {string} familyId
332 * @param {string} tier
333 * @param {number} itemId stable numeric id (inventory / saves)
334 * @param {object} [extra] per-tier overrides (key, seedValue, description, …)
335 */
336export function buildTierItemDef (familyId, tier, itemId, extra = {})
337{
338 const family = familyDefinition(familyId);
339 if (!family)
340 { throw new Error(`Unknown item family: ${familyId}`); }
341 const name = family.axis === TierAxis.cloth
342 ? (CLOTH_DISPLAY_NAME[tier] ?? tier)
343 : (MATERIAL_DISPLAY_NAME[tier] ?? tier);
344 const key = extra.key ?? `${tier}-${family.label.replace(/ /g, "-")}`;
345 const seedValue = extra.seedValue ?? (
346 typeof family.seedValue === "function" ? family.seedValue(tier) : family.seedValue
347 );
348 const description = extra.description ?? family.description(name);
349 const equipment = extra.equipment ?? buildFamilyEquipment(family, tier);
350 return Object.freeze({
351 itemId,
352 familyId,
353 key,
354 displayName: extra.displayName ?? `${name} ${family.label}`,
355 description,
356 seedValue,
357 stackable: extra.stackable ?? family.stackable,
358 maxStackSize: extra.maxStackSize ?? family.maxStackSize,
359 uniqueInstance: false,
360 category: extra.category ?? family.category,
361 iconKey: extra.iconKey ?? family.iconKey,
362 defaultState: extra.defaultState ?? family.defaultState,
363 tier,
364 ...(family.tintFromTier ? { tintFromTier: true } : {}),
365 ...(family.npcShopAllowed ? { npcShopAllowed: true } : {}),
366 ...(family.woodTierFromMetal ? { woodTier: woodTierForMetal(tier), dualTintFromTier: true } : {}),
367 ...(family.dualTintFromTier && !family.woodTierFromMetal ? { dualTintFromTier: true } : {}),
368 ...(family.clothTierFromWood ? { clothTier: clothTierForWood(tier) } : {}),
369 ...(equipment ? { equipment } : {})
370 });
371}
372
373/** Build `{ [itemId]: def }` from a tier → itemId map. */
374export function buildFamilyDefinitionMap (familyId, tierToItemId, extraByTier = {})
375{
376 const defs = {};
377 for (const [tier, id] of Object.entries(tierToItemId))
378 {
379 const itemId = id | 0;
380 if (!itemId)
381 { continue; }
382 defs[itemId] = buildTierItemDef(familyId, tier, itemId, extraByTier[tier] ?? {});
383 }
384 return defs;
385}
386
387/** Resolve itemId for a family tier from a lookup table. */
388export function itemIdForFamilyTier (familyTierMap, tier)
389{
390 return familyTierMap[tier] ?? null;
391}
392
393/** Reverse lookup: tier for an item id within a family map, or null. */
394export function tierForFamilyItemId (familyTierMap, itemId)
395{
396 const want = itemId | 0;
397 for (const [tier, id] of Object.entries(familyTierMap))
398 {
399 if ((id | 0) === want)
400 { return tier; }
401 }
402 return null;
403}