| 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 | |
| 6 | import { ItemId, itemDefinition, EQUIPMENT_SLOT } from "../definitions/items.js"; |
| 7 | import { EQUIPMENT_SLOTS, emptyEquipment, emptySlots, insertStack } from "./inventory.js"; |
| 8 | import { godsOrigin, attachOrigins } from "./origin.js"; |
| 9 | import { applyCreationClothingToEquipment, normalizeCreateClothing } from "./createClothing.js"; |
| 10 | |
| 11 | /** Defaults match the previous hard-coded starter kit (clothing comes from character creation). */ |
| 12 | export 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 | }); |
| 20 | |
| 21 | export const DEFAULT_STARTER_INVENTORY = Object.freeze([ |
| 22 | Object.freeze({ itemId: ItemId.paper, quantity: 5 }), |
| 23 | Object.freeze({ itemId: ItemId.apple, quantity: 3 }), |
| 24 | Object.freeze({ itemId: ItemId.stick, quantity: 1 }), |
| 25 | Object.freeze({ itemId: ItemId.coins, quantity: 25 }) |
| 26 | ]); |
| 27 | |
| 28 | export 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 | |
| 52 | export 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 | |
| 70 | export function normalizeStarterLoadout (settings) |
| 71 | { |
| 72 | // Accept world-settings shape or already-normalized `{ equipment, inventory }`. |
| 73 | const equipmentRaw = settings?.starterEquipment ?? settings?.equipment ?? DEFAULT_STARTER_EQUIPMENT; |
| 74 | const inventoryRaw = settings?.starterInventory ?? settings?.inventory ?? DEFAULT_STARTER_INVENTORY; |
| 75 | const equipment = normalizeStarterEquipment(equipmentRaw); |
| 76 | const inventory = normalizeStarterInventory(inventoryRaw); |
| 77 | return { equipment, inventory }; |
| 78 | } |
| 79 | |
| 80 | export function starterLoadoutsEqual (a, b) |
| 81 | { |
| 82 | const left = normalizeStarterLoadout(a); |
| 83 | const right = normalizeStarterLoadout(b); |
| 84 | for (const slot of EQUIPMENT_SLOTS) |
| 85 | { |
| 86 | if (left.equipment[slot] !== right.equipment[slot]) |
| 87 | { return false; } |
| 88 | } |
| 89 | if (left.inventory.length !== right.inventory.length) |
| 90 | { return false; } |
| 91 | for (let i = 0; i < left.inventory.length; i++) |
| 92 | { |
| 93 | if (left.inventory[i].itemId !== right.inventory[i].itemId) |
| 94 | { return false; } |
| 95 | if (left.inventory[i].quantity !== right.inventory[i].quantity) |
| 96 | { return false; } |
| 97 | } |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | export function diffStarterLoadout (previous, next) |
| 102 | { |
| 103 | const bullets = []; |
| 104 | const prev = normalizeStarterLoadout(previous); |
| 105 | const upcoming = normalizeStarterLoadout(next); |
| 106 | for (const slot of EQUIPMENT_SLOTS) |
| 107 | { |
| 108 | if (prev.equipment[slot] === upcoming.equipment[slot]) |
| 109 | { continue; } |
| 110 | const fromName = prev.equipment[slot] |
| 111 | ? (itemDefinition(prev.equipment[slot])?.displayName ?? `#${prev.equipment[slot]}`) |
| 112 | : "empty"; |
| 113 | const toName = upcoming.equipment[slot] |
| 114 | ? (itemDefinition(upcoming.equipment[slot])?.displayName ?? `#${upcoming.equipment[slot]}`) |
| 115 | : "empty"; |
| 116 | bullets.push(`Starter ${slot} changed from ${fromName} to ${toName}.`); |
| 117 | } |
| 118 | const prevInv = JSON.stringify(prev.inventory); |
| 119 | const nextInv = JSON.stringify(upcoming.inventory); |
| 120 | if (prevInv !== nextInv) |
| 121 | { bullets.push("Starter inventory list changed."); } |
| 122 | return bullets; |
| 123 | } |
| 124 | |
| 125 | /** Build bag contents for a brand-new character. */ |
| 126 | export function buildStarterBagContents (settings, creationClothing = null) |
| 127 | { |
| 128 | const { equipment: equipIds, inventory } = normalizeStarterLoadout(settings); |
| 129 | const origin = godsOrigin(); |
| 130 | const slots = emptySlots(); |
| 131 | for (const entry of inventory) |
| 132 | { |
| 133 | const def = itemDefinition(entry.itemId); |
| 134 | if (!def) |
| 135 | { continue; } |
| 136 | const stack = attachOrigins({ |
| 137 | itemId: entry.itemId, |
| 138 | quantity: entry.quantity, |
| 139 | origins: [{ origin, quantity: entry.quantity }] |
| 140 | }); |
| 141 | const result = insertStack(slots, stack, def); |
| 142 | if (result.ok) |
| 143 | { slots.splice(0, slots.length, ...result.slots); } |
| 144 | } |
| 145 | |
| 146 | const equipment = emptyEquipment(); |
| 147 | for (const slot of EQUIPMENT_SLOTS) |
| 148 | { |
| 149 | const itemId = equipIds[slot]; |
| 150 | if (itemId == null) |
| 151 | { |
| 152 | equipment.slots[slot] = null; |
| 153 | continue; |
| 154 | } |
| 155 | equipment.slots[slot] = attachOrigins({ |
| 156 | itemId, |
| 157 | quantity: 1, |
| 158 | origins: [{ origin, quantity: 1 }] |
| 159 | }); |
| 160 | } |
| 161 | applyCreationClothingToEquipment(equipment, normalizeCreateClothing(creationClothing)); |
| 162 | return { slots, equipment }; |
| 163 | } |