| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { emptyEquipment, emptySlots, insertStack, takeFromSlot } from "../../shared/items/inventory.js"; |
| 4 | import { ItemOriginKind } from "../../shared/items/origin.js"; |
| 5 | import { ItemId, itemDefinition } from "../../shared/definitions/items.js"; |
| 6 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 7 | import { Component } from "../src/ecs/components.js"; |
| 8 | import { encodeChunkBlob, createEmptyChunkData } from "../src/world/chunkData.js"; |
| 9 | import { |
| 10 | makeCharacter, |
| 11 | makeSimulation, |
| 12 | stepTimes |
| 13 | } from "./helpers/simulationHarness.js"; |
| 14 | |
| 15 | function workerOf (simulation, entityId) |
| 16 | { |
| 17 | const record = simulation.entityRecord(entityId); |
| 18 | assert.ok(record, "entity must be remembered"); |
| 19 | return simulation.workers[record.workerId]; |
| 20 | } |
| 21 | |
| 22 | test("stackable items merge until max stack then need a new slot", () => |
| 23 | { |
| 24 | const slots = emptySlots(); |
| 25 | const def = itemDefinition(ItemId.apple); |
| 26 | assert.equal(insertStack(slots, { itemId: ItemId.apple, quantity: 40 }, def).ok, true); |
| 27 | const first = insertStack(slots, { itemId: ItemId.apple, quantity: 40 }, def); |
| 28 | assert.equal(first.ok, true); |
| 29 | const second = insertStack(first.slots, { itemId: ItemId.apple, quantity: 40 }, def); |
| 30 | assert.equal(second.ok, true); |
| 31 | const taken = takeFromSlot(second.slots, 0, 10); |
| 32 | assert.equal(taken.ok, true); |
| 33 | assert.equal(taken.taken.quantity, 10); |
| 34 | assert.equal(taken.slots[0].quantity, 70); |
| 35 | }); |
| 36 | |
| 37 | test("a player can drop and pick up an item on their tile", () => |
| 38 | { |
| 39 | const harness = makeSimulation({ workerCount: 1 }); |
| 40 | try |
| 41 | { |
| 42 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 43 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 44 | const bag = harness.simulation.bagOf(entityId); |
| 45 | const appleSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); |
| 46 | assert.ok(appleSlot >= 0); |
| 47 | harness.simulation.enqueue(1, { |
| 48 | type: "drop-item", |
| 49 | characterId: character.character_id, |
| 50 | slotIndex: appleSlot, |
| 51 | quantity: 1 |
| 52 | }); |
| 53 | const events = harness.simulation.step().events; |
| 54 | const appear = events.find((event) => event.type === "ground-item-appear"); |
| 55 | assert.ok(appear); |
| 56 | assert.equal(appear.tileX, 8); |
| 57 | assert.equal(appear.itemId, ItemId.apple); |
| 58 | const afterDrop = harness.simulation.bagOf(entityId); |
| 59 | assert.equal(afterDrop.inventory[appleSlot].quantity, 2); |
| 60 | harness.simulation.enqueue(1, { |
| 61 | type: "pickup-item", |
| 62 | characterId: character.character_id, |
| 63 | groundItemId: appear.groundItemId |
| 64 | }); |
| 65 | harness.simulation.step(); |
| 66 | const afterPick = harness.simulation.bagOf(entityId); |
| 67 | assert.equal(afterPick.inventory[appleSlot].quantity, 3); |
| 68 | const snapshot = harness.simulation.snapshotChunk(0, 0); |
| 69 | assert.equal((snapshot.groundItems ?? []).length, 0); |
| 70 | } |
| 71 | finally |
| 72 | { harness.close(); } |
| 73 | }); |
| 74 | |
| 75 | test("walkTilesPerSecond controls how many tiles move per simulated second", () => |
| 76 | { |
| 77 | const harness = makeSimulation({ workerCount: 1 }); |
| 78 | try |
| 79 | { |
| 80 | harness.simulation.applySettings({ walkTilesPerSecond: 2, simulationTickHz: 20 }); |
| 81 | const slow = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8 }); |
| 82 | const slowId = harness.simulation.spawnCharacter(1, slow); |
| 83 | harness.simulation.enqueue(1, { type: "move-to", characterId: slow.character_id, tileX: 20, tileY: 8 }); |
| 84 | stepTimes(harness.simulation, 20); |
| 85 | assert.equal(harness.simulation.entityRecord(slowId).tileX, 4); |
| 86 | |
| 87 | harness.simulation.applySettings({ walkTilesPerSecond: 4, simulationTickHz: 20 }); |
| 88 | harness.simulation.enqueue(1, { type: "move-to", characterId: slow.character_id, tileX: 20, tileY: 8 }); |
| 89 | stepTimes(harness.simulation, 20); |
| 90 | assert.equal(harness.simulation.entityRecord(slowId).tileX, 8); |
| 91 | } |
| 92 | finally |
| 93 | { harness.close(); } |
| 94 | }); |
| 95 | |
| 96 | test("equip uses the item key-equip sprite for appearance layers", () => |
| 97 | { |
| 98 | const harness = makeSimulation({ workerCount: 1 }); |
| 99 | try |
| 100 | { |
| 101 | const character = makeCharacter(harness.stateDb); |
| 102 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 103 | const bag = harness.simulation.bagOf(entityId); |
| 104 | const shirtSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.clothShirt); |
| 105 | harness.simulation.enqueue(1, { |
| 106 | type: "equip-item", |
| 107 | characterId: character.character_id, |
| 108 | slotIndex: shirtSlot |
| 109 | }); |
| 110 | harness.simulation.step(); |
| 111 | const view = harness.simulation.entityRecord(entityId); |
| 112 | assert.ok(view.layers.some((layer) => layer.sprite === "item:cloth-shirt:cloth-shirt:equip")); |
| 113 | } |
| 114 | finally |
| 115 | { harness.close(); } |
| 116 | }); |
| 117 | |
| 118 | test("equipping from a stack takes one item and leaves the remainder", () => |
| 119 | { |
| 120 | const harness = makeSimulation({ workerCount: 1 }); |
| 121 | try |
| 122 | { |
| 123 | const character = makeCharacter(harness.stateDb); |
| 124 | harness.stateDb.saveBag(character.character_id, { |
| 125 | inventory: (() => |
| 126 | { |
| 127 | const slots = emptySlots(); |
| 128 | slots[0] = { |
| 129 | itemId: ItemId.stick, |
| 130 | quantity: 3, |
| 131 | origins: [{ origin: { kind: ItemOriginKind.crafted, displayName: "Ada" }, quantity: 3 }] |
| 132 | }; |
| 133 | return slots; |
| 134 | })(), |
| 135 | equipment: emptyEquipment(), |
| 136 | vault: [], |
| 137 | vaultReserved: [] |
| 138 | }); |
| 139 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 140 | harness.simulation.enqueue(1, { |
| 141 | type: "equip-item", |
| 142 | characterId: character.character_id, |
| 143 | slotIndex: 0 |
| 144 | }); |
| 145 | harness.simulation.step(); |
| 146 | const bag = harness.simulation.bagOf(entityId); |
| 147 | assert.equal(bag.inventory[0].quantity, 2); |
| 148 | assert.equal(bag.equipment.slots.weapon.quantity, 1); |
| 149 | assert.deepEqual(bag.equipment.slots.weapon.origins, [{ |
| 150 | origin: { kind: ItemOriginKind.crafted, displayName: "Ada" }, |
| 151 | quantity: 1 |
| 152 | }]); |
| 153 | } |
| 154 | finally |
| 155 | { harness.close(); } |
| 156 | }); |
| 157 | |
| 158 | test("unequipping returns one item to inventory", () => |
| 159 | { |
| 160 | const harness = makeSimulation({ workerCount: 1 }); |
| 161 | try |
| 162 | { |
| 163 | const character = makeCharacter(harness.stateDb); |
| 164 | const origin = { kind: ItemOriginKind.crafted, displayName: "Ada" }; |
| 165 | harness.stateDb.saveBag(character.character_id, { |
| 166 | inventory: emptySlots(), |
| 167 | equipment: { |
| 168 | slots: { |
| 169 | weapon: { |
| 170 | itemId: ItemId.stick, |
| 171 | quantity: 1, |
| 172 | origins: [{ origin, quantity: 1 }] |
| 173 | } |
| 174 | } |
| 175 | }, |
| 176 | vault: [], |
| 177 | vaultReserved: [] |
| 178 | }); |
| 179 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 180 | harness.simulation.enqueue(1, { |
| 181 | type: "unequip-item", |
| 182 | characterId: character.character_id, |
| 183 | equipSlot: "weapon" |
| 184 | }); |
| 185 | harness.simulation.step(); |
| 186 | const bag = harness.simulation.bagOf(entityId); |
| 187 | assert.equal(bag.equipment.slots.weapon, null); |
| 188 | assert.equal(bag.inventory[0].quantity, 1); |
| 189 | assert.deepEqual(bag.inventory[0].origins, [{ origin, quantity: 1 }]); |
| 190 | } |
| 191 | finally |
| 192 | { harness.close(); } |
| 193 | }); |
| 194 | |
| 195 | test("unequipping uses a second slot when the item cannot stack in inventory", () => |
| 196 | { |
| 197 | const harness = makeSimulation({ workerCount: 1 }); |
| 198 | try |
| 199 | { |
| 200 | const character = makeCharacter(harness.stateDb); |
| 201 | const origin = { kind: ItemOriginKind.crafted, displayName: "Ada" }; |
| 202 | harness.stateDb.saveBag(character.character_id, { |
| 203 | inventory: (() => |
| 204 | { |
| 205 | const slots = emptySlots(); |
| 206 | slots[0] = { |
| 207 | itemId: ItemId.stick, |
| 208 | quantity: 1, |
| 209 | origins: [{ origin, quantity: 1 }] |
| 210 | }; |
| 211 | return slots; |
| 212 | })(), |
| 213 | equipment: { |
| 214 | slots: { |
| 215 | weapon: { |
| 216 | itemId: ItemId.stick, |
| 217 | quantity: 1, |
| 218 | origins: [{ origin, quantity: 1 }] |
| 219 | } |
| 220 | } |
| 221 | }, |
| 222 | vault: [], |
| 223 | vaultReserved: [] |
| 224 | }); |
| 225 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 226 | harness.simulation.enqueue(1, { |
| 227 | type: "unequip-item", |
| 228 | characterId: character.character_id, |
| 229 | equipSlot: "weapon" |
| 230 | }); |
| 231 | harness.simulation.step(); |
| 232 | const bag = harness.simulation.bagOf(entityId); |
| 233 | assert.equal(bag.inventory[0].quantity, 1); |
| 234 | assert.equal(bag.inventory[1].quantity, 1); |
| 235 | assert.equal(bag.inventory.filter(Boolean).length, 2); |
| 236 | } |
| 237 | finally |
| 238 | { harness.close(); } |
| 239 | }); |
| 240 | |
| 241 | test("a spawner in a simulating open chunk creates deer up to maxAlive", () => |
| 242 | { |
| 243 | const harness = makeSimulation({ workerCount: 1 }); |
| 244 | try |
| 245 | { |
| 246 | const data = createEmptyChunkData(); |
| 247 | data.open = true; |
| 248 | data.blocks[8 * 16 + 8] = { |
| 249 | typeId: BlockType.spawner, |
| 250 | state: null, |
| 251 | config: { spawnerId: "0:0:136", mobTypeId: "deer", maxAlive: 2, spawnRangeTiles: 3 } |
| 252 | }; |
| 253 | harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); |
| 254 | harness.simulation.reloadWorld(); |
| 255 | const character = makeCharacter(harness.stateDb, { tileX: 4, tileY: 4 }); |
| 256 | harness.simulation.spawnCharacter(1, character); |
| 257 | for (let i = 0; i < 80; i++) |
| 258 | { harness.simulation.step(); } |
| 259 | const deer = [...harness.simulation.directory.values()].filter((entity) => entity.firstName === "Deer"); |
| 260 | assert.ok(deer.length >= 1); |
| 261 | assert.ok(deer.length <= 2); |
| 262 | const pigeons = [...harness.simulation.directory.values()].filter((entity) => entity.firstName === "Pigeon"); |
| 263 | assert.equal(pigeons.length, 0); |
| 264 | } |
| 265 | finally |
| 266 | { harness.close(); } |
| 267 | }); |
| 268 | |
| 269 | test("eating food restores player health", () => |
| 270 | { |
| 271 | const harness = makeSimulation({ workerCount: 1 }); |
| 272 | try |
| 273 | { |
| 274 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 275 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 276 | const worker = workerOf(harness.simulation, playerId); |
| 277 | const handle = worker.world.ecs.handleOf(playerId); |
| 278 | const health = worker.world.ecs.get(handle, Component.health); |
| 279 | health.current = 6; |
| 280 | const bag = harness.simulation.bagOf(playerId); |
| 281 | const appleSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); |
| 282 | assert.ok(appleSlot >= 0); |
| 283 | harness.simulation.enqueue(1, { |
| 284 | type: "eat-item", |
| 285 | characterId: character.character_id, |
| 286 | slotIndex: appleSlot |
| 287 | }); |
| 288 | harness.simulation.step(); |
| 289 | const after = worker.world.ecs.get(handle, Component.health); |
| 290 | assert.equal(after.current, 10); |
| 291 | assert.equal(after.max, 10); |
| 292 | const afterBag = harness.simulation.bagOf(playerId); |
| 293 | assert.equal(afterBag.inventory[appleSlot].quantity, 2); |
| 294 | } |
| 295 | finally |
| 296 | { harness.close(); } |
| 297 | }); |