| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 4 | import { ItemId } from "../../shared/definitions/items.js"; |
| 5 | import { pigeonSpeedAtProgress, advanceCourierFlight } from "../../shared/courier/flight.js"; |
| 6 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 7 | import { |
| 8 | makeCharacter, |
| 9 | makeSimulation, |
| 10 | stepTimes |
| 11 | } from "./helpers/simulationHarness.js"; |
| 12 | |
| 13 | test("pigeon flight uses takeoff, cruise, then landing speeds", () => |
| 14 | { |
| 15 | const settings = { |
| 16 | pigeonTakeoffTilesPerSec: 3, |
| 17 | pigeonCruiseTilesPerSec: 8, |
| 18 | pigeonLandingTilesPerSec: 3, |
| 19 | pigeonPhaseTiles: 5 |
| 20 | }; |
| 21 | assert.equal(pigeonSpeedAtProgress(0, 40, settings), 3); |
| 22 | assert.equal(pigeonSpeedAtProgress(4.9, 40, settings), 3); |
| 23 | assert.equal(pigeonSpeedAtProgress(10, 40, settings), 8); |
| 24 | assert.equal(pigeonSpeedAtProgress(36, 40, settings), 3); |
| 25 | |
| 26 | const takeoff = advanceCourierFlight({ |
| 27 | originX: 0, originY: 0, destX: 40, destY: 0, traveled: 0, dt: 1, settings |
| 28 | }); |
| 29 | assert.equal(takeoff.speed, 3); |
| 30 | assert.ok(Math.abs(takeoff.traveled - 3) < 1e-6); |
| 31 | |
| 32 | const cruise = advanceCourierFlight({ |
| 33 | originX: 0, originY: 0, destX: 40, destY: 0, traveled: 10, dt: 1, settings |
| 34 | }); |
| 35 | assert.equal(cruise.speed, 8); |
| 36 | assert.ok(Math.abs(cruise.traveled - 18) < 1e-6); |
| 37 | }); |
| 38 | |
| 39 | function placeBank (worldDb, tileX, tileY) |
| 40 | { |
| 41 | const chunkX = Math.floor(tileX / 16); |
| 42 | const chunkY = Math.floor(tileY / 16); |
| 43 | const data = createEmptyChunkData(); |
| 44 | data.open = true; |
| 45 | const localX = tileX - chunkX * 16; |
| 46 | const localY = tileY - chunkY * 16; |
| 47 | data.blocks[localY * 16 + localX] = { typeId: BlockType.bank, state: 0 }; |
| 48 | worldDb.saveChunkVersion( |
| 49 | chunkX, |
| 50 | chunkY, |
| 51 | worldDb.getActiveRevisionId(), |
| 52 | true, |
| 53 | encodeChunkBlob(data) |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | test("pigeon to bank deposits money into the sender vault", () => |
| 58 | { |
| 59 | const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 }); |
| 60 | try |
| 61 | { |
| 62 | placeBank(harness.worldDb, 20, 8); |
| 63 | harness.simulation.reloadWorld(); |
| 64 | harness.simulation.applySettings({ |
| 65 | pigeonTakeoffTilesPerSec: 20, |
| 66 | pigeonCruiseTilesPerSec: 20, |
| 67 | pigeonLandingTilesPerSec: 20, |
| 68 | pigeonPhaseTiles: 5, |
| 69 | simulationTickHz: 20 |
| 70 | }); |
| 71 | const character = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8 }); |
| 72 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 73 | const before = harness.simulation.bagOf(entityId); |
| 74 | assert.ok((before.currency | 0) >= 5); |
| 75 | const result = harness.simulation.dispatchPigeon({ |
| 76 | characterId: character.character_id, |
| 77 | destination: { kind: "bank" }, |
| 78 | cargo: { kind: "money", amount: 5 } |
| 79 | }); |
| 80 | assert.equal(result.transit.destinationKind, "bank"); |
| 81 | assert.equal(harness.simulation.bagOf(entityId).currency, before.currency - 5); |
| 82 | |
| 83 | stepTimes(harness.simulation, 80); |
| 84 | const after = harness.stateDb.getBag(character.character_id); |
| 85 | assert.equal(after.vaultCurrency, (before.vaultCurrency | 0) + 5); |
| 86 | assert.equal(harness.stateDb.listActiveCourierTransits().length, 0); |
| 87 | } |
| 88 | finally |
| 89 | { harness.close(); } |
| 90 | }); |
| 91 | |
| 92 | test("pigeon delivers an item to an online player inventory", () => |
| 93 | { |
| 94 | const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 }); |
| 95 | try |
| 96 | { |
| 97 | harness.simulation.applySettings({ |
| 98 | pigeonTakeoffTilesPerSec: 40, |
| 99 | pigeonCruiseTilesPerSec: 40, |
| 100 | pigeonLandingTilesPerSec: 40, |
| 101 | simulationTickHz: 20 |
| 102 | }); |
| 103 | const sender = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8, firstName: "Ada", lastName: "Sender" }); |
| 104 | const receiver = makeCharacter(harness.stateDb, { tileX: 10, tileY: 8, firstName: "Bob", lastName: "Receiver" }); |
| 105 | const senderId = harness.simulation.spawnCharacter(1, sender); |
| 106 | const receiverId = harness.simulation.spawnCharacter(2, receiver); |
| 107 | const bag = harness.simulation.bagOf(senderId); |
| 108 | const appleSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); |
| 109 | assert.ok(appleSlot >= 0); |
| 110 | const senderQty = bag.inventory[appleSlot].quantity; |
| 111 | const receiverBefore = harness.simulation.bagOf(receiverId); |
| 112 | const receiverApples = receiverBefore.inventory |
| 113 | .filter((slot) => slot?.itemId === ItemId.apple) |
| 114 | .reduce((sum, slot) => sum + slot.quantity, 0); |
| 115 | |
| 116 | harness.simulation.dispatchPigeon({ |
| 117 | characterId: sender.character_id, |
| 118 | destination: { kind: "player", firstName: "Bob", lastName: "Receiver" }, |
| 119 | cargo: { kind: "item", slotIndex: appleSlot, quantity: 1 } |
| 120 | }); |
| 121 | assert.equal(harness.simulation.bagOf(senderId).inventory[appleSlot].quantity, senderQty - 1); |
| 122 | |
| 123 | stepTimes(harness.simulation, 40); |
| 124 | const receiverAfter = harness.simulation.bagOf(receiverId); |
| 125 | const receiverApplesAfter = receiverAfter.inventory |
| 126 | .filter((slot) => slot?.itemId === ItemId.apple) |
| 127 | .reduce((sum, slot) => sum + slot.quantity, 0); |
| 128 | assert.equal(receiverApplesAfter, receiverApples + 1); |
| 129 | assert.equal(harness.stateDb.listActiveCourierTransits().length, 0); |
| 130 | } |
| 131 | finally |
| 132 | { harness.close(); } |
| 133 | }); |