BlueGit

timfa questward

Files

questward / shared / definitions / items.js

items.js

items.js 12.7 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 })
289 }),
290 ...CRAFTING_ITEM_STUBS
291});
292
293export function itemDefinition (itemId)
294{
295 return ITEM_DEFINITIONS[itemId] ?? null;
296}
297
298export { formatQuantity };
299
300export function itemDisplayName (stack, def = itemDefinition(stack?.itemId))
301{
302 if (!def)
303 { return "Item"; }
304 const qty = stack?.quantity ?? 1;
305 if (stack?.itemId === ItemId.coins)
306 { return qty === 1 ? (def.displayName ?? "Coin") : (def.displayNamePlural ?? "Coins"); }
307 if (stack?.itemId === ItemId.potion && stack.instance?.displayName)
308 { return stack.instance.displayName; }
309 if (stack?.itemId === ItemId.recipePaper && stack.instance?.displayName)
310 { return stack.instance.displayName; }
311 return def.displayName ?? "Item";
312}
313
314export function stackDisplayLabel (stack)
315{
316 const name = itemDisplayName(stack);
317 const qty = stack?.quantity ?? 1;
318 if (qty <= 1)
319 { return name; }
320 return `${name} (${formatQuantity(qty)})`;
321}
322
323export function coinSpriteTierForQuantity (quantity)
324{
325 const qty = Math.max(0, Math.floor(Number(quantity) || 0));
326 let tier = COIN_SPRITE_TIERS[0];
327 for (const candidate of COIN_SPRITE_TIERS)
328 {
329 if (qty >= candidate)
330 { tier = candidate; }
331 else
332 { break; }
333 }
334 return tier;
335}
336
337/** Stack-aware description (letters include open state and author). */
338export function itemDescription (stack, { localCharacterId } = {})
339{
340 const def = itemDefinition(stack?.itemId);
341 if (!def)
342 { return ""; }
343 if (stack.itemId === ItemId.letter && stack.instance?.kind === "letter")
344 {
345 const state = stack.instance.opened === false ? "unopened" : "opened";
346 const authorId = stack.instance.authorCharacterId;
347 const byYou = authorId != null && Number(authorId) === Number(localCharacterId);
348 const author = byYou
349 ? "you"
350 : (stack.instance.authorDisplayName?.trim() || "someone");
351 return `An ${state} letter, written by ${author}.`;
352 }
353 if (stack.itemId === ItemId.filledCheque && stack.instance?.kind === "filled-cheque")
354 {
355 const reserved = itemDefinition(stack.instance.itemId);
356 const qty = stack.instance.quantity | 0;
357 const name = reserved?.displayName ?? "goods";
358 return `Bearer cheque for ${qty}× ${name}.`;
359 }
360 if (stack.itemId === ItemId.potion && stack.instance?.kind === "potion")
361 {
362 const name = stack.instance.displayName ?? "Potion";
363 return stack.instance.unstable
364 ? `${name}. An unstable brew — effects are unpredictable.`
365 : `${name}. Drink to apply its effect (effects TBD).`;
366 }
367 if (stack.itemId === ItemId.recipePaper && stack.instance?.kind === "recipe-paper")
368 { return "An alchemy recipe you wrote down. Read like a letter."; }
369 return def.description ?? "";
370}
371
372export function itemOriginSummary (stack)
373{
374 return formatOriginBreakdown(stack);
375}
376
377export function itemDefaultState (def)
378{
379 return def?.defaultState ?? def?.key ?? "paper";
380}
381
382export function itemSpriteName (itemId, instance = null)
383{
384 const def = itemDefinition(itemId);
385 if (!def)
386 { return spriteImageName("item", "paper", "paper"); }
387 if (itemId === ItemId.letter)
388 { return spriteImageName("item", "letter", letterVisualState(instance)); }
389 return def.sprite ?? spriteImageName("item", def.key, itemDefaultState(def));
390}
391
392export function itemEquipSprite (itemId)
393{
394 const def = itemDefinition(itemId);
395 const equipment = def?.equipment;
396 if (!equipment)
397 { return null; }
398 return equipment.equipSprite
399 ?? equipment.overlaySpriteId
400 ?? equipImageName("item", def.key, itemDefaultState(def));
401}
402
403/** Largest equipped light radius (e.g. lantern in offhand). */
404export function equippedLightRadiusTiles (equipment)
405{
406 let best = 0;
407 for (const stack of Object.values(equipment?.slots ?? {}))
408 {
409 if (!stack)
410 { continue; }
411 const radius = itemDefinition(stack.itemId)?.equipment?.lightRadiusTiles ?? 0;
412 if (radius > best)
413 { best = radius; }
414 }
415 return best;
416}
417
418export function letterVisualState (instance)
419{
420 return instance?.opened === false ? "letter-closed" : "letter-opened";
421}
422
423export function itemAssetPath (itemId, companion = null, instance = null)
424{
425 const def = itemDefinition(itemId);
426 const key = def?.iconKey ?? def?.key ?? "paper";
427 const state = itemId === ItemId.letter
428 ? letterVisualState(instance)
429 : itemDefaultState(def ?? { key });
430 const suffix = companion ? `-${companion}` : "";
431 return `/assets/items/${key}/${state}${suffix}.png`;
432}
433
434export function itemAssetPathForStack (stack)
435{
436 if (!stack)
437 { return itemAssetPath(null); }
438 if (stack.itemId === ItemId.coins)
439 {
440 const tier = coinSpriteTierForQuantity(stack.quantity);
441 return `/assets/items/coins/coins-${tier}.png`;
442 }
443 if (stack.itemId === ItemId.potion)
444 {
445 const fill = Math.max(1, Math.min(5, stack.instance?.fillLevel | 0 || 4));
446 return `/assets/items/potion/potion-${fill}.png`;
447 }
448 if (stack.itemId === ItemId.glassBottle)
449 { return `/assets/items/glass-bottle/glass-bottle.png`; }
450 return itemAssetPath(stack.itemId, null, stack.instance);
451}
452
453export const LETTER_MAX_BODY_CHARS = 2000;
454
455export const STARTER_ITEMS = Object.freeze([
456 Object.freeze({ itemId: ItemId.paper, quantity: 5 }),
457 Object.freeze({ itemId: ItemId.apple, quantity: 3 }),
458 Object.freeze({ itemId: ItemId.stick, quantity: 1 }),
459 Object.freeze({ itemId: ItemId.clothShirt, quantity: 1 }),
460 Object.freeze({ itemId: ItemId.coins, quantity: 25 })
461]);