BlueGit

timfa questward

Files

questward / shared / definitions / items.js

items.js

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