BlueGit

timfa questward

Files

questward / shared / definitions / items.js

items.js

items.js 13.4 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 baseDamage: 2
132 })
133 })
134 }),
135 [ItemId.blankCheque]: Object.freeze({
136 itemId: ItemId.blankCheque,
137 key: "blank-cheque",
138 displayName: "Blank cheque",
139 description: "Bank stationery for writing cheques against deposited goods.",
140 seedValue: 5,
141 stackable: true,
142 maxStackSize: DEFAULT_MAX_STACK,
143 uniqueInstance: false,
144 category: "document",
145 marketplaceSellable: false,
146 marketplaceSellBlockedReason: "Blank cheques cannot be sold on the marketplace."
147 }),
148 [ItemId.filledCheque]: Object.freeze({
149 itemId: ItemId.filledCheque,
150 key: "filled-cheque",
151 displayName: "Cheque",
152 description: "A bearer cheque linked to reserved goods in the issuer's bank.",
153 seedValue: 5,
154 stackable: false,
155 maxStackSize: 1,
156 uniqueInstance: true,
157 category: "document",
158 defaultState: "filled-cheque",
159 marketplaceSellable: true
160 }),
161 [ItemId.clothShirt]: Object.freeze({
162 itemId: ItemId.clothShirt,
163 key: "cloth-shirt",
164 displayName: "Cloth shirt",
165 description: "A simple cloth shirt, plain and practical.",
166 seedValue: 8,
167 stackable: false,
168 maxStackSize: 1,
169 uniqueInstance: false,
170 category: "finished",
171 iconKey: "clothing",
172 defaultState: "shirt",
173 dyeable: 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 { return "Notes on how to brew a particular draught."; }
373 return def.description ?? "";
374}
375
376export function itemOriginSummary (stack)
377{
378 return formatOriginBreakdown(stack);
379}
380
381export function itemDefaultState (def)
382{
383 return def?.defaultState ?? def?.key ?? "paper";
384}
385
386export function itemSpriteName (itemId, instance = null)
387{
388 const def = itemDefinition(itemId);
389 if (!def)
390 { return spriteImageName("item", "paper", "paper"); }
391 if (itemId === ItemId.letter)
392 { return spriteImageName("item", "letter", letterVisualState(instance)); }
393 const folderKey = def.iconKey ?? def.key;
394 return def.sprite ?? spriteImageName("item", folderKey, itemDefaultState(def));
395}
396
397export function itemEquipSprite (itemId)
398{
399 const def = itemDefinition(itemId);
400 const equipment = def?.equipment;
401 if (!equipment)
402 { return null; }
403 const folderKey = def.iconKey ?? def.key;
404 return equipment.equipSprite
405 ?? equipment.overlaySpriteId
406 ?? equipImageName("item", folderKey, itemDefaultState(def));
407}
408
409/** Largest equipped light radius (e.g. lantern in offhand). */
410export function equippedLightRadiusTiles (equipment)
411{
412 let best = 0;
413 for (const stack of Object.values(equipment?.slots ?? {}))
414 {
415 if (!stack)
416 { continue; }
417 const radius = itemDefinition(stack.itemId)?.equipment?.lightRadiusTiles ?? 0;
418 if (radius > best)
419 { best = radius; }
420 }
421 return best;
422}
423
424export function letterVisualState (instance)
425{
426 return instance?.opened === false ? "letter-closed" : "letter-opened";
427}
428
429export function itemAssetPath (itemId, companion = null, instance = null)
430{
431 const def = itemDefinition(itemId);
432 if (def?.herb)
433 {
434 const suffix = companion ? `-${companion}` : "";
435 return `/assets/herbs/${def.key}${suffix}.png`;
436 }
437 const key = def?.iconKey ?? def?.key ?? "paper";
438 const state = itemId === ItemId.letter
439 ? letterVisualState(instance)
440 : itemDefaultState(def ?? { key });
441 const suffix = companion ? `-${companion}` : "";
442 return `/assets/items/${key}/${state}${suffix}.png`;
443}
444
445export function itemAssetPathForStack (stack)
446{
447 if (!stack)
448 { return itemAssetPath(null); }
449 if (stack.itemId === ItemId.coins)
450 {
451 const tier = coinSpriteTierForQuantity(stack.quantity);
452 return `/assets/items/coins/coins-${tier}.png`;
453 }
454 if (stack.itemId === ItemId.potion)
455 {
456 const fill = Math.max(1, Math.min(5, stack.instance?.fillLevel | 0 || 4));
457 return `/assets/items/potion/potion-${fill}.png`;
458 }
459 if (stack.itemId === ItemId.dye)
460 {
461 const fill = Math.max(1, Math.min(5, stack.instance?.fillLevel | 0 || 4));
462 return `/assets/items/potion/potion-${fill}.png`;
463 }
464 if (stack.itemId === ItemId.glassBottle)
465 { return `/assets/items/potion/potion-0.png`; }
466 if (stack.itemId === ItemId.flower)
467 { return `/assets/items/flower/flower.png`; }
468 return itemAssetPath(stack.itemId, null, stack.instance);
469}
470
471export const LETTER_MAX_BODY_CHARS = 2000;
472
473export const STARTER_ITEMS = Object.freeze([
474 Object.freeze({ itemId: ItemId.paper, quantity: 5 }),
475 Object.freeze({ itemId: ItemId.apple, quantity: 3 }),
476 Object.freeze({ itemId: ItemId.stick, quantity: 1 }),
477 Object.freeze({ itemId: ItemId.clothShirt, quantity: 1 }),
478 Object.freeze({ itemId: ItemId.coins, quantity: 25 })
479]);