| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { WoodTier, tierRank } from "../../shared/definitions/materialTiers.js"; |
| 4 | import { |
| 5 | canChopWood, |
| 6 | woodcuttingDurationMs, |
| 7 | treeExhaustDurationMs, |
| 8 | logItemIdForWoodTier |
| 9 | } from "../../shared/definitions/gathering.js"; |
| 10 | import { ItemId, itemDefinition } from "../../shared/definitions/items.js"; |
| 11 | import { SkillId } from "../../shared/definitions/skills.js"; |
| 12 | import { BlockType, isChopableTreeBlock, woodTierForTreeBlock } from "../../shared/definitions/tiles.js"; |
| 13 | import { SeasonId, resolveSeasonalVisualState, treeStumpStateForSeason, scaleRegrowDurationMs } from "../../shared/world/season.js"; |
| 14 | import { ItemOriginKind, GatherOriginVerb } from "../../shared/items/origin.js"; |
| 15 | import { totalXpForLevel } from "../../shared/skills/progression.js"; |
| 16 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 17 | import { Component } from "../src/ecs/components.js"; |
| 18 | |
| 19 | function countItem (slots, itemId) |
| 20 | { |
| 21 | return (slots ?? []).reduce((sum, slot) => |
| 22 | { |
| 23 | if (slot?.itemId === itemId) |
| 24 | { return sum + (slot.quantity | 0); } |
| 25 | return sum; |
| 26 | }, 0); |
| 27 | } |
| 28 | |
| 29 | function placeTree (worker, tileX, tileY, state = SeasonId.summer) |
| 30 | { |
| 31 | const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); |
| 32 | const localX = tileX % 16; |
| 33 | const localY = tileY % 16; |
| 34 | const index = localY * 16 + localX; |
| 35 | data.blocks[index] = { typeId: BlockType.pineTree, state, config: null }; |
| 36 | return { data, index }; |
| 37 | } |
| 38 | |
| 39 | function bagAxe (worker, playerId, itemId) |
| 40 | { |
| 41 | const handle = worker.world.ecs.handleOf(playerId); |
| 42 | const inventory = worker.world.ecs.get(handle, Component.inventory); |
| 43 | inventory.slots[0] = { itemId, quantity: 1 }; |
| 44 | } |
| 45 | |
| 46 | function setSkillLevel (worker, playerId, skillId, level) |
| 47 | { |
| 48 | const handle = worker.world.ecs.handleOf(playerId); |
| 49 | const skills = worker.world.ecs.get(handle, Component.skills); |
| 50 | skills.xp[skillId] = totalXpForLevel(level); |
| 51 | } |
| 52 | |
| 53 | function advanceMs (harness, ms) |
| 54 | { |
| 55 | const ticks = Math.ceil(ms / 50) + 3; |
| 56 | for (let i = 0; i < ticks; i++) |
| 57 | { |
| 58 | harness.advance(50); |
| 59 | harness.simulation.step(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | test("pine tree yields pine logs and uses axe gates", () => |
| 64 | { |
| 65 | assert.equal(woodTierForTreeBlock(BlockType.pineTree), WoodTier.pine); |
| 66 | assert.equal(logItemIdForWoodTier(WoodTier.pine), ItemId.pineLog); |
| 67 | assert.equal(itemDefinition(ItemId.bronzeAxe)?.equipment?.kind, "axeTool"); |
| 68 | assert.equal(canChopWood(1, WoodTier.pine, WoodTier.pine).ok, true); |
| 69 | // Birch needs Woodcutting 10; tool may be one tier below once skill is met. |
| 70 | assert.equal(canChopWood(1, WoodTier.pine, WoodTier.birch).ok, false); |
| 71 | assert.equal(canChopWood(10, WoodTier.pine, WoodTier.birch).ok, true); |
| 72 | assert.equal(canChopWood(10, WoodTier.pine, WoodTier.oak).ok, false); |
| 73 | assert.equal(woodcuttingDurationMs(WoodTier.pine, WoodTier.pine), 5000); |
| 74 | assert.equal(treeExhaustDurationMs(WoodTier.pine), 10_000); |
| 75 | }); |
| 76 | |
| 77 | test("seasonal stump state follows calendar season", () => |
| 78 | { |
| 79 | const states = ["fall", "fall-stump", "summer", "summer-stump", "winter", "winter-stump"]; |
| 80 | assert.equal( |
| 81 | resolveSeasonalVisualState(states, "summer-stump", SeasonId.winter), |
| 82 | "winter-stump" |
| 83 | ); |
| 84 | assert.equal( |
| 85 | resolveSeasonalVisualState(states, "summer", SeasonId.fall), |
| 86 | SeasonId.fall |
| 87 | ); |
| 88 | assert.equal(treeStumpStateForSeason(SeasonId.summer), "summer-stump"); |
| 89 | assert.equal(isChopableTreeBlock({ typeId: BlockType.pineTree, state: "summer" }), true); |
| 90 | assert.equal(isChopableTreeBlock({ typeId: BlockType.pineTree, state: "summer-stump" }), false); |
| 91 | }); |
| 92 | |
| 93 | test("chopping without an axe is rejected", () => |
| 94 | { |
| 95 | const harness = makeSimulation({ workerCount: 1 }); |
| 96 | try |
| 97 | { |
| 98 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 99 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 100 | const record = harness.simulation.entityRecord(playerId); |
| 101 | const worker = harness.simulation.workers[record.workerId]; |
| 102 | placeTree(worker, 9, 8); |
| 103 | |
| 104 | harness.simulation.enqueue(1, { |
| 105 | type: "chop", |
| 106 | characterId: character.character_id, |
| 107 | tileX: 9, |
| 108 | tileY: 8 |
| 109 | }); |
| 110 | const result = harness.simulation.step(); |
| 111 | assert.ok(result.events.some((event) => |
| 112 | event.type === "command-error" |
| 113 | && event.message === "You need an axe to chop this!")); |
| 114 | } |
| 115 | finally |
| 116 | { harness.close(); } |
| 117 | }); |
| 118 | |
| 119 | test("chopping a pine tree yields a pine log, stump, and woodcutting XP", () => |
| 120 | { |
| 121 | const harness = makeSimulation({ workerCount: 1 }); |
| 122 | try |
| 123 | { |
| 124 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 125 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 126 | const record = harness.simulation.entityRecord(playerId); |
| 127 | const worker = harness.simulation.workers[record.workerId]; |
| 128 | bagAxe(worker, playerId, ItemId.bronzeAxe); |
| 129 | setSkillLevel(worker, playerId, SkillId.woodcutting, 1); |
| 130 | placeTree(worker, 9, 8, SeasonId.summer); |
| 131 | |
| 132 | harness.simulation.enqueue(1, { |
| 133 | type: "chop", |
| 134 | characterId: character.character_id, |
| 135 | tileX: 9, |
| 136 | tileY: 8 |
| 137 | }); |
| 138 | const start = harness.simulation.step(); |
| 139 | assert.ok(start.events.some((event) => |
| 140 | event.type === "gather-swing" |
| 141 | && event.toolItemId === ItemId.bronzeAxe), JSON.stringify(start.events)); |
| 142 | advanceMs(harness, woodcuttingDurationMs(WoodTier.pine, WoodTier.pine) + 200); |
| 143 | |
| 144 | const handle = worker.world.ecs.handleOf(playerId); |
| 145 | const inventory = worker.world.ecs.get(handle, Component.inventory); |
| 146 | assert.equal(countItem(inventory.slots, ItemId.pineLog), 1); |
| 147 | const log = inventory.slots.find((slot) => slot?.itemId === ItemId.pineLog); |
| 148 | assert.equal(log?.origins?.[0]?.origin?.kind, ItemOriginKind.gathered); |
| 149 | assert.equal(log?.origins?.[0]?.origin?.verb, GatherOriginVerb.felled); |
| 150 | |
| 151 | const block = worker.world.chunkData(0, 0).blocks[8 * 16 + 9]; |
| 152 | assert.equal(block.typeId, BlockType.pineTree); |
| 153 | assert.ok(String(block.state).endsWith("-stump")); |
| 154 | assert.equal(isChopableTreeBlock(block), false); |
| 155 | |
| 156 | const skills = worker.world.ecs.get(handle, Component.skills); |
| 157 | assert.ok((skills.xp[SkillId.woodcutting] | 0) > totalXpForLevel(1)); |
| 158 | } |
| 159 | finally |
| 160 | { harness.close(); } |
| 161 | }); |
| 162 | |
| 163 | test("tree stump respawns after exhaust duration", () => |
| 164 | { |
| 165 | const harness = makeSimulation({ workerCount: 1 }); |
| 166 | try |
| 167 | { |
| 168 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 169 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 170 | const record = harness.simulation.entityRecord(playerId); |
| 171 | const worker = harness.simulation.workers[record.workerId]; |
| 172 | bagAxe(worker, playerId, ItemId.bronzeAxe); |
| 173 | placeTree(worker, 9, 8); |
| 174 | |
| 175 | harness.simulation.enqueue(1, { |
| 176 | type: "chop", |
| 177 | characterId: character.character_id, |
| 178 | tileX: 9, |
| 179 | tileY: 8 |
| 180 | }); |
| 181 | advanceMs(harness, woodcuttingDurationMs(WoodTier.pine, WoodTier.pine) + 200); |
| 182 | assert.ok(String(worker.world.chunkData(0, 0).blocks[8 * 16 + 9].state).endsWith("-stump")); |
| 183 | |
| 184 | const exhaustMs = scaleRegrowDurationMs(treeExhaustDurationMs(WoodTier.pine), harness.now()); |
| 185 | advanceMs(harness, exhaustMs + 200); |
| 186 | const block = worker.world.chunkData(0, 0).blocks[8 * 16 + 9]; |
| 187 | assert.equal(isChopableTreeBlock(block), true); |
| 188 | assert.equal(tierRank(WoodTier.pine), 1); |
| 189 | } |
| 190 | finally |
| 191 | { harness.close(); } |
| 192 | }); |