| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import { BlockType } from "../../../shared/definitions/tiles.js"; |
| 3 | import { itemDefinition, itemSpriteName } from "../../../shared/definitions/items.js"; |
| 4 | import { |
| 5 | emptyEquipment, |
| 6 | emptySlots, |
| 7 | insertStack, |
| 8 | publicEquipment, |
| 9 | publicSlots, |
| 10 | takeFromSlot |
| 11 | } from "../../../shared/items/inventory.js"; |
| 12 | import { tileKey, tileToChunk, tileToLocal } from "../world/coordinates.js"; |
| 13 | import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; |
| 14 | |
| 15 | export function publicGroundItem (item) |
| 16 | { |
| 17 | const def = itemDefinition(item.stack?.itemId); |
| 18 | return { |
| 19 | groundItemId: item.groundItemId, |
| 20 | tileX: item.tileX, |
| 21 | tileY: item.tileY, |
| 22 | itemId: item.stack.itemId, |
| 23 | quantity: item.stack.quantity, |
| 24 | displayName: def?.displayName ?? "Item", |
| 25 | sprite: itemSpriteName(item.stack.itemId), |
| 26 | expiresAt: item.expiresAt |
| 27 | }; |
| 28 | } |
| 29 | |
| 30 | export function bagView (ecs, handle) |
| 31 | { |
| 32 | return { |
| 33 | inventory: publicSlots(ecs.get(handle, Component.inventory)?.slots ?? emptySlots()), |
| 34 | equipment: publicEquipment(ecs.get(handle, Component.equipment) ?? emptyEquipment()), |
| 35 | currency: ecs.get(handle, Component.currency)?.amount ?? 0, |
| 36 | vault: publicSlots(ecs.get(handle, Component.vault)?.slots ?? emptySlots()), |
| 37 | vaultCurrency: ecs.get(handle, Component.vault)?.currency ?? 0 |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | export function handleItemCommand (world, sessionId, command, now) |
| 42 | { |
| 43 | switch (command.type) |
| 44 | { |
| 45 | case "drop-item": |
| 46 | return dropItem(world, sessionId, command, now); |
| 47 | case "pickup-item": |
| 48 | return pickupItem(world, sessionId, command); |
| 49 | case "equip-item": |
| 50 | return equipItem(world, sessionId, command); |
| 51 | case "unequip-item": |
| 52 | return unequipItem(world, sessionId, command); |
| 53 | case "open-bank": |
| 54 | return openBank(world, sessionId, command); |
| 55 | case "bank-deposit": |
| 56 | return bankMove(world, sessionId, command, "inventory", "vault"); |
| 57 | case "bank-withdraw": |
| 58 | return bankMove(world, sessionId, command, "vault", "inventory"); |
| 59 | case "bank-deposit-money": |
| 60 | return bankMoney(world, sessionId, command, "currency", "vault"); |
| 61 | case "bank-withdraw-money": |
| 62 | return bankMoney(world, sessionId, command, "vault", "currency"); |
| 63 | case "claim-inheritance": |
| 64 | return claimInheritance(world, sessionId, command); |
| 65 | default: |
| 66 | return null; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | export function expireGroundItems (world, now) |
| 71 | { |
| 72 | const events = []; |
| 73 | for (const item of [...world.groundItems.values()]) |
| 74 | { |
| 75 | if (item.expiresAt > now) |
| 76 | { continue; } |
| 77 | removeGroundItem(world, item.groundItemId); |
| 78 | events.push({ type: "ground-item-despawn", ...publicGroundItem(item) }); |
| 79 | } |
| 80 | return events; |
| 81 | } |
| 82 | |
| 83 | export function groundItemsInChunk (world, chunkX, chunkY) |
| 84 | { |
| 85 | return [...world.groundItems.values()] |
| 86 | .filter((item) => tileToChunk(item.tileX) === chunkX && tileToChunk(item.tileY) === chunkY) |
| 87 | .map(publicGroundItem); |
| 88 | } |
| 89 | |
| 90 | export function groundItemsInRegion (world, regionX, regionY, tileToRegion) |
| 91 | { |
| 92 | return [...world.groundItems.values()].filter((item) => |
| 93 | tileToRegion(item.tileX) === regionX && tileToRegion(item.tileY) === regionY |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | export function addGroundItem (world, item) |
| 98 | { |
| 99 | world.groundItems.set(item.groundItemId, item); |
| 100 | const key = tileKey(item.tileX, item.tileY); |
| 101 | if (!world.groundByTile.has(key)) |
| 102 | { world.groundByTile.set(key, new Set()); } |
| 103 | world.groundByTile.get(key).add(item.groundItemId); |
| 104 | } |
| 105 | |
| 106 | export function removeGroundItem (world, groundItemId) |
| 107 | { |
| 108 | const item = world.groundItems.get(groundItemId); |
| 109 | if (!item) |
| 110 | { return null; } |
| 111 | world.groundItems.delete(groundItemId); |
| 112 | const key = tileKey(item.tileX, item.tileY); |
| 113 | world.groundByTile.get(key)?.delete(groundItemId); |
| 114 | return item; |
| 115 | } |
| 116 | |
| 117 | export function replaceGroundItems (world, items) |
| 118 | { |
| 119 | world.groundItems.clear(); |
| 120 | world.groundByTile.clear(); |
| 121 | for (const item of items ?? []) |
| 122 | { addGroundItem(world, item); } |
| 123 | } |
| 124 | |
| 125 | export function setGroundItemsForRegion (world, regionX, regionY, items, tileToRegion) |
| 126 | { |
| 127 | for (const item of [...world.groundItems.values()]) |
| 128 | { |
| 129 | if (tileToRegion(item.tileX) === regionX && tileToRegion(item.tileY) === regionY) |
| 130 | { removeGroundItem(world, item.groundItemId); } |
| 131 | } |
| 132 | for (const item of items ?? []) |
| 133 | { addGroundItem(world, item); } |
| 134 | } |
| 135 | |
| 136 | function dropItem (world, sessionId, command, now) |
| 137 | { |
| 138 | const { handle } = world.requireOwnedEntity(sessionId, command.characterId); |
| 139 | const inventory = ensureInventory(world.ecs, handle); |
| 140 | const taken = takeFromSlot(inventory.slots, command.slotIndex, command.quantity ?? null); |
| 141 | if (!taken.ok) |
| 142 | { throw userError("That inventory slot is empty."); } |
| 143 | inventory.slots = taken.slots; |
| 144 | const position = world.ecs.get(handle, Component.position); |
| 145 | const item = { |
| 146 | groundItemId: command.groundItemId, |
| 147 | tileX: position.tileX, |
| 148 | tileY: position.tileY, |
| 149 | stack: taken.taken, |
| 150 | droppedAt: now, |
| 151 | expiresAt: now + (world.settings.groundItemDespawnDurationMs ?? 600_000), |
| 152 | source: "player-drop", |
| 153 | droppedByCharacterId: command.characterId |
| 154 | }; |
| 155 | addGroundItem(world, item); |
| 156 | return { |
| 157 | events: [{ type: "ground-item-appear", ...publicGroundItem(item), stack: item.stack, droppedAt: item.droppedAt, source: item.source, droppedByCharacterId: item.droppedByCharacterId, expiresAt: item.expiresAt }], |
| 158 | private: bagView(world.ecs, handle) |
| 159 | }; |
| 160 | } |
| 161 | |
| 162 | function pickupItem (world, sessionId, command) |
| 163 | { |
| 164 | const { handle } = world.requireOwnedEntity(sessionId, command.characterId); |
| 165 | const item = world.groundItems.get(command.groundItemId); |
| 166 | if (!item) |
| 167 | { throw userError("That item is no longer on the ground."); } |
| 168 | const position = world.ecs.get(handle, Component.position); |
| 169 | if (position.tileX !== item.tileX || position.tileY !== item.tileY) |
| 170 | { throw userError("Stand on the item to pick it up."); } |
| 171 | const def = itemDefinition(item.stack.itemId); |
| 172 | if (!def) |
| 173 | { throw userError("Unknown item."); } |
| 174 | const inventory = ensureInventory(world.ecs, handle); |
| 175 | const inserted = insertStack(inventory.slots, item.stack, def); |
| 176 | if (!inserted.ok) |
| 177 | { throw userError("Your inventory cannot hold that."); } |
| 178 | inventory.slots = inserted.slots; |
| 179 | removeGroundItem(world, item.groundItemId); |
| 180 | return { |
| 181 | events: [{ type: "ground-item-despawn", ...publicGroundItem(item) }], |
| 182 | private: bagView(world.ecs, handle) |
| 183 | }; |
| 184 | } |
| 185 | |
| 186 | function equipItem (world, sessionId, command) |
| 187 | { |
| 188 | const { handle } = world.requireOwnedEntity(sessionId, command.characterId); |
| 189 | const inventory = ensureInventory(world.ecs, handle); |
| 190 | const equipment = ensureEquipment(world.ecs, handle); |
| 191 | const taken = takeFromSlot(inventory.slots, command.slotIndex, 1); |
| 192 | if (!taken.ok) |
| 193 | { throw userError("That inventory slot is empty."); } |
| 194 | const def = itemDefinition(taken.taken.itemId); |
| 195 | const slot = def?.equipment?.slot; |
| 196 | if (!slot) |
| 197 | { |
| 198 | inventory.slots = insertStack(inventory.slots, taken.taken, def).slots; |
| 199 | throw userError("That item cannot be equipped."); |
| 200 | } |
| 201 | const previous = equipment.slots[slot]; |
| 202 | if (previous) |
| 203 | { |
| 204 | const back = insertStack(taken.slots, previous, itemDefinition(previous.itemId)); |
| 205 | if (!back.ok) |
| 206 | { |
| 207 | inventory.slots = insertStack(inventory.slots, taken.taken, def).slots; |
| 208 | throw userError("No inventory space to unequip the current item."); |
| 209 | } |
| 210 | inventory.slots = back.slots; |
| 211 | } |
| 212 | else |
| 213 | { inventory.slots = taken.slots; } |
| 214 | equipment.slots[slot] = { ...taken.taken, quantity: 1 }; |
| 215 | return { events: [], private: bagView(world.ecs, handle), appearanceChanged: true }; |
| 216 | } |
| 217 | |
| 218 | function unequipItem (world, sessionId, command) |
| 219 | { |
| 220 | const { handle } = world.requireOwnedEntity(sessionId, command.characterId); |
| 221 | const inventory = ensureInventory(world.ecs, handle); |
| 222 | const equipment = ensureEquipment(world.ecs, handle); |
| 223 | const worn = equipment.slots[command.equipSlot]; |
| 224 | if (!worn) |
| 225 | { throw userError("Nothing is equipped there."); } |
| 226 | const def = itemDefinition(worn.itemId); |
| 227 | const inserted = insertStack(inventory.slots, worn, def); |
| 228 | if (!inserted.ok) |
| 229 | { throw userError("Your inventory cannot hold that."); } |
| 230 | inventory.slots = inserted.slots; |
| 231 | equipment.slots[command.equipSlot] = null; |
| 232 | return { events: [], private: bagView(world.ecs, handle), appearanceChanged: true }; |
| 233 | } |
| 234 | |
| 235 | function openBank (world, sessionId, command) |
| 236 | { |
| 237 | const { handle } = world.requireOwnedEntity(sessionId, command.characterId); |
| 238 | const position = world.ecs.get(handle, Component.position); |
| 239 | if (!nearBank(world, position.tileX, position.tileY)) |
| 240 | { throw userError("Stand next to a bank."); } |
| 241 | return { events: [], private: bagView(world.ecs, handle), bank: true }; |
| 242 | } |
| 243 | |
| 244 | function bankMove (world, sessionId, command, fromKey, toKey) |
| 245 | { |
| 246 | const { handle } = world.requireOwnedEntity(sessionId, command.characterId); |
| 247 | requireBank(world, handle); |
| 248 | const inventory = ensureInventory(world.ecs, handle); |
| 249 | const vault = ensureVault(world.ecs, handle); |
| 250 | const fromSlots = fromKey === "vault" ? vault.slots : inventory.slots; |
| 251 | const taken = takeFromSlot(fromSlots, command.slotIndex, command.quantity ?? null); |
| 252 | if (!taken.ok) |
| 253 | { throw userError("That slot is empty."); } |
| 254 | const def = itemDefinition(taken.taken.itemId); |
| 255 | const dest = toKey === "vault" ? vault : inventory; |
| 256 | const inserted = insertStack(dest.slots, taken.taken, def); |
| 257 | if (!inserted.ok) |
| 258 | { throw userError("Not enough space."); } |
| 259 | if (fromKey === "vault") |
| 260 | { vault.slots = taken.slots; } |
| 261 | else |
| 262 | { inventory.slots = taken.slots; } |
| 263 | dest.slots = inserted.slots; |
| 264 | return { events: [], private: bagView(world.ecs, handle), bank: true }; |
| 265 | } |
| 266 | |
| 267 | function bankMoney (world, sessionId, command, fromKey, toKey) |
| 268 | { |
| 269 | const { handle } = world.requireOwnedEntity(sessionId, command.characterId); |
| 270 | requireBank(world, handle); |
| 271 | const amount = Math.max(0, command.amount | 0); |
| 272 | if (!amount) |
| 273 | { throw userError("Enter an amount."); } |
| 274 | const currency = world.ecs.get(handle, Component.currency) ?? world.ecs.add(handle, Component.currency, { amount: 0 }); |
| 275 | const vault = ensureVault(world.ecs, handle); |
| 276 | const from = fromKey === "vault" ? vault : currency; |
| 277 | const to = toKey === "vault" ? vault : currency; |
| 278 | const fromAmount = fromKey === "vault" ? vault.currency : currency.amount; |
| 279 | if (fromAmount < amount) |
| 280 | { throw userError("Not enough coins."); } |
| 281 | if (fromKey === "vault") |
| 282 | { vault.currency -= amount; } |
| 283 | else |
| 284 | { currency.amount -= amount; } |
| 285 | if (toKey === "vault") |
| 286 | { vault.currency += amount; } |
| 287 | else |
| 288 | { currency.amount += amount; } |
| 289 | return { events: [], private: bagView(world.ecs, handle), bank: true }; |
| 290 | } |
| 291 | |
| 292 | function claimInheritance (world, sessionId, command) |
| 293 | { |
| 294 | const { handle } = world.requireOwnedEntity(sessionId, command.characterId); |
| 295 | requireBank(world, handle); |
| 296 | const vault = ensureVault(world.ecs, handle); |
| 297 | const remainder = []; |
| 298 | for (const stack of command.stacks ?? []) |
| 299 | { |
| 300 | const def = itemDefinition(stack.itemId); |
| 301 | if (!def) |
| 302 | { |
| 303 | remainder.push(stack); |
| 304 | continue; |
| 305 | } |
| 306 | const inserted = insertStack(vault.slots, stack, def); |
| 307 | if (inserted.ok) |
| 308 | { vault.slots = inserted.slots; } |
| 309 | else |
| 310 | { remainder.push(stack); } |
| 311 | } |
| 312 | vault.currency += command.currency | 0; |
| 313 | return { events: [], private: bagView(world.ecs, handle), bank: true, remainder }; |
| 314 | } |
| 315 | |
| 316 | function requireBank (world, handle) |
| 317 | { |
| 318 | const position = world.ecs.get(handle, Component.position); |
| 319 | if (!nearBank(world, position.tileX, position.tileY)) |
| 320 | { throw userError("Stand next to a bank."); } |
| 321 | } |
| 322 | |
| 323 | function nearBank (world, tileX, tileY) |
| 324 | { |
| 325 | for (let dy = -1; dy <= 1; dy++) |
| 326 | { |
| 327 | for (let dx = -1; dx <= 1; dx++) |
| 328 | { |
| 329 | const data = world.chunkData(tileToChunk(tileX + dx), tileToChunk(tileY + dy)); |
| 330 | const index = tileToLocal(tileY + dy) * CHUNK_SIZE + tileToLocal(tileX + dx); |
| 331 | if (data.blocks[index]?.typeId === BlockType.bank) |
| 332 | { return true; } |
| 333 | } |
| 334 | } |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | function ensureInventory (ecs, handle) |
| 339 | { |
| 340 | return ecs.get(handle, Component.inventory) ?? ecs.add(handle, Component.inventory, { slots: emptySlots() }); |
| 341 | } |
| 342 | |
| 343 | function ensureEquipment (ecs, handle) |
| 344 | { |
| 345 | return ecs.get(handle, Component.equipment) ?? ecs.add(handle, Component.equipment, emptyEquipment()); |
| 346 | } |
| 347 | |
| 348 | function ensureVault (ecs, handle) |
| 349 | { |
| 350 | return ecs.get(handle, Component.vault) ?? ecs.add(handle, Component.vault, { slots: emptySlots(), currency: 0 }); |
| 351 | } |
| 352 | |
| 353 | function userError (message) |
| 354 | { |
| 355 | const error = new Error(message); |
| 356 | error.userFacing = true; |
| 357 | return error; |
| 358 | } |