BlueGit

timfa questward

Files

questward / shared / definitions / items.js

items.js

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