BlueGit

timfa questward

Files

questward / shared / definitions / items.js

items.js

items.js 12.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 { POTION_MAX_STACK } from "./alchemy.js";
10
11export const ItemId = Object.freeze({
12 paper: 1,
13 apple: 2,
14 stick: 3,
15 blankCheque: 4,
16 clothShirt: 5,
17 letter: 6,
18 filledCheque: 7,
19 coins: 8,
20 ...CraftingItemId,
21 lantern: 26,
22 bones: 27,
23 skull: 28,
24 glassBottle: 29,
25 greenHerb: 30,
26 redBeet: 31,
27 potion: 32,
28 recipePaper: 33,
29 bronzePlantClippers: 34
30});
31
32export const EQUIPMENT_SLOT = Object.freeze({
33 head: "head",
34 chest: "chest",
35 legs: "legs",
36 boots: "boots",
37 weapon: "weapon",
38 offhand: "offhand"
39});
40
41export const EQUIPMENT_DRAW_ORDER = Object.freeze([
42 EQUIPMENT_SLOT.boots,
43 EQUIPMENT_SLOT.legs,
44 EQUIPMENT_SLOT.chest,
45 EQUIPMENT_SLOT.head,
46 EQUIPMENT_SLOT.offhand,
47 EQUIPMENT_SLOT.weapon
48]);
49
50export const DEFAULT_MAX_STACK = 99;
51export const BLANK_CHEQUE_CARRY_CAP = 99;
52export const COIN_MAX_STACK = 999_000_000_000;
53
54/** Coin sprite tiers (coins-<n>.png); pick the largest n where stack quantity >= n. */
55export const COIN_SPRITE_TIERS = Object.freeze([
56 1, 5, 15, 50, 150, 500, 1000, 1500, 2000, 5000, 10000, 20000, 50000,
57 1_000_000, 10_000_000, 50_000_000, 100_000_000, 500_000_000,
58 1_000_000_000, 10_000_000_000, 100_000_000_000, 1_000_000_000_000,
59 10_000_000_000_000, 100_000_000_000_000, 500_000_000_000_000,
60 999_999_999_999_999
61]);
62
63export const ITEM_DEFINITIONS = Object.freeze({
64 [ItemId.paper]: Object.freeze({
65 itemId: ItemId.paper,
66 key: "paper",
67 displayName: "Paper",
68 description: "Blank paper. Right-click to write a letter.",
69 seedValue: 2,
70 stackable: true,
71 maxStackSize: DEFAULT_MAX_STACK,
72 uniqueInstance: false,
73 category: "document"
74 }),
75 [ItemId.letter]: Object.freeze({
76 itemId: ItemId.letter,
77 key: "letter",
78 displayName: "Letter",
79 description: "A written letter.",
80 seedValue: 2,
81 stackable: false,
82 maxStackSize: 1,
83 uniqueInstance: true,
84 category: "document",
85 defaultState: "letter-opened"
86 }),
87 [ItemId.apple]: Object.freeze({
88 itemId: ItemId.apple,
89 key: "apple",
90 displayName: "Apple",
91 description: "Restores a little health when eaten.",
92 seedValue: 1,
93 stackable: true,
94 maxStackSize: DEFAULT_MAX_STACK,
95 uniqueInstance: false,
96 category: "food",
97 healing: 4
98 }),
99 [ItemId.stick]: Object.freeze({
100 itemId: ItemId.stick,
101 key: "stick",
102 displayName: "Stick",
103 description: "A wooden stick. Can be wielded.",
104 seedValue: 3,
105 stackable: false,
106 maxStackSize: 1,
107 uniqueInstance: false,
108 category: "finished",
109 equipment: Object.freeze({
110 slot: EQUIPMENT_SLOT.weapon,
111 tier: MaterialTier.bronze,
112 kind: EquipmentKind.clubWeapon,
113 attack: Object.freeze({
114 mode: "melee",
115 rangeTiles: 1,
116 hitSkillId: SkillId.clubfighting,
117 damageSkillId: SkillId.strength,
118 attackType: AttackType.blunt,
119 cooldownMs: 700,
120 baseDamage: 2
121 })
122 })
123 }),
124 [ItemId.blankCheque]: Object.freeze({
125 itemId: ItemId.blankCheque,
126 key: "blank-cheque",
127 displayName: "Blank cheque",
128 description: "Free bank paper. Write a cheque at a bank to reserve vault goods.",
129 seedValue: 5,
130 stackable: true,
131 maxStackSize: DEFAULT_MAX_STACK,
132 uniqueInstance: false,
133 category: "document",
134 marketplaceSellable: false,
135 marketplaceSellBlockedReason: "Blank cheques cannot be sold on the marketplace."
136 }),
137 [ItemId.filledCheque]: Object.freeze({
138 itemId: ItemId.filledCheque,
139 key: "filled-cheque",
140 displayName: "Cheque",
141 description: "A bearer cheque linked to reserved goods in the issuer's bank.",
142 seedValue: 5,
143 stackable: false,
144 maxStackSize: 1,
145 uniqueInstance: true,
146 category: "document",
147 defaultState: "filled-cheque",
148 marketplaceSellable: true
149 }),
150 [ItemId.clothShirt]: Object.freeze({
151 itemId: ItemId.clothShirt,
152 key: "cloth-shirt",
153 displayName: "Cloth shirt",
154 description: "A placeholder shirt worn on the chest.",
155 seedValue: 8,
156 stackable: false,
157 maxStackSize: 1,
158 uniqueInstance: false,
159 category: "finished",
160 equipment: Object.freeze({
161 slot: EQUIPMENT_SLOT.chest,
162 tier: MaterialTier.bronze,
163 kind: EquipmentKind.clothArmour
164 })
165 }),
166 [ItemId.coins]: Object.freeze({
167 itemId: ItemId.coins,
168 key: "coins",
169 displayName: "Coin",
170 displayNamePlural: "Coins",
171 description: "Currency carried in your bag.",
172 seedValue: 1,
173 stackable: true,
174 maxStackSize: COIN_MAX_STACK,
175 uniqueInstance: false,
176 category: "currency"
177 }),
178 [ItemId.lantern]: Object.freeze({
179 itemId: ItemId.lantern,
180 key: "lantern",
181 displayName: "Lantern",
182 description: "A handheld light source. Equip in the offhand to illuminate nearby tiles at night.",
183 seedValue: 12,
184 stackable: false,
185 maxStackSize: 1,
186 uniqueInstance: false,
187 category: "tool",
188 equipment: Object.freeze({
189 slot: EQUIPMENT_SLOT.offhand,
190 lightRadiusTiles: 6
191 })
192 }),
193 [ItemId.bones]: Object.freeze({
194 itemId: ItemId.bones,
195 key: "bones",
196 displayName: "Bones",
197 description: "The remains of a fallen adventurer.",
198 seedValue: 1,
199 stackable: true,
200 maxStackSize: DEFAULT_MAX_STACK,
201 uniqueInstance: false,
202 category: "ingredient"
203 }),
204 [ItemId.skull]: Object.freeze({
205 itemId: ItemId.skull,
206 key: "skull",
207 displayName: "Skull",
208 description: "A grim reminder of an adventurer's death.",
209 seedValue: 1,
210 stackable: true,
211 maxStackSize: DEFAULT_MAX_STACK,
212 uniqueInstance: false,
213 category: "misc"
214 }),
215 [ItemId.glassBottle]: Object.freeze({
216 itemId: ItemId.glassBottle,
217 key: "glass-bottle",
218 displayName: "Glass bottle",
219 description: "An empty bottle for alchemy. Used when bottling a cauldron brew.",
220 seedValue: 2,
221 stackable: true,
222 maxStackSize: DEFAULT_MAX_STACK,
223 uniqueInstance: false,
224 category: "tool"
225 }),
226 [ItemId.greenHerb]: Object.freeze({
227 itemId: ItemId.greenHerb,
228 key: "green-herb",
229 displayName: "Green herb",
230 description: "A common herb. Gathered with plant clippers (Herbalism). Used in alchemy.",
231 seedValue: 2,
232 stackable: true,
233 maxStackSize: DEFAULT_MAX_STACK,
234 uniqueInstance: false,
235 category: "ingredient"
236 }),
237 [ItemId.redBeet]: Object.freeze({
238 itemId: ItemId.redBeet,
239 key: "red-beet",
240 displayName: "Red beet",
241 description: "A root vegetable used in some alchemy recipes.",
242 seedValue: 1,
243 stackable: true,
244 maxStackSize: DEFAULT_MAX_STACK,
245 uniqueInstance: false,
246 category: "ingredient"
247 }),
248 [ItemId.potion]: Object.freeze({
249 itemId: ItemId.potion,
250 key: "potion",
251 displayName: "Potion",
252 description: "A bottled brew. Drink to apply its effect (effects TBD).",
253 seedValue: 8,
254 stackable: true,
255 maxStackSize: POTION_MAX_STACK,
256 uniqueInstance: false,
257 category: "potion",
258 potion: true,
259 iconKey: "potion"
260 }),
261 [ItemId.recipePaper]: Object.freeze({
262 itemId: ItemId.recipePaper,
263 key: "recipe-paper",
264 displayName: "Recipe paper",
265 description: "An alchemy recipe you wrote down. Read like a letter.",
266 seedValue: 2,
267 stackable: false,
268 maxStackSize: 1,
269 uniqueInstance: true,
270 category: "document",
271 defaultState: "recipe-paper",
272 iconKey: "paper"
273 }),
274 [ItemId.bronzePlantClippers]: Object.freeze({
275 itemId: ItemId.bronzePlantClippers,
276 key: "bronze-plant-clippers",
277 displayName: "Bronze plant clippers",
278 description: "Herbalism tool. Tier sets minimum Herbalism to equip; better tiers gather faster (later).",
279 seedValue: 10,
280 stackable: false,
281 maxStackSize: 1,
282 uniqueInstance: false,
283 category: "tool",
284 equipment: Object.freeze({
285 slot: EQUIPMENT_SLOT.weapon,
286 tier: MaterialTier.bronze,
287 kind: EquipmentKind.plantClippers,
288 tintFromTier: true
289 })
290 }),
291 ...CRAFTING_ITEM_STUBS
292});
293
294export function itemDefinition (itemId)
295{
296 return ITEM_DEFINITIONS[itemId] ?? null;
297}
298
299export { formatQuantity };
300
301export function itemDisplayName (stack, def = itemDefinition(stack?.itemId))
302{
303 if (!def)
304 { return "Item"; }
305 const qty = stack?.quantity ?? 1;
306 if (stack?.itemId === ItemId.coins)
307 { return qty === 1 ? (def.displayName ?? "Coin") : (def.displayNamePlural ?? "Coins"); }
308 if (stack?.itemId === ItemId.potion && stack.instance?.displayName)
309 { return stack.instance.displayName; }
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}. An unstable brew — effects are unpredictable.`
366 : `${name}. Drink to apply its effect (effects TBD).`;
367 }
368 if (stack.itemId === ItemId.recipePaper && stack.instance?.kind === "recipe-paper")
369 { return "An alchemy recipe you wrote down. Read like a letter."; }
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 const key = def?.iconKey ?? def?.key ?? "paper";
430 const state = itemId === ItemId.letter
431 ? letterVisualState(instance)
432 : itemDefaultState(def ?? { key });
433 const suffix = companion ? `-${companion}` : "";
434 return `/assets/items/${key}/${state}${suffix}.png`;
435}
436
437export function itemAssetPathForStack (stack)
438{
439 if (!stack)
440 { return itemAssetPath(null); }
441 if (stack.itemId === ItemId.coins)
442 {
443 const tier = coinSpriteTierForQuantity(stack.quantity);
444 return `/assets/items/coins/coins-${tier}.png`;
445 }
446 if (stack.itemId === ItemId.potion)
447 {
448 const fill = Math.max(1, Math.min(5, stack.instance?.fillLevel | 0 || 4));
449 return `/assets/items/potion/potion-${fill}.png`;
450 }
451 if (stack.itemId === ItemId.glassBottle)
452 { return `/assets/items/glass-bottle/glass-bottle.png`; }
453 return itemAssetPath(stack.itemId, null, stack.instance);
454}
455
456export const LETTER_MAX_BODY_CHARS = 2000;
457
458export const STARTER_ITEMS = Object.freeze([
459 Object.freeze({ itemId: ItemId.paper, quantity: 5 }),
460 Object.freeze({ itemId: ItemId.apple, quantity: 3 }),
461 Object.freeze({ itemId: ItemId.stick, quantity: 1 }),
462 Object.freeze({ itemId: ItemId.clothShirt, quantity: 1 }),
463 Object.freeze({ itemId: ItemId.coins, quantity: 25 })
464]);