BlueGit

timfa questward

Files

questward / shared / definitions / items.js

items.js

items.js 9.6 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";
9
10export const ItemId = Object.freeze({
11 paper: 1,
12 apple: 2,
13 stick: 3,
14 blankCheque: 4,
15 clothShirt: 5,
16 letter: 6,
17 filledCheque: 7,
18 coins: 8,
19 ...CraftingItemId,
20 lantern: 26,
21 bones: 27,
22 skull: 28
23});
24
25export const EQUIPMENT_SLOT = Object.freeze({
26 head: "head",
27 chest: "chest",
28 legs: "legs",
29 boots: "boots",
30 weapon: "weapon",
31 offhand: "offhand"
32});
33
34export const EQUIPMENT_DRAW_ORDER = Object.freeze([
35 EQUIPMENT_SLOT.boots,
36 EQUIPMENT_SLOT.legs,
37 EQUIPMENT_SLOT.chest,
38 EQUIPMENT_SLOT.head,
39 EQUIPMENT_SLOT.offhand,
40 EQUIPMENT_SLOT.weapon
41]);
42
43export const DEFAULT_MAX_STACK = 99;
44export const BLANK_CHEQUE_CARRY_CAP = 99;
45export const COIN_MAX_STACK = 999_000_000_000;
46
47/** Coin sprite tiers (coins-<n>.png); pick the largest n where stack quantity >= n. */
48export const COIN_SPRITE_TIERS = Object.freeze([
49 1, 5, 15, 50, 150, 500, 1000, 1500, 2000, 5000, 10000, 20000, 50000,
50 1_000_000, 10_000_000, 50_000_000, 100_000_000, 500_000_000,
51 1_000_000_000, 10_000_000_000, 100_000_000_000, 1_000_000_000_000,
52 10_000_000_000_000, 100_000_000_000_000, 500_000_000_000_000,
53 999_999_999_999_999
54]);
55
56export const ITEM_DEFINITIONS = Object.freeze({
57 [ItemId.paper]: Object.freeze({
58 itemId: ItemId.paper,
59 key: "paper",
60 displayName: "Paper",
61 description: "Blank paper. Right-click to write a letter.",
62 seedValue: 2,
63 stackable: true,
64 maxStackSize: DEFAULT_MAX_STACK,
65 uniqueInstance: false,
66 category: "document"
67 }),
68 [ItemId.letter]: Object.freeze({
69 itemId: ItemId.letter,
70 key: "letter",
71 displayName: "Letter",
72 description: "A written letter.",
73 seedValue: 2,
74 stackable: false,
75 maxStackSize: 1,
76 uniqueInstance: true,
77 category: "document",
78 defaultState: "letter-opened"
79 }),
80 [ItemId.apple]: Object.freeze({
81 itemId: ItemId.apple,
82 key: "apple",
83 displayName: "Apple",
84 description: "Restores a little health when eaten.",
85 seedValue: 1,
86 stackable: true,
87 maxStackSize: DEFAULT_MAX_STACK,
88 uniqueInstance: false,
89 category: "food",
90 healing: 4
91 }),
92 [ItemId.stick]: Object.freeze({
93 itemId: ItemId.stick,
94 key: "stick",
95 displayName: "Stick",
96 description: "A wooden stick. Can be wielded.",
97 seedValue: 3,
98 stackable: false,
99 maxStackSize: 1,
100 uniqueInstance: false,
101 category: "finished",
102 equipment: Object.freeze({
103 slot: EQUIPMENT_SLOT.weapon,
104 tier: MaterialTier.bronze,
105 kind: EquipmentKind.clubWeapon,
106 attack: Object.freeze({
107 mode: "melee",
108 rangeTiles: 1,
109 hitSkillId: SkillId.clubfighting,
110 damageSkillId: SkillId.strength,
111 attackType: AttackType.blunt,
112 cooldownMs: 700,
113 baseDamage: 2
114 })
115 })
116 }),
117 [ItemId.blankCheque]: Object.freeze({
118 itemId: ItemId.blankCheque,
119 key: "blank-cheque",
120 displayName: "Blank cheque",
121 description: "Free bank paper. Write a cheque at a bank to reserve vault goods.",
122 seedValue: 5,
123 stackable: true,
124 maxStackSize: DEFAULT_MAX_STACK,
125 uniqueInstance: false,
126 category: "document",
127 marketplaceSellable: false,
128 marketplaceSellBlockedReason: "Blank cheques cannot be sold on the marketplace."
129 }),
130 [ItemId.filledCheque]: Object.freeze({
131 itemId: ItemId.filledCheque,
132 key: "filled-cheque",
133 displayName: "Cheque",
134 description: "A bearer cheque linked to reserved goods in the issuer's bank.",
135 seedValue: 5,
136 stackable: false,
137 maxStackSize: 1,
138 uniqueInstance: true,
139 category: "document",
140 defaultState: "filled-cheque",
141 marketplaceSellable: true
142 }),
143 [ItemId.clothShirt]: Object.freeze({
144 itemId: ItemId.clothShirt,
145 key: "cloth-shirt",
146 displayName: "Cloth shirt",
147 description: "A placeholder shirt worn on the chest.",
148 seedValue: 8,
149 stackable: false,
150 maxStackSize: 1,
151 uniqueInstance: false,
152 category: "finished",
153 equipment: Object.freeze({
154 slot: EQUIPMENT_SLOT.chest,
155 tier: MaterialTier.bronze,
156 kind: EquipmentKind.clothArmour
157 })
158 }),
159 [ItemId.coins]: Object.freeze({
160 itemId: ItemId.coins,
161 key: "coins",
162 displayName: "Coin",
163 displayNamePlural: "Coins",
164 description: "Currency carried in your bag.",
165 seedValue: 1,
166 stackable: true,
167 maxStackSize: COIN_MAX_STACK,
168 uniqueInstance: false,
169 category: "currency"
170 }),
171 [ItemId.lantern]: Object.freeze({
172 itemId: ItemId.lantern,
173 key: "lantern",
174 displayName: "Lantern",
175 description: "A handheld light source. Equip in the offhand to illuminate nearby tiles at night.",
176 seedValue: 12,
177 stackable: false,
178 maxStackSize: 1,
179 uniqueInstance: false,
180 category: "tool",
181 equipment: Object.freeze({
182 slot: EQUIPMENT_SLOT.offhand,
183 lightRadiusTiles: 6
184 })
185 }),
186 [ItemId.bones]: Object.freeze({
187 itemId: ItemId.bones,
188 key: "bones",
189 displayName: "Bones",
190 description: "The remains of a fallen adventurer.",
191 seedValue: 1,
192 stackable: true,
193 maxStackSize: DEFAULT_MAX_STACK,
194 uniqueInstance: false,
195 category: "ingredient"
196 }),
197 [ItemId.skull]: Object.freeze({
198 itemId: ItemId.skull,
199 key: "skull",
200 displayName: "Skull",
201 description: "A grim reminder of an adventurer's death.",
202 seedValue: 1,
203 stackable: true,
204 maxStackSize: DEFAULT_MAX_STACK,
205 uniqueInstance: false,
206 category: "misc"
207 }),
208 ...CRAFTING_ITEM_STUBS
209});
210
211export function itemDefinition (itemId)
212{
213 return ITEM_DEFINITIONS[itemId] ?? null;
214}
215
216export { formatQuantity };
217
218export function itemDisplayName (stack, def = itemDefinition(stack?.itemId))
219{
220 if (!def)
221 { return "Item"; }
222 const qty = stack?.quantity ?? 1;
223 if (stack?.itemId === ItemId.coins)
224 { return qty === 1 ? (def.displayName ?? "Coin") : (def.displayNamePlural ?? "Coins"); }
225 return def.displayName ?? "Item";
226}
227
228export function stackDisplayLabel (stack)
229{
230 const name = itemDisplayName(stack);
231 const qty = stack?.quantity ?? 1;
232 if (qty <= 1)
233 { return name; }
234 return `${name} (${formatQuantity(qty)})`;
235}
236
237export function coinSpriteTierForQuantity (quantity)
238{
239 const qty = Math.max(0, Math.floor(Number(quantity) || 0));
240 let tier = COIN_SPRITE_TIERS[0];
241 for (const candidate of COIN_SPRITE_TIERS)
242 {
243 if (qty >= candidate)
244 { tier = candidate; }
245 else
246 { break; }
247 }
248 return tier;
249}
250
251/** Stack-aware description (letters include open state and author). */
252export function itemDescription (stack, { localCharacterId } = {})
253{
254 const def = itemDefinition(stack?.itemId);
255 if (!def)
256 { return ""; }
257 if (stack.itemId === ItemId.letter && stack.instance?.kind === "letter")
258 {
259 const state = stack.instance.opened === false ? "unopened" : "opened";
260 const authorId = stack.instance.authorCharacterId;
261 const byYou = authorId != null && Number(authorId) === Number(localCharacterId);
262 const author = byYou
263 ? "you"
264 : (stack.instance.authorDisplayName?.trim() || "someone");
265 return `An ${state} letter, written by ${author}.`;
266 }
267 if (stack.itemId === ItemId.filledCheque && stack.instance?.kind === "filled-cheque")
268 {
269 const reserved = itemDefinition(stack.instance.itemId);
270 const qty = stack.instance.quantity | 0;
271 const name = reserved?.displayName ?? "goods";
272 return `Bearer cheque for ${qty}× ${name}.`;
273 }
274 return def.description ?? "";
275}
276
277export function itemOriginSummary (stack)
278{
279 return formatOriginBreakdown(stack);
280}
281
282export function itemDefaultState (def)
283{
284 return def?.defaultState ?? def?.key ?? "paper";
285}
286
287export function itemSpriteName (itemId, instance = null)
288{
289 const def = itemDefinition(itemId);
290 if (!def)
291 { return spriteImageName("item", "paper", "paper"); }
292 if (itemId === ItemId.letter)
293 { return spriteImageName("item", "letter", letterVisualState(instance)); }
294 return def.sprite ?? spriteImageName("item", def.key, itemDefaultState(def));
295}
296
297export function itemEquipSprite (itemId)
298{
299 const def = itemDefinition(itemId);
300 const equipment = def?.equipment;
301 if (!equipment)
302 { return null; }
303 return equipment.equipSprite
304 ?? equipment.overlaySpriteId
305 ?? equipImageName("item", def.key, itemDefaultState(def));
306}
307
308/** Largest equipped light radius (e.g. lantern in offhand). */
309export function equippedLightRadiusTiles (equipment)
310{
311 let best = 0;
312 for (const stack of Object.values(equipment?.slots ?? {}))
313 {
314 if (!stack)
315 { continue; }
316 const radius = itemDefinition(stack.itemId)?.equipment?.lightRadiusTiles ?? 0;
317 if (radius > best)
318 { best = radius; }
319 }
320 return best;
321}
322
323export function letterVisualState (instance)
324{
325 return instance?.opened === false ? "letter-closed" : "letter-opened";
326}
327
328export function itemAssetPath (itemId, companion = null, instance = null)
329{
330 const def = itemDefinition(itemId);
331 const key = def?.iconKey ?? def?.key ?? "paper";
332 const state = itemId === ItemId.letter
333 ? letterVisualState(instance)
334 : itemDefaultState(def ?? { key });
335 const suffix = companion ? `-${companion}` : "";
336 return `/assets/items/${key}/${state}${suffix}.png`;
337}
338
339export function itemAssetPathForStack (stack)
340{
341 if (!stack)
342 { return itemAssetPath(null); }
343 if (stack.itemId === ItemId.coins)
344 {
345 const tier = coinSpriteTierForQuantity(stack.quantity);
346 return `/assets/items/coins/coins-${tier}.png`;
347 }
348 return itemAssetPath(stack.itemId, null, stack.instance);
349}
350
351export const LETTER_MAX_BODY_CHARS = 2000;
352
353export const STARTER_ITEMS = Object.freeze([
354 Object.freeze({ itemId: ItemId.paper, quantity: 5 }),
355 Object.freeze({ itemId: ItemId.apple, quantity: 3 }),
356 Object.freeze({ itemId: ItemId.stick, quantity: 1 }),
357 Object.freeze({ itemId: ItemId.clothShirt, quantity: 1 }),
358 Object.freeze({ itemId: ItemId.coins, quantity: 25 })
359]);