import test from "node:test"; import assert from "node:assert/strict"; import { emptyEquipment, emptySlots, insertStack, takeFromSlot } from "../../shared/items/inventory.js"; import { ItemOriginKind } from "../../shared/items/origin.js"; import { ItemId, itemDefinition } from "../../shared/definitions/items.js"; import { Component } from "../src/ecs/components.js"; import { encodeChunkBlob, createEmptyChunkData } from "../src/world/chunkData.js"; import { makeCharacter, makeSimulation, stepTimes } from "./helpers/simulationHarness.js"; function workerOf (simulation, entityId) { const record = simulation.entityRecord(entityId); assert.ok(record, "entity must be remembered"); return simulation.workers[record.workerId]; } test("stackable items merge until max stack then need a new slot", () => { const slots = emptySlots(); const def = itemDefinition(ItemId.apple); assert.equal(insertStack(slots, { itemId: ItemId.apple, quantity: 40 }, def).ok, true); const first = insertStack(slots, { itemId: ItemId.apple, quantity: 40 }, def); assert.equal(first.ok, true); const second = insertStack(first.slots, { itemId: ItemId.apple, quantity: 40 }, def); assert.equal(second.ok, true); const taken = takeFromSlot(second.slots, 0, 10); assert.equal(taken.ok, true); assert.equal(taken.taken.quantity, 10); assert.equal(taken.slots[0].quantity, 70); }); test("a player can drop and pick up an item on their tile", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const entityId = harness.simulation.spawnCharacter(1, character); const bag = harness.simulation.bagOf(entityId); const appleSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); assert.ok(appleSlot >= 0); harness.simulation.enqueue(1, { type: "drop-item", characterId: character.character_id, slotIndex: appleSlot, quantity: 1 }); const events = harness.simulation.step().events; const appear = events.find((event) => event.type === "ground-item-appear"); assert.ok(appear); assert.equal(appear.tileX, 8); assert.equal(appear.itemId, ItemId.apple); const afterDrop = harness.simulation.bagOf(entityId); assert.equal(afterDrop.inventory[appleSlot].quantity, 2); harness.simulation.enqueue(1, { type: "pickup-item", characterId: character.character_id, groundItemId: appear.groundItemId }); harness.simulation.step(); const afterPick = harness.simulation.bagOf(entityId); assert.equal(afterPick.inventory[appleSlot].quantity, 3); const snapshot = harness.simulation.snapshotChunk(0, 0); assert.equal((snapshot.groundItems ?? []).length, 0); } finally { harness.close(); } }); test("walkTilesPerSecond controls how many tiles move per simulated second", () => { const harness = makeSimulation({ workerCount: 1 }); try { harness.simulation.applySettings({ walkTilesPerSecond: 2, simulationTickHz: 20 }); const slow = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8 }); const slowId = harness.simulation.spawnCharacter(1, slow); harness.simulation.enqueue(1, { type: "move-to", characterId: slow.character_id, tileX: 20, tileY: 8 }); stepTimes(harness.simulation, 20); assert.equal(harness.simulation.entityRecord(slowId).tileX, 4); harness.simulation.applySettings({ walkTilesPerSecond: 4, simulationTickHz: 20 }); harness.simulation.enqueue(1, { type: "move-to", characterId: slow.character_id, tileX: 20, tileY: 8 }); stepTimes(harness.simulation, 20); assert.equal(harness.simulation.entityRecord(slowId).tileX, 8); } finally { harness.close(); } }); test("equip uses the item key-equip sprite for appearance layers", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb); const entityId = harness.simulation.spawnCharacter(1, character); const bag = harness.simulation.bagOf(entityId); const shirtSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.clothShirt); harness.simulation.enqueue(1, { type: "equip-item", characterId: character.character_id, slotIndex: shirtSlot }); harness.simulation.step(); const view = harness.simulation.entityRecord(entityId); assert.ok(view.layers.some((layer) => layer.sprite === "item:clothing:shirt:equip")); } finally { harness.close(); } }); test("equipping from a stack takes one item and leaves the remainder", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb); harness.stateDb.saveBag(character.character_id, { inventory: (() => { const slots = emptySlots(); slots[0] = { itemId: ItemId.stick, quantity: 3, origins: [{ origin: { kind: ItemOriginKind.crafted, displayName: "Ada" }, quantity: 3 }] }; return slots; })(), equipment: emptyEquipment(), vault: [], vaultReserved: [] }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.enqueue(1, { type: "equip-item", characterId: character.character_id, slotIndex: 0 }); harness.simulation.step(); const bag = harness.simulation.bagOf(entityId); assert.equal(bag.inventory[0].quantity, 2); assert.equal(bag.equipment.slots.weapon.quantity, 1); assert.deepEqual(bag.equipment.slots.weapon.origins, [{ origin: { kind: ItemOriginKind.crafted, displayName: "Ada" }, quantity: 1 }]); } finally { harness.close(); } }); test("unequipping returns one item to inventory", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb); const origin = { kind: ItemOriginKind.crafted, displayName: "Ada" }; harness.stateDb.saveBag(character.character_id, { inventory: emptySlots(), equipment: { slots: { weapon: { itemId: ItemId.stick, quantity: 1, origins: [{ origin, quantity: 1 }] } } }, vault: [], vaultReserved: [] }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.enqueue(1, { type: "unequip-item", characterId: character.character_id, equipSlot: "weapon" }); harness.simulation.step(); const bag = harness.simulation.bagOf(entityId); assert.equal(bag.equipment.slots.weapon, null); assert.equal(bag.inventory[0].quantity, 1); assert.deepEqual(bag.inventory[0].origins, [{ origin, quantity: 1 }]); } finally { harness.close(); } }); test("unequipping uses a second slot when the item cannot stack in inventory", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb); const origin = { kind: ItemOriginKind.crafted, displayName: "Ada" }; harness.stateDb.saveBag(character.character_id, { inventory: (() => { const slots = emptySlots(); slots[0] = { itemId: ItemId.stick, quantity: 1, origins: [{ origin, quantity: 1 }] }; return slots; })(), equipment: { slots: { weapon: { itemId: ItemId.stick, quantity: 1, origins: [{ origin, quantity: 1 }] } } }, vault: [], vaultReserved: [] }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.enqueue(1, { type: "unequip-item", characterId: character.character_id, equipSlot: "weapon" }); harness.simulation.step(); const bag = harness.simulation.bagOf(entityId); assert.equal(bag.inventory[0].quantity, 1); assert.equal(bag.inventory[1].quantity, 1); assert.equal(bag.inventory.filter(Boolean).length, 2); } finally { harness.close(); } }); test("a spawner in a simulating open chunk creates deer up to maxAlive", () => { const harness = makeSimulation({ workerCount: 1 }); try { const data = createEmptyChunkData(); data.open = true; data.spawners = [{ localX: 8, localY: 8, config: { spawnerId: "0:0:136", mobTypeId: "deer", maxAlive: 2, spawnRangeTiles: 3 } }]; harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data)); harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 4, tileY: 4 }); harness.simulation.spawnCharacter(1, character); harness.simulation.step(); const deer = [...harness.simulation.directory.values()].filter((entity) => entity.firstName === "Deer"); assert.equal(deer.length, 2); const pigeons = [...harness.simulation.directory.values()].filter((entity) => entity.firstName === "Pigeon"); assert.equal(pigeons.length, 0); } finally { harness.close(); } }); test("eating food restores player health", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const worker = workerOf(harness.simulation, playerId); const handle = worker.world.ecs.handleOf(playerId); const health = worker.world.ecs.get(handle, Component.health); health.current = 6; const bag = harness.simulation.bagOf(playerId); const appleSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); assert.ok(appleSlot >= 0); harness.simulation.enqueue(1, { type: "eat-item", characterId: character.character_id, slotIndex: appleSlot }); harness.simulation.step(); const after = worker.world.ecs.get(handle, Component.health); assert.equal(after.current, 10); assert.equal(after.max, 10); const afterBag = harness.simulation.bagOf(playerId); assert.equal(afterBag.inventory[appleSlot].quantity, 2); } finally { harness.close(); } });