| 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 | // One tier below is allowed once the Mining skill gate is met. |
| 142 | assert.equal(canMineMaterial(5, MaterialTier.bronze, MaterialTier.iron).ok, true); |
| 143 | assert.equal(canMineMaterial(5, MaterialTier.iron, MaterialTier.iron).ok, true); |
| 144 | assert.equal(canMineMaterial(10, MaterialTier.bronze, MaterialTier.steel).ok, false); |
| 145 | }); |
| 146 | |
| 147 | test("gemstones require equal or higher pickaxe tier", () => |
| 148 | { |
| 149 | assert.equal(canMineMaterial(1, MaterialTier.bronze, GatherMaterialId.tourmaline).ok, true); |
| 150 | assert.equal(canMineMaterial(10, MaterialTier.iron, GatherMaterialId.opal).ok, false); |
| 151 | assert.equal(canMineMaterial(10, MaterialTier.steel, GatherMaterialId.opal).ok, true); |
| 152 | }); |
| 153 | |
| 154 | test("mining without a pickaxe is rejected", () => |
| 155 | { |
| 156 | const harness = makeSimulation({ workerCount: 1 }); |
| 157 | try |
| 158 | { |
| 159 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 160 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 161 | const record = harness.simulation.entityRecord(playerId); |
| 162 | const worker = harness.simulation.workers[record.workerId]; |
| 163 | placeOre(worker, 9, 8, "copper-ore"); |
| 164 | |
| 165 | harness.simulation.enqueue(1, { |
| 166 | type: "mine", |
| 167 | characterId: character.character_id, |
| 168 | tileX: 9, |
| 169 | tileY: 8 |
| 170 | }); |
| 171 | const result = harness.simulation.step(); |
| 172 | assert.ok(result.events.some((event) => |
| 173 | event.type === "command-error" |
| 174 | && event.message === "You need a pickaxe to mine this!")); |
| 175 | } |
| 176 | finally |
| 177 | { harness.close(); } |
| 178 | }); |
| 179 | |
| 180 | test("plain boulder cannot be mined", () => |
| 181 | { |
| 182 | const harness = makeSimulation({ workerCount: 1 }); |
| 183 | try |
| 184 | { |
| 185 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 186 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 187 | const record = harness.simulation.entityRecord(playerId); |
| 188 | const worker = harness.simulation.workers[record.workerId]; |
| 189 | bagPickaxe(worker, playerId, ItemId.bronzePickaxe); |
| 190 | placeOre(worker, 9, 8, EXHAUSTED_ORE_BOULDER_STATE); |
| 191 | |
| 192 | harness.simulation.enqueue(1, { |
| 193 | type: "mine", |
| 194 | characterId: character.character_id, |
| 195 | tileX: 9, |
| 196 | tileY: 8 |
| 197 | }); |
| 198 | const result = harness.simulation.step(); |
| 199 | assert.ok(result.events.some((event) => |
| 200 | event.type === "command-error" |
| 201 | && (/no ore|exhausted/i.test(event.message ?? "")))); |
| 202 | } |
| 203 | finally |
| 204 | { harness.close(); } |
| 205 | }); |
| 206 | |
| 207 | test("pickaxe two tiers below cannot mine the ore", () => |
| 208 | { |
| 209 | const harness = makeSimulation({ workerCount: 1 }); |
| 210 | try |
| 211 | { |
| 212 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 213 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 214 | const record = harness.simulation.entityRecord(playerId); |
| 215 | const worker = harness.simulation.workers[record.workerId]; |
| 216 | bagPickaxe(worker, playerId, ItemId.bronzePickaxe); |
| 217 | setSkillLevel(worker, playerId, SkillId.mining, 20); |
| 218 | placeOre(worker, 9, 8, "coal"); |
| 219 | |
| 220 | harness.simulation.enqueue(1, { |
| 221 | type: "mine", |
| 222 | characterId: character.character_id, |
| 223 | tileX: 9, |
| 224 | tileY: 8 |
| 225 | }); |
| 226 | const result = harness.simulation.step(); |
| 227 | assert.ok(result.events.some((event) => |
| 228 | event.type === "command-error" |
| 229 | && event.message === "You need a pickaxe to mine this!")); |
| 230 | } |
| 231 | finally |
| 232 | { harness.close(); } |
| 233 | }); |
| 234 | |
| 235 | test("bronze pickaxe can mine iron ore (one tier below)", () => |
| 236 | { |
| 237 | const harness = makeSimulation({ workerCount: 1 }); |
| 238 | try |
| 239 | { |
| 240 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 241 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 242 | const record = harness.simulation.entityRecord(playerId); |
| 243 | const worker = harness.simulation.workers[record.workerId]; |
| 244 | bagPickaxe(worker, playerId, ItemId.bronzePickaxe); |
| 245 | setSkillLevel(worker, playerId, SkillId.mining, 10); |
| 246 | placeOre(worker, 9, 8, "iron-ore"); |
| 247 | |
| 248 | harness.simulation.enqueue(1, { |
| 249 | type: "mine", |
| 250 | characterId: character.character_id, |
| 251 | tileX: 9, |
| 252 | tileY: 8 |
| 253 | }); |
| 254 | const start = harness.simulation.step(); |
| 255 | assert.ok(start.events.some((event) => |
| 256 | event.type === "gather-swing" |
| 257 | && event.toolItemId === ItemId.bronzePickaxe), JSON.stringify(start.events)); |
| 258 | } |
| 259 | finally |
| 260 | { harness.close(); } |
| 261 | }); |
| 262 | |
| 263 | test("mining uses the best pickaxe in inventory without equipping", () => |
| 264 | { |
| 265 | const harness = makeSimulation({ workerCount: 1 }); |
| 266 | try |
| 267 | { |
| 268 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 269 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 270 | const record = harness.simulation.entityRecord(playerId); |
| 271 | const worker = harness.simulation.workers[record.workerId]; |
| 272 | setSkillLevel(worker, playerId, SkillId.mining, 10); |
| 273 | const handle = worker.world.ecs.handleOf(playerId); |
| 274 | const inventory = worker.world.ecs.get(handle, Component.inventory); |
| 275 | inventory.slots[0] = { itemId: ItemId.bronzePickaxe, quantity: 1 }; |
| 276 | inventory.slots[1] = { itemId: ItemId.ironPickaxe, quantity: 1 }; |
| 277 | placeOre(worker, 9, 8, "iron-ore"); |
| 278 | |
| 279 | harness.simulation.enqueue(1, { |
| 280 | type: "mine", |
| 281 | characterId: character.character_id, |
| 282 | tileX: 9, |
| 283 | tileY: 8 |
| 284 | }); |
| 285 | harness.simulation.step(); |
| 286 | advanceMs(harness, miningDurationMs(MaterialTier.iron, MaterialTier.iron)); |
| 287 | |
| 288 | const bag = harness.simulation.bagOf(playerId); |
| 289 | assert.ok(countItem(bag.inventory, ItemId.ironOre) >= 1); |
| 290 | const oreStack = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.ironOre); |
| 291 | assert.ok(oreStack?.origins?.some((entry) => |
| 292 | entry.origin?.kind === "gathered" |
| 293 | && entry.origin?.verb === "Mined" |
| 294 | && typeof entry.origin?.displayName === "string" |
| 295 | && entry.origin.displayName.length > 0)); |
| 296 | } |
| 297 | finally |
| 298 | { harness.close(); } |
| 299 | }); |
| 300 | |
| 301 | test("mining grants ore and Mining XP then exhausts and respawns the node", () => |
| 302 | { |
| 303 | const harness = makeSimulation({ workerCount: 1 }); |
| 304 | try |
| 305 | { |
| 306 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 307 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 308 | const record = harness.simulation.entityRecord(playerId); |
| 309 | const worker = harness.simulation.workers[record.workerId]; |
| 310 | bagPickaxe(worker, playerId, ItemId.bronzePickaxe); |
| 311 | const { data, index } = placeOre(worker, 9, 8, "copper-ore"); |
| 312 | |
| 313 | harness.simulation.enqueue(1, { |
| 314 | type: "mine", |
| 315 | characterId: character.character_id, |
| 316 | tileX: 9, |
| 317 | tileY: 8 |
| 318 | }); |
| 319 | const start = harness.simulation.step(); |
| 320 | assert.ok(start.events.some((event) => event.type === "gather-swing" || event.type === "tile-shove")); |
| 321 | const swing = start.events.find((event) => event.type === "gather-swing"); |
| 322 | assert.equal(swing?.toolItemId, ItemId.bronzePickaxe); |
| 323 | |
| 324 | advanceMs(harness, miningDurationMs(GatherMaterialId.copper, MaterialTier.bronze)); |
| 325 | |
| 326 | const bag = harness.simulation.bagOf(playerId); |
| 327 | assert.ok(countItem(bag.inventory, ItemId.copperOre) >= 1); |
| 328 | const skills = harness.stateDb.getSkills(character.character_id); |
| 329 | assert.ok(skills.xp.mining > 0); |
| 330 | assert.equal(oreItemIdForMaterial(GatherMaterialId.copper), ItemId.copperOre); |
| 331 | assert.equal(data.blocks[index].state, EXHAUSTED_ORE_BOULDER_STATE); |
| 332 | |
| 333 | harness.simulation.enqueue(1, { |
| 334 | type: "mine", |
| 335 | characterId: character.character_id, |
| 336 | tileX: 9, |
| 337 | tileY: 8 |
| 338 | }); |
| 339 | const exhaustedResult = harness.simulation.step(); |
| 340 | assert.ok(exhaustedResult.events.some((event) => |
| 341 | event.type === "command-error" && /exhausted/i.test(event.message ?? ""))); |
| 342 | |
| 343 | advanceMs(harness, oreExhaustDurationMs(GatherMaterialId.copper)); |
| 344 | assert.equal(data.blocks[index].state, "copper-ore"); |
| 345 | } |
| 346 | finally |
| 347 | { harness.close(); } |
| 348 | }); |
| 349 | |
| 350 | test("coal mining yields coal and exhausts the node", () => |
| 351 | { |
| 352 | const harness = makeSimulation({ workerCount: 1 }); |
| 353 | try |
| 354 | { |
| 355 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 356 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 357 | const record = harness.simulation.entityRecord(playerId); |
| 358 | const worker = harness.simulation.workers[record.workerId]; |
| 359 | setSkillLevel(worker, playerId, SkillId.mining, 20); |
| 360 | bagPickaxe(worker, playerId, ItemId.steelPickaxe); |
| 361 | const { data, index } = placeOre(worker, 9, 8, "coal"); |
| 362 | |
| 363 | harness.simulation.enqueue(1, { |
| 364 | type: "mine", |
| 365 | characterId: character.character_id, |
| 366 | tileX: 9, |
| 367 | tileY: 8 |
| 368 | }); |
| 369 | harness.simulation.step(); |
| 370 | advanceMs(harness, miningDurationMs(GatherMaterialId.coal, MaterialTier.steel)); |
| 371 | |
| 372 | const bag = harness.simulation.bagOf(playerId); |
| 373 | assert.ok(countItem(bag.inventory, ItemId.coal) >= 1); |
| 374 | assert.equal(data.blocks[index].state, EXHAUSTED_ORE_BOULDER_STATE); |
| 375 | } |
| 376 | finally |
| 377 | { harness.close(); } |
| 378 | }); |