BlueGit

timfa questward

Files

questward / shared / definitions / items.js

items.js

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