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