BlueGit

timfa questward

Files

questward / server / tests / crafting.test.js

crafting.test.js

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