BlueGit

timfa questward

Files

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

tanning-ingredients.test.js

tanning-ingredients.test.js 9.8 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("animal fat and tanning brew alchemy recipes resolve", () =>
152{
153 clearResolvedAlchemyPotionsCache();
154 const potions = resolvedAlchemyPotions();
155 const fat = potions.find((row) => row.potionKey === "animalFat");
156 const tan = potions.find((row) => row.potionKey === "tanningBrew");
157 assert.ok(fat);
158 assert.equal(fat.effect?.type, StatusEffectType.foodPoisoning);
159 assert.ok(tan);
160 assert.equal(tan.outputItemId, LeatherItemId.tanningBrew);
161 assert.equal(tan.requiresEmptyBottles, false);
162
163 const fatEval = evaluateAlchemyBottle({
164 skills: { levels: { [SkillId.alchemy]: 1 } },
165 brewStacks: [{ itemId: CraftingItemId.rawMeat, quantity: 1 }],
166 takenSteps: fat.steps
167 });
168 assert.equal(fatEval.success, true);
169 assert.equal(fatEval.result.potionKey, "animalFat");
170
171 const fatBottle = potionItemInstance(fatEval.result);
172 assert.equal(isFullPotionBottle({ itemId: ItemId.potion, instance: fatBottle }), true);
173
174 const tanEval = evaluateAlchemyBottle({
175 skills: { levels: { [SkillId.alchemy]: 10 } },
176 brewStacks: [
177 { itemId: WoodItemId.oakBark, quantity: 2 },
178 { itemId: CraftingItemId.salt, quantity: 1 },
179 { itemId: ItemId.potion, quantity: 1, instance: fatBottle }
180 ],
181 takenSteps: tan.steps
182 });
183 assert.equal(tanEval.success, true);
184 assert.equal(tanEval.result.outputItemId, LeatherItemId.tanningBrew);
185});
186
187test("cauldron bottles animal fat then tanning brew returns the empty bottle", () =>
188{
189 const harness = makeSimulation({ workerCount: 1 });
190 try
191 {
192 placeCauldron(harness.worldDb, 8, 8);
193 harness.simulation.reloadWorld();
194 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
195 setSkillLevel(harness, character.character_id, SkillId.alchemy, 10);
196 const inventory = emptySlots();
197 inventory[0] = { itemId: CraftingItemId.rawMeat, quantity: 1 };
198 inventory[1] = { itemId: ItemId.glassBottle, quantity: 1 };
199 inventory[2] = { itemId: WoodItemId.oakBark, quantity: 2 };
200 inventory[3] = { itemId: CraftingItemId.salt, quantity: 1 };
201 harness.stateDb.saveBag(character.character_id, {
202 inventory,
203 equipment: emptyEquipment(),
204 vault: [],
205 vaultReserved: []
206 });
207 const entityId = harness.simulation.spawnCharacter(1, character);
208 const cid = character.character_id;
209
210 harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid });
211 harness.simulation.step();
212 harness.simulation.enqueue(1, {
213 type: "cauldron-insert",
214 characterId: cid,
215 itemId: CraftingItemId.rawMeat,
216 quantity: 1
217 });
218 for (const action of [AlchemyStepKind.heat, AlchemyStepKind.stir, AlchemyStepKind.cool])
219 {
220 harness.simulation.enqueue(1, {
221 type: "cauldron-action",
222 characterId: cid,
223 action
224 });
225 }
226 harness.simulation.enqueue(1, { type: "cauldron-bottle", characterId: cid });
227 harness.simulation.step();
228
229 let bag = harness.simulation.bagOf(entityId);
230 assert.equal(countItem(bag.inventory, CraftingItemId.rawMeat), 0);
231 assert.equal(countItem(bag.inventory, ItemId.potion), 1);
232 const fatSlot = bag.inventory.find((slot) => slot?.itemId === ItemId.potion);
233 assert.equal(fatSlot.instance.potionKey, "animalFat");
234 assert.equal(countItem(bag.inventory, ItemId.glassBottle), 0);
235
236 harness.simulation.enqueue(1, { type: "cauldron-clear", characterId: cid });
237 harness.simulation.step();
238 harness.simulation.enqueue(1, {
239 type: "cauldron-insert",
240 characterId: cid,
241 itemId: WoodItemId.oakBark,
242 quantity: 2
243 });
244 harness.simulation.enqueue(1, {
245 type: "cauldron-action",
246 characterId: cid,
247 action: AlchemyStepKind.heat
248 });
249 harness.simulation.enqueue(1, {
250 type: "cauldron-insert",
251 characterId: cid,
252 itemId: CraftingItemId.salt,
253 quantity: 1
254 });
255 harness.simulation.enqueue(1, {
256 type: "cauldron-action",
257 characterId: cid,
258 action: AlchemyStepKind.stir
259 });
260 harness.simulation.enqueue(1, {
261 type: "cauldron-insert",
262 characterId: cid,
263 itemId: ItemId.potion,
264 quantity: 1,
265 instance: structuredClone(fatSlot.instance)
266 });
267 harness.simulation.enqueue(1, {
268 type: "cauldron-action",
269 characterId: cid,
270 action: AlchemyStepKind.cool
271 });
272 harness.simulation.enqueue(1, { type: "cauldron-bottle", characterId: cid });
273 harness.simulation.step();
274
275 bag = harness.simulation.bagOf(entityId);
276 assert.equal(countItem(bag.inventory, LeatherItemId.tanningBrew), 1);
277 assert.equal(countItem(bag.inventory, ItemId.potion), 0);
278 assert.equal(countItem(bag.inventory, ItemId.glassBottle), 1);
279 assert.equal(countItem(bag.inventory, WoodItemId.oakBark), 0);
280 assert.equal(countItem(bag.inventory, CraftingItemId.salt), 0);
281 }
282 finally
283 { harness.close(); }
284});