BlueGit

timfa questward

Files

questward / shared / definitions / items.js

items.js

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