BlueGit

timfa questward

Files

questward / server / tests / crafting.test.js

crafting.test.js

crafting.test.js 17.7 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { BlockType, blockDefinition } from "../../shared/definitions/tiles.js";
4import { CraftingStationId, CRAFT_DURATION_MS } from "../../shared/definitions/craftingStations.js";
5import { ItemId } from "../../shared/definitions/items.js";
6import { ArmourItemId } from "../../shared/definitions/armourItems.js";
7import { SkillId } from "../../shared/definitions/skills.js";
8import
9{
10 MaterialTier,
11 GatherMaterialId,
12 tierSmeltSkillLevel,
13 smeltingDurationMs,
14 associatedBarTier
15} from "../../shared/definitions/materialTiers.js";
16import { craftDurationMs } from "../../shared/definitions/gathering.js";
17import { recipesForStation, recipeDefinition } from "../../shared/crafting/stationRecipes.js";
18import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";
19import { totalXpForLevel } from "../../shared/skills/progression.js";
20import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
21import { Component } from "../src/ecs/components.js";
22
23function countItem (slots, itemId)
24{
25 return (slots ?? []).reduce((sum, slot) =>
26 {
27 if (slot?.itemId === itemId)
28 { return sum + (slot.quantity | 0); }
29 return sum;
30 }, 0);
31}
32
33function placeFurnace (worker, tileX, tileY)
34{
35 const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16));
36 const localX = tileX % 16;
37 const localY = tileY % 16;
38 data.blocks[localY * 16 + localX] = {
39 typeId: BlockType.furnace,
40 state: "furnace",
41 config: null
42 };
43}
44
45function setSkillLevel (worker, playerId, skillId, level)
46{
47 const handle = worker.world.ecs.handleOf(playerId);
48 const skills = worker.world.ecs.get(handle, Component.skills);
49 skills.xp[skillId] = totalXpForLevel(level);
50}
51
52function giveInventory (harness, playerId, items)
53{
54 const inventory = emptySlots();
55 items.forEach((stack, index) =>
56 {
57 inventory[index] = { itemId: stack.itemId, quantity: stack.quantity };
58 });
59 harness.simulation.applyBag(playerId, {
60 inventory,
61 equipment: emptyEquipment(),
62 vault: [],
63 vaultReserved: []
64 });
65}
66
67function advanceMs (harness, ms)
68{
69 const ticks = Math.ceil(ms / 50) + 3;
70 for (let i = 0; i < ticks; i++)
71 {
72 harness.advance(50);
73 harness.simulation.step();
74 }
75}
76
77test("furnace recipes: bronze is tin+copper; steel is iron+coal; high tiers need coal", () =>
78{
79 assert.equal(blockDefinition(BlockType.furnace)?.key, "furnace");
80 const bronze = recipeDefinition("smelt-bronze");
81 assert.deepEqual(bronze.inputs.map((row) => row.itemId).sort(), [
82 ItemId.copperOre,
83 ItemId.tinOre
84 ].sort());
85 assert.equal(bronze.output.itemId, ItemId.bronzeBar);
86 assert.equal(bronze.minSkillLevel, tierSmeltSkillLevel(MaterialTier.bronze));
87
88 const steel = recipesForStation(CraftingStationId.furnace)
89 .find((recipe) => recipe.recipeId === "smelt-steel");
90 assert.ok(steel);
91 assert.deepEqual(steel.inputs.map((row) => row.itemId).sort(), [
92 ItemId.coal,
93 ItemId.ironOre
94 ].sort());
95 assert.equal(steel.output.itemId, ItemId.steelBar);
96
97 const silver = recipesForStation(CraftingStationId.furnace)
98 .find((recipe) => recipe.recipeId === "smelt-silver");
99 assert.ok(silver);
100 assert.deepEqual(silver.inputs.map((row) => row.itemId), [ItemId.silverOre]);
101 assert.equal(silver.output.itemId, ItemId.silverBar);
102 assert.equal(silver.minSkillLevel, tierSmeltSkillLevel(GatherMaterialId.silver));
103
104 const gold = recipesForStation(CraftingStationId.furnace)
105 .find((recipe) => recipe.recipeId === "smelt-gold");
106 assert.ok(gold);
107 assert.deepEqual(gold.inputs.map((row) => row.itemId), [ItemId.goldOre]);
108 assert.equal(gold.output.itemId, ItemId.goldBar);
109 assert.equal(gold.minSkillLevel, tierSmeltSkillLevel(GatherMaterialId.gold));
110
111 const electrum = recipesForStation(CraftingStationId.furnace)
112 .find((recipe) => recipe.recipeId === "smelt-electrum");
113 assert.ok(electrum);
114 assert.deepEqual(electrum.inputs.map((row) => row.itemId).sort(), [
115 ItemId.coal,
116 ItemId.goldOre,
117 ItemId.silverOre
118 ].sort());
119 assert.equal(electrum.output.itemId, ItemId.electrumBar);
120 assert.equal(electrum.minSkillLevel, tierSmeltSkillLevel(GatherMaterialId.electrum));
121
122 const adamantium = recipeDefinition("smelt-adamantium");
123 assert.deepEqual(adamantium.inputs, [
124 { itemId: ItemId.adamantiumOre, quantity: 1 },
125 { itemId: ItemId.coal, quantity: 1 }
126 ]);
127 const orichalcum = recipeDefinition("smelt-orichalcum");
128 assert.deepEqual(orichalcum.inputs, [
129 { itemId: ItemId.orichalcumOre, quantity: 1 },
130 { itemId: ItemId.coal, quantity: 3 }
131 ]);
132 const dragon = recipeDefinition("smelt-dragon");
133 assert.deepEqual(dragon.inputs, [
134 { itemId: ItemId.dragonOre, quantity: 1 },
135 { itemId: ItemId.coal, quantity: 5 }
136 ]);
137});
138
139test("tin copper coal map to associated bar tiers for smelt duration", () =>
140{
141 assert.equal(associatedBarTier(GatherMaterialId.tin), MaterialTier.bronze);
142 assert.equal(associatedBarTier(GatherMaterialId.copper), MaterialTier.bronze);
143 assert.equal(associatedBarTier(GatherMaterialId.coal), MaterialTier.steel);
144 assert.equal(associatedBarTier(GatherMaterialId.silver), GatherMaterialId.silver);
145 assert.equal(associatedBarTier(GatherMaterialId.gold), GatherMaterialId.gold);
146 assert.equal(smeltingDurationMs(MaterialTier.bronze), 2000);
147 assert.equal(smeltingDurationMs(GatherMaterialId.tin), 2000);
148 assert.equal(smeltingDurationMs(MaterialTier.steel), 6000);
149 assert.equal(smeltingDurationMs(GatherMaterialId.coal), 6000);
150 assert.equal(smeltingDurationMs(GatherMaterialId.silver), 4000);
151 assert.equal(smeltingDurationMs(GatherMaterialId.gold), 6000);
152 assert.equal(craftDurationMs(recipeDefinition("smelt-bronze"), CRAFT_DURATION_MS), 2000);
153 assert.equal(craftDurationMs(recipeDefinition("smelt-steel"), CRAFT_DURATION_MS), 6000);
154 assert.equal(craftDurationMs(recipeDefinition("smelt-silver"), CRAFT_DURATION_MS), 4000);
155 assert.equal(craftDurationMs(recipeDefinition("smelt-gold"), CRAFT_DURATION_MS), 6000);
156});
157
158test("open furnace near station emits shove and station-open", () =>
159{
160 const harness = makeSimulation({ workerCount: 1 });
161 try
162 {
163 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
164 const playerId = harness.simulation.spawnCharacter(1, character);
165 const record = harness.simulation.entityRecord(playerId);
166 const worker = harness.simulation.workers[record.workerId];
167 placeFurnace(worker, 9, 8);
168
169 harness.simulation.enqueue(1, {
170 type: "open-station",
171 characterId: character.character_id,
172 stationId: CraftingStationId.furnace
173 });
174 const result = harness.simulation.step();
175 assert.ok(result.events.some((event) => event.type === "tile-shove"));
176 assert.ok(result.events.some((event) =>
177 event.type === "station-open" && event.stationId === CraftingStationId.furnace));
178 }
179 finally
180 { harness.close(); }
181});
182
183test("open leatherworking table near station emits station-open", () =>
184{
185 const harness = makeSimulation({ workerCount: 1 });
186 try
187 {
188 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
189 const playerId = harness.simulation.spawnCharacter(1, character);
190 const record = harness.simulation.entityRecord(playerId);
191 const worker = harness.simulation.workers[record.workerId];
192 const data = worker.world.chunkData(Math.floor(9 / 16), Math.floor(8 / 16));
193 data.blocks[8 * 16 + 9] = {
194 typeId: BlockType.leatherworkingTable,
195 state: "leatherworking-table",
196 config: null
197 };
198
199 harness.simulation.enqueue(1, {
200 type: "open-station",
201 characterId: character.character_id,
202 stationId: CraftingStationId.leatherworkingStation
203 });
204 const result = harness.simulation.step();
205 assert.ok(result.events.some((event) =>
206 event.type === "station-open" && event.stationId === CraftingStationId.leatherworkingStation));
207 }
208 finally
209 { harness.close(); }
210});
211
212test("smelting bronze consumes tin and copper, grants bar and Smithing XP", () =>
213{
214 const harness = makeSimulation({ workerCount: 1 });
215 try
216 {
217 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
218 const playerId = harness.simulation.spawnCharacter(1, character);
219 const record = harness.simulation.entityRecord(playerId);
220 const worker = harness.simulation.workers[record.workerId];
221 placeFurnace(worker, 9, 8);
222 giveInventory(harness, playerId, [
223 { itemId: ItemId.tinOre, quantity: 1 },
224 { itemId: ItemId.copperOre, quantity: 1 }
225 ]);
226
227 harness.simulation.enqueue(1, {
228 type: "start-craft",
229 characterId: character.character_id,
230 stationId: CraftingStationId.furnace,
231 recipeId: "smelt-bronze"
232 });
233 const start = harness.simulation.step();
234 assert.ok(start.events.some((event) => event.type === "tile-shove"));
235 assert.ok(start.events.some((event) =>
236 event.type === "item-bubble" && event.itemId === ItemId.bronzeBar));
237
238 const smithingBefore = harness.stateDb.getSkills(character.character_id).xp.smithing ?? 0;
239 advanceMs(harness, craftDurationMs(recipeDefinition("smelt-bronze"), CRAFT_DURATION_MS));
240
241 const bag = harness.simulation.bagOf(playerId);
242 assert.equal(countItem(bag.inventory, ItemId.tinOre), 0);
243 assert.equal(countItem(bag.inventory, ItemId.copperOre), 0);
244 assert.equal(countItem(bag.inventory, ItemId.bronzeBar), 1);
245 const bar = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.bronzeBar);
246 assert.equal(bar?.origins?.[0]?.origin?.verb, "Forged");
247 assert.match(bar?.origins?.[0]?.origin?.displayName ?? "", /^Ada/);
248 const smithingAfter = harness.stateDb.getSkills(character.character_id).xp.smithing ?? 0;
249 assert.equal(smithingAfter - smithingBefore, 2);
250 }
251 finally
252 { harness.close(); }
253});
254
255test("smelting steel requires iron ore and coal", () =>
256{
257 const harness = makeSimulation({ workerCount: 1 });
258 try
259 {
260 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
261 const playerId = harness.simulation.spawnCharacter(1, character);
262 const record = harness.simulation.entityRecord(playerId);
263 const worker = harness.simulation.workers[record.workerId];
264 placeFurnace(worker, 9, 8);
265 setSkillLevel(worker, playerId, SkillId.smithing, tierSmeltSkillLevel(MaterialTier.steel));
266 giveInventory(harness, playerId, [
267 { itemId: ItemId.ironOre, quantity: 1 },
268 { itemId: ItemId.coal, quantity: 1 }
269 ]);
270
271 harness.simulation.enqueue(1, {
272 type: "start-craft",
273 characterId: character.character_id,
274 stationId: CraftingStationId.furnace,
275 recipeId: "smelt-steel"
276 });
277 harness.simulation.step();
278 advanceMs(harness, craftDurationMs(recipeDefinition("smelt-steel"), CRAFT_DURATION_MS));
279
280 const bag = harness.simulation.bagOf(playerId);
281 assert.equal(countItem(bag.inventory, ItemId.ironOre), 0);
282 assert.equal(countItem(bag.inventory, ItemId.coal), 0);
283 assert.equal(countItem(bag.inventory, ItemId.steelBar), 1);
284 }
285 finally
286 { harness.close(); }
287});
288
289test("craft rejects missing materials", () =>
290{
291 const harness = makeSimulation({ workerCount: 1 });
292 try
293 {
294 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
295 const playerId = harness.simulation.spawnCharacter(1, character);
296 const record = harness.simulation.entityRecord(playerId);
297 const worker = harness.simulation.workers[record.workerId];
298 placeFurnace(worker, 9, 8);
299 giveInventory(harness, playerId, [{ itemId: ItemId.copperOre, quantity: 1 }]);
300
301 harness.simulation.enqueue(1, {
302 type: "start-craft",
303 characterId: character.character_id,
304 stationId: CraftingStationId.furnace,
305 recipeId: "smelt-bronze"
306 });
307 const result = harness.simulation.step();
308 assert.ok(result.events.some((event) =>
309 event.type === "command-error" && /missing|materials/i.test(event.message ?? "")));
310 }
311 finally
312 { harness.close(); }
313});
314
315test("craft rejects low Smithing for iron bars", () =>
316{
317 const harness = makeSimulation({ workerCount: 1 });
318 try
319 {
320 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
321 const playerId = harness.simulation.spawnCharacter(1, character);
322 const record = harness.simulation.entityRecord(playerId);
323 const worker = harness.simulation.workers[record.workerId];
324 placeFurnace(worker, 9, 8);
325 giveInventory(harness, playerId, [{ itemId: ItemId.ironOre, quantity: 1 }]);
326
327 harness.simulation.enqueue(1, {
328 type: "start-craft",
329 characterId: character.character_id,
330 stationId: CraftingStationId.furnace,
331 recipeId: "smelt-iron"
332 });
333 const result = harness.simulation.step();
334 assert.ok(result.events.some((event) =>
335 event.type === "command-error" && /smithing|requires/i.test(event.message ?? "")));
336 }
337 finally
338 { harness.close(); }
339});
340
341test("craft rejects furnace recipes when not next to a furnace", () =>
342{
343 const harness = makeSimulation({ workerCount: 1 });
344 try
345 {
346 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
347 const playerId = harness.simulation.spawnCharacter(1, character);
348 giveInventory(harness, playerId, [
349 { itemId: ItemId.tinOre, quantity: 1 },
350 { itemId: ItemId.copperOre, quantity: 1 }
351 ]);
352
353 harness.simulation.enqueue(1, {
354 type: "start-craft",
355 characterId: character.character_id,
356 stationId: CraftingStationId.furnace,
357 recipeId: "smelt-bronze"
358 });
359 const result = harness.simulation.step();
360 assert.ok(result.events.some((event) =>
361 event.type === "command-error" && /station|next to/i.test(event.message ?? "")));
362 }
363 finally
364 { harness.close(); }
365});
366
367function placeStation (worker, tileX, tileY, typeId, state)
368{
369 const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16));
370 const localX = tileX % 16;
371 const localY = tileY % 16;
372 data.blocks[localY * 16 + localX] = {
373 typeId,
374 state,
375 config: null
376 };
377}
378
379test("bronze pickaxe chain: pine log → handle, bar → head, assemble pickaxe", () =>
380{
381 const harness = makeSimulation({ workerCount: 1 });
382 try
383 {
384 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
385 const playerId = harness.simulation.spawnCharacter(1, character);
386 const record = harness.simulation.entityRecord(playerId);
387 const worker = harness.simulation.workers[record.workerId];
388 placeStation(worker, 9, 8, BlockType.woodworkingBench, "woodworking-bench");
389 placeStation(worker, 9, 9, BlockType.anvil, "anvil");
390 placeStation(worker, 8, 9, BlockType.craftingBench, "crafting-bench");
391 giveInventory(harness, playerId, [
392 { itemId: ItemId.pineLog, quantity: 1 },
393 { itemId: ItemId.bronzeBar, quantity: 1 }
394 ]);
395
396 harness.simulation.enqueue(1, {
397 type: "start-craft",
398 characterId: character.character_id,
399 stationId: CraftingStationId.woodworkingBench,
400 recipeId: "handle-pine"
401 });
402 advanceMs(harness, craftDurationMs(recipeDefinition("handle-pine"), CRAFT_DURATION_MS));
403 let bag = harness.simulation.bagOf(playerId);
404 assert.equal(countItem(bag.inventory, ItemId.pineHandle), 1);
405 assert.equal(countItem(bag.inventory, ItemId.pineLog), 0);
406 const handle = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.pineHandle);
407 assert.equal(handle?.origins?.[0]?.origin?.verb, "Carved");
408
409 harness.simulation.enqueue(1, {
410 type: "start-craft",
411 characterId: character.character_id,
412 stationId: CraftingStationId.anvil,
413 recipeId: "pickaxe-head-bronze"
414 });
415 advanceMs(harness, craftDurationMs(recipeDefinition("pickaxe-head-bronze"), CRAFT_DURATION_MS));
416 bag = harness.simulation.bagOf(playerId);
417 assert.equal(countItem(bag.inventory, ItemId.bronzePickaxeHead), 1);
418 assert.equal(countItem(bag.inventory, ItemId.bronzeBar), 0);
419 const head = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.bronzePickaxeHead);
420 assert.equal(head?.origins?.[0]?.origin?.verb, "Smithed");
421
422 harness.simulation.enqueue(1, {
423 type: "start-craft",
424 characterId: character.character_id,
425 stationId: CraftingStationId.craftingBench,
426 recipeId: "pickaxe-bronze"
427 });
428 advanceMs(harness, craftDurationMs(recipeDefinition("pickaxe-bronze"), CRAFT_DURATION_MS));
429 bag = harness.simulation.bagOf(playerId);
430 assert.equal(countItem(bag.inventory, ItemId.bronzePickaxe), 1);
431 assert.equal(countItem(bag.inventory, ItemId.pineHandle), 0);
432 assert.equal(countItem(bag.inventory, ItemId.bronzePickaxeHead), 0);
433 const pickaxe = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.bronzePickaxe);
434 assert.equal(pickaxe?.origins?.[0]?.origin?.verb, "Assembled");
435 assert.match(pickaxe?.origins?.[0]?.origin?.displayName ?? "", /^Ada/);
436 }
437 finally
438 { harness.close(); }
439});
440
441test("anvil armour recipes use bar counts per piece", () =>
442{
443 const chest = recipeDefinition("armour-chestplate-bronze");
444 assert.ok(chest);
445 assert.equal(chest.stationId, CraftingStationId.anvil);
446 assert.equal(chest.output.itemId, ArmourItemId.bronzeChestplate);
447 assert.deepEqual(chest.inputs, [{ itemId: ItemId.bronzeBar, quantity: 6 }]);
448
449 const greaves = recipeDefinition("armour-greaves-iron");
450 assert.deepEqual(greaves.inputs, [{ itemId: ItemId.ironBar, quantity: 4 }]);
451
452 const skirt = recipeDefinition("armour-skirt-iron");
453 assert.ok(skirt);
454 assert.equal(skirt.output.itemId, ArmourItemId.ironSkirt);
455 assert.deepEqual(skirt.inputs, [{ itemId: ItemId.ironBar, quantity: 4 }]);
456
457 const boots = recipeDefinition("armour-boots-steel");
458 assert.deepEqual(boots.inputs, [{ itemId: ItemId.steelBar, quantity: 2 }]);
459
460 const helmet = recipeDefinition("armour-helmet-mithril");
461 assert.deepEqual(helmet.inputs, [{ itemId: ItemId.mithrilBar, quantity: 2 }]);
462
463 const fullHelm = recipeDefinition("armour-full-helm-adamantium");
464 assert.deepEqual(fullHelm.inputs, [{ itemId: ItemId.adamantiumBar, quantity: 3 }]);
465
466 const horned = recipeDefinition("armour-horned-helm-orichalcum");
467 assert.deepEqual(horned.inputs, [
468 { itemId: ItemId.orichalcumBar, quantity: 2 },
469 { itemId: ItemId.bones, quantity: 1 }
470 ]);
471
472 const shield = recipeDefinition("armour-shield-iron");
473 assert.ok(shield);
474 assert.equal(shield.output.itemId, ArmourItemId.ironShield);
475 assert.deepEqual(shield.inputs, [
476 { itemId: ItemId.ironBar, quantity: 4 },
477 { itemId: ItemId.hardLeather, quantity: 1 }
478 ]);
479
480 const anvilArmour = recipesForStation(CraftingStationId.anvil)
481 .filter((recipe) => recipe.recipeId.startsWith("armour-"));
482 assert.equal(anvilArmour.length, 7 * 8);
483});