| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { mkdtempSync } from "node:fs"; |
| 4 | import { tmpdir } from "node:os"; |
| 5 | import { join } from "node:path"; |
| 6 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 7 | import { ItemId } from "../../shared/definitions/items.js"; |
| 8 | import { pigeonSpeedAtProgress, advanceCourierFlight } from "../../shared/courier/flight.js"; |
| 9 | import { ensureCourierLegs } from "../../shared/courier/doorwayGraph.js"; |
| 10 | import { OVERWORLD_ID, DimensionLightingMode, DimensionSeasonMode } from "../../shared/world/dimensions.js"; |
| 11 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 12 | import { WorldDatabase } from "../src/persistence/worldDb.js"; |
| 13 | import { StateDatabase } from "../src/persistence/stateDb.js"; |
| 14 | import { Simulation } from "../src/simulation/simulation.js"; |
| 15 | import { |
| 16 | makeCharacter, |
| 17 | makeSimulation, |
| 18 | stepTimes, |
| 19 | writeOpenChunk, |
| 20 | openChunkStrip |
| 21 | } from "./helpers/simulationHarness.js"; |
| 22 | |
| 23 | test("pigeon flight uses takeoff, cruise, then landing speeds", () => |
| 24 | { |
| 25 | const settings = { |
| 26 | pigeonTakeoffTilesPerSec: 3, |
| 27 | pigeonCruiseTilesPerSec: 8, |
| 28 | pigeonLandingTilesPerSec: 3, |
| 29 | pigeonPhaseTiles: 5 |
| 30 | }; |
| 31 | assert.equal(pigeonSpeedAtProgress(0, 40, settings), 3); |
| 32 | assert.equal(pigeonSpeedAtProgress(4.9, 40, settings), 3); |
| 33 | assert.equal(pigeonSpeedAtProgress(10, 40, settings), 8); |
| 34 | assert.equal(pigeonSpeedAtProgress(36, 40, settings), 3); |
| 35 | |
| 36 | const takeoff = advanceCourierFlight({ |
| 37 | originX: 0, originY: 0, destX: 40, destY: 0, traveled: 0, dt: 1, settings |
| 38 | }); |
| 39 | assert.equal(takeoff.speed, 3); |
| 40 | assert.ok(Math.abs(takeoff.traveled - 3) < 1e-6); |
| 41 | |
| 42 | const cruise = advanceCourierFlight({ |
| 43 | originX: 0, originY: 0, destX: 40, destY: 0, traveled: 10, dt: 1, settings |
| 44 | }); |
| 45 | assert.equal(cruise.speed, 8); |
| 46 | assert.ok(Math.abs(cruise.traveled - 18) < 1e-6); |
| 47 | }); |
| 48 | |
| 49 | function placeBank (worldDb, tileX, tileY) |
| 50 | { |
| 51 | const chunkX = Math.floor(tileX / 16); |
| 52 | const chunkY = Math.floor(tileY / 16); |
| 53 | const data = createEmptyChunkData(); |
| 54 | data.open = true; |
| 55 | const localX = tileX - chunkX * 16; |
| 56 | const localY = tileY - chunkY * 16; |
| 57 | data.blocks[localY * 16 + localX] = { typeId: BlockType.bank, state: 0 }; |
| 58 | worldDb.saveChunkVersion( |
| 59 | chunkX, |
| 60 | chunkY, |
| 61 | worldDb.getActiveRevisionId(), |
| 62 | true, |
| 63 | encodeChunkBlob(data) |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | function countItem (slots, itemId) |
| 68 | { |
| 69 | return (slots ?? []) |
| 70 | .filter((slot) => slot?.itemId === itemId) |
| 71 | .reduce((sum, slot) => sum + (slot.quantity | 0), 0); |
| 72 | } |
| 73 | |
| 74 | const CAVE_ID = "courier-cave"; |
| 75 | |
| 76 | function placeBlock (worldDb, tileX, tileY, block, dimensionId = OVERWORLD_ID) |
| 77 | { |
| 78 | const chunkX = Math.floor(tileX / 16); |
| 79 | const chunkY = Math.floor(tileY / 16); |
| 80 | const rev = worldDb.getActiveRevisionId(); |
| 81 | const existing = worldDb.getChunkVersion(chunkX, chunkY, rev, dimensionId); |
| 82 | const chunkData = createEmptyChunkData(); |
| 83 | chunkData.open = true; |
| 84 | if (existing?.data_blob) |
| 85 | { |
| 86 | const parsed = typeof existing.data_blob === "string" |
| 87 | ? JSON.parse(existing.data_blob) |
| 88 | : existing.data_blob; |
| 89 | chunkData.grounds = parsed.grounds ?? chunkData.grounds; |
| 90 | chunkData.blocks = parsed.blocks ?? chunkData.blocks; |
| 91 | chunkData.open = parsed.open ?? true; |
| 92 | } |
| 93 | const localX = tileX - chunkX * 16; |
| 94 | const localY = tileY - chunkY * 16; |
| 95 | chunkData.blocks[localY * 16 + localX] = block; |
| 96 | worldDb.saveChunkVersion( |
| 97 | chunkX, |
| 98 | chunkY, |
| 99 | rev, |
| 100 | true, |
| 101 | encodeChunkBlob(chunkData), |
| 102 | dimensionId |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | function placeDoorway (worldDb, tileX, tileY, targetDimensionId, targetTileX, targetTileY, dimensionId = OVERWORLD_ID) |
| 107 | { |
| 108 | placeBlock(worldDb, tileX, tileY, { |
| 109 | typeId: BlockType.doorway, |
| 110 | state: "door", |
| 111 | config: { |
| 112 | targetDimensionId, |
| 113 | tileX: targetTileX, |
| 114 | tileY: targetTileY |
| 115 | } |
| 116 | }, dimensionId); |
| 117 | } |
| 118 | |
| 119 | function placeBankDim (worldDb, tileX, tileY, dimensionId = OVERWORLD_ID) |
| 120 | { |
| 121 | placeBlock(worldDb, tileX, tileY, { typeId: BlockType.bank, state: 0 }, dimensionId); |
| 122 | } |
| 123 | |
| 124 | function makeDimCourierHarness () |
| 125 | { |
| 126 | const dir = mkdtempSync(join(tmpdir(), "mmo-courier-dim-")); |
| 127 | const worldDb = new WorldDatabase(join(dir, "world.sqlite")); |
| 128 | const stateDb = new StateDatabase(join(dir, "state.sqlite")); |
| 129 | openChunkStrip(worldDb, 2); |
| 130 | const rev = worldDb.getActiveRevisionId(); |
| 131 | worldDb.saveDimension(rev, { |
| 132 | dimensionId: CAVE_ID, |
| 133 | displayName: "Courier Cave", |
| 134 | lightingMode: DimensionLightingMode.alwaysLit, |
| 135 | seasonMode: DimensionSeasonMode.summer, |
| 136 | defaultGroundTypeId: 1 |
| 137 | }); |
| 138 | const cave = createEmptyChunkData(); |
| 139 | cave.open = true; |
| 140 | worldDb.saveChunkVersion(0, 0, rev, true, encodeChunkBlob(cave), CAVE_ID); |
| 141 | writeOpenChunk(worldDb, 0, 0); |
| 142 | writeOpenChunk(worldDb, 1, 0); |
| 143 | let now = 1_000; |
| 144 | const simulation = new Simulation(worldDb, stateDb, { |
| 145 | workerCount: 1, |
| 146 | workerMode: "inprocess", |
| 147 | now: () => now |
| 148 | }); |
| 149 | return { |
| 150 | simulation, |
| 151 | worldDb, |
| 152 | stateDb, |
| 153 | advance (ms) |
| 154 | { now += ms; }, |
| 155 | close () |
| 156 | { |
| 157 | simulation.forceUnloadAll(); |
| 158 | worldDb.close(); |
| 159 | stateDb.close(); |
| 160 | } |
| 161 | }; |
| 162 | } |
| 163 | |
| 164 | test("pigeon to bank deposits coin stacks into the sender vault", () => |
| 165 | { |
| 166 | const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 }); |
| 167 | try |
| 168 | { |
| 169 | placeBank(harness.worldDb, 20, 8); |
| 170 | harness.simulation.reloadWorld(); |
| 171 | harness.simulation.applySettings({ |
| 172 | pigeonTakeoffTilesPerSec: 20, |
| 173 | pigeonCruiseTilesPerSec: 20, |
| 174 | pigeonLandingTilesPerSec: 20, |
| 175 | pigeonPhaseTiles: 5, |
| 176 | simulationTickHz: 20 |
| 177 | }); |
| 178 | const character = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8 }); |
| 179 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 180 | const before = harness.simulation.bagOf(entityId); |
| 181 | const coinSlot = before.inventory.findIndex((slot) => slot?.itemId === ItemId.coins); |
| 182 | assert.ok(coinSlot >= 0); |
| 183 | assert.ok(before.inventory[coinSlot].quantity >= 5); |
| 184 | const beforeCoins = before.inventory[coinSlot].quantity; |
| 185 | const result = harness.simulation.dispatchPigeon({ |
| 186 | characterId: character.character_id, |
| 187 | destination: { kind: "bank" }, |
| 188 | cargo: { kind: "item", slotIndex: coinSlot, quantity: 5 } |
| 189 | }); |
| 190 | assert.equal(result.transit.destinationKind, "bank"); |
| 191 | assert.equal(harness.simulation.bagOf(entityId).inventory[coinSlot].quantity, beforeCoins - 5); |
| 192 | |
| 193 | stepTimes(harness.simulation, 80); |
| 194 | const after = harness.stateDb.getBag(character.character_id); |
| 195 | assert.equal(countItem(after.vault, ItemId.coins), 5); |
| 196 | assert.equal(harness.stateDb.listActiveCourierTransits().length, 0); |
| 197 | } |
| 198 | finally |
| 199 | { harness.close(); } |
| 200 | }); |
| 201 | |
| 202 | test("pigeon to offline player flies to the bank and credits the recipient vault", () => |
| 203 | { |
| 204 | const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 }); |
| 205 | try |
| 206 | { |
| 207 | placeBank(harness.worldDb, 20, 8); |
| 208 | harness.simulation.reloadWorld(); |
| 209 | harness.simulation.applySettings({ |
| 210 | pigeonTakeoffTilesPerSec: 40, |
| 211 | pigeonCruiseTilesPerSec: 40, |
| 212 | pigeonLandingTilesPerSec: 40, |
| 213 | simulationTickHz: 20 |
| 214 | }); |
| 215 | const sender = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8, firstName: "Ada", lastName: "Sender" }); |
| 216 | const receiver = makeCharacter(harness.stateDb, { tileX: 10, tileY: 8, firstName: "Bob", lastName: "Receiver" }); |
| 217 | const senderId = harness.simulation.spawnCharacter(1, sender); |
| 218 | harness.simulation.despawnCharacter(receiver.character_id); |
| 219 | const bag = harness.simulation.bagOf(senderId); |
| 220 | const appleSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); |
| 221 | assert.ok(appleSlot >= 0); |
| 222 | |
| 223 | const result = harness.simulation.dispatchPigeon({ |
| 224 | characterId: sender.character_id, |
| 225 | destination: { kind: "player", firstName: "Bob", lastName: "Receiver" }, |
| 226 | cargo: { kind: "item", slotIndex: appleSlot, quantity: 1 } |
| 227 | }); |
| 228 | assert.equal(result.transit.destinationKind, "bank"); |
| 229 | assert.equal(result.transit.vaultCharacterId, receiver.character_id); |
| 230 | assert.ok(Math.hypot(result.transit.destX - 20.5, result.transit.destY - 8.5) < 1); |
| 231 | |
| 232 | stepTimes(harness.simulation, 40); |
| 233 | const receiverVault = harness.stateDb.getBag(receiver.character_id); |
| 234 | assert.equal(countItem(receiverVault.vault, ItemId.apple), 1); |
| 235 | assert.equal(harness.stateDb.listActiveCourierTransits().length, 0); |
| 236 | } |
| 237 | finally |
| 238 | { harness.close(); } |
| 239 | }); |
| 240 | |
| 241 | test("logging in after offline bank deposits sends a bank resting notice by pigeon", () => |
| 242 | { |
| 243 | const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 }); |
| 244 | try |
| 245 | { |
| 246 | placeBank(harness.worldDb, 20, 8); |
| 247 | harness.simulation.reloadWorld(); |
| 248 | harness.simulation.applySettings({ |
| 249 | pigeonTakeoffTilesPerSec: 40, |
| 250 | pigeonCruiseTilesPerSec: 40, |
| 251 | pigeonLandingTilesPerSec: 40, |
| 252 | simulationTickHz: 20 |
| 253 | }); |
| 254 | const sender = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8, firstName: "Ada", lastName: "Sender" }); |
| 255 | const receiver = makeCharacter(harness.stateDb, { tileX: 10, tileY: 8, firstName: "Bob", lastName: "Receiver" }); |
| 256 | const senderId = harness.simulation.spawnCharacter(1, sender); |
| 257 | harness.simulation.despawnCharacter(receiver.character_id); |
| 258 | |
| 259 | const bag = harness.simulation.bagOf(senderId); |
| 260 | const appleSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); |
| 261 | harness.simulation.dispatchPigeon({ |
| 262 | characterId: sender.character_id, |
| 263 | destination: { kind: "player", firstName: "Bob", lastName: "Receiver" }, |
| 264 | cargo: { kind: "item", slotIndex: appleSlot, quantity: 1 } |
| 265 | }); |
| 266 | stepTimes(harness.simulation, 40); |
| 267 | assert.equal(harness.stateDb.listBankRestNotices(receiver.character_id).length, 1); |
| 268 | |
| 269 | const receiverId = harness.simulation.spawnCharacter(2, receiver); |
| 270 | const active = harness.stateDb.listActiveCourierTransits(); |
| 271 | assert.equal(active.length, 1); |
| 272 | assert.equal(active[0].destinationKind, "player"); |
| 273 | assert.equal(active[0].cargo.stack.itemId, ItemId.letter); |
| 274 | assert.match(active[0].cargo.stack.instance.body, /While you have been resting/); |
| 275 | assert.match(active[0].cargo.stack.instance.body, /Ada Sender/); |
| 276 | |
| 277 | stepTimes(harness.simulation, 40); |
| 278 | const receiverBag = harness.simulation.bagOf(receiverId); |
| 279 | const letterSlot = receiverBag.inventory.find((slot) => slot?.itemId === ItemId.letter); |
| 280 | assert.ok(letterSlot); |
| 281 | assert.equal(letterSlot.instance.authorDisplayName, "The Bank"); |
| 282 | assert.equal(letterSlot.origins?.[0]?.origin?.verb, "Written"); |
| 283 | assert.equal(letterSlot.origins?.[0]?.origin?.displayName, "The Bank"); |
| 284 | assert.doesNotMatch(letterSlot.instance.body, /Signed,/); |
| 285 | } |
| 286 | finally |
| 287 | { harness.close(); } |
| 288 | }); |
| 289 | |
| 290 | test("pigeon delivers an item to an online player inventory", () => |
| 291 | { |
| 292 | const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 }); |
| 293 | try |
| 294 | { |
| 295 | harness.simulation.applySettings({ |
| 296 | pigeonTakeoffTilesPerSec: 40, |
| 297 | pigeonCruiseTilesPerSec: 40, |
| 298 | pigeonLandingTilesPerSec: 40, |
| 299 | simulationTickHz: 20 |
| 300 | }); |
| 301 | const sender = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8, firstName: "Ada", lastName: "Sender" }); |
| 302 | const receiver = makeCharacter(harness.stateDb, { tileX: 10, tileY: 8, firstName: "Bob", lastName: "Receiver" }); |
| 303 | const senderId = harness.simulation.spawnCharacter(1, sender); |
| 304 | const receiverId = harness.simulation.spawnCharacter(2, receiver); |
| 305 | const bag = harness.simulation.bagOf(senderId); |
| 306 | const appleSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); |
| 307 | assert.ok(appleSlot >= 0); |
| 308 | const senderQty = bag.inventory[appleSlot].quantity; |
| 309 | const receiverBefore = harness.simulation.bagOf(receiverId); |
| 310 | const receiverApples = receiverBefore.inventory |
| 311 | .filter((slot) => slot?.itemId === ItemId.apple) |
| 312 | .reduce((sum, slot) => sum + slot.quantity, 0); |
| 313 | |
| 314 | harness.simulation.dispatchPigeon({ |
| 315 | characterId: sender.character_id, |
| 316 | destination: { kind: "player", firstName: "Bob", lastName: "Receiver" }, |
| 317 | cargo: { kind: "item", slotIndex: appleSlot, quantity: 1 } |
| 318 | }); |
| 319 | assert.equal(harness.simulation.bagOf(senderId).inventory[appleSlot].quantity, senderQty - 1); |
| 320 | |
| 321 | const events = stepTimes(harness.simulation, 40); |
| 322 | const receiverAfter = harness.simulation.bagOf(receiverId); |
| 323 | const receiverApplesAfter = receiverAfter.inventory |
| 324 | .filter((slot) => slot?.itemId === ItemId.apple) |
| 325 | .reduce((sum, slot) => sum + slot.quantity, 0); |
| 326 | assert.equal(receiverApplesAfter, receiverApples + 1); |
| 327 | assert.equal(harness.stateDb.listActiveCourierTransits().length, 0); |
| 328 | assert.ok(events.some((event) => |
| 329 | event.type === "item-bubble" |
| 330 | && event.entityId === receiverId |
| 331 | && event.itemId === ItemId.apple |
| 332 | && event.quantity === 1 |
| 333 | ), "pigeon delivery should show the craft-style item bubble"); |
| 334 | } |
| 335 | finally |
| 336 | { harness.close(); } |
| 337 | }); |
| 338 | |
| 339 | test("pigeon rejects blank cheques", () => |
| 340 | { |
| 341 | const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 }); |
| 342 | try |
| 343 | { |
| 344 | placeBank(harness.worldDb, 8, 8); |
| 345 | harness.simulation.reloadWorld(); |
| 346 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 347 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 348 | harness.simulation.enqueue(1, { |
| 349 | type: "get-blank-cheques", |
| 350 | characterId: character.character_id |
| 351 | }); |
| 352 | harness.simulation.step(); |
| 353 | const bag = harness.simulation.bagOf(entityId); |
| 354 | const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque); |
| 355 | assert.ok(blankIndex >= 0); |
| 356 | assert.throws( |
| 357 | () => harness.simulation.dispatchPigeon({ |
| 358 | characterId: character.character_id, |
| 359 | destination: { kind: "bank" }, |
| 360 | cargo: { kind: "item", slotIndex: blankIndex, quantity: 1 } |
| 361 | }), |
| 362 | (err) => err.message === "Blank cheques cannot be sent by pigeon." |
| 363 | ); |
| 364 | } |
| 365 | finally |
| 366 | { harness.close(); } |
| 367 | }); |
| 368 | |
| 369 | test("pigeon to bank redeems a filled cheque into reserved goods", () => |
| 370 | { |
| 371 | const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 }); |
| 372 | try |
| 373 | { |
| 374 | placeBank(harness.worldDb, 20, 8); |
| 375 | placeBank(harness.worldDb, 8, 8); |
| 376 | harness.simulation.reloadWorld(); |
| 377 | harness.simulation.applySettings({ |
| 378 | pigeonTakeoffTilesPerSec: 40, |
| 379 | pigeonCruiseTilesPerSec: 40, |
| 380 | pigeonLandingTilesPerSec: 40, |
| 381 | simulationTickHz: 20 |
| 382 | }); |
| 383 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 384 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 385 | harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id }); |
| 386 | harness.simulation.step(); |
| 387 | let bag = harness.simulation.bagOf(entityId); |
| 388 | harness.simulation.enqueue(1, { |
| 389 | type: "bank-deposit", |
| 390 | characterId: character.character_id, |
| 391 | slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple), |
| 392 | quantity: null |
| 393 | }); |
| 394 | harness.simulation.step(); |
| 395 | bag = harness.simulation.bagOf(entityId); |
| 396 | const applesBefore = countItem(bag.vault, ItemId.apple); |
| 397 | harness.simulation.enqueue(1, { |
| 398 | type: "write-cheque", |
| 399 | characterId: character.character_id, |
| 400 | slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque), |
| 401 | vaultSlotIndex: bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple), |
| 402 | quantity: 2 |
| 403 | }); |
| 404 | harness.simulation.step(); |
| 405 | bag = harness.simulation.bagOf(entityId); |
| 406 | const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque); |
| 407 | assert.ok(chequeIndex >= 0); |
| 408 | assert.equal((bag.vaultReserved ?? []).length, 1); |
| 409 | |
| 410 | harness.simulation.dispatchPigeon({ |
| 411 | characterId: character.character_id, |
| 412 | destination: { kind: "bank" }, |
| 413 | cargo: { kind: "item", slotIndex: chequeIndex, quantity: 1 } |
| 414 | }); |
| 415 | stepTimes(harness.simulation, 80); |
| 416 | bag = harness.simulation.bagOf(entityId); |
| 417 | assert.equal(bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque), -1); |
| 418 | assert.equal(countItem(bag.vault, ItemId.filledCheque), 0); |
| 419 | assert.equal((bag.vaultReserved ?? []).length, 0); |
| 420 | assert.equal(countItem(bag.vault, ItemId.apple), applesBefore); |
| 421 | } |
| 422 | finally |
| 423 | { harness.close(); } |
| 424 | }); |
| 425 | |
| 426 | test("same-dimension pigeon still delivers with multi-leg schema", () => |
| 427 | { |
| 428 | const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 }); |
| 429 | try |
| 430 | { |
| 431 | placeBank(harness.worldDb, 20, 8); |
| 432 | harness.simulation.reloadWorld(); |
| 433 | harness.simulation.applySettings({ |
| 434 | pigeonTakeoffTilesPerSec: 40, |
| 435 | pigeonCruiseTilesPerSec: 40, |
| 436 | pigeonLandingTilesPerSec: 40, |
| 437 | simulationTickHz: 20 |
| 438 | }); |
| 439 | const character = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8 }); |
| 440 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 441 | const bag = harness.simulation.bagOf(entityId); |
| 442 | const coinSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.coins); |
| 443 | const result = harness.simulation.dispatchPigeon({ |
| 444 | characterId: character.character_id, |
| 445 | destination: { kind: "bank" }, |
| 446 | cargo: { kind: "item", slotIndex: coinSlot, quantity: 3 } |
| 447 | }); |
| 448 | assert.equal(result.transit.originDimensionId, OVERWORLD_ID); |
| 449 | assert.equal(result.transit.destDimensionId, OVERWORLD_ID); |
| 450 | assert.ok(Array.isArray(result.transit.legs)); |
| 451 | assert.equal(result.transit.legs.length, 1); |
| 452 | assert.equal(result.transit.legs[0].kind, "fly"); |
| 453 | stepTimes(harness.simulation, 80); |
| 454 | const after = harness.stateDb.getBag(character.character_id); |
| 455 | assert.equal(countItem(after.vault, ItemId.coins), 3); |
| 456 | } |
| 457 | finally |
| 458 | { harness.close(); } |
| 459 | }); |
| 460 | |
| 461 | test("cross-dimension pigeon flies through doorway portals", () => |
| 462 | { |
| 463 | const harness = makeDimCourierHarness(); |
| 464 | try |
| 465 | { |
| 466 | placeBankDim(harness.worldDb, 20, 8, OVERWORLD_ID); |
| 467 | placeDoorway(harness.worldDb, 4, 8, OVERWORLD_ID, 8, 8, CAVE_ID); |
| 468 | harness.simulation.reloadWorld(); |
| 469 | harness.simulation.applySettings({ |
| 470 | pigeonTakeoffTilesPerSec: 40, |
| 471 | pigeonCruiseTilesPerSec: 40, |
| 472 | pigeonLandingTilesPerSec: 40, |
| 473 | simulationTickHz: 20 |
| 474 | }); |
| 475 | const character = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8 }); |
| 476 | harness.stateDb.saveCharacterPosition(character.character_id, 2, 8, "walk", CAVE_ID); |
| 477 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 478 | assert.equal(harness.simulation.positionOf(entityId).dimensionId, CAVE_ID); |
| 479 | const bag = harness.simulation.bagOf(entityId); |
| 480 | const coinSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.coins); |
| 481 | const result = harness.simulation.dispatchPigeon({ |
| 482 | characterId: character.character_id, |
| 483 | destination: { kind: "bank" }, |
| 484 | cargo: { kind: "item", slotIndex: coinSlot, quantity: 2 } |
| 485 | }); |
| 486 | assert.equal(result.transit.originDimensionId, CAVE_ID); |
| 487 | assert.equal(result.transit.destDimensionId, OVERWORLD_ID); |
| 488 | assert.ok(result.transit.legs.some((leg) => leg.kind === "portal")); |
| 489 | assert.ok(result.transit.legs.some((leg) => leg.kind === "fly")); |
| 490 | stepTimes(harness.simulation, 200); |
| 491 | const after = harness.stateDb.getBag(character.character_id); |
| 492 | assert.equal(countItem(after.vault, ItemId.coins), 2); |
| 493 | assert.equal(harness.stateDb.listActiveCourierTransits().length, 0); |
| 494 | } |
| 495 | finally |
| 496 | { harness.close(); } |
| 497 | }); |
| 498 | |
| 499 | test("cross-dimension pigeon without doorway path uses fade/abstract fallback", () => |
| 500 | { |
| 501 | const harness = makeDimCourierHarness(); |
| 502 | try |
| 503 | { |
| 504 | placeBankDim(harness.worldDb, 20, 8, OVERWORLD_ID); |
| 505 | harness.simulation.reloadWorld(); |
| 506 | harness.simulation.applySettings({ |
| 507 | pigeonTakeoffTilesPerSec: 40, |
| 508 | pigeonCruiseTilesPerSec: 40, |
| 509 | pigeonLandingTilesPerSec: 40, |
| 510 | simulationTickHz: 20 |
| 511 | }); |
| 512 | const character = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8 }); |
| 513 | harness.stateDb.saveCharacterPosition(character.character_id, 2, 8, "walk", CAVE_ID); |
| 514 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 515 | const bag = harness.simulation.bagOf(entityId); |
| 516 | const coinSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.coins); |
| 517 | const result = harness.simulation.dispatchPigeon({ |
| 518 | characterId: character.character_id, |
| 519 | destination: { kind: "bank" }, |
| 520 | cargo: { kind: "item", slotIndex: coinSlot, quantity: 1 } |
| 521 | }); |
| 522 | assert.ok(result.transit.legs.some((leg) => leg.kind === "abstract")); |
| 523 | assert.ok(!result.transit.legs.some((leg) => leg.kind === "portal")); |
| 524 | stepTimes(harness.simulation, 300); |
| 525 | const after = harness.stateDb.getBag(character.character_id); |
| 526 | assert.equal(countItem(after.vault, ItemId.coins), 1); |
| 527 | assert.equal(harness.stateDb.listActiveCourierTransits().length, 0); |
| 528 | } |
| 529 | finally |
| 530 | { harness.close(); } |
| 531 | }); |
| 532 | |
| 533 | test("legacy courier_transits rows migrate to overworld single-fly legs", () => |
| 534 | { |
| 535 | const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 }); |
| 536 | try |
| 537 | { |
| 538 | placeBank(harness.worldDb, 20, 8); |
| 539 | harness.simulation.reloadWorld(); |
| 540 | harness.simulation.applySettings({ |
| 541 | pigeonTakeoffTilesPerSec: 40, |
| 542 | pigeonCruiseTilesPerSec: 40, |
| 543 | pigeonLandingTilesPerSec: 40, |
| 544 | simulationTickHz: 20 |
| 545 | }); |
| 546 | const character = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8 }); |
| 547 | harness.simulation.spawnCharacter(1, character); |
| 548 | const now = Date.now(); |
| 549 | harness.stateDb.db.prepare(` |
| 550 | INSERT INTO courier_transits ( |
| 551 | sender_character_id, destination_kind, destination_character_id, cargo_blob, |
| 552 | origin_x, origin_y, dest_x, dest_y, traveled, status, entity_id, updated_at, created_at, |
| 553 | vault_character_id |
| 554 | ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
| 555 | `).run( |
| 556 | character.character_id, |
| 557 | "bank", |
| 558 | null, |
| 559 | JSON.stringify({ kind: "item", stack: { itemId: ItemId.coins, quantity: 4 } }), |
| 560 | 2.5, |
| 561 | 8.5, |
| 562 | 20.5, |
| 563 | 8.5, |
| 564 | 0, |
| 565 | "in_transit", |
| 566 | null, |
| 567 | now, |
| 568 | now, |
| 569 | character.character_id |
| 570 | ); |
| 571 | const listed = harness.stateDb.listActiveCourierTransits(); |
| 572 | assert.equal(listed.length, 1); |
| 573 | assert.equal(listed[0].originDimensionId, OVERWORLD_ID); |
| 574 | assert.equal(listed[0].destDimensionId, OVERWORLD_ID); |
| 575 | assert.equal(listed[0].legs, null); |
| 576 | ensureCourierLegs(listed[0]); |
| 577 | assert.equal(listed[0].legs.length, 1); |
| 578 | stepTimes(harness.simulation, 80); |
| 579 | const after = harness.stateDb.getBag(character.character_id); |
| 580 | assert.equal(countItem(after.vault, ItemId.coins), 4); |
| 581 | assert.equal(harness.stateDb.listActiveCourierTransits().length, 0); |
| 582 | } |
| 583 | finally |
| 584 | { harness.close(); } |
| 585 | }); |