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