BlueGit

timfa questward

Files

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

firestarting-cooking.test.js

firestarting-cooking.test.js 11.1 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 places campfire and expiry drops ash", () =>
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 = 9001;
173 addGroundItem(worker.world, {
174 groundItemId,
175 tileX: 9,
176 tileY: 8,
177 stack: { itemId: WoodItemId.pineLog, quantity: 1 },
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 assert.equal(block?.state, WoodTier.pine);
193 assert.ok(worker.world.campfireExpires.has("9,8"));
194
195 const bag = harness.simulation.bagOf(playerId);
196 const tinder = bag.inventory.find((s) => s?.itemId === CraftingItemId.tinderbox);
197 assert.equal(tinder?.instance?.charges, 5);
198
199 const entry = worker.world.campfireExpires.get("9,8");
200 entry.expiresAt = 0;
201 advanceMs(harness, 200);
202 assert.equal(worker.world.chunkData(0, 0).blocks[8 * 16 + 9], null);
203 const ashOnGround = [...worker.world.groundItems.values()].some(
204 (item) => item.stack?.itemId === ashItemIdForWood(WoodTier.pine)
205 );
206 assert.equal(ashOnGround, true);
207 }
208 finally
209 { harness.close(); }
210});
211
212test("start-fire rejects too-low Firestarting for birch", () =>
213{
214 const harness = makeSimulation({ workerCount: 1 });
215 try
216 {
217 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
218 const playerId = harness.simulation.spawnCharacter(1, character);
219 const worker = workerOf(harness.simulation, playerId);
220 setSkillLevel(worker, playerId, SkillId.firestarting, 1);
221 giveInventory(harness, playerId, [
222 { itemId: CraftingItemId.tinderbox, quantity: 1, instance: tinderboxItemInstance(5) }
223 ]);
224 addGroundItem(worker.world, {
225 groundItemId: 9002,
226 tileX: 9,
227 tileY: 8,
228 stack: { itemId: WoodItemId.birchLog, quantity: 1 },
229 droppedAt: harness.now(),
230 expiresAt: harness.now() + 600_000,
231 source: "test"
232 });
233
234 harness.simulation.enqueue(1, {
235 type: "start-fire",
236 characterId: character.character_id,
237 groundItemId: 9002
238 });
239 advanceMs(harness, 200);
240 assert.equal(worker.world.groundItems.has(9002), true);
241 assert.equal(worker.world.chunkData(0, 0).blocks[8 * 16 + 9], null);
242 }
243 finally
244 { harness.close(); }
245});
246
247test("oven cook can burn to burnt venison with seeded rng", () =>
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.01;
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.rawMeat), 0);
272 assert.equal(countItem(bag.inventory, CraftingItemId.burntMeat), 1);
273 assert.equal(countItem(bag.inventory, CraftingItemId.cookedMeat), 0);
274 }
275 finally
276 { harness.close(); }
277});
278
279test("oven cook succeeds when rng avoids burn", () =>
280{
281 const harness = makeSimulation({ workerCount: 1 });
282 try
283 {
284 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
285 const playerId = harness.simulation.spawnCharacter(1, character);
286 const worker = workerOf(harness.simulation, playerId);
287 worker.world.rng = () => 0.99;
288 setSkillLevel(worker, playerId, SkillId.cooking, 1);
289 placeStation(worker, 9, 8, BlockType.oven, "oven");
290 giveInventory(harness, playerId, [
291 { itemId: CraftingItemId.rawMeat, quantity: 1 }
292 ]);
293
294 harness.simulation.enqueue(1, {
295 type: "start-craft",
296 characterId: character.character_id,
297 stationId: CraftingStationId.oven,
298 recipeId: "cook-venison"
299 });
300 advanceMs(harness, CRAFT_DURATION_MS + 500);
301
302 const bag = harness.simulation.bagOf(playerId);
303 assert.equal(countItem(bag.inventory, CraftingItemId.cookedMeat), 1);
304 }
305 finally
306 { harness.close(); }
307});
308
309test("eating cooked venison heals and applies health regen", () =>
310{
311 const harness = makeSimulation({ workerCount: 1 });
312 try
313 {
314 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
315 const playerId = harness.simulation.spawnCharacter(1, character);
316 const worker = workerOf(harness.simulation, playerId);
317 const handle = worker.world.ecs.handleOf(playerId);
318 const health = worker.world.ecs.get(handle, Component.health);
319 health.current = 2;
320 giveInventory(harness, playerId, [
321 { itemId: CraftingItemId.cookedMeat, quantity: 1 }
322 ]);
323
324 harness.simulation.enqueue(1, {
325 type: "eat-item",
326 characterId: character.character_id,
327 slotIndex: 0
328 });
329 harness.simulation.step();
330
331 assert.equal(health.current, 5);
332 const status = worker.world.ecs.get(handle, Component.statusEffects);
333 const regen = status?.effects?.find((e) => e.type === StatusEffectType.healthRegen);
334 assert.ok(regen);
335 assert.equal(regen.amountPerSecond, 1);
336 }
337 finally
338 { harness.close(); }
339});