| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 4 | import { ItemId } from "../../shared/definitions/items.js"; |
| 5 | import { SkillId } from "../../shared/definitions/skills.js"; |
| 6 | import { isAdjacentTile, canTalkOrShopNear } from "../../shared/navigation/adjacency.js"; |
| 7 | import { totalXpForLevel } from "../../shared/skills/progression.js"; |
| 8 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 9 | import { Component } from "../src/ecs/components.js"; |
| 10 | |
| 11 | test("isAdjacentTile excludes same tile and allows neighbours", () => |
| 12 | { |
| 13 | assert.equal(isAdjacentTile(5, 5, 5, 5), false); |
| 14 | assert.equal(isAdjacentTile(5, 5, 6, 5), true); |
| 15 | assert.equal(isAdjacentTile(5, 5, 7, 5), false); |
| 16 | }); |
| 17 | |
| 18 | test("canTalkOrShopNear allows standing across a table or marketplace counter", () => |
| 19 | { |
| 20 | const blocks = new Map([ |
| 21 | ["10,8", BlockType.table], |
| 22 | ["20,8", BlockType.marketplace] |
| 23 | ]); |
| 24 | const getBlock = (x, y) => blocks.get(`${x},${y}`) ?? null; |
| 25 | // NPC at 10,9; table at 10,8; player at 10,7 — over the table. |
| 26 | assert.equal(canTalkOrShopNear(10, 7, 10, 9, getBlock), true); |
| 27 | assert.equal(isAdjacentTile(10, 7, 10, 9), false); |
| 28 | // Direct adjacency still works without furniture. |
| 29 | assert.equal(canTalkOrShopNear(11, 9, 10, 9, getBlock), true); |
| 30 | // Two tiles away with no counter is too far. |
| 31 | assert.equal(canTalkOrShopNear(10, 7, 10, 9, () => null), false); |
| 32 | // Marketplace counter works the same way. |
| 33 | assert.equal(canTalkOrShopNear(20, 7, 20, 9, getBlock), true); |
| 34 | }); |
| 35 | |
| 36 | function placeOre (worker, tileX, tileY, state) |
| 37 | { |
| 38 | const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); |
| 39 | const localX = tileX % 16; |
| 40 | const localY = tileY % 16; |
| 41 | const index = localY * 16 + localX; |
| 42 | data.blocks[index] = { typeId: BlockType.boulder, state, config: null }; |
| 43 | } |
| 44 | |
| 45 | function placeFlower (worker, tileX, tileY) |
| 46 | { |
| 47 | const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); |
| 48 | const localX = tileX % 16; |
| 49 | const localY = tileY % 16; |
| 50 | const index = localY * 16 + localX; |
| 51 | data.blocks[index] = { typeId: BlockType.flower, state: "red", config: null }; |
| 52 | } |
| 53 | |
| 54 | test("mining rejects standing on the ore boulder", () => |
| 55 | { |
| 56 | const harness = makeSimulation({ workerCount: 1 }); |
| 57 | try |
| 58 | { |
| 59 | const character = makeCharacter(harness.stateDb, { tileX: 9, tileY: 8 }); |
| 60 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 61 | const record = harness.simulation.entityRecord(playerId); |
| 62 | const worker = harness.simulation.workers[record.workerId]; |
| 63 | const handle = worker.world.ecs.handleOf(playerId); |
| 64 | worker.world.ecs.get(handle, Component.inventory).slots[0] = { |
| 65 | itemId: ItemId.bronzePickaxe, |
| 66 | quantity: 1 |
| 67 | }; |
| 68 | const skills = worker.world.ecs.get(handle, Component.skills); |
| 69 | skills.xp[SkillId.mining] = totalXpForLevel(10); |
| 70 | placeOre(worker, 9, 8, "iron-ore"); |
| 71 | |
| 72 | harness.simulation.enqueue(1, { |
| 73 | type: "mine", |
| 74 | characterId: character.character_id, |
| 75 | tileX: 9, |
| 76 | tileY: 8 |
| 77 | }); |
| 78 | const result = harness.simulation.step(); |
| 79 | assert.ok(result.events.some((event) => |
| 80 | event.type === "command-error" |
| 81 | && /stand next/i.test(event.message ?? ""))); |
| 82 | } |
| 83 | finally |
| 84 | { harness.close(); } |
| 85 | }); |
| 86 | |
| 87 | test("picking rejects standing on the plant", () => |
| 88 | { |
| 89 | const harness = makeSimulation({ workerCount: 1 }); |
| 90 | try |
| 91 | { |
| 92 | const character = makeCharacter(harness.stateDb, { tileX: 9, tileY: 8 }); |
| 93 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 94 | const record = harness.simulation.entityRecord(playerId); |
| 95 | const worker = harness.simulation.workers[record.workerId]; |
| 96 | placeFlower(worker, 9, 8); |
| 97 | |
| 98 | harness.simulation.enqueue(1, { |
| 99 | type: "pick", |
| 100 | characterId: character.character_id, |
| 101 | tileX: 9, |
| 102 | tileY: 8 |
| 103 | }); |
| 104 | const result = harness.simulation.step(); |
| 105 | assert.ok(result.events.some((event) => |
| 106 | event.type === "command-error" |
| 107 | && /stand next/i.test(event.message ?? ""))); |
| 108 | } |
| 109 | finally |
| 110 | { harness.close(); } |
| 111 | }); |