BlueGit

timfa questward

Files

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

firestarting-cooking.test.js

firestarting-cooking.test.js 9.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 } 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("start-fire places campfire and expiry drops ash", () =>
117{
118 const harness = makeSimulation({ workerCount: 1 });
119 try
120 {
121 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
122 const playerId = harness.simulation.spawnCharacter(1, character);
123 const worker = workerOf(harness.simulation, playerId);
124 worker.world.rng = () => 0.999;
125 setSkillLevel(worker, playerId, SkillId.firestarting, 1);
126 giveInventory(harness, playerId, [
127 { itemId: CraftingItemId.tinderbox, quantity: 1, instance: tinderboxItemInstance(6) }
128 ]);
129
130 const groundItemId = 9001;
131 addGroundItem(worker.world, {
132 groundItemId,
133 tileX: 9,
134 tileY: 8,
135 stack: { itemId: WoodItemId.pineLog, quantity: 1 },
136 droppedAt: harness.now(),
137 expiresAt: harness.now() + 600_000,
138 source: "test"
139 });
140
141 harness.simulation.enqueue(1, {
142 type: "start-fire",
143 characterId: character.character_id,
144 groundItemId
145 });
146 advanceMs(harness, 4000);
147
148 const block = worker.world.chunkData(0, 0).blocks[8 * 16 + 9];
149 assert.equal(block?.typeId, BlockType.campfire);
150 assert.equal(block?.state, WoodTier.pine);
151 assert.ok(worker.world.campfireExpires.has("9,8"));
152
153 const bag = harness.simulation.bagOf(playerId);
154 const tinder = bag.inventory.find((s) => s?.itemId === CraftingItemId.tinderbox);
155 assert.equal(tinder?.instance?.charges, 5);
156
157 const entry = worker.world.campfireExpires.get("9,8");
158 entry.expiresAt = 0;
159 advanceMs(harness, 200);
160 assert.equal(worker.world.chunkData(0, 0).blocks[8 * 16 + 9], null);
161 const ashOnGround = [...worker.world.groundItems.values()].some(
162 (item) => item.stack?.itemId === ashItemIdForWood(WoodTier.pine)
163 );
164 assert.equal(ashOnGround, true);
165 }
166 finally
167 { harness.close(); }
168});
169
170test("start-fire rejects too-low Firestarting for birch", () =>
171{
172 const harness = makeSimulation({ workerCount: 1 });
173 try
174 {
175 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
176 const playerId = harness.simulation.spawnCharacter(1, character);
177 const worker = workerOf(harness.simulation, playerId);
178 setSkillLevel(worker, playerId, SkillId.firestarting, 1);
179 giveInventory(harness, playerId, [
180 { itemId: CraftingItemId.tinderbox, quantity: 1, instance: tinderboxItemInstance(5) }
181 ]);
182 addGroundItem(worker.world, {
183 groundItemId: 9002,
184 tileX: 9,
185 tileY: 8,
186 stack: { itemId: WoodItemId.birchLog, quantity: 1 },
187 droppedAt: harness.now(),
188 expiresAt: harness.now() + 600_000,
189 source: "test"
190 });
191
192 harness.simulation.enqueue(1, {
193 type: "start-fire",
194 characterId: character.character_id,
195 groundItemId: 9002
196 });
197 advanceMs(harness, 200);
198 assert.equal(worker.world.groundItems.has(9002), true);
199 assert.equal(worker.world.chunkData(0, 0).blocks[8 * 16 + 9], null);
200 }
201 finally
202 { harness.close(); }
203});
204
205test("oven cook can burn to burnt venison with seeded rng", () =>
206{
207 const harness = makeSimulation({ workerCount: 1 });
208 try
209 {
210 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
211 const playerId = harness.simulation.spawnCharacter(1, character);
212 const worker = workerOf(harness.simulation, playerId);
213 worker.world.rng = () => 0.01;
214 setSkillLevel(worker, playerId, SkillId.cooking, 1);
215 placeStation(worker, 9, 8, BlockType.oven, "oven");
216 giveInventory(harness, playerId, [
217 { itemId: CraftingItemId.rawMeat, quantity: 1 }
218 ]);
219
220 harness.simulation.enqueue(1, {
221 type: "start-craft",
222 characterId: character.character_id,
223 stationId: CraftingStationId.oven,
224 recipeId: "cook-venison"
225 });
226 advanceMs(harness, CRAFT_DURATION_MS + 500);
227
228 const bag = harness.simulation.bagOf(playerId);
229 assert.equal(countItem(bag.inventory, CraftingItemId.rawMeat), 0);
230 assert.equal(countItem(bag.inventory, CraftingItemId.burntMeat), 1);
231 assert.equal(countItem(bag.inventory, CraftingItemId.cookedMeat), 0);
232 }
233 finally
234 { harness.close(); }
235});
236
237test("oven cook succeeds when rng avoids burn", () =>
238{
239 const harness = makeSimulation({ workerCount: 1 });
240 try
241 {
242 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
243 const playerId = harness.simulation.spawnCharacter(1, character);
244 const worker = workerOf(harness.simulation, playerId);
245 worker.world.rng = () => 0.99;
246 setSkillLevel(worker, playerId, SkillId.cooking, 1);
247 placeStation(worker, 9, 8, BlockType.oven, "oven");
248 giveInventory(harness, playerId, [
249 { itemId: CraftingItemId.rawMeat, quantity: 1 }
250 ]);
251
252 harness.simulation.enqueue(1, {
253 type: "start-craft",
254 characterId: character.character_id,
255 stationId: CraftingStationId.oven,
256 recipeId: "cook-venison"
257 });
258 advanceMs(harness, CRAFT_DURATION_MS + 500);
259
260 const bag = harness.simulation.bagOf(playerId);
261 assert.equal(countItem(bag.inventory, CraftingItemId.cookedMeat), 1);
262 }
263 finally
264 { harness.close(); }
265});
266
267test("eating cooked venison heals and applies health regen", () =>
268{
269 const harness = makeSimulation({ workerCount: 1 });
270 try
271 {
272 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
273 const playerId = harness.simulation.spawnCharacter(1, character);
274 const worker = workerOf(harness.simulation, playerId);
275 const handle = worker.world.ecs.handleOf(playerId);
276 const health = worker.world.ecs.get(handle, Component.health);
277 health.current = 5;
278 health.max = 20;
279 giveInventory(harness, playerId, [
280 { itemId: CraftingItemId.cookedMeat, quantity: 1 }
281 ]);
282
283 harness.simulation.enqueue(1, {
284 type: "eat-item",
285 characterId: character.character_id,
286 slotIndex: 0
287 });
288 harness.simulation.step();
289
290 assert.equal(health.current, 10);
291 const status = worker.world.ecs.get(handle, Component.statusEffects);
292 const regen = status?.effects?.find((e) => e.type === StatusEffectType.healthRegen);
293 assert.ok(regen);
294 assert.equal(regen.amountPerSecond, 1);
295 }
296 finally
297 { harness.close(); }
298});