| 1 | import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; |
| 2 | import { BlockType } from "../../../shared/definitions/tiles.js"; |
| 3 | import { MobId, mobDefinition, mobSpriteName } from "../../../shared/definitions/mobs.js"; |
| 4 | import { itemDefinition } from "../../../shared/definitions/items.js"; |
| 5 | import { insertStack, takeFromSlot, emptySlots } from "../../../shared/items/inventory.js"; |
| 6 | import { advanceCourierFlight } from "../../../shared/courier/flight.js"; |
| 7 | import { GAIT } from "../../../shared/protocol/constants.js"; |
| 8 | import { decodeChunkBlob } from "../world/chunkData.js"; |
| 9 | |
| 10 | export class CourierService |
| 11 | { |
| 12 | constructor ({ stateDb, worldDb, simulation }) |
| 13 | { |
| 14 | this.stateDb = stateDb; |
| 15 | this.worldDb = worldDb; |
| 16 | this.simulation = simulation; |
| 17 | this.bankTiles = []; |
| 18 | this.refreshBankIndex(); |
| 19 | } |
| 20 | |
| 21 | refreshBankIndex () |
| 22 | { |
| 23 | const revisionId = this.worldDb.getActiveRevisionId(); |
| 24 | const banks = []; |
| 25 | for (const coord of this.worldDb.listChunkCoords(revisionId)) |
| 26 | { |
| 27 | const row = this.worldDb.getChunkVersion(coord.chunk_x, coord.chunk_y, revisionId); |
| 28 | if (!row) |
| 29 | { continue; } |
| 30 | const data = decodeChunkBlob(row.data_blob, this.simulation.settings.defaultGroundTypeId); |
| 31 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 32 | { |
| 33 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 34 | { |
| 35 | const block = data.blocks[localY * CHUNK_SIZE + localX]; |
| 36 | if (block?.typeId !== BlockType.bank) |
| 37 | { continue; } |
| 38 | banks.push({ |
| 39 | tileX: coord.chunk_x * CHUNK_SIZE + localX, |
| 40 | tileY: coord.chunk_y * CHUNK_SIZE + localY |
| 41 | }); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | this.bankTiles = banks; |
| 46 | } |
| 47 | |
| 48 | nearestBank (tileX, tileY) |
| 49 | { |
| 50 | if (!this.bankTiles.length) |
| 51 | { return null; } |
| 52 | let best = null; |
| 53 | let bestDist = Infinity; |
| 54 | for (const bank of this.bankTiles) |
| 55 | { |
| 56 | const dist = Math.hypot(bank.tileX - tileX, bank.tileY - tileY); |
| 57 | if (dist < bestDist) |
| 58 | { |
| 59 | bestDist = dist; |
| 60 | best = bank; |
| 61 | } |
| 62 | } |
| 63 | return best; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Player dispatch: cargo is money or one inventory stack. |
| 68 | * destination: { kind: "bank" } | { kind: "player", firstName, lastName } |
| 69 | */ |
| 70 | dispatchFromPlayer ({ characterId, originTileX, originTileY, destination, cargo }) |
| 71 | { |
| 72 | const onlineId = this.simulation.characterEntities.get(characterId); |
| 73 | const bag = onlineId |
| 74 | ? this.simulation.bagOf(onlineId) |
| 75 | : this.stateDb.getBag(characterId); |
| 76 | if (!bag) |
| 77 | { |
| 78 | const error = new Error("No bag."); |
| 79 | error.userFacing = true; |
| 80 | throw error; |
| 81 | } |
| 82 | |
| 83 | let takenCargo; |
| 84 | if (cargo?.kind === "money") |
| 85 | { |
| 86 | const amount = Math.max(0, cargo.amount | 0); |
| 87 | if (amount <= 0 || amount > (bag.currency | 0)) |
| 88 | { |
| 89 | const error = new Error("Not enough coins."); |
| 90 | error.userFacing = true; |
| 91 | throw error; |
| 92 | } |
| 93 | bag.currency -= amount; |
| 94 | takenCargo = { kind: "money", amount }; |
| 95 | } |
| 96 | else if (cargo?.kind === "item") |
| 97 | { |
| 98 | const taken = takeFromSlot(bag.inventory, cargo.slotIndex | 0, cargo.quantity ?? null); |
| 99 | if (!taken.ok || !taken.taken) |
| 100 | { |
| 101 | const error = new Error("That item is not available."); |
| 102 | error.userFacing = true; |
| 103 | throw error; |
| 104 | } |
| 105 | bag.inventory = taken.slots; |
| 106 | takenCargo = { kind: "item", stack: taken.taken }; |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | const error = new Error("Pigeon cargo must be money or one item stack."); |
| 111 | error.userFacing = true; |
| 112 | throw error; |
| 113 | } |
| 114 | |
| 115 | const dest = this.#resolveDestination(destination, originTileX, originTileY); |
| 116 | this.#persistBag(characterId, bag); |
| 117 | const transit = this.stateDb.insertCourierTransit({ |
| 118 | senderCharacterId: characterId, |
| 119 | destinationKind: dest.kind, |
| 120 | destinationCharacterId: dest.characterId ?? null, |
| 121 | cargo: takenCargo, |
| 122 | originX: originTileX + 0.5, |
| 123 | originY: originTileY + 0.5, |
| 124 | destX: dest.tileX + 0.5, |
| 125 | destY: dest.tileY + 0.5, |
| 126 | traveled: 0, |
| 127 | status: "in_transit", |
| 128 | entityId: null |
| 129 | }); |
| 130 | return { transit, bag }; |
| 131 | } |
| 132 | |
| 133 | /** Market / system dispatch already holding cargo (no bag take). */ |
| 134 | dispatchHeldCargo ({ senderCharacterId, originTileX, originTileY, destination, cargo }) |
| 135 | { |
| 136 | const dest = this.#resolveDestination(destination, originTileX, originTileY); |
| 137 | return this.stateDb.insertCourierTransit({ |
| 138 | senderCharacterId, |
| 139 | destinationKind: dest.kind, |
| 140 | destinationCharacterId: dest.characterId ?? null, |
| 141 | cargo, |
| 142 | originX: originTileX + 0.5, |
| 143 | originY: originTileY + 0.5, |
| 144 | destX: dest.tileX + 0.5, |
| 145 | destY: dest.tileY + 0.5, |
| 146 | traveled: 0, |
| 147 | status: "in_transit", |
| 148 | entityId: null |
| 149 | }); |
| 150 | } |
| 151 | |
| 152 | step (now, dt) |
| 153 | { |
| 154 | const events = []; |
| 155 | const settings = this.simulation.settings; |
| 156 | for (const transit of this.stateDb.listActiveCourierTransits()) |
| 157 | { |
| 158 | this.#retargetPlayerDest(transit); |
| 159 | const advanced = advanceCourierFlight({ |
| 160 | originX: transit.originX, |
| 161 | originY: transit.originY, |
| 162 | destX: transit.destX, |
| 163 | destY: transit.destY, |
| 164 | traveled: transit.traveled, |
| 165 | dt, |
| 166 | settings |
| 167 | }); |
| 168 | transit.traveled = advanced.traveled; |
| 169 | transit.updatedAt = now; |
| 170 | |
| 171 | if (advanced.arrived) |
| 172 | { |
| 173 | events.push(...this.#deliver(transit, now)); |
| 174 | continue; |
| 175 | } |
| 176 | |
| 177 | events.push(...this.#syncVisiblePigeon(transit, advanced, now)); |
| 178 | this.stateDb.updateCourierTransit(transit); |
| 179 | } |
| 180 | return events; |
| 181 | } |
| 182 | |
| 183 | #retargetPlayerDest (transit) |
| 184 | { |
| 185 | if (transit.destinationKind !== "player" || !transit.destinationCharacterId) |
| 186 | { return; } |
| 187 | const onlineId = this.simulation.characterEntities.get(transit.destinationCharacterId); |
| 188 | if (onlineId) |
| 189 | { |
| 190 | const pos = this.simulation.positionOf(onlineId); |
| 191 | if (pos) |
| 192 | { |
| 193 | transit.destX = pos.tileX + 0.5; |
| 194 | transit.destY = pos.tileY + 0.5; |
| 195 | } |
| 196 | return; |
| 197 | } |
| 198 | const character = this.stateDb.getCharacter(transit.destinationCharacterId); |
| 199 | if (character) |
| 200 | { |
| 201 | transit.destX = character.tile_x + 0.5; |
| 202 | transit.destY = character.tile_y + 0.5; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | #resolveDestination (destination, originTileX, originTileY) |
| 207 | { |
| 208 | if (destination?.kind === "player") |
| 209 | { |
| 210 | const character = this.stateDb.getCharacterByName(destination.firstName, destination.lastName); |
| 211 | if (!character) |
| 212 | { |
| 213 | const error = new Error("No character with that name."); |
| 214 | error.userFacing = true; |
| 215 | throw error; |
| 216 | } |
| 217 | const onlineId = this.simulation.characterEntities.get(character.character_id); |
| 218 | const pos = onlineId ? this.simulation.positionOf(onlineId) : null; |
| 219 | return { |
| 220 | kind: "player", |
| 221 | characterId: character.character_id, |
| 222 | tileX: pos?.tileX ?? character.tile_x, |
| 223 | tileY: pos?.tileY ?? character.tile_y |
| 224 | }; |
| 225 | } |
| 226 | if (destination?.kind === "bank") |
| 227 | { |
| 228 | const bank = this.nearestBank(originTileX, originTileY); |
| 229 | if (!bank) |
| 230 | { |
| 231 | const error = new Error("No bank exists in the world yet."); |
| 232 | error.userFacing = true; |
| 233 | throw error; |
| 234 | } |
| 235 | return { kind: "bank", tileX: bank.tileX, tileY: bank.tileY }; |
| 236 | } |
| 237 | const error = new Error("Pigeon destination must be bank or player."); |
| 238 | error.userFacing = true; |
| 239 | throw error; |
| 240 | } |
| 241 | |
| 242 | #syncVisiblePigeon (transit, advanced, now) |
| 243 | { |
| 244 | const events = []; |
| 245 | const tileX = Math.floor(advanced.x); |
| 246 | const tileY = Math.floor(advanced.y); |
| 247 | if (!this.simulation.chunkIsSimulating(tileX, tileY)) |
| 248 | { |
| 249 | if (transit.entityId) |
| 250 | { |
| 251 | this.simulation.despawnEntity(transit.entityId); |
| 252 | events.push({ type: "entity-despawn", entityId: transit.entityId, tileX, tileY }); |
| 253 | transit.entityId = null; |
| 254 | } |
| 255 | return events; |
| 256 | } |
| 257 | |
| 258 | if (!transit.entityId || !this.simulation.entityRecord(transit.entityId)) |
| 259 | { |
| 260 | transit.entityId = null; |
| 261 | const def = mobDefinition(MobId.pigeon); |
| 262 | const entityId = this.simulation.spawnEntity({ |
| 263 | position: { tileX, tileY }, |
| 264 | movement: { gait: GAIT.walk, walkCarry: 0 }, |
| 265 | path: { tiles: [] }, |
| 266 | flight: { |
| 267 | x: advanced.x, |
| 268 | y: advanced.y, |
| 269 | targetX: transit.destX, |
| 270 | targetY: transit.destY, |
| 271 | speed: advanced.speed, |
| 272 | nextPickAt: now + 60_000, |
| 273 | lastAt: now, |
| 274 | courierId: transit.courierId |
| 275 | }, |
| 276 | appearance: { |
| 277 | sex: def.sex, |
| 278 | firstName: def.displayName, |
| 279 | lastName: "", |
| 280 | sprite: mobSpriteName(MobId.pigeon) |
| 281 | }, |
| 282 | creature: { mobTypeId: MobId.pigeon }, |
| 283 | health: { current: 1, max: 1 } |
| 284 | }, { requireResident: true }); |
| 285 | if (entityId != null) |
| 286 | { transit.entityId = entityId; } |
| 287 | return events; |
| 288 | } |
| 289 | |
| 290 | const moved = this.simulation.updateCourierFlight(transit.entityId, { |
| 291 | x: advanced.x, |
| 292 | y: advanced.y, |
| 293 | targetX: transit.destX, |
| 294 | targetY: transit.destY, |
| 295 | speed: advanced.speed, |
| 296 | lastAt: now, |
| 297 | courierId: transit.courierId |
| 298 | }, now); |
| 299 | if (moved?.despawned) |
| 300 | { |
| 301 | transit.entityId = null; |
| 302 | events.push({ type: "entity-despawn", entityId: moved.entityId, tileX, tileY }); |
| 303 | } |
| 304 | else if (moved?.event) |
| 305 | { events.push(moved.event); } |
| 306 | return events; |
| 307 | } |
| 308 | |
| 309 | #deliver (transit, now) |
| 310 | { |
| 311 | const events = []; |
| 312 | if (transit.entityId) |
| 313 | { |
| 314 | this.simulation.despawnEntity(transit.entityId); |
| 315 | events.push({ |
| 316 | type: "entity-despawn", |
| 317 | entityId: transit.entityId, |
| 318 | tileX: Math.floor(transit.destX), |
| 319 | tileY: Math.floor(transit.destY) |
| 320 | }); |
| 321 | transit.entityId = null; |
| 322 | } |
| 323 | |
| 324 | if (transit.destinationKind === "bank") |
| 325 | { this.#creditVault(transit.senderCharacterId, transit.cargo); } |
| 326 | else if (transit.destinationKind === "player") |
| 327 | { |
| 328 | const delivered = this.#tryDeliverToPlayer(transit.destinationCharacterId, transit.cargo); |
| 329 | if (!delivered) |
| 330 | { |
| 331 | const bank = this.nearestBank(Math.floor(transit.destX), Math.floor(transit.destY)) |
| 332 | ?? this.nearestBank(Math.floor(transit.originX), Math.floor(transit.originY)); |
| 333 | if (bank) |
| 334 | { |
| 335 | transit.originX = transit.destX; |
| 336 | transit.originY = transit.destY; |
| 337 | transit.destX = bank.tileX + 0.5; |
| 338 | transit.destY = bank.tileY + 0.5; |
| 339 | transit.traveled = 0; |
| 340 | transit.destinationKind = "bank"; |
| 341 | transit.updatedAt = now; |
| 342 | this.stateDb.updateCourierTransit(transit); |
| 343 | events.push({ |
| 344 | type: "pigeon-reroute-bank", |
| 345 | courierId: transit.courierId, |
| 346 | characterId: transit.destinationCharacterId |
| 347 | }); |
| 348 | return events; |
| 349 | } |
| 350 | this.#creditVault(transit.destinationCharacterId, transit.cargo); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | transit.status = "delivered"; |
| 355 | transit.traveled = Math.hypot(transit.destX - transit.originX, transit.destY - transit.originY); |
| 356 | transit.updatedAt = now; |
| 357 | this.stateDb.updateCourierTransit(transit); |
| 358 | events.push({ |
| 359 | type: "pigeon-delivered", |
| 360 | courierId: transit.courierId, |
| 361 | senderCharacterId: transit.senderCharacterId, |
| 362 | destinationKind: transit.destinationKind, |
| 363 | destinationCharacterId: transit.destinationCharacterId, |
| 364 | cargo: transit.cargo |
| 365 | }); |
| 366 | return events; |
| 367 | } |
| 368 | |
| 369 | #tryDeliverToPlayer (characterId, cargo) |
| 370 | { |
| 371 | const onlineId = this.simulation.characterEntities.get(characterId); |
| 372 | if (onlineId) |
| 373 | { |
| 374 | if (cargo.kind === "money") |
| 375 | { |
| 376 | const bag = this.simulation.bagOf(onlineId); |
| 377 | if (!bag) |
| 378 | { return false; } |
| 379 | bag.currency = (bag.currency | 0) + (cargo.amount | 0); |
| 380 | this.#persistBag(characterId, bag); |
| 381 | return true; |
| 382 | } |
| 383 | if (cargo.kind === "item") |
| 384 | { |
| 385 | const bag = this.simulation.bagOf(onlineId); |
| 386 | if (!bag) |
| 387 | { return false; } |
| 388 | const def = itemDefinition(cargo.stack.itemId); |
| 389 | const result = insertStack(bag.inventory, cargo.stack, def); |
| 390 | if (!result.ok || result.remainder) |
| 391 | { return false; } |
| 392 | bag.inventory = result.slots; |
| 393 | this.#persistBag(characterId, bag); |
| 394 | return true; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | if (cargo.kind === "money") |
| 399 | { |
| 400 | this.#creditVault(characterId, cargo); |
| 401 | return true; |
| 402 | } |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | #creditVault (characterId, cargo) |
| 407 | { |
| 408 | const onlineId = this.simulation.characterEntities.get(characterId); |
| 409 | const bag = onlineId |
| 410 | ? this.simulation.bagOf(onlineId) |
| 411 | : this.stateDb.getBag(characterId); |
| 412 | if (!bag) |
| 413 | { return; } |
| 414 | if (cargo.kind === "money") |
| 415 | { |
| 416 | bag.vaultCurrency = (bag.vaultCurrency | 0) + (cargo.amount | 0); |
| 417 | this.#persistBag(characterId, bag); |
| 418 | return; |
| 419 | } |
| 420 | if (cargo.kind === "item") |
| 421 | { |
| 422 | const def = itemDefinition(cargo.stack.itemId); |
| 423 | const result = insertStack(bag.vault ?? emptySlots(), cargo.stack, def); |
| 424 | bag.vault = result.ok ? result.slots : [...(bag.vault ?? []), cargo.stack]; |
| 425 | this.#persistBag(characterId, bag); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | #persistBag (characterId, bag) |
| 430 | { |
| 431 | this.stateDb.saveBag(characterId, bag); |
| 432 | const onlineId = this.simulation.characterEntities.get(characterId); |
| 433 | if (onlineId) |
| 434 | { |
| 435 | this.simulation.applyBag(onlineId, bag); |
| 436 | this.simulation.pushInventory(characterId, bag); |
| 437 | } |
| 438 | } |
| 439 | } |