| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 4 | import { ItemId, itemDefinition, itemDisplayName, itemSpriteNameForStack } from "../../shared/definitions/items.js"; |
| 5 | import { CraftingItemId } from "../../shared/definitions/craftingItems.js"; |
| 6 | import { |
| 7 | WoodItemId, |
| 8 | debarkedLogInstance, |
| 9 | isLogDebarked, |
| 10 | barkItemIdForWood |
| 11 | } from "../../shared/definitions/woodItems.js"; |
| 12 | import { WoodTier, GatherMaterialId, MaterialTier, oreBoulderState } from "../../shared/definitions/materialTiers.js"; |
| 13 | import { SkillId } from "../../shared/definitions/skills.js"; |
| 14 | import { LeatherItemId } from "../../shared/definitions/leatherItems.js"; |
| 15 | import { AlchemyStepKind } from "../../shared/definitions/alchemy.js"; |
| 16 | import { canMineMaterial, oreItemIdForMaterial, woodworkingXpForDebark } from "../../shared/definitions/gathering.js"; |
| 17 | import { |
| 18 | clearResolvedAlchemyPotionsCache, |
| 19 | resolvedAlchemyPotions, |
| 20 | evaluateAlchemyBottle, |
| 21 | potionItemInstance |
| 22 | } from "../../shared/alchemy/recipes.js"; |
| 23 | import { StatusEffectType } from "../../shared/statusEffects/rules.js"; |
| 24 | import { isFullPotionBottle } from "../../shared/items/charges.js"; |
| 25 | import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js"; |
| 26 | import { totalXpForLevel } from "../../shared/skills/progression.js"; |
| 27 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 28 | import { addGroundItem } from "../src/items/bags.js"; |
| 29 | import { Component } from "../src/ecs/components.js"; |
| 30 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 31 | |
| 32 | function placeCauldron (worldDb, tileX, tileY) |
| 33 | { |
| 34 | const chunkX = Math.floor(tileX / 16); |
| 35 | const chunkY = Math.floor(tileY / 16); |
| 36 | const data = createEmptyChunkData(); |
| 37 | data.open = true; |
| 38 | const localX = tileX - chunkX * 16; |
| 39 | const localY = tileY - chunkY * 16; |
| 40 | data.blocks[localY * 16 + localX] = { typeId: BlockType.cauldron, state: 0 }; |
| 41 | worldDb.saveChunkVersion( |
| 42 | chunkX, |
| 43 | chunkY, |
| 44 | worldDb.getActiveRevisionId(), |
| 45 | true, |
| 46 | encodeChunkBlob(data) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | function countItem (slots, itemId) |
| 51 | { |
| 52 | return (slots ?? []) |
| 53 | .filter((slot) => slot?.itemId === itemId) |
| 54 | .reduce((sum, slot) => sum + (slot.quantity | 0), 0); |
| 55 | } |
| 56 | |
| 57 | function setSkillLevel (harness, characterId, skillId, level) |
| 58 | { |
| 59 | const skills = harness.stateDb.getSkills(characterId); |
| 60 | skills.xp[skillId] = totalXpForLevel(level); |
| 61 | harness.stateDb.saveSkills(characterId, skills); |
| 62 | } |
| 63 | |
| 64 | test("rock salt mines with bronze pick and uses salt-ore boulder state", () => |
| 65 | { |
| 66 | assert.equal(oreBoulderState(GatherMaterialId.salt), "salt-ore"); |
| 67 | assert.equal(oreItemIdForMaterial(GatherMaterialId.salt), CraftingItemId.salt); |
| 68 | assert.equal(canMineMaterial(1, MaterialTier.bronze, GatherMaterialId.salt).ok, true); |
| 69 | const def = itemDefinition(CraftingItemId.salt); |
| 70 | assert.equal(def.key, "rock-salt"); |
| 71 | assert.equal(def.iconKey, "ash"); |
| 72 | assert.equal(def.tier, GatherMaterialId.salt); |
| 73 | }); |
| 74 | |
| 75 | test("debarked logs keep oak itemId but change name and sprite", () => |
| 76 | { |
| 77 | const plain = { itemId: WoodItemId.oakLog, quantity: 1 }; |
| 78 | const stripped = { itemId: WoodItemId.oakLog, quantity: 1, instance: debarkedLogInstance() }; |
| 79 | assert.equal(isLogDebarked(plain), false); |
| 80 | assert.equal(isLogDebarked(stripped), true); |
| 81 | assert.equal(itemDisplayName(plain), "Oak log"); |
| 82 | assert.equal(itemDisplayName(stripped), "Debarked Oak log"); |
| 83 | assert.match(itemSpriteNameForStack(stripped), /log-debarked/); |
| 84 | assert.equal(barkItemIdForWood(WoodTier.oak), WoodItemId.oakBark); |
| 85 | }); |
| 86 | |
| 87 | test("debark Woodworking XP is half wood tier rank rounded up", () => |
| 88 | { |
| 89 | assert.equal(woodworkingXpForDebark(WoodTier.pine), 1); |
| 90 | assert.equal(woodworkingXpForDebark(WoodTier.birch), 1); |
| 91 | assert.equal(woodworkingXpForDebark(WoodTier.oak), 2); |
| 92 | assert.equal(woodworkingXpForDebark(WoodTier.elder), 4); |
| 93 | }); |
| 94 | |
| 95 | test("debark-log on ground oak log yields bark and debarked flag", () => |
| 96 | { |
| 97 | const harness = makeSimulation({ workerCount: 1 }); |
| 98 | try |
| 99 | { |
| 100 | harness.simulation.reloadWorld(); |
| 101 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 102 | setSkillLevel(harness, character.character_id, SkillId.woodcutting, 20); |
| 103 | setSkillLevel(harness, character.character_id, SkillId.woodworking, 1); |
| 104 | const inventory = emptySlots(); |
| 105 | inventory[0] = { itemId: ItemId.ironAxe, quantity: 1 }; |
| 106 | harness.stateDb.saveBag(character.character_id, { |
| 107 | inventory, |
| 108 | equipment: emptyEquipment(), |
| 109 | vault: [], |
| 110 | vaultReserved: [] |
| 111 | }); |
| 112 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 113 | const record = harness.simulation.entityRecord(entityId); |
| 114 | const worker = harness.simulation.workers[record.workerId]; |
| 115 | const handle = worker.world.ecs.handleOf(entityId); |
| 116 | const woodworkingXpBefore = worker.world.ecs.get(handle, Component.skills).xp[SkillId.woodworking] | 0; |
| 117 | |
| 118 | const groundItemId = (worker.world.nextGroundItemId = (worker.world.nextGroundItemId ?? 1) + 1); |
| 119 | addGroundItem(worker.world, { |
| 120 | groundItemId, |
| 121 | tileX: 8, |
| 122 | tileY: 9, |
| 123 | stack: { itemId: WoodItemId.oakLog, quantity: 1 }, |
| 124 | droppedAt: Date.now(), |
| 125 | expiresAt: Date.now() + 600_000, |
| 126 | source: "test" |
| 127 | }); |
| 128 | |
| 129 | harness.simulation.enqueue(1, { |
| 130 | type: "debark-log", |
| 131 | characterId: character.character_id, |
| 132 | groundItemId |
| 133 | }); |
| 134 | harness.simulation.step(); |
| 135 | |
| 136 | const gather = worker.world.ecs.get(handle, Component.gather); |
| 137 | assert.equal(gather?.kind, "debark"); |
| 138 | assert.equal(gather?.durationMs, 2000); |
| 139 | |
| 140 | for (let i = 0; i < 50; i++) |
| 141 | { |
| 142 | harness.advance(50); |
| 143 | harness.simulation.step(); |
| 144 | } |
| 145 | |
| 146 | const item = worker.world.groundItems.get(groundItemId); |
| 147 | assert.ok(item); |
| 148 | assert.equal(isLogDebarked(item.stack), true); |
| 149 | assert.equal(item.stack.itemId, WoodItemId.oakLog); |
| 150 | |
| 151 | const bark = [...worker.world.groundItems.values()] |
| 152 | .find((row) => row.stack?.itemId === WoodItemId.oakBark); |
| 153 | assert.ok(bark); |
| 154 | assert.equal(bark.stack.quantity, 1); |
| 155 | assert.equal(worker.world.ecs.has(handle, Component.gather), false); |
| 156 | |
| 157 | const woodworkingXpAfter = worker.world.ecs.get(handle, Component.skills).xp[SkillId.woodworking] | 0; |
| 158 | assert.equal(woodworkingXpAfter - woodworkingXpBefore, woodworkingXpForDebark(WoodTier.oak)); |
| 159 | } |
| 160 | finally |
| 161 | { harness.close(); } |
| 162 | }); |
| 163 | |
| 164 | test("debark-log on a ground log stack strips only one log", () => |
| 165 | { |
| 166 | const harness = makeSimulation({ workerCount: 1 }); |
| 167 | try |
| 168 | { |
| 169 | harness.simulation.reloadWorld(); |
| 170 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 171 | setSkillLevel(harness, character.character_id, SkillId.woodcutting, 20); |
| 172 | const inventory = emptySlots(); |
| 173 | inventory[0] = { itemId: ItemId.ironAxe, quantity: 1 }; |
| 174 | harness.stateDb.saveBag(character.character_id, { |
| 175 | inventory, |
| 176 | equipment: emptyEquipment(), |
| 177 | vault: [], |
| 178 | vaultReserved: [] |
| 179 | }); |
| 180 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 181 | const record = harness.simulation.entityRecord(entityId); |
| 182 | const worker = harness.simulation.workers[record.workerId]; |
| 183 | |
| 184 | const groundItemId = (worker.world.nextGroundItemId = (worker.world.nextGroundItemId ?? 1) + 1); |
| 185 | addGroundItem(worker.world, { |
| 186 | groundItemId, |
| 187 | tileX: 8, |
| 188 | tileY: 9, |
| 189 | stack: { itemId: WoodItemId.oakLog, quantity: 4 }, |
| 190 | droppedAt: Date.now(), |
| 191 | expiresAt: Date.now() + 600_000, |
| 192 | source: "test" |
| 193 | }); |
| 194 | |
| 195 | harness.simulation.enqueue(1, { |
| 196 | type: "debark-log", |
| 197 | characterId: character.character_id, |
| 198 | groundItemId |
| 199 | }); |
| 200 | harness.simulation.step(); |
| 201 | |
| 202 | for (let i = 0; i < 50; i++) |
| 203 | { |
| 204 | harness.advance(50); |
| 205 | harness.simulation.step(); |
| 206 | } |
| 207 | |
| 208 | const remain = worker.world.groundItems.get(groundItemId); |
| 209 | assert.ok(remain); |
| 210 | assert.equal(remain.stack.quantity, 3); |
| 211 | assert.equal(isLogDebarked(remain.stack), false); |
| 212 | |
| 213 | const debarked = [...worker.world.groundItems.values()] |
| 214 | .find((row) => row.groundItemId !== groundItemId |
| 215 | && row.stack?.itemId === WoodItemId.oakLog |
| 216 | && isLogDebarked(row.stack)); |
| 217 | assert.ok(debarked); |
| 218 | assert.equal(debarked.stack.quantity, 1); |
| 219 | |
| 220 | const bark = [...worker.world.groundItems.values()] |
| 221 | .find((row) => row.stack?.itemId === WoodItemId.oakBark); |
| 222 | assert.ok(bark); |
| 223 | assert.equal(bark.stack.quantity, 1); |
| 224 | } |
| 225 | finally |
| 226 | { harness.close(); } |
| 227 | }); |
| 228 | |
| 229 | test("debarking a ground log stack merges all debarked logs into one pile", () => |
| 230 | { |
| 231 | const harness = makeSimulation({ workerCount: 1 }); |
| 232 | try |
| 233 | { |
| 234 | harness.simulation.reloadWorld(); |
| 235 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 236 | setSkillLevel(harness, character.character_id, SkillId.woodcutting, 20); |
| 237 | const inventory = emptySlots(); |
| 238 | inventory[0] = { itemId: ItemId.ironAxe, quantity: 1 }; |
| 239 | harness.stateDb.saveBag(character.character_id, { |
| 240 | inventory, |
| 241 | equipment: emptyEquipment(), |
| 242 | vault: [], |
| 243 | vaultReserved: [] |
| 244 | }); |
| 245 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 246 | const record = harness.simulation.entityRecord(entityId); |
| 247 | const worker = harness.simulation.workers[record.workerId]; |
| 248 | |
| 249 | const groundItemId = (worker.world.nextGroundItemId = (worker.world.nextGroundItemId ?? 1) + 1); |
| 250 | addGroundItem(worker.world, { |
| 251 | groundItemId, |
| 252 | tileX: 8, |
| 253 | tileY: 9, |
| 254 | stack: { itemId: WoodItemId.oakLog, quantity: 3 }, |
| 255 | droppedAt: Date.now(), |
| 256 | expiresAt: Date.now() + 600_000, |
| 257 | source: "test" |
| 258 | }); |
| 259 | |
| 260 | for (let n = 0; n < 3; n++) |
| 261 | { |
| 262 | const plain = [...worker.world.groundItems.values()] |
| 263 | .find((row) => row.stack?.itemId === WoodItemId.oakLog && !isLogDebarked(row.stack)); |
| 264 | assert.ok(plain, `plain log pile before debark ${n + 1}`); |
| 265 | harness.simulation.enqueue(1, { |
| 266 | type: "debark-log", |
| 267 | characterId: character.character_id, |
| 268 | groundItemId: plain.groundItemId |
| 269 | }); |
| 270 | harness.simulation.step(); |
| 271 | for (let i = 0; i < 50; i++) |
| 272 | { |
| 273 | harness.advance(50); |
| 274 | harness.simulation.step(); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | const plainLeft = [...worker.world.groundItems.values()] |
| 279 | .filter((row) => row.stack?.itemId === WoodItemId.oakLog && !isLogDebarked(row.stack)); |
| 280 | assert.equal(plainLeft.length, 0); |
| 281 | |
| 282 | const debarkedPiles = [...worker.world.groundItems.values()] |
| 283 | .filter((row) => row.stack?.itemId === WoodItemId.oakLog && isLogDebarked(row.stack)); |
| 284 | assert.equal(debarkedPiles.length, 1); |
| 285 | assert.equal(debarkedPiles[0].stack.quantity, 3); |
| 286 | |
| 287 | const bark = [...worker.world.groundItems.values()] |
| 288 | .find((row) => row.stack?.itemId === WoodItemId.oakBark); |
| 289 | assert.ok(bark); |
| 290 | assert.equal(bark.stack.quantity, 3); |
| 291 | } |
| 292 | finally |
| 293 | { harness.close(); } |
| 294 | }); |
| 295 | |
| 296 | test("animal fat and tanning brew alchemy recipes resolve", () => |
| 297 | { |
| 298 | clearResolvedAlchemyPotionsCache(); |
| 299 | const potions = resolvedAlchemyPotions(); |
| 300 | const fat = potions.find((row) => row.potionKey === "animalFat"); |
| 301 | const tan = potions.find((row) => row.potionKey === "tanningBrew"); |
| 302 | assert.ok(fat); |
| 303 | assert.equal(fat.effect?.type, StatusEffectType.foodPoisoning); |
| 304 | assert.ok(tan); |
| 305 | assert.equal(tan.outputItemId ?? null, null); |
| 306 | assert.equal(tan.effect?.type, StatusEffectType.foodPoisoning); |
| 307 | |
| 308 | const fatEval = evaluateAlchemyBottle({ |
| 309 | skills: { levels: { [SkillId.alchemy]: 1 } }, |
| 310 | brewStacks: [{ itemId: CraftingItemId.rawMeat, quantity: 1 }], |
| 311 | takenSteps: fat.steps |
| 312 | }); |
| 313 | assert.equal(fatEval.success, true); |
| 314 | assert.equal(fatEval.result.potionKey, "animalFat"); |
| 315 | |
| 316 | const fatBottle = potionItemInstance(fatEval.result); |
| 317 | assert.equal(isFullPotionBottle({ itemId: ItemId.potion, instance: fatBottle }), true); |
| 318 | |
| 319 | const tanEval = evaluateAlchemyBottle({ |
| 320 | skills: { levels: { [SkillId.alchemy]: 10 } }, |
| 321 | brewStacks: [ |
| 322 | { itemId: WoodItemId.oakBark, quantity: 2 }, |
| 323 | { itemId: CraftingItemId.salt, quantity: 1 }, |
| 324 | { itemId: ItemId.potion, quantity: 1, instance: fatBottle } |
| 325 | ], |
| 326 | takenSteps: tan.steps |
| 327 | }); |
| 328 | assert.equal(tanEval.success, true); |
| 329 | assert.equal(tanEval.result.potionKey, "tanningBrew"); |
| 330 | assert.equal(tanEval.result.outputItemId ?? null, null); |
| 331 | assert.equal(tanEval.result.effect?.type, StatusEffectType.foodPoisoning); |
| 332 | }); |
| 333 | |
| 334 | test("cauldron bottles animal fat then tanning brew as a potion", () => |
| 335 | { |
| 336 | const harness = makeSimulation({ workerCount: 1 }); |
| 337 | try |
| 338 | { |
| 339 | placeCauldron(harness.worldDb, 8, 8); |
| 340 | harness.simulation.reloadWorld(); |
| 341 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 342 | setSkillLevel(harness, character.character_id, SkillId.alchemy, 10); |
| 343 | const inventory = emptySlots(); |
| 344 | inventory[0] = { itemId: CraftingItemId.rawMeat, quantity: 1 }; |
| 345 | inventory[1] = { itemId: ItemId.glassBottle, quantity: 1 }; |
| 346 | inventory[2] = { itemId: WoodItemId.oakBark, quantity: 2 }; |
| 347 | inventory[3] = { itemId: CraftingItemId.salt, quantity: 1 }; |
| 348 | harness.stateDb.saveBag(character.character_id, { |
| 349 | inventory, |
| 350 | equipment: emptyEquipment(), |
| 351 | vault: [], |
| 352 | vaultReserved: [] |
| 353 | }); |
| 354 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 355 | const cid = character.character_id; |
| 356 | |
| 357 | harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid }); |
| 358 | harness.simulation.step(); |
| 359 | harness.simulation.enqueue(1, { |
| 360 | type: "cauldron-insert", |
| 361 | characterId: cid, |
| 362 | itemId: CraftingItemId.rawMeat, |
| 363 | quantity: 1 |
| 364 | }); |
| 365 | for (const action of [AlchemyStepKind.heat, AlchemyStepKind.stir, AlchemyStepKind.cool]) |
| 366 | { |
| 367 | harness.simulation.enqueue(1, { |
| 368 | type: "cauldron-action", |
| 369 | characterId: cid, |
| 370 | action |
| 371 | }); |
| 372 | } |
| 373 | harness.simulation.enqueue(1, { type: "cauldron-bottle", characterId: cid }); |
| 374 | harness.simulation.step(); |
| 375 | |
| 376 | let bag = harness.simulation.bagOf(entityId); |
| 377 | assert.equal(countItem(bag.inventory, CraftingItemId.rawMeat), 0); |
| 378 | assert.equal(countItem(bag.inventory, ItemId.potion), 1); |
| 379 | const fatSlot = bag.inventory.find((slot) => slot?.itemId === ItemId.potion); |
| 380 | assert.equal(fatSlot.instance.potionKey, "animalFat"); |
| 381 | assert.equal(countItem(bag.inventory, ItemId.glassBottle), 0); |
| 382 | |
| 383 | harness.simulation.enqueue(1, { type: "cauldron-clear", characterId: cid }); |
| 384 | harness.simulation.step(); |
| 385 | harness.simulation.enqueue(1, { |
| 386 | type: "cauldron-insert", |
| 387 | characterId: cid, |
| 388 | itemId: WoodItemId.oakBark, |
| 389 | quantity: 2 |
| 390 | }); |
| 391 | harness.simulation.enqueue(1, { |
| 392 | type: "cauldron-action", |
| 393 | characterId: cid, |
| 394 | action: AlchemyStepKind.heat |
| 395 | }); |
| 396 | harness.simulation.enqueue(1, { |
| 397 | type: "cauldron-insert", |
| 398 | characterId: cid, |
| 399 | itemId: CraftingItemId.salt, |
| 400 | quantity: 1 |
| 401 | }); |
| 402 | harness.simulation.enqueue(1, { |
| 403 | type: "cauldron-action", |
| 404 | characterId: cid, |
| 405 | action: AlchemyStepKind.stir |
| 406 | }); |
| 407 | harness.simulation.enqueue(1, { |
| 408 | type: "cauldron-insert", |
| 409 | characterId: cid, |
| 410 | itemId: ItemId.potion, |
| 411 | quantity: 1, |
| 412 | instance: structuredClone(fatSlot.instance) |
| 413 | }); |
| 414 | harness.simulation.enqueue(1, { |
| 415 | type: "cauldron-action", |
| 416 | characterId: cid, |
| 417 | action: AlchemyStepKind.cool |
| 418 | }); |
| 419 | harness.simulation.enqueue(1, { type: "cauldron-bottle", characterId: cid }); |
| 420 | harness.simulation.step(); |
| 421 | |
| 422 | bag = harness.simulation.bagOf(entityId); |
| 423 | const brew = bag.inventory.find((slot) => |
| 424 | slot?.itemId === ItemId.potion && slot.instance?.potionKey === "tanningBrew" |
| 425 | ); |
| 426 | assert.ok(brew); |
| 427 | assert.equal(brew.instance.fillLevel, 4); |
| 428 | assert.equal(countItem(bag.inventory, LeatherItemId.tanningBrew), 0); |
| 429 | assert.equal(countItem(bag.inventory, ItemId.glassBottle), 1); |
| 430 | assert.equal(countItem(bag.inventory, WoodItemId.oakBark), 0); |
| 431 | assert.equal(countItem(bag.inventory, CraftingItemId.salt), 0); |
| 432 | } |
| 433 | finally |
| 434 | { harness.close(); } |
| 435 | }); |