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