| 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 coin stacks 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 | const coinSlot = before.inventory.findIndex((slot) => slot?.itemId === ItemId.coins); |
| 75 | assert.ok(coinSlot >= 0); |
| 76 | assert.ok(before.inventory[coinSlot].quantity >= 5); |
| 77 | const beforeCoins = before.inventory[coinSlot].quantity; |
| 78 | const result = harness.simulation.dispatchPigeon({ |
| 79 | characterId: character.character_id, |
| 80 | destination: { kind: "bank" }, |
| 81 | cargo: { kind: "item", slotIndex: coinSlot, quantity: 5 } |
| 82 | }); |
| 83 | assert.equal(result.transit.destinationKind, "bank"); |
| 84 | assert.equal(harness.simulation.bagOf(entityId).inventory[coinSlot].quantity, beforeCoins - 5); |
| 85 | |
| 86 | stepTimes(harness.simulation, 80); |
| 87 | const after = harness.stateDb.getBag(character.character_id); |
| 88 | assert.equal(countItem(after.vault, ItemId.coins), 5); |
| 89 | assert.equal(harness.stateDb.listActiveCourierTransits().length, 0); |
| 90 | } |
| 91 | finally |
| 92 | { harness.close(); } |
| 93 | }); |
| 94 | |
| 95 | function countItem (slots, itemId) |
| 96 | { |
| 97 | return (slots ?? []) |
| 98 | .filter((slot) => slot?.itemId === itemId) |
| 99 | .reduce((sum, slot) => sum + (slot.quantity | 0), 0); |
| 100 | } |
| 101 | |
| 102 | test("pigeon delivers an item to an online player inventory", () => |
| 103 | { |
| 104 | const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 }); |
| 105 | try |
| 106 | { |
| 107 | harness.simulation.applySettings({ |
| 108 | pigeonTakeoffTilesPerSec: 40, |
| 109 | pigeonCruiseTilesPerSec: 40, |
| 110 | pigeonLandingTilesPerSec: 40, |
| 111 | simulationTickHz: 20 |
| 112 | }); |
| 113 | const sender = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8, firstName: "Ada", lastName: "Sender" }); |
| 114 | const receiver = makeCharacter(harness.stateDb, { tileX: 10, tileY: 8, firstName: "Bob", lastName: "Receiver" }); |
| 115 | const senderId = harness.simulation.spawnCharacter(1, sender); |
| 116 | const receiverId = harness.simulation.spawnCharacter(2, receiver); |
| 117 | const bag = harness.simulation.bagOf(senderId); |
| 118 | const appleSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); |
| 119 | assert.ok(appleSlot >= 0); |
| 120 | const senderQty = bag.inventory[appleSlot].quantity; |
| 121 | const receiverBefore = harness.simulation.bagOf(receiverId); |
| 122 | const receiverApples = receiverBefore.inventory |
| 123 | .filter((slot) => slot?.itemId === ItemId.apple) |
| 124 | .reduce((sum, slot) => sum + slot.quantity, 0); |
| 125 | |
| 126 | harness.simulation.dispatchPigeon({ |
| 127 | characterId: sender.character_id, |
| 128 | destination: { kind: "player", firstName: "Bob", lastName: "Receiver" }, |
| 129 | cargo: { kind: "item", slotIndex: appleSlot, quantity: 1 } |
| 130 | }); |
| 131 | assert.equal(harness.simulation.bagOf(senderId).inventory[appleSlot].quantity, senderQty - 1); |
| 132 | |
| 133 | stepTimes(harness.simulation, 40); |
| 134 | const receiverAfter = harness.simulation.bagOf(receiverId); |
| 135 | const receiverApplesAfter = receiverAfter.inventory |
| 136 | .filter((slot) => slot?.itemId === ItemId.apple) |
| 137 | .reduce((sum, slot) => sum + slot.quantity, 0); |
| 138 | assert.equal(receiverApplesAfter, receiverApples + 1); |
| 139 | assert.equal(harness.stateDb.listActiveCourierTransits().length, 0); |
| 140 | } |
| 141 | finally |
| 142 | { harness.close(); } |
| 143 | }); |
| 144 | |
| 145 | test("pigeon rejects blank cheques", () => |
| 146 | { |
| 147 | const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 }); |
| 148 | try |
| 149 | { |
| 150 | placeBank(harness.worldDb, 8, 8); |
| 151 | harness.simulation.reloadWorld(); |
| 152 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 153 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 154 | harness.simulation.enqueue(1, { |
| 155 | type: "get-blank-cheques", |
| 156 | characterId: character.character_id |
| 157 | }); |
| 158 | harness.simulation.step(); |
| 159 | const bag = harness.simulation.bagOf(entityId); |
| 160 | const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque); |
| 161 | assert.ok(blankIndex >= 0); |
| 162 | assert.throws( |
| 163 | () => harness.simulation.dispatchPigeon({ |
| 164 | characterId: character.character_id, |
| 165 | destination: { kind: "bank" }, |
| 166 | cargo: { kind: "item", slotIndex: blankIndex, quantity: 1 } |
| 167 | }), |
| 168 | (err) => err.message === "Blank cheques cannot be sent by pigeon." |
| 169 | ); |
| 170 | } |
| 171 | finally |
| 172 | { harness.close(); } |
| 173 | }); |
| 174 | |
| 175 | test("pigeon to bank redeems a filled cheque into reserved goods", () => |
| 176 | { |
| 177 | const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 }); |
| 178 | try |
| 179 | { |
| 180 | placeBank(harness.worldDb, 20, 8); |
| 181 | placeBank(harness.worldDb, 8, 8); |
| 182 | harness.simulation.reloadWorld(); |
| 183 | harness.simulation.applySettings({ |
| 184 | pigeonTakeoffTilesPerSec: 40, |
| 185 | pigeonCruiseTilesPerSec: 40, |
| 186 | pigeonLandingTilesPerSec: 40, |
| 187 | simulationTickHz: 20 |
| 188 | }); |
| 189 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 190 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 191 | harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id }); |
| 192 | harness.simulation.step(); |
| 193 | let bag = harness.simulation.bagOf(entityId); |
| 194 | harness.simulation.enqueue(1, { |
| 195 | type: "bank-deposit", |
| 196 | characterId: character.character_id, |
| 197 | slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple), |
| 198 | quantity: null |
| 199 | }); |
| 200 | harness.simulation.step(); |
| 201 | bag = harness.simulation.bagOf(entityId); |
| 202 | const applesBefore = countItem(bag.vault, ItemId.apple); |
| 203 | harness.simulation.enqueue(1, { |
| 204 | type: "write-cheque", |
| 205 | characterId: character.character_id, |
| 206 | slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque), |
| 207 | vaultSlotIndex: bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple), |
| 208 | quantity: 2 |
| 209 | }); |
| 210 | harness.simulation.step(); |
| 211 | bag = harness.simulation.bagOf(entityId); |
| 212 | const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque); |
| 213 | assert.ok(chequeIndex >= 0); |
| 214 | assert.equal((bag.vaultReserved ?? []).length, 1); |
| 215 | |
| 216 | harness.simulation.dispatchPigeon({ |
| 217 | characterId: character.character_id, |
| 218 | destination: { kind: "bank" }, |
| 219 | cargo: { kind: "item", slotIndex: chequeIndex, quantity: 1 } |
| 220 | }); |
| 221 | stepTimes(harness.simulation, 80); |
| 222 | bag = harness.simulation.bagOf(entityId); |
| 223 | assert.equal(bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque), -1); |
| 224 | assert.equal(countItem(bag.vault, ItemId.filledCheque), 0); |
| 225 | assert.equal((bag.vaultReserved ?? []).length, 0); |
| 226 | assert.equal(countItem(bag.vault, ItemId.apple), applesBefore); |
| 227 | } |
| 228 | finally |
| 229 | { harness.close(); } |
| 230 | }); |