| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { BlockType, blockDefinition } from "../../shared/definitions/tiles.js"; |
| 4 | import { CraftingStationId, CRAFT_DURATION_MS } from "../../shared/definitions/craftingStations.js"; |
| 5 | import { ItemId } from "../../shared/definitions/items.js"; |
| 6 | import { SkillId } from "../../shared/definitions/skills.js"; |
| 7 | import { MaterialTier, tierSmeltSkillLevel } from "../../shared/definitions/materialTiers.js"; |
| 8 | import { recipesForStation, recipeDefinition } from "../../shared/crafting/stationRecipes.js"; |
| 9 | import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js"; |
| 10 | import { totalXpForLevel } from "../../shared/skills/progression.js"; |
| 11 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 12 | import { Component } from "../src/ecs/components.js"; |
| 13 | |
| 14 | function countItem (slots, itemId) |
| 15 | { |
| 16 | return (slots ?? []).reduce((sum, slot) => |
| 17 | { |
| 18 | if (slot?.itemId === itemId) |
| 19 | { return sum + (slot.quantity | 0); } |
| 20 | return sum; |
| 21 | }, 0); |
| 22 | } |
| 23 | |
| 24 | function placeFurnace (worker, tileX, tileY) |
| 25 | { |
| 26 | const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); |
| 27 | const localX = tileX % 16; |
| 28 | const localY = tileY % 16; |
| 29 | data.blocks[localY * 16 + localX] = { |
| 30 | typeId: BlockType.furnace, |
| 31 | state: "furnace", |
| 32 | config: null |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | function setSkillLevel (worker, playerId, skillId, level) |
| 37 | { |
| 38 | const handle = worker.world.ecs.handleOf(playerId); |
| 39 | const skills = worker.world.ecs.get(handle, Component.skills); |
| 40 | skills.xp[skillId] = totalXpForLevel(level); |
| 41 | } |
| 42 | |
| 43 | function giveInventory (harness, playerId, items) |
| 44 | { |
| 45 | const inventory = emptySlots(); |
| 46 | items.forEach((stack, index) => |
| 47 | { |
| 48 | inventory[index] = { itemId: stack.itemId, quantity: stack.quantity }; |
| 49 | }); |
| 50 | harness.simulation.applyBag(playerId, { |
| 51 | inventory, |
| 52 | equipment: emptyEquipment(), |
| 53 | vault: [], |
| 54 | vaultReserved: [] |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | function advanceMs (harness, ms) |
| 59 | { |
| 60 | const ticks = Math.ceil(ms / 50) + 3; |
| 61 | for (let i = 0; i < ticks; i++) |
| 62 | { |
| 63 | harness.advance(50); |
| 64 | harness.simulation.step(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | test("furnace block and steel recipe exist", () => |
| 69 | { |
| 70 | assert.equal(blockDefinition(BlockType.furnace)?.key, "furnace"); |
| 71 | const recipes = recipesForStation(CraftingStationId.furnace); |
| 72 | const steel = recipes.find((recipe) => recipe.recipeId === "smelt-steel"); |
| 73 | assert.ok(steel); |
| 74 | assert.deepEqual(steel.inputs.map((row) => row.itemId).sort(), [ |
| 75 | ItemId.coal, |
| 76 | ItemId.ironOre |
| 77 | ].sort()); |
| 78 | assert.equal(steel.output.itemId, ItemId.steelBar); |
| 79 | assert.equal(recipeDefinition("smelt-bronze")?.minSkillLevel, tierSmeltSkillLevel(MaterialTier.bronze)); |
| 80 | }); |
| 81 | |
| 82 | test("open furnace near station emits shove and station-open", () => |
| 83 | { |
| 84 | const harness = makeSimulation({ workerCount: 1 }); |
| 85 | try |
| 86 | { |
| 87 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 88 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 89 | const record = harness.simulation.entityRecord(playerId); |
| 90 | const worker = harness.simulation.workers[record.workerId]; |
| 91 | placeFurnace(worker, 9, 8); |
| 92 | |
| 93 | harness.simulation.enqueue(1, { |
| 94 | type: "open-station", |
| 95 | characterId: character.character_id, |
| 96 | stationId: CraftingStationId.furnace |
| 97 | }); |
| 98 | const result = harness.simulation.step(); |
| 99 | assert.ok(result.events.some((event) => event.type === "tile-shove")); |
| 100 | assert.ok(result.events.some((event) => |
| 101 | event.type === "station-open" && event.stationId === CraftingStationId.furnace)); |
| 102 | } |
| 103 | finally |
| 104 | { harness.close(); } |
| 105 | }); |
| 106 | |
| 107 | test("smelting bronze consumes ore, grants bar and Smithing XP", () => |
| 108 | { |
| 109 | const harness = makeSimulation({ workerCount: 1 }); |
| 110 | try |
| 111 | { |
| 112 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 113 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 114 | const record = harness.simulation.entityRecord(playerId); |
| 115 | const worker = harness.simulation.workers[record.workerId]; |
| 116 | placeFurnace(worker, 9, 8); |
| 117 | giveInventory(harness, playerId, [{ itemId: ItemId.bronzeOre, quantity: 1 }]); |
| 118 | |
| 119 | harness.simulation.enqueue(1, { |
| 120 | type: "start-craft", |
| 121 | characterId: character.character_id, |
| 122 | stationId: CraftingStationId.furnace, |
| 123 | recipeId: "smelt-bronze" |
| 124 | }); |
| 125 | const start = harness.simulation.step(); |
| 126 | assert.ok(start.events.some((event) => event.type === "tile-shove")); |
| 127 | assert.ok(start.events.some((event) => |
| 128 | event.type === "item-bubble" && event.itemId === ItemId.bronzeBar)); |
| 129 | |
| 130 | const smithingBefore = harness.stateDb.getSkills(character.character_id).xp.smithing ?? 0; |
| 131 | advanceMs(harness, CRAFT_DURATION_MS); |
| 132 | |
| 133 | const bag = harness.simulation.bagOf(playerId); |
| 134 | assert.equal(countItem(bag.inventory, ItemId.bronzeOre), 0); |
| 135 | assert.equal(countItem(bag.inventory, ItemId.bronzeBar), 1); |
| 136 | const smithingAfter = harness.stateDb.getSkills(character.character_id).xp.smithing ?? 0; |
| 137 | assert.ok(smithingAfter > smithingBefore); |
| 138 | } |
| 139 | finally |
| 140 | { harness.close(); } |
| 141 | }); |
| 142 | |
| 143 | test("smelting steel requires iron ore and coal", () => |
| 144 | { |
| 145 | const harness = makeSimulation({ workerCount: 1 }); |
| 146 | try |
| 147 | { |
| 148 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 149 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 150 | const record = harness.simulation.entityRecord(playerId); |
| 151 | const worker = harness.simulation.workers[record.workerId]; |
| 152 | placeFurnace(worker, 9, 8); |
| 153 | setSkillLevel(worker, playerId, SkillId.smithing, tierSmeltSkillLevel(MaterialTier.steel)); |
| 154 | giveInventory(harness, playerId, [ |
| 155 | { itemId: ItemId.ironOre, quantity: 1 }, |
| 156 | { itemId: ItemId.coal, quantity: 1 } |
| 157 | ]); |
| 158 | |
| 159 | harness.simulation.enqueue(1, { |
| 160 | type: "start-craft", |
| 161 | characterId: character.character_id, |
| 162 | stationId: CraftingStationId.furnace, |
| 163 | recipeId: "smelt-steel" |
| 164 | }); |
| 165 | harness.simulation.step(); |
| 166 | advanceMs(harness, CRAFT_DURATION_MS); |
| 167 | |
| 168 | const bag = harness.simulation.bagOf(playerId); |
| 169 | assert.equal(countItem(bag.inventory, ItemId.ironOre), 0); |
| 170 | assert.equal(countItem(bag.inventory, ItemId.coal), 0); |
| 171 | assert.equal(countItem(bag.inventory, ItemId.steelBar), 1); |
| 172 | } |
| 173 | finally |
| 174 | { harness.close(); } |
| 175 | }); |
| 176 | |
| 177 | test("craft rejects missing materials", () => |
| 178 | { |
| 179 | const harness = makeSimulation({ workerCount: 1 }); |
| 180 | try |
| 181 | { |
| 182 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 183 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 184 | const record = harness.simulation.entityRecord(playerId); |
| 185 | const worker = harness.simulation.workers[record.workerId]; |
| 186 | placeFurnace(worker, 9, 8); |
| 187 | giveInventory(harness, playerId, []); |
| 188 | |
| 189 | harness.simulation.enqueue(1, { |
| 190 | type: "start-craft", |
| 191 | characterId: character.character_id, |
| 192 | stationId: CraftingStationId.furnace, |
| 193 | recipeId: "smelt-bronze" |
| 194 | }); |
| 195 | const result = harness.simulation.step(); |
| 196 | assert.ok(result.events.some((event) => |
| 197 | event.type === "command-error" && /missing|materials/i.test(event.message ?? ""))); |
| 198 | } |
| 199 | finally |
| 200 | { harness.close(); } |
| 201 | }); |
| 202 | |
| 203 | test("craft rejects low Smithing for iron bars", () => |
| 204 | { |
| 205 | const harness = makeSimulation({ workerCount: 1 }); |
| 206 | try |
| 207 | { |
| 208 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 209 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 210 | const record = harness.simulation.entityRecord(playerId); |
| 211 | const worker = harness.simulation.workers[record.workerId]; |
| 212 | placeFurnace(worker, 9, 8); |
| 213 | giveInventory(harness, playerId, [{ itemId: ItemId.ironOre, quantity: 1 }]); |
| 214 | |
| 215 | harness.simulation.enqueue(1, { |
| 216 | type: "start-craft", |
| 217 | characterId: character.character_id, |
| 218 | stationId: CraftingStationId.furnace, |
| 219 | recipeId: "smelt-iron" |
| 220 | }); |
| 221 | const result = harness.simulation.step(); |
| 222 | assert.ok(result.events.some((event) => |
| 223 | event.type === "command-error" && /smithing|requires/i.test(event.message ?? ""))); |
| 224 | } |
| 225 | finally |
| 226 | { harness.close(); } |
| 227 | }); |
| 228 | |
| 229 | test("craft rejects furnace recipes when not next to a furnace", () => |
| 230 | { |
| 231 | const harness = makeSimulation({ workerCount: 1 }); |
| 232 | try |
| 233 | { |
| 234 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 235 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 236 | giveInventory(harness, playerId, [{ itemId: ItemId.bronzeOre, quantity: 1 }]); |
| 237 | |
| 238 | harness.simulation.enqueue(1, { |
| 239 | type: "start-craft", |
| 240 | characterId: character.character_id, |
| 241 | stationId: CraftingStationId.furnace, |
| 242 | recipeId: "smelt-bronze" |
| 243 | }); |
| 244 | const result = harness.simulation.step(); |
| 245 | assert.ok(result.events.some((event) => |
| 246 | event.type === "command-error" && /station|next to/i.test(event.message ?? ""))); |
| 247 | } |
| 248 | finally |
| 249 | { harness.close(); } |
| 250 | }); |