| 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 to offline player flies to the bank and credits the recipient vault", () => |
| 103 | { |
| 104 | const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 }); |
| 105 | try |
| 106 | { |
| 107 | placeBank(harness.worldDb, 20, 8); |
| 108 | harness.simulation.reloadWorld(); |
| 109 | harness.simulation.applySettings({ |
| 110 | pigeonTakeoffTilesPerSec: 40, |
| 111 | pigeonCruiseTilesPerSec: 40, |
| 112 | pigeonLandingTilesPerSec: 40, |
| 113 | simulationTickHz: 20 |
| 114 | }); |
| 115 | const sender = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8, firstName: "Ada", lastName: "Sender" }); |
| 116 | const receiver = makeCharacter(harness.stateDb, { tileX: 10, tileY: 8, firstName: "Bob", lastName: "Receiver" }); |
| 117 | const senderId = harness.simulation.spawnCharacter(1, sender); |
| 118 | harness.simulation.despawnCharacter(receiver.character_id); |
| 119 | const bag = harness.simulation.bagOf(senderId); |
| 120 | const appleSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); |
| 121 | assert.ok(appleSlot >= 0); |
| 122 | |
| 123 | const result = harness.simulation.dispatchPigeon({ |
| 124 | characterId: sender.character_id, |
| 125 | destination: { kind: "player", firstName: "Bob", lastName: "Receiver" }, |
| 126 | cargo: { kind: "item", slotIndex: appleSlot, quantity: 1 } |
| 127 | }); |
| 128 | assert.equal(result.transit.destinationKind, "bank"); |
| 129 | assert.equal(result.transit.vaultCharacterId, receiver.character_id); |
| 130 | assert.ok(Math.hypot(result.transit.destX - 20.5, result.transit.destY - 8.5) < 1); |
| 131 | |
| 132 | stepTimes(harness.simulation, 40); |
| 133 | const receiverVault = harness.stateDb.getBag(receiver.character_id); |
| 134 | assert.equal(countItem(receiverVault.vault, ItemId.apple), 1); |
| 135 | assert.equal(harness.stateDb.listActiveCourierTransits().length, 0); |
| 136 | } |
| 137 | finally |
| 138 | { harness.close(); } |
| 139 | }); |
| 140 | |
| 141 | test("logging in after offline bank deposits sends a bank resting notice by pigeon", () => |
| 142 | { |
| 143 | const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 }); |
| 144 | try |
| 145 | { |
| 146 | placeBank(harness.worldDb, 20, 8); |
| 147 | harness.simulation.reloadWorld(); |
| 148 | harness.simulation.applySettings({ |
| 149 | pigeonTakeoffTilesPerSec: 40, |
| 150 | pigeonCruiseTilesPerSec: 40, |
| 151 | pigeonLandingTilesPerSec: 40, |
| 152 | simulationTickHz: 20 |
| 153 | }); |
| 154 | const sender = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8, firstName: "Ada", lastName: "Sender" }); |
| 155 | const receiver = makeCharacter(harness.stateDb, { tileX: 10, tileY: 8, firstName: "Bob", lastName: "Receiver" }); |
| 156 | const senderId = harness.simulation.spawnCharacter(1, sender); |
| 157 | harness.simulation.despawnCharacter(receiver.character_id); |
| 158 | |
| 159 | const bag = harness.simulation.bagOf(senderId); |
| 160 | const appleSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); |
| 161 | harness.simulation.dispatchPigeon({ |
| 162 | characterId: sender.character_id, |
| 163 | destination: { kind: "player", firstName: "Bob", lastName: "Receiver" }, |
| 164 | cargo: { kind: "item", slotIndex: appleSlot, quantity: 1 } |
| 165 | }); |
| 166 | stepTimes(harness.simulation, 40); |
| 167 | assert.equal(harness.stateDb.listBankRestNotices(receiver.character_id).length, 1); |
| 168 | |
| 169 | const receiverId = harness.simulation.spawnCharacter(2, receiver); |
| 170 | const active = harness.stateDb.listActiveCourierTransits(); |
| 171 | assert.equal(active.length, 1); |
| 172 | assert.equal(active[0].destinationKind, "player"); |
| 173 | assert.equal(active[0].cargo.stack.itemId, ItemId.letter); |
| 174 | assert.match(active[0].cargo.stack.instance.body, /While you have been resting/); |
| 175 | assert.match(active[0].cargo.stack.instance.body, /Ada Sender/); |
| 176 | |
| 177 | stepTimes(harness.simulation, 40); |
| 178 | const receiverBag = harness.simulation.bagOf(receiverId); |
| 179 | const letterSlot = receiverBag.inventory.find((slot) => slot?.itemId === ItemId.letter); |
| 180 | assert.ok(letterSlot); |
| 181 | assert.equal(letterSlot.instance.authorDisplayName, "The Bank"); |
| 182 | assert.equal(letterSlot.origins?.[0]?.origin?.verb, "Written"); |
| 183 | assert.equal(letterSlot.origins?.[0]?.origin?.displayName, "The Bank"); |
| 184 | assert.doesNotMatch(letterSlot.instance.body, /Signed,/); |
| 185 | } |
| 186 | finally |
| 187 | { harness.close(); } |
| 188 | }); |
| 189 | test("pigeon delivers an item to an online player inventory", () => |
| 190 | { |
| 191 | const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 }); |
| 192 | try |
| 193 | { |
| 194 | harness.simulation.applySettings({ |
| 195 | pigeonTakeoffTilesPerSec: 40, |
| 196 | pigeonCruiseTilesPerSec: 40, |
| 197 | pigeonLandingTilesPerSec: 40, |
| 198 | simulationTickHz: 20 |
| 199 | }); |
| 200 | const sender = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8, firstName: "Ada", lastName: "Sender" }); |
| 201 | const receiver = makeCharacter(harness.stateDb, { tileX: 10, tileY: 8, firstName: "Bob", lastName: "Receiver" }); |
| 202 | const senderId = harness.simulation.spawnCharacter(1, sender); |
| 203 | const receiverId = harness.simulation.spawnCharacter(2, receiver); |
| 204 | const bag = harness.simulation.bagOf(senderId); |
| 205 | const appleSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); |
| 206 | assert.ok(appleSlot >= 0); |
| 207 | const senderQty = bag.inventory[appleSlot].quantity; |
| 208 | const receiverBefore = harness.simulation.bagOf(receiverId); |
| 209 | const receiverApples = receiverBefore.inventory |
| 210 | .filter((slot) => slot?.itemId === ItemId.apple) |
| 211 | .reduce((sum, slot) => sum + slot.quantity, 0); |
| 212 | |
| 213 | harness.simulation.dispatchPigeon({ |
| 214 | characterId: sender.character_id, |
| 215 | destination: { kind: "player", firstName: "Bob", lastName: "Receiver" }, |
| 216 | cargo: { kind: "item", slotIndex: appleSlot, quantity: 1 } |
| 217 | }); |
| 218 | assert.equal(harness.simulation.bagOf(senderId).inventory[appleSlot].quantity, senderQty - 1); |
| 219 | |
| 220 | stepTimes(harness.simulation, 40); |
| 221 | const receiverAfter = harness.simulation.bagOf(receiverId); |
| 222 | const receiverApplesAfter = receiverAfter.inventory |
| 223 | .filter((slot) => slot?.itemId === ItemId.apple) |
| 224 | .reduce((sum, slot) => sum + slot.quantity, 0); |
| 225 | assert.equal(receiverApplesAfter, receiverApples + 1); |
| 226 | assert.equal(harness.stateDb.listActiveCourierTransits().length, 0); |
| 227 | } |
| 228 | finally |
| 229 | { harness.close(); } |
| 230 | }); |
| 231 | |
| 232 | test("pigeon rejects blank cheques", () => |
| 233 | { |
| 234 | const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 }); |
| 235 | try |
| 236 | { |
| 237 | placeBank(harness.worldDb, 8, 8); |
| 238 | harness.simulation.reloadWorld(); |
| 239 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 240 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 241 | harness.simulation.enqueue(1, { |
| 242 | type: "get-blank-cheques", |
| 243 | characterId: character.character_id |
| 244 | }); |
| 245 | harness.simulation.step(); |
| 246 | const bag = harness.simulation.bagOf(entityId); |
| 247 | const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque); |
| 248 | assert.ok(blankIndex >= 0); |
| 249 | assert.throws( |
| 250 | () => harness.simulation.dispatchPigeon({ |
| 251 | characterId: character.character_id, |
| 252 | destination: { kind: "bank" }, |
| 253 | cargo: { kind: "item", slotIndex: blankIndex, quantity: 1 } |
| 254 | }), |
| 255 | (err) => err.message === "Blank cheques cannot be sent by pigeon." |
| 256 | ); |
| 257 | } |
| 258 | finally |
| 259 | { harness.close(); } |
| 260 | }); |
| 261 | |
| 262 | test("pigeon to bank redeems a filled cheque into reserved goods", () => |
| 263 | { |
| 264 | const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 }); |
| 265 | try |
| 266 | { |
| 267 | placeBank(harness.worldDb, 20, 8); |
| 268 | placeBank(harness.worldDb, 8, 8); |
| 269 | harness.simulation.reloadWorld(); |
| 270 | harness.simulation.applySettings({ |
| 271 | pigeonTakeoffTilesPerSec: 40, |
| 272 | pigeonCruiseTilesPerSec: 40, |
| 273 | pigeonLandingTilesPerSec: 40, |
| 274 | simulationTickHz: 20 |
| 275 | }); |
| 276 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 277 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 278 | harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id }); |
| 279 | harness.simulation.step(); |
| 280 | let bag = harness.simulation.bagOf(entityId); |
| 281 | harness.simulation.enqueue(1, { |
| 282 | type: "bank-deposit", |
| 283 | characterId: character.character_id, |
| 284 | slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple), |
| 285 | quantity: null |
| 286 | }); |
| 287 | harness.simulation.step(); |
| 288 | bag = harness.simulation.bagOf(entityId); |
| 289 | const applesBefore = countItem(bag.vault, ItemId.apple); |
| 290 | harness.simulation.enqueue(1, { |
| 291 | type: "write-cheque", |
| 292 | characterId: character.character_id, |
| 293 | slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque), |
| 294 | vaultSlotIndex: bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple), |
| 295 | quantity: 2 |
| 296 | }); |
| 297 | harness.simulation.step(); |
| 298 | bag = harness.simulation.bagOf(entityId); |
| 299 | const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque); |
| 300 | assert.ok(chequeIndex >= 0); |
| 301 | assert.equal((bag.vaultReserved ?? []).length, 1); |
| 302 | |
| 303 | harness.simulation.dispatchPigeon({ |
| 304 | characterId: character.character_id, |
| 305 | destination: { kind: "bank" }, |
| 306 | cargo: { kind: "item", slotIndex: chequeIndex, quantity: 1 } |
| 307 | }); |
| 308 | stepTimes(harness.simulation, 80); |
| 309 | bag = harness.simulation.bagOf(entityId); |
| 310 | assert.equal(bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque), -1); |
| 311 | assert.equal(countItem(bag.vault, ItemId.filledCheque), 0); |
| 312 | assert.equal((bag.vaultReserved ?? []).length, 0); |
| 313 | assert.equal(countItem(bag.vault, ItemId.apple), applesBefore); |
| 314 | } |
| 315 | finally |
| 316 | { harness.close(); } |
| 317 | }); |