BlueGit

timfa questward

Files

questward / shared / definitions / items.js

items.js

items.js 16.0 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 {
347 return String(stack.instance.displayName).replace(/\s+Recipe$/u, " recipe");
348 }
349 if (stack?.itemId === CraftingItemId.tinderbox && stack.instance?.kind === "tinderbox")
350 {
351 const charges = Math.max(0, stack.instance.charges | 0);
352 return `${def.displayName} (${charges})`;
353 }
354 if (stack?.instance?.kind === "tipped-arrow" && stack.instance.element)
355 {
356 const el = String(stack.instance.element);
357 const label = el.charAt(0).toUpperCase() + el.slice(1);
358 return `${label} ${def.displayName ?? "arrow"}`;
359 }
360 return def.displayName ?? "Item";
361}
362
363export function stackDisplayLabel (stack)
364{
365 const name = itemDisplayName(stack);
366 const qty = stack?.quantity ?? 1;
367 if (qty <= 1)
368 { return name; }
369 return `${name} (${formatQuantity(qty)})`;
370}
371
372export function coinSpriteTierForQuantity (quantity)
373{
374 const qty = Math.max(0, Math.floor(Number(quantity) || 0));
375 let tier = COIN_SPRITE_TIERS[0];
376 for (const candidate of COIN_SPRITE_TIERS)
377 {
378 if (qty >= candidate)
379 { tier = candidate; }
380 else
381 { break; }
382 }
383 return tier;
384}
385
386/** Stack-aware description (letters include open state and author). */
387export function itemDescription (stack, { localCharacterId } = {})
388{
389 const def = itemDefinition(stack?.itemId);
390 if (!def)
391 { return ""; }
392 if (stack.itemId === ItemId.letter && stack.instance?.kind === "letter")
393 {
394 const state = stack.instance.opened === false ? "unopened" : "opened";
395 const authorId = stack.instance.authorCharacterId;
396 const byYou = authorId != null && Number(authorId) === Number(localCharacterId);
397 const author = byYou
398 ? "you"
399 : (stack.instance.authorDisplayName?.trim() || "someone");
400 return `An ${state} letter, written by ${author}.`;
401 }
402 if (stack.itemId === ItemId.filledCheque && stack.instance?.kind === "filled-cheque")
403 {
404 const reserved = itemDefinition(stack.instance.itemId);
405 const qty = stack.instance.quantity | 0;
406 const name = reserved?.displayName ?? "goods";
407 return `Bearer cheque for ${qty}× ${name}.`;
408 }
409 if (stack.itemId === ItemId.potion && stack.instance?.kind === "potion")
410 {
411 const name = stack.instance.displayName ?? "Potion";
412 return stack.instance.unstable
413 ? `${name}. The brew smells wrong — best sipped with caution.`
414 : `${name}. A carefully bottled draught.`;
415 }
416 if (stack.itemId === ItemId.recipePaper && stack.instance?.kind === "recipe-paper")
417 {
418 const authorId = stack.instance.authorCharacterId;
419 const byYou = authorId != null && Number(authorId) === Number(localCharacterId);
420 const author = byYou
421 ? "you"
422 : (stack.instance.authorDisplayName?.trim() || "someone");
423 return `Written by ${author}.`;
424 }
425 return def.description ?? "";
426}
427
428export function itemOriginSummary (stack)
429{
430 // Recipe papers already say "Written by …" in the description — avoid a duplicate origin line.
431 if (stack?.itemId === ItemId.recipePaper && stack.instance?.kind === "recipe-paper")
432 { return ""; }
433 return formatOriginBreakdown(stack);
434}
435
436export function itemDefaultState (def)
437{
438 return def?.defaultState ?? def?.key ?? "paper";
439}
440
441export function itemSpriteName (itemId, instance = null)
442{
443 const def = itemDefinition(itemId);
444 if (!def)
445 { return spriteImageName("item", "paper", "paper"); }
446 if (itemId === ItemId.letter)
447 { return spriteImageName("item", "letter", letterVisualState(instance)); }
448 if (instance?.kind === "tipped-arrow")
449 { return spriteImageName("item", "arrow", "tipped-arrow"); }
450 const folderKey = def.iconKey ?? def.key;
451 return def.sprite ?? spriteImageName("item", folderKey, itemDefaultState(def));
452}
453
454export function itemEquipSprite (itemId)
455{
456 const def = itemDefinition(itemId);
457 if (!def)
458 { return null; }
459 const folderKey = def.iconKey ?? def.key;
460 const equipment = def.equipment;
461 if (equipment)
462 {
463 if (equipment.hideEquipSprite)
464 { return null; }
465 return equipment.equipSprite
466 ?? equipment.overlaySpriteId
467 ?? equipImageName("item", folderKey, itemDefaultState(def));
468 }
469 /** Inventory tools (e.g. tinderbox) may still show equip art during gather/firestarting. */
470 if (def.gatherEquipSprite)
471 { return equipImageName("item", folderKey, itemDefaultState(def)); }
472 return null;
473}
474
475/** Largest equipped light radius (e.g. lantern in offhand). */
476export function equippedLightRadiusTiles (equipment)
477{
478 let best = 0;
479 for (const stack of Object.values(equipment?.slots ?? {}))
480 {
481 if (!stack)
482 { continue; }
483 const radius = itemDefinition(stack.itemId)?.equipment?.lightRadiusTiles ?? 0;
484 if (radius > best)
485 { best = radius; }
486 }
487 return best;
488}
489
490export function letterVisualState (instance)
491{
492 return instance?.opened === false ? "letter-closed" : "letter-opened";
493}
494
495export function itemAssetPath (itemId, companion = null, instance = null)
496{
497 const def = itemDefinition(itemId);
498 if (def?.herb)
499 {
500 const suffix = companion ? `-${companion}` : "";
501 return `/assets/herbs/${def.key}${suffix}.png`;
502 }
503 const key = def?.iconKey ?? def?.key ?? "paper";
504 const state = itemId === ItemId.letter
505 ? letterVisualState(instance)
506 : itemDefaultState(def ?? { key });
507 const suffix = companion ? `-${companion}` : "";
508 return `/assets/items/${key}/${state}${suffix}.png`;
509}
510
511export function itemAssetPathForStack (stack)
512{
513 if (!stack)
514 { return itemAssetPath(null); }
515 if (stack.itemId === ItemId.coins)
516 {
517 const tier = coinSpriteTierForQuantity(stack.quantity);
518 return `/assets/items/coins/coins-${tier}.png`;
519 }
520 if (stack.itemId === ItemId.potion)
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.dye)
526 {
527 const fill = Math.max(1, Math.min(5, stack.instance?.fillLevel | 0 || 4));
528 return `/assets/items/potion/potion-${fill}.png`;
529 }
530 if (stack.itemId === ItemId.glassBottle)
531 { return `/assets/items/potion/potion-0.png`; }
532 if (stack.itemId === ItemId.flower)
533 { return `/assets/items/flower/flower.png`; }
534 if (stack.instance?.kind === "tipped-arrow")
535 { return `/assets/items/arrow/tipped-arrow.png`; }
536 return itemAssetPath(stack.itemId, null, stack.instance);
537}
538
539export const LETTER_MAX_BODY_CHARS = 2000;
540
541export const STARTER_ITEMS = Object.freeze([
542 Object.freeze({ itemId: ItemId.paper, quantity: 5 }),
543 Object.freeze({ itemId: ItemId.apple, quantity: 3 }),
544 Object.freeze({ itemId: ItemId.stick, quantity: 1 }),
545 Object.freeze({ itemId: ItemId.coins, quantity: 25 })
546]);