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, 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 /** Cotton shirt (ClothItemId.cottonShirt). */
26 clothShirt: 5,
27 letter: 6,
28 filledCheque: 7,
29 coins: 8,
30 ...CraftingItemId,
31 lantern: 26,
32 bones: 27,
33 skull: 28,
34 glassBottle: 29,
35 potion: 32,
36 recipePaper: 33,
37 egg: 275,
38 ...SpellbookItemId,
39 ...MetalItemId,
40 ...WoodItemId,
41 ...ArmourItemId,
42 ...ClothItemId,
43 ...LeatherItemId,
44 ...DyeItemId,
45 ...HerbItemId,
46 ...RangedItemId
47});
48
49export const EQUIPMENT_SLOT = Object.freeze({
50 head: "head",
51 chest: "chest",
52 legs: "legs",
53 boots: "boots",
54 weapon: "weapon",
55 offhand: "offhand"
56});
57
58/** Back → front. Weapon sits above chest but under legs (legs also cover shirt hem). */
59export const EQUIPMENT_DRAW_ORDER = Object.freeze([
60 EQUIPMENT_SLOT.boots,
61 EQUIPMENT_SLOT.chest,
62 EQUIPMENT_SLOT.weapon,
63 EQUIPMENT_SLOT.legs,
64 EQUIPMENT_SLOT.head,
65 EQUIPMENT_SLOT.offhand
66]);
67
68export const DEFAULT_MAX_STACK = 99;
69export const BLANK_CHEQUE_CARRY_CAP = 99;
70export const COIN_MAX_STACK = 999_000_000_000;
71
72/** Coin sprite tiers (coins-<n>.png); pick the largest n where stack quantity >= n. */
73export const COIN_SPRITE_TIERS = Object.freeze([
74 1, 5, 15, 50, 150, 500, 1000, 1500, 2000, 5000, 10000, 20000, 50000,
75 1_000_000, 10_000_000, 50_000_000, 100_000_000, 500_000_000,
76 1_000_000_000, 10_000_000_000, 100_000_000_000, 1_000_000_000_000,
77 10_000_000_000_000, 100_000_000_000_000, 500_000_000_000_000,
78 999_999_999_999_999
79]);
80
81export const ITEM_DEFINITIONS = Object.freeze({
82 [ItemId.paper]: Object.freeze({
83 itemId: ItemId.paper,
84 key: "paper",
85 displayName: "Paper",
86 description: "Blank sheet of paper.",
87 seedValue: 2,
88 stackable: true,
89 maxStackSize: DEFAULT_MAX_STACK,
90 uniqueInstance: false,
91 category: "document"
92 }),
93 [ItemId.letter]: Object.freeze({
94 itemId: ItemId.letter,
95 key: "letter",
96 displayName: "Letter",
97 description: "A written letter.",
98 seedValue: 2,
99 stackable: false,
100 maxStackSize: 1,
101 uniqueInstance: true,
102 category: "document",
103 defaultState: "letter-opened",
104 marketplaceSellable: false,
105 marketplaceSellBlockedReason: "Letters cannot be sold on the marketplace."
106 }),
107 [ItemId.apple]: Object.freeze({
108 itemId: ItemId.apple,
109 key: "apple",
110 displayName: "Apple",
111 description: "Round and juicy!",
112 seedValue: 1,
113 stackable: true,
114 maxStackSize: DEFAULT_MAX_STACK,
115 uniqueInstance: false,
116 category: "food",
117 healing: 4
118 }),
119 [ItemId.egg]: Object.freeze({
120 itemId: ItemId.egg,
121 key: "egg",
122 displayName: "Egg",
123 description: "A fresh egg, still warm from the nest.",
124 seedValue: 1,
125 stackable: true,
126 maxStackSize: DEFAULT_MAX_STACK,
127 uniqueInstance: false,
128 category: "food",
129 healing: 2
130 }),
131 [ItemId.stick]: Object.freeze({
132 itemId: ItemId.stick,
133 key: "stick",
134 displayName: "Stick",
135 description: "A wooden stick. Simple and sturdy.",
136 seedValue: 3,
137 stackable: false,
138 maxStackSize: 1,
139 uniqueInstance: false,
140 category: "finished",
141 equipment: Object.freeze({
142 slot: EQUIPMENT_SLOT.weapon,
143 tier: WoodTier.pine,
144 kind: EquipmentKind.clubWeapon,
145 attack: Object.freeze({
146 mode: "melee",
147 rangeTiles: 1,
148 hitSkillId: SkillId.clubfighting,
149 damageSkillId: SkillId.strength,
150 attackType: AttackType.blunt,
151 cooldownMs: 700
152 })
153 })
154 }),
155 [ItemId.blankCheque]: Object.freeze({
156 itemId: ItemId.blankCheque,
157 key: "blank-cheque",
158 displayName: "Blank cheque",
159 description: "Bank stationery for writing cheques against deposited goods.",
160 seedValue: 5,
161 stackable: true,
162 maxStackSize: DEFAULT_MAX_STACK,
163 uniqueInstance: false,
164 category: "document",
165 marketplaceSellable: false,
166 marketplaceSellBlockedReason: "Blank cheques cannot be sold on the marketplace."
167 }),
168 [ItemId.filledCheque]: Object.freeze({
169 itemId: ItemId.filledCheque,
170 key: "filled-cheque",
171 displayName: "Cheque",
172 description: "A bearer cheque linked to reserved goods in the issuer's bank.",
173 seedValue: 5,
174 stackable: false,
175 maxStackSize: 1,
176 uniqueInstance: true,
177 category: "document",
178 defaultState: "filled-cheque",
179 marketplaceSellable: true
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 ...LEATHER_ITEM_DEFINITIONS,
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 ...SPELLBOOK_ITEM_DEFINITIONS,
279 [DyeItemId.flower]: FLOWER_ITEM_DEFINITION,
280 [DyeItemId.dye]: DYE_ITEM_DEFINITION
281});
282
283export function itemDefinition (itemId)
284{
285 return ITEM_DEFINITIONS[itemId] ?? null;
286}
287
288/** Every defined item for editor pickers (shop stock, dev tools, …). */
289export function listAllItemPickerEntries ()
290{
291 return Object.values(ITEM_DEFINITIONS)
292 .filter((def) => def?.itemId != null)
293 .map((def) => ({
294 itemId: def.itemId,
295 displayName: def.displayName ?? String(def.itemId),
296 key: def.key ?? String(def.itemId)
297 }))
298 .sort((a, b) => a.displayName.localeCompare(b.displayName) || (a.itemId - b.itemId));
299}
300
301/** Garments that accept optional `instance.dyeColor` (tailor / cloth clothing). */
302export function isDyeableClothing (itemIdOrDef)
303{
304 const def = typeof itemIdOrDef === "number"
305 ? itemDefinition(itemIdOrDef)
306 : itemIdOrDef;
307 return !!def?.dyeable;
308}
309
310export { formatQuantity };
311
312export function itemDisplayName (stack, def = itemDefinition(stack?.itemId))
313{
314 if (!def)
315 { return "Item"; }
316 const qty = stack?.quantity ?? 1;
317 if (stack?.itemId === ItemId.coins)
318 { return qty === 1 ? (def.displayName ?? "Coin") : (def.displayNamePlural ?? "Coins"); }
319 if (stack?.itemId === ItemId.potion && stack.instance?.displayName)
320 { return stack.instance.displayName; }
321 if (stack?.itemId === ItemId.dye && stack.instance?.kind === "dye")
322 { return "Dye"; }
323 if (stack?.itemId === ItemId.flower && stack.instance?.colorId)
324 {
325 return `${FLOWER_COLOR_DISPLAY[stack.instance.colorId] ?? "Flower"} flower`;
326 }
327 if (stack?.itemId === ItemId.recipePaper && stack.instance?.displayName)
328 { return stack.instance.displayName; }
329 return def.displayName ?? "Item";
330}
331
332export function stackDisplayLabel (stack)
333{
334 const name = itemDisplayName(stack);
335 const qty = stack?.quantity ?? 1;
336 if (qty <= 1)
337 { return name; }
338 return `${name} (${formatQuantity(qty)})`;
339}
340
341export function coinSpriteTierForQuantity (quantity)
342{
343 const qty = Math.max(0, Math.floor(Number(quantity) || 0));
344 let tier = COIN_SPRITE_TIERS[0];
345 for (const candidate of COIN_SPRITE_TIERS)
346 {
347 if (qty >= candidate)
348 { tier = candidate; }
349 else
350 { break; }
351 }
352 return tier;
353}
354
355/** Stack-aware description (letters include open state and author). */
356export function itemDescription (stack, { localCharacterId } = {})
357{
358 const def = itemDefinition(stack?.itemId);
359 if (!def)
360 { return ""; }
361 if (stack.itemId === ItemId.letter && stack.instance?.kind === "letter")
362 {
363 const state = stack.instance.opened === false ? "unopened" : "opened";
364 const authorId = stack.instance.authorCharacterId;
365 const byYou = authorId != null && Number(authorId) === Number(localCharacterId);
366 const author = byYou
367 ? "you"
368 : (stack.instance.authorDisplayName?.trim() || "someone");
369 return `An ${state} letter, written by ${author}.`;
370 }
371 if (stack.itemId === ItemId.filledCheque && stack.instance?.kind === "filled-cheque")
372 {
373 const reserved = itemDefinition(stack.instance.itemId);
374 const qty = stack.instance.quantity | 0;
375 const name = reserved?.displayName ?? "goods";
376 return `Bearer cheque for ${qty}× ${name}.`;
377 }
378 if (stack.itemId === ItemId.potion && stack.instance?.kind === "potion")
379 {
380 const name = stack.instance.displayName ?? "Potion";
381 return stack.instance.unstable
382 ? `${name}. The brew smells wrong — best sipped with caution.`
383 : `${name}. A carefully bottled draught.`;
384 }
385 if (stack.itemId === ItemId.recipePaper && stack.instance?.kind === "recipe-paper")
386 {
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 `Written by ${author}.`;
393 }
394 return def.description ?? "";
395}
396
397export function itemOriginSummary (stack)
398{
399 return formatOriginBreakdown(stack);
400}
401
402export function itemDefaultState (def)
403{
404 return def?.defaultState ?? def?.key ?? "paper";
405}
406
407export function itemSpriteName (itemId, instance = null)
408{
409 const def = itemDefinition(itemId);
410 if (!def)
411 { return spriteImageName("item", "paper", "paper"); }
412 if (itemId === ItemId.letter)
413 { return spriteImageName("item", "letter", letterVisualState(instance)); }
414 const folderKey = def.iconKey ?? def.key;
415 return def.sprite ?? spriteImageName("item", folderKey, itemDefaultState(def));
416}
417
418export function itemEquipSprite (itemId)
419{
420 const def = itemDefinition(itemId);
421 const equipment = def?.equipment;
422 if (!equipment)
423 { return null; }
424 const folderKey = def.iconKey ?? def.key;
425 return equipment.equipSprite
426 ?? equipment.overlaySpriteId
427 ?? equipImageName("item", folderKey, itemDefaultState(def));
428}
429
430/** Largest equipped light radius (e.g. lantern in offhand). */
431export function equippedLightRadiusTiles (equipment)
432{
433 let best = 0;
434 for (const stack of Object.values(equipment?.slots ?? {}))
435 {
436 if (!stack)
437 { continue; }
438 const radius = itemDefinition(stack.itemId)?.equipment?.lightRadiusTiles ?? 0;
439 if (radius > best)
440 { best = radius; }
441 }
442 return best;
443}
444
445export function letterVisualState (instance)
446{
447 return instance?.opened === false ? "letter-closed" : "letter-opened";
448}
449
450export function itemAssetPath (itemId, companion = null, instance = null)
451{
452 const def = itemDefinition(itemId);
453 if (def?.herb)
454 {
455 const suffix = companion ? `-${companion}` : "";
456 return `/assets/herbs/${def.key}${suffix}.png`;
457 }
458 const key = def?.iconKey ?? def?.key ?? "paper";
459 const state = itemId === ItemId.letter
460 ? letterVisualState(instance)
461 : itemDefaultState(def ?? { key });
462 const suffix = companion ? `-${companion}` : "";
463 return `/assets/items/${key}/${state}${suffix}.png`;
464}
465
466export function itemAssetPathForStack (stack)
467{
468 if (!stack)
469 { return itemAssetPath(null); }
470 if (stack.itemId === ItemId.coins)
471 {
472 const tier = coinSpriteTierForQuantity(stack.quantity);
473 return `/assets/items/coins/coins-${tier}.png`;
474 }
475 if (stack.itemId === ItemId.potion)
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.dye)
481 {
482 const fill = Math.max(1, Math.min(5, stack.instance?.fillLevel | 0 || 4));
483 return `/assets/items/potion/potion-${fill}.png`;
484 }
485 if (stack.itemId === ItemId.glassBottle)
486 { return `/assets/items/potion/potion-0.png`; }
487 if (stack.itemId === ItemId.flower)
488 { return `/assets/items/flower/flower.png`; }
489 return itemAssetPath(stack.itemId, null, stack.instance);
490}
491
492export const LETTER_MAX_BODY_CHARS = 2000;
493
494export const STARTER_ITEMS = Object.freeze([
495 Object.freeze({ itemId: ItemId.paper, quantity: 5 }),
496 Object.freeze({ itemId: ItemId.apple, quantity: 3 }),
497 Object.freeze({ itemId: ItemId.stick, quantity: 1 }),
498 Object.freeze({ itemId: ItemId.coins, quantity: 25 })
499]);