| 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 } 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 | function placeOre (worker, tileX, tileY, state) |
| 19 | { |
| 20 | const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); |
| 21 | const localX = tileX % 16; |
| 22 | const localY = tileY % 16; |
| 23 | const index = localY * 16 + localX; |
| 24 | data.blocks[index] = { typeId: BlockType.boulder, state, config: null }; |
| 25 | } |
| 26 | |
| 27 | function placeFlower (worker, tileX, tileY) |
| 28 | { |
| 29 | const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); |
| 30 | const localX = tileX % 16; |
| 31 | const localY = tileY % 16; |
| 32 | const index = localY * 16 + localX; |
| 33 | data.blocks[index] = { typeId: BlockType.flower, state: "red", config: null }; |
| 34 | } |
| 35 | |
| 36 | test("mining rejects standing on the ore boulder", () => |
| 37 | { |
| 38 | const harness = makeSimulation({ workerCount: 1 }); |
| 39 | try |
| 40 | { |
| 41 | const character = makeCharacter(harness.stateDb, { tileX: 9, tileY: 8 }); |
| 42 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 43 | const record = harness.simulation.entityRecord(playerId); |
| 44 | const worker = harness.simulation.workers[record.workerId]; |
| 45 | const handle = worker.world.ecs.handleOf(playerId); |
| 46 | worker.world.ecs.get(handle, Component.inventory).slots[0] = { |
| 47 | itemId: ItemId.bronzePickaxe, |
| 48 | quantity: 1 |
| 49 | }; |
| 50 | const skills = worker.world.ecs.get(handle, Component.skills); |
| 51 | skills.xp[SkillId.mining] = totalXpForLevel(10); |
| 52 | placeOre(worker, 9, 8, "iron-ore"); |
| 53 | |
| 54 | harness.simulation.enqueue(1, { |
| 55 | type: "mine", |
| 56 | characterId: character.character_id, |
| 57 | tileX: 9, |
| 58 | tileY: 8 |
| 59 | }); |
| 60 | const result = harness.simulation.step(); |
| 61 | assert.ok(result.events.some((event) => |
| 62 | event.type === "command-error" |
| 63 | && /stand next/i.test(event.message ?? ""))); |
| 64 | } |
| 65 | finally |
| 66 | { harness.close(); } |
| 67 | }); |
| 68 | |
| 69 | test("picking rejects standing on the plant", () => |
| 70 | { |
| 71 | const harness = makeSimulation({ workerCount: 1 }); |
| 72 | try |
| 73 | { |
| 74 | const character = makeCharacter(harness.stateDb, { tileX: 9, tileY: 8 }); |
| 75 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 76 | const record = harness.simulation.entityRecord(playerId); |
| 77 | const worker = harness.simulation.workers[record.workerId]; |
| 78 | placeFlower(worker, 9, 8); |
| 79 | |
| 80 | harness.simulation.enqueue(1, { |
| 81 | type: "pick", |
| 82 | characterId: character.character_id, |
| 83 | tileX: 9, |
| 84 | tileY: 8 |
| 85 | }); |
| 86 | const result = harness.simulation.step(); |
| 87 | assert.ok(result.events.some((event) => |
| 88 | event.type === "command-error" |
| 89 | && /stand next/i.test(event.message ?? ""))); |
| 90 | } |
| 91 | finally |
| 92 | { harness.close(); } |
| 93 | }); |