BlueGit

timfa questward

Files

questward / server / tests / firestarting-cooking.test.js

firestarting-cooking.test.js

firestarting-cooking.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 {
5 CraftingStationId,
6 firestartingDifficulty,
7 campfireDurationMs,
8 COOK_BURN_CHANCE_OVEN,
9 COOK_BURN_CHANCE_CAMPFIRE,
10 CRAFT_DURATION_MS
11} from "../../shared/definitions/craftingStations.js";
12import { CraftingItemId, tinderboxItemInstance } from "../../shared/definitions/craftingItems.js";
13import { MobId, mobDefinition } from "../../shared/definitions/mobs.js";
14import { WoodItemId, ashItemIdForWood } from "../../shared/definitions/woodItems.js";
15import { WoodTier } from "../../shared/definitions/materialTiers.js";
16import { SkillId } from "../../shared/definitions/skills.js";
17import { recipeDefinition, recipesForStation } from "../../shared/crafting/stationRecipes.js";
18import { ItemId, itemDefinition } from "../../shared/definitions/items.js";
19import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";
20import { totalXpForLevel } from "../../shared/skills/progression.js";
21import { ashTintColor } from "../../shared/render/tint.js";
22import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
23import { Component } from "../src/ecs/components.js";
24import { addGroundItem } from "../src/items/bags.js";
25import { StatusEffectType } from "../../shared/statusEffects/rules.js";
26
27function countItem (slots, itemId)
28{
29 return (slots ?? []).reduce((sum, slot) =>
30 {
31 if (slot?.itemId === itemId)
32 { return sum + (slot.quantity | 0); }
33 return sum;
34 }, 0);
35}
36
37function workerOf (simulation, playerId)
38{
39 const record = simulation.entityRecord(playerId);
40 return simulation.workers[record.workerId];
41}
42
43function setSkillLevel (worker, playerId, skillId, level)
44{
45 const handle = worker.world.ecs.handleOf(playerId);
46 const skills = worker.world.ecs.get(handle, Component.skills);
47 skills.xp[skillId] = totalXpForLevel(level);
48}
49
50function giveInventory (harness, playerId, items)
51{
52 const inventory = emptySlots();
53 items.forEach((stack, index) =>
54 {
55 inventory[index] = {
56 itemId: stack.itemId,
57 quantity: stack.quantity,
58 ...(stack.instance ? { instance: structuredClone(stack.instance) } : {})
59 };
60 });
61 harness.simulation.applyBag(playerId, {
62 inventory,
63 equipment: emptyEquipment(),
64 vault: [],
65 vaultReserved: []
66 });
67}
68
69function advanceMs (harness, ms)
70{
71 const ticks = Math.ceil(ms / 50) + 3;
72 for (let i = 0; i < ticks; i++)
73 {
74 harness.advance(50);
75 harness.simulation.step();
76 }
77}
78
79function placeStation (worker, tileX, tileY, typeId, state)
80{
81 const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16));
82 const localX = ((tileX % 16) + 16) % 16;
83 const localY = ((tileY % 16) + 16) % 16;
84 data.blocks[localY * 16 + localX] = {
85 typeId,
86 state,
87 config: null
88 };
89}
90
91test("firestarting difficulty and campfire duration scale with wood tier", () =>
92{
93 assert.equal(firestartingDifficulty(WoodTier.pine), 1);
94 assert.equal(firestartingDifficulty(WoodTier.birch), 10);
95 assert.equal(firestartingDifficulty(WoodTier.oak), 20);
96 assert.equal(campfireDurationMs(WoodTier.pine), 3 * 60_000);
97 assert.equal(campfireDurationMs(WoodTier.oak), 9 * 60_000);
98 assert.equal(COOK_BURN_CHANCE_OVEN, 0.1);
99 assert.equal(COOK_BURN_CHANCE_CAMPFIRE, 0.35);
100});
101
102test("ash tint sits between dark grey and wood colour", () =>
103{
104 const tint = ashTintColor(WoodTier.pine);
105 assert.ok(tint.r > 0.12 && tint.r < 0.9);
106});
107
108test("cook-venison recipe exists for oven and campfire with burnt output", () =>
109{
110 const recipe = recipeDefinition("cook-venison");
111 assert.ok(recipe);
112 assert.deepEqual(recipe.stationIds, [CraftingStationId.oven, CraftingStationId.fireplace]);
113 assert.equal(recipe.output.itemId, CraftingItemId.cookedMeat);
114 assert.equal(recipe.burntOutput.itemId, CraftingItemId.burntMeat);
115 assert.equal(recipe.inputs[0].itemId, CraftingItemId.rawMeat);
116});
117
118test("eggs cook only in an oven and cannot be eaten raw", () =>
119{
120 assert.equal(itemDefinition(ItemId.egg).healing ?? 0, 0);
121 assert.equal(itemDefinition(ItemId.cookedEgg).healing, 3);
122 const recipe = recipeDefinition("cook-egg");
123 assert.ok(recipe);
124 assert.deepEqual(recipe.stationIds, [CraftingStationId.oven]);
125 assert.equal(recipe.output.itemId, ItemId.cookedEgg);
126 assert.equal(recipe.burntOutput.itemId, ItemId.burntEgg);
127 assert.equal(recipe.inputs[0].itemId, ItemId.egg);
128 assert.equal(
129 recipesForStation(CraftingStationId.fireplace).some((r) => r.recipeId === "cook-egg"),
130 false
131 );
132});
133
134test("sheep and goat drop cookable meats for oven and campfire", () =>
135{
136 const sheep = mobDefinition(MobId.sheep);
137 assert.ok(sheep.drops.some((drop) => drop.itemId === CraftingItemId.rawMutton));
138 const goat = mobDefinition(MobId.cashmereGoat);
139 assert.ok(goat.drops.some((drop) => drop.itemId === CraftingItemId.rawGoat));
140 const mutton = recipeDefinition("cook-mutton");
141 assert.deepEqual(mutton.stationIds, [CraftingStationId.oven, CraftingStationId.fireplace]);
142 assert.equal(mutton.output.itemId, CraftingItemId.cookedMutton);
143 const goatCook = recipeDefinition("cook-goat");
144 assert.deepEqual(goatCook.stationIds, [CraftingStationId.oven, CraftingStationId.fireplace]);
145 assert.equal(goatCook.output.itemId, CraftingItemId.cookedGoat);
146});
147
148test("fish cook recipes use the species name as craft label", () =>
149{
150 const trout = recipeDefinition("cook-trout");
151 assert.ok(trout);
152 assert.equal(trout.craftGroupLabel, "Trout");
153 assert.equal(trout.craftGroup, "cook-trout");
154 const minnow = recipeDefinition("cook-minnow");
155 assert.equal(minnow.craftGroupLabel, "Minnow");
156});
157
158test("start-fire on a log stack consumes only one log", () =>
159{
160 const harness = makeSimulation({ workerCount: 1 });
161 try
162 {
163 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
164 const playerId = harness.simulation.spawnCharacter(1, character);
165 const worker = workerOf(harness.simulation, playerId);
166 worker.world.rng = () => 0.999;
167 setSkillLevel(worker, playerId, SkillId.firestarting, 1);
168 giveInventory(harness, playerId, [
169 { itemId: CraftingItemId.tinderbox, quantity: 1, instance: tinderboxItemInstance(6) }
170 ]);
171
172 const groundItemId = 9010;
173 addGroundItem(worker.world, {
174 groundItemId,
175 tileX: 9,
176 tileY: 8,
177 stack: { itemId: WoodItemId.pineLog, quantity: 5 },
178 droppedAt: harness.now(),
179 expiresAt: harness.now() + 600_000,
180 source: "test"
181 });
182
183 harness.simulation.enqueue(1, {
184 type: "start-fire",
185 characterId: character.character_id,
186 groundItemId
187 });
188 advanceMs(harness, 4000);
189
190 const block = worker.world.chunkData(0, 0).blocks[8 * 16 + 9];
191 assert.equal(block?.typeId, BlockType.campfire);
192 const remain = worker.world.groundItems.get(groundItemId);
193 assert.ok(remain);
194 assert.equal(remain.stack.itemId, WoodItemId.pineLog);
195 assert.equal(remain.stack.quantity, 4);
196 }
197 finally
198 { harness.close(); }
199});
200
201test("start-fire places campfire and expiry drops ash", () =>
202{
203 const harness = makeSimulation({ workerCount: 1 });
204 try
205 {
206 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
207 const playerId = harness.simulation.spawnCharacter(1, character);
208 const worker = workerOf(harness.simulation, playerId);
209 worker.world.rng = () => 0.999;
210 setSkillLevel(worker, playerId, SkillId.firestarting, 1);
211 giveInventory(harness, playerId, [
212 { itemId: CraftingItemId.tinderbox, quantity: 1, instance: tinderboxItemInstance(6) }
213 ]);
214
215 const groundItemId = 9001;
216 addGroundItem(worker.world, {
217 groundItemId,
218 tileX: 9,
219 tileY: 8,
220 stack: { itemId: WoodItemId.pineLog, quantity: 1 },
221 droppedAt: harness.now(),
222 expiresAt: harness.now() + 600_000,
223 source: "test"
224 });
225
226 harness.simulation.enqueue(1, {
227 type: "start-fire",
228 characterId: character.character_id,
229 groundItemId
230 });
231 advanceMs(harness, 4000);
232
233 const block = worker.world.chunkData(0, 0).blocks[8 * 16 + 9];
234 assert.equal(block?.typeId, BlockType.campfire);
235 assert.equal(block?.state, WoodTier.pine);
236 assert.ok(worker.world.campfireExpires.has("9,8"));
237
238 const bag = harness.simulation.bagOf(playerId);
239 const tinder = bag.inventory.find((s) => s?.itemId === CraftingItemId.tinderbox);
240 assert.equal(tinder?.instance?.charges, 5);
241
242 const entry = worker.world.campfireExpires.get("9,8");
243 entry.expiresAt = 0;
244 advanceMs(harness, 200);
245 assert.equal(worker.world.chunkData(0, 0).blocks[8 * 16 + 9], null);
246 const ashOnGround = [...worker.world.groundItems.values()].some(
247 (item) => item.stack?.itemId === ashItemIdForWood(WoodTier.pine)
248 );
249 assert.equal(ashOnGround, true);
250 }
251 finally
252 { harness.close(); }
253});
254
255test("start-fire rejects too-low Firestarting for birch", () =>
256{
257 const harness = makeSimulation({ workerCount: 1 });
258 try
259 {
260 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
261 const playerId = harness.simulation.spawnCharacter(1, character);
262 const worker = workerOf(harness.simulation, playerId);
263 setSkillLevel(worker, playerId, SkillId.firestarting, 1);
264 giveInventory(harness, playerId, [
265 { itemId: CraftingItemId.tinderbox, quantity: 1, instance: tinderboxItemInstance(5) }
266 ]);
267 addGroundItem(worker.world, {
268 groundItemId: 9002,
269 tileX: 9,
270 tileY: 8,
271 stack: { itemId: WoodItemId.birchLog, quantity: 1 },
272 droppedAt: harness.now(),
273 expiresAt: harness.now() + 600_000,
274 source: "test"
275 });
276
277 harness.simulation.enqueue(1, {
278 type: "start-fire",
279 characterId: character.character_id,
280 groundItemId: 9002
281 });
282 advanceMs(harness, 200);
283 assert.equal(worker.world.groundItems.has(9002), true);
284 assert.equal(worker.world.chunkData(0, 0).blocks[8 * 16 + 9], null);
285 }
286 finally
287 { harness.close(); }
288});
289
290test("oven cook can burn to burnt venison with seeded rng", () =>
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 worker = workerOf(harness.simulation, playerId);
298 worker.world.rng = () => 0.01;
299 setSkillLevel(worker, playerId, SkillId.cooking, 1);
300 placeStation(worker, 9, 8, BlockType.oven, "oven");
301 giveInventory(harness, playerId, [
302 { itemId: CraftingItemId.rawMeat, quantity: 1 }
303 ]);
304
305 harness.simulation.enqueue(1, {
306 type: "start-craft",
307 characterId: character.character_id,
308 stationId: CraftingStationId.oven,
309 recipeId: "cook-venison"
310 });
311 advanceMs(harness, CRAFT_DURATION_MS + 500);
312
313 const bag = harness.simulation.bagOf(playerId);
314 assert.equal(countItem(bag.inventory, CraftingItemId.rawMeat), 0);
315 assert.equal(countItem(bag.inventory, CraftingItemId.burntMeat), 1);
316 assert.equal(countItem(bag.inventory, CraftingItemId.cookedMeat), 0);
317 }
318 finally
319 { harness.close(); }
320});
321
322test("oven cook succeeds when rng avoids burn", () =>
323{
324 const harness = makeSimulation({ workerCount: 1 });
325 try
326 {
327 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
328 const playerId = harness.simulation.spawnCharacter(1, character);
329 const worker = workerOf(harness.simulation, playerId);
330 worker.world.rng = () => 0.99;
331 setSkillLevel(worker, playerId, SkillId.cooking, 1);
332 placeStation(worker, 9, 8, BlockType.oven, "oven");
333 giveInventory(harness, playerId, [
334 { itemId: CraftingItemId.rawMeat, quantity: 1 }
335 ]);
336
337 harness.simulation.enqueue(1, {
338 type: "start-craft",
339 characterId: character.character_id,
340 stationId: CraftingStationId.oven,
341 recipeId: "cook-venison"
342 });
343 advanceMs(harness, CRAFT_DURATION_MS + 500);
344
345 const bag = harness.simulation.bagOf(playerId);
346 assert.equal(countItem(bag.inventory, CraftingItemId.cookedMeat), 1);
347 }
348 finally
349 { harness.close(); }
350});
351
352test("eating cooked venison heals and applies health regen", () =>
353{
354 const harness = makeSimulation({ workerCount: 1 });
355 try
356 {
357 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
358 const playerId = harness.simulation.spawnCharacter(1, character);
359 const worker = workerOf(harness.simulation, playerId);
360 const handle = worker.world.ecs.handleOf(playerId);
361 const health = worker.world.ecs.get(handle, Component.health);
362 health.current = 2;
363 giveInventory(harness, playerId, [
364 { itemId: CraftingItemId.cookedMeat, quantity: 1 }
365 ]);
366
367 harness.simulation.enqueue(1, {
368 type: "eat-item",
369 characterId: character.character_id,
370 slotIndex: 0
371 });
372 harness.simulation.step();
373
374 assert.equal(health.current, 5);
375 const status = worker.world.ecs.get(handle, Component.statusEffects);
376 const regen = status?.effects?.find((e) => e.type === StatusEffectType.healthRegen);
377 assert.ok(regen);
378 assert.equal(regen.amountPerSecond, 1);
379 }
380 finally
381 { harness.close(); }
382});