BlueGit

timfa questward

Files

questward / shared / definitions / items.js

items.js

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