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 })
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.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 ...METAL_ITEM_DEFINITIONS,
272 ...WOOD_ITEM_DEFINITIONS,
273 ...ARMOUR_ITEM_DEFINITIONS,
274 ...CLOTH_ITEM_DEFINITIONS,
275 ...HERB_ITEM_DEFINITIONS,
276 ...RANGED_ITEM_DEFINITIONS,
277 [DyeItemId.flower]: FLOWER_ITEM_DEFINITION,
278 [DyeItemId.dye]: DYE_ITEM_DEFINITION
279});
280
281export function itemDefinition (itemId)
282{
283 return ITEM_DEFINITIONS[itemId] ?? null;
284}
285
286/** Garments that accept optional `instance.dyeColor` (tailor / cloth clothing). */
287export function isDyeableClothing (itemIdOrDef)
288{
289 const def = typeof itemIdOrDef === "number"
290 ? itemDefinition(itemIdOrDef)
291 : itemIdOrDef;
292 return !!def?.dyeable;
293}
294
295export { formatQuantity };
296
297export function itemDisplayName (stack, def = itemDefinition(stack?.itemId))
298{
299 if (!def)
300 { return "Item"; }
301 const qty = stack?.quantity ?? 1;
302 if (stack?.itemId === ItemId.coins)
303 { return qty === 1 ? (def.displayName ?? "Coin") : (def.displayNamePlural ?? "Coins"); }
304 if (stack?.itemId === ItemId.potion && stack.instance?.displayName)
305 { return stack.instance.displayName; }
306 if (stack?.itemId === ItemId.dye && stack.instance?.kind === "dye")
307 { return "Dye"; }
308 if (stack?.itemId === ItemId.flower && stack.instance?.colorId)
309 {
310 return `${FLOWER_COLOR_DISPLAY[stack.instance.colorId] ?? "Flower"} flower`;
311 }
312 if (stack?.itemId === ItemId.recipePaper && stack.instance?.displayName)
313 { return stack.instance.displayName; }
314 return def.displayName ?? "Item";
315}
316
317export function stackDisplayLabel (stack)
318{
319 const name = itemDisplayName(stack);
320 const qty = stack?.quantity ?? 1;
321 if (qty <= 1)
322 { return name; }
323 return `${name} (${formatQuantity(qty)})`;
324}
325
326export function coinSpriteTierForQuantity (quantity)
327{
328 const qty = Math.max(0, Math.floor(Number(quantity) || 0));
329 let tier = COIN_SPRITE_TIERS[0];
330 for (const candidate of COIN_SPRITE_TIERS)
331 {
332 if (qty >= candidate)
333 { tier = candidate; }
334 else
335 { break; }
336 }
337 return tier;
338}
339
340/** Stack-aware description (letters include open state and author). */
341export function itemDescription (stack, { localCharacterId } = {})
342{
343 const def = itemDefinition(stack?.itemId);
344 if (!def)
345 { return ""; }
346 if (stack.itemId === ItemId.letter && stack.instance?.kind === "letter")
347 {
348 const state = stack.instance.opened === false ? "unopened" : "opened";
349 const authorId = stack.instance.authorCharacterId;
350 const byYou = authorId != null && Number(authorId) === Number(localCharacterId);
351 const author = byYou
352 ? "you"
353 : (stack.instance.authorDisplayName?.trim() || "someone");
354 return `An ${state} letter, written by ${author}.`;
355 }
356 if (stack.itemId === ItemId.filledCheque && stack.instance?.kind === "filled-cheque")
357 {
358 const reserved = itemDefinition(stack.instance.itemId);
359 const qty = stack.instance.quantity | 0;
360 const name = reserved?.displayName ?? "goods";
361 return `Bearer cheque for ${qty}× ${name}.`;
362 }
363 if (stack.itemId === ItemId.potion && stack.instance?.kind === "potion")
364 {
365 const name = stack.instance.displayName ?? "Potion";
366 return stack.instance.unstable
367 ? `${name}. The brew smells wrong — best sipped with caution.`
368 : `${name}. A carefully bottled draught.`;
369 }
370 if (stack.itemId === ItemId.recipePaper && stack.instance?.kind === "recipe-paper")
371 { return "Notes on how to brew a particular draught."; }
372 return def.description ?? "";
373}
374
375export function itemOriginSummary (stack)
376{
377 return formatOriginBreakdown(stack);
378}
379
380export function itemDefaultState (def)
381{
382 return def?.defaultState ?? def?.key ?? "paper";
383}
384
385export function itemSpriteName (itemId, instance = null)
386{
387 const def = itemDefinition(itemId);
388 if (!def)
389 { return spriteImageName("item", "paper", "paper"); }
390 if (itemId === ItemId.letter)
391 { return spriteImageName("item", "letter", letterVisualState(instance)); }
392 const folderKey = def.iconKey ?? def.key;
393 return def.sprite ?? spriteImageName("item", folderKey, itemDefaultState(def));
394}
395
396export function itemEquipSprite (itemId)
397{
398 const def = itemDefinition(itemId);
399 const equipment = def?.equipment;
400 if (!equipment)
401 { return null; }
402 const folderKey = def.iconKey ?? def.key;
403 return equipment.equipSprite
404 ?? equipment.overlaySpriteId
405 ?? equipImageName("item", folderKey, itemDefaultState(def));
406}
407
408/** Largest equipped light radius (e.g. lantern in offhand). */
409export function equippedLightRadiusTiles (equipment)
410{
411 let best = 0;
412 for (const stack of Object.values(equipment?.slots ?? {}))
413 {
414 if (!stack)
415 { continue; }
416 const radius = itemDefinition(stack.itemId)?.equipment?.lightRadiusTiles ?? 0;
417 if (radius > best)
418 { best = radius; }
419 }
420 return best;
421}
422
423export function letterVisualState (instance)
424{
425 return instance?.opened === false ? "letter-closed" : "letter-opened";
426}
427
428export function itemAssetPath (itemId, companion = null, instance = null)
429{
430 const def = itemDefinition(itemId);
431 if (def?.herb)
432 {
433 const suffix = companion ? `-${companion}` : "";
434 return `/assets/herbs/${def.key}${suffix}.png`;
435 }
436 const key = def?.iconKey ?? def?.key ?? "paper";
437 const state = itemId === ItemId.letter
438 ? letterVisualState(instance)
439 : itemDefaultState(def ?? { key });
440 const suffix = companion ? `-${companion}` : "";
441 return `/assets/items/${key}/${state}${suffix}.png`;
442}
443
444export function itemAssetPathForStack (stack)
445{
446 if (!stack)
447 { return itemAssetPath(null); }
448 if (stack.itemId === ItemId.coins)
449 {
450 const tier = coinSpriteTierForQuantity(stack.quantity);
451 return `/assets/items/coins/coins-${tier}.png`;
452 }
453 if (stack.itemId === ItemId.potion)
454 {
455 const fill = Math.max(1, Math.min(5, stack.instance?.fillLevel | 0 || 4));
456 return `/assets/items/potion/potion-${fill}.png`;
457 }
458 if (stack.itemId === ItemId.dye)
459 {
460 const fill = Math.max(1, Math.min(5, stack.instance?.fillLevel | 0 || 4));
461 return `/assets/items/potion/potion-${fill}.png`;
462 }
463 if (stack.itemId === ItemId.glassBottle)
464 { return `/assets/items/potion/potion-0.png`; }
465 if (stack.itemId === ItemId.flower)
466 { return `/assets/items/flower/flower.png`; }
467 return itemAssetPath(stack.itemId, null, stack.instance);
468}
469
470export const LETTER_MAX_BODY_CHARS = 2000;
471
472export const STARTER_ITEMS = Object.freeze([
473 Object.freeze({ itemId: ItemId.paper, quantity: 5 }),
474 Object.freeze({ itemId: ItemId.apple, quantity: 3 }),
475 Object.freeze({ itemId: ItemId.stick, quantity: 1 }),
476 Object.freeze({ itemId: ItemId.clothShirt, quantity: 1 }),
477 Object.freeze({ itemId: ItemId.coins, quantity: 25 })
478]);