BlueGit

timfa questward

Files

questward / server / tests / tanning-ingredients.test.js

tanning-ingredients.test.js

0d794626 History
tanning-ingredients.test.js 12.4 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { BlockType } from "../../shared/definitions/tiles.js";
4import { ItemId, itemDefinition, itemDisplayName, itemSpriteNameForStack } from "../../shared/definitions/items.js";
5import { CraftingItemId } from "../../shared/definitions/craftingItems.js";
6import {
7 WoodItemId,
8 debarkedLogInstance,
9 isLogDebarked,
10 barkItemIdForWood
11} from "../../shared/definitions/woodItems.js";
12import { WoodTier, GatherMaterialId, MaterialTier, oreBoulderState } from "../../shared/definitions/materialTiers.js";
13import { SkillId } from "../../shared/definitions/skills.js";
14import { LeatherItemId } from "../../shared/definitions/leatherItems.js";
15import { AlchemyStepKind } from "../../shared/definitions/alchemy.js";
16import { canMineMaterial, oreItemIdForMaterial, woodworkingXpForDebark } from "../../shared/definitions/gathering.js";
17import {
18 clearResolvedAlchemyPotionsCache,
19 resolvedAlchemyPotions,
20 evaluateAlchemyBottle,
21 potionItemInstance
22} from "../../shared/alchemy/recipes.js";
23import { StatusEffectType } from "../../shared/statusEffects/rules.js";
24import { isFullPotionBottle } from "../../shared/items/charges.js";
25import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";
26import { totalXpForLevel } from "../../shared/skills/progression.js";
27import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
28import { addGroundItem } from "../src/items/bags.js";
29import { Component } from "../src/ecs/components.js";
30import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
31
32function placeCauldron (worldDb, tileX, tileY)
33{
34 const chunkX = Math.floor(tileX / 16);
35 const chunkY = Math.floor(tileY / 16);
36 const data = createEmptyChunkData();
37 data.open = true;
38 const localX = tileX - chunkX * 16;
39 const localY = tileY - chunkY * 16;
40 data.blocks[localY * 16 + localX] = { typeId: BlockType.cauldron, state: 0 };
41 worldDb.saveChunkVersion(
42 chunkX,
43 chunkY,
44 worldDb.getActiveRevisionId(),
45 true,
46 encodeChunkBlob(data)
47 );
48}
49
50function countItem (slots, itemId)
51{
52 return (slots ?? [])
53 .filter((slot) => slot?.itemId === itemId)
54 .reduce((sum, slot) => sum + (slot.quantity | 0), 0);
55}
56
57function setSkillLevel (harness, characterId, skillId, level)
58{
59 const skills = harness.stateDb.getSkills(characterId);
60 skills.xp[skillId] = totalXpForLevel(level);
61 harness.stateDb.saveSkills(characterId, skills);
62}
63
64test("rock salt mines with bronze pick and uses salt-ore boulder state", () =>
65{
66 assert.equal(oreBoulderState(GatherMaterialId.salt), "salt-ore");
67 assert.equal(oreItemIdForMaterial(GatherMaterialId.salt), CraftingItemId.salt);
68 assert.equal(canMineMaterial(1, MaterialTier.bronze, GatherMaterialId.salt).ok, true);
69 const def = itemDefinition(CraftingItemId.salt);
70 assert.equal(def.key, "rock-salt");
71 assert.equal(def.iconKey, "ash");
72 assert.equal(def.tier, GatherMaterialId.salt);
73});
74
75test("debarked logs keep oak itemId but change name and sprite", () =>
76{
77 const plain = { itemId: WoodItemId.oakLog, quantity: 1 };
78 const stripped = { itemId: WoodItemId.oakLog, quantity: 1, instance: debarkedLogInstance() };
79 assert.equal(isLogDebarked(plain), false);
80 assert.equal(isLogDebarked(stripped), true);
81 assert.equal(itemDisplayName(plain), "Oak log");
82 assert.equal(itemDisplayName(stripped), "Debarked Oak log");
83 assert.match(itemSpriteNameForStack(stripped), /log-debarked/);
84 assert.equal(barkItemIdForWood(WoodTier.oak), WoodItemId.oakBark);
85});
86
87test("debark Crafting XP is half wood tier rank rounded up", () =>
88{
89 assert.equal(woodworkingXpForDebark(WoodTier.pine), 1);
90 assert.equal(woodworkingXpForDebark(WoodTier.birch), 1);
91 assert.equal(woodworkingXpForDebark(WoodTier.oak), 2);
92 assert.equal(woodworkingXpForDebark(WoodTier.elder), 4);
93});
94
95test("debark-log on ground oak log yields bark and debarked flag", () =>
96{
97 const harness = makeSimulation({ workerCount: 1 });
98 try
99 {
100 harness.simulation.reloadWorld();
101 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
102 setSkillLevel(harness, character.character_id, SkillId.woodcutting, 20);
103 setSkillLevel(harness, character.character_id, SkillId.crafting, 1);
104 const inventory = emptySlots();
105 inventory[0] = { itemId: ItemId.ironAxe, quantity: 1 };
106 harness.stateDb.saveBag(character.character_id, {
107 inventory,
108 equipment: emptyEquipment(),
109 vault: [],
110 vaultReserved: []
111 });
112 const entityId = harness.simulation.spawnCharacter(1, character);
113 const record = harness.simulation.entityRecord(entityId);
114 const worker = harness.simulation.workers[record.workerId];
115 const handle = worker.world.ecs.handleOf(entityId);
116 const craftingXpBefore = worker.world.ecs.get(handle, Component.skills).xp[SkillId.crafting] | 0;
117
118 const groundItemId = (worker.world.nextGroundItemId = (worker.world.nextGroundItemId ?? 1) + 1);
119 addGroundItem(worker.world, {
120 groundItemId,
121 tileX: 8,
122 tileY: 9,
123 stack: { itemId: WoodItemId.oakLog, quantity: 1 },
124 droppedAt: Date.now(),
125 expiresAt: Date.now() + 600_000,
126 source: "test"
127 });
128
129 harness.simulation.enqueue(1, {
130 type: "debark-log",
131 characterId: character.character_id,
132 groundItemId
133 });
134 harness.simulation.step();
135
136 const gather = worker.world.ecs.get(handle, Component.gather);
137 assert.equal(gather?.kind, "debark");
138 assert.equal(gather?.durationMs, 2000);
139
140 for (let i = 0; i < 50; i++)
141 {
142 harness.advance(50);
143 harness.simulation.step();
144 }
145
146 const item = worker.world.groundItems.get(groundItemId);
147 assert.ok(item);
148 assert.equal(isLogDebarked(item.stack), true);
149 assert.equal(item.stack.itemId, WoodItemId.oakLog);
150
151 const bark = [...worker.world.groundItems.values()]
152 .find((row) => row.stack?.itemId === WoodItemId.oakBark);
153 assert.ok(bark);
154 assert.equal(bark.stack.quantity, 1);
155 assert.equal(worker.world.ecs.has(handle, Component.gather), false);
156
157 const craftingXpAfter = worker.world.ecs.get(handle, Component.skills).xp[SkillId.crafting] | 0;
158 assert.equal(craftingXpAfter - craftingXpBefore, woodworkingXpForDebark(WoodTier.oak));
159 }
160 finally
161 { harness.close(); }
162});
163
164test("debark-log on a ground log stack strips only one log", () =>
165{
166 const harness = makeSimulation({ workerCount: 1 });
167 try
168 {
169 harness.simulation.reloadWorld();
170 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
171 setSkillLevel(harness, character.character_id, SkillId.woodcutting, 20);
172 const inventory = emptySlots();
173 inventory[0] = { itemId: ItemId.ironAxe, quantity: 1 };
174 harness.stateDb.saveBag(character.character_id, {
175 inventory,
176 equipment: emptyEquipment(),
177 vault: [],
178 vaultReserved: []
179 });
180 const entityId = harness.simulation.spawnCharacter(1, character);
181 const record = harness.simulation.entityRecord(entityId);
182 const worker = harness.simulation.workers[record.workerId];
183
184 const groundItemId = (worker.world.nextGroundItemId = (worker.world.nextGroundItemId ?? 1) + 1);
185 addGroundItem(worker.world, {
186 groundItemId,
187 tileX: 8,
188 tileY: 9,
189 stack: { itemId: WoodItemId.oakLog, quantity: 4 },
190 droppedAt: Date.now(),
191 expiresAt: Date.now() + 600_000,
192 source: "test"
193 });
194
195 harness.simulation.enqueue(1, {
196 type: "debark-log",
197 characterId: character.character_id,
198 groundItemId
199 });
200 harness.simulation.step();
201
202 for (let i = 0; i < 50; i++)
203 {
204 harness.advance(50);
205 harness.simulation.step();
206 }
207
208 const remain = worker.world.groundItems.get(groundItemId);
209 assert.ok(remain);
210 assert.equal(remain.stack.quantity, 3);
211 assert.equal(isLogDebarked(remain.stack), false);
212
213 const debarked = [...worker.world.groundItems.values()]
214 .find((row) => row.groundItemId !== groundItemId
215 && row.stack?.itemId === WoodItemId.oakLog
216 && isLogDebarked(row.stack));
217 assert.ok(debarked);
218 assert.equal(debarked.stack.quantity, 1);
219
220 const bark = [...worker.world.groundItems.values()]
221 .find((row) => row.stack?.itemId === WoodItemId.oakBark);
222 assert.ok(bark);
223 assert.equal(bark.stack.quantity, 1);
224 }
225 finally
226 { harness.close(); }
227});
228
229test("animal fat and tanning brew alchemy recipes resolve", () =>
230{
231 clearResolvedAlchemyPotionsCache();
232 const potions = resolvedAlchemyPotions();
233 const fat = potions.find((row) => row.potionKey === "animalFat");
234 const tan = potions.find((row) => row.potionKey === "tanningBrew");
235 assert.ok(fat);
236 assert.equal(fat.effect?.type, StatusEffectType.foodPoisoning);
237 assert.ok(tan);
238 assert.equal(tan.outputItemId, LeatherItemId.tanningBrew);
239 assert.equal(tan.requiresEmptyBottles, false);
240
241 const fatEval = evaluateAlchemyBottle({
242 skills: { levels: { [SkillId.alchemy]: 1 } },
243 brewStacks: [{ itemId: CraftingItemId.rawMeat, quantity: 1 }],
244 takenSteps: fat.steps
245 });
246 assert.equal(fatEval.success, true);
247 assert.equal(fatEval.result.potionKey, "animalFat");
248
249 const fatBottle = potionItemInstance(fatEval.result);
250 assert.equal(isFullPotionBottle({ itemId: ItemId.potion, instance: fatBottle }), true);
251
252 const tanEval = evaluateAlchemyBottle({
253 skills: { levels: { [SkillId.alchemy]: 10 } },
254 brewStacks: [
255 { itemId: WoodItemId.oakBark, quantity: 2 },
256 { itemId: CraftingItemId.salt, quantity: 1 },
257 { itemId: ItemId.potion, quantity: 1, instance: fatBottle }
258 ],
259 takenSteps: tan.steps
260 });
261 assert.equal(tanEval.success, true);
262 assert.equal(tanEval.result.outputItemId, LeatherItemId.tanningBrew);
263});
264
265test("cauldron bottles animal fat then tanning brew returns the empty bottle", () =>
266{
267 const harness = makeSimulation({ workerCount: 1 });
268 try
269 {
270 placeCauldron(harness.worldDb, 8, 8);
271 harness.simulation.reloadWorld();
272 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
273 setSkillLevel(harness, character.character_id, SkillId.alchemy, 10);
274 const inventory = emptySlots();
275 inventory[0] = { itemId: CraftingItemId.rawMeat, quantity: 1 };
276 inventory[1] = { itemId: ItemId.glassBottle, quantity: 1 };
277 inventory[2] = { itemId: WoodItemId.oakBark, quantity: 2 };
278 inventory[3] = { itemId: CraftingItemId.salt, quantity: 1 };
279 harness.stateDb.saveBag(character.character_id, {
280 inventory,
281 equipment: emptyEquipment(),
282 vault: [],
283 vaultReserved: []
284 });
285 const entityId = harness.simulation.spawnCharacter(1, character);
286 const cid = character.character_id;
287
288 harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid });
289 harness.simulation.step();
290 harness.simulation.enqueue(1, {
291 type: "cauldron-insert",
292 characterId: cid,
293 itemId: CraftingItemId.rawMeat,
294 quantity: 1
295 });
296 for (const action of [AlchemyStepKind.heat, AlchemyStepKind.stir, AlchemyStepKind.cool])
297 {
298 harness.simulation.enqueue(1, {
299 type: "cauldron-action",
300 characterId: cid,
301 action
302 });
303 }
304 harness.simulation.enqueue(1, { type: "cauldron-bottle", characterId: cid });
305 harness.simulation.step();
306
307 let bag = harness.simulation.bagOf(entityId);
308 assert.equal(countItem(bag.inventory, CraftingItemId.rawMeat), 0);
309 assert.equal(countItem(bag.inventory, ItemId.potion), 1);
310 const fatSlot = bag.inventory.find((slot) => slot?.itemId === ItemId.potion);
311 assert.equal(fatSlot.instance.potionKey, "animalFat");
312 assert.equal(countItem(bag.inventory, ItemId.glassBottle), 0);
313
314 harness.simulation.enqueue(1, { type: "cauldron-clear", characterId: cid });
315 harness.simulation.step();
316 harness.simulation.enqueue(1, {
317 type: "cauldron-insert",
318 characterId: cid,
319 itemId: WoodItemId.oakBark,
320 quantity: 2
321 });
322 harness.simulation.enqueue(1, {
323 type: "cauldron-action",
324 characterId: cid,
325 action: AlchemyStepKind.heat
326 });
327 harness.simulation.enqueue(1, {
328 type: "cauldron-insert",
329 characterId: cid,
330 itemId: CraftingItemId.salt,
331 quantity: 1
332 });
333 harness.simulation.enqueue(1, {
334 type: "cauldron-action",
335 characterId: cid,
336 action: AlchemyStepKind.stir
337 });
338 harness.simulation.enqueue(1, {
339 type: "cauldron-insert",
340 characterId: cid,
341 itemId: ItemId.potion,
342 quantity: 1,
343 instance: structuredClone(fatSlot.instance)
344 });
345 harness.simulation.enqueue(1, {
346 type: "cauldron-action",
347 characterId: cid,
348 action: AlchemyStepKind.cool
349 });
350 harness.simulation.enqueue(1, { type: "cauldron-bottle", characterId: cid });
351 harness.simulation.step();
352
353 bag = harness.simulation.bagOf(entityId);
354 assert.equal(countItem(bag.inventory, LeatherItemId.tanningBrew), 1);
355 assert.equal(countItem(bag.inventory, ItemId.potion), 0);
356 assert.equal(countItem(bag.inventory, ItemId.glassBottle), 1);
357 assert.equal(countItem(bag.inventory, WoodItemId.oakBark), 0);
358 assert.equal(countItem(bag.inventory, CraftingItemId.salt), 0);
359 }
360 finally
361 { harness.close(); }
362});