BlueGit

timfa questward

Files

questward / shared / items / starterLoadout.js

starterLoadout.js

starterLoadout.js 4.6 KiB Download Raw
1/**
2 * New-character starter inventory + equipment from world settings.
3 * Existing characters are unaffected; only applied when a bag is first created.
4 */
5
6import { ItemId, itemDefinition, EQUIPMENT_SLOT } from "../definitions/items.js";
7import { EQUIPMENT_SLOTS, emptyEquipment, emptySlots, insertStack } from "./inventory.js";
8import { godsOrigin, attachOrigins } from "./origin.js";
9
10/** Defaults match the previous hard-coded starter kit. */
11export const DEFAULT_STARTER_EQUIPMENT = Object.freeze({
12 [EQUIPMENT_SLOT.head]: null,
13 [EQUIPMENT_SLOT.chest]: null,
14 [EQUIPMENT_SLOT.legs]: null,
15 [EQUIPMENT_SLOT.boots]: null,
16 [EQUIPMENT_SLOT.weapon]: null,
17 [EQUIPMENT_SLOT.offhand]: ItemId.lantern
18});
19
20export const DEFAULT_STARTER_INVENTORY = Object.freeze([
21 Object.freeze({ itemId: ItemId.paper, quantity: 5 }),
22 Object.freeze({ itemId: ItemId.apple, quantity: 3 }),
23 Object.freeze({ itemId: ItemId.stick, quantity: 1 }),
24 Object.freeze({ itemId: ItemId.clothShirt, quantity: 1 }),
25 Object.freeze({ itemId: ItemId.coins, quantity: 25 })
26]);
27
28export function normalizeStarterEquipment (raw)
29{
30 const source = raw && typeof raw === "object" ? raw : {};
31 const slots = {};
32 for (const slot of EQUIPMENT_SLOTS)
33 {
34 const value = source[slot];
35 if (value == null || value === "" || value === 0)
36 {
37 slots[slot] = null;
38 continue;
39 }
40 const itemId = Number(typeof value === "object" ? value.itemId : value);
41 const def = itemDefinition(itemId);
42 if (!def?.equipment?.slot || def.equipment.slot !== slot)
43 {
44 slots[slot] = null;
45 continue;
46 }
47 slots[slot] = itemId;
48 }
49 return slots;
50}
51
52export function normalizeStarterInventory (raw)
53{
54 if (!Array.isArray(raw))
55 { return DEFAULT_STARTER_INVENTORY.map((entry) => ({ ...entry })); }
56 const out = [];
57 for (const entry of raw)
58 {
59 if (!entry || typeof entry !== "object")
60 { continue; }
61 const itemId = Number(entry.itemId);
62 const quantity = Math.max(1, Math.floor(Number(entry.quantity) || 1));
63 if (!itemDefinition(itemId))
64 { continue; }
65 out.push({ itemId, quantity });
66 }
67 return out;
68}
69
70export function normalizeStarterLoadout (settings)
71{
72 const equipment = normalizeStarterEquipment(
73 settings?.starterEquipment ?? DEFAULT_STARTER_EQUIPMENT
74 );
75 const inventory = normalizeStarterInventory(
76 settings?.starterInventory ?? DEFAULT_STARTER_INVENTORY
77 );
78 return { equipment, inventory };
79}
80
81export function starterLoadoutsEqual (a, b)
82{
83 const left = normalizeStarterLoadout(a);
84 const right = normalizeStarterLoadout(b);
85 for (const slot of EQUIPMENT_SLOTS)
86 {
87 if (left.equipment[slot] !== right.equipment[slot])
88 { return false; }
89 }
90 if (left.inventory.length !== right.inventory.length)
91 { return false; }
92 for (let i = 0; i < left.inventory.length; i++)
93 {
94 if (left.inventory[i].itemId !== right.inventory[i].itemId)
95 { return false; }
96 if (left.inventory[i].quantity !== right.inventory[i].quantity)
97 { return false; }
98 }
99 return true;
100}
101
102export function diffStarterLoadout (previous, next)
103{
104 const bullets = [];
105 const prev = normalizeStarterLoadout(previous);
106 const upcoming = normalizeStarterLoadout(next);
107 for (const slot of EQUIPMENT_SLOTS)
108 {
109 if (prev.equipment[slot] === upcoming.equipment[slot])
110 { continue; }
111 const fromName = prev.equipment[slot]
112 ? (itemDefinition(prev.equipment[slot])?.displayName ?? `#${prev.equipment[slot]}`)
113 : "empty";
114 const toName = upcoming.equipment[slot]
115 ? (itemDefinition(upcoming.equipment[slot])?.displayName ?? `#${upcoming.equipment[slot]}`)
116 : "empty";
117 bullets.push(`Starter ${slot} changed from ${fromName} to ${toName}.`);
118 }
119 const prevInv = JSON.stringify(prev.inventory);
120 const nextInv = JSON.stringify(upcoming.inventory);
121 if (prevInv !== nextInv)
122 { bullets.push("Starter inventory list changed."); }
123 return bullets;
124}
125
126/** Build bag contents for a brand-new character. */
127export function buildStarterBagContents (settings)
128{
129 const { equipment: equipIds, inventory } = normalizeStarterLoadout(settings);
130 const origin = godsOrigin();
131 const slots = emptySlots();
132 for (const entry of inventory)
133 {
134 const def = itemDefinition(entry.itemId);
135 if (!def)
136 { continue; }
137 const stack = attachOrigins({
138 itemId: entry.itemId,
139 quantity: entry.quantity,
140 origins: [{ origin, quantity: entry.quantity }]
141 });
142 const result = insertStack(slots, stack, def);
143 if (result.ok)
144 { slots.splice(0, slots.length, ...result.slots); }
145 }
146
147 const equipment = emptyEquipment();
148 for (const slot of EQUIPMENT_SLOTS)
149 {
150 const itemId = equipIds[slot];
151 if (itemId == null)
152 {
153 equipment.slots[slot] = null;
154 continue;
155 }
156 equipment.slots[slot] = attachOrigins({
157 itemId,
158 quantity: 1,
159 origins: [{ origin, quantity: 1 }]
160 });
161 }
162 return { slots, equipment };
163}