BlueGit

timfa questward

Files

questward / shared / definitions / items.js

items.js

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