BlueGit

timfa questward

Files

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

tanning-ingredients.test.js

tanning-ingredients.test.js 11.7 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 } 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-log on ground oak log yields bark and debarked flag", () =>
88{
89 const harness = makeSimulation({ workerCount: 1 });
90 try
91 {
92 harness.simulation.reloadWorld();
93 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
94 setSkillLevel(harness, character.character_id, SkillId.woodcutting, 20);
95 const inventory = emptySlots();
96 inventory[0] = { itemId: ItemId.ironAxe, quantity: 1 };
97 harness.stateDb.saveBag(character.character_id, {
98 inventory,
99 equipment: emptyEquipment(),
100 vault: [],
101 vaultReserved: []
102 });
103 const entityId = harness.simulation.spawnCharacter(1, character);
104 const record = harness.simulation.entityRecord(entityId);
105 const worker = harness.simulation.workers[record.workerId];
106
107 const groundItemId = (worker.world.nextGroundItemId = (worker.world.nextGroundItemId ?? 1) + 1);
108 addGroundItem(worker.world, {
109 groundItemId,
110 tileX: 8,
111 tileY: 9,
112 stack: { itemId: WoodItemId.oakLog, quantity: 1 },
113 droppedAt: Date.now(),
114 expiresAt: Date.now() + 600_000,
115 source: "test"
116 });
117
118 harness.simulation.enqueue(1, {
119 type: "debark-log",
120 characterId: character.character_id,
121 groundItemId
122 });
123 harness.simulation.step();
124
125 const handle = worker.world.ecs.handleOf(entityId);
126 const gather = worker.world.ecs.get(handle, Component.gather);
127 assert.equal(gather?.kind, "debark");
128 assert.equal(gather?.durationMs, 2000);
129
130 for (let i = 0; i < 50; i++)
131 {
132 harness.advance(50);
133 harness.simulation.step();
134 }
135
136 const item = worker.world.groundItems.get(groundItemId);
137 assert.ok(item);
138 assert.equal(isLogDebarked(item.stack), true);
139 assert.equal(item.stack.itemId, WoodItemId.oakLog);
140
141 const bark = [...worker.world.groundItems.values()]
142 .find((row) => row.stack?.itemId === WoodItemId.oakBark);
143 assert.ok(bark);
144 assert.equal(bark.stack.quantity, 1);
145 assert.equal(worker.world.ecs.has(handle, Component.gather), false);
146 }
147 finally
148 { harness.close(); }
149});
150
151test("debark-log on a ground log stack strips only one log", () =>
152{
153 const harness = makeSimulation({ workerCount: 1 });
154 try
155 {
156 harness.simulation.reloadWorld();
157 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
158 setSkillLevel(harness, character.character_id, SkillId.woodcutting, 20);
159 const inventory = emptySlots();
160 inventory[0] = { itemId: ItemId.ironAxe, quantity: 1 };
161 harness.stateDb.saveBag(character.character_id, {
162 inventory,
163 equipment: emptyEquipment(),
164 vault: [],
165 vaultReserved: []
166 });
167 const entityId = harness.simulation.spawnCharacter(1, character);
168 const record = harness.simulation.entityRecord(entityId);
169 const worker = harness.simulation.workers[record.workerId];
170
171 const groundItemId = (worker.world.nextGroundItemId = (worker.world.nextGroundItemId ?? 1) + 1);
172 addGroundItem(worker.world, {
173 groundItemId,
174 tileX: 8,
175 tileY: 9,
176 stack: { itemId: WoodItemId.oakLog, quantity: 4 },
177 droppedAt: Date.now(),
178 expiresAt: Date.now() + 600_000,
179 source: "test"
180 });
181
182 harness.simulation.enqueue(1, {
183 type: "debark-log",
184 characterId: character.character_id,
185 groundItemId
186 });
187 harness.simulation.step();
188
189 for (let i = 0; i < 50; i++)
190 {
191 harness.advance(50);
192 harness.simulation.step();
193 }
194
195 const remain = worker.world.groundItems.get(groundItemId);
196 assert.ok(remain);
197 assert.equal(remain.stack.quantity, 3);
198 assert.equal(isLogDebarked(remain.stack), false);
199
200 const debarked = [...worker.world.groundItems.values()]
201 .find((row) => row.groundItemId !== groundItemId
202 && row.stack?.itemId === WoodItemId.oakLog
203 && isLogDebarked(row.stack));
204 assert.ok(debarked);
205 assert.equal(debarked.stack.quantity, 1);
206
207 const bark = [...worker.world.groundItems.values()]
208 .find((row) => row.stack?.itemId === WoodItemId.oakBark);
209 assert.ok(bark);
210 assert.equal(bark.stack.quantity, 1);
211 }
212 finally
213 { harness.close(); }
214});
215
216test("animal fat and tanning brew alchemy recipes resolve", () =>
217{
218 clearResolvedAlchemyPotionsCache();
219 const potions = resolvedAlchemyPotions();
220 const fat = potions.find((row) => row.potionKey === "animalFat");
221 const tan = potions.find((row) => row.potionKey === "tanningBrew");
222 assert.ok(fat);
223 assert.equal(fat.effect?.type, StatusEffectType.foodPoisoning);
224 assert.ok(tan);
225 assert.equal(tan.outputItemId, LeatherItemId.tanningBrew);
226 assert.equal(tan.requiresEmptyBottles, false);
227
228 const fatEval = evaluateAlchemyBottle({
229 skills: { levels: { [SkillId.alchemy]: 1 } },
230 brewStacks: [{ itemId: CraftingItemId.rawMeat, quantity: 1 }],
231 takenSteps: fat.steps
232 });
233 assert.equal(fatEval.success, true);
234 assert.equal(fatEval.result.potionKey, "animalFat");
235
236 const fatBottle = potionItemInstance(fatEval.result);
237 assert.equal(isFullPotionBottle({ itemId: ItemId.potion, instance: fatBottle }), true);
238
239 const tanEval = evaluateAlchemyBottle({
240 skills: { levels: { [SkillId.alchemy]: 10 } },
241 brewStacks: [
242 { itemId: WoodItemId.oakBark, quantity: 2 },
243 { itemId: CraftingItemId.salt, quantity: 1 },
244 { itemId: ItemId.potion, quantity: 1, instance: fatBottle }
245 ],
246 takenSteps: tan.steps
247 });
248 assert.equal(tanEval.success, true);
249 assert.equal(tanEval.result.outputItemId, LeatherItemId.tanningBrew);
250});
251
252test("cauldron bottles animal fat then tanning brew returns the empty bottle", () =>
253{
254 const harness = makeSimulation({ workerCount: 1 });
255 try
256 {
257 placeCauldron(harness.worldDb, 8, 8);
258 harness.simulation.reloadWorld();
259 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
260 setSkillLevel(harness, character.character_id, SkillId.alchemy, 10);
261 const inventory = emptySlots();
262 inventory[0] = { itemId: CraftingItemId.rawMeat, quantity: 1 };
263 inventory[1] = { itemId: ItemId.glassBottle, quantity: 1 };
264 inventory[2] = { itemId: WoodItemId.oakBark, quantity: 2 };
265 inventory[3] = { itemId: CraftingItemId.salt, quantity: 1 };
266 harness.stateDb.saveBag(character.character_id, {
267 inventory,
268 equipment: emptyEquipment(),
269 vault: [],
270 vaultReserved: []
271 });
272 const entityId = harness.simulation.spawnCharacter(1, character);
273 const cid = character.character_id;
274
275 harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid });
276 harness.simulation.step();
277 harness.simulation.enqueue(1, {
278 type: "cauldron-insert",
279 characterId: cid,
280 itemId: CraftingItemId.rawMeat,
281 quantity: 1
282 });
283 for (const action of [AlchemyStepKind.heat, AlchemyStepKind.stir, AlchemyStepKind.cool])
284 {
285 harness.simulation.enqueue(1, {
286 type: "cauldron-action",
287 characterId: cid,
288 action
289 });
290 }
291 harness.simulation.enqueue(1, { type: "cauldron-bottle", characterId: cid });
292 harness.simulation.step();
293
294 let bag = harness.simulation.bagOf(entityId);
295 assert.equal(countItem(bag.inventory, CraftingItemId.rawMeat), 0);
296 assert.equal(countItem(bag.inventory, ItemId.potion), 1);
297 const fatSlot = bag.inventory.find((slot) => slot?.itemId === ItemId.potion);
298 assert.equal(fatSlot.instance.potionKey, "animalFat");
299 assert.equal(countItem(bag.inventory, ItemId.glassBottle), 0);
300
301 harness.simulation.enqueue(1, { type: "cauldron-clear", characterId: cid });
302 harness.simulation.step();
303 harness.simulation.enqueue(1, {
304 type: "cauldron-insert",
305 characterId: cid,
306 itemId: WoodItemId.oakBark,
307 quantity: 2
308 });
309 harness.simulation.enqueue(1, {
310 type: "cauldron-action",
311 characterId: cid,
312 action: AlchemyStepKind.heat
313 });
314 harness.simulation.enqueue(1, {
315 type: "cauldron-insert",
316 characterId: cid,
317 itemId: CraftingItemId.salt,
318 quantity: 1
319 });
320 harness.simulation.enqueue(1, {
321 type: "cauldron-action",
322 characterId: cid,
323 action: AlchemyStepKind.stir
324 });
325 harness.simulation.enqueue(1, {
326 type: "cauldron-insert",
327 characterId: cid,
328 itemId: ItemId.potion,
329 quantity: 1,
330 instance: structuredClone(fatSlot.instance)
331 });
332 harness.simulation.enqueue(1, {
333 type: "cauldron-action",
334 characterId: cid,
335 action: AlchemyStepKind.cool
336 });
337 harness.simulation.enqueue(1, { type: "cauldron-bottle", characterId: cid });
338 harness.simulation.step();
339
340 bag = harness.simulation.bagOf(entityId);
341 assert.equal(countItem(bag.inventory, LeatherItemId.tanningBrew), 1);
342 assert.equal(countItem(bag.inventory, ItemId.potion), 0);
343 assert.equal(countItem(bag.inventory, ItemId.glassBottle), 1);
344 assert.equal(countItem(bag.inventory, WoodItemId.oakBark), 0);
345 assert.equal(countItem(bag.inventory, CraftingItemId.salt), 0);
346 }
347 finally
348 { harness.close(); }
349});