| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { emptySlots, insertStack, takeFromSlot } from "../../shared/items/inventory.js"; |
| 4 | import { ItemId, itemDefinition } from "../../shared/definitions/items.js"; |
| 5 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 6 | import { encodeChunkBlob, createEmptyChunkData } from "../src/world/chunkData.js"; |
| 7 | import { |
| 8 | makeCharacter, |
| 9 | makeSimulation, |
| 10 | stepTimes |
| 11 | } from "./helpers/simulationHarness.js"; |
| 12 | |
| 13 | test("stackable items merge until max stack then need a new slot", () => |
| 14 | { |
| 15 | const slots = emptySlots(); |
| 16 | const def = itemDefinition(ItemId.apple); |
| 17 | assert.equal(insertStack(slots, { itemId: ItemId.apple, quantity: 40 }, def).ok, true); |
| 18 | const first = insertStack(slots, { itemId: ItemId.apple, quantity: 40 }, def); |
| 19 | assert.equal(first.ok, true); |
| 20 | const second = insertStack(first.slots, { itemId: ItemId.apple, quantity: 40 }, def); |
| 21 | assert.equal(second.ok, true); |
| 22 | const taken = takeFromSlot(second.slots, 0, 10); |
| 23 | assert.equal(taken.ok, true); |
| 24 | assert.equal(taken.taken.quantity, 10); |
| 25 | assert.equal(taken.slots[0].quantity, 70); |
| 26 | }); |
| 27 | |
| 28 | test("a player can drop and pick up an item on their tile", () => |
| 29 | { |
| 30 | const harness = makeSimulation({ workerCount: 1 }); |
| 31 | try |
| 32 | { |
| 33 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 34 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 35 | const bag = harness.simulation.bagOf(entityId); |
| 36 | const appleSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); |
| 37 | assert.ok(appleSlot >= 0); |
| 38 | harness.simulation.enqueue(1, { |
| 39 | type: "drop-item", |
| 40 | characterId: character.character_id, |
| 41 | slotIndex: appleSlot, |
| 42 | quantity: 1 |
| 43 | }); |
| 44 | const events = harness.simulation.step().events; |
| 45 | const appear = events.find((event) => event.type === "ground-item-appear"); |
| 46 | assert.ok(appear); |
| 47 | assert.equal(appear.tileX, 8); |
| 48 | assert.equal(appear.itemId, ItemId.apple); |
| 49 | const afterDrop = harness.simulation.bagOf(entityId); |
| 50 | assert.equal(afterDrop.inventory[appleSlot].quantity, 2); |
| 51 | harness.simulation.enqueue(1, { |
| 52 | type: "pickup-item", |
| 53 | characterId: character.character_id, |
| 54 | groundItemId: appear.groundItemId |
| 55 | }); |
| 56 | harness.simulation.step(); |
| 57 | const afterPick = harness.simulation.bagOf(entityId); |
| 58 | assert.equal(afterPick.inventory[appleSlot].quantity, 3); |
| 59 | const snapshot = harness.simulation.snapshotChunk(0, 0); |
| 60 | assert.equal((snapshot.groundItems ?? []).length, 0); |
| 61 | } |
| 62 | finally |
| 63 | { harness.close(); } |
| 64 | }); |
| 65 | |
| 66 | test("walkTilesPerSecond controls how many tiles move per simulated second", () => |
| 67 | { |
| 68 | const harness = makeSimulation({ workerCount: 1 }); |
| 69 | try |
| 70 | { |
| 71 | harness.simulation.applySettings({ walkTilesPerSecond: 2, simulationTickHz: 20 }); |
| 72 | const slow = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8 }); |
| 73 | const slowId = harness.simulation.spawnCharacter(1, slow); |
| 74 | harness.simulation.enqueue(1, { type: "move-to", characterId: slow.character_id, tileX: 20, tileY: 8 }); |
| 75 | stepTimes(harness.simulation, 20); |
| 76 | assert.equal(harness.simulation.entityRecord(slowId).tileX, 4); |
| 77 | |
| 78 | harness.simulation.applySettings({ walkTilesPerSecond: 4, simulationTickHz: 20 }); |
| 79 | harness.simulation.enqueue(1, { type: "move-to", characterId: slow.character_id, tileX: 20, tileY: 8 }); |
| 80 | stepTimes(harness.simulation, 20); |
| 81 | assert.equal(harness.simulation.entityRecord(slowId).tileX, 8); |
| 82 | } |
| 83 | finally |
| 84 | { harness.close(); } |
| 85 | }); |
| 86 | |
| 87 | test("equip uses the item key-equip sprite for appearance layers", () => |
| 88 | { |
| 89 | const harness = makeSimulation({ workerCount: 1 }); |
| 90 | try |
| 91 | { |
| 92 | const character = makeCharacter(harness.stateDb); |
| 93 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 94 | const bag = harness.simulation.bagOf(entityId); |
| 95 | const shirtSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.clothShirt); |
| 96 | harness.simulation.enqueue(1, { |
| 97 | type: "equip-item", |
| 98 | characterId: character.character_id, |
| 99 | slotIndex: shirtSlot |
| 100 | }); |
| 101 | harness.simulation.step(); |
| 102 | const view = harness.simulation.entityRecord(entityId); |
| 103 | assert.ok(view.layers.some((layer) => layer.sprite === "item:cloth-shirt:cloth-shirt:equip")); |
| 104 | } |
| 105 | finally |
| 106 | { harness.close(); } |
| 107 | }); |
| 108 | |
| 109 | test("a spawner in a simulating open chunk creates deer up to maxAlive", () => |
| 110 | { |
| 111 | const harness = makeSimulation({ workerCount: 1 }); |
| 112 | try |
| 113 | { |
| 114 | const data = createEmptyChunkData(); |
| 115 | data.open = true; |
| 116 | data.blocks[8 * 16 + 8] = { |
| 117 | typeId: BlockType.spawner, |
| 118 | state: null, |
| 119 | config: { spawnerId: "0:0:136", mobTypeId: "deer", maxAlive: 2, spawnRangeTiles: 3 } |
| 120 | }; |
| 121 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 122 | harness.simulation.reloadWorld(); |
| 123 | const character = makeCharacter(harness.stateDb, { tileX: 4, tileY: 4 }); |
| 124 | harness.simulation.spawnCharacter(1, character); |
| 125 | for (let i = 0; i < 80; i++) |
| 126 | { harness.simulation.step(); } |
| 127 | const deer = [...harness.simulation.directory.values()].filter((entity) => entity.firstName === "Deer"); |
| 128 | assert.ok(deer.length >= 1); |
| 129 | assert.ok(deer.length <= 2); |
| 130 | const pigeons = [...harness.simulation.directory.values()].filter((entity) => entity.firstName === "Pigeon"); |
| 131 | assert.equal(pigeons.length, 0); |
| 132 | } |
| 133 | finally |
| 134 | { harness.close(); } |
| 135 | }); |