BlueGit

timfa questward

Files

questward / shared / definitions / items.js

items.js

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