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