BlueGit

timfa questward

Files

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

firestarting-cooking.test.js

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