BlueGit

timfa questward

Files

questward / shared / definitions / items.js

items.js

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