BlueGit

timfa questward

Files

questward / shared / definitions / items.js

items.js

items.js 14.2 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 { POTION_MAX_STACK } from "./alchemy.js";
15
16export const ItemId = Object.freeze({
17 paper: 1,
18 apple: 2,
19 stick: 3,
20 blankCheque: 4,
21 clothShirt: 5,
22 letter: 6,
23 filledCheque: 7,
24 coins: 8,
25 ...CraftingItemId,
26 lantern: 26,
27 bones: 27,
28 skull: 28,
29 glassBottle: 29,
30 greenHerb: 30,
31 redBeet: 31,
32 potion: 32,
33 recipePaper: 33,
34 bronzePlantClippers: 34,
35 ...MetalItemId,
36 ...WoodItemId,
37 ...ArmourItemId,
38 ...ClothItemId,
39 ...DyeItemId
40});
41
42export const EQUIPMENT_SLOT = Object.freeze({
43 head: "head",
44 chest: "chest",
45 legs: "legs",
46 boots: "boots",
47 weapon: "weapon",
48 offhand: "offhand"
49});
50
51export const EQUIPMENT_DRAW_ORDER = Object.freeze([
52 EQUIPMENT_SLOT.boots,
53 EQUIPMENT_SLOT.legs,
54 EQUIPMENT_SLOT.chest,
55 EQUIPMENT_SLOT.head,
56 EQUIPMENT_SLOT.offhand,
57 EQUIPMENT_SLOT.weapon
58]);
59
60export const DEFAULT_MAX_STACK = 99;
61export const BLANK_CHEQUE_CARRY_CAP = 99;
62export const COIN_MAX_STACK = 999_000_000_000;
63
64/** Coin sprite tiers (coins-<n>.png); pick the largest n where stack quantity >= n. */
65export const COIN_SPRITE_TIERS = Object.freeze([
66 1, 5, 15, 50, 150, 500, 1000, 1500, 2000, 5000, 10000, 20000, 50000,
67 1_000_000, 10_000_000, 50_000_000, 100_000_000, 500_000_000,
68 1_000_000_000, 10_000_000_000, 100_000_000_000, 1_000_000_000_000,
69 10_000_000_000_000, 100_000_000_000_000, 500_000_000_000_000,
70 999_999_999_999_999
71]);
72
73export const ITEM_DEFINITIONS = Object.freeze({
74 [ItemId.paper]: Object.freeze({
75 itemId: ItemId.paper,
76 key: "paper",
77 displayName: "Paper",
78 description: "Blank sheet of paper.",
79 seedValue: 2,
80 stackable: true,
81 maxStackSize: DEFAULT_MAX_STACK,
82 uniqueInstance: false,
83 category: "document"
84 }),
85 [ItemId.letter]: Object.freeze({
86 itemId: ItemId.letter,
87 key: "letter",
88 displayName: "Letter",
89 description: "A written letter.",
90 seedValue: 2,
91 stackable: false,
92 maxStackSize: 1,
93 uniqueInstance: true,
94 category: "document",
95 defaultState: "letter-opened"
96 }),
97 [ItemId.apple]: Object.freeze({
98 itemId: ItemId.apple,
99 key: "apple",
100 displayName: "Apple",
101 description: "Round and juicy!",
102 seedValue: 1,
103 stackable: true,
104 maxStackSize: DEFAULT_MAX_STACK,
105 uniqueInstance: false,
106 category: "food",
107 healing: 4
108 }),
109 [ItemId.stick]: Object.freeze({
110 itemId: ItemId.stick,
111 key: "stick",
112 displayName: "Stick",
113 description: "A wooden stick. Simple and sturdy.",
114 seedValue: 3,
115 stackable: false,
116 maxStackSize: 1,
117 uniqueInstance: false,
118 category: "finished",
119 equipment: Object.freeze({
120 slot: EQUIPMENT_SLOT.weapon,
121 tier: MaterialTier.bronze,
122 kind: EquipmentKind.clubWeapon,
123 attack: Object.freeze({
124 mode: "melee",
125 rangeTiles: 1,
126 hitSkillId: SkillId.clubfighting,
127 damageSkillId: SkillId.strength,
128 attackType: AttackType.blunt,
129 cooldownMs: 700,
130 baseDamage: 2
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 clothing: Object.freeze({ warm: false, style: true }),
174 equipment: Object.freeze({
175 slot: EQUIPMENT_SLOT.chest,
176 tier: MaterialTier.bronze,
177 kind: EquipmentKind.clothArmour,
178 equipSprite: equipImageName("item", "clothing", "shirt")
179 })
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.greenHerb]: Object.freeze({
245 itemId: ItemId.greenHerb,
246 key: "green-herb",
247 displayName: "Green herb",
248 description: "A fragrant common herb.",
249 seedValue: 2,
250 stackable: true,
251 maxStackSize: DEFAULT_MAX_STACK,
252 uniqueInstance: false,
253 category: "ingredient"
254 }),
255 [ItemId.redBeet]: Object.freeze({
256 itemId: ItemId.redBeet,
257 key: "red-beet",
258 displayName: "Red beet",
259 description: "A root vegetable.",
260 seedValue: 1,
261 stackable: true,
262 maxStackSize: DEFAULT_MAX_STACK,
263 uniqueInstance: false,
264 category: "ingredient"
265 }),
266 [ItemId.potion]: Object.freeze({
267 itemId: ItemId.potion,
268 key: "potion",
269 displayName: "Potion",
270 description: "A bottled brew. Sip with care.",
271 seedValue: 8,
272 stackable: true,
273 maxStackSize: POTION_MAX_STACK,
274 uniqueInstance: false,
275 category: "potion",
276 potion: true,
277 iconKey: "potion"
278 }),
279 [ItemId.recipePaper]: Object.freeze({
280 itemId: ItemId.recipePaper,
281 key: "recipe-paper",
282 displayName: "Recipe paper",
283 description: "An alchemy recipe written for a cauldron.",
284 seedValue: 2,
285 stackable: false,
286 maxStackSize: 1,
287 uniqueInstance: true,
288 category: "document",
289 defaultState: "recipe-paper",
290 iconKey: "paper"
291 }),
292 [ItemId.bronzePlantClippers]: Object.freeze({
293 itemId: ItemId.bronzePlantClippers,
294 key: "bronze-plant-clippers",
295 displayName: "Bronze plant clippers",
296 description: "Fine shears for cutting herbs and blooms.",
297 seedValue: 10,
298 stackable: false,
299 maxStackSize: 1,
300 uniqueInstance: false,
301 category: "tool",
302 npcShopAllowed: true,
303 equipment: Object.freeze({
304 slot: EQUIPMENT_SLOT.weapon,
305 tier: MaterialTier.bronze,
306 kind: EquipmentKind.plantClippers,
307 tintFromTier: true
308 })
309 }),
310 ...CRAFTING_ITEM_STUBS,
311 ...METAL_ITEM_DEFINITIONS,
312 ...WOOD_ITEM_DEFINITIONS,
313 ...ARMOUR_ITEM_DEFINITIONS,
314 ...CLOTH_ITEM_DEFINITIONS,
315 [DyeItemId.flower]: FLOWER_ITEM_DEFINITION,
316 [DyeItemId.dye]: DYE_ITEM_DEFINITION
317});
318
319export function itemDefinition (itemId)
320{
321 return ITEM_DEFINITIONS[itemId] ?? null;
322}
323
324/** Garments that accept optional `instance.dyeColor` (tailor / cloth clothing). */
325export function isDyeableClothing (itemIdOrDef)
326{
327 const def = typeof itemIdOrDef === "number"
328 ? itemDefinition(itemIdOrDef)
329 : itemIdOrDef;
330 return !!def?.dyeable;
331}
332
333export { formatQuantity };
334
335export function itemDisplayName (stack, def = itemDefinition(stack?.itemId))
336{
337 if (!def)
338 { return "Item"; }
339 const qty = stack?.quantity ?? 1;
340 if (stack?.itemId === ItemId.coins)
341 { return qty === 1 ? (def.displayName ?? "Coin") : (def.displayNamePlural ?? "Coins"); }
342 if (stack?.itemId === ItemId.potion && stack.instance?.displayName)
343 { return stack.instance.displayName; }
344 if (stack?.itemId === ItemId.dye && stack.instance?.kind === "dye")
345 { return "Dye"; }
346 if (stack?.itemId === ItemId.flower && stack.instance?.colorId)
347 {
348 return `${FLOWER_COLOR_DISPLAY[stack.instance.colorId] ?? "Flower"} flower`;
349 }
350 if (stack?.itemId === ItemId.recipePaper && stack.instance?.displayName)
351 { return stack.instance.displayName; }
352 return def.displayName ?? "Item";
353}
354
355export function stackDisplayLabel (stack)
356{
357 const name = itemDisplayName(stack);
358 const qty = stack?.quantity ?? 1;
359 if (qty <= 1)
360 { return name; }
361 return `${name} (${formatQuantity(qty)})`;
362}
363
364export function coinSpriteTierForQuantity (quantity)
365{
366 const qty = Math.max(0, Math.floor(Number(quantity) || 0));
367 let tier = COIN_SPRITE_TIERS[0];
368 for (const candidate of COIN_SPRITE_TIERS)
369 {
370 if (qty >= candidate)
371 { tier = candidate; }
372 else
373 { break; }
374 }
375 return tier;
376}
377
378/** Stack-aware description (letters include open state and author). */
379export function itemDescription (stack, { localCharacterId } = {})
380{
381 const def = itemDefinition(stack?.itemId);
382 if (!def)
383 { return ""; }
384 if (stack.itemId === ItemId.letter && stack.instance?.kind === "letter")
385 {
386 const state = stack.instance.opened === false ? "unopened" : "opened";
387 const authorId = stack.instance.authorCharacterId;
388 const byYou = authorId != null && Number(authorId) === Number(localCharacterId);
389 const author = byYou
390 ? "you"
391 : (stack.instance.authorDisplayName?.trim() || "someone");
392 return `An ${state} letter, written by ${author}.`;
393 }
394 if (stack.itemId === ItemId.filledCheque && stack.instance?.kind === "filled-cheque")
395 {
396 const reserved = itemDefinition(stack.instance.itemId);
397 const qty = stack.instance.quantity | 0;
398 const name = reserved?.displayName ?? "goods";
399 return `Bearer cheque for ${qty}× ${name}.`;
400 }
401 if (stack.itemId === ItemId.potion && stack.instance?.kind === "potion")
402 {
403 const name = stack.instance.displayName ?? "Potion";
404 return stack.instance.unstable
405 ? `${name}. The brew smells wrong — best sipped with caution.`
406 : `${name}. A carefully bottled draught.`;
407 }
408 if (stack.itemId === ItemId.recipePaper && stack.instance?.kind === "recipe-paper")
409 { return "Notes on how to brew a particular draught."; }
410 return def.description ?? "";
411}
412
413export function itemOriginSummary (stack)
414{
415 return formatOriginBreakdown(stack);
416}
417
418export function itemDefaultState (def)
419{
420 return def?.defaultState ?? def?.key ?? "paper";
421}
422
423export function itemSpriteName (itemId, instance = null)
424{
425 const def = itemDefinition(itemId);
426 if (!def)
427 { return spriteImageName("item", "paper", "paper"); }
428 if (itemId === ItemId.letter)
429 { return spriteImageName("item", "letter", letterVisualState(instance)); }
430 const folderKey = def.iconKey ?? def.key;
431 return def.sprite ?? spriteImageName("item", folderKey, itemDefaultState(def));
432}
433
434export function itemEquipSprite (itemId)
435{
436 const def = itemDefinition(itemId);
437 const equipment = def?.equipment;
438 if (!equipment)
439 { return null; }
440 const folderKey = def.iconKey ?? def.key;
441 return equipment.equipSprite
442 ?? equipment.overlaySpriteId
443 ?? equipImageName("item", folderKey, itemDefaultState(def));
444}
445
446/** Largest equipped light radius (e.g. lantern in offhand). */
447export function equippedLightRadiusTiles (equipment)
448{
449 let best = 0;
450 for (const stack of Object.values(equipment?.slots ?? {}))
451 {
452 if (!stack)
453 { continue; }
454 const radius = itemDefinition(stack.itemId)?.equipment?.lightRadiusTiles ?? 0;
455 if (radius > best)
456 { best = radius; }
457 }
458 return best;
459}
460
461export function letterVisualState (instance)
462{
463 return instance?.opened === false ? "letter-closed" : "letter-opened";
464}
465
466export function itemAssetPath (itemId, companion = null, instance = null)
467{
468 const def = itemDefinition(itemId);
469 const key = def?.iconKey ?? def?.key ?? "paper";
470 const state = itemId === ItemId.letter
471 ? letterVisualState(instance)
472 : itemDefaultState(def ?? { key });
473 const suffix = companion ? `-${companion}` : "";
474 return `/assets/items/${key}/${state}${suffix}.png`;
475}
476
477export function itemAssetPathForStack (stack)
478{
479 if (!stack)
480 { return itemAssetPath(null); }
481 if (stack.itemId === ItemId.coins)
482 {
483 const tier = coinSpriteTierForQuantity(stack.quantity);
484 return `/assets/items/coins/coins-${tier}.png`;
485 }
486 if (stack.itemId === ItemId.potion)
487 {
488 const fill = Math.max(1, Math.min(5, stack.instance?.fillLevel | 0 || 4));
489 return `/assets/items/potion/potion-${fill}.png`;
490 }
491 if (stack.itemId === ItemId.dye)
492 {
493 const fill = Math.max(1, Math.min(5, stack.instance?.fillLevel | 0 || 4));
494 return `/assets/items/potion/potion-${fill}.png`;
495 }
496 if (stack.itemId === ItemId.glassBottle)
497 { return `/assets/items/potion/potion-0.png`; }
498 if (stack.itemId === ItemId.flower)
499 { return `/assets/items/flower/flower.png`; }
500 return itemAssetPath(stack.itemId, null, stack.instance);
501}
502
503export const LETTER_MAX_BODY_CHARS = 2000;
504
505export const STARTER_ITEMS = Object.freeze([
506 Object.freeze({ itemId: ItemId.paper, quantity: 5 }),
507 Object.freeze({ itemId: ItemId.apple, quantity: 3 }),
508 Object.freeze({ itemId: ItemId.stick, quantity: 1 }),
509 Object.freeze({ itemId: ItemId.clothShirt, quantity: 1 }),
510 Object.freeze({ itemId: ItemId.coins, quantity: 25 })
511]);