BlueGit

timfa questward

Files

questward / shared / definitions / items.js

items.js

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