BlueGit

timfa questward

Files

questward / server / tests / crafting.test.js

crafting.test.js

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