BlueGit

timfa questward

Files

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

firestarting-cooking.test.js

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