BlueGit

timfa questward

Files

questward / shared / items / starterLoadout.js

starterLoadout.js

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