import test from "node:test"; import assert from "node:assert/strict"; import { BlockType } from "../../shared/definitions/tiles.js"; import { ItemId, itemDefinition, itemDisplayName, itemSpriteNameForStack } from "../../shared/definitions/items.js"; import { CraftingItemId } from "../../shared/definitions/craftingItems.js"; import { WoodItemId, debarkedLogInstance, isLogDebarked, barkItemIdForWood } from "../../shared/definitions/woodItems.js"; import { WoodTier, GatherMaterialId, MaterialTier, oreBoulderState } from "../../shared/definitions/materialTiers.js"; import { SkillId } from "../../shared/definitions/skills.js"; import { LeatherItemId } from "../../shared/definitions/leatherItems.js"; import { AlchemyStepKind } from "../../shared/definitions/alchemy.js"; import { canMineMaterial, oreItemIdForMaterial, woodworkingXpForDebark } from "../../shared/definitions/gathering.js"; import { clearResolvedAlchemyPotionsCache, resolvedAlchemyPotions, evaluateAlchemyBottle, potionItemInstance } from "../../shared/alchemy/recipes.js"; import { StatusEffectType } from "../../shared/statusEffects/rules.js"; import { isFullPotionBottle } from "../../shared/items/charges.js"; import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js"; import { totalXpForLevel } from "../../shared/skills/progression.js"; import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; import { addGroundItem } from "../src/items/bags.js"; import { Component } from "../src/ecs/components.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; function placeCauldron (worldDb, tileX, tileY) { const chunkX = Math.floor(tileX / 16); const chunkY = Math.floor(tileY / 16); const data = createEmptyChunkData(); data.open = true; const localX = tileX - chunkX * 16; const localY = tileY - chunkY * 16; data.blocks[localY * 16 + localX] = { typeId: BlockType.cauldron, state: 0 }; worldDb.saveChunkVersion( chunkX, chunkY, worldDb.getActiveRevisionId(), true, encodeChunkBlob(data) ); } function countItem (slots, itemId) { return (slots ?? []) .filter((slot) => slot?.itemId === itemId) .reduce((sum, slot) => sum + (slot.quantity | 0), 0); } function setSkillLevel (harness, characterId, skillId, level) { const skills = harness.stateDb.getSkills(characterId); skills.xp[skillId] = totalXpForLevel(level); harness.stateDb.saveSkills(characterId, skills); } test("rock salt mines with bronze pick and uses salt-ore boulder state", () => { assert.equal(oreBoulderState(GatherMaterialId.salt), "salt-ore"); assert.equal(oreItemIdForMaterial(GatherMaterialId.salt), CraftingItemId.salt); assert.equal(canMineMaterial(1, MaterialTier.bronze, GatherMaterialId.salt).ok, true); const def = itemDefinition(CraftingItemId.salt); assert.equal(def.key, "rock-salt"); assert.equal(def.iconKey, "ash"); assert.equal(def.tier, GatherMaterialId.salt); }); test("debarked logs keep oak itemId but change name and sprite", () => { const plain = { itemId: WoodItemId.oakLog, quantity: 1 }; const stripped = { itemId: WoodItemId.oakLog, quantity: 1, instance: debarkedLogInstance() }; assert.equal(isLogDebarked(plain), false); assert.equal(isLogDebarked(stripped), true); assert.equal(itemDisplayName(plain), "Oak log"); assert.equal(itemDisplayName(stripped), "Debarked Oak log"); assert.match(itemSpriteNameForStack(stripped), /log-debarked/); assert.equal(barkItemIdForWood(WoodTier.oak), WoodItemId.oakBark); }); test("debark Woodworking XP is half wood tier rank rounded up", () => { assert.equal(woodworkingXpForDebark(WoodTier.pine), 1); assert.equal(woodworkingXpForDebark(WoodTier.birch), 1); assert.equal(woodworkingXpForDebark(WoodTier.oak), 2); assert.equal(woodworkingXpForDebark(WoodTier.elder), 4); }); test("debark-log on ground oak log yields bark and debarked flag", () => { const harness = makeSimulation({ workerCount: 1 }); try { harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); setSkillLevel(harness, character.character_id, SkillId.woodcutting, 20); setSkillLevel(harness, character.character_id, SkillId.woodworking, 1); const inventory = emptySlots(); inventory[0] = { itemId: ItemId.ironAxe, quantity: 1 }; harness.stateDb.saveBag(character.character_id, { inventory, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); const entityId = harness.simulation.spawnCharacter(1, character); const record = harness.simulation.entityRecord(entityId); const worker = harness.simulation.workers[record.workerId]; const handle = worker.world.ecs.handleOf(entityId); const woodworkingXpBefore = worker.world.ecs.get(handle, Component.skills).xp[SkillId.woodworking] | 0; const groundItemId = (worker.world.nextGroundItemId = (worker.world.nextGroundItemId ?? 1) + 1); addGroundItem(worker.world, { groundItemId, tileX: 8, tileY: 9, stack: { itemId: WoodItemId.oakLog, quantity: 1 }, droppedAt: Date.now(), expiresAt: Date.now() + 600_000, source: "test" }); harness.simulation.enqueue(1, { type: "debark-log", characterId: character.character_id, groundItemId }); harness.simulation.step(); const gather = worker.world.ecs.get(handle, Component.gather); assert.equal(gather?.kind, "debark"); assert.equal(gather?.durationMs, 2000); for (let i = 0; i < 50; i++) { harness.advance(50); harness.simulation.step(); } const item = worker.world.groundItems.get(groundItemId); assert.ok(item); assert.equal(isLogDebarked(item.stack), true); assert.equal(item.stack.itemId, WoodItemId.oakLog); const bark = [...worker.world.groundItems.values()] .find((row) => row.stack?.itemId === WoodItemId.oakBark); assert.ok(bark); assert.equal(bark.stack.quantity, 1); assert.equal(worker.world.ecs.has(handle, Component.gather), false); const woodworkingXpAfter = worker.world.ecs.get(handle, Component.skills).xp[SkillId.woodworking] | 0; assert.equal(woodworkingXpAfter - woodworkingXpBefore, woodworkingXpForDebark(WoodTier.oak)); } finally { harness.close(); } }); test("debark-log on a ground log stack strips only one log", () => { const harness = makeSimulation({ workerCount: 1 }); try { harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); setSkillLevel(harness, character.character_id, SkillId.woodcutting, 20); const inventory = emptySlots(); inventory[0] = { itemId: ItemId.ironAxe, quantity: 1 }; harness.stateDb.saveBag(character.character_id, { inventory, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); const entityId = harness.simulation.spawnCharacter(1, character); const record = harness.simulation.entityRecord(entityId); const worker = harness.simulation.workers[record.workerId]; const groundItemId = (worker.world.nextGroundItemId = (worker.world.nextGroundItemId ?? 1) + 1); addGroundItem(worker.world, { groundItemId, tileX: 8, tileY: 9, stack: { itemId: WoodItemId.oakLog, quantity: 4 }, droppedAt: Date.now(), expiresAt: Date.now() + 600_000, source: "test" }); harness.simulation.enqueue(1, { type: "debark-log", characterId: character.character_id, groundItemId }); harness.simulation.step(); for (let i = 0; i < 50; i++) { harness.advance(50); harness.simulation.step(); } const remain = worker.world.groundItems.get(groundItemId); assert.ok(remain); assert.equal(remain.stack.quantity, 3); assert.equal(isLogDebarked(remain.stack), false); const debarked = [...worker.world.groundItems.values()] .find((row) => row.groundItemId !== groundItemId && row.stack?.itemId === WoodItemId.oakLog && isLogDebarked(row.stack)); assert.ok(debarked); assert.equal(debarked.stack.quantity, 1); const bark = [...worker.world.groundItems.values()] .find((row) => row.stack?.itemId === WoodItemId.oakBark); assert.ok(bark); assert.equal(bark.stack.quantity, 1); } finally { harness.close(); } }); test("debarking a ground log stack merges all debarked logs into one pile", () => { const harness = makeSimulation({ workerCount: 1 }); try { harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); setSkillLevel(harness, character.character_id, SkillId.woodcutting, 20); const inventory = emptySlots(); inventory[0] = { itemId: ItemId.ironAxe, quantity: 1 }; harness.stateDb.saveBag(character.character_id, { inventory, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); const entityId = harness.simulation.spawnCharacter(1, character); const record = harness.simulation.entityRecord(entityId); const worker = harness.simulation.workers[record.workerId]; const groundItemId = (worker.world.nextGroundItemId = (worker.world.nextGroundItemId ?? 1) + 1); addGroundItem(worker.world, { groundItemId, tileX: 8, tileY: 9, stack: { itemId: WoodItemId.oakLog, quantity: 3 }, droppedAt: Date.now(), expiresAt: Date.now() + 600_000, source: "test" }); for (let n = 0; n < 3; n++) { const plain = [...worker.world.groundItems.values()] .find((row) => row.stack?.itemId === WoodItemId.oakLog && !isLogDebarked(row.stack)); assert.ok(plain, `plain log pile before debark ${n + 1}`); harness.simulation.enqueue(1, { type: "debark-log", characterId: character.character_id, groundItemId: plain.groundItemId }); harness.simulation.step(); for (let i = 0; i < 50; i++) { harness.advance(50); harness.simulation.step(); } } const plainLeft = [...worker.world.groundItems.values()] .filter((row) => row.stack?.itemId === WoodItemId.oakLog && !isLogDebarked(row.stack)); assert.equal(plainLeft.length, 0); const debarkedPiles = [...worker.world.groundItems.values()] .filter((row) => row.stack?.itemId === WoodItemId.oakLog && isLogDebarked(row.stack)); assert.equal(debarkedPiles.length, 1); assert.equal(debarkedPiles[0].stack.quantity, 3); const bark = [...worker.world.groundItems.values()] .find((row) => row.stack?.itemId === WoodItemId.oakBark); assert.ok(bark); assert.equal(bark.stack.quantity, 3); } finally { harness.close(); } }); test("animal fat and tanning brew alchemy recipes resolve", () => { clearResolvedAlchemyPotionsCache(); const potions = resolvedAlchemyPotions(); const fat = potions.find((row) => row.potionKey === "animalFat"); const tan = potions.find((row) => row.potionKey === "tanningBrew"); assert.ok(fat); assert.equal(fat.effect?.type, StatusEffectType.foodPoisoning); assert.ok(tan); assert.equal(tan.outputItemId ?? null, null); assert.equal(tan.effect?.type, StatusEffectType.foodPoisoning); const fatEval = evaluateAlchemyBottle({ skills: { levels: { [SkillId.alchemy]: 1 } }, brewStacks: [{ itemId: CraftingItemId.rawMeat, quantity: 1 }], takenSteps: fat.steps }); assert.equal(fatEval.success, true); assert.equal(fatEval.result.potionKey, "animalFat"); const fatBottle = potionItemInstance(fatEval.result); assert.equal(isFullPotionBottle({ itemId: ItemId.potion, instance: fatBottle }), true); const tanEval = evaluateAlchemyBottle({ skills: { levels: { [SkillId.alchemy]: 10 } }, brewStacks: [ { itemId: WoodItemId.oakBark, quantity: 2 }, { itemId: CraftingItemId.salt, quantity: 1 }, { itemId: ItemId.potion, quantity: 1, instance: fatBottle } ], takenSteps: tan.steps }); assert.equal(tanEval.success, true); assert.equal(tanEval.result.potionKey, "tanningBrew"); assert.equal(tanEval.result.outputItemId ?? null, null); assert.equal(tanEval.result.effect?.type, StatusEffectType.foodPoisoning); }); test("cauldron bottles animal fat then tanning brew as a potion", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeCauldron(harness.worldDb, 8, 8); harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); setSkillLevel(harness, character.character_id, SkillId.alchemy, 10); const inventory = emptySlots(); inventory[0] = { itemId: CraftingItemId.rawMeat, quantity: 1 }; inventory[1] = { itemId: ItemId.glassBottle, quantity: 1 }; inventory[2] = { itemId: WoodItemId.oakBark, quantity: 2 }; inventory[3] = { itemId: CraftingItemId.salt, quantity: 1 }; harness.stateDb.saveBag(character.character_id, { inventory, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); const entityId = harness.simulation.spawnCharacter(1, character); const cid = character.character_id; harness.simulation.enqueue(1, { type: "open-cauldron", characterId: cid }); harness.simulation.step(); harness.simulation.enqueue(1, { type: "cauldron-insert", characterId: cid, itemId: CraftingItemId.rawMeat, quantity: 1 }); for (const action of [AlchemyStepKind.heat, AlchemyStepKind.stir, AlchemyStepKind.cool]) { harness.simulation.enqueue(1, { type: "cauldron-action", characterId: cid, action }); } harness.simulation.enqueue(1, { type: "cauldron-bottle", characterId: cid }); harness.simulation.step(); let bag = harness.simulation.bagOf(entityId); assert.equal(countItem(bag.inventory, CraftingItemId.rawMeat), 0); assert.equal(countItem(bag.inventory, ItemId.potion), 1); const fatSlot = bag.inventory.find((slot) => slot?.itemId === ItemId.potion); assert.equal(fatSlot.instance.potionKey, "animalFat"); assert.equal(countItem(bag.inventory, ItemId.glassBottle), 0); harness.simulation.enqueue(1, { type: "cauldron-clear", characterId: cid }); harness.simulation.step(); harness.simulation.enqueue(1, { type: "cauldron-insert", characterId: cid, itemId: WoodItemId.oakBark, quantity: 2 }); harness.simulation.enqueue(1, { type: "cauldron-action", characterId: cid, action: AlchemyStepKind.heat }); harness.simulation.enqueue(1, { type: "cauldron-insert", characterId: cid, itemId: CraftingItemId.salt, quantity: 1 }); harness.simulation.enqueue(1, { type: "cauldron-action", characterId: cid, action: AlchemyStepKind.stir }); harness.simulation.enqueue(1, { type: "cauldron-insert", characterId: cid, itemId: ItemId.potion, quantity: 1, instance: structuredClone(fatSlot.instance) }); harness.simulation.enqueue(1, { type: "cauldron-action", characterId: cid, action: AlchemyStepKind.cool }); harness.simulation.enqueue(1, { type: "cauldron-bottle", characterId: cid }); harness.simulation.step(); bag = harness.simulation.bagOf(entityId); const brew = bag.inventory.find((slot) => slot?.itemId === ItemId.potion && slot.instance?.potionKey === "tanningBrew" ); assert.ok(brew); assert.equal(brew.instance.fillLevel, 4); assert.equal(countItem(bag.inventory, LeatherItemId.tanningBrew), 0); assert.equal(countItem(bag.inventory, ItemId.glassBottle), 1); assert.equal(countItem(bag.inventory, WoodItemId.oakBark), 0); assert.equal(countItem(bag.inventory, CraftingItemId.salt), 0); } finally { harness.close(); } });