BlueGit

timfa questward

Files

questward / shared / definitions / items.js

items.js

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