BlueGit

timfa questward

Files

questward / shared / definitions / items.js

items.js

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