| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { |
| 4 | MaterialTier, |
| 5 | GatherMaterialId, |
| 6 | tierMinSkillLevel, |
| 7 | tierMineSkillLevel, |
| 8 | tierSmeltSkillLevel, |
| 9 | tierRank, |
| 10 | miningDurationMs, |
| 11 | oreExhaustDurationMs, |
| 12 | oreBoulderState, |
| 13 | materialFromOreBoulderState, |
| 14 | EXHAUSTED_ORE_BOULDER_STATE |
| 15 | } from "../../shared/definitions/materialTiers.js"; |
| 16 | import { |
| 17 | canMineMaterial, |
| 18 | oreItemIdForMaterial |
| 19 | } from "../../shared/definitions/gathering.js"; |
| 20 | import { ItemId, itemDefinition } from "../../shared/definitions/items.js"; |
| 21 | import { SkillId } from "../../shared/definitions/skills.js"; |
| 22 | import { canEquipItem } from "../../shared/equipment/stats.js"; |
| 23 | import { normalizePlayerSkills } from "../../shared/skills/state.js"; |
| 24 | import { totalXpForLevel } from "../../shared/skills/progression.js"; |
| 25 | import { BlockType, blockDefinition } from "../../shared/definitions/tiles.js"; |
| 26 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 27 | import { Component } from "../src/ecs/components.js"; |
| 28 | |
| 29 | function countItem (slots, itemId) |
| 30 | { |
| 31 | return (slots ?? []).reduce((sum, slot) => |
| 32 | { |
| 33 | if (slot?.itemId === itemId) |
| 34 | { return sum + (slot.quantity | 0); } |
| 35 | return sum; |
| 36 | }, 0); |
| 37 | } |
| 38 | |
| 39 | function placeOre (worker, tileX, tileY, state) |
| 40 | { |
| 41 | const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); |
| 42 | const localX = tileX % 16; |
| 43 | const localY = tileY % 16; |
| 44 | const index = localY * 16 + localX; |
| 45 | data.blocks[index] = { typeId: BlockType.boulder, state, config: null }; |
| 46 | return { data, index }; |
| 47 | } |
| 48 | |
| 49 | function bagPickaxe (worker, playerId, itemId) |
| 50 | { |
| 51 | const handle = worker.world.ecs.handleOf(playerId); |
| 52 | const inventory = worker.world.ecs.get(handle, Component.inventory); |
| 53 | inventory.slots[0] = { itemId, quantity: 1 }; |
| 54 | } |
| 55 | |
| 56 | function setSkillLevel (worker, playerId, skillId, level) |
| 57 | { |
| 58 | const handle = worker.world.ecs.handleOf(playerId); |
| 59 | const skills = worker.world.ecs.get(handle, Component.skills); |
| 60 | skills.xp[skillId] = totalXpForLevel(level); |
| 61 | } |
| 62 | |
| 63 | function advanceMs (harness, ms) |
| 64 | { |
| 65 | const ticks = Math.ceil(ms / 50) + 3; |
| 66 | for (let i = 0; i < ticks; i++) |
| 67 | { |
| 68 | harness.advance(50); |
| 69 | harness.simulation.step(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | test("tier skill gates: equip / mine / smelt", () => |
| 74 | { |
| 75 | assert.equal(tierMinSkillLevel(MaterialTier.bronze), 1); |
| 76 | assert.equal(tierMinSkillLevel(MaterialTier.mithril), 35); |
| 77 | assert.equal(tierMinSkillLevel(MaterialTier.dragon), 90); |
| 78 | assert.equal(tierMineSkillLevel(MaterialTier.iron), 5); |
| 79 | assert.equal(tierMineSkillLevel(MaterialTier.mithril), 18); |
| 80 | assert.equal(tierSmeltSkillLevel(MaterialTier.iron), 8); |
| 81 | assert.equal(tierSmeltSkillLevel(MaterialTier.steel), 15); |
| 82 | assert.equal(tierMineSkillLevel(GatherMaterialId.silver), 5); |
| 83 | assert.equal(tierSmeltSkillLevel(GatherMaterialId.silver), 8); |
| 84 | assert.equal(tierRank(GatherMaterialId.silver), 2); |
| 85 | assert.equal(tierMineSkillLevel(GatherMaterialId.gold), 10); |
| 86 | assert.equal(tierSmeltSkillLevel(GatherMaterialId.gold), 15); |
| 87 | assert.equal(tierRank(GatherMaterialId.gold), 3); |
| 88 | assert.equal(tierRank(MaterialTier.dragon), 7); |
| 89 | }); |
| 90 | |
| 91 | test("mining duration uses resource and tool ranks", () => |
| 92 | { |
| 93 | assert.equal(miningDurationMs(MaterialTier.bronze, MaterialTier.bronze), 5000); |
| 94 | assert.equal(miningDurationMs(MaterialTier.iron, MaterialTier.bronze), 13000); |
| 95 | assert.ok(miningDurationMs(MaterialTier.bronze, MaterialTier.dragon) >= 500); |
| 96 | }); |
| 97 | |
| 98 | test("ore exhaust duration is 10s times associated bar rank", () => |
| 99 | { |
| 100 | assert.equal(oreExhaustDurationMs(MaterialTier.bronze), 10_000); |
| 101 | assert.equal(oreExhaustDurationMs(MaterialTier.iron), 20_000); |
| 102 | assert.equal(oreExhaustDurationMs(GatherMaterialId.tin), 10_000); |
| 103 | assert.equal(oreExhaustDurationMs(GatherMaterialId.copper), 10_000); |
| 104 | assert.equal(oreExhaustDurationMs(GatherMaterialId.coal), 30_000); |
| 105 | assert.equal(oreExhaustDurationMs(GatherMaterialId.gold), 30_000); |
| 106 | assert.equal(oreExhaustDurationMs(GatherMaterialId.silver), 20_000); |
| 107 | assert.equal(oreExhaustDurationMs(MaterialTier.dragon), 70_000); |
| 108 | }); |
| 109 | |
| 110 | test("ore boulder states map to materials", () => |
| 111 | { |
| 112 | assert.equal(materialFromOreBoulderState("tin-ore"), GatherMaterialId.tin); |
| 113 | assert.equal(materialFromOreBoulderState("copper-ore"), GatherMaterialId.copper); |
| 114 | assert.equal(materialFromOreBoulderState("coal"), GatherMaterialId.coal); |
| 115 | assert.equal(materialFromOreBoulderState("silver-ore"), GatherMaterialId.silver); |
| 116 | assert.equal(materialFromOreBoulderState("gold-ore"), GatherMaterialId.gold); |
| 117 | assert.equal(materialFromOreBoulderState("boulder"), null); |
| 118 | assert.equal(materialFromOreBoulderState("bronze-ore"), null); |
| 119 | assert.equal(oreBoulderState(MaterialTier.mithril), "mithril-ore"); |
| 120 | assert.equal(oreBoulderState(GatherMaterialId.silver), "silver-ore"); |
| 121 | assert.equal(oreBoulderState(GatherMaterialId.gold), "gold-ore"); |
| 122 | }); |
| 123 | |
| 124 | test("boulder block exists", () => |
| 125 | { |
| 126 | assert.equal(blockDefinition(BlockType.boulder)?.key, "boulder"); |
| 127 | }); |
| 128 | |
| 129 | test("iron pickaxe requires Mining 10", () => |
| 130 | { |
| 131 | const skills = normalizePlayerSkills(null); |
| 132 | assert.equal(canEquipItem(skills, itemDefinition(ItemId.ironPickaxe)).ok, false); |
| 133 | skills.xp[SkillId.mining] = totalXpForLevel(10); |
| 134 | assert.equal(canEquipItem(skills, itemDefinition(ItemId.ironPickaxe)).ok, true); |
| 135 | assert.equal(canEquipItem(skills, itemDefinition(ItemId.bronzePickaxe)).ok, true); |
| 136 | }); |
| 137 | |
| 138 | test("canMineMaterial enforces skill and tool tier", () => |
| 139 | { |
| 140 | assert.equal(canMineMaterial(1, MaterialTier.bronze, MaterialTier.iron).ok, false); |
| 141 | assert.equal(canMineMaterial(5, MaterialTier.bronze, MaterialTier.iron).ok, false); |
| 142 | assert.equal(canMineMaterial(5, MaterialTier.iron, MaterialTier.iron).ok, true); |
| 143 | }); |
| 144 | |
| 145 | test("mining without a pickaxe is rejected", () => |
| 146 | { |
| 147 | const harness = makeSimulation({ workerCount: 1 }); |
| 148 | try |
| 149 | { |
| 150 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 151 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 152 | const record = harness.simulation.entityRecord(playerId); |
| 153 | const worker = harness.simulation.workers[record.workerId]; |
| 154 | placeOre(worker, 9, 8, "copper-ore"); |
| 155 | |
| 156 | harness.simulation.enqueue(1, { |
| 157 | type: "mine", |
| 158 | characterId: character.character_id, |
| 159 | tileX: 9, |
| 160 | tileY: 8 |
| 161 | }); |
| 162 | const result = harness.simulation.step(); |
| 163 | assert.ok(result.events.some((event) => |
| 164 | event.type === "command-error" |
| 165 | && event.message === "You need a pickaxe to mine this!")); |
| 166 | } |
| 167 | finally |
| 168 | { harness.close(); } |
| 169 | }); |
| 170 | |
| 171 | test("plain boulder cannot be mined", () => |
| 172 | { |
| 173 | const harness = makeSimulation({ workerCount: 1 }); |
| 174 | try |
| 175 | { |
| 176 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 177 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 178 | const record = harness.simulation.entityRecord(playerId); |
| 179 | const worker = harness.simulation.workers[record.workerId]; |
| 180 | bagPickaxe(worker, playerId, ItemId.bronzePickaxe); |
| 181 | placeOre(worker, 9, 8, EXHAUSTED_ORE_BOULDER_STATE); |
| 182 | |
| 183 | harness.simulation.enqueue(1, { |
| 184 | type: "mine", |
| 185 | characterId: character.character_id, |
| 186 | tileX: 9, |
| 187 | tileY: 8 |
| 188 | }); |
| 189 | const result = harness.simulation.step(); |
| 190 | assert.ok(result.events.some((event) => |
| 191 | event.type === "command-error" |
| 192 | && (/no ore|exhausted/i.test(event.message ?? "")))); |
| 193 | } |
| 194 | finally |
| 195 | { harness.close(); } |
| 196 | }); |
| 197 | |
| 198 | test("weak pickaxe cannot mine higher-tier ore", () => |
| 199 | { |
| 200 | const harness = makeSimulation({ workerCount: 1 }); |
| 201 | try |
| 202 | { |
| 203 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 204 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 205 | const record = harness.simulation.entityRecord(playerId); |
| 206 | const worker = harness.simulation.workers[record.workerId]; |
| 207 | bagPickaxe(worker, playerId, ItemId.bronzePickaxe); |
| 208 | setSkillLevel(worker, playerId, SkillId.mining, 10); |
| 209 | placeOre(worker, 9, 8, "iron-ore"); |
| 210 | |
| 211 | harness.simulation.enqueue(1, { |
| 212 | type: "mine", |
| 213 | characterId: character.character_id, |
| 214 | tileX: 9, |
| 215 | tileY: 8 |
| 216 | }); |
| 217 | const result = harness.simulation.step(); |
| 218 | assert.ok(result.events.some((event) => |
| 219 | event.type === "command-error" |
| 220 | && event.message === "You need a pickaxe to mine this!")); |
| 221 | } |
| 222 | finally |
| 223 | { harness.close(); } |
| 224 | }); |
| 225 | |
| 226 | test("mining uses the best pickaxe in inventory without equipping", () => |
| 227 | { |
| 228 | const harness = makeSimulation({ workerCount: 1 }); |
| 229 | try |
| 230 | { |
| 231 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 232 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 233 | const record = harness.simulation.entityRecord(playerId); |
| 234 | const worker = harness.simulation.workers[record.workerId]; |
| 235 | setSkillLevel(worker, playerId, SkillId.mining, 10); |
| 236 | const handle = worker.world.ecs.handleOf(playerId); |
| 237 | const inventory = worker.world.ecs.get(handle, Component.inventory); |
| 238 | inventory.slots[0] = { itemId: ItemId.bronzePickaxe, quantity: 1 }; |
| 239 | inventory.slots[1] = { itemId: ItemId.ironPickaxe, quantity: 1 }; |
| 240 | placeOre(worker, 9, 8, "iron-ore"); |
| 241 | |
| 242 | harness.simulation.enqueue(1, { |
| 243 | type: "mine", |
| 244 | characterId: character.character_id, |
| 245 | tileX: 9, |
| 246 | tileY: 8 |
| 247 | }); |
| 248 | harness.simulation.step(); |
| 249 | advanceMs(harness, miningDurationMs(MaterialTier.iron, MaterialTier.iron)); |
| 250 | |
| 251 | const bag = harness.simulation.bagOf(playerId); |
| 252 | assert.ok(countItem(bag.inventory, ItemId.ironOre) >= 1); |
| 253 | } |
| 254 | finally |
| 255 | { harness.close(); } |
| 256 | }); |
| 257 | |
| 258 | test("mining grants ore and Mining XP then exhausts and respawns the node", () => |
| 259 | { |
| 260 | const harness = makeSimulation({ workerCount: 1 }); |
| 261 | try |
| 262 | { |
| 263 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 264 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 265 | const record = harness.simulation.entityRecord(playerId); |
| 266 | const worker = harness.simulation.workers[record.workerId]; |
| 267 | bagPickaxe(worker, playerId, ItemId.bronzePickaxe); |
| 268 | const { data, index } = placeOre(worker, 9, 8, "copper-ore"); |
| 269 | |
| 270 | harness.simulation.enqueue(1, { |
| 271 | type: "mine", |
| 272 | characterId: character.character_id, |
| 273 | tileX: 9, |
| 274 | tileY: 8 |
| 275 | }); |
| 276 | const start = harness.simulation.step(); |
| 277 | assert.ok(start.events.some((event) => event.type === "gather-swing" || event.type === "tile-shove")); |
| 278 | const swing = start.events.find((event) => event.type === "gather-swing"); |
| 279 | assert.equal(swing?.toolItemId, ItemId.bronzePickaxe); |
| 280 | |
| 281 | advanceMs(harness, miningDurationMs(GatherMaterialId.copper, MaterialTier.bronze)); |
| 282 | |
| 283 | const bag = harness.simulation.bagOf(playerId); |
| 284 | assert.ok(countItem(bag.inventory, ItemId.copperOre) >= 1); |
| 285 | const skills = harness.stateDb.getSkills(character.character_id); |
| 286 | assert.ok(skills.xp.mining > 0); |
| 287 | assert.equal(oreItemIdForMaterial(GatherMaterialId.copper), ItemId.copperOre); |
| 288 | assert.equal(data.blocks[index].state, EXHAUSTED_ORE_BOULDER_STATE); |
| 289 | |
| 290 | harness.simulation.enqueue(1, { |
| 291 | type: "mine", |
| 292 | characterId: character.character_id, |
| 293 | tileX: 9, |
| 294 | tileY: 8 |
| 295 | }); |
| 296 | const exhaustedResult = harness.simulation.step(); |
| 297 | assert.ok(exhaustedResult.events.some((event) => |
| 298 | event.type === "command-error" && /exhausted/i.test(event.message ?? ""))); |
| 299 | |
| 300 | advanceMs(harness, oreExhaustDurationMs(GatherMaterialId.copper)); |
| 301 | assert.equal(data.blocks[index].state, "copper-ore"); |
| 302 | } |
| 303 | finally |
| 304 | { harness.close(); } |
| 305 | }); |
| 306 | |
| 307 | test("coal mining yields coal and exhausts the node", () => |
| 308 | { |
| 309 | const harness = makeSimulation({ workerCount: 1 }); |
| 310 | try |
| 311 | { |
| 312 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 313 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 314 | const record = harness.simulation.entityRecord(playerId); |
| 315 | const worker = harness.simulation.workers[record.workerId]; |
| 316 | setSkillLevel(worker, playerId, SkillId.mining, 20); |
| 317 | bagPickaxe(worker, playerId, ItemId.steelPickaxe); |
| 318 | const { data, index } = placeOre(worker, 9, 8, "coal"); |
| 319 | |
| 320 | harness.simulation.enqueue(1, { |
| 321 | type: "mine", |
| 322 | characterId: character.character_id, |
| 323 | tileX: 9, |
| 324 | tileY: 8 |
| 325 | }); |
| 326 | harness.simulation.step(); |
| 327 | advanceMs(harness, miningDurationMs(GatherMaterialId.coal, MaterialTier.steel)); |
| 328 | |
| 329 | const bag = harness.simulation.bagOf(playerId); |
| 330 | assert.ok(countItem(bag.inventory, ItemId.coal) >= 1); |
| 331 | assert.equal(data.blocks[index].state, EXHAUSTED_ORE_BOULDER_STATE); |
| 332 | } |
| 333 | finally |
| 334 | { harness.close(); } |
| 335 | }); |