| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { ItemId, itemDefinition } from "../../shared/definitions/items.js"; |
| 4 | import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js"; |
| 5 | import |
| 6 | { |
| 7 | addGroundItem, |
| 8 | placeOrMergeGroundStack |
| 9 | } from "../src/items/bags.js"; |
| 10 | import |
| 11 | { |
| 12 | makeCharacter, |
| 13 | makeSimulation |
| 14 | } from "./helpers/simulationHarness.js"; |
| 15 | |
| 16 | function emptyWorld (settings = {}) |
| 17 | { |
| 18 | return { |
| 19 | groundItems: new Map(), |
| 20 | groundByTile: new Map(), |
| 21 | nextGroundItemId: 1, |
| 22 | settings: { |
| 23 | groundItemDespawnDurationMs: 600_000, |
| 24 | ...settings |
| 25 | } |
| 26 | }; |
| 27 | } |
| 28 | |
| 29 | function giveInventory (harness, entityId, items) |
| 30 | { |
| 31 | const inventory = emptySlots(); |
| 32 | items.forEach((stack, index) => |
| 33 | { |
| 34 | inventory[index] = { |
| 35 | itemId: stack.itemId, |
| 36 | quantity: stack.quantity, |
| 37 | ...(stack.instance ? { instance: structuredClone(stack.instance) } : {}) |
| 38 | }; |
| 39 | }); |
| 40 | harness.simulation.applyBag(entityId, { |
| 41 | inventory, |
| 42 | equipment: emptyEquipment(), |
| 43 | vault: [], |
| 44 | vaultReserved: [] |
| 45 | }); |
| 46 | } |
| 47 | |
| 48 | test("placeOrMergeGroundStack merges matching piles and resets despawn", () => |
| 49 | { |
| 50 | const world = emptyWorld(); |
| 51 | const now = 10_000; |
| 52 | addGroundItem(world, { |
| 53 | groundItemId: 1, |
| 54 | tileX: 4, |
| 55 | tileY: 5, |
| 56 | stack: { itemId: ItemId.coins, quantity: 25 }, |
| 57 | droppedAt: 1_000, |
| 58 | expiresAt: 5_000, |
| 59 | source: "mob-drop" |
| 60 | }); |
| 61 | |
| 62 | const placed = placeOrMergeGroundStack(world, { |
| 63 | tileX: 4, |
| 64 | tileY: 5, |
| 65 | stack: { itemId: ItemId.coins, quantity: 10 }, |
| 66 | now, |
| 67 | source: "player-drop", |
| 68 | droppedByCharacterId: 7, |
| 69 | groundItemId: 99 |
| 70 | }); |
| 71 | |
| 72 | assert.equal(world.groundItems.size, 1); |
| 73 | const pile = world.groundItems.get(1); |
| 74 | assert.equal(pile.stack.quantity, 35); |
| 75 | assert.equal(pile.expiresAt, now + 600_000); |
| 76 | assert.equal(pile.droppedAt, now); |
| 77 | assert.equal(pile.source, "mob-drop"); |
| 78 | assert.equal(placed.events.length, 1); |
| 79 | assert.equal(placed.events[0].groundItemId, 1); |
| 80 | assert.equal(placed.events[0].quantity, 35); |
| 81 | assert.equal(placed.events[0].expiresAt, now + 600_000); |
| 82 | assert.equal(world.groundItems.has(99), false); |
| 83 | }); |
| 84 | |
| 85 | test("placeOrMergeGroundStack overflows into a new pile at max stack", () => |
| 86 | { |
| 87 | const world = emptyWorld(); |
| 88 | const max = itemDefinition(ItemId.apple).maxStackSize ?? 99; |
| 89 | addGroundItem(world, { |
| 90 | groundItemId: 1, |
| 91 | tileX: 1, |
| 92 | tileY: 1, |
| 93 | stack: { itemId: ItemId.apple, quantity: max }, |
| 94 | droppedAt: 1_000, |
| 95 | expiresAt: 2_000, |
| 96 | source: "player-drop" |
| 97 | }); |
| 98 | |
| 99 | const placed = placeOrMergeGroundStack(world, { |
| 100 | tileX: 1, |
| 101 | tileY: 1, |
| 102 | stack: { itemId: ItemId.apple, quantity: 5 }, |
| 103 | now: 50_000, |
| 104 | source: "player-drop", |
| 105 | groundItemId: 2 |
| 106 | }); |
| 107 | |
| 108 | assert.equal(world.groundItems.size, 2); |
| 109 | assert.equal(world.groundItems.get(1).stack.quantity, max); |
| 110 | assert.equal(world.groundItems.get(1).expiresAt, 2_000); |
| 111 | assert.equal(world.groundItems.get(2).stack.quantity, 5); |
| 112 | assert.equal(world.groundItems.get(2).expiresAt, 50_000 + 600_000); |
| 113 | assert.equal(placed.events.length, 1); |
| 114 | assert.equal(placed.events[0].groundItemId, 2); |
| 115 | }); |
| 116 | |
| 117 | test("placeOrMergeGroundStack does not merge non-stackable equipment", () => |
| 118 | { |
| 119 | const world = emptyWorld(); |
| 120 | const equipmentId = Object.values(ItemId).find((id) => |
| 121 | { |
| 122 | const def = itemDefinition(id); |
| 123 | return def?.equipment && def.equipment.kind !== "arrowAmmo"; |
| 124 | }); |
| 125 | assert.ok(equipmentId); |
| 126 | addGroundItem(world, { |
| 127 | groundItemId: 1, |
| 128 | tileX: 2, |
| 129 | tileY: 2, |
| 130 | stack: { itemId: equipmentId, quantity: 1 }, |
| 131 | droppedAt: 1, |
| 132 | expiresAt: 100, |
| 133 | source: "mob-drop" |
| 134 | }); |
| 135 | placeOrMergeGroundStack(world, { |
| 136 | tileX: 2, |
| 137 | tileY: 2, |
| 138 | stack: { itemId: equipmentId, quantity: 1 }, |
| 139 | now: 200, |
| 140 | source: "mob-drop", |
| 141 | groundItemId: 2 |
| 142 | }); |
| 143 | assert.equal(world.groundItems.size, 2); |
| 144 | }); |
| 145 | |
| 146 | test("dropping coins onto an existing ground coin pile merges and refreshes despawn", () => |
| 147 | { |
| 148 | const harness = makeSimulation({ workerCount: 1 }); |
| 149 | try |
| 150 | { |
| 151 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 152 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 153 | giveInventory(harness, entityId, [ |
| 154 | { itemId: ItemId.coins, quantity: 40 } |
| 155 | ]); |
| 156 | const bag = harness.simulation.bagOf(entityId); |
| 157 | const coinSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.coins); |
| 158 | assert.ok(coinSlot >= 0); |
| 159 | |
| 160 | harness.simulation.enqueue(1, { |
| 161 | type: "drop-item", |
| 162 | characterId: character.character_id, |
| 163 | slotIndex: coinSlot, |
| 164 | quantity: 15 |
| 165 | }); |
| 166 | const first = harness.simulation.step().events.find((event) => event.type === "ground-item-appear"); |
| 167 | assert.ok(first); |
| 168 | assert.equal(first.quantity, 15); |
| 169 | const firstExpires = first.expiresAt; |
| 170 | const groundId = first.groundItemId; |
| 171 | |
| 172 | harness.advance(5_000); |
| 173 | harness.simulation.enqueue(1, { |
| 174 | type: "drop-item", |
| 175 | characterId: character.character_id, |
| 176 | slotIndex: coinSlot, |
| 177 | quantity: 10 |
| 178 | }); |
| 179 | const secondEvents = harness.simulation.step().events.filter((event) => event.type === "ground-item-appear"); |
| 180 | assert.equal(secondEvents.length, 1); |
| 181 | assert.equal(secondEvents[0].groundItemId, groundId); |
| 182 | assert.equal(secondEvents[0].quantity, 25); |
| 183 | assert.ok(secondEvents[0].expiresAt > firstExpires); |
| 184 | |
| 185 | const snapshot = harness.simulation.snapshotChunk(0, 0); |
| 186 | const piles = (snapshot.groundItems ?? []).filter((item) => item.itemId === ItemId.coins); |
| 187 | assert.equal(piles.length, 1); |
| 188 | assert.equal(piles[0].quantity, 25); |
| 189 | } |
| 190 | finally |
| 191 | { harness.close(); } |
| 192 | }); |