BlueGit

timfa questward

Files

questward / shared / definitions / items.js

items.js

items.js 17.5 KiB Download Raw
1import { equipImageName, spriteImageName } from "./tileSprites.js";
2import { SkillId } from "./skills.js";
3import { MaterialTier, WoodTier } from "./materialTiers.js";
4import { AttackType } from "./attackTypes.js";
5import { EquipmentKind } from "./equipmentKinds.js";
6import { formatQuantity } from "../items/formatQuantity.js";
7import { formatOriginBreakdown } from "../items/origin.js";
8import { CraftingItemId, CRAFTING_ITEM_STUBS } from "./craftingItems.js";
9import { MetalItemId, METAL_ITEM_DEFINITIONS } from "./metalItems.js";
10import { WoodItemId, WOOD_ITEM_DEFINITIONS } from "./woodItems.js";
11import { ArmourItemId, ARMOUR_ITEM_DEFINITIONS } from "./armourItems.js";
12import { ClothItemId, CLOTH_ITEM_DEFINITIONS } from "./clothItems.js";
13import { LeatherItemId, LEATHER_ITEM_DEFINITIONS } from "./leatherItems.js";
14import { LeatherArmourItemId, LEATHER_ARMOUR_ITEM_DEFINITIONS } from "./leatherArmourItems.js";
15import { WizardItemId, WIZARD_ITEM_DEFINITIONS } from "./wizardItems.js";
16import { DyeItemId, FLOWER_ITEM_DEFINITION, DYE_ITEM_DEFINITION, FLOWER_COLOR_DISPLAY } from "./dyes.js";
17import { HERB_ITEM_DEFINITIONS, HerbItemId } from "./herbs.js";
18import { RANGED_ITEM_DEFINITIONS, RangedItemId } from "./rangedItems.js";
19import { POTION_MAX_STACK } from "./alchemy.js";
20import { SpellbookItemId, SPELLBOOK_ITEM_DEFINITIONS } from "./spellItems.js";
21import { FISH_ITEM_DEFINITIONS, FishingToolItemId } from "./fishItems.js";
22import { GemItemId, GEM_ITEM_DEFINITIONS } from "./gemItems.js";
23import { SwordItemId, SWORD_ITEM_DEFINITIONS } from "./swordItems.js";
24import { WarAxeItemId, WAR_AXE_ITEM_DEFINITIONS } from "./warAxeItems.js";
25import { EssenceItemId, ESSENCE_ITEM_DEFINITIONS } from "./essenceItems.js";
26
27export const ItemId = Object.freeze({
28 paper: 1,
29 apple: 2,
30 stick: 3,
31 blankCheque: 4,
32 /** Cotton shirt (ClothItemId.cottonShirt). */
33 clothShirt: 5,
34 letter: 6,
35 filledCheque: 7,
36 coins: 8,
37 ...CraftingItemId,
38 lantern: 26,
39 bones: 27,
40 skull: 28,
41 glassBottle: 29,
42 potion: 32,
43 recipePaper: 33,
44 egg: 275,
45 cookedEgg: 504,
46 burntEgg: 505,
47 ...SpellbookItemId,
48 ...MetalItemId,
49 ...WoodItemId,
50 ...FishingToolItemId,
51 ...ArmourItemId,
52 ...ClothItemId,
53 ...LeatherItemId,
54 ...LeatherArmourItemId,
55 ...WizardItemId,
56 ...DyeItemId,
57 ...HerbItemId,
58 ...RangedItemId,
59 ...GemItemId,
60 ...SwordItemId,
61 ...WarAxeItemId
62});
63
64export const EQUIPMENT_SLOT = Object.freeze({
65 head: "head",
66 chest: "chest",
67 legs: "legs",
68 boots: "boots",
69 weapon: "weapon",
70 offhand: "offhand"
71});
72
73/** Back → front. Weapon sits above chest but under legs (legs also cover shirt hem). */
74export const EQUIPMENT_DRAW_ORDER = Object.freeze([
75 EQUIPMENT_SLOT.boots,
76 EQUIPMENT_SLOT.chest,
77 EQUIPMENT_SLOT.weapon,
78 EQUIPMENT_SLOT.legs,
79 EQUIPMENT_SLOT.head,
80 EQUIPMENT_SLOT.offhand
81]);
82
83export const DEFAULT_MAX_STACK = 99;
84export const BLANK_CHEQUE_CARRY_CAP = 99;
85export const COIN_MAX_STACK = 999_000_000_000;
86
87/** Coin sprite tiers (coins-<n>.png); pick the largest n where stack quantity >= n. */
88export const COIN_SPRITE_TIERS = Object.freeze([
89 1, 5, 15, 50, 150, 500, 1000, 1500, 2000, 5000, 10000, 20000, 50000,
90 1_000_000, 10_000_000, 50_000_000, 100_000_000, 500_000_000,
91 1_000_000_000, 10_000_000_000, 100_000_000_000, 1_000_000_000_000,
92 10_000_000_000_000, 100_000_000_000_000, 500_000_000_000_000,
93 999_999_999_999_999
94]);
95
96export const ITEM_DEFINITIONS = Object.freeze({
97 [ItemId.paper]: Object.freeze({
98 itemId: ItemId.paper,
99 key: "paper",
100 displayName: "Paper",
101 description: "Blank sheet of paper.",
102 seedValue: 2,
103 stackable: true,
104 maxStackSize: DEFAULT_MAX_STACK,
105 uniqueInstance: false,
106 category: "document"
107 }),
108 [ItemId.letter]: Object.freeze({
109 itemId: ItemId.letter,
110 key: "letter",
111 displayName: "Letter",
112 description: "A written letter.",
113 seedValue: 2,
114 stackable: false,
115 maxStackSize: 1,
116 uniqueInstance: true,
117 category: "document",
118 defaultState: "letter-opened",
119 marketplaceSellable: false,
120 marketplaceSellBlockedReason: "Letters cannot be sold on the marketplace."
121 }),
122 [ItemId.apple]: Object.freeze({
123 itemId: ItemId.apple,
124 key: "apple",
125 displayName: "Apple",
126 description: "Round and juicy!",
127 seedValue: 1,
128 stackable: true,
129 maxStackSize: DEFAULT_MAX_STACK,
130 uniqueInstance: false,
131 category: "food",
132 healing: 4
133 }),
134 [ItemId.egg]: Object.freeze({
135 itemId: ItemId.egg,
136 key: "egg",
137 displayName: "Egg",
138 description: "A fresh egg, still warm from the nest. Bake it in an oven before eating.",
139 seedValue: 1,
140 stackable: true,
141 maxStackSize: DEFAULT_MAX_STACK,
142 uniqueInstance: false,
143 category: "ingredient",
144 iconKey: "egg",
145 defaultState: "egg"
146 }),
147 [ItemId.cookedEgg]: Object.freeze({
148 itemId: ItemId.cookedEgg,
149 key: "cooked-egg",
150 displayName: "Cooked egg",
151 description: "An egg baked until firm, ready to eat.",
152 seedValue: 2,
153 stackable: true,
154 maxStackSize: DEFAULT_MAX_STACK,
155 uniqueInstance: false,
156 category: "food",
157 iconKey: "egg",
158 defaultState: "egg",
159 healing: 3
160 }),
161 [ItemId.burntEgg]: Object.freeze({
162 itemId: ItemId.burntEgg,
163 key: "burnt-egg",
164 displayName: "Burnt egg",
165 description: "An egg left too long in the oven — dry and inedible.",
166 seedValue: 1,
167 stackable: true,
168 maxStackSize: DEFAULT_MAX_STACK,
169 uniqueInstance: false,
170 category: "food",
171 iconKey: "egg",
172 defaultState: "egg"
173 }),
174 [ItemId.stick]: Object.freeze({
175 itemId: ItemId.stick,
176 key: "stick",
177 displayName: "Stick",
178 description: "A wooden stick. Simple and sturdy.",
179 seedValue: 3,
180 stackable: false,
181 maxStackSize: 1,
182 uniqueInstance: false,
183 category: "finished",
184 equipment: Object.freeze({
185 slot: EQUIPMENT_SLOT.weapon,
186 tier: WoodTier.pine,
187 kind: EquipmentKind.clubWeapon,
188 attack: Object.freeze({
189 mode: "melee",
190 rangeTiles: 1,
191 hitSkillId: SkillId.clubfighting,
192 damageSkillId: SkillId.strength,
193 attackType: AttackType.blunt,
194 cooldownMs: 700
195 })
196 })
197 }),
198 [ItemId.blankCheque]: Object.freeze({
199 itemId: ItemId.blankCheque,
200 key: "blank-cheque",
201 displayName: "Blank cheque",
202 description: "Bank stationery for writing cheques against deposited goods.",
203 seedValue: 5,
204 stackable: true,
205 maxStackSize: DEFAULT_MAX_STACK,
206 uniqueInstance: false,
207 category: "document",
208 marketplaceSellable: false,
209 marketplaceSellBlockedReason: "Blank cheques cannot be sold on the marketplace."
210 }),
211 [ItemId.filledCheque]: Object.freeze({
212 itemId: ItemId.filledCheque,
213 key: "filled-cheque",
214 displayName: "Cheque",
215 description: "A bearer cheque linked to reserved goods in the issuer's bank.",
216 seedValue: 5,
217 stackable: false,
218 maxStackSize: 1,
219 uniqueInstance: true,
220 category: "document",
221 defaultState: "filled-cheque",
222 marketplaceSellable: true
223 }),
224 [ItemId.coins]: Object.freeze({
225 itemId: ItemId.coins,
226 key: "coins",
227 displayName: "Coin",
228 displayNamePlural: "Coins",
229 description: "Hard currency of the realm.",
230 seedValue: 1,
231 stackable: true,
232 maxStackSize: COIN_MAX_STACK,
233 uniqueInstance: false,
234 category: "currency"
235 }),
236 [ItemId.lantern]: Object.freeze({
237 itemId: ItemId.lantern,
238 key: "lantern",
239 displayName: "Lantern",
240 description: "A handheld lamp that pushes back the night.",
241 seedValue: 12,
242 stackable: false,
243 maxStackSize: 1,
244 uniqueInstance: false,
245 category: "tool",
246 equipment: Object.freeze({
247 slot: EQUIPMENT_SLOT.offhand,
248 lightRadiusTiles: 6
249 })
250 }),
251 [ItemId.bones]: Object.freeze({
252 itemId: ItemId.bones,
253 key: "bones",
254 displayName: "Bones",
255 description: "Bones of a dead creature.",
256 seedValue: 1,
257 stackable: true,
258 maxStackSize: DEFAULT_MAX_STACK,
259 uniqueInstance: false,
260 category: "ingredient"
261 }),
262 [ItemId.skull]: Object.freeze({
263 itemId: ItemId.skull,
264 key: "skull",
265 displayName: "Skull",
266 description: "Unlucky.",
267 seedValue: 1,
268 stackable: true,
269 maxStackSize: DEFAULT_MAX_STACK,
270 uniqueInstance: false,
271 category: "misc"
272 }),
273 [ItemId.glassBottle]: Object.freeze({
274 itemId: ItemId.glassBottle,
275 key: "glass-bottle",
276 displayName: "Glass bottle",
277 description: "A clear bottle, ready to hold a brew or dye.",
278 seedValue: 2,
279 stackable: true,
280 maxStackSize: DEFAULT_MAX_STACK,
281 uniqueInstance: false,
282 category: "tool",
283 iconKey: "potion",
284 defaultState: "potion-0",
285 npcShopAllowed: true
286 }),
287 [ItemId.potion]: Object.freeze({
288 itemId: ItemId.potion,
289 key: "potion",
290 displayName: "Potion",
291 description: "A bottled brew. Sip with care.",
292 seedValue: 8,
293 stackable: true,
294 maxStackSize: POTION_MAX_STACK,
295 uniqueInstance: false,
296 category: "potion",
297 potion: true,
298 iconKey: "potion"
299 }),
300 [ItemId.recipePaper]: Object.freeze({
301 itemId: ItemId.recipePaper,
302 key: "recipe-paper",
303 displayName: "Recipe paper",
304 description: "An alchemy recipe written for a cauldron.",
305 seedValue: 2,
306 stackable: false,
307 maxStackSize: 1,
308 uniqueInstance: true,
309 category: "document",
310 defaultState: "recipe-paper",
311 iconKey: "paper"
312 }),
313 ...CRAFTING_ITEM_STUBS,
314 ...LEATHER_ITEM_DEFINITIONS,
315 ...LEATHER_ARMOUR_ITEM_DEFINITIONS,
316 ...WIZARD_ITEM_DEFINITIONS,
317 ...METAL_ITEM_DEFINITIONS,
318 ...WOOD_ITEM_DEFINITIONS,
319 ...ARMOUR_ITEM_DEFINITIONS,
320 ...CLOTH_ITEM_DEFINITIONS,
321 ...HERB_ITEM_DEFINITIONS,
322 ...RANGED_ITEM_DEFINITIONS,
323 ...SPELLBOOK_ITEM_DEFINITIONS,
324 ...FISH_ITEM_DEFINITIONS,
325 ...GEM_ITEM_DEFINITIONS,
326 ...SWORD_ITEM_DEFINITIONS,
327 ...WAR_AXE_ITEM_DEFINITIONS,
328 ...ESSENCE_ITEM_DEFINITIONS,
329 [DyeItemId.flower]: FLOWER_ITEM_DEFINITION,
330 [DyeItemId.dye]: DYE_ITEM_DEFINITION
331});
332
333export function itemDefinition (itemId)
334{
335 return ITEM_DEFINITIONS[itemId] ?? null;
336}
337
338/** Every defined item for editor pickers (shop stock, dev tools, …). */
339export function listAllItemPickerEntries ()
340{
341 return Object.values(ITEM_DEFINITIONS)
342 .filter((def) => def?.itemId != null)
343 .map((def) => ({
344 itemId: def.itemId,
345 displayName: def.displayName ?? String(def.itemId),
346 key: def.key ?? String(def.itemId)
347 }))
348 .sort((a, b) => a.displayName.localeCompare(b.displayName) || (a.itemId - b.itemId));
349}
350
351/** Garments that accept optional `instance.dyeColor` (tailor / cloth clothing). */
352export function isDyeableClothing (itemIdOrDef)
353{
354 const def = typeof itemIdOrDef === "number"
355 ? itemDefinition(itemIdOrDef)
356 : itemIdOrDef;
357 return !!def?.dyeable;
358}
359
360export { formatQuantity };
361
362export function itemDisplayName (stack, def = itemDefinition(stack?.itemId))
363{
364 if (!def)
365 { return "Item"; }
366 const qty = stack?.quantity ?? 1;
367 if (stack?.itemId === ItemId.coins)
368 { return qty === 1 ? (def.displayName ?? "Coin") : (def.displayNamePlural ?? "Coins"); }
369 if (stack?.itemId === ItemId.potion && stack.instance?.displayName)
370 { return stack.instance.displayName; }
371 if (stack?.itemId === ItemId.dye && stack.instance?.kind === "dye")
372 {
373 if (stack.instance.colorId && FLOWER_COLOR_DISPLAY[stack.instance.colorId])
374 { return `${FLOWER_COLOR_DISPLAY[stack.instance.colorId]} dye`; }
375 return "Dye";
376 }
377 if (stack?.itemId === ItemId.flower && stack.instance?.colorId)
378 {
379 return `${FLOWER_COLOR_DISPLAY[stack.instance.colorId] ?? "Flower"} flower`;
380 }
381 if (stack?.itemId === ItemId.recipePaper && stack.instance?.displayName)
382 {
383 return String(stack.instance.displayName).replace(/\s+Recipe$/u, " recipe");
384 }
385 if (stack?.itemId === CraftingItemId.tinderbox && stack.instance?.kind === "tinderbox")
386 {
387 const charges = Math.max(0, stack.instance.charges | 0);
388 return `${def.displayName} (${charges})`;
389 }
390 if (stack?.instance?.kind === "tipped-arrow" && stack.instance.element)
391 {
392 const el = String(stack.instance.element);
393 const label = el.charAt(0).toUpperCase() + el.slice(1);
394 return `${label} ${def.displayName ?? "arrow"}`;
395 }
396 return def.displayName ?? "Item";
397}
398
399export function stackDisplayLabel (stack)
400{
401 const name = itemDisplayName(stack);
402 const qty = stack?.quantity ?? 1;
403 if (qty <= 1)
404 { return name; }
405 return `${name} (${formatQuantity(qty)})`;
406}
407
408export function coinSpriteTierForQuantity (quantity)
409{
410 const qty = Math.max(0, Math.floor(Number(quantity) || 0));
411 let tier = COIN_SPRITE_TIERS[0];
412 for (const candidate of COIN_SPRITE_TIERS)
413 {
414 if (qty >= candidate)
415 { tier = candidate; }
416 else
417 { break; }
418 }
419 return tier;
420}
421
422/** Stack-aware description (letters include open state and author). */
423export function itemDescription (stack, { localCharacterId } = {})
424{
425 const def = itemDefinition(stack?.itemId);
426 if (!def)
427 { return ""; }
428 if (stack.itemId === ItemId.letter && stack.instance?.kind === "letter")
429 {
430 const state = stack.instance.opened === false ? "unopened" : "opened";
431 const authorId = stack.instance.authorCharacterId;
432 const byYou = authorId != null && Number(authorId) === Number(localCharacterId);
433 const author = byYou
434 ? "you"
435 : (stack.instance.authorDisplayName?.trim() || "someone");
436 return `An ${state} letter, written by ${author}.`;
437 }
438 if (stack.itemId === ItemId.filledCheque && stack.instance?.kind === "filled-cheque")
439 {
440 const reserved = itemDefinition(stack.instance.itemId);
441 const qty = stack.instance.quantity | 0;
442 const name = reserved?.displayName ?? "goods";
443 return `Bearer cheque for ${qty}× ${name}.`;
444 }
445 if (stack.itemId === ItemId.potion && stack.instance?.kind === "potion")
446 {
447 const name = stack.instance.displayName ?? "Potion";
448 return stack.instance.unstable
449 ? `${name}. The brew smells wrong — best sipped with caution.`
450 : `${name}. A carefully bottled draught.`;
451 }
452 if (stack.itemId === ItemId.recipePaper && stack.instance?.kind === "recipe-paper")
453 {
454 const authorId = stack.instance.authorCharacterId;
455 const byYou = authorId != null && Number(authorId) === Number(localCharacterId);
456 const author = byYou
457 ? "you"
458 : (stack.instance.authorDisplayName?.trim() || "someone");
459 return `Written by ${author}.`;
460 }
461 return def.description ?? "";
462}
463
464export function itemOriginSummary (stack)
465{
466 // Recipe papers already say "Written by …" in the description — avoid a duplicate origin line.
467 if (stack?.itemId === ItemId.recipePaper && stack.instance?.kind === "recipe-paper")
468 { return ""; }
469 return formatOriginBreakdown(stack);
470}
471
472export function itemDefaultState (def)
473{
474 return def?.defaultState ?? def?.key ?? "paper";
475}
476
477export function itemSpriteName (itemId, instance = null, quantity = null)
478{
479 const def = itemDefinition(itemId);
480 if (!def)
481 { return spriteImageName("item", "paper", "paper"); }
482 if (itemId === ItemId.coins)
483 {
484 const tier = coinSpriteTierForQuantity(quantity ?? 1);
485 return spriteImageName("item", "coins", `coins-${tier}`);
486 }
487 if (itemId === ItemId.letter)
488 { return spriteImageName("item", "letter", letterVisualState(instance)); }
489 if (instance?.kind === "tipped-arrow")
490 { return spriteImageName("item", "arrow", "tipped-arrow"); }
491 const folderKey = def.iconKey ?? def.key;
492 return def.sprite ?? spriteImageName("item", folderKey, itemDefaultState(def));
493}
494
495/** Stack-aware sprite key (coins use quantity tiers like inventory icons). */
496export function itemSpriteNameForStack (stack)
497{
498 if (!stack?.itemId)
499 { return itemSpriteName(null); }
500 return itemSpriteName(stack.itemId, stack.instance ?? null, stack.quantity);
501}
502
503export function itemEquipSprite (itemId)
504{
505 const def = itemDefinition(itemId);
506 if (!def)
507 { return null; }
508 const folderKey = def.iconKey ?? def.key;
509 const equipment = def.equipment;
510 if (equipment)
511 {
512 if (equipment.hideEquipSprite)
513 { return null; }
514 return equipment.equipSprite
515 ?? equipment.overlaySpriteId
516 ?? equipImageName("item", folderKey, itemDefaultState(def));
517 }
518 /** Inventory tools (e.g. tinderbox) may still show equip art during gather/firestarting. */
519 if (def.gatherEquipSprite)
520 { return equipImageName("item", folderKey, itemDefaultState(def)); }
521 return null;
522}
523
524/** Largest equipped light radius (e.g. lantern in offhand). */
525export function equippedLightRadiusTiles (equipment)
526{
527 let best = 0;
528 for (const stack of Object.values(equipment?.slots ?? {}))
529 {
530 if (!stack)
531 { continue; }
532 const radius = itemDefinition(stack.itemId)?.equipment?.lightRadiusTiles ?? 0;
533 if (radius > best)
534 { best = radius; }
535 }
536 return best;
537}
538
539export function letterVisualState (instance)
540{
541 return instance?.opened === false ? "letter-closed" : "letter-opened";
542}
543
544export function itemAssetPath (itemId, companion = null, instance = null)
545{
546 const def = itemDefinition(itemId);
547 if (def?.herb)
548 {
549 const suffix = companion ? `-${companion}` : "";
550 return `/assets/herbs/${def.key}${suffix}.png`;
551 }
552 const key = def?.iconKey ?? def?.key ?? "paper";
553 const state = itemId === ItemId.letter
554 ? letterVisualState(instance)
555 : itemDefaultState(def ?? { key });
556 const suffix = companion ? `-${companion}` : "";
557 return `/assets/items/${key}/${state}${suffix}.png`;
558}
559
560export function itemAssetPathForStack (stack)
561{
562 if (!stack)
563 { return itemAssetPath(null); }
564 if (stack.itemId === ItemId.coins)
565 {
566 const tier = coinSpriteTierForQuantity(stack.quantity);
567 return `/assets/items/coins/coins-${tier}.png`;
568 }
569 if (stack.itemId === ItemId.potion)
570 {
571 const fill = Math.max(1, Math.min(5, stack.instance?.fillLevel | 0 || 4));
572 return `/assets/items/potion/potion-${fill}.png`;
573 }
574 if (stack.itemId === ItemId.dye)
575 {
576 const fill = Math.max(1, Math.min(5, stack.instance?.fillLevel | 0 || 4));
577 return `/assets/items/potion/potion-${fill}.png`;
578 }
579 if (stack.itemId === ItemId.glassBottle)
580 { return `/assets/items/potion/potion-0.png`; }
581 if (stack.itemId === ItemId.flower)
582 { return `/assets/items/flower/flower.png`; }
583 if (stack.instance?.kind === "tipped-arrow")
584 { return `/assets/items/arrow/tipped-arrow.png`; }
585 return itemAssetPath(stack.itemId, null, stack.instance);
586}
587
588export const LETTER_MAX_BODY_CHARS = 2000;
589
590export const STARTER_ITEMS = Object.freeze([
591 Object.freeze({ itemId: ItemId.paper, quantity: 5 }),
592 Object.freeze({ itemId: ItemId.apple, quantity: 3 }),
593 Object.freeze({ itemId: ItemId.stick, quantity: 1 }),
594 Object.freeze({ itemId: ItemId.coins, quantity: 25 })
595]);